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(
const ComPtr<ID3D11Texture2D>& src,
UINT index)
Microsoft::WRL::ComPtr<ID3D11Texture2D> IsolatedD3D11Device::GetCompatibleSharedTexture(
const ComPtr<ID3D11Texture2D>& src)
{
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;
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;
texture->GetDesc(&targetDesc);
cachedSharedTexture_->GetDesc(&targetDesc);
if (targetDesc.Format == srcDesc.Format &&
targetDesc.Width == srcDesc.Width &&
targetDesc.Height == srcDesc.Height)
{
return sharedTextureWrapper;
return cachedSharedTexture_;
}
}
// for sharing this texture with unity device
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.");
return nullptr;
}
return sharedTextureWrapper;
return cachedSharedTexture_;
}
@@ -7,36 +7,19 @@
#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
class IsolatedD3D11Device
{
public:
explicit IsolatedD3D11Device(UINT cachedTextureNum);
IsolatedD3D11Device();
~IsolatedD3D11Device();
HRESULT Create(const Microsoft::WRL::ComPtr<IDXGIAdapter>& adapter);
Microsoft::WRL::ComPtr<ID3D11Device> GetDevice();
std::shared_ptr<SharedTextureWrapper> GetCompatibleSharedTexture(
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& src,
UINT index);
Microsoft::WRL::ComPtr<ID3D11Texture2D> GetCompatibleSharedTexture(
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& src);
private:
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()
{
const UINT cachedTextureNum = 2;
device_ = std::make_shared<IsolatedD3D11Device>(cachedTextureNum);
device_ = std::make_shared<IsolatedD3D11Device>();
if (FAILED(device_->Create(monitor_->GetAdapter())))
{
@@ -315,18 +314,13 @@ void Duplicator::Duplicate(UINT timeout)
return;
}
//auto sharedTextureWrapper = device_->GetCompatibleSharedTexture(texture, lastFrameId_ % 2);
auto sharedTextureWrapper = device_->GetCompatibleSharedTexture(texture, 0);
if (!sharedTextureWrapper)
auto sharedTexture = device_->GetCompatibleSharedTexture(texture);
if (!sharedTexture)
{
Debug::Error("Duplicator::Duplicate() => Shared texture is null.");
return;
}
auto sharedTexture = sharedTextureWrapper->Lock();
if (!sharedTexture) return;
ScopedReleaser sharedTextureReleaser([&] { sharedTextureWrapper->Unlock(); });
{
UDD_SCOPE_TIMER(CopyResource);
ComPtr<ID3D11DeviceContext> context;
@@ -342,7 +336,7 @@ void Duplicator::Duplicate(UINT timeout)
lastFrame_ = Frame
{
lastFrameId_++,
sharedTextureWrapper,
sharedTexture,
frameInfo,
metaData_
};
@@ -45,7 +45,7 @@ public:
struct Frame
{
UINT id;
std::shared_ptr<class SharedTextureWrapper> texture;
Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
DXGI_OUTDUPL_FRAME_INFO info;
Metadata metaData;
};
@@ -75,15 +75,10 @@ void Monitor::Render()
if (frame.id == lastFrameId_) return;
lastFrameId_ = frame.id;
const auto& sharedTextureWrapper = frame.texture;
auto sharedTexture = sharedTextureWrapper->Lock();
if (!sharedTexture) return;
ScopedReleaser sharedTextureReleaser([&] { sharedTextureWrapper->Unlock(); });
if (unityTexture_)
{
D3D11_TEXTURE2D_DESC srcDesc, dstDesc;
sharedTexture->GetDesc(&srcDesc);
frame.texture->GetDesc(&srcDesc);
unityTexture_->GetDesc(&dstDesc);
if (srcDesc.Width != dstDesc.Width ||
srcDesc.Height != dstDesc.Height)
@@ -97,7 +92,7 @@ void Monitor::Render()
{
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
context->CopyResource(unityTexture_, sharedTexture.Get());
context->CopyResource(unityTexture_, frame.texture.Get());
auto cursor = GetMonitorManager()->GetCursor();
cursor->Draw(unityTexture_);