From 7b4b334e777e573040569801d3b4ab28a606a127 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 5 May 2022 19:31:18 +0800 Subject: ... --- include/cru/osx/graphics/quartz/ImageFactory.h | 1 + include/cru/platform/graphics/ImageFactory.h | 7 +++++++ 2 files changed, 8 insertions(+) (limited to 'include') diff --git a/include/cru/osx/graphics/quartz/ImageFactory.h b/include/cru/osx/graphics/quartz/ImageFactory.h index e9854738..19e9c8c1 100644 --- a/include/cru/osx/graphics/quartz/ImageFactory.h +++ b/include/cru/osx/graphics/quartz/ImageFactory.h @@ -15,5 +15,6 @@ class QuartzImageFactory : public OsxQuartzResource, public: std::unique_ptr DecodeFromStream(io::Stream* stream) override; + std::unique_ptr CreateBitmap(int width, int height) override; }; } // namespace cru::platform::graphics::osx::quartz diff --git a/include/cru/platform/graphics/ImageFactory.h b/include/cru/platform/graphics/ImageFactory.h index 2a7902b2..e74f57e0 100644 --- a/include/cru/platform/graphics/ImageFactory.h +++ b/include/cru/platform/graphics/ImageFactory.h @@ -7,5 +7,12 @@ namespace cru::platform::graphics { struct CRU_PLATFORM_GRAPHICS_API IImageFactory : public virtual IGraphicsResource { virtual std::unique_ptr DecodeFromStream(io::Stream* stream) = 0; + + /** + * \brief Create an empty bitmap with given width and height. + * \remarks Implementation should ensure that the bitmap supports alpha + * channel. It had better be in 32-bit rgba format. + */ + virtual std::unique_ptr CreateBitmap(int width, int height) = 0; }; } // namespace cru::platform::graphics -- cgit v1.2.3 From 56a8047c4093c928b8551bbacda796382984512e Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 5 May 2022 20:05:35 +0800 Subject: ... --- include/cru/osx/graphics/quartz/Image.h | 7 ++++++- include/cru/platform/graphics/Image.h | 15 +++++++++++++++ src/osx/graphics/quartz/Image.cpp | 31 +++++++++++++++++++++++++++++-- src/osx/graphics/quartz/ImageFactory.cpp | 11 ++++++++++- src/platform/graphics/CMakeLists.txt | 1 + src/platform/graphics/Image.cpp | 15 +++++++++++++++ 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/platform/graphics/Image.cpp (limited to 'include') diff --git a/include/cru/osx/graphics/quartz/Image.h b/include/cru/osx/graphics/quartz/Image.h index 458f5db0..5b3aead9 100644 --- a/include/cru/osx/graphics/quartz/Image.h +++ b/include/cru/osx/graphics/quartz/Image.h @@ -9,7 +9,8 @@ namespace cru::platform::graphics::osx::quartz { class QuartzImage : public OsxQuartzResource, public virtual IImage { public: QuartzImage(IGraphicsFactory* graphics_factory, IImageFactory* image_factory, - CGImageRef image, bool auto_release); + CGImageRef image, bool auto_release, + unsigned char* buffer = nullptr); CRU_DELETE_COPY(QuartzImage) CRU_DELETE_MOVE(QuartzImage) @@ -22,11 +23,15 @@ class QuartzImage : public OsxQuartzResource, public virtual IImage { std::unique_ptr CreateWithRect(const Rect& rect) override; + std::unique_ptr CreatePainter() override; + CGImageRef GetCGImage() const { return image_; } private: IImageFactory* image_factory_; CGImageRef image_; bool auto_release_ = false; + + unsigned char* buffer_; }; } // namespace cru::platform::graphics::osx::quartz diff --git a/include/cru/platform/graphics/Image.h b/include/cru/platform/graphics/Image.h index 51e27678..17d2170e 100644 --- a/include/cru/platform/graphics/Image.h +++ b/include/cru/platform/graphics/Image.h @@ -6,5 +6,20 @@ struct CRU_PLATFORM_GRAPHICS_API IImage : public virtual IGraphicsResource { virtual float GetWidth() = 0; virtual float GetHeight() = 0; virtual std::unique_ptr CreateWithRect(const Rect& rect) = 0; + + /** + * \brief Create a painter for this image. + * \remarks Not all image could create a painter. If not this method will + * throw. Currently we only ensure images returned by + * IImageFactory::CreateBitmap or CloneToBitmap can create a painter. + */ + virtual std::unique_ptr CreatePainter() = 0; + + /** + * \brief Create a bitmap image with the same pixels as this image's. + * \remarks This method can be used to create a bitmap image, so you can draw + * on the new bitmap, if the original image can't be directly painted. + */ + virtual std::unique_ptr CloneToBitmap(); }; } // namespace cru::platform::graphics diff --git a/src/osx/graphics/quartz/Image.cpp b/src/osx/graphics/quartz/Image.cpp index db10da76..feddad8c 100644 --- a/src/osx/graphics/quartz/Image.cpp +++ b/src/osx/graphics/quartz/Image.cpp @@ -1,14 +1,17 @@ #include "cru/osx/graphics/quartz/Image.h" +#include "cru/common/Exception.h" #include "cru/osx/graphics/quartz/Convert.h" +#include "cru/osx/graphics/quartz/Painter.h" namespace cru::platform::graphics::osx::quartz { QuartzImage::QuartzImage(IGraphicsFactory* graphics_factory, IImageFactory* image_factory, CGImageRef image, - bool auto_release) + bool auto_release, unsigned char* buffer) : OsxQuartzResource(graphics_factory), image_factory_(image_factory), image_(image), - auto_release_(auto_release) {} + auto_release_(auto_release), + buffer_(buffer) {} QuartzImage::~QuartzImage() { if (auto_release_) { @@ -26,4 +29,28 @@ std::unique_ptr QuartzImage::CreateWithRect(const Rect& rect) { return std::make_unique(GetGraphicsFactory(), image_factory_, new_cg_image, true); } + +std::unique_ptr QuartzImage::CreatePainter() { + if (!buffer_) + throw Exception( + u"Failed to create painter for image because failed to get its " + u"buffer."); + + auto width = CGImageGetWidth(image_); + auto height = CGImageGetHeight(image_); + auto bits_per_component = CGImageGetBitsPerComponent(image_); + auto bytes_per_row = CGImageGetBytesPerRow(image_); + auto color_space = CGImageGetColorSpace(image_); + auto bitmap_info = CGImageGetBitmapInfo(image_); + + auto cg_context = + CGBitmapContextCreate(buffer_, width, height, bits_per_component, + bytes_per_row, color_space, bitmap_info); + + return std::make_unique( + GetGraphicsFactory(), cg_context, true, Size(width, height), + [](QuartzCGContextPainter* painter) { + + }); +} } // namespace cru::platform::graphics::osx::quartz diff --git a/src/osx/graphics/quartz/ImageFactory.cpp b/src/osx/graphics/quartz/ImageFactory.cpp index 95315e1a..a8c17719 100644 --- a/src/osx/graphics/quartz/ImageFactory.cpp +++ b/src/osx/graphics/quartz/ImageFactory.cpp @@ -35,6 +35,15 @@ std::unique_ptr QuartzImageFactory::CreateBitmap(int width, CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB(); + const auto buffer_size = width * height * 4; + auto buffer = new unsigned char[buffer_size]; + + auto cg_data_provider = CGDataProviderCreateWithData( + nullptr, buffer, buffer_size, + [](void* info, const void* data, size_t size) { + delete[] static_cast(data); + }); + auto cg_image = CGImageCreate(width, height, 8, 32, 4 * width, color_space, kCGImageAlphaLast, nullptr, nullptr, true, kCGRenderingIntentDefault); @@ -42,6 +51,6 @@ std::unique_ptr QuartzImageFactory::CreateBitmap(int width, CGColorSpaceRelease(color_space); return std::unique_ptr( - new QuartzImage(GetGraphicsFactory(), this, cg_image, true)); + new QuartzImage(GetGraphicsFactory(), this, cg_image, true, buffer)); } } // namespace cru::platform::graphics::osx::quartz diff --git a/src/platform/graphics/CMakeLists.txt b/src/platform/graphics/CMakeLists.txt index 1d983188..dc0a429e 100644 --- a/src/platform/graphics/CMakeLists.txt +++ b/src/platform/graphics/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(cru_platform_graphics SHARED ForDllExport.cpp Geometry.cpp + Image.cpp NullPainter.cpp ) target_compile_definitions(cru_platform_graphics PRIVATE CRU_PLATFORM_GRAPHICS_EXPORT_API) diff --git a/src/platform/graphics/Image.cpp b/src/platform/graphics/Image.cpp new file mode 100644 index 00000000..2c787dd7 --- /dev/null +++ b/src/platform/graphics/Image.cpp @@ -0,0 +1,15 @@ +#include "cru/platform/graphics/Image.h" + +#include "cru/platform/graphics/Factory.h" +#include "cru/platform/graphics/ImageFactory.h" +#include "cru/platform/graphics/Painter.h" + +namespace cru::platform::graphics { +std::unique_ptr IImage::CloneToBitmap() { + auto image = GetGraphicsFactory()->GetImageFactory()->CreateBitmap( + GetWidth(), GetHeight()); + auto painter = image->CreatePainter(); + painter->DrawImage(Point{}, this); + return image; +} +} // namespace cru::platform::graphics -- cgit v1.2.3 From b3e5d228311fac99d5a6f5699276ff87229b8c7b Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 5 May 2022 20:06:29 +0800 Subject: ... --- include/cru/platform/graphics/Image.h | 1 + include/cru/platform/graphics/ImageFactory.h | 1 + 2 files changed, 2 insertions(+) (limited to 'include') diff --git a/include/cru/platform/graphics/Image.h b/include/cru/platform/graphics/Image.h index 17d2170e..de3b32d5 100644 --- a/include/cru/platform/graphics/Image.h +++ b/include/cru/platform/graphics/Image.h @@ -12,6 +12,7 @@ struct CRU_PLATFORM_GRAPHICS_API IImage : public virtual IGraphicsResource { * \remarks Not all image could create a painter. If not this method will * throw. Currently we only ensure images returned by * IImageFactory::CreateBitmap or CloneToBitmap can create a painter. + * \todo Implement on Windows. */ virtual std::unique_ptr CreatePainter() = 0; diff --git a/include/cru/platform/graphics/ImageFactory.h b/include/cru/platform/graphics/ImageFactory.h index e74f57e0..d2c2a468 100644 --- a/include/cru/platform/graphics/ImageFactory.h +++ b/include/cru/platform/graphics/ImageFactory.h @@ -12,6 +12,7 @@ struct CRU_PLATFORM_GRAPHICS_API IImageFactory * \brief Create an empty bitmap with given width and height. * \remarks Implementation should ensure that the bitmap supports alpha * channel. It had better be in 32-bit rgba format. + * \todo Implement on Windows. */ virtual std::unique_ptr CreateBitmap(int width, int height) = 0; }; -- cgit v1.2.3 From a465e2c410c1ae5098d2e073442219bb07a31b9d Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 7 May 2022 18:39:51 +0800 Subject: ... --- demos/input_method/main.cpp | 2 ++ demos/svg_path/main.cpp | 1 + include/cru/platform/graphics/Painter.h | 4 ++++ include/cru/win/graphics/direct/Image.h | 2 ++ include/cru/win/graphics/direct/ImageFactory.h | 2 ++ include/cru/win/graphics/direct/Painter.h | 11 ++++++++--- src/win/graphics/direct/Image.cpp | 7 +++++++ src/win/graphics/direct/ImageFactory.cpp | 20 ++++++++++++++++++++ src/win/graphics/direct/Painter.cpp | 17 ++++++++++++++++- src/win/graphics/direct/WindowPainter.cpp | 11 +++-------- 10 files changed, 65 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/demos/input_method/main.cpp b/demos/input_method/main.cpp index 7d55f2db..abbbed2c 100644 --- a/demos/input_method/main.cpp +++ b/demos/input_method/main.cpp @@ -99,6 +99,8 @@ int main() { cursor_lefttop.height}, brush.get()); } + + painter->EndDraw(); }); window->KeyDownEvent()->AddHandler( diff --git a/demos/svg_path/main.cpp b/demos/svg_path/main.cpp index 970aa317..87c410e9 100644 --- a/demos/svg_path/main.cpp +++ b/demos/svg_path/main.cpp @@ -39,6 +39,7 @@ M6.5 0a.5.5 0 0 0 0 1H7v1.07a7.001 7.001 0 0 0-3.273 12.474l-.602.602a.5.5 0 0 0 painter->ConcatTransform(Matrix::Scale(10, 10)); painter->FillGeometry(geometry.get(), brush.get()); painter->PopState(); + painter->EndDraw(); }); window->SetVisibility(WindowVisibilityType::Show); diff --git a/include/cru/platform/graphics/Painter.h b/include/cru/platform/graphics/Painter.h index 38ff8849..4268133e 100644 --- a/include/cru/platform/graphics/Painter.h +++ b/include/cru/platform/graphics/Painter.h @@ -3,6 +3,10 @@ namespace cru::platform::graphics { +/** + * \brief Painter is a object to paint on something like window, bitmap and etc. + * \remarks Remember to call EndDraw() when you are done with painting. + */ struct CRU_PLATFORM_GRAPHICS_API IPainter : virtual IPlatformResource { virtual Matrix GetTransform() = 0; virtual void SetTransform(const Matrix& matrix) = 0; diff --git a/include/cru/win/graphics/direct/Image.h b/include/cru/win/graphics/direct/Image.h index 0b51f350..bcf2386b 100644 --- a/include/cru/win/graphics/direct/Image.h +++ b/include/cru/win/graphics/direct/Image.h @@ -20,6 +20,8 @@ class CRU_WIN_GRAPHICS_DIRECT_API Direct2DImage : public DirectGraphicsResource, std::unique_ptr CreateWithRect(const Rect& rect) override; + std::unique_ptr CreatePainter() override; + const Microsoft::WRL::ComPtr& GetD2DBitmap() const { return d2d_bitmap_; } diff --git a/include/cru/win/graphics/direct/ImageFactory.h b/include/cru/win/graphics/direct/ImageFactory.h index 34d363b0..923e682f 100644 --- a/include/cru/win/graphics/direct/ImageFactory.h +++ b/include/cru/win/graphics/direct/ImageFactory.h @@ -19,6 +19,8 @@ class CRU_WIN_GRAPHICS_DIRECT_API WinImageFactory public: std::unique_ptr DecodeFromStream(io::Stream* stream) override; + std::unique_ptr CreateBitmap(int width, int height) override; + private: DirectGraphicsFactory* graphics_factory_; diff --git a/include/cru/win/graphics/direct/Painter.h b/include/cru/win/graphics/direct/Painter.h index b1795395..da89ba5d 100644 --- a/include/cru/win/graphics/direct/Painter.h +++ b/include/cru/win/graphics/direct/Painter.h @@ -2,6 +2,7 @@ #include "ComResource.h" #include "Resource.h" +#include "cru/common/Base.h" #include "cru/platform/graphics/Painter.h" #include @@ -11,13 +12,15 @@ class CRU_WIN_GRAPHICS_DIRECT_API D2DDeviceContextPainter : public DirectResource, public virtual IPainter, public virtual IComResource { + CRU_DEFINE_CLASS_LOG_TAG(u"D2DDeviceContextPainter") public: - explicit D2DDeviceContextPainter(ID2D1DeviceContext1* device_context); + explicit D2DDeviceContextPainter(ID2D1DeviceContext1* device_context, + bool release = false); CRU_DELETE_COPY(D2DDeviceContextPainter) CRU_DELETE_MOVE(D2DDeviceContextPainter) - ~D2DDeviceContextPainter() override = default; + ~D2DDeviceContextPainter() override; public: ID2D1DeviceContext1* GetComInterface() const override { @@ -57,7 +60,7 @@ class CRU_WIN_GRAPHICS_DIRECT_API D2DDeviceContextPainter void EndDraw() override final; protected: - virtual void DoEndDraw() = 0; + virtual void DoEndDraw() {} private: bool IsValid() { return is_drawing_; } @@ -71,5 +74,7 @@ class CRU_WIN_GRAPHICS_DIRECT_API D2DDeviceContextPainter drawing_state_stack_; bool is_drawing_ = true; + + bool release_; }; } // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/Image.cpp b/src/win/graphics/direct/Image.cpp index 11f5e763..855098d7 100644 --- a/src/win/graphics/direct/Image.cpp +++ b/src/win/graphics/direct/Image.cpp @@ -3,6 +3,7 @@ #include "cru/win/graphics/direct/ConvertUtil.h" #include "cru/win/graphics/direct/Exception.h" #include "cru/win/graphics/direct/Factory.h" +#include "cru/win/graphics/direct/Painter.h" namespace cru::platform::graphics::win::direct { Direct2DImage::Direct2DImage(DirectGraphicsFactory* graphics_factory, @@ -29,4 +30,10 @@ std::unique_ptr Direct2DImage::CreateWithRect(const Rect& rect) { return std::make_unique(GetDirectFactory(), std::move(bitmap)); } +std::unique_ptr Direct2DImage::CreatePainter() { + auto device_context = GetDirectFactory()->CreateD2D1DeviceContext(); + device_context->SetTarget(d2d_bitmap_.Get()); + return std::make_unique(device_context.Detach(), + true); +} } // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/ImageFactory.cpp b/src/win/graphics/direct/ImageFactory.cpp index e7794aa5..76694e30 100644 --- a/src/win/graphics/direct/ImageFactory.cpp +++ b/src/win/graphics/direct/ImageFactory.cpp @@ -1,4 +1,5 @@ #include "cru/win/graphics/direct/ImageFactory.h" +#include "cru/common/platform/win/Exception.h" #include "cru/common/platform/win/StreamConvert.h" #include "cru/win/graphics/direct/Exception.h" #include "cru/win/graphics/direct/Factory.h" @@ -42,4 +43,23 @@ std::unique_ptr WinImageFactory::DecodeFromStream(io::Stream* stream) { return std::make_unique(graphics_factory_, std::move(d2d_image)); } + +std::unique_ptr WinImageFactory::CreateBitmap(int width, int height) { + if (width <= 0) throw Exception(u"Bitmap width must be greater than 0."); + if (height <= 0) throw Exception(u"Bitmap height must be greater than 0."); + + Microsoft::WRL::ComPtr bitmap; + + auto d2d_context = graphics_factory_->GetDefaultD2D1DeviceContext(); + d2d_context->CreateBitmap( + D2D1::SizeU(width, height), + D2D1::BitmapProperties(D2D1::PixelFormat(DXGI_FORMAT_R8G8B8A8_TYPELESS, + D2D1_ALPHA_MODE_STRAIGHT)), + &bitmap); + + Microsoft::WRL::ComPtr bitmap1; + ThrowIfFailed(bitmap.As(&bitmap1), "Failed to convert bitmap to bitmap1."); + + return std::make_unique(graphics_factory_, std::move(bitmap1)); +} } // namespace cru::platform::graphics::win::direct diff --git a/src/win/graphics/direct/Painter.cpp b/src/win/graphics/direct/Painter.cpp index 32386877..3e8bd5c1 100644 --- a/src/win/graphics/direct/Painter.cpp +++ b/src/win/graphics/direct/Painter.cpp @@ -1,5 +1,6 @@ #include "cru/win/graphics/direct/Painter.h" +#include "cru/common/log/Logger.h" #include "cru/platform/Check.h" #include "cru/win/graphics/direct/Brush.h" #include "cru/win/graphics/direct/ConvertUtil.h" @@ -12,9 +13,22 @@ namespace cru::platform::graphics::win::direct { D2DDeviceContextPainter::D2DDeviceContextPainter( - ID2D1DeviceContext1* device_context) { + ID2D1DeviceContext1* device_context, bool release) { Expects(device_context); device_context_ = device_context; + release_ = release; + device_context->BeginDraw(); +} + +D2DDeviceContextPainter::~D2DDeviceContextPainter() { + if (is_drawing_) { + CRU_LOG_INFO(u"You may forget to call EndDraw before destroying painter."); + } + + if (release_) { + device_context_->Release(); + device_context_ = nullptr; + } } platform::Matrix D2DDeviceContextPainter::GetTransform() { @@ -155,6 +169,7 @@ void D2DDeviceContextPainter::PopState() { void D2DDeviceContextPainter::EndDraw() { if (is_drawing_) { is_drawing_ = false; + ThrowIfFailed(device_context_->EndDraw()); DoEndDraw(); } } diff --git a/src/win/graphics/direct/WindowPainter.cpp b/src/win/graphics/direct/WindowPainter.cpp index 0f78119e..c0808887 100644 --- a/src/win/graphics/direct/WindowPainter.cpp +++ b/src/win/graphics/direct/WindowPainter.cpp @@ -7,14 +7,9 @@ namespace cru::platform::graphics::win::direct { D2DWindowPainter::D2DWindowPainter(D2DWindowRenderTarget* render_target) : D2DDeviceContextPainter(render_target->GetD2D1DeviceContext()), - render_target_(render_target) { - render_target_->GetD2D1DeviceContext()->BeginDraw(); -} + render_target_(render_target) {} -D2DWindowPainter::~D2DWindowPainter() { EndDraw(); } +D2DWindowPainter::~D2DWindowPainter() {} -void D2DWindowPainter::DoEndDraw() { - ThrowIfFailed(render_target_->GetD2D1DeviceContext()->EndDraw()); - render_target_->Present(); -} +void D2DWindowPainter::DoEndDraw() { render_target_->Present(); } } // namespace cru::platform::graphics::win::direct -- cgit v1.2.3