diff options
Diffstat (limited to 'include/cru/win/graphics/direct')
-rw-r--r-- | include/cru/win/graphics/direct/Brush.hpp | 39 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/ComResource.hpp | 11 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/ConvertUtil.hpp | 107 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Exception.hpp | 7 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Factory.hpp | 58 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Font.hpp | 33 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Geometry.hpp | 57 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Painter.hpp | 60 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/Resource.hpp | 49 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/TextLayout.hpp | 55 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/WindowPainter.hpp | 21 | ||||
-rw-r--r-- | include/cru/win/graphics/direct/WindowRenderTarget.hpp | 42 |
12 files changed, 539 insertions, 0 deletions
diff --git a/include/cru/win/graphics/direct/Brush.hpp b/include/cru/win/graphics/direct/Brush.hpp new file mode 100644 index 00000000..fbff83b5 --- /dev/null +++ b/include/cru/win/graphics/direct/Brush.hpp @@ -0,0 +1,39 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/Brush.hpp" + +namespace cru::platform::graphics::win::direct { +struct ID2DBrush : virtual IBrush { + virtual ID2D1Brush* GetD2DBrushInterface() const = 0; +}; + +class D2DSolidColorBrush : public DirectGraphResource, + public virtual ISolidColorBrush, + public virtual ID2DBrush, + public virtual IComResource<ID2D1SolidColorBrush> { + public: + explicit D2DSolidColorBrush(DirectGraphFactory* factory); + + CRU_DELETE_COPY(D2DSolidColorBrush) + CRU_DELETE_MOVE(D2DSolidColorBrush) + + ~D2DSolidColorBrush() override = default; + + public: + Color GetColor() override { return color_; } + void SetColor(const Color& color) override; + + ID2D1Brush* GetD2DBrushInterface() const override { return brush_.Get(); } + + ID2D1SolidColorBrush* GetComInterface() const override { + return brush_.Get(); + } + + private: + Color color_ = colors::black; + + Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/ComResource.hpp b/include/cru/win/graphics/direct/ComResource.hpp new file mode 100644 index 00000000..34ea39ed --- /dev/null +++ b/include/cru/win/graphics/direct/ComResource.hpp @@ -0,0 +1,11 @@ +#pragma once +#include "../../WinPreConfig.hpp" + +#include "cru/common/Base.hpp" + +namespace cru::platform::graphics::win::direct { +template <typename TInterface> +struct IComResource : virtual Interface { + virtual TInterface* GetComInterface() const = 0; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/ConvertUtil.hpp b/include/cru/win/graphics/direct/ConvertUtil.hpp new file mode 100644 index 00000000..0d8da8a1 --- /dev/null +++ b/include/cru/win/graphics/direct/ConvertUtil.hpp @@ -0,0 +1,107 @@ +#pragma once +#include "../../WinPreConfig.hpp" + +#include "cru/platform/graphics/Base.hpp" + +namespace cru::platform::graphics::win::direct { +inline D2D1_MATRIX_3X2_F Convert(const platform::Matrix& matrix) { + D2D1_MATRIX_3X2_F m; + m._11 = matrix.m11; + m._12 = matrix.m12; + m._21 = matrix.m21; + m._22 = matrix.m22; + m._31 = matrix.m31; + m._32 = matrix.m32; + return m; +} + +inline D2D1_COLOR_F Convert(const Color& color) { + return D2D1::ColorF(color.red / 255.0f, color.green / 255.0f, + color.blue / 255.0f, color.alpha / 255.0f); +} + +inline D2D1_POINT_2F Convert(const Point& point) { + return D2D1::Point2F(point.x, point.y); +} + +inline D2D1_RECT_F Convert(const Rect& rect) { + return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, + rect.top + rect.height); +} + +inline D2D1_ROUNDED_RECT Convert(const RoundedRect& rounded_rect) { + return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, + rounded_rect.radius_y); +} + +inline D2D1_ELLIPSE Convert(const Ellipse& ellipse) { + return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, + ellipse.radius_y); +} + +inline platform::Matrix Convert(const D2D1_MATRIX_3X2_F& matrix) { + return platform::Matrix{matrix._11, matrix._12, matrix._21, + matrix._22, matrix._31, matrix._32}; +} + +inline Color Convert(const D2D1_COLOR_F& color) { + auto floor = [](float n) { return static_cast<std::uint8_t>(n + 0.5f); }; + return Color{floor(color.r * 255.0f), floor(color.g * 255.0f), + floor(color.b * 255.0f), floor(color.a * 255.0f)}; +} + +inline Point Convert(const D2D1_POINT_2F& point) { + return Point(point.x, point.y); +} + +inline Rect Convert(const D2D1_RECT_F& rect) { + return Rect(rect.left, rect.top, rect.right - rect.left, + rect.bottom - rect.top); +} + +inline RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { + return RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, + rounded_rect.radiusY); +} + +inline Ellipse Convert(const D2D1_ELLIPSE& ellipse) { + return Ellipse(Convert(ellipse.point), ellipse.radiusX, ellipse.radiusY); +} + +inline bool operator==(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return left.x == right.x && left.y == right.y; +} + +inline bool operator!=(const D2D1_POINT_2F& left, const D2D1_POINT_2F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return left.left == right.left && left.top == right.top && + left.right == right.right && left.bottom == right.bottom; +} + +inline bool operator!=(const D2D1_RECT_F& left, const D2D1_RECT_F& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return left.rect == right.rect && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ROUNDED_RECT& left, + const D2D1_ROUNDED_RECT& right) { + return !(left == right); +} + +inline bool operator==(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return left.point == right.point && left.radiusX == right.radiusX && + left.radiusY == right.radiusY; +} + +inline bool operator!=(const D2D1_ELLIPSE& left, const D2D1_ELLIPSE& right) { + return !(left == right); +} +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Exception.hpp b/include/cru/win/graphics/direct/Exception.hpp new file mode 100644 index 00000000..72493f2f --- /dev/null +++ b/include/cru/win/graphics/direct/Exception.hpp @@ -0,0 +1,7 @@ +#pragma once +#include "../../Exception.hpp" + +namespace cru::platform::graphics::win::direct { +using platform::win::HResultError; +using platform::win::ThrowIfFailed; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Factory.hpp b/include/cru/win/graphics/direct/Factory.hpp new file mode 100644 index 00000000..70f3ede1 --- /dev/null +++ b/include/cru/win/graphics/direct/Factory.hpp @@ -0,0 +1,58 @@ +#pragma once +#include "Resource.hpp" + +#include "cru/platform/graphics/Factory.hpp" + +namespace cru::platform::graphics::win::direct { +class DirectGraphFactory : public DirectResource, public virtual IGraphFactory { + public: + DirectGraphFactory(); + + CRU_DELETE_COPY(DirectGraphFactory) + CRU_DELETE_MOVE(DirectGraphFactory) + + ~DirectGraphFactory() override; + + public: + ID3D11Device* GetD3D11Device() const { return d3d11_device_.Get(); } + ID2D1Factory1* GetD2D1Factory() const { return d2d1_factory_.Get(); } + ID2D1Device* GetD2D1Device() const { return d2d1_device_.Get(); } + IDXGIFactory2* GetDxgiFactory() const { return dxgi_factory_.Get(); } + IDWriteFactory* GetDWriteFactory() const { return dwrite_factory_.Get(); } + IDWriteFontCollection* GetSystemFontCollection() const { + return dwrite_system_font_collection_.Get(); + } + + public: + Microsoft::WRL::ComPtr<ID2D1DeviceContext> 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 + // device. + ID2D1DeviceContext* GetDefaultD2D1DeviceContext() { + return d2d1_device_context_.Get(); + } + + public: + std::unique_ptr<ISolidColorBrush> CreateSolidColorBrush() override; + + std::unique_ptr<IGeometryBuilder> CreateGeometryBuilder() override; + + std::unique_ptr<IFont> CreateFont(std::u16string font_family, + float font_size) override; + + std::unique_ptr<ITextLayout> CreateTextLayout(std::shared_ptr<IFont> font, + std::u16string text) override; + + 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<IDXGIFactory2> dxgi_factory_; + Microsoft::WRL::ComPtr<IDWriteFactory> dwrite_factory_; + Microsoft::WRL::ComPtr<IDWriteFontCollection> dwrite_system_font_collection_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Font.hpp b/include/cru/win/graphics/direct/Font.hpp new file mode 100644 index 00000000..fd3921a3 --- /dev/null +++ b/include/cru/win/graphics/direct/Font.hpp @@ -0,0 +1,33 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/Font.hpp" + +#include <string_view> + +namespace cru::platform::graphics::win::direct { +class DWriteFont : public DirectGraphResource, + public virtual IFont, + public virtual IComResource<IDWriteTextFormat> { + public: + DWriteFont(DirectGraphFactory* factory, std::u16string font_family, + float font_size); + + CRU_DELETE_COPY(DWriteFont) + CRU_DELETE_MOVE(DWriteFont) + + ~DWriteFont() override = default; + + public: + IDWriteTextFormat* GetComInterface() const override { + return text_format_.Get(); + } + + float GetFontSize() override; + + private: + std::u16string font_family_; + Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Geometry.hpp b/include/cru/win/graphics/direct/Geometry.hpp new file mode 100644 index 00000000..edfec590 --- /dev/null +++ b/include/cru/win/graphics/direct/Geometry.hpp @@ -0,0 +1,57 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/Geometry.hpp" + +namespace cru::platform::graphics::win::direct { +class D2DGeometryBuilder : public DirectGraphResource, + public virtual IGeometryBuilder { + public: + explicit D2DGeometryBuilder(DirectGraphFactory* factory); + + CRU_DELETE_COPY(D2DGeometryBuilder) + CRU_DELETE_MOVE(D2DGeometryBuilder) + + ~D2DGeometryBuilder() override = default; + + public: + void BeginFigure(const Point& point) override; + void LineTo(const Point& point) override; + void QuadraticBezierTo(const Point& control_point, + const Point& end_point) override; + void CloseFigure(bool close) override; + + std::unique_ptr<IGeometry> Build() override; + + private: + bool IsValid() { return geometry_ != nullptr; } + void CheckValidation(); + + private: + Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; + Microsoft::WRL::ComPtr<ID2D1GeometrySink> geometry_sink_; +}; + +class D2DGeometry : public DirectGraphResource, + public virtual IGeometry, + public IComResource<ID2D1Geometry> { + public: + D2DGeometry(DirectGraphFactory* factory, + Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry); + + CRU_DELETE_COPY(D2DGeometry) + CRU_DELETE_MOVE(D2DGeometry) + + ~D2DGeometry() override = default; + + public: + ID2D1Geometry* GetComInterface() const override { return geometry_.Get(); } + + public: + bool FillContains(const Point& point) override; + + private: + Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Painter.hpp b/include/cru/win/graphics/direct/Painter.hpp new file mode 100644 index 00000000..93c768e7 --- /dev/null +++ b/include/cru/win/graphics/direct/Painter.hpp @@ -0,0 +1,60 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/Painter.hpp" + +#include <vector> + +namespace cru::platform::graphics::win::direct { +class D2DPainter : public DirectResource, + public virtual IPainter, + public virtual IComResource<ID2D1RenderTarget> { + public: + explicit D2DPainter(ID2D1RenderTarget* render_target); + + CRU_DELETE_COPY(D2DPainter) + CRU_DELETE_MOVE(D2DPainter) + + ~D2DPainter() override = default; + + public: + ID2D1RenderTarget* GetComInterface() const override { return render_target_; } + + public: + Matrix GetTransform() override; + void SetTransform(const platform::Matrix& matrix) override; + + void Clear(const Color& color) override; + + void StrokeRectangle(const Rect& rectangle, IBrush* brush, + float width) override; + void FillRectangle(const Rect& rectangle, IBrush* brush) override; + + void StrokeGeometry(IGeometry* geometry, IBrush* brush, float width) override; + void FillGeometry(IGeometry* geometry, IBrush* brush) override; + + void DrawText(const Point& offset, ITextLayout* text_layout, + IBrush* brush) override; + + void PushLayer(const Rect& bounds) override; + + void PopLayer() override; + + void EndDraw() override final; + + protected: + virtual void DoEndDraw() = 0; + + private: + bool IsValid() { return is_drawing_; } + void CheckValidation(); + + private: + ID2D1RenderTarget* render_target_; + + std::vector<Microsoft::WRL::ComPtr<ID2D1Layer>> layers_; + + bool is_drawing_ = true; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/Resource.hpp b/include/cru/win/graphics/direct/Resource.hpp new file mode 100644 index 00000000..f60f373e --- /dev/null +++ b/include/cru/win/graphics/direct/Resource.hpp @@ -0,0 +1,49 @@ +#pragma once +#include "../../WinPreConfig.hpp" + +#include "cru/platform/graphics/Resource.hpp" + +#include <string_view> + +namespace cru::platform::graphics::win::direct { +class DirectGraphFactory; + +class DirectResource : public Object, public virtual INativeResource { + public: + static constexpr std::u16string_view k_platform_id = u"Windows Direct"; + + protected: + DirectResource() = default; + + public: + CRU_DELETE_COPY(DirectResource) + CRU_DELETE_MOVE(DirectResource) + + ~DirectResource() override = default; + + public: + std::u16string_view GetPlatformId() const final { return k_platform_id; } +}; + +class DirectGraphResource : public DirectResource, + public virtual IGraphResource { + protected: + // Param factory can't be null. + explicit DirectGraphResource(DirectGraphFactory* factory); + + public: + CRU_DELETE_COPY(DirectGraphResource) + CRU_DELETE_MOVE(DirectGraphResource) + + ~DirectGraphResource() override = default; + + public: + IGraphFactory* GetGraphFactory() final; + + public: + DirectGraphFactory* GetDirectFactory() const { return factory_; } + + private: + DirectGraphFactory* factory_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/TextLayout.hpp b/include/cru/win/graphics/direct/TextLayout.hpp new file mode 100644 index 00000000..3320431f --- /dev/null +++ b/include/cru/win/graphics/direct/TextLayout.hpp @@ -0,0 +1,55 @@ +#pragma once +#include "ComResource.hpp" +#include "Resource.hpp" + +#include "cru/platform/graphics/TextLayout.hpp" + +#include <limits> +#include <memory> + +namespace cru::platform::graphics::win::direct { +class DWriteFont; + +class DWriteTextLayout : public DirectGraphResource, + public virtual ITextLayout, + public virtual IComResource<IDWriteTextLayout> { + public: + DWriteTextLayout(DirectGraphFactory* factory, std::shared_ptr<IFont> font, + std::u16string text); + + CRU_DELETE_COPY(DWriteTextLayout) + CRU_DELETE_MOVE(DWriteTextLayout) + + ~DWriteTextLayout() override; + + public: + IDWriteTextLayout* GetComInterface() const override { + return text_layout_.Get(); + } + + public: + std::u16string GetText() override; + std::u16string_view GetTextView() override; + void SetText(std::u16string new_text) override; + + std::shared_ptr<IFont> GetFont() override; + void SetFont(std::shared_ptr<IFont> font) override; + + void SetMaxWidth(float max_width) override; + void SetMaxHeight(float max_height) override; + + Rect GetTextBounds() override; + // Return empty vector if text_range.count is 0. Text range could be in + // reverse direction, it should be normalized first in implementation. + std::vector<Rect> TextRangeRect(const TextRange& text_range) override; + Point TextSinglePoint(Index position, bool trailing) override; + TextHitTestResult HitTest(const Point& point) override; + + private: + std::u16string text_; + std::shared_ptr<DWriteFont> font_; + float max_width_ = std::numeric_limits<float>::max(); + float max_height_ = std::numeric_limits<float>::max(); + Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/WindowPainter.hpp b/include/cru/win/graphics/direct/WindowPainter.hpp new file mode 100644 index 00000000..b5faf7b5 --- /dev/null +++ b/include/cru/win/graphics/direct/WindowPainter.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "Painter.hpp" +#include "WindowRenderTarget.hpp" + +namespace cru::platform::graphics::win::direct { +class D2DWindowPainter : public graphics::win::direct::D2DPainter { + public: + explicit D2DWindowPainter(D2DWindowRenderTarget* window); + + CRU_DELETE_COPY(D2DWindowPainter) + CRU_DELETE_MOVE(D2DWindowPainter) + + ~D2DWindowPainter() override; + + protected: + void DoEndDraw() override; + + private: + D2DWindowRenderTarget* render_target_; +}; +} // namespace cru::platform::graphics::win::direct diff --git a/include/cru/win/graphics/direct/WindowRenderTarget.hpp b/include/cru/win/graphics/direct/WindowRenderTarget.hpp new file mode 100644 index 00000000..75b1bf20 --- /dev/null +++ b/include/cru/win/graphics/direct/WindowRenderTarget.hpp @@ -0,0 +1,42 @@ +#pragma once +#include "Factory.hpp" + +namespace cru::platform::graphics::win::direct { +// Represents a window render target. +class D2DWindowRenderTarget : public Object { + public: + D2DWindowRenderTarget(gsl::not_null<DirectGraphFactory*> factory, HWND hwnd); + + CRU_DELETE_COPY(D2DWindowRenderTarget) + CRU_DELETE_MOVE(D2DWindowRenderTarget) + + ~D2DWindowRenderTarget() override = default; + + public: + graphics::win::direct::DirectGraphFactory* GetDirectFactory() const { + return factory_; + } + + ID2D1DeviceContext* GetD2D1DeviceContext() { + return d2d1_device_context_.Get(); + } + + void SetDpi(float x, float y); + + // Resize the underlying buffer. + void ResizeBuffer(int width, int height); + + // Present the data of the underlying buffer to the window. + void Present(); + + private: + void CreateTargetBitmap(); + + private: + DirectGraphFactory* factory_; + HWND hwnd_; + Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context_; + Microsoft::WRL::ComPtr<IDXGISwapChain1> dxgi_swap_chain_; + Microsoft::WRL::ComPtr<ID2D1Bitmap1> target_bitmap_; +}; +} // namespace cru::platform::graphics::win::direct |