use shared texture.
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -163,6 +163,8 @@ public static class Lib
|
|||||||
[DllImport("uDesktopDuplication")]
|
[DllImport("uDesktopDuplication")]
|
||||||
public static extern int SetTexturePtr(int id, IntPtr ptr);
|
public static extern int SetTexturePtr(int id, IntPtr ptr);
|
||||||
[DllImport("uDesktopDuplication")]
|
[DllImport("uDesktopDuplication")]
|
||||||
|
public static extern IntPtr GetSharedTextureHandle(int id);
|
||||||
|
[DllImport("uDesktopDuplication")]
|
||||||
public static extern int GetMoveRectCount(int id);
|
public static extern int GetMoveRectCount(int id);
|
||||||
[DllImport("uDesktopDuplication", EntryPoint = "GetMoveRects")]
|
[DllImport("uDesktopDuplication", EntryPoint = "GetMoveRects")]
|
||||||
private static extern IntPtr GetMoveRects_Internal(int id);
|
private static extern IntPtr GetMoveRects_Internal(int id);
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ IUnityInterfaces* GetUnity()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ComPtr<ID3D11Device> GetDevice()
|
ComPtr<ID3D11Device> GetUnityDevice()
|
||||||
{
|
{
|
||||||
return GetUnity()->Get<IUnityGraphicsD3D11>()->GetDevice();
|
return GetUnity()->Get<IUnityGraphicsD3D11>()->GetDevice();
|
||||||
}
|
}
|
||||||
@@ -62,7 +62,7 @@ LUID GetUnityAdapterLuid()
|
|||||||
{
|
{
|
||||||
UDD_FUNCTION_SCOPE_TIMER
|
UDD_FUNCTION_SCOPE_TIMER
|
||||||
|
|
||||||
const auto device = GetDevice();
|
const auto device = GetUnityDevice();
|
||||||
|
|
||||||
Microsoft::WRL::ComPtr<IDXGIDevice1> dxgiDevice;
|
Microsoft::WRL::ComPtr<IDXGIDevice1> dxgiDevice;
|
||||||
if (FAILED(device->QueryInterface(IID_PPV_ARGS(&dxgiDevice)))){
|
if (FAILED(device->QueryInterface(IID_PPV_ARGS(&dxgiDevice)))){
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ IUnityInterfaces* GetUnity();
|
|||||||
|
|
||||||
// ID3D11Device (in Unity) getter
|
// ID3D11Device (in Unity) getter
|
||||||
struct ID3D11Device;
|
struct ID3D11Device;
|
||||||
Microsoft::WRL::ComPtr<ID3D11Device> GetDevice();
|
Microsoft::WRL::ComPtr<ID3D11Device> GetUnityDevice();
|
||||||
|
|
||||||
// Manager getter
|
// Manager getter
|
||||||
class MonitorManager;
|
class MonitorManager;
|
||||||
|
|||||||
@@ -383,7 +383,7 @@ void Cursor::Draw(const Microsoft::WRL::ComPtr<ID3D11Texture2D>& texture)
|
|||||||
|
|
||||||
const auto capturedImageWidth = capturedImageArea_.right - capturedImageArea_.left;
|
const auto capturedImageWidth = capturedImageArea_.right - capturedImageArea_.left;
|
||||||
ComPtr<ID3D11DeviceContext> context;
|
ComPtr<ID3D11DeviceContext> context;
|
||||||
GetDevice()->GetImmediateContext(&context);
|
GetUnityDevice()->GetImmediateContext(&context);
|
||||||
context->UpdateSubresource(texture.Get(), 0, &capturedImageArea_, bgraBuffer_.Get(), capturedImageWidth * 4, 0);
|
context->UpdateSubresource(texture.Get(), 0, &capturedImageArea_, bgraBuffer_.Get(), capturedImageWidth * 4, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -418,7 +418,7 @@ void Cursor::GetTexture(ID3D11Texture2D* texture)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ComPtr<ID3D11DeviceContext> context;
|
ComPtr<ID3D11DeviceContext> context;
|
||||||
GetDevice()->GetImmediateContext(&context);
|
GetUnityDevice()->GetImmediateContext(&context);
|
||||||
context->UpdateSubresource(texture, 0, nullptr, bgraBuffer_.Get(), GetWidth() * 4, 0);
|
context->UpdateSubresource(texture, 0, nullptr, bgraBuffer_.Get(), GetWidth() * 4, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -306,6 +306,16 @@ void Duplicator::Duplicate(UINT timeout)
|
|||||||
context->CopyResource(sharedTexture.Get(), texture.Get());
|
context->CopyResource(sharedTexture.Get(), texture.Get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
HANDLE sharedHandle;
|
||||||
|
ComPtr<IDXGIResource> dxgiResource;
|
||||||
|
sharedTexture.As(&dxgiResource);
|
||||||
|
if (FAILED(dxgiResource->GetSharedHandle(&sharedHandle)))
|
||||||
|
{
|
||||||
|
Debug::Error("Duplicator::Duplicate() => Failed to get shared handle.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
UpdateCursor(sharedTexture, frameInfo);
|
UpdateCursor(sharedTexture, frameInfo);
|
||||||
UpdateMetadata(frameInfo.TotalMetadataBufferSize);
|
UpdateMetadata(frameInfo.TotalMetadataBufferSize);
|
||||||
|
|
||||||
@@ -315,6 +325,7 @@ void Duplicator::Duplicate(UINT timeout)
|
|||||||
{
|
{
|
||||||
lastFrameId_++,
|
lastFrameId_++,
|
||||||
sharedTexture,
|
sharedTexture,
|
||||||
|
sharedHandle,
|
||||||
frameInfo,
|
frameInfo,
|
||||||
metaData_
|
metaData_
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -44,8 +44,9 @@ public:
|
|||||||
|
|
||||||
struct Frame
|
struct Frame
|
||||||
{
|
{
|
||||||
UINT id;
|
UINT id = 0;
|
||||||
Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
|
Microsoft::WRL::ComPtr<ID3D11Texture2D> texture = nullptr;
|
||||||
|
HANDLE textureHandle = nullptr;
|
||||||
DXGI_OUTDUPL_FRAME_INFO info;
|
DXGI_OUTDUPL_FRAME_INFO info;
|
||||||
Metadata metaData;
|
Metadata metaData;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -117,9 +117,19 @@ void Monitor::Render()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
ComPtr<ID3D11Texture2D> desktopTexture;
|
||||||
|
if (FAILED(GetUnityDevice()->OpenSharedResource(
|
||||||
|
frame.textureHandle,
|
||||||
|
__uuidof(ID3D11Texture2D),
|
||||||
|
&desktopTexture)))
|
||||||
|
{
|
||||||
|
Debug::Error("Monitor::Render() => Failed to open shared resource.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ComPtr<ID3D11DeviceContext> context;
|
ComPtr<ID3D11DeviceContext> context;
|
||||||
GetDevice()->GetImmediateContext(&context);
|
GetUnityDevice()->GetImmediateContext(&context);
|
||||||
context->CopyResource(unityTexture_, frame.texture.Get());
|
context->CopyResource(unityTexture_, desktopTexture.Get());
|
||||||
|
|
||||||
auto& manager = GetMonitorManager();
|
auto& manager = GetMonitorManager();
|
||||||
if (id_ == manager->GetCursorMonitorId())
|
if (id_ == manager->GetCursorMonitorId())
|
||||||
@@ -198,6 +208,15 @@ ID3D11Texture2D* Monitor::GetUnityTexture() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
HANDLE Monitor::GetSharedTextureHandle() const
|
||||||
|
{
|
||||||
|
UDD_FUNCTION_SCOPE_TIMER
|
||||||
|
|
||||||
|
const auto& frame = duplicator_->GetLastFrame();
|
||||||
|
return frame.textureHandle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
ComPtr<IDXGIOutputDuplication> Monitor::GetDeskDupl()
|
ComPtr<IDXGIOutputDuplication> Monitor::GetDeskDupl()
|
||||||
{
|
{
|
||||||
return duplicator_->GetDuplication();
|
return duplicator_->GetDuplication();
|
||||||
@@ -344,7 +363,7 @@ void Monitor::CopyTextureFromGpuToCpu(ID3D11Texture2D* texture)
|
|||||||
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
|
||||||
desc.MiscFlags = 0;
|
desc.MiscFlags = 0;
|
||||||
|
|
||||||
if (FAILED(GetDevice()->CreateTexture2D(&desc, nullptr, &textureForGetPixels_)))
|
if (FAILED(GetUnityDevice()->CreateTexture2D(&desc, nullptr, &textureForGetPixels_)))
|
||||||
{
|
{
|
||||||
Debug::Error("Monitor::CopyTextureFromGpuToCpu() => GetDevice()->CreateTexture2D() failed.");
|
Debug::Error("Monitor::CopyTextureFromGpuToCpu() => GetDevice()->CreateTexture2D() failed.");
|
||||||
return;
|
return;
|
||||||
@@ -353,7 +372,7 @@ void Monitor::CopyTextureFromGpuToCpu(ID3D11Texture2D* texture)
|
|||||||
|
|
||||||
{
|
{
|
||||||
ComPtr<ID3D11DeviceContext> context;
|
ComPtr<ID3D11DeviceContext> context;
|
||||||
GetDevice()->GetImmediateContext(&context);
|
GetUnityDevice()->GetImmediateContext(&context);
|
||||||
context->CopyResource(textureForGetPixels_.Get(), texture);
|
context->CopyResource(textureForGetPixels_.Get(), texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ public:
|
|||||||
DuplicatorState GetDuplicatorState() const;
|
DuplicatorState GetDuplicatorState() const;
|
||||||
void SetUnityTexture(ID3D11Texture2D* texture);
|
void SetUnityTexture(ID3D11Texture2D* texture);
|
||||||
ID3D11Texture2D* GetUnityTexture() const;
|
ID3D11Texture2D* GetUnityTexture() const;
|
||||||
|
HANDLE GetSharedTextureHandle() const;
|
||||||
void GetName(char* buf, int len) const;
|
void GetName(char* buf, int len) const;
|
||||||
bool IsPrimary() const;
|
bool IsPrimary() const;
|
||||||
bool HasBeenUpdated() const;
|
bool HasBeenUpdated() const;
|
||||||
|
|||||||
@@ -368,6 +368,16 @@ extern "C"
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UNITY_INTERFACE_EXPORT void* UNITY_INTERFACE_API GetSharedTextureHandle(int id)
|
||||||
|
{
|
||||||
|
if (!g_manager) return nullptr;
|
||||||
|
if (auto monitor = g_manager->GetMonitor(id))
|
||||||
|
{
|
||||||
|
return monitor->GetSharedTextureHandle();
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetMoveRectCount(int id)
|
UNITY_INTERFACE_EXPORT int UNITY_INTERFACE_API GetMoveRectCount(int id)
|
||||||
{
|
{
|
||||||
if (!g_manager) return -1;
|
if (!g_manager) return -1;
|
||||||
|
|||||||
@@ -21,33 +21,33 @@
|
|||||||
<PropertyGroup Label="Globals">
|
<PropertyGroup Label="Globals">
|
||||||
<ProjectGuid>{BC5DE2F9-BDCC-4449-A017-E5642E04BB56}</ProjectGuid>
|
<ProjectGuid>{BC5DE2F9-BDCC-4449-A017-E5642E04BB56}</ProjectGuid>
|
||||||
<RootNamespace>uDesktopDuplication</RootNamespace>
|
<RootNamespace>uDesktopDuplication</RootNamespace>
|
||||||
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
|
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||||
<ProjectName>uDesktopDuplication</ProjectName>
|
<ProjectName>uDesktopDuplication</ProjectName>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||||
<UseDebugLibraries>true</UseDebugLibraries>
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
<PlatformToolset>v140</PlatformToolset>
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||||
<UseDebugLibraries>true</UseDebugLibraries>
|
<UseDebugLibraries>true</UseDebugLibraries>
|
||||||
<PlatformToolset>v140</PlatformToolset>
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||||
<UseDebugLibraries>false</UseDebugLibraries>
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
<PlatformToolset>v140</PlatformToolset>
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||||
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
<ConfigurationType>DynamicLibrary</ConfigurationType>
|
||||||
<UseDebugLibraries>false</UseDebugLibraries>
|
<UseDebugLibraries>false</UseDebugLibraries>
|
||||||
<PlatformToolset>v140</PlatformToolset>
|
<PlatformToolset>v142</PlatformToolset>
|
||||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||||
<CharacterSet>MultiByte</CharacterSet>
|
<CharacterSet>MultiByte</CharacterSet>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user