aboutsummaryrefslogtreecommitdiff
path: root/include/cru/win/graph/direct
diff options
context:
space:
mode:
Diffstat (limited to 'include/cru/win/graph/direct')
-rw-r--r--include/cru/win/graph/direct/brush.hpp26
-rw-r--r--include/cru/win/graph/direct/com_resource.hpp11
-rw-r--r--include/cru/win/graph/direct/convert_util.hpp108
-rw-r--r--include/cru/win/graph/direct/direct_factory.hpp16
-rw-r--r--include/cru/win/graph/direct/exception.hpp36
-rw-r--r--include/cru/win/graph/direct/font.hpp28
-rw-r--r--include/cru/win/graph/direct/geometry.hpp48
-rw-r--r--include/cru/win/graph/direct/graph_factory.hpp60
-rw-r--r--include/cru/win/graph/direct/painter.hpp43
-rw-r--r--include/cru/win/graph/direct/text_layout.hpp42
10 files changed, 418 insertions, 0 deletions
diff --git a/include/cru/win/graph/direct/brush.hpp b/include/cru/win/graph/direct/brush.hpp
new file mode 100644
index 00000000..9775b5c1
--- /dev/null
+++ b/include/cru/win/graph/direct/brush.hpp
@@ -0,0 +1,26 @@
+#pragma once
+#include "com_resource.hpp"
+#include "direct_factory.hpp"
+
+#include "cru/platform/graph/brush.hpp"
+
+namespace cru::platform::graph::win::direct {
+class D2DSolidColorBrush : public SolidColorBrush,
+ public IComResource<ID2D1SolidColorBrush> {
+ public:
+ explicit D2DSolidColorBrush(IDirectFactory* factory);
+ D2DSolidColorBrush(const D2DSolidColorBrush& other) = delete;
+ D2DSolidColorBrush(D2DSolidColorBrush&& other) = delete;
+ D2DSolidColorBrush& operator=(const D2DSolidColorBrush& other) = delete;
+ D2DSolidColorBrush& operator=(D2DSolidColorBrush&& other) = delete;
+ ~D2DSolidColorBrush() override = default;
+
+ ID2D1SolidColorBrush* GetComInterface() override { return brush_.Get(); }
+
+ protected:
+ void OnSetColor(const Color& color) override;
+
+ private:
+ Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> brush_;
+};
+} // namespace cru::platform::graph::win::direct
diff --git a/include/cru/win/graph/direct/com_resource.hpp b/include/cru/win/graph/direct/com_resource.hpp
new file mode 100644
index 00000000..aa2c366b
--- /dev/null
+++ b/include/cru/win/graph/direct/com_resource.hpp
@@ -0,0 +1,11 @@
+#pragma once
+#include "../../win_pre_config.hpp"
+
+namespace cru::platform::graph::win::direct {
+template<typename TInterface>
+struct IComResource {
+ virtual ~IComResource() = default;
+
+ virtual TInterface* GetComInterface() = 0;
+};
+} // namespace cru::platform::graph::win_direct
diff --git a/include/cru/win/graph/direct/convert_util.hpp b/include/cru/win/graph/direct/convert_util.hpp
new file mode 100644
index 00000000..2c45c63a
--- /dev/null
+++ b/include/cru/win/graph/direct/convert_util.hpp
@@ -0,0 +1,108 @@
+#pragma once
+#include "../../win_pre_config.hpp"
+
+#include "cru/platform/graphic_base.hpp"
+#include "cru/platform/matrix.hpp"
+
+namespace cru::platform::graph::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::graph::win::direct
diff --git a/include/cru/win/graph/direct/direct_factory.hpp b/include/cru/win/graph/direct/direct_factory.hpp
new file mode 100644
index 00000000..b150d5aa
--- /dev/null
+++ b/include/cru/win/graph/direct/direct_factory.hpp
@@ -0,0 +1,16 @@
+#pragma once
+#include "../../win_pre_config.hpp"
+
+namespace cru::platform::graph::win::direct {
+// Interface provides access to root d2d resources.
+struct IDirectFactory {
+ virtual ~IDirectFactory() = default;
+
+ virtual ID2D1Factory1* GetD2D1Factory() const = 0;
+ virtual ID2D1DeviceContext* GetD2D1DeviceContext() const = 0;
+ virtual ID3D11Device* GetD3D11Device() const = 0;
+ virtual IDXGIFactory2* GetDxgiFactory() const = 0;
+ virtual IDWriteFactory* GetDWriteFactory() const = 0;
+ virtual IDWriteFontCollection* GetSystemFontCollection() const = 0;
+};
+} // namespace cru::platform::graph::win::direct
diff --git a/include/cru/win/graph/direct/exception.hpp b/include/cru/win/graph/direct/exception.hpp
new file mode 100644
index 00000000..bfa14aaf
--- /dev/null
+++ b/include/cru/win/graph/direct/exception.hpp
@@ -0,0 +1,36 @@
+#pragma once
+#include "../../win_pre_config.hpp"
+
+#include "cru/platform/exception.hpp"
+
+#include <stdexcept>
+#include <string_view>
+
+namespace cru::platform::graph::win::direct {
+
+class HResultError : public PlatformException {
+ public:
+ explicit HResultError(HRESULT h_result);
+ explicit HResultError(HRESULT h_result,
+ const std::string_view& additional_message);
+ HResultError(const HResultError& other) = default;
+ HResultError(HResultError&& other) = default;
+ HResultError& operator=(const HResultError& other) = default;
+ HResultError& operator=(HResultError&& other) = default;
+ ~HResultError() override = default;
+
+ HRESULT GetHResult() const { return h_result_; }
+
+ private:
+ HRESULT h_result_;
+};
+
+inline void ThrowIfFailed(const HRESULT h_result) {
+ if (FAILED(h_result)) throw HResultError(h_result);
+}
+
+inline void ThrowIfFailed(const HRESULT h_result,
+ const std::string_view& message) {
+ if (FAILED(h_result)) throw HResultError(h_result, message);
+}
+} // namespace cru::platform::graph::win::direct \ No newline at end of file
diff --git a/include/cru/win/graph/direct/font.hpp b/include/cru/win/graph/direct/font.hpp
new file mode 100644
index 00000000..44fa7edd
--- /dev/null
+++ b/include/cru/win/graph/direct/font.hpp
@@ -0,0 +1,28 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/font.hpp"
+
+#include <string_view>
+
+namespace cru::win::graph {
+struct IWinNativeFactory;
+
+class WinFontDescriptor : public Object,
+ public virtual platform::graph::IFontDescriptor {
+ public:
+ explicit WinFontDescriptor(IWinNativeFactory* factory,
+ const std::wstring_view& font_family,
+ float font_size);
+ WinFontDescriptor(const WinFontDescriptor& other) = delete;
+ WinFontDescriptor(WinFontDescriptor&& other) = delete;
+ WinFontDescriptor& operator=(const WinFontDescriptor& other) = delete;
+ WinFontDescriptor& operator=(WinFontDescriptor&& other) = delete;
+ ~WinFontDescriptor() override = default;
+
+ IDWriteTextFormat* GetDWriteTextFormat() const { return text_format_.Get(); }
+
+ private:
+ Microsoft::WRL::ComPtr<IDWriteTextFormat> text_format_;
+};
+} // namespace cru::win::graph
diff --git a/include/cru/win/graph/direct/geometry.hpp b/include/cru/win/graph/direct/geometry.hpp
new file mode 100644
index 00000000..e312f13c
--- /dev/null
+++ b/include/cru/win/graph/direct/geometry.hpp
@@ -0,0 +1,48 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/geometry.hpp"
+
+namespace cru::win::graph {
+struct IWinNativeFactory;
+
+class WinGeometryBuilder : public Object,
+ public virtual platform::graph::IGeometryBuilder {
+ public:
+ explicit WinGeometryBuilder(IWinNativeFactory* factory);
+ WinGeometryBuilder(const WinGeometryBuilder& other) = delete;
+ WinGeometryBuilder(WinGeometryBuilder&& other) = delete;
+ WinGeometryBuilder& operator=(const WinGeometryBuilder& other) = delete;
+ WinGeometryBuilder& operator=(WinGeometryBuilder&& other) = delete;
+ ~WinGeometryBuilder() override;
+
+ void BeginFigure(const ui::Point& point) override;
+ void LineTo(const ui::Point& point) override;
+ void QuadraticBezierTo(const ui::Point& control_point,
+ const ui::Point& end_point) override;
+ void CloseFigure(bool close) override;
+ platform::graph::IGeometry* End() override;
+ bool IsEnded() const override { return geometry_ != nullptr; }
+
+ private:
+ Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_;
+ Microsoft::WRL::ComPtr<ID2D1GeometrySink> geometry_sink_;
+};
+
+class WinGeometry : public Object, public virtual platform::graph::IGeometry {
+ public:
+ explicit WinGeometry(Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry);
+ WinGeometry(const WinGeometry& other) = delete;
+ WinGeometry(WinGeometry&& other) = delete;
+ WinGeometry& operator=(const WinGeometry& other) = delete;
+ WinGeometry& operator=(WinGeometry&& other) = delete;
+ ~WinGeometry() override = default;
+
+ bool FillContains(const ui::Point& point) override;
+
+ ID2D1PathGeometry* GetNative() const { return geometry_.Get(); }
+
+ private:
+ Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry_;
+};
+} // namespace cru::win::graph
diff --git a/include/cru/win/graph/direct/graph_factory.hpp b/include/cru/win/graph/direct/graph_factory.hpp
new file mode 100644
index 00000000..b3c901be
--- /dev/null
+++ b/include/cru/win/graph/direct/graph_factory.hpp
@@ -0,0 +1,60 @@
+#pragma once
+#include "direct_factory.hpp"
+
+#include "brush.hpp"
+
+#include "cru/platform/graph/graph_factory.hpp"
+
+namespace cru::platform::graph::win::direct {
+class DirectGraphFactory : public GraphFactory, IDirectFactory {
+ friend GraphFactory* GraphFactory::CreateInstance();
+
+ public:
+ static DirectGraphFactory* GetInstance();
+
+ private:
+ DirectGraphFactory();
+
+ public:
+ DirectGraphFactory(const DirectGraphFactory& other) = delete;
+ DirectGraphFactory(DirectGraphFactory&& other) = delete;
+ DirectGraphFactory& operator=(const DirectGraphFactory& other) = delete;
+ DirectGraphFactory& operator=(DirectGraphFactory&& other) = delete;
+ ~DirectGraphFactory() override;
+
+ ID2D1Factory1* GetD2D1Factory() const override { return d2d1_factory_.Get(); }
+ ID2D1DeviceContext* GetD2D1DeviceContext() const override {
+ return d2d1_device_context_.Get();
+ }
+ ID3D11Device* GetD3D11Device() const override { return d3d11_device_.Get(); }
+ IDXGIFactory2* GetDxgiFactory() const override { return dxgi_factory_.Get(); }
+ IDWriteFactory* GetDWriteFactory() const override {
+ return dwrite_factory_.Get();
+ }
+ IDWriteFontCollection* GetSystemFontCollection() const override {
+ return dwrite_system_font_collection_.Get();
+ }
+
+ D2DSolidColorBrush* CreateSolidColorBrush() override;
+
+ D2DGeometryBuilder* CreateGeometryBuilder() override;
+ D2DFont* CreateFont(
+ const std::wstring_view& font_family, float font_size) override;
+ DWriteTextLayout* CreateTextLayout(
+ std::shared_ptr<Font> font,
+ std::wstring text) override;
+
+ bool IsAutoDelete() const override { return auto_delete_; }
+ void SetAutoDelete(bool value) override { auto_delete_ = value; }
+
+ private:
+ bool auto_delete_ = false;
+
+ Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device_;
+ Microsoft::WRL::ComPtr<ID2D1Factory1> d2d1_factory_;
+ 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::win::graph
diff --git a/include/cru/win/graph/direct/painter.hpp b/include/cru/win/graph/direct/painter.hpp
new file mode 100644
index 00000000..f218488c
--- /dev/null
+++ b/include/cru/win/graph/direct/painter.hpp
@@ -0,0 +1,43 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/painter.hpp"
+
+namespace cru::win::graph {
+class GraphManager;
+
+class WinPainter : public Object, public virtual platform::graph::IPainter {
+ public:
+ explicit WinPainter(ID2D1RenderTarget* render_target);
+ WinPainter(const WinPainter& other) = delete;
+ WinPainter(WinPainter&& other) = delete;
+ WinPainter& operator=(const WinPainter& other) = delete;
+ WinPainter& operator=(WinPainter&& other) = delete;
+ ~WinPainter() override = default;
+
+ platform::Matrix GetTransform() override;
+ void SetTransform(const platform::Matrix& matrix) override;
+ void Clear(const ui::Color& color) override;
+ void StrokeRectangle(const ui::Rect& rectangle, platform::graph::IBrush* brush,
+ float width) override;
+ void FillRectangle(const ui::Rect& rectangle,
+ platform::graph::IBrush* brush) override;
+ void StrokeGeometry(platform::graph::IGeometry* geometry,
+ platform::graph::IBrush* brush, float width) override;
+ void FillGeometry(platform::graph::IGeometry* geometry,
+ platform::graph::IBrush* brush) override;
+ void DrawText(const ui::Point& offset,
+ platform::graph::ITextLayout* text_layout,
+ platform::graph::IBrush* brush) override;
+ void End() override final;
+ bool IsEnded() const override final { return is_draw_ended_; }
+
+ protected:
+ virtual void DoEndDraw() = 0;
+
+ private:
+ ID2D1RenderTarget* render_target_;
+
+ bool is_draw_ended_ = false;
+};
+} // namespace cru::win::graph
diff --git a/include/cru/win/graph/direct/text_layout.hpp b/include/cru/win/graph/direct/text_layout.hpp
new file mode 100644
index 00000000..7339eff9
--- /dev/null
+++ b/include/cru/win/graph/direct/text_layout.hpp
@@ -0,0 +1,42 @@
+#pragma once
+#include "../win_pre_config.hpp"
+
+#include "cru/platform/graph/text_layout.hpp"
+
+#include <memory>
+
+namespace cru::win::graph {
+struct IWinNativeFactory;
+class WinFontDescriptor;
+
+class WinTextLayout : public Object, public virtual platform::graph::ITextLayout {
+ public:
+ explicit WinTextLayout(IWinNativeFactory* factory,
+ std::shared_ptr<WinFontDescriptor> font, std::wstring text);
+ WinTextLayout(const WinTextLayout& other) = delete;
+ WinTextLayout(WinTextLayout&& other) = delete;
+ WinTextLayout& operator=(const WinTextLayout& other) = delete;
+ WinTextLayout& operator=(WinTextLayout&& other) = delete;
+ ~WinTextLayout() override = default;
+
+ std::wstring GetText() override;
+ void SetText(std::wstring new_text) override;
+ std::shared_ptr<platform::graph::IFontDescriptor> GetFont();
+ void SetFont(std::shared_ptr<platform::graph::IFontDescriptor> font);
+ void SetMaxWidth(float max_width) override;
+ void SetMaxHeight(float max_height) override;
+ ui::Rect GetTextBounds() override;
+ std::vector<ui::Rect> TextRangeRect(
+ const ui::TextRange& text_range) override;
+
+ IDWriteTextLayout* GetDWriteTextLayout() const { return text_layout_.Get(); }
+
+ private:
+ IWinNativeFactory* factory_;
+ std::wstring text_;
+ std::shared_ptr<WinFontDescriptor> font_descriptor_;
+ float max_width_ = 0.0f;
+ float max_height_ = 0.0f;
+ Microsoft::WRL::ComPtr<IDWriteTextLayout> text_layout_;
+};
+} // namespace cru::platform::win