remove unnecessary codes.

This commit is contained in:
hecomi
2017-01-08 00:09:24 +09:00
parent da072aa9f3
commit 808a71ed6d
7 changed files with 21 additions and 89 deletions
@@ -15,39 +15,8 @@ using namespace Microsoft::WRL;
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& SharedTextureWrapper::Get() const IsolatedD3D11Device::IsolatedD3D11Device()
{ {
return pointer_;
}
Microsoft::WRL::ComPtr<ID3D11Texture2D> SharedTextureWrapper::Lock()
{
if (locked_) return nullptr;
locked_ = true;
return pointer_;
}
void SharedTextureWrapper::Unlock()
{
locked_ = false;
}
bool SharedTextureWrapper::IsLocked() const
{
return locked_;
}
IsolatedD3D11Device::IsolatedD3D11Device(UINT cachedTextureNum)
{
for (UINT i = 0; i < cachedTextureNum; ++i)
{
cachedSharedTextures_.push_back(std::make_shared<SharedTextureWrapper>());
}
} }
@@ -96,42 +65,33 @@ ComPtr<ID3D11Device> IsolatedD3D11Device::GetDevice()
} }
std::shared_ptr<SharedTextureWrapper> IsolatedD3D11Device::GetCompatibleSharedTexture( Microsoft::WRL::ComPtr<ID3D11Texture2D> IsolatedD3D11Device::GetCompatibleSharedTexture(
const ComPtr<ID3D11Texture2D>& src, const ComPtr<ID3D11Texture2D>& src)
UINT index)
{ {
if (index < 0 || index >= cachedSharedTextures_.size())
{
Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => ", index, " is out of cachedTextures range.");
return nullptr;
}
auto& sharedTextureWrapper = cachedSharedTextures_.at(index);
auto& texture = sharedTextureWrapper->pointer_; // raw pointer
// check if the format and size of the current texture are same as the source one
D3D11_TEXTURE2D_DESC srcDesc; D3D11_TEXTURE2D_DESC srcDesc;
src->GetDesc(&srcDesc); src->GetDesc(&srcDesc);
if (texture)
// check if the format and size of the current texture are same as the source one
if (cachedSharedTexture_)
{ {
D3D11_TEXTURE2D_DESC targetDesc; D3D11_TEXTURE2D_DESC targetDesc;
texture->GetDesc(&targetDesc); cachedSharedTexture_->GetDesc(&targetDesc);
if (targetDesc.Format == srcDesc.Format && if (targetDesc.Format == srcDesc.Format &&
targetDesc.Width == srcDesc.Width && targetDesc.Width == srcDesc.Width &&
targetDesc.Height == srcDesc.Height) targetDesc.Height == srcDesc.Height)
{ {
return sharedTextureWrapper; return cachedSharedTexture_;
} }
} }
// for sharing this texture with unity device // for sharing this texture with unity device
srcDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; srcDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
if (FAILED(device_->CreateTexture2D(&srcDesc, nullptr, &texture))) if (FAILED(device_->CreateTexture2D(&srcDesc, nullptr, &cachedSharedTexture_)))
{ {
Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => Creating shared texture failed."); Debug::Error("IsolatedD3D11Device::GetCompatibleSharedTexture() => Creating shared texture failed.");
return nullptr; return nullptr;
} }
return sharedTextureWrapper; return cachedSharedTexture_;
} }
@@ -7,36 +7,19 @@
#include <wrl/client.h> #include <wrl/client.h>
// Shared texture wrapper to manager locking state
class SharedTextureWrapper
{
friend class IsolatedD3D11Device;
public:
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& Get() const;
Microsoft::WRL::ComPtr<ID3D11Texture2D> Lock();
void Unlock();
bool IsLocked() const;
private:
std::atomic<bool> locked_ = false;
Microsoft::WRL::ComPtr<ID3D11Texture2D> pointer_;
};
// Thraed safe self created ID3D11Device from specified adapter // Thraed safe self created ID3D11Device from specified adapter
class IsolatedD3D11Device class IsolatedD3D11Device
{ {
public: public:
explicit IsolatedD3D11Device(UINT cachedTextureNum); IsolatedD3D11Device();
~IsolatedD3D11Device(); ~IsolatedD3D11Device();
HRESULT Create(const Microsoft::WRL::ComPtr<IDXGIAdapter>& adapter); HRESULT Create(const Microsoft::WRL::ComPtr<IDXGIAdapter>& adapter);
Microsoft::WRL::ComPtr<ID3D11Device> GetDevice(); Microsoft::WRL::ComPtr<ID3D11Device> GetDevice();
std::shared_ptr<SharedTextureWrapper> GetCompatibleSharedTexture( Microsoft::WRL::ComPtr<ID3D11Texture2D> GetCompatibleSharedTexture(
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& src, const Microsoft::WRL::ComPtr<ID3D11Texture2D>& src);
UINT index);
private: private:
Microsoft::WRL::ComPtr<ID3D11Device> device_; Microsoft::WRL::ComPtr<ID3D11Device> device_;
std::vector<std::shared_ptr<SharedTextureWrapper>> cachedSharedTextures_; Microsoft::WRL::ComPtr<ID3D11Texture2D> cachedSharedTexture_;
}; };
@@ -33,8 +33,7 @@ Duplicator::~Duplicator()
void Duplicator::InitializeDevice() void Duplicator::InitializeDevice()
{ {
const UINT cachedTextureNum = 2; device_ = std::make_shared<IsolatedD3D11Device>();
device_ = std::make_shared<IsolatedD3D11Device>(cachedTextureNum);
if (FAILED(device_->Create(monitor_->GetAdapter()))) if (FAILED(device_->Create(monitor_->GetAdapter())))
{ {
@@ -315,18 +314,13 @@ void Duplicator::Duplicate(UINT timeout)
return; return;
} }
//auto sharedTextureWrapper = device_->GetCompatibleSharedTexture(texture, lastFrameId_ % 2); auto sharedTexture = device_->GetCompatibleSharedTexture(texture);
auto sharedTextureWrapper = device_->GetCompatibleSharedTexture(texture, 0); if (!sharedTexture)
if (!sharedTextureWrapper)
{ {
Debug::Error("Duplicator::Duplicate() => Shared texture is null."); Debug::Error("Duplicator::Duplicate() => Shared texture is null.");
return; return;
} }
auto sharedTexture = sharedTextureWrapper->Lock();
if (!sharedTexture) return;
ScopedReleaser sharedTextureReleaser([&] { sharedTextureWrapper->Unlock(); });
{ {
UDD_SCOPE_TIMER(CopyResource); UDD_SCOPE_TIMER(CopyResource);
ComPtr<ID3D11DeviceContext> context; ComPtr<ID3D11DeviceContext> context;
@@ -342,7 +336,7 @@ void Duplicator::Duplicate(UINT timeout)
lastFrame_ = Frame lastFrame_ = Frame
{ {
lastFrameId_++, lastFrameId_++,
sharedTextureWrapper, sharedTexture,
frameInfo, frameInfo,
metaData_ metaData_
}; };
@@ -45,7 +45,7 @@ public:
struct Frame struct Frame
{ {
UINT id; UINT id;
std::shared_ptr<class SharedTextureWrapper> texture; Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
DXGI_OUTDUPL_FRAME_INFO info; DXGI_OUTDUPL_FRAME_INFO info;
Metadata metaData; Metadata metaData;
}; };
@@ -75,15 +75,10 @@ void Monitor::Render()
if (frame.id == lastFrameId_) return; if (frame.id == lastFrameId_) return;
lastFrameId_ = frame.id; lastFrameId_ = frame.id;
const auto& sharedTextureWrapper = frame.texture;
auto sharedTexture = sharedTextureWrapper->Lock();
if (!sharedTexture) return;
ScopedReleaser sharedTextureReleaser([&] { sharedTextureWrapper->Unlock(); });
if (unityTexture_) if (unityTexture_)
{ {
D3D11_TEXTURE2D_DESC srcDesc, dstDesc; D3D11_TEXTURE2D_DESC srcDesc, dstDesc;
sharedTexture->GetDesc(&srcDesc); frame.texture->GetDesc(&srcDesc);
unityTexture_->GetDesc(&dstDesc); unityTexture_->GetDesc(&dstDesc);
if (srcDesc.Width != dstDesc.Width || if (srcDesc.Width != dstDesc.Width ||
srcDesc.Height != dstDesc.Height) srcDesc.Height != dstDesc.Height)
@@ -97,7 +92,7 @@ void Monitor::Render()
{ {
ComPtr<ID3D11DeviceContext> context; ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context); GetDevice()->GetImmediateContext(&context);
context->CopyResource(unityTexture_, sharedTexture.Get()); context->CopyResource(unityTexture_, frame.texture.Get());
auto cursor = GetMonitorManager()->GetCursor(); auto cursor = GetMonitorManager()->GetCursor();
cursor->Draw(unityTexture_); cursor->Draw(unityTexture_);