add debug logs and use safer code.
This commit is contained in:
Binary file not shown.
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
|
||||||
// Unity interface and ID3D11Device getters
|
// Unity interface and ID3D11Device getters
|
||||||
@@ -12,6 +13,15 @@ struct ID3D11Device;
|
|||||||
ID3D11Device* GetDevice();
|
ID3D11Device* GetDevice();
|
||||||
|
|
||||||
|
|
||||||
|
// Utility
|
||||||
|
template <class T>
|
||||||
|
auto MakeUniqueWithReleaser(T* ptr)
|
||||||
|
{
|
||||||
|
const auto deleter = [](T* ptr) { ptr->Release(); };
|
||||||
|
return std::unique_ptr<T, decltype(deleter)>(ptr, deleter);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Manager getter
|
// Manager getter
|
||||||
class MonitorManager;
|
class MonitorManager;
|
||||||
const std::unique_ptr<MonitorManager>& GetMonitorManager();
|
const std::unique_ptr<MonitorManager>& GetMonitorManager();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <d3d11.h>
|
#include <d3d11.h>
|
||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
|
#include "Debug.h"
|
||||||
#include "MonitorManager.h"
|
#include "MonitorManager.h"
|
||||||
#include "Monitor.h"
|
#include "Monitor.h"
|
||||||
#include "Cursor.h"
|
#include "Cursor.h"
|
||||||
@@ -53,8 +54,10 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
|
|||||||
reinterpret_cast<void*>(apiBuffer_.get()),
|
reinterpret_cast<void*>(apiBuffer_.get()),
|
||||||
&bufferSize,
|
&bufferSize,
|
||||||
&shapeInfo_);
|
&shapeInfo_);
|
||||||
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr))
|
||||||
{
|
{
|
||||||
|
Debug::Error("Cursor::UpdateBuffer() => GetFramePointerShape() failed.");
|
||||||
apiBuffer_.reset();
|
apiBuffer_.reset();
|
||||||
apiBufferSize_ = 0;
|
apiBufferSize_ = 0;
|
||||||
}
|
}
|
||||||
@@ -103,8 +106,10 @@ void Cursor::UpdateTexture()
|
|||||||
hr = GetDevice()->CreateTexture2D(&desc, nullptr, &texture);
|
hr = GetDevice()->CreateTexture2D(&desc, nullptr, &texture);
|
||||||
if (FAILED(hr))
|
if (FAILED(hr))
|
||||||
{
|
{
|
||||||
|
Debug::Error("Cursor::UpdateTexture() => GetDevice()->CreateTexture2D() failed.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const auto textureReleaser = MakeUniqueWithReleaser(texture);
|
||||||
|
|
||||||
D3D11_BOX box;
|
D3D11_BOX box;
|
||||||
box.front = 0;
|
box.front = 0;
|
||||||
@@ -114,6 +119,24 @@ void Cursor::UpdateTexture()
|
|||||||
box.right = x_ + w;
|
box.right = x_ + w;
|
||||||
box.bottom = y_ + h;
|
box.bottom = y_ + h;
|
||||||
|
|
||||||
|
const UINT mw = monitor_->GetWidth();
|
||||||
|
const UINT mh = monitor_->GetHeight();
|
||||||
|
if (box.left < 0 || box.top < 0 || box.right >= mw || box.bottom >= mh)
|
||||||
|
{
|
||||||
|
char buf[256];
|
||||||
|
sprintf_s(buf, 256,
|
||||||
|
"Cursor::UpdateTexture() => D3D11_BOX is out of area (%d, %d) ~ (%d, %d).",
|
||||||
|
box.left, box.top, box.right, box.bottom);
|
||||||
|
Debug::Error(buf);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (monitor_->GetUnityTexture() == nullptr)
|
||||||
|
{
|
||||||
|
Debug::Error("Cursor::UpdateTexture() => Monitor::GetUnityTexture() is null.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ID3D11DeviceContext* context;
|
ID3D11DeviceContext* context;
|
||||||
GetDevice()->GetImmediateContext(&context);
|
GetDevice()->GetImmediateContext(&context);
|
||||||
context->CopySubresourceRegion(texture, 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box);
|
context->CopySubresourceRegion(texture, 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box);
|
||||||
@@ -121,17 +144,18 @@ void Cursor::UpdateTexture()
|
|||||||
|
|
||||||
IDXGISurface* surface = nullptr;
|
IDXGISurface* surface = nullptr;
|
||||||
hr = texture->QueryInterface(__uuidof(IDXGISurface), (void**)&surface);
|
hr = texture->QueryInterface(__uuidof(IDXGISurface), (void**)&surface);
|
||||||
texture->Release();
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr))
|
||||||
{
|
{
|
||||||
|
Debug::Error("Cursor::UpdateTexture() => texture->QueryInterface() failed.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const auto surfaceReleaser = MakeUniqueWithReleaser(surface);
|
||||||
|
|
||||||
DXGI_MAPPED_RECT mappedSurface;
|
DXGI_MAPPED_RECT mappedSurface;
|
||||||
hr = surface->Map(&mappedSurface, DXGI_MAP_READ);
|
hr = surface->Map(&mappedSurface, DXGI_MAP_READ);
|
||||||
if (FAILED(hr))
|
if (FAILED(hr))
|
||||||
{
|
{
|
||||||
surface->Release();
|
Debug::Error("Cursor::UpdateTexture() => surface->Map() failed.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +208,6 @@ void Cursor::UpdateTexture()
|
|||||||
}
|
}
|
||||||
|
|
||||||
hr = surface->Unmap();
|
hr = surface->Unmap();
|
||||||
surface->Release();
|
|
||||||
if (FAILED(hr))
|
if (FAILED(hr))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -206,10 +229,34 @@ void Cursor::UpdateTexture()
|
|||||||
|
|
||||||
void Cursor::GetTexture(ID3D11Texture2D* texture)
|
void Cursor::GetTexture(ID3D11Texture2D* texture)
|
||||||
{
|
{
|
||||||
if (bgra32Buffer_ == nullptr || texture == nullptr) return;
|
if (bgra32Buffer_ == nullptr)
|
||||||
|
{
|
||||||
|
Debug::Error("Cursor::GetTexture() => bgra32Buffer is null.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (texture == nullptr)
|
||||||
|
{
|
||||||
|
Debug::Error("Cursor::GetTexture() => The given texture is null.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
D3D11_TEXTURE2D_DESC desc;
|
||||||
|
texture->GetDesc(&desc);
|
||||||
|
if ((int)desc.Width < GetWidth() || (int)desc.Height < GetHeight())
|
||||||
|
{
|
||||||
|
char buf[256];
|
||||||
|
sprintf_s(buf, 256,
|
||||||
|
"Cursor::GetTexture() => The given texture has smaller width / height.\n"
|
||||||
|
"Given => (%d, %d) Buffer => (%d, %d)",
|
||||||
|
desc.Width, desc.Height, GetWidth(), GetHeight());
|
||||||
|
Debug::Error(buf);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ID3D11DeviceContext* context;
|
ID3D11DeviceContext* context;
|
||||||
GetDevice()->GetImmediateContext(&context);
|
GetDevice()->GetImmediateContext(&context);
|
||||||
context->UpdateSubresource(texture, 0, nullptr, bgra32Buffer_.get(), shapeInfo_.Width * 4, 0);
|
context->UpdateSubresource(texture, 0, nullptr, bgra32Buffer_.get(), GetWidth() * 4, 0);
|
||||||
context->Release();
|
context->Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ void Monitor::Render(UINT timeout)
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const auto resourceReleaser = MakeUniqueWithReleaser(resource);
|
||||||
|
|
||||||
if (unityTexture_)
|
if (unityTexture_)
|
||||||
{
|
{
|
||||||
@@ -108,8 +109,6 @@ void Monitor::Render(UINT timeout)
|
|||||||
GetDevice()->GetImmediateContext(&context);
|
GetDevice()->GetImmediateContext(&context);
|
||||||
context->CopyResource(unityTexture_, texture);
|
context->CopyResource(unityTexture_, texture);
|
||||||
context->Release();
|
context->Release();
|
||||||
|
|
||||||
resource->Release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cursor_->UpdateBuffer(frameInfo);
|
cursor_->UpdateBuffer(frameInfo);
|
||||||
|
|||||||
@@ -32,24 +32,24 @@ void MonitorManager::Initialize()
|
|||||||
// Get factory
|
// Get factory
|
||||||
IDXGIFactory1* factory;
|
IDXGIFactory1* factory;
|
||||||
CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&factory));
|
CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&factory));
|
||||||
|
const auto factoryReleaser = MakeUniqueWithReleaser(factory);
|
||||||
|
|
||||||
// Check all display adapters
|
// Check all display adapters
|
||||||
int id = 0;
|
int id = 0;
|
||||||
IDXGIAdapter1* adapter;
|
IDXGIAdapter1* adapter;
|
||||||
for (int i = 0; (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND); ++i)
|
for (int i = 0; (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND); ++i)
|
||||||
{
|
{
|
||||||
|
const auto adapterReleaser = MakeUniqueWithReleaser(adapter);
|
||||||
// Search the main monitor from all outputs
|
// Search the main monitor from all outputs
|
||||||
IDXGIOutput* output;
|
IDXGIOutput* output;
|
||||||
for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j)
|
for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j)
|
||||||
{
|
{
|
||||||
|
const auto outputReleaser = MakeUniqueWithReleaser(output);
|
||||||
auto monitor = std::make_shared<Monitor>(id++);
|
auto monitor = std::make_shared<Monitor>(id++);
|
||||||
monitor->Initialize(output);
|
monitor->Initialize(output);
|
||||||
monitors_.push_back(monitor);
|
monitors_.push_back(monitor);
|
||||||
output->Release();
|
|
||||||
}
|
}
|
||||||
adapter->Release();
|
|
||||||
}
|
}
|
||||||
factory->Release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user