draw cursor texture to unity texture instead of cached texture.

This commit is contained in:
hecomi
2017-01-08 00:02:41 +09:00
parent 7237d283ef
commit da072aa9f3
9 changed files with 45 additions and 23 deletions
@@ -62,16 +62,16 @@ void Cursor::UpdateBuffer(Duplicator* duplicator, const DXGI_OUTDUPL_FRAME_INFO&
}
void Cursor::Draw(
void Cursor::UpdateTexture(
Duplicator* duplicator,
const ComPtr<ID3D11Texture2D>& targetTexture)
const ComPtr<ID3D11Texture2D>& desktopTexture)
{
auto monitor = duplicator->GetMonitor();
// Check desktop texure
if (targetTexture == nullptr)
if (desktopTexture == nullptr)
{
Debug::Error("Cursor::UpdateTexture() => target texture is null.");
Debug::Error("Cursor::UpdateTexture() => Desktop texture is null.");
return;
}
@@ -194,7 +194,7 @@ void Cursor::Draw(
return;
}
D3D11_BOX capturedImageArea
capturedImageArea_ = D3D11_BOX
{
static_cast<UINT>(capturedImageLeft),
static_cast<UINT>(capturedImageTop),
@@ -218,7 +218,7 @@ void Cursor::Draw(
desc.Usage = D3D11_USAGE_STAGING;
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.MiscFlags = 0;
desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
if (FAILED(duplicator->GetDevice()->CreateTexture2D(&desc, nullptr, &texture)))
{
@@ -231,7 +231,7 @@ void Cursor::Draw(
{
ComPtr<ID3D11DeviceContext> context;
duplicator->GetDevice()->GetImmediateContext(&context);
context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, targetTexture.Get(), 0, &capturedImageArea);
context->CopySubresourceRegion(texture.Get(), 0, 0, 0, 0, desktopTexture.Get(), 0, &capturedImageArea_);
}
// Get mapped surface to access pixels in CPU
@@ -359,12 +359,6 @@ void Cursor::Draw(
}
}
{
ComPtr<ID3D11DeviceContext> context;
duplicator->GetDevice()->GetImmediateContext(&context);
context->UpdateSubresource(targetTexture.Get(), 0, &capturedImageArea, bgraBuffer_.Get(), capturedImageWidth * 4, 0);
}
if (FAILED(surface->Unmap()))
{
Debug::Error("Cursor::UpdateTexture() => surface->Unmap() failed.");
@@ -373,6 +367,21 @@ void Cursor::Draw(
}
void Cursor::Draw(const Microsoft::WRL::ComPtr<ID3D11Texture2D>& texture)
{
if (texture == nullptr)
{
Debug::Error("Cursor::UpdateTexture() => Desktop texture is null.");
return;
}
const auto capturedImageWidth = capturedImageArea_.right - capturedImageArea_.left;
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
context->UpdateSubresource(texture.Get(), 0, &capturedImageArea_, bgraBuffer_.Get(), capturedImageWidth * 4, 0);
}
void Cursor::GetTexture(ID3D11Texture2D* texture)
{
if (!bgraBuffer_)
@@ -17,9 +17,10 @@ public:
void UpdateBuffer(
Duplicator* duplicator,
const DXGI_OUTDUPL_FRAME_INFO& frameInfo);
void Draw(
Duplicator* duplicator,
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& targetTexture);
void UpdateTexture(
Duplicator* duplicator,
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& desktopTexture);
void Draw(const Microsoft::WRL::ComPtr<ID3D11Texture2D>& texture);
void GetTexture(ID3D11Texture2D* texture);
bool IsVisible() const;
@@ -38,4 +39,5 @@ private:
Buffer<BYTE> bgraBuffer_;
DXGI_OUTDUPL_POINTER_SHAPE_INFO shapeInfo_;
LARGE_INTEGER timestamp_;
D3D11_BOX capturedImageArea_;
};
@@ -148,7 +148,12 @@ private:
#define UDD_FUNCTION_SCOPE_TIMER \
ScopedTimer _timer_##__COUNTER__([](std::chrono::microseconds us) \
{ \
Debug::Log(__FUNCTION__, "@", __FILE__, ":", __LINE__, " => ", us.count(), " (us)"); \
Debug::Log(__FUNCTION__, "@", __FILE__, ":", __LINE__, " => ", us.count(), " [us]"); \
});
#define UDD_SCOPE_TIMER(Name) \
ScopedTimer _timer_##__COUNTER__([](std::chrono::microseconds us) \
{ \
Debug::Log(#Name, " => ", us.count(), " [us]"); \
});
#else
#define UDD_FUNCTION_SCOPE_TIMER
@@ -15,7 +15,7 @@ using namespace Microsoft::WRL;
Microsoft::WRL::ComPtr<ID3D11Texture2D> SharedTextureWrapper::Get()
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& SharedTextureWrapper::Get() const
{
return pointer_;
}
@@ -12,7 +12,7 @@ class SharedTextureWrapper
{
friend class IsolatedD3D11Device;
public:
Microsoft::WRL::ComPtr<ID3D11Texture2D> Get();
const Microsoft::WRL::ComPtr<ID3D11Texture2D>& Get() const;
Microsoft::WRL::ComPtr<ID3D11Texture2D> Lock();
void Unlock();
bool IsLocked() const;
@@ -327,9 +327,12 @@ void Duplicator::Duplicate(UINT timeout)
if (!sharedTexture) return;
ScopedReleaser sharedTextureReleaser([&] { sharedTextureWrapper->Unlock(); });
ComPtr<ID3D11DeviceContext> context;
device_->GetDevice()->GetImmediateContext(&context);
context->CopyResource(sharedTexture.Get(), texture.Get());
{
UDD_SCOPE_TIMER(CopyResource);
ComPtr<ID3D11DeviceContext> context;
device_->GetDevice()->GetImmediateContext(&context);
context->CopyResource(sharedTexture.Get(), texture.Get());
}
UpdateCursor(sharedTexture, frameInfo);
UpdateMetadata(frameInfo.TotalMetadataBufferSize);
@@ -362,7 +365,7 @@ void Duplicator::UpdateCursor(
{
auto cursor = manager->GetCursor();
cursor->UpdateBuffer(this, frameInfo);
cursor->Draw(this, texture);
cursor->UpdateTexture(this, texture);
}
}
@@ -98,6 +98,9 @@ void Monitor::Render()
ComPtr<ID3D11DeviceContext> context;
GetDevice()->GetImmediateContext(&context);
context->CopyResource(unityTexture_, sharedTexture.Get());
auto cursor = GetMonitorManager()->GetCursor();
cursor->Draw(unityTexture_);
}
}