use shared texture.

This commit is contained in:
hecomi
2019-09-21 02:21:53 +09:00
parent 20589c23b9
commit cfdfb1f50e
12 changed files with 60 additions and 16 deletions
@@ -163,6 +163,8 @@ public static class Lib
[DllImport("uDesktopDuplication")]
public static extern int SetTexturePtr(int id, IntPtr ptr);
[DllImport("uDesktopDuplication")]
public static extern IntPtr GetSharedTextureHandle(int id);
[DllImport("uDesktopDuplication")]
public static extern int GetMoveRectCount(int id);
[DllImport("uDesktopDuplication", EntryPoint = "GetMoveRects")]
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();
}
@@ -62,7 +62,7 @@ LUID GetUnityAdapterLuid()
{
UDD_FUNCTION_SCOPE_TIMER
const auto device = GetDevice();
const auto device = GetUnityDevice();
Microsoft::WRL::ComPtr<IDXGIDevice1> dxgiDevice;
if (FAILED(device->QueryInterface(IID_PPV_ARGS(&dxgiDevice)))){
@@ -16,7 +16,7 @@ IUnityInterfaces* GetUnity();
// ID3D11Device (in Unity) getter
struct ID3D11Device;
Microsoft::WRL::ComPtr<ID3D11Device> GetDevice();
Microsoft::WRL::ComPtr<ID3D11Device> GetUnityDevice();
// Manager getter
class MonitorManager;
@@ -383,7 +383,7 @@ void Cursor::Draw(const Microsoft::WRL::ComPtr<ID3D11Texture2D>& texture)
const auto capturedImageWidth = capturedImageArea_.right - capturedImageArea_.left;
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
GetUnityDevice()->GetImmediateContext(&context);
context->UpdateSubresource(texture.Get(), 0, &capturedImageArea_, bgraBuffer_.Get(), capturedImageWidth * 4, 0);
}
@@ -418,7 +418,7 @@ void Cursor::GetTexture(ID3D11Texture2D* texture)
}
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
GetUnityDevice()->GetImmediateContext(&context);
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());
}
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);
UpdateMetadata(frameInfo.TotalMetadataBufferSize);
@@ -315,6 +325,7 @@ void Duplicator::Duplicate(UINT timeout)
{
lastFrameId_++,
sharedTexture,
sharedHandle,
frameInfo,
metaData_
};
@@ -44,8 +44,9 @@ public:
struct Frame
{
UINT id;
Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
UINT id = 0;
Microsoft::WRL::ComPtr<ID3D11Texture2D> texture = nullptr;
HANDLE textureHandle = nullptr;
DXGI_OUTDUPL_FRAME_INFO info;
Metadata metaData;
};
@@ -117,9 +117,19 @@ void Monitor::Render()
}
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;
GetDevice()->GetImmediateContext(&context);
context->CopyResource(unityTexture_, frame.texture.Get());
GetUnityDevice()->GetImmediateContext(&context);
context->CopyResource(unityTexture_, desktopTexture.Get());
auto& manager = GetMonitorManager();
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()
{
return duplicator_->GetDuplication();
@@ -344,7 +363,7 @@ void Monitor::CopyTextureFromGpuToCpu(ID3D11Texture2D* texture)
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0;
if (FAILED(GetDevice()->CreateTexture2D(&desc, nullptr, &textureForGetPixels_)))
if (FAILED(GetUnityDevice()->CreateTexture2D(&desc, nullptr, &textureForGetPixels_)))
{
Debug::Error("Monitor::CopyTextureFromGpuToCpu() => GetDevice()->CreateTexture2D() failed.");
return;
@@ -353,7 +372,7 @@ void Monitor::CopyTextureFromGpuToCpu(ID3D11Texture2D* texture)
{
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
GetUnityDevice()->GetImmediateContext(&context);
context->CopyResource(textureForGetPixels_.Get(), texture);
}
@@ -33,6 +33,7 @@ public:
DuplicatorState GetDuplicatorState() const;
void SetUnityTexture(ID3D11Texture2D* texture);
ID3D11Texture2D* GetUnityTexture() const;
HANDLE GetSharedTextureHandle() const;
void GetName(char* buf, int len) const;
bool IsPrimary() 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)
{
if (!g_manager) return -1;
@@ -21,33 +21,33 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{BC5DE2F9-BDCC-4449-A017-E5642E04BB56}</ProjectGuid>
<RootNamespace>uDesktopDuplication</RootNamespace>
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectName>uDesktopDuplication</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>