diff options
Diffstat (limited to 'src/win/native')
-rw-r--r-- | src/win/native/ui_application.cpp | 3 | ||||
-rw-r--r-- | src/win/native/window.cpp | 2 | ||||
-rw-r--r-- | src/win/native/window_d2d_painter.cpp | 21 | ||||
-rw-r--r-- | src/win/native/window_d2d_painter.hpp | 8 | ||||
-rw-r--r-- | src/win/native/window_render_target.cpp | 25 |
5 files changed, 20 insertions, 39 deletions
diff --git a/src/win/native/ui_application.cpp b/src/win/native/ui_application.cpp index 0ae8ee81..75fce6ce 100644 --- a/src/win/native/ui_application.cpp +++ b/src/win/native/ui_application.cpp @@ -28,8 +28,7 @@ WinUiApplication::WinUiApplication() { instance_handle_ = ::GetModuleHandleW(nullptr); if (!instance_handle_) - throw Win32Error(::GetLastError(), - "Failed to get module(instance) handle."); + throw Win32Error("Failed to get module(instance) handle."); log::Logger::GetInstance()->AddSource( std::make_unique<::cru::platform::win::WinDebugLoggerSource>()); diff --git a/src/win/native/window.cpp b/src/win/native/window.cpp index f996af02..d3c12a44 100644 --- a/src/win/native/window.cpp +++ b/src/win/native/window.cpp @@ -163,7 +163,7 @@ void WinNativeWindow::RequestRepaint() { } std::unique_ptr<graph::IPainter> WinNativeWindow::BeginPaint() { - return std::make_unique<WindowD2DPainter>(this); + return std::make_unique<WindowD2DPainter>(window_render_target_.get()); } void WinNativeWindow::SetCursor(std::shared_ptr<ICursor> cursor) { diff --git a/src/win/native/window_d2d_painter.cpp b/src/win/native/window_d2d_painter.cpp index ab74a964..023559f4 100644 --- a/src/win/native/window_d2d_painter.cpp +++ b/src/win/native/window_d2d_painter.cpp @@ -9,25 +9,16 @@ namespace cru::platform::native::win { using namespace cru::platform::graph::win::direct; -WindowD2DPainter::WindowD2DPainter(WinNativeWindow* window) - : D2DPainter(window->GetWindowRenderTarget() - ->GetDirectFactory() - ->GetD2D1DeviceContext()), - window_(window) { - window->GetWindowRenderTarget()->SetAsTarget(); - window->GetWindowRenderTarget() - ->GetDirectFactory() - ->GetD2D1DeviceContext() - ->BeginDraw(); +WindowD2DPainter::WindowD2DPainter(WindowRenderTarget* render_target) + : D2DPainter(render_target->GetD2D1DeviceContext()), + render_target_(render_target) { + render_target_->GetD2D1DeviceContext()->BeginDraw(); } WindowD2DPainter::~WindowD2DPainter() { EndDraw(); } void WindowD2DPainter::DoEndDraw() { - ThrowIfFailed(window_->GetWindowRenderTarget() - ->GetDirectFactory() - ->GetD2D1DeviceContext() - ->EndDraw()); - window_->GetWindowRenderTarget()->Present(); + ThrowIfFailed(render_target_->GetD2D1DeviceContext()->EndDraw()); + render_target_->Present(); } } // namespace cru::platform::native::win diff --git a/src/win/native/window_d2d_painter.hpp b/src/win/native/window_d2d_painter.hpp index 6877babd..40a5dee6 100644 --- a/src/win/native/window_d2d_painter.hpp +++ b/src/win/native/window_d2d_painter.hpp @@ -1,19 +1,21 @@ #pragma once #include "cru/win/graph/direct/painter.hpp" -#include "cru/win/native/window.hpp" +#include "cru/win/native/window_render_target.hpp" namespace cru::platform::native::win { class WindowD2DPainter : public graph::win::direct::D2DPainter { public: - explicit WindowD2DPainter(WinNativeWindow* window); + explicit WindowD2DPainter(WindowRenderTarget* window); + CRU_DELETE_COPY(WindowD2DPainter) CRU_DELETE_MOVE(WindowD2DPainter) + ~WindowD2DPainter() override; protected: void DoEndDraw() override; private: - WinNativeWindow* window_; + WindowRenderTarget* render_target_; }; } // namespace cru::platform::native::win diff --git a/src/win/native/window_render_target.cpp b/src/win/native/window_render_target.cpp index f15aeb6e..23ad2afa 100644 --- a/src/win/native/window_render_target.cpp +++ b/src/win/native/window_render_target.cpp @@ -15,6 +15,8 @@ WindowRenderTarget::WindowRenderTarget(DirectGraphFactory* factory, HWND hwnd) const auto d3d11_device = factory->GetD3D11Device(); const auto dxgi_factory = factory->GetDxgiFactory(); + d2d1_device_context_ = factory->CreateD2D1DeviceContext(); + // Allocate a descriptor. DXGI_SWAP_CHAIN_DESC1 swap_chain_desc; swap_chain_desc.Width = 0; // use automatic sizing @@ -41,27 +43,12 @@ WindowRenderTarget::WindowRenderTarget(DirectGraphFactory* factory, HWND hwnd) } void WindowRenderTarget::ResizeBuffer(const int width, const int height) { - const auto factory = factory_; - const auto d2d1_device_context = factory->GetD2D1DeviceContext(); - - Microsoft::WRL::ComPtr<ID2D1Image> old_target; - d2d1_device_context->GetTarget(&old_target); - const auto target_this = old_target == this->target_bitmap_; - if (target_this) d2d1_device_context->SetTarget(nullptr); - - old_target = nullptr; + // In order to resize buffer, we need to untarget the buffer first. + d2d1_device_context_->SetTarget(nullptr); target_bitmap_ = nullptr; - ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0)); - CreateTargetBitmap(); - - if (target_this) d2d1_device_context->SetTarget(target_bitmap_.Get()); -} - -void WindowRenderTarget::SetAsTarget() { - factory_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.Get()); } void WindowRenderTarget::Present() { @@ -85,7 +72,9 @@ void WindowRenderTarget::CreateTargetBitmap() { // Get a D2D surface from the DXGI back buffer to use as the D2D render // target. - ThrowIfFailed(factory_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( + ThrowIfFailed(d2d1_device_context_->CreateBitmapFromDxgiSurface( dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); + + d2d1_device_context_->SetTarget(target_bitmap_.Get()); } } // namespace cru::platform::native::win |