diff options
-rw-r--r-- | include/cru/common/platform/win/WinPreConfig.hpp | 8 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Base.hpp | 6 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Factory.hpp | 14 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Image.hpp | 16 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Painter.hpp | 18 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/WindowPainter.hpp | 2 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/WindowRenderTarget.hpp | 4 | ||||
-rw-r--r-- | src/win/graphics/direct/Factory.cpp | 4 | ||||
-rw-r--r-- | src/win/graphics/direct/Image.cpp | 31 | ||||
-rw-r--r-- | src/win/graphics/direct/ImageFactory.cpp | 5 | ||||
-rw-r--r-- | src/win/graphics/direct/Painter.cpp | 107 | ||||
-rw-r--r-- | src/win/graphics/direct/WindowPainter.cpp | 2 |
12 files changed, 122 insertions, 95 deletions
diff --git a/include/cru/common/platform/win/WinPreConfig.hpp b/include/cru/common/platform/win/WinPreConfig.hpp index 881b5f6b..ac397820 100644 --- a/include/cru/common/platform/win/WinPreConfig.hpp +++ b/include/cru/common/platform/win/WinPreConfig.hpp @@ -2,8 +2,6 @@ #include "../../PreConfig.hpp" #ifdef CRU_PLATFORM_WINDOWS - - #define NOMINMAX #define WIN32_LEAN_AND_MEAN #include <Windows.h> @@ -12,10 +10,4 @@ #undef CreateFont #undef CreateEvent -#include <d2d1_2.h> -#include <d3d11.h> -#include <dwrite.h> -#include <dxgi1_2.h> -#include <wrl/client.h> - #endif diff --git a/include/cru/win/graphics/direct/Base.hpp b/include/cru/win/graphics/direct/Base.hpp index b25a1c14..49fc7897 100644 --- a/include/cru/win/graphics/direct/Base.hpp +++ b/include/cru/win/graphics/direct/Base.hpp @@ -1,6 +1,12 @@ #pragma once #include "../../WinPreConfig.hpp" +#include <d2d1_2.h> +#include <d3d11.h> +#include <dwrite.h> +#include <dxgi1_2.h> +#include <wrl/client.h> + #ifdef CRU_PLATFORM_WINDOWS #ifdef CRU_WIN_GRAPHICS_DIRECT_EXPORT_API #define CRU_WIN_GRAPHICS_DIRECT_API __declspec(dllexport) diff --git a/include/cru/win/graphics/direct/Factory.hpp b/include/cru/win/graphics/direct/Factory.hpp index ffa8c764..ba504f0d 100644 --- a/include/cru/win/graphics/direct/Factory.hpp +++ b/include/cru/win/graphics/direct/Factory.hpp @@ -20,8 +20,8 @@ class CRU_WIN_GRAPHICS_DIRECT_API DirectGraphicsFactory public: ID3D11Device* GetD3D11Device() const { return d3d11_device_.Get(); } - ID2D1Factory1* GetD2D1Factory() const { return d2d1_factory_.Get(); } - ID2D1Device* GetD2D1Device() const { return d2d1_device_.Get(); } + ID2D1Factory2* GetD2D1Factory() const { return d2d1_factory_.Get(); } + ID2D1Device1* GetD2D1Device() const { return d2d1_device_.Get(); } IDXGIFactory2* GetDxgiFactory() const { return dxgi_factory_.Get(); } IDWriteFactory* GetDWriteFactory() const { return dwrite_factory_.Get(); } IDWriteFontCollection* GetSystemFontCollection() const { @@ -29,7 +29,7 @@ class CRU_WIN_GRAPHICS_DIRECT_API DirectGraphicsFactory } public: - Microsoft::WRL::ComPtr<ID2D1DeviceContext> CreateD2D1DeviceContext(); + Microsoft::WRL::ComPtr<ID2D1DeviceContext1> CreateD2D1DeviceContext(); // This context should only be used to create graphic resources like brush. // Because graphic resources can be shared if they are created in the same @@ -53,11 +53,9 @@ class CRU_WIN_GRAPHICS_DIRECT_API DirectGraphicsFactory private: Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_; - // ID2D1Factory1 is a interface only available in Windows 8 and Windows 7 with - // update. It is d2d v1.1. - Microsoft::WRL::ComPtr<ID2D1Factory1> d2d1_factory_; - Microsoft::WRL::ComPtr<ID2D1Device> d2d1_device_; - Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context_; + Microsoft::WRL::ComPtr<ID2D1Factory2> d2d1_factory_; + Microsoft::WRL::ComPtr<ID2D1Device1> d2d1_device_; + Microsoft::WRL::ComPtr<ID2D1DeviceContext1> d2d1_device_context_; Microsoft::WRL::ComPtr<IDXGIFactory2> dxgi_factory_; Microsoft::WRL::ComPtr<IDWriteFactory> dwrite_factory_; Microsoft::WRL::ComPtr<IDWriteFontCollection> dwrite_system_font_collection_; diff --git a/include/cru/win/graphics/direct/Image.hpp b/include/cru/win/graphics/direct/Image.hpp index 33d848bd..06448621 100644 --- a/include/cru/win/graphics/direct/Image.hpp +++ b/include/cru/win/graphics/direct/Image.hpp @@ -6,8 +6,8 @@ namespace cru::platform::graphics::win::direct { class CRU_WIN_GRAPHICS_DIRECT_API Direct2DImage : public DirectGraphicsResource, public virtual IImage { public: - explicit Direct2DImage(DirectGraphicsFactory* graphics_factory, - ID2D1Image* d2d_image, bool auto_release); + Direct2DImage(DirectGraphicsFactory* graphics_factory, + Microsoft::WRL::ComPtr<ID2D1Bitmap1> d2d_bitmap); CRU_DELETE_COPY(Direct2DImage) CRU_DELETE_MOVE(Direct2DImage) @@ -15,11 +15,17 @@ class CRU_WIN_GRAPHICS_DIRECT_API Direct2DImage : public DirectGraphicsResource, ~Direct2DImage() override; public: - ID2D1Image* GetD2DImage() const { return d2d_image_; } + float GetWidth() override; + float GetHeight() override; + + std::unique_ptr<IImage> CreateWithRect(const Rect& rect) override; + + const Microsoft::WRL::ComPtr<ID2D1Bitmap1>& GetD2DBitmap() const { + return d2d_bitmap_; + } private: - ID2D1Image* d2d_image_; - bool auto_release_; + Microsoft::WRL::ComPtr<ID2D1Bitmap1> d2d_bitmap_; }; } // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Painter.hpp b/include/cru/win/graphics/direct/Painter.hpp index 11fb5619..57a59357 100644 --- a/include/cru/win/graphics/direct/Painter.hpp +++ b/include/cru/win/graphics/direct/Painter.hpp @@ -7,20 +7,22 @@ #include <vector> namespace cru::platform::graphics::win::direct { -class CRU_WIN_GRAPHICS_DIRECT_API D2DPainter +class CRU_WIN_GRAPHICS_DIRECT_API D2DDeviceContextPainter : public DirectResource, public virtual IPainter, - public virtual IComResource<ID2D1RenderTarget> { + public virtual IComResource<ID2D1DeviceContext1> { public: - explicit D2DPainter(ID2D1RenderTarget* render_target); + explicit D2DDeviceContextPainter(ID2D1DeviceContext1* device_context); - CRU_DELETE_COPY(D2DPainter) - CRU_DELETE_MOVE(D2DPainter) + CRU_DELETE_COPY(D2DDeviceContextPainter) + CRU_DELETE_MOVE(D2DDeviceContextPainter) - ~D2DPainter() override = default; + ~D2DDeviceContextPainter() override = default; public: - ID2D1RenderTarget* GetComInterface() const override { return render_target_; } + ID2D1DeviceContext1* GetComInterface() const override { + return device_context_; + } public: Matrix GetTransform() override; @@ -62,7 +64,7 @@ class CRU_WIN_GRAPHICS_DIRECT_API D2DPainter void CheckValidation(); private: - ID2D1RenderTarget* render_target_; + ID2D1DeviceContext1* device_context_; std::vector<Microsoft::WRL::ComPtr<ID2D1Layer>> layers_; std::vector<Microsoft::WRL::ComPtr<ID2D1DrawingStateBlock>> diff --git a/include/cru/win/graphics/direct/WindowPainter.hpp b/include/cru/win/graphics/direct/WindowPainter.hpp index a39bc2b7..3d66bc2e 100644 --- a/include/cru/win/graphics/direct/WindowPainter.hpp +++ b/include/cru/win/graphics/direct/WindowPainter.hpp @@ -4,7 +4,7 @@ namespace cru::platform::graphics::win::direct { class CRU_WIN_GRAPHICS_DIRECT_API D2DWindowPainter - : public graphics::win::direct::D2DPainter { + : public graphics::win::direct::D2DDeviceContextPainter { public: explicit D2DWindowPainter(D2DWindowRenderTarget* window); diff --git a/include/cru/win/graphics/direct/WindowRenderTarget.hpp b/include/cru/win/graphics/direct/WindowRenderTarget.hpp index 4774a1d3..6b775241 100644 --- a/include/cru/win/graphics/direct/WindowRenderTarget.hpp +++ b/include/cru/win/graphics/direct/WindowRenderTarget.hpp @@ -18,7 +18,7 @@ class CRU_WIN_GRAPHICS_DIRECT_API D2DWindowRenderTarget : public Object { return factory_; } - ID2D1DeviceContext* GetD2D1DeviceContext() { + ID2D1DeviceContext1* GetD2D1DeviceContext() { return d2d1_device_context_.Get(); } @@ -36,7 +36,7 @@ class CRU_WIN_GRAPHICS_DIRECT_API D2DWindowRenderTarget : public Object { private: DirectGraphicsFactory* factory_; HWND hwnd_; - Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context_; + Microsoft::WRL::ComPtr<ID2D1DeviceContext1> d2d1_device_context_; Microsoft::WRL::ComPtr<IDXGISwapChain1> dxgi_swap_chain_; Microsoft::WRL::ComPtr<ID2D1Bitmap1> target_bitmap_; }; diff --git a/src/win/graphics/direct/Factory.cpp b/src/win/graphics/direct/Factory.cpp index 86ca01b9..cbdfe991 100644 --- a/src/win/graphics/direct/Factory.cpp +++ b/src/win/graphics/direct/Factory.cpp @@ -81,9 +81,9 @@ DirectGraphicsFactory::DirectGraphicsFactory() { DirectGraphicsFactory::~DirectGraphicsFactory() { UninitializeCom(); } -Microsoft::WRL::ComPtr<ID2D1DeviceContext> +Microsoft::WRL::ComPtr<ID2D1DeviceContext1> DirectGraphicsFactory::CreateD2D1DeviceContext() { - Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context; + Microsoft::WRL::ComPtr<ID2D1DeviceContext1> d2d1_device_context; ThrowIfFailed(d2d1_device_->CreateDeviceContext( D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context)); return d2d1_device_context; diff --git a/src/win/graphics/direct/Image.cpp b/src/win/graphics/direct/Image.cpp index 46b763be..d09a7016 100644 --- a/src/win/graphics/direct/Image.cpp +++ b/src/win/graphics/direct/Image.cpp @@ -1,15 +1,32 @@ #include "cru/win/graphics/direct/Image.hpp" +#include "cru/common/platform/win/Exception.hpp" +#include "cru/win/graphics/direct/ConvertUtil.hpp" +#include "cru/win/graphics/direct/Exception.hpp" +#include "cru/win/graphics/direct/Factory.hpp" namespace cru::platform::graphics::win::direct { Direct2DImage::Direct2DImage(DirectGraphicsFactory* graphics_factory, - ID2D1Image* d2d_image, bool auto_release) + Microsoft::WRL::ComPtr<ID2D1Bitmap1> d2d_bitmap) : DirectGraphicsResource(graphics_factory), - d2d_image_(d2d_image), - auto_release_(auto_release) {} + d2d_bitmap_(std::move(d2d_bitmap)) {} -Direct2DImage::~Direct2DImage() { - if (auto_release_) { - d2d_image_->Release(); - } +Direct2DImage::~Direct2DImage() {} + +float Direct2DImage::GetWidth() { return d2d_bitmap_->GetSize().width; } + +float Direct2DImage::GetHeight() { return d2d_bitmap_->GetSize().height; } + +std::unique_ptr<IImage> Direct2DImage::CreateWithRect(const Rect& rect) { + auto device_context = GetDirectFactory()->CreateD2D1DeviceContext(); + Microsoft::WRL::ComPtr<ID2D1Bitmap1> bitmap; + ThrowIfFailed(device_context->CreateBitmap( + D2D1::SizeU(rect.width, rect.height), nullptr, 0, + D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET), &bitmap)); + device_context->SetTarget(bitmap.Get()); + device_context->BeginDraw(); + device_context->DrawBitmap(d2d_bitmap_.Get(), Convert(rect)); + ThrowIfFailed(device_context->EndDraw()); + return std::make_unique<Direct2DImage>(GetDirectFactory(), std::move(bitmap)); } + } // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/ImageFactory.cpp b/src/win/graphics/direct/ImageFactory.cpp index dc43a9b9..ced590ec 100644 --- a/src/win/graphics/direct/ImageFactory.cpp +++ b/src/win/graphics/direct/ImageFactory.cpp @@ -43,10 +43,11 @@ std::unique_ptr<IImage> WinImageFactory::DecodeFromStream(io::Stream* stream) { auto d2d_context = graphics_factory_->GetDefaultD2D1DeviceContext(); - ID2D1Bitmap* d2d_image; + Microsoft::WRL::ComPtr<ID2D1Bitmap1> d2d_image; d2d_context->CreateBitmapFromWicBitmap(wic_bitmap_frame_decode.Get(), NULL, &d2d_image); - return std::make_unique<Direct2DImage>(graphics_factory_, d2d_image, true); + return std::make_unique<Direct2DImage>(graphics_factory_, + std::move(d2d_image)); } } // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/Painter.cpp b/src/win/graphics/direct/Painter.cpp index 26a4bb14..395b1d90 100644 --- a/src/win/graphics/direct/Painter.cpp +++ b/src/win/graphics/direct/Painter.cpp @@ -11,150 +11,155 @@ #include <type_traits> namespace cru::platform::graphics::win::direct { -D2DPainter::D2DPainter(ID2D1RenderTarget* render_target) { - Expects(render_target); - render_target_ = render_target; +D2DDeviceContextPainter::D2DDeviceContextPainter( + ID2D1DeviceContext1* device_context) { + Expects(device_context); + device_context_ = device_context; } -platform::Matrix D2DPainter::GetTransform() { +platform::Matrix D2DDeviceContextPainter::GetTransform() { CheckValidation(); D2D1_MATRIX_3X2_F m; - render_target_->GetTransform(&m); + device_context_->GetTransform(&m); return Convert(m); } -void D2DPainter::SetTransform(const platform::Matrix& matrix) { +void D2DDeviceContextPainter::SetTransform(const platform::Matrix& matrix) { CheckValidation(); - render_target_->SetTransform(Convert(matrix)); + device_context_->SetTransform(Convert(matrix)); } -void D2DPainter::ConcatTransform(const Matrix& matrix) { +void D2DDeviceContextPainter::ConcatTransform(const Matrix& matrix) { SetTransform(GetTransform() * matrix); } -void D2DPainter::Clear(const Color& color) { +void D2DDeviceContextPainter::Clear(const Color& color) { CheckValidation(); - render_target_->Clear(Convert(color)); + device_context_->Clear(Convert(color)); } -void D2DPainter::DrawLine(const Point& start, const Point& end, IBrush* brush, - float width) { +void D2DDeviceContextPainter::DrawLine(const Point& start, const Point& end, + IBrush* brush, float width) { CheckValidation(); const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId()); - render_target_->DrawLine(Convert(start), Convert(end), - b->GetD2DBrushInterface(), width); + device_context_->DrawLine(Convert(start), Convert(end), + b->GetD2DBrushInterface(), width); } -void D2DPainter::StrokeRectangle(const Rect& rectangle, IBrush* brush, - float width) { +void D2DDeviceContextPainter::StrokeRectangle(const Rect& rectangle, + IBrush* brush, float width) { CheckValidation(); const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId()); - render_target_->DrawRectangle(Convert(rectangle), b->GetD2DBrushInterface(), - width); + device_context_->DrawRectangle(Convert(rectangle), b->GetD2DBrushInterface(), + width); } -void D2DPainter::FillRectangle(const Rect& rectangle, IBrush* brush) { +void D2DDeviceContextPainter::FillRectangle(const Rect& rectangle, + IBrush* brush) { CheckValidation(); const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId()); - render_target_->FillRectangle(Convert(rectangle), b->GetD2DBrushInterface()); + device_context_->FillRectangle(Convert(rectangle), b->GetD2DBrushInterface()); } -void D2DPainter::StrokeEllipse(const Rect& outline_rect, IBrush* brush, - float width) { +void D2DDeviceContextPainter::StrokeEllipse(const Rect& outline_rect, + IBrush* brush, float width) { CheckValidation(); const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId()); - render_target_->DrawEllipse( + device_context_->DrawEllipse( D2D1::Ellipse(Convert(outline_rect.GetCenter()), outline_rect.width / 2.0f, outline_rect.height / 2.0f), b->GetD2DBrushInterface(), width); } -void D2DPainter::FillEllipse(const Rect& outline_rect, IBrush* brush) { +void D2DDeviceContextPainter::FillEllipse(const Rect& outline_rect, + IBrush* brush) { CheckValidation(); const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId()); - render_target_->FillEllipse( + device_context_->FillEllipse( D2D1::Ellipse(Convert(outline_rect.GetCenter()), outline_rect.width / 2.0f, outline_rect.height / 2.0f), b->GetD2DBrushInterface()); } -void D2DPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush, - float width) { +void D2DDeviceContextPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush, + float width) { CheckValidation(); const auto g = CheckPlatform<D2DGeometry>(geometry, GetPlatformId()); const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId()); - render_target_->DrawGeometry(g->GetComInterface(), b->GetD2DBrushInterface(), - width); + device_context_->DrawGeometry(g->GetComInterface(), b->GetD2DBrushInterface(), + width); } -void D2DPainter::FillGeometry(IGeometry* geometry, IBrush* brush) { +void D2DDeviceContextPainter::FillGeometry(IGeometry* geometry, IBrush* brush) { CheckValidation(); const auto g = CheckPlatform<D2DGeometry>(geometry, GetPlatformId()); const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId()); - render_target_->FillGeometry(g->GetComInterface(), b->GetD2DBrushInterface()); + device_context_->FillGeometry(g->GetComInterface(), + b->GetD2DBrushInterface()); } -void D2DPainter::DrawText(const Point& offset, ITextLayout* text_layout, - IBrush* brush) { +void D2DDeviceContextPainter::DrawText(const Point& offset, + ITextLayout* text_layout, + IBrush* brush) { CheckValidation(); const auto t = CheckPlatform<DWriteTextLayout>(text_layout, GetPlatformId()); const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId()); - render_target_->DrawTextLayout(Convert(offset), t->GetComInterface(), - b->GetD2DBrushInterface()); + device_context_->DrawTextLayout(Convert(offset), t->GetComInterface(), + b->GetD2DBrushInterface()); } -void D2DPainter::DrawImage(const Point& offset, IImage* image) { +void D2DDeviceContextPainter::DrawImage(const Point& offset, IImage* image) { CheckValidation(); const auto i = CheckPlatform<Direct2DImage>(image, GetPlatformId()); Microsoft::WRL::ComPtr<ID2D1DeviceContext> device_context; - render_target_->QueryInterface(device_context.GetAddressOf()); - device_context->DrawImage(i->GetD2DImage(), Convert(offset)); + device_context_->QueryInterface(device_context.GetAddressOf()); + device_context->DrawImage(i->GetD2DBitmap().Get(), Convert(offset)); } -void D2DPainter::PushLayer(const Rect& bounds) { +void D2DDeviceContextPainter::PushLayer(const Rect& bounds) { CheckValidation(); Microsoft::WRL::ComPtr<ID2D1Layer> layer; - ThrowIfFailed(render_target_->CreateLayer(&layer)); + ThrowIfFailed(device_context_->CreateLayer(&layer)); - render_target_->PushLayer(D2D1::LayerParameters(Convert(bounds)), - layer.Get()); + device_context_->PushLayer(D2D1::LayerParameters(Convert(bounds)), + layer.Get()); layers_.push_back(std::move(layer)); } -void D2DPainter::PopLayer() { - render_target_->PopLayer(); +void D2DDeviceContextPainter::PopLayer() { + device_context_->PopLayer(); layers_.pop_back(); } -void D2DPainter::PushState() { +void D2DDeviceContextPainter::PushState() { Microsoft::WRL::ComPtr<ID2D1Factory> factory = nullptr; - render_target_->GetFactory(&factory); + device_context_->GetFactory(&factory); Microsoft::WRL::ComPtr<ID2D1DrawingStateBlock> state_block; factory->CreateDrawingStateBlock(&state_block); - render_target_->SaveDrawingState(state_block.Get()); + device_context_->SaveDrawingState(state_block.Get()); drawing_state_stack_.push_back(std::move(state_block)); } -void D2DPainter::PopState() { +void D2DDeviceContextPainter::PopState() { Expects(!drawing_state_stack_.empty()); auto drawing_state = drawing_state_stack_.back(); drawing_state_stack_.pop_back(); - render_target_->RestoreDrawingState(drawing_state.Get()); + device_context_->RestoreDrawingState(drawing_state.Get()); } -void D2DPainter::EndDraw() { +void D2DDeviceContextPainter::EndDraw() { if (is_drawing_) { is_drawing_ = false; DoEndDraw(); } } -void D2DPainter::CheckValidation() { +void D2DDeviceContextPainter::CheckValidation() { if (!is_drawing_) { throw cru::platform::ReuseException( u"Can't do that on painter after end drawing."); diff --git a/src/win/graphics/direct/WindowPainter.cpp b/src/win/graphics/direct/WindowPainter.cpp index c88667b6..d29ec3ba 100644 --- a/src/win/graphics/direct/WindowPainter.cpp +++ b/src/win/graphics/direct/WindowPainter.cpp @@ -6,7 +6,7 @@ namespace cru::platform::graphics::win::direct { D2DWindowPainter::D2DWindowPainter(D2DWindowRenderTarget* render_target) - : D2DPainter(render_target->GetD2D1DeviceContext()), + : D2DDeviceContextPainter(render_target->GetD2D1DeviceContext()), render_target_(render_target) { render_target_->GetD2D1DeviceContext()->BeginDraw(); } |