make code more stable (still has crash bug).
This commit is contained in:
Binary file not shown.
@@ -17,6 +17,17 @@ class MonitorManager;
|
|||||||
const std::unique_ptr<MonitorManager>& GetMonitorManager();
|
const std::unique_ptr<MonitorManager>& GetMonitorManager();
|
||||||
|
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
auto MakeUniqueWithReleaser(T* ptr)
|
||||||
|
{
|
||||||
|
const auto deleter = [](T* ptr)
|
||||||
|
{
|
||||||
|
if (ptr != nullptr) ptr->Release();
|
||||||
|
};
|
||||||
|
return std::unique_ptr<T, decltype(deleter)>(ptr, deleter);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Message is pooled and fetch from Unity.
|
// Message is pooled and fetch from Unity.
|
||||||
enum class Message
|
enum class Message
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -58,12 +58,12 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
|
|||||||
// Get mouse pointer information
|
// Get mouse pointer information
|
||||||
UINT bufferSize;
|
UINT bufferSize;
|
||||||
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo;
|
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo;
|
||||||
if (FAILED(
|
const auto hr = monitor_->GetDeskDupl()->GetFramePointerShape(
|
||||||
monitor_->GetDeskDupl()->GetFramePointerShape(
|
apiBufferSize_,
|
||||||
apiBufferSize_,
|
reinterpret_cast<void*>(apiBuffer_.get()),
|
||||||
reinterpret_cast<void*>(apiBuffer_.get()),
|
&bufferSize,
|
||||||
&bufferSize,
|
&shapeInfo);
|
||||||
&shapeInfo)))
|
if (FAILED(hr))
|
||||||
{
|
{
|
||||||
Debug::Error("Cursor::UpdateBuffer() => GetFramePointerShape() failed.");
|
Debug::Error("Cursor::UpdateBuffer() => GetFramePointerShape() failed.");
|
||||||
apiBuffer_.reset();
|
apiBuffer_.reset();
|
||||||
@@ -149,12 +149,13 @@ void Cursor::UpdateTexture()
|
|||||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||||
desc.MiscFlags = 0;
|
desc.MiscFlags = 0;
|
||||||
|
|
||||||
ComPtr<ID3D11Texture2D> texture;
|
ID3D11Texture2D* texture;
|
||||||
if (FAILED(GetDevice()->CreateTexture2D(&desc, nullptr, &texture)))
|
if (FAILED(GetDevice()->CreateTexture2D(&desc, nullptr, &texture)))
|
||||||
{
|
{
|
||||||
Debug::Error("Cursor::UpdateTexture() => GetDevice()->CreateTexture2D() failed.");
|
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;
|
||||||
@@ -170,16 +171,18 @@ void Cursor::UpdateTexture()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ComPtr<ID3D11DeviceContext> context;
|
ID3D11DeviceContext* context;
|
||||||
GetDevice()->GetImmediateContext(&context);
|
GetDevice()->GetImmediateContext(&context);
|
||||||
context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box);
|
context->CopySubresourceRegion(texture, 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box);
|
||||||
|
context->Release();
|
||||||
|
|
||||||
ComPtr<IDXGISurface> surface;
|
IDXGISurface* surface;
|
||||||
if (FAILED(texture.As<IDXGISurface>(&surface)))
|
if (FAILED(texture->QueryInterface<IDXGISurface>(&surface)))
|
||||||
{
|
{
|
||||||
Debug::Error("Cursor::UpdateTexture() => texture->QueryInterface() failed.");
|
Debug::Error("Cursor::UpdateTexture() => texture->QueryInterface() failed.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const auto surfaceReleaser = MakeUniqueWithReleaser(surface);
|
||||||
|
|
||||||
DXGI_MAPPED_RECT mappedSurface;
|
DXGI_MAPPED_RECT mappedSurface;
|
||||||
if (FAILED(surface->Map(&mappedSurface, DXGI_MAP_READ)))
|
if (FAILED(surface->Map(&mappedSurface, DXGI_MAP_READ)))
|
||||||
@@ -279,9 +282,10 @@ void Cursor::GetTexture(ID3D11Texture2D* texture)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ComPtr<ID3D11DeviceContext> context;
|
ID3D11DeviceContext* context;
|
||||||
GetDevice()->GetImmediateContext(&context);
|
GetDevice()->GetImmediateContext(&context);
|
||||||
context->UpdateSubresource(texture, 0, nullptr, bgra32Buffer_.get(), GetWidth() * 4, 0);
|
context->UpdateSubresource(texture, 0, nullptr, bgra32Buffer_.get(), GetWidth() * 4, 0);
|
||||||
|
context->Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,10 @@ Monitor::Monitor(int id)
|
|||||||
|
|
||||||
Monitor::~Monitor()
|
Monitor::~Monitor()
|
||||||
{
|
{
|
||||||
deskDupl_->Release();
|
if (deskDupl_)
|
||||||
|
{
|
||||||
|
deskDupl_->Release();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -110,7 +113,7 @@ void Monitor::Render(UINT timeout)
|
|||||||
{
|
{
|
||||||
if (!deskDupl_) return;
|
if (!deskDupl_) return;
|
||||||
|
|
||||||
ComPtr<IDXGIResource> resource;
|
IDXGIResource* resource;
|
||||||
DXGI_OUTDUPL_FRAME_INFO frameInfo;
|
DXGI_OUTDUPL_FRAME_INFO frameInfo;
|
||||||
|
|
||||||
const auto hr = deskDupl_->AcquireNextFrame(timeout, &frameInfo, &resource);
|
const auto hr = deskDupl_->AcquireNextFrame(timeout, &frameInfo, &resource);
|
||||||
@@ -154,8 +157,8 @@ void Monitor::Render(UINT timeout)
|
|||||||
|
|
||||||
if (unityTexture_)
|
if (unityTexture_)
|
||||||
{
|
{
|
||||||
ComPtr<ID3D11Texture2D> texture;
|
ID3D11Texture2D* texture;
|
||||||
if (FAILED(resource.As<ID3D11Texture2D>(&texture)))
|
if (FAILED(resource->QueryInterface<ID3D11Texture2D>(&texture)))
|
||||||
{
|
{
|
||||||
Debug::Error("Monitor::Render() => resource.As() failed.");
|
Debug::Error("Monitor::Render() => resource.As() failed.");
|
||||||
return;
|
return;
|
||||||
@@ -178,9 +181,10 @@ void Monitor::Render(UINT timeout)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ComPtr<ID3D11DeviceContext> context;
|
ID3D11DeviceContext* context;
|
||||||
GetDevice()->GetImmediateContext(&context);
|
GetDevice()->GetImmediateContext(&context);
|
||||||
context->CopyResource(unityTexture_, texture.Get());
|
context->CopyResource(unityTexture_, texture);
|
||||||
|
context->Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,7 +222,7 @@ ID3D11Texture2D* Monitor::GetUnityTexture() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const ComPtr<IDXGIOutputDuplication>& Monitor::GetDeskDupl()
|
IDXGIOutputDuplication* Monitor::GetDeskDupl()
|
||||||
{
|
{
|
||||||
return deskDupl_;
|
return deskDupl_;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public:
|
|||||||
int GetRotation() const;
|
int GetRotation() const;
|
||||||
int GetDpiX() const;
|
int GetDpiX() const;
|
||||||
int GetDpiY() const;
|
int GetDpiY() const;
|
||||||
const Microsoft::WRL::ComPtr<IDXGIOutputDuplication>& GetDeskDupl();
|
IDXGIOutputDuplication* GetDeskDupl();
|
||||||
const std::unique_ptr<Cursor>& GetCursor();
|
const std::unique_ptr<Cursor>& GetCursor();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -57,7 +57,7 @@ private:
|
|||||||
int width_ = -1, height_ = -1;
|
int width_ = -1, height_ = -1;
|
||||||
State state_ = State::NotSet;
|
State state_ = State::NotSet;
|
||||||
std::unique_ptr<Cursor> cursor_;
|
std::unique_ptr<Cursor> cursor_;
|
||||||
Microsoft::WRL::ComPtr<IDXGIOutputDuplication> deskDupl_;
|
IDXGIOutputDuplication* deskDupl_ = nullptr;
|
||||||
ID3D11Texture2D* unityTexture_ = nullptr;
|
ID3D11Texture2D* unityTexture_ = nullptr;
|
||||||
DXGI_OUTPUT_DESC outputDesc_;
|
DXGI_OUTPUT_DESC outputDesc_;
|
||||||
MONITORINFOEX monitorInfo_;
|
MONITORINFOEX monitorInfo_;
|
||||||
|
|||||||
Reference in New Issue
Block a user