From 0a38eda14f1ee1ca10eb3ffb1037ed54b85f17ab Mon Sep 17 00:00:00 2001 From: hecomi Date: Sat, 5 Nov 2016 22:36:08 +0900 Subject: [PATCH] update files. --- .../uDesktopDuplication/Monitor.cpp | 33 ++-- .../uDesktopDuplication/Monitor.h | 5 +- .../uDesktopDuplication/MonitorManager.cpp | 184 +----------------- .../uDesktopDuplication/MonitorManager.h | 19 +- .../uDesktopDuplication/main.cpp | 142 ++++++++++---- 5 files changed, 139 insertions(+), 244 deletions(-) diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp index a7c6ac1..cde2cef 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.cpp @@ -5,8 +5,13 @@ #include "Monitor.h" -Monitor::Monitor(int id, IDXGIOutput* output) +Monitor::Monitor(int id) : id_(id) + , cursor_(std::make_unique(this)) +{ +} + +HRESULT Monitor::Initialize(IDXGIOutput* output) { output->GetDesc(&outputDesc_); monitorInfo_.cbSize = sizeof(MONITORINFOEX); @@ -15,36 +20,31 @@ Monitor::Monitor(int id, IDXGIOutput* output) auto output1 = reinterpret_cast(output); const auto hr = output1->DuplicateOutput(GetDevice(), &deskDupl_); - auto error = 0; - // TODO: error check switch (hr) { case S_OK: + available_ = true; break; case E_INVALIDARG: - error = 1; break; case E_ACCESSDENIED: - error = 2; // For example, when the user presses Ctrl + Alt + Delete and the screen // switches to admin screen, this error occurs. + // GetMonitorManager()->RequireReinitilization(); break; case DXGI_ERROR_UNSUPPORTED: - error = 3; + // If the display adapter on the computer is running under the Microsoft Hybrid system, + // this error occurs. break; case DXGI_ERROR_NOT_CURRENTLY_AVAILABLE: - error = 4; - // Other application use Desktop Duplication API... + // When other application use Desktop Duplication API, this error occurs. break; case DXGI_ERROR_SESSION_DISCONNECTED: - error = 5; break; } - cursor_ = std::make_unique(this); - - // MEMO: 'output' will be released automatically after this ctor. + return hr; } @@ -61,7 +61,6 @@ HRESULT Monitor::Render(UINT timeout) { if (deskDupl_ == nullptr) { - GetMonitorManager()->RequireReinitilization(); return 0; } @@ -89,7 +88,7 @@ HRESULT Monitor::Render(UINT timeout) resource->Release(); deskDupl_->ReleaseFrame(); - return 0; + return S_OK; } @@ -99,6 +98,12 @@ int Monitor::GetId() const } +bool Monitor::IsAvailable() const +{ + return available_; +} + + void Monitor::SetUnityTexture(ID3D11Texture2D* texture) { unityTexture_ = texture; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h index 92168df..1cee5a6 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/Monitor.h @@ -7,13 +7,15 @@ class Cursor; class Monitor { public: - Monitor(int id, IDXGIOutput* output); + explicit Monitor(int id); ~Monitor(); + HRESULT Initialize(IDXGIOutput* output); HRESULT Render(UINT timeout = 0); void UpdateCursorTexture(ID3D11Texture2D* texture); public: int GetId() const; + bool IsAvailable() const; void SetUnityTexture(ID3D11Texture2D* texture); ID3D11Texture2D* GetUnityTexture() const; void GetName(char* buf, int len) const; @@ -30,6 +32,7 @@ public: private: int id_ = -1; + bool available_ = false; std::unique_ptr cursor_; IDXGIOutputDuplication* deskDupl_ = nullptr; ID3D11Texture2D* unityTexture_ = nullptr; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.cpp index b898958..940edb5 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.cpp @@ -42,7 +42,8 @@ void MonitorManager::Initialize() IDXGIOutput* output; for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j) { - auto monitor = std::make_shared(id++, output); + auto monitor = std::make_shared(id++); + monitor->Initialize(output); monitors_.push_back(monitor); output->Release(); } @@ -89,9 +90,12 @@ void MonitorManager::OnRender(int id) { if (auto monitor = GetMonitor(id)) { + if (!monitor->IsAvailable()) return; + + const auto hr = monitor->Render(timeout_); + // If any monitor setting has changed (e.g. monitor size has changed), // it is necessary to re-initialize monitors. - const auto hr = monitor->Render(timeout_); if (hr == DXGI_ERROR_ACCESS_LOST) { isReinitializationRequired_ = true; @@ -100,28 +104,15 @@ void MonitorManager::OnRender(int id) } -void MonitorManager::UpdateCursorTexture(int id, ID3D11Texture2D* texture) -{ - if (auto monitor = GetMonitor(id)) - { - monitor->UpdateCursorTexture(texture); - } -} - - void MonitorManager::SetTimeout(int timeout) { timeout_ = timeout; } -void MonitorManager::SetTexturePtr(int id, void* texture) +int MonitorManager::GetTimeout() const { - if (auto monitor = GetMonitor(id)) - { - auto d3d11Texture = reinterpret_cast(texture); - monitor->SetUnityTexture(d3d11Texture); - } + return timeout_; } @@ -154,163 +145,4 @@ int MonitorManager::GetTotalHeight() const const auto minTop = *std::min_element(tops.begin(), tops.end()); const auto maxBottom = *std::max_element(bottoms.begin(), bottoms.end()); return maxBottom - minTop; -} - - -void MonitorManager::GetName(int id, char* buf, int len) const -{ - if (auto monitor = GetMonitor(id)) - { - monitor->GetName(buf, len); - } -} - - -bool MonitorManager::IsPrimary(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->IsPrimary(); - } - return false; -} - - -int MonitorManager::GetLeft(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetLeft(); - } - return 0; -} - - -int MonitorManager::GetRight(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetRight(); - } - return 0; -} - - -int MonitorManager::GetTop(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetTop(); - } - return 0; -} - - -int MonitorManager::GetBottom(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetBottom(); - } - return 0; -} - - -int MonitorManager::GetWidth(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetWidth(); - } - return -1; -} - - -int MonitorManager::GetHeight(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetHeight(); - } - return -1; -} - - -int MonitorManager::GetRotation(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetRotation(); - } - return DXGI_MODE_ROTATION_UNSPECIFIED; -} - - -bool MonitorManager::IsCursorVisible(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetCursor()->IsVisible(); - } - return false; -} - - -int MonitorManager::GetCursorX(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetCursor()->GetX(); - } - return -1; -} - - -int MonitorManager::GetCursorY(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetCursor()->GetY(); - } - return -1; -} - - -int MonitorManager::GetCursorShapeWidth(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetCursor()->GetWidth(); - } - return -1; -} - - -int MonitorManager::GetCursorShapeHeight(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetCursor()->GetHeight(); - } - return -1; -} - - -int MonitorManager::GetCursorShapePitch(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetCursor()->GetPitch(); - } - return -1; -} - - -int MonitorManager::GetCursorShapeType(int id) const -{ - if (auto monitor = GetMonitor(id)) - { - return monitor->GetCursor()->GetType(); - } - return -1; } \ No newline at end of file diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.h b/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.h index f0769ce..c4d5d6e 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.h +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/MonitorManager.h @@ -29,31 +29,14 @@ private: public: void OnRender(int id); void Update(); - void UpdateCursorTexture(int id, ID3D11Texture2D* ptr); void SetTimeout(int timeout); - void SetTexturePtr(int id, void* texture); + int GetTimeout() const; // Getters from Unity public: int GetMonitorCount() const; int GetTotalWidth() const; int GetTotalHeight() const; - void GetName(int id, char* buf, int len) const; - bool IsPrimary(int id) const; - int GetLeft(int id) const; - int GetRight(int id) const; - int GetTop(int id) const; - int GetBottom(int id) const; - int GetWidth(int id) const; - int GetHeight(int id) const; - int GetRotation(int id) const; - bool IsCursorVisible(int id) const; - int GetCursorX(int id) const; - int GetCursorY(int id) const; - int GetCursorShapeWidth(int id) const; - int GetCursorShapeHeight(int id) const; - int GetCursorShapePitch(int id) const; - int GetCursorShapeType(int id) const; private: int timeout_ = 10; diff --git a/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp b/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp index 0201dc3..5682225 100644 --- a/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp +++ b/Plugins/uDesktopDuplication/uDesktopDuplication/main.cpp @@ -10,6 +10,8 @@ #include "IUnityGraphicsD3D11.h" #include "Common.h" +#include "Monitor.h" +#include "Cursor.h" #include "MonitorManager.h" #pragma comment(lib, "dxgi.lib") @@ -127,108 +129,178 @@ extern "C" UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetName(int id, char* buf, int len) { if (!g_manager) return; - g_manager->GetName(id, buf, len); + if (auto monitor = g_manager->GetMonitor(id)) + { + monitor->GetName(buf, len); + } } UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsPrimary(int id) { if (!g_manager) return false; - return g_manager->IsPrimary(id); + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->IsPrimary(); + } + return false; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetLeft(int id) { - if (!g_manager) return -1; - return g_manager->GetLeft(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetLeft(); + } + return -1; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetRight(int id) { - if (!g_manager) return -1; - return g_manager->GetRight(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetRight(); + } + return 0; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetTop(int id) { - if (!g_manager) return -1; - return g_manager->GetTop(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetTop(); + } + return 0; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetBottom(int id) { - if (!g_manager) return -1; - return g_manager->GetBottom(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetBottom(); + } + return 0; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetWidth(int id) { - if (!g_manager) return -1; - return g_manager->GetWidth(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetWidth(); + } + return -1; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetHeight(int id) { - if (!g_manager) return -1; - return g_manager->GetHeight(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetHeight(); + } + return -1; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetRotation(int id) { - if (!g_manager) return -1; - return g_manager->GetRotation(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetRotation(); + } + return DXGI_MODE_ROTATION_UNSPECIFIED; } UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsCursorVisible(int id) { - if (!g_manager) return false; - return g_manager->IsCursorVisible(id); + if (!g_manager) return false; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetCursor()->IsVisible(); + } + return false; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorX(int id) { - if (!g_manager) return -1; - return g_manager->GetCursorX(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetCursor()->GetX(); + } + return -1; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorY(int id) { - if (!g_manager) return -1; - return g_manager->GetCursorY(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetCursor()->GetY(); + } + return -1; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeWidth(int id) { - if (!g_manager) return -1; - return g_manager->GetCursorShapeWidth(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetCursor()->GetWidth(); + } + return -1; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeHeight(int id) { - if (!g_manager) return -1; - return g_manager->GetCursorShapeHeight(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetCursor()->GetHeight(); + } + return -1; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapePitch(int id) { - if (!g_manager) return -1; - return g_manager->GetCursorShapePitch(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetCursor()->GetPitch(); + } + return -1; } UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeType(int id) { - if (!g_manager) return -1; - return g_manager->GetCursorShapeType(id); + if (!g_manager) return -1; + if (auto monitor = g_manager->GetMonitor(id)) + { + return monitor->GetCursor()->GetType(); + } + return -1; } - UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UpdateCursorTexture(int id, ID3D11Texture2D* ptr) + UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UpdateCursorTexture(int id, ID3D11Texture2D* texture) { - if (!g_manager) return; - g_manager->UpdateCursorTexture(id, ptr); + if (!g_manager) return; + if (auto monitor = g_manager->GetMonitor(id)) + { + monitor->UpdateCursorTexture(texture); + } } UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTexturePtr(int id, void* texture) { - if (!g_manager) return; - g_manager->SetTexturePtr(id, texture); + if (!g_manager) return; + if (auto monitor = g_manager->GetMonitor(id)) + { + auto d3d11Texture = reinterpret_cast(texture); + monitor->SetUnityTexture(d3d11Texture); + } } } \ No newline at end of file