diff options
Diffstat (limited to 'src/win/graph/direct')
-rw-r--r-- | src/win/graph/direct/CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/win/graph/direct/WindowPainter.cpp | 20 | ||||
-rw-r--r-- | src/win/graph/direct/WindowRenderTarget.cpp | 81 |
3 files changed, 105 insertions, 0 deletions
diff --git a/src/win/graph/direct/CMakeLists.txt b/src/win/graph/direct/CMakeLists.txt index 5505b0b5..070b7b51 100644 --- a/src/win/graph/direct/CMakeLists.txt +++ b/src/win/graph/direct/CMakeLists.txt @@ -8,6 +8,8 @@ add_library(cru_win_graph_direct STATIC Painter.cpp Resource.cpp TextLayout.cpp + WindowPainter.cpp + WindowRenderTarget.cpp ) target_sources(cru_win_graph_direct PUBLIC ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Brush.hpp @@ -20,6 +22,8 @@ target_sources(cru_win_graph_direct PUBLIC ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Painter.hpp ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Resource.hpp ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/TextLayout.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/WindowPainter.hpp + ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/WindowRenderTarget.hpp ) target_link_libraries(cru_win_graph_direct PUBLIC D3D11 D2d1 DWrite) target_link_libraries(cru_win_graph_direct PUBLIC cru_win_base cru_platform_graph) diff --git a/src/win/graph/direct/WindowPainter.cpp b/src/win/graph/direct/WindowPainter.cpp new file mode 100644 index 00000000..74f7da7a --- /dev/null +++ b/src/win/graph/direct/WindowPainter.cpp @@ -0,0 +1,20 @@ +#include "cru/win/graph/direct/WindowPainter.hpp" + +#include "cru/win/graph/direct/Exception.hpp" +#include "cru/win/graph/direct/Factory.hpp" +#include "cru/win/graph/direct/WindowRenderTarget.hpp" + +namespace cru::platform::graph::win::direct { +D2DWindowPainter::D2DWindowPainter(D2DWindowRenderTarget* render_target) + : D2DPainter(render_target->GetD2D1DeviceContext()), + render_target_(render_target) { + render_target_->GetD2D1DeviceContext()->BeginDraw(); +} + +D2DWindowPainter::~D2DWindowPainter() { EndDraw(); } + +void D2DWindowPainter::DoEndDraw() { + ThrowIfFailed(render_target_->GetD2D1DeviceContext()->EndDraw()); + render_target_->Present(); +} +} // namespace cru::platform::graph::win::direct diff --git a/src/win/graph/direct/WindowRenderTarget.cpp b/src/win/graph/direct/WindowRenderTarget.cpp new file mode 100644 index 00000000..d26fccf6 --- /dev/null +++ b/src/win/graph/direct/WindowRenderTarget.cpp @@ -0,0 +1,81 @@ +#include "cru/win/graph/direct/WindowRenderTarget.hpp" + +#include "cru/win/graph/direct/Exception.hpp" +#include "cru/win/graph/direct/Factory.hpp" + +namespace cru::platform::graph::win::direct { +D2DWindowRenderTarget::D2DWindowRenderTarget( + gsl::not_null<DirectGraphFactory*> factory, HWND hwnd) + : factory_(factory), hwnd_(hwnd) { + const auto d3d11_device = factory->GetD3D11Device(); + const auto dxgi_factory = factory->GetDxgiFactory(); + + d2d1_device_context_ = factory->CreateD2D1DeviceContext(); + d2d1_device_context_->SetUnitMode(D2D1_UNIT_MODE_DIPS); + + // Allocate a descriptor. + DXGI_SWAP_CHAIN_DESC1 swap_chain_desc; + swap_chain_desc.Width = 0; // use automatic sizing + swap_chain_desc.Height = 0; + swap_chain_desc.Format = + DXGI_FORMAT_B8G8R8A8_UNORM; // this is the most common swapchain format + swap_chain_desc.Stereo = false; + swap_chain_desc.SampleDesc.Count = 1; // don't use multi-sampling + swap_chain_desc.SampleDesc.Quality = 0; + swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + swap_chain_desc.BufferCount = 2; // use double buffering to enable flip + swap_chain_desc.Scaling = DXGI_SCALING_NONE; + swap_chain_desc.SwapEffect = + DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // all apps must use this SwapEffect + swap_chain_desc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED; + swap_chain_desc.Flags = 0; + + // Get the final swap chain for this window from the DXGI factory. + ThrowIfFailed(dxgi_factory->CreateSwapChainForHwnd( + d3d11_device, hwnd, &swap_chain_desc, nullptr, nullptr, + &dxgi_swap_chain_)); + + CreateTargetBitmap(); +} + +void D2DWindowRenderTarget::SetDpi(float x, float y) { + d2d1_device_context_->SetDpi(x, y); +} + +void D2DWindowRenderTarget::ResizeBuffer(const int width, const int height) { + // 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(); +} + +void D2DWindowRenderTarget::Present() { + ThrowIfFailed(dxgi_swap_chain_->Present(1, 0)); +} + +void D2DWindowRenderTarget::CreateTargetBitmap() { + Expects(target_bitmap_ == nullptr); // target bitmap must not exist. + + // Direct2D needs the dxgi version of the backbuffer surface pointer. + Microsoft::WRL::ComPtr<IDXGISurface> dxgi_back_buffer; + ThrowIfFailed( + dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer))); + + float dpi_x, dpi_y; + d2d1_device_context_->GetDpi(&dpi_x, &dpi_y); + + auto bitmap_properties = D2D1::BitmapProperties1( + D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW, + D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE), + dpi_x, dpi_y); + + // Get a D2D surface from the DXGI back buffer to use as the D2D render + // target. + ThrowIfFailed(d2d1_device_context_->CreateBitmapFromDxgiSurface( + dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); + + d2d1_device_context_->SetTarget(target_bitmap_.Get()); +} +} // namespace cru::platform::graph::win::direct |