move cursor owner from monitor to manager.

This commit is contained in:
hecomi
2016-11-27 14:44:23 +09:00
parent 5cfbd70e0b
commit 8201a22523
10 changed files with 67 additions and 114 deletions
@@ -8,8 +8,7 @@
using namespace Microsoft::WRL;
Cursor::Cursor(Monitor* monitor)
: monitor_(monitor)
Cursor::Cursor()
{
}
@@ -19,7 +18,7 @@ Cursor::~Cursor()
}
void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
void Cursor::UpdateBuffer(Monitor* monitor, const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
{
if (frameInfo.LastMouseUpdateTime.QuadPart == 0)
{
@@ -29,18 +28,13 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
isVisible_ = frameInfo.PointerPosition.Visible != 0;
if (isVisible_)
{
GetMonitorManager()->SetCursorMonitorId(monitor_->GetId());
GetMonitorManager()->SetCursorMonitorId(monitor->GetId());
}
x_ = frameInfo.PointerPosition.Position.x;
y_ = frameInfo.PointerPosition.Position.y;
timestamp_ = frameInfo.LastMouseUpdateTime;
if (!IsCursorOnParentMonitor())
{
return;
}
if (frameInfo.PointerShapeBufferSize == 0)
{
return;
@@ -55,7 +49,7 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
// Get mouse pointer information
UINT bufferSize;
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo;
const auto hr = monitor_->GetDeskDupl()->GetFramePointerShape(
const auto hr = monitor->GetDeskDupl()->GetFramePointerShape(
apiBuffer_.Size(),
apiBuffer_.Get(),
&bufferSize,
@@ -72,15 +66,10 @@ void Cursor::UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
}
void Cursor::UpdateTexture()
void Cursor::Draw(Monitor* monitor)
{
if (!IsCursorOnParentMonitor())
{
return;
}
// Check desktop texure
if (monitor_->GetUnityTexture() == nullptr)
if (monitor->GetUnityTexture() == nullptr)
{
Debug::Error("Cursor::UpdateTexture() => Monitor::GetUnityTexture() is null.");
return;
@@ -107,15 +96,15 @@ void Cursor::UpdateTexture()
// Calculate information to capture desktop image under cursor.
D3D11_BOX box;
const auto monitorRot = static_cast<DXGI_MODE_ROTATION>(monitor_->GetRotation());
const auto monitorRot = static_cast<DXGI_MODE_ROTATION>(monitor->GetRotation());
auto colMin = 0;
auto colMax = cursorImageWidth;
auto rowMin = 0;
auto rowMax = cursorImageHeight;
{
const auto monitorWidth = monitor_->GetWidth();
const auto monitorHeight = monitor_->GetHeight();
const auto monitorWidth = monitor->GetWidth();
const auto monitorHeight = monitor->GetHeight();
const auto isVertical =
monitorRot == DXGI_MODE_ROTATION_ROTATE90 ||
monitorRot == DXGI_MODE_ROTATION_ROTATE270;
@@ -230,7 +219,7 @@ void Cursor::UpdateTexture()
{
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor_->GetUnityTexture(), 0, &box);
context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, monitor->GetUnityTexture(), 0, &box);
}
// Get mapped surface
@@ -265,6 +254,7 @@ void Cursor::UpdateTexture()
return desktop32[col * desktopPitch + (capturedImageHeight - 1 - row)];
case DXGI_MODE_ROTATION_IDENTITY:
case DXGI_MODE_ROTATION_UNSPECIFIED:
default:
return desktop32[row * desktopPitch + col];
}
};
@@ -389,7 +379,7 @@ void Cursor::UpdateTexture()
{
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
context->UpdateSubresource(monitor_->GetUnityTexture(), 0, &box, bgraBuffer_.Get(), GetWidth() * 4, 0);
context->UpdateSubresource(monitor->GetUnityTexture(), 0, &box, bgraBuffer_.Get(), GetWidth() * 4, 0);
}
if (FAILED(surface->Unmap()))
@@ -474,10 +464,4 @@ int Cursor::GetPitch() const
int Cursor::GetType() const
{
return shapeInfo_.Type;
}
bool Cursor::IsCursorOnParentMonitor() const
{
return GetMonitorManager()->GetCursorMonitorId() == monitor_->GetId();
}
@@ -10,10 +10,10 @@ class Monitor;
class Cursor
{
public:
explicit Cursor(Monitor* monitor);
explicit Cursor();
~Cursor();
void UpdateBuffer(const DXGI_OUTDUPL_FRAME_INFO& frameInfo);
void UpdateTexture();
void UpdateBuffer(Monitor* monitor, const DXGI_OUTDUPL_FRAME_INFO& frameInfo);
void Draw(Monitor* monitor);
void GetTexture(ID3D11Texture2D* texture);
bool IsVisible() const;
@@ -25,9 +25,6 @@ public:
int GetType() const;
private:
bool IsCursorOnParentMonitor() const;
Monitor* monitor_;
bool isVisible_ = false;
int x_ = -1;
int y_ = -1;
@@ -10,7 +10,6 @@ using namespace Microsoft::WRL;
Monitor::Monitor(int id)
: id_(id)
, cursor_(std::make_unique<Cursor>(this))
{
}
@@ -197,7 +196,16 @@ void Monitor::Render(UINT timeout)
}
UpdateMetadata(frameInfo);
UpdateCursor(frameInfo);
if (frameInfo.PointerPosition.Visible)
{
GetMonitorManager()->SetCursorMonitorId(id_);
}
if (GetMonitorManager()->GetCursorMonitorId() == id_)
{
UpdateCursor(frameInfo);
}
hr = deskDupl_->ReleaseFrame();
if (FAILED(hr))
@@ -229,8 +237,9 @@ void Monitor::Render(UINT timeout)
void Monitor::UpdateCursor(const DXGI_OUTDUPL_FRAME_INFO& frameInfo)
{
cursor_->UpdateBuffer(frameInfo);
cursor_->UpdateTexture();
auto cursor_ = GetMonitorManager()->GetCursor();
cursor_->UpdateBuffer(this, frameInfo);
cursor_->Draw(this);
}
@@ -360,18 +369,6 @@ IDXGIOutputDuplication* Monitor::GetDeskDupl()
}
const std::unique_ptr<Cursor>& Monitor::GetCursor()
{
return cursor_;
}
void Monitor::GetCursorTexture(ID3D11Texture2D* texture)
{
cursor_->GetTexture(texture);
}
void Monitor::GetName(char* buf, int len) const
{
strcpy_s(buf, len, monitorInfo_.szDevice);
@@ -6,8 +6,6 @@
#include <memory>
#include "Common.h"
class Cursor;
enum class MonitorState
{
NotSet = -1,
@@ -31,7 +29,6 @@ public:
~Monitor();
void Initialize(IDXGIOutput* output);
void Render(UINT timeout = 0);
void GetCursorTexture(ID3D11Texture2D* texture);
public:
int GetId() const;
@@ -50,7 +47,6 @@ public:
int GetDpiX() const;
int GetDpiY() const;
IDXGIOutputDuplication* GetDeskDupl();
const std::unique_ptr<Cursor>& GetCursor();
int GetMoveRectCount() const;
DXGI_OUTDUPL_MOVE_RECT* GetMoveRects() const;
int GetDirtyRectCount() const;
@@ -66,7 +62,6 @@ private:
UINT dpiX_ = -1, dpiY_ = -1;
int width_ = -1, height_ = -1;
State state_ = State::NotSet;
std::unique_ptr<Cursor> cursor_;
IDXGIOutputDuplication* deskDupl_ = nullptr;
ID3D11Texture2D* unityTexture_ = nullptr;
DXGI_OUTPUT_DESC outputDesc_;
@@ -112,6 +112,12 @@ std::shared_ptr<Monitor> MonitorManager::GetMonitor(int id) const
}
std::shared_ptr<Cursor> MonitorManager::GetCursor() const
{
return cursor_;
}
void MonitorManager::Update()
{
if (isReinitializationRequired_)
@@ -21,6 +21,7 @@ public:
void SetCursorMonitorId(int id) { cursorMonitorId_ = id; }
int GetCursorMonitorId() const { return cursorMonitorId_; }
std::shared_ptr<Monitor> GetMonitor(int id) const;
std::shared_ptr<Cursor> GetCursor() const;
private:
void Initialize();
@@ -41,7 +42,7 @@ public:
private:
int timeout_ = 10;
std::vector<std::shared_ptr<Monitor>> monitors_;
std::shared_ptr<Cursor> cursor_;
std::shared_ptr<Cursor> cursor_ = std::make_shared<Cursor>();
int cursorMonitorId_ = -1;
bool isReinitializationRequired_ = false;
};
@@ -295,83 +295,52 @@ extern "C"
return -1;
}
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsCursorVisible(int id)
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsCursorVisible()
{
if (!g_manager) return false;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetCursor()->IsVisible();
}
return false;
return g_manager->GetCursor()->IsVisible();
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorX(int id)
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorX()
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetCursor()->GetX();
}
return -1;
return g_manager->GetCursor()->GetX();
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorY(int id)
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorY()
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetCursor()->GetY();
}
return -1;
return g_manager->GetCursor()->GetY();
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeWidth(int id)
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeWidth()
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetCursor()->GetWidth();
}
return -1;
return g_manager->GetCursor()->GetWidth();
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeHeight(int id)
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeHeight()
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetCursor()->GetHeight();
}
return -1;
return g_manager->GetCursor()->GetHeight();
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapePitch(int id)
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapePitch()
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetCursor()->GetPitch();
}
return -1;
return g_manager->GetCursor()->GetPitch();
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeType(int id)
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetCursorShapeType()
{
if (!g_manager) return -1;
if (auto monitor = g_manager->GetMonitor(id))
{
return monitor->GetCursor()->GetType();
}
return -1;
return g_manager->GetCursor()->GetType();
}
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetCursorTexture(int id, ID3D11Texture2D* texture)
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetCursorTexture(ID3D11Texture2D* texture)
{
if (!g_manager) return;
if (auto monitor = g_manager->GetMonitor(id))
{
monitor->GetCursorTexture(texture);
}
return g_manager->GetCursor()->GetTexture(texture);
}
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTexturePtr(int id, void* texture)
@@ -391,6 +360,7 @@ extern "C"
{
return monitor->GetMoveRectCount();
}
return 0;
}
UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API GetMoveRects(int id)
@@ -400,6 +370,7 @@ extern "C"
{
return monitor->GetMoveRects();
}
return nullptr;
}
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetDirtyRectCount(int id)
@@ -409,6 +380,7 @@ extern "C"
{
return monitor->GetDirtyRectCount();
}
return 0;
}
UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API GetDirtyRects(int id)
@@ -418,5 +390,6 @@ extern "C"
{
return monitor->GetDirtyRects();
}
return nullptr;
}
}