Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2cad8bf73 | ||
|
|
ad4b49c177 | ||
|
|
92b926bcde | ||
|
|
3e90889266 | ||
|
|
6531fe1dd6 | ||
|
|
0b62376ac5 | ||
|
|
9f611d5d34 | ||
|
|
dd4e27030d |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -30,7 +30,7 @@ public class MultipleMonitorCreator : MonoBehaviour
|
||||
var texture = go.GetComponent<uDesktopDuplication.Texture>();
|
||||
var halfWidth = texture.monitor.width * ratio * scale / 2;
|
||||
x += halfWidth;
|
||||
go.transform.position = new Vector3(x, 0f, 0f);
|
||||
go.transform.localPosition = new Vector3(x, 0f, 0f);
|
||||
x += halfWidth + margin;
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4fad0d0b5018b8649ac41af2fef8dbf2
|
||||
folderAsset: yes
|
||||
timeCreated: 1478011152
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -4,20 +4,22 @@
|
||||
Properties
|
||||
{
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
|
||||
Tags { "RenderType"="Opaque" }
|
||||
Tags { "RenderType"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "Queue" = "Transparent+1" }
|
||||
|
||||
Cull [_Cull]
|
||||
ZWrite Off
|
||||
Offset -1, -1
|
||||
Offset -0.01, -0.01
|
||||
|
||||
CGINCLUDE
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
#include "./uDD_Common.cginc"
|
||||
#include "../../../Shaders/uDD_Common.cginc"
|
||||
|
||||
half4 _PositionScale;
|
||||
#define _PointerX _PositionScale.x
|
||||
@@ -65,9 +65,13 @@ public class Cursor : MonoBehaviour
|
||||
|
||||
void UpdateTexture()
|
||||
{
|
||||
var scale = new Vector2(monitor.pointerShapeWidth, monitor.pointerShapeHeight);
|
||||
var w = monitor.pointerShapeWidth;
|
||||
var h = monitor.pointerShapeHeight;
|
||||
if (w == 0 || h == 0) return;
|
||||
|
||||
var scale = new Vector2(w, h);
|
||||
if (!pointerTextures_.ContainsKey(scale)) {
|
||||
var texture = new Texture2D(monitor.pointerShapeWidth, monitor.pointerShapeHeight, TextureFormat.BGRA32, false);
|
||||
var texture = new Texture2D(w, h, TextureFormat.BGRA32, false);
|
||||
texture.wrapMode = TextureWrapMode.Clamp;
|
||||
pointerTextures_.Add(scale, texture);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ inline void uddInvertUV(inout float2 uv)
|
||||
#endif
|
||||
}
|
||||
|
||||
inline void uddToLinearIfNeeded(inout float3 rgb)
|
||||
inline void uddToLinearIfNeeded(inout fixed3 rgb)
|
||||
{
|
||||
if (!IsGammaSpace()) {
|
||||
rgb = GammaToLinearSpace(rgb);
|
||||
|
||||
@@ -7,11 +7,14 @@ Properties
|
||||
_MainTex ("Albedo (RGB)", 2D) = "white" {}
|
||||
_Glossiness ("Smoothness", Range(0, 1)) = 0.5
|
||||
_Metallic ("Metallic", Range(0, 1)) = 0.0
|
||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType"="Opaque" }
|
||||
|
||||
Cull [_Cull]
|
||||
|
||||
CGPROGRAM
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ Properties
|
||||
{
|
||||
_Color ("Color", Color) = (1, 1, 1, 1)
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||
}
|
||||
|
||||
SubShader
|
||||
@@ -12,6 +13,8 @@ SubShader
|
||||
|
||||
Tags { "RenderType"="Opaque" }
|
||||
|
||||
Cull [_Cull]
|
||||
|
||||
CGINCLUDE
|
||||
|
||||
#include "UnityCG.cginc"
|
||||
|
||||
@@ -6,6 +6,7 @@ Properties
|
||||
_Color ("Color", Color) = (1, 1, 1, 1)
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
_Mask ("Mask", Range(0, 1)) = 0.1
|
||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||
}
|
||||
|
||||
SubShader
|
||||
@@ -13,7 +14,8 @@ SubShader
|
||||
|
||||
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
|
||||
|
||||
ZWrite Off
|
||||
Cull [_Cull]
|
||||
ZWrite On
|
||||
Blend SrcAlpha OneMinusSrcAlpha
|
||||
|
||||
CGINCLUDE
|
||||
|
||||
@@ -5,6 +5,7 @@ Properties
|
||||
{
|
||||
_Color ("Color", Color) = (1, 1, 1, 1)
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
[KeywordEnum(Off, Front, Back)] _Cull("Culling", Int) = 2
|
||||
}
|
||||
|
||||
SubShader
|
||||
@@ -12,7 +13,8 @@ SubShader
|
||||
|
||||
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
|
||||
|
||||
ZWrite Off
|
||||
Cull [_Cull]
|
||||
ZWrite On
|
||||
Blend SrcAlpha OneMinusSrcAlpha
|
||||
|
||||
CGINCLUDE
|
||||
|
||||
@@ -1,405 +0,0 @@
|
||||
#include <d3d11.h>
|
||||
#include <dxgi1_2.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "IUnityInterface.h"
|
||||
#include "IUnityGraphics.h"
|
||||
#include "IUnityGraphicsD3D11.h"
|
||||
|
||||
#pragma comment(lib, "dxgi.lib")
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Monitor
|
||||
{
|
||||
IDXGIOutputDuplication* deskDupl = nullptr;
|
||||
ID3D11Texture2D* texture = nullptr;
|
||||
DXGI_OUTPUT_DESC outputDesc;
|
||||
MONITORINFOEX monitorInfo;
|
||||
|
||||
struct Pointer
|
||||
{
|
||||
bool isVisible = false;
|
||||
int x = -1;
|
||||
int y = -1;
|
||||
BYTE* apiBuffer = nullptr;
|
||||
UINT apiBufferSize = 0;
|
||||
BYTE* bgra32Buffer = nullptr;
|
||||
UINT bgra32BufferSize = 0;
|
||||
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo;
|
||||
};
|
||||
Pointer pointer;
|
||||
};
|
||||
|
||||
IUnityInterfaces* g_unity = nullptr;
|
||||
int g_timeout = 10;
|
||||
HRESULT g_errorCode = 0;
|
||||
std::string g_errorMessage = "";
|
||||
std::vector<Monitor> g_monitors;
|
||||
}
|
||||
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void FinalizeDuplication()
|
||||
{
|
||||
for (auto& monitor : g_monitors)
|
||||
{
|
||||
monitor.deskDupl->Release();
|
||||
}
|
||||
g_monitors.clear();
|
||||
}
|
||||
|
||||
void InitializeDuplication()
|
||||
{
|
||||
FinalizeDuplication();
|
||||
|
||||
IDXGIFactory1* factory;
|
||||
CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&factory));
|
||||
|
||||
// Check all display adapters.
|
||||
IDXGIAdapter1* adapter;
|
||||
for (int i = 0; (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND); ++i)
|
||||
{
|
||||
// Search the main monitor from all outputs.
|
||||
IDXGIOutput* output;
|
||||
for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j)
|
||||
{
|
||||
Monitor monitor;
|
||||
|
||||
output->GetDesc(&monitor.outputDesc);
|
||||
monitor.monitorInfo.cbSize = sizeof(MONITORINFOEX);
|
||||
GetMonitorInfo(monitor.outputDesc.Monitor, &monitor.monitorInfo);
|
||||
|
||||
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
|
||||
auto output1 = reinterpret_cast<IDXGIOutput1*>(output);
|
||||
output1->DuplicateOutput(device, &monitor.deskDupl);
|
||||
output->Release();
|
||||
|
||||
g_monitors.push_back(monitor);
|
||||
}
|
||||
|
||||
adapter->Release();
|
||||
}
|
||||
|
||||
factory->Release();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)
|
||||
{
|
||||
g_unity = unityInterfaces;
|
||||
InitializeDuplication();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityPluginUnload()
|
||||
{
|
||||
g_unity = nullptr;
|
||||
FinalizeDuplication();
|
||||
}
|
||||
|
||||
bool IsValidMonitorId(int id)
|
||||
{
|
||||
return id >= 0 && id < g_monitors.size();
|
||||
}
|
||||
|
||||
bool UpdateMouse(const DXGI_OUTDUPL_FRAME_INFO& frameInfo, Monitor& monitor)
|
||||
{
|
||||
auto& pointer = monitor.pointer;
|
||||
pointer.isVisible = frameInfo.PointerPosition.Visible != 0;
|
||||
pointer.x = frameInfo.PointerPosition.Position.x;
|
||||
pointer.y = frameInfo.PointerPosition.Position.y;
|
||||
if (frameInfo.PointerShapeBufferSize != 0)
|
||||
{
|
||||
if (frameInfo.PointerShapeBufferSize > pointer.apiBufferSize)
|
||||
{
|
||||
if (pointer.apiBuffer) delete[] pointer.apiBuffer;
|
||||
pointer.apiBuffer = new BYTE[frameInfo.PointerShapeBufferSize];
|
||||
pointer.apiBufferSize = frameInfo.PointerShapeBufferSize;
|
||||
}
|
||||
if (pointer.apiBuffer)
|
||||
{
|
||||
UINT bufferSize;
|
||||
monitor.deskDupl->GetFramePointerShape(
|
||||
frameInfo.PointerShapeBufferSize,
|
||||
reinterpret_cast<void*>(pointer.apiBuffer),
|
||||
&bufferSize,
|
||||
&pointer.shapeInfo);
|
||||
}
|
||||
|
||||
// Convert buffer give by API into BGRA32
|
||||
const auto bgraBufferSize = pointer.shapeInfo.Width * pointer.shapeInfo.Height * 4;
|
||||
if (bgraBufferSize > pointer.bgra32BufferSize)
|
||||
{
|
||||
if (pointer.bgra32Buffer) delete[] pointer.bgra32Buffer;
|
||||
pointer.bgra32Buffer = new BYTE[bgraBufferSize];
|
||||
pointer.bgra32BufferSize = bgraBufferSize;
|
||||
}
|
||||
if (pointer.bgra32Buffer)
|
||||
{
|
||||
const auto w = pointer.shapeInfo.Width;
|
||||
const auto h = pointer.shapeInfo.Height;
|
||||
const auto p = pointer.shapeInfo.Pitch;
|
||||
|
||||
// If masked, copy the desktop image and merge it with masked image.
|
||||
const bool isMasked =
|
||||
pointer.shapeInfo.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME ||
|
||||
pointer.shapeInfo.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR;
|
||||
if (isMasked)
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
desc.Width = w;
|
||||
desc.Height = h;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.SampleDesc.Quality = 0;
|
||||
desc.Usage = D3D11_USAGE_STAGING;
|
||||
desc.BindFlags = 0;
|
||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
desc.MiscFlags = 0;
|
||||
|
||||
ID3D11DeviceContext* context;
|
||||
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
|
||||
device->GetImmediateContext(&context);
|
||||
|
||||
ID3D11Texture2D* texture = nullptr;
|
||||
hr = device->CreateTexture2D(&desc, nullptr, &texture);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D11_BOX box;
|
||||
box.left = pointer.x;
|
||||
box.top = pointer.y;
|
||||
box.right = pointer.x + w;
|
||||
box.bottom = pointer.y + h;
|
||||
context->CopySubresourceRegion(texture, 0, 0, 0, 0, monitor.texture, 0, &box);
|
||||
|
||||
IDXGISurface* surface = nullptr;
|
||||
hr = texture->QueryInterface(__uuidof(IDXGISurface), (void**)&surface);
|
||||
texture->Release();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DXGI_MAPPED_RECT mappedSurface;
|
||||
hr = surface->Map(&mappedSurface, DXGI_MAP_READ);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
surface->Release();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Finally, get the texture behind the mouse pointer.
|
||||
const auto desktop32 = reinterpret_cast<UINT*>(mappedSurface.pBits);
|
||||
const auto desktopPitch = mappedSurface.Pitch / sizeof(UINT);
|
||||
|
||||
// Access RGBA values at the same time
|
||||
auto output32 = reinterpret_cast<UINT*>(pointer.bgra32Buffer);
|
||||
|
||||
if (pointer.shapeInfo.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME)
|
||||
{
|
||||
for (UINT row = 0; row < h; ++row)
|
||||
{
|
||||
for (UINT col = 0; col < w; ++col)
|
||||
{
|
||||
const int i = row * w + col;
|
||||
const UINT mask = 0b10000000 >> (i % 8);
|
||||
const BYTE andMask = pointer.apiBuffer[col / 8 + row * p] & mask;
|
||||
const BYTE xorMask = pointer.apiBuffer[col / 8 + (row + h / 2) * p] & mask;
|
||||
const UINT andMask32 = andMask ? 0xFFFFFFFF : 0xFF000000;
|
||||
const UINT xorMask32 = xorMask ? 0x00FFFFFF : 0x00000000;
|
||||
output32[i] = (desktop32[row * desktopPitch + col] & andMask32) ^ xorMask32;
|
||||
}
|
||||
}
|
||||
}
|
||||
else // DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR
|
||||
{
|
||||
const auto buffer32 = reinterpret_cast<UINT*>(pointer.apiBuffer);
|
||||
|
||||
for (UINT row = 0; row < h; ++row)
|
||||
{
|
||||
for (UINT col = 0; col < w; ++col)
|
||||
{
|
||||
const int i = row * w + col;
|
||||
const int j = row * p / sizeof(UINT) + col;
|
||||
|
||||
UINT MaskVal = 0xFF000000 & buffer32[j];
|
||||
|
||||
const UINT mask = 0xFF000000 & buffer[j];
|
||||
if (mask) {
|
||||
output[i] = 0xFF000000 ^ buffer[j];
|
||||
} else {
|
||||
output[i] = 0x00000000;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hr = surface->Unmap();
|
||||
surface->Release();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void UNITY_INTERFACE_API OnRenderEvent(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return;
|
||||
auto& monitor = g_monitors[id];
|
||||
|
||||
if (monitor.deskDupl == nullptr || monitor.texture == nullptr) return;
|
||||
|
||||
IDXGIResource* resource = nullptr;
|
||||
DXGI_OUTDUPL_FRAME_INFO frameInfo;
|
||||
|
||||
g_errorCode = monitor.deskDupl->AcquireNextFrame(g_timeout, &frameInfo, &resource);
|
||||
if (FAILED(g_errorCode))
|
||||
{
|
||||
if (g_errorCode == DXGI_ERROR_ACCESS_LOST)
|
||||
{
|
||||
InitializeDuplication();
|
||||
g_errorMessage = "[IDXGIOutputDuplication::AcquireNextFrame()] Access lost.";
|
||||
}
|
||||
else
|
||||
{
|
||||
g_errorMessage = "[IDXGIOutputDuplication::AcquireNextFrame()] Unknown error.";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ID3D11Texture2D* texture;
|
||||
resource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&texture));
|
||||
|
||||
ID3D11DeviceContext* context;
|
||||
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
|
||||
device->GetImmediateContext(&context);
|
||||
context->CopyResource(monitor.texture, texture);
|
||||
|
||||
if (!UpdateMouse(frameInfo, monitor))
|
||||
{
|
||||
g_errorCode = -999;
|
||||
g_errorMessage = "[UpdateMouse()] failed.";
|
||||
}
|
||||
|
||||
resource->Release();
|
||||
monitor.deskDupl->ReleaseFrame();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT UnityRenderingEvent UNITY_INTERFACE_API GetRenderEventFunc()
|
||||
{
|
||||
return OnRenderEvent;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT size_t UNITY_INTERFACE_API GetMonitorCount()
|
||||
{
|
||||
return g_monitors.size();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTimeout(int timeout)
|
||||
{
|
||||
g_timeout = timeout;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetName(int id, char* buf, int len)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return;
|
||||
strcpy_s(buf, len, g_monitors[id].monitorInfo.szDevice);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsPrimary(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return false;
|
||||
return g_monitors[id].monitorInfo.dwFlags == MONITORINFOF_PRIMARY;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetWidth(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
const auto rect = g_monitors[id].monitorInfo.rcMonitor;
|
||||
return rect.right - rect.left;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetHeight(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
const auto rect = g_monitors[id].monitorInfo.rcMonitor;
|
||||
return rect.bottom - rect.top;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API IsPointerVisible(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return false;
|
||||
return g_monitors[id].pointer.isVisible;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerX(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.x;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerY(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.y;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerShapeWidth(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.shapeInfo.Width;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerShapeHeight(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.shapeInfo.Height;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerShapePitch(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.shapeInfo.Pitch;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerShapeType(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.shapeInfo.Type;
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UpdatePointerTexture(int id, ID3D11Texture2D* ptr)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return;
|
||||
const auto& pointer = g_monitors[id].pointer;
|
||||
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
|
||||
ID3D11DeviceContext* context;
|
||||
device->GetImmediateContext(&context);
|
||||
context->UpdateSubresource(ptr, 0, nullptr, pointer.bgra32Buffer, pointer.shapeInfo.Pitch, 0);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTexturePtr(int id, void* texture)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return;
|
||||
g_monitors[id].texture = reinterpret_cast<ID3D11Texture2D*>(texture);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetErrorCode()
|
||||
{
|
||||
return static_cast<int>(g_errorCode);
|
||||
}
|
||||
}
|
||||
424
Plugins/uDesktopDuplication/uDesktopDuplication/Duplication.cpp
Normal file
424
Plugins/uDesktopDuplication/uDesktopDuplication/Duplication.cpp
Normal file
@@ -0,0 +1,424 @@
|
||||
#include <d3d11.h>
|
||||
#include <dxgi1_2.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include "IUnityInterface.h"
|
||||
#include "IUnityGraphicsD3D11.h"
|
||||
#include "Duplication.h"
|
||||
|
||||
|
||||
Duplication::Duplication(IUnityInterfaces* unity)
|
||||
: unity_(unity)
|
||||
, device_(unity->Get<IUnityGraphicsD3D11>()->GetDevice())
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
|
||||
Duplication::~Duplication()
|
||||
{
|
||||
Finalize();
|
||||
unity_ = nullptr;
|
||||
}
|
||||
|
||||
|
||||
void Duplication::Initialize()
|
||||
{
|
||||
Finalize();
|
||||
|
||||
// Get factory
|
||||
IDXGIFactory1* factory;
|
||||
CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&factory));
|
||||
|
||||
// Check all display adapters
|
||||
int id = 0;
|
||||
IDXGIAdapter1* adapter;
|
||||
for (int i = 0; (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND); ++i)
|
||||
{
|
||||
// Search the main monitor from all outputs
|
||||
IDXGIOutput* output;
|
||||
for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j)
|
||||
{
|
||||
Monitor monitor;
|
||||
monitor.id = id++;
|
||||
|
||||
output->GetDesc(&monitor.outputDesc);
|
||||
monitor.monitorInfo.cbSize = sizeof(MONITORINFOEX);
|
||||
GetMonitorInfo(monitor.outputDesc.Monitor, &monitor.monitorInfo);
|
||||
|
||||
auto output1 = reinterpret_cast<IDXGIOutput1*>(output);
|
||||
output1->DuplicateOutput(device_, &monitor.deskDupl);
|
||||
output->Release();
|
||||
|
||||
monitors_.push_back(monitor);
|
||||
}
|
||||
|
||||
adapter->Release();
|
||||
}
|
||||
|
||||
factory->Release();
|
||||
}
|
||||
|
||||
|
||||
void Duplication::Finalize()
|
||||
{
|
||||
// Release all duplicaitons
|
||||
for (auto& monitor : monitors_)
|
||||
{
|
||||
monitor.deskDupl->Release();
|
||||
}
|
||||
monitors_.clear();
|
||||
}
|
||||
|
||||
|
||||
bool Duplication::IsValidId(int id) const
|
||||
{
|
||||
return id >= 0 && id < monitors_.size();
|
||||
}
|
||||
|
||||
|
||||
void Duplication::OnRender(int id)
|
||||
{
|
||||
errorCode_ = 0;
|
||||
errorMessage_ = "";
|
||||
|
||||
if (!IsValidId(id)) return;
|
||||
auto& monitor = monitors_[id];
|
||||
|
||||
if (monitor.deskDupl == nullptr || monitor.texture == nullptr) return;
|
||||
|
||||
IDXGIResource* resource = nullptr;
|
||||
DXGI_OUTDUPL_FRAME_INFO frameInfo;
|
||||
|
||||
errorCode_ = monitor.deskDupl->AcquireNextFrame(timeout_, &frameInfo, &resource);
|
||||
if (FAILED(errorCode_))
|
||||
{
|
||||
if (errorCode_ == DXGI_ERROR_ACCESS_LOST)
|
||||
{
|
||||
Initialize();
|
||||
errorMessage_ = "[IDXGIOutputDuplication::AcquireNextFrame()] Access lost.";
|
||||
}
|
||||
else
|
||||
{
|
||||
errorMessage_ = "[IDXGIOutputDuplication::AcquireNextFrame()] Maybe timeout.";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ID3D11Texture2D* texture;
|
||||
resource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&texture));
|
||||
|
||||
ID3D11DeviceContext* context;
|
||||
device_->GetImmediateContext(&context);
|
||||
context->CopyResource(monitor.texture, texture);
|
||||
|
||||
if (!UpdateMouse(frameInfo, monitor))
|
||||
{
|
||||
errorCode_ = -999;
|
||||
errorMessage_ = "[UpdateMouse()] failed.";
|
||||
}
|
||||
|
||||
resource->Release();
|
||||
monitor.deskDupl->ReleaseFrame();
|
||||
}
|
||||
|
||||
|
||||
bool Duplication::UpdateMouse(const DXGI_OUTDUPL_FRAME_INFO& frameInfo, Monitor& monitor)
|
||||
{
|
||||
auto& pointer = monitor.pointer;
|
||||
pointer.isVisible = frameInfo.PointerPosition.Visible != 0;
|
||||
pointer.x = frameInfo.PointerPosition.Position.x;
|
||||
pointer.y = frameInfo.PointerPosition.Position.y;
|
||||
|
||||
// Pointer type
|
||||
const auto pointerType = pointer.shapeInfo.Type;
|
||||
const bool isMono = pointerType == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME;
|
||||
const bool isColorMask = pointerType == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR;
|
||||
|
||||
if (pointer.isVisible)
|
||||
{
|
||||
mouseMonitor_ = monitor.id;
|
||||
}
|
||||
|
||||
if (mouseMonitor_ != monitor.id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Increase the buffer size if needed
|
||||
if (frameInfo.PointerShapeBufferSize > pointer.apiBufferSize)
|
||||
{
|
||||
if (pointer.apiBuffer) delete[] pointer.apiBuffer;
|
||||
pointer.apiBuffer = new BYTE[frameInfo.PointerShapeBufferSize];
|
||||
pointer.apiBufferSize = frameInfo.PointerShapeBufferSize;
|
||||
}
|
||||
if (!pointer.apiBuffer) return true;
|
||||
|
||||
// Get information about the mouse pointer if needed
|
||||
if (frameInfo.PointerShapeBufferSize != 0)
|
||||
{
|
||||
UINT bufferSize;
|
||||
monitor.deskDupl->GetFramePointerShape(
|
||||
frameInfo.PointerShapeBufferSize,
|
||||
reinterpret_cast<void*>(pointer.apiBuffer),
|
||||
&bufferSize,
|
||||
&pointer.shapeInfo);
|
||||
}
|
||||
|
||||
// Size
|
||||
const auto w = pointer.shapeInfo.Width;
|
||||
const auto h = pointer.shapeInfo.Height / (isMono ? 2 : 1);
|
||||
const auto p = pointer.shapeInfo.Pitch;
|
||||
|
||||
// Convert the buffer given by API into BGRA32
|
||||
const auto bgraBufferSize = w * h * 4;
|
||||
if (bgraBufferSize > pointer.bgra32BufferSize)
|
||||
{
|
||||
if (pointer.bgra32Buffer) delete[] pointer.bgra32Buffer;
|
||||
pointer.bgra32Buffer = new BYTE[bgraBufferSize];
|
||||
pointer.bgra32BufferSize = bgraBufferSize;
|
||||
}
|
||||
if (!pointer.bgra32Buffer) return true;
|
||||
|
||||
// If masked, copy the desktop image and merge it with masked image.
|
||||
if (isMono || isColorMask)
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
desc.Width = w;
|
||||
desc.Height = h;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.SampleDesc.Quality = 0;
|
||||
desc.Usage = D3D11_USAGE_STAGING;
|
||||
desc.BindFlags = 0;
|
||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
desc.MiscFlags = 0;
|
||||
|
||||
ID3D11DeviceContext* context;
|
||||
device_->GetImmediateContext(&context);
|
||||
|
||||
ID3D11Texture2D* texture = nullptr;
|
||||
hr = device_->CreateTexture2D(&desc, nullptr, &texture);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D11_BOX box;
|
||||
box.front = 0;
|
||||
box.back = 1;
|
||||
box.left = pointer.x;
|
||||
box.top = pointer.y;
|
||||
box.right = pointer.x + w;
|
||||
box.bottom = pointer.y + h;
|
||||
context->CopySubresourceRegion(texture, 0, 0, 0, 0, monitor.texture, 0, &box);
|
||||
|
||||
IDXGISurface* surface = nullptr;
|
||||
hr = texture->QueryInterface(__uuidof(IDXGISurface), (void**)&surface);
|
||||
texture->Release();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DXGI_MAPPED_RECT mappedSurface;
|
||||
hr = surface->Map(&mappedSurface, DXGI_MAP_READ);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
surface->Release();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Finally, get the texture behind the mouse pointer.
|
||||
const auto desktop32 = reinterpret_cast<UINT*>(mappedSurface.pBits);
|
||||
const UINT desktopPitch = mappedSurface.Pitch / sizeof(UINT);
|
||||
|
||||
// Access RGBA values at the same time
|
||||
auto output32 = reinterpret_cast<UINT*>(pointer.bgra32Buffer);
|
||||
|
||||
if (isMono)
|
||||
{
|
||||
for (UINT row = 0; row < h; ++row)
|
||||
{
|
||||
BYTE mask = 0x80;
|
||||
for (UINT col = 0; col < w; ++col)
|
||||
{
|
||||
const int i = row * w + col;
|
||||
const BYTE andMask = pointer.apiBuffer[col / 8 + row * p] & mask;
|
||||
const BYTE xorMask = pointer.apiBuffer[col / 8 + (row + h) * p] & mask;
|
||||
const UINT andMask32 = andMask ? 0xFFFFFFFF : 0xFF000000;
|
||||
const UINT xorMask32 = xorMask ? 0x00FFFFFF : 0x00000000;
|
||||
output32[i] = (desktop32[row * desktopPitch + col] & andMask32) ^ xorMask32;
|
||||
mask = (mask == 0x01) ? 0x80 : (mask >> 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else // DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR
|
||||
{
|
||||
const auto buffer32 = reinterpret_cast<UINT*>(pointer.apiBuffer);
|
||||
|
||||
for (UINT row = 0; row < h; ++row)
|
||||
{
|
||||
for (UINT col = 0; col < w; ++col)
|
||||
{
|
||||
const int i = row * w + col;
|
||||
const int j = row * p / sizeof(UINT) + col;
|
||||
|
||||
UINT mask = 0xFF000000 & buffer32[j];
|
||||
if (mask)
|
||||
{
|
||||
output32[i] = (desktop32[row * desktopPitch + col] ^ buffer32[j]) | 0xFF000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
output32[i] = buffer32[j] | 0xFF000000;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hr = surface->Unmap();
|
||||
surface->Release();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else // DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR
|
||||
{
|
||||
auto output32 = reinterpret_cast<UINT*>(pointer.bgra32Buffer);
|
||||
const auto buffer32 = reinterpret_cast<UINT*>(pointer.apiBuffer);
|
||||
for (UINT i = 0; i < w * h; ++i)
|
||||
{
|
||||
output32[i] = buffer32[i];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void Duplication::UpdatePointerTexture(int id, ID3D11Texture2D* ptr)
|
||||
{
|
||||
if (!IsValidId(id)) return;
|
||||
const auto& pointer = monitors_[id].pointer;
|
||||
if (!pointer.bgra32Buffer) return;
|
||||
ID3D11DeviceContext* context;
|
||||
device_->GetImmediateContext(&context);
|
||||
context->UpdateSubresource(ptr, 0, nullptr, pointer.bgra32Buffer, pointer.shapeInfo.Width * 4, 0);
|
||||
}
|
||||
|
||||
|
||||
void Duplication::SetTimeout(int timeout)
|
||||
{
|
||||
timeout_ = timeout;
|
||||
}
|
||||
|
||||
|
||||
void Duplication::SetTexturePtr(int id, void* texture)
|
||||
{
|
||||
if (!IsValidId(id)) return;
|
||||
monitors_[id].texture = reinterpret_cast<ID3D11Texture2D*>(texture);
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetMonitorCount() const
|
||||
{
|
||||
return static_cast<int>(monitors_.size());
|
||||
}
|
||||
|
||||
|
||||
void Duplication::GetName(int id, char* buf, int len) const
|
||||
{
|
||||
if (!IsValidId(id)) return;
|
||||
strcpy_s(buf, len, monitors_[id].monitorInfo.szDevice);
|
||||
}
|
||||
|
||||
|
||||
bool Duplication::IsPrimary(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return false;
|
||||
return monitors_[id].monitorInfo.dwFlags == MONITORINFOF_PRIMARY;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetWidth(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return -1;
|
||||
const auto rect = monitors_[id].monitorInfo.rcMonitor;
|
||||
return rect.right - rect.left;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetHeight(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return -1;
|
||||
const auto rect = monitors_[id].monitorInfo.rcMonitor;
|
||||
return rect.bottom - rect.top;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::IsPointerVisible(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return false;
|
||||
return monitors_[id].pointer.isVisible;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetPointerX(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return -1;
|
||||
return monitors_[id].pointer.x;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetPointerY(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return -1;
|
||||
return monitors_[id].pointer.y;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetPointerShapeWidth(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return -1;
|
||||
return monitors_[id].pointer.shapeInfo.Width;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetPointerShapeHeight(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return -1;
|
||||
const auto& info = monitors_[id].pointer.shapeInfo;
|
||||
return (info.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME) ? info.Height / 2 : info.Height;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetPointerShapePitch(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return -1;
|
||||
return monitors_[id].pointer.shapeInfo.Pitch;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetPointerShapeType(int id) const
|
||||
{
|
||||
if (!IsValidId(id)) return -1;
|
||||
return monitors_[id].pointer.shapeInfo.Type;
|
||||
}
|
||||
|
||||
|
||||
int Duplication::GetErrorCode() const
|
||||
{
|
||||
return errorCode_;
|
||||
}
|
||||
|
||||
|
||||
void Duplication::GetErrorMessage(char* buf, int len) const
|
||||
{
|
||||
strcpy_s(buf, len, errorMessage_.c_str());
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
#include <d3d11.h>
|
||||
#include <dxgi1_2.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
struct IUnityInterfaces;
|
||||
|
||||
struct Pointer
|
||||
{
|
||||
bool isVisible = false;
|
||||
int x = -1;
|
||||
int y = -1;
|
||||
BYTE* apiBuffer = nullptr;
|
||||
UINT apiBufferSize = 0;
|
||||
BYTE* bgra32Buffer = nullptr;
|
||||
UINT bgra32BufferSize = 0;
|
||||
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo;
|
||||
};
|
||||
|
||||
struct Monitor
|
||||
{
|
||||
int id = -1;
|
||||
IDXGIOutputDuplication* deskDupl = nullptr;
|
||||
ID3D11Texture2D* texture = nullptr;
|
||||
DXGI_OUTPUT_DESC outputDesc;
|
||||
MONITORINFOEX monitorInfo;
|
||||
Pointer pointer;
|
||||
};
|
||||
|
||||
class Duplication
|
||||
{
|
||||
public:
|
||||
explicit Duplication(IUnityInterfaces* unity);
|
||||
~Duplication();
|
||||
void OnRender(int id);
|
||||
|
||||
public:
|
||||
void UpdatePointerTexture(int id, ID3D11Texture2D* ptr);
|
||||
void SetTimeout(int timeout);
|
||||
void SetTexturePtr(int id, void* texture);
|
||||
|
||||
public:
|
||||
int GetMonitorCount() const;
|
||||
void GetName(int id, char* buf, int len) const;
|
||||
bool IsPrimary(int id) const;
|
||||
int GetWidth(int id) const;
|
||||
int GetHeight(int id) const;
|
||||
int IsPointerVisible(int id) const;
|
||||
int GetPointerX(int id) const;
|
||||
int GetPointerY(int id) const;
|
||||
int GetPointerShapeWidth(int id) const;
|
||||
int GetPointerShapeHeight(int id) const;
|
||||
int GetPointerShapePitch(int id) const;
|
||||
int GetPointerShapeType(int id) const;
|
||||
int GetErrorCode() const;
|
||||
void GetErrorMessage(char* buf, int len) const;
|
||||
|
||||
private:
|
||||
void Initialize();
|
||||
void Finalize();
|
||||
bool UpdateMouse(const DXGI_OUTDUPL_FRAME_INFO& frameInfo, Monitor& monitor);
|
||||
|
||||
bool IsValidId(int id) const;
|
||||
|
||||
IUnityInterfaces* unity_ = nullptr;
|
||||
ID3D11Device* device_ = nullptr;
|
||||
int mouseMonitor_ = 0;
|
||||
int timeout_ = 10;
|
||||
HRESULT errorCode_ = 0;
|
||||
std::string errorMessage_ = "";
|
||||
std::vector<Monitor> monitors_;
|
||||
};
|
||||
@@ -2,331 +2,36 @@
|
||||
#include <dxgi1_2.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include "IUnityInterface.h"
|
||||
#include "IUnityGraphics.h"
|
||||
#include "IUnityGraphicsD3D11.h"
|
||||
#include "Duplication.h"
|
||||
|
||||
#pragma comment(lib, "dxgi.lib")
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Monitor
|
||||
{
|
||||
int id = -1;
|
||||
IDXGIOutputDuplication* deskDupl = nullptr;
|
||||
ID3D11Texture2D* texture = nullptr;
|
||||
DXGI_OUTPUT_DESC outputDesc;
|
||||
MONITORINFOEX monitorInfo;
|
||||
|
||||
struct Pointer
|
||||
{
|
||||
bool isVisible = false;
|
||||
int x = -1;
|
||||
int y = -1;
|
||||
BYTE* apiBuffer = nullptr;
|
||||
UINT apiBufferSize = 0;
|
||||
BYTE* bgra32Buffer = nullptr;
|
||||
UINT bgra32BufferSize = 0;
|
||||
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo;
|
||||
};
|
||||
Pointer pointer;
|
||||
};
|
||||
|
||||
IUnityInterfaces* g_unity = nullptr;
|
||||
int g_mouseMonitor = 0;
|
||||
int g_timeout = 10;
|
||||
HRESULT g_errorCode = 0;
|
||||
std::string g_errorMessage = "";
|
||||
std::vector<Monitor> g_monitors;
|
||||
std::unique_ptr<Duplication> g_dupl;
|
||||
}
|
||||
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void FinalizeDuplication()
|
||||
{
|
||||
for (auto& monitor : g_monitors)
|
||||
{
|
||||
monitor.deskDupl->Release();
|
||||
}
|
||||
g_monitors.clear();
|
||||
}
|
||||
|
||||
void InitializeDuplication()
|
||||
{
|
||||
FinalizeDuplication();
|
||||
|
||||
IDXGIFactory1* factory;
|
||||
CreateDXGIFactory1(__uuidof(IDXGIFactory1), reinterpret_cast<void**>(&factory));
|
||||
|
||||
// Check all display adapters.
|
||||
int id = 0;
|
||||
IDXGIAdapter1* adapter;
|
||||
for (int i = 0; (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND); ++i)
|
||||
{
|
||||
// Search the main monitor from all outputs.
|
||||
IDXGIOutput* output;
|
||||
for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j)
|
||||
{
|
||||
Monitor monitor;
|
||||
monitor.id = id++;
|
||||
|
||||
output->GetDesc(&monitor.outputDesc);
|
||||
monitor.monitorInfo.cbSize = sizeof(MONITORINFOEX);
|
||||
GetMonitorInfo(monitor.outputDesc.Monitor, &monitor.monitorInfo);
|
||||
|
||||
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
|
||||
auto output1 = reinterpret_cast<IDXGIOutput1*>(output);
|
||||
output1->DuplicateOutput(device, &monitor.deskDupl);
|
||||
output->Release();
|
||||
|
||||
g_monitors.push_back(monitor);
|
||||
}
|
||||
|
||||
adapter->Release();
|
||||
}
|
||||
|
||||
factory->Release();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)
|
||||
{
|
||||
g_unity = unityInterfaces;
|
||||
InitializeDuplication();
|
||||
g_dupl = std::make_unique<Duplication>(unityInterfaces);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityPluginUnload()
|
||||
{
|
||||
g_unity = nullptr;
|
||||
FinalizeDuplication();
|
||||
}
|
||||
|
||||
bool IsValidMonitorId(int id)
|
||||
{
|
||||
return id >= 0 && id < g_monitors.size();
|
||||
}
|
||||
|
||||
bool UpdateMouse(const DXGI_OUTDUPL_FRAME_INFO& frameInfo, Monitor& monitor)
|
||||
{
|
||||
auto& pointer = monitor.pointer;
|
||||
pointer.isVisible = frameInfo.PointerPosition.Visible != 0;
|
||||
pointer.x = frameInfo.PointerPosition.Position.x;
|
||||
pointer.y = frameInfo.PointerPosition.Position.y;
|
||||
|
||||
// Pointer type
|
||||
const auto pointerType = pointer.shapeInfo.Type;
|
||||
const bool isMono = pointerType == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME;
|
||||
const bool isColorMask = pointerType == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR;
|
||||
|
||||
if (pointer.isVisible)
|
||||
{
|
||||
g_mouseMonitor = monitor.id;
|
||||
}
|
||||
|
||||
if (g_mouseMonitor != monitor.id) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Increase the buffer size if needed
|
||||
if (frameInfo.PointerShapeBufferSize > pointer.apiBufferSize)
|
||||
{
|
||||
if (pointer.apiBuffer) delete[] pointer.apiBuffer;
|
||||
pointer.apiBuffer = new BYTE[frameInfo.PointerShapeBufferSize];
|
||||
pointer.apiBufferSize = frameInfo.PointerShapeBufferSize;
|
||||
}
|
||||
if (!pointer.apiBuffer) return true;
|
||||
|
||||
// Get information about the mouse pointer if needed
|
||||
if (frameInfo.PointerShapeBufferSize != 0)
|
||||
{
|
||||
UINT bufferSize;
|
||||
monitor.deskDupl->GetFramePointerShape(
|
||||
frameInfo.PointerShapeBufferSize,
|
||||
reinterpret_cast<void*>(pointer.apiBuffer),
|
||||
&bufferSize,
|
||||
&pointer.shapeInfo);
|
||||
}
|
||||
|
||||
// Size
|
||||
const auto w = pointer.shapeInfo.Width;
|
||||
const auto h = pointer.shapeInfo.Height / (isMono ? 2 : 1);
|
||||
const auto p = pointer.shapeInfo.Pitch;
|
||||
|
||||
// Convert the buffer given by API into BGRA32
|
||||
const auto bgraBufferSize = w * h * 4;
|
||||
if (bgraBufferSize > pointer.bgra32BufferSize)
|
||||
{
|
||||
if (pointer.bgra32Buffer) delete[] pointer.bgra32Buffer;
|
||||
pointer.bgra32Buffer = new BYTE[bgraBufferSize];
|
||||
pointer.bgra32BufferSize = bgraBufferSize;
|
||||
}
|
||||
if (!pointer.bgra32Buffer) return true;
|
||||
|
||||
// If masked, copy the desktop image and merge it with masked image.
|
||||
if (isMono || isColorMask)
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
D3D11_TEXTURE2D_DESC desc;
|
||||
desc.Width = w;
|
||||
desc.Height = h;
|
||||
desc.MipLevels = 1;
|
||||
desc.ArraySize = 1;
|
||||
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
|
||||
desc.SampleDesc.Count = 1;
|
||||
desc.SampleDesc.Quality = 0;
|
||||
desc.Usage = D3D11_USAGE_STAGING;
|
||||
desc.BindFlags = 0;
|
||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||
desc.MiscFlags = 0;
|
||||
|
||||
ID3D11DeviceContext* context;
|
||||
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
|
||||
device->GetImmediateContext(&context);
|
||||
|
||||
ID3D11Texture2D* texture = nullptr;
|
||||
hr = device->CreateTexture2D(&desc, nullptr, &texture);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D11_BOX box;
|
||||
box.front = 0;
|
||||
box.back = 1;
|
||||
box.left = pointer.x;
|
||||
box.top = pointer.y;
|
||||
box.right = pointer.x + w;
|
||||
box.bottom = pointer.y + h;
|
||||
context->CopySubresourceRegion(texture, 0, 0, 0, 0, monitor.texture, 0, &box);
|
||||
|
||||
IDXGISurface* surface = nullptr;
|
||||
hr = texture->QueryInterface(__uuidof(IDXGISurface), (void**)&surface);
|
||||
texture->Release();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DXGI_MAPPED_RECT mappedSurface;
|
||||
hr = surface->Map(&mappedSurface, DXGI_MAP_READ);
|
||||
if (FAILED(hr))
|
||||
{
|
||||
surface->Release();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Finally, get the texture behind the mouse pointer.
|
||||
const auto desktop32 = reinterpret_cast<UINT*>(mappedSurface.pBits);
|
||||
const UINT desktopPitch = mappedSurface.Pitch / sizeof(UINT);
|
||||
|
||||
// Access RGBA values at the same time
|
||||
auto output32 = reinterpret_cast<UINT*>(pointer.bgra32Buffer);
|
||||
|
||||
if (isMono)
|
||||
{
|
||||
for (UINT row = 0; row < h; ++row)
|
||||
{
|
||||
BYTE mask = 0x80;
|
||||
for (UINT col = 0; col < w; ++col)
|
||||
{
|
||||
const int i = row * w + col;
|
||||
const BYTE andMask = pointer.apiBuffer[col / 8 + row * p] & mask;
|
||||
const BYTE xorMask = pointer.apiBuffer[col / 8 + (row + h) * p] & mask;
|
||||
const UINT andMask32 = andMask ? 0xFFFFFFFF : 0xFF000000;
|
||||
const UINT xorMask32 = xorMask ? 0x00FFFFFF : 0x00000000;
|
||||
output32[i] = (desktop32[row * desktopPitch + col] & andMask32) ^ xorMask32;
|
||||
mask = (mask == 0x01) ? 0x80 : (mask >> 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else // DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MASKED_COLOR
|
||||
{
|
||||
const auto buffer32 = reinterpret_cast<UINT*>(pointer.apiBuffer);
|
||||
|
||||
for (UINT row = 0; row < h; ++row)
|
||||
{
|
||||
for (UINT col = 0; col < w; ++col)
|
||||
{
|
||||
const int i = row * w + col;
|
||||
const int j = row * p / sizeof(UINT) + col;
|
||||
|
||||
UINT mask = 0xFF000000 & buffer32[j];
|
||||
if (mask)
|
||||
{
|
||||
output32[i] = (desktop32[row * desktopPitch + col] ^ buffer32[j]) | 0xFF000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
output32[i] = buffer32[j] | 0xFF000000;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hr = surface->Unmap();
|
||||
surface->Release();
|
||||
if (FAILED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else // DXGI_OUTDUPL_POINTER_SHAPE_TYPE_COLOR
|
||||
{
|
||||
auto output32 = reinterpret_cast<UINT*>(pointer.bgra32Buffer);
|
||||
const auto buffer32 = reinterpret_cast<UINT*>(pointer.apiBuffer);
|
||||
for (UINT i = 0; i < w * h; ++i)
|
||||
{
|
||||
output32[i] = buffer32[i];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
g_dupl.reset();
|
||||
}
|
||||
|
||||
void UNITY_INTERFACE_API OnRenderEvent(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return;
|
||||
auto& monitor = g_monitors[id];
|
||||
|
||||
if (monitor.deskDupl == nullptr || monitor.texture == nullptr) return;
|
||||
|
||||
IDXGIResource* resource = nullptr;
|
||||
DXGI_OUTDUPL_FRAME_INFO frameInfo;
|
||||
|
||||
g_errorCode = monitor.deskDupl->AcquireNextFrame(g_timeout, &frameInfo, &resource);
|
||||
if (FAILED(g_errorCode))
|
||||
{
|
||||
if (g_errorCode == DXGI_ERROR_ACCESS_LOST)
|
||||
{
|
||||
InitializeDuplication();
|
||||
g_errorMessage = "[IDXGIOutputDuplication::AcquireNextFrame()] Access lost.";
|
||||
}
|
||||
else
|
||||
{
|
||||
g_errorMessage = "[IDXGIOutputDuplication::AcquireNextFrame()] Maybe timeout.";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ID3D11Texture2D* texture;
|
||||
resource->QueryInterface(__uuidof(ID3D11Texture2D), reinterpret_cast<void**>(&texture));
|
||||
|
||||
ID3D11DeviceContext* context;
|
||||
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
|
||||
device->GetImmediateContext(&context);
|
||||
context->CopyResource(monitor.texture, texture);
|
||||
|
||||
if (!UpdateMouse(frameInfo, monitor))
|
||||
{
|
||||
g_errorCode = -999;
|
||||
g_errorMessage = "[UpdateMouse()] failed.";
|
||||
}
|
||||
|
||||
resource->Release();
|
||||
monitor.deskDupl->ReleaseFrame();
|
||||
g_dupl->OnRender(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT UnityRenderingEvent UNITY_INTERFACE_API GetRenderEventFunc()
|
||||
@@ -336,112 +41,87 @@ extern "C"
|
||||
|
||||
UNITY_INTERFACE_EXPORT size_t UNITY_INTERFACE_API GetMonitorCount()
|
||||
{
|
||||
return g_monitors.size();
|
||||
return g_dupl->GetMonitorCount();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTimeout(int timeout)
|
||||
{
|
||||
g_timeout = timeout;
|
||||
g_dupl->SetTimeout(timeout);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetName(int id, char* buf, int len)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return;
|
||||
strcpy_s(buf, len, g_monitors[id].monitorInfo.szDevice);
|
||||
g_dupl->GetName(id, buf, len);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT bool UNITY_INTERFACE_API IsPrimary(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return false;
|
||||
return g_monitors[id].monitorInfo.dwFlags == MONITORINFOF_PRIMARY;
|
||||
return g_dupl->IsPrimary(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetWidth(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
const auto rect = g_monitors[id].monitorInfo.rcMonitor;
|
||||
return rect.right - rect.left;
|
||||
return g_dupl->GetWidth(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetHeight(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
const auto rect = g_monitors[id].monitorInfo.rcMonitor;
|
||||
return rect.bottom - rect.top;
|
||||
return g_dupl->GetHeight(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API IsPointerVisible(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return false;
|
||||
return g_monitors[id].pointer.isVisible;
|
||||
return g_dupl->IsPointerVisible(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerX(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.x;
|
||||
return g_dupl->GetPointerX(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerY(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.y;
|
||||
return g_dupl->GetPointerY(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerShapeWidth(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.shapeInfo.Width;
|
||||
return g_dupl->GetPointerShapeWidth(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerShapeHeight(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
const auto& info = g_monitors[id].pointer.shapeInfo;
|
||||
return (info.Type == DXGI_OUTDUPL_POINTER_SHAPE_TYPE_MONOCHROME) ? info.Height / 2 : info.Height;
|
||||
return g_dupl->GetPointerShapeHeight(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerShapePitch(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.shapeInfo.Pitch;
|
||||
return g_dupl->GetPointerShapePitch(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetPointerShapeType(int id)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return -1;
|
||||
return g_monitors[id].pointer.shapeInfo.Type;
|
||||
return g_dupl->GetPointerShapeType(id);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UpdatePointerTexture(int id, ID3D11Texture2D* ptr)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return;
|
||||
const auto& pointer = g_monitors[id].pointer;
|
||||
if (!pointer.bgra32Buffer) return;
|
||||
auto device = g_unity->Get<IUnityGraphicsD3D11>()->GetDevice();
|
||||
ID3D11DeviceContext* context;
|
||||
device->GetImmediateContext(&context);
|
||||
context->UpdateSubresource(ptr, 0, nullptr, pointer.bgra32Buffer, pointer.shapeInfo.Width * 4, 0);
|
||||
g_dupl->UpdatePointerTexture(id, ptr);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API SetTexturePtr(int id, void* texture)
|
||||
{
|
||||
if (!IsValidMonitorId(id)) return;
|
||||
g_monitors[id].texture = reinterpret_cast<ID3D11Texture2D*>(texture);
|
||||
g_dupl->SetTexturePtr(id, texture);
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetErrorCode()
|
||||
{
|
||||
const auto code = g_errorCode;
|
||||
g_errorCode = 0;
|
||||
return static_cast<int>(code);
|
||||
return g_dupl->GetErrorCode();
|
||||
}
|
||||
|
||||
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API GetErrorMessage(char* buf, int len)
|
||||
{
|
||||
const auto message = g_errorMessage;
|
||||
g_errorMessage = "";
|
||||
strcpy_s(buf, len, message.c_str());
|
||||
g_dupl->GetErrorMessage(buf, len);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -122,9 +122,11 @@
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Duplication.cpp" />
|
||||
<ClCompile Include="main.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Duplication.h" />
|
||||
<ClInclude Include="IUnityGraphics.h" />
|
||||
<ClInclude Include="IUnityGraphicsD3D11.h" />
|
||||
<ClInclude Include="IUnityInterface.h" />
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user