From 3e89aa733587043645f5fda72596e4ff3cd21d2a Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 1 Apr 2019 15:28:07 +0800 Subject: ... --- include/cru/common/ui_base.hpp | 10 ++- include/cru/platform/basic_types.hpp | 1 - include/cru/platform/brush.hpp | 13 +++ include/cru/platform/debug.hpp | 2 +- include/cru/platform/dpi_util.hpp | 32 -------- include/cru/platform/geometry.hpp | 26 ++++++ include/cru/platform/graph_factory.hpp | 14 ++++ include/cru/platform/native_window.hpp | 2 +- include/cru/platform/painter.hpp | 15 ++++ include/cru/platform/ui_applicaition.hpp | 2 - include/cru/platform/win/d2d_util.hpp | 91 +++++++++++++++++++++ include/cru/platform/win/graph_manager.hpp | 44 ++++++++++ include/cru/platform/win/win_application.hpp | 11 ++- include/cru/platform/win/win_brush.hpp | 23 ++++++ include/cru/platform/win/win_geometry.hpp | 45 +++++++++++ include/cru/platform/win/win_graph_factory.hpp | 24 ++++++ include/cru/platform/win/win_native_window.hpp | 7 ++ include/cru/platform/win/win_pre_config.hpp | 6 ++ include/cru/platform/win/window_render_target.hpp | 44 ++++++++++ src/CMakeLists.txt | 2 - src/graph/graph_manager.cpp | 79 ------------------ src/graph/graph_manager.hpp | 60 -------------- src/graph/graph_util.hpp | 63 --------------- src/graph/window_render_target.cpp | 97 ----------------------- src/graph/window_render_target.hpp | 49 ------------ src/platform_win/CMakeLists.txt | 7 +- src/platform_win/debug.cpp | 2 +- src/platform_win/dpi_util.hpp | 32 ++++++++ src/platform_win/graph_manager.cpp | 51 ++++++++++++ src/platform_win/win_application.cpp | 13 ++- src/platform_win/win_brush.cpp | 21 +++++ src/platform_win/win_geometry.cpp | 65 +++++++++++++++ src/platform_win/win_graph_factory.cpp | 31 ++++++++ src/platform_win/win_native_window.cpp | 14 ++-- src/platform_win/window_render_target.cpp | 89 +++++++++++++++++++++ src/ui/d2d_util.hpp | 82 ------------------- src/util/com_util.hpp | 22 ----- 37 files changed, 683 insertions(+), 508 deletions(-) create mode 100644 include/cru/platform/brush.hpp delete mode 100644 include/cru/platform/dpi_util.hpp create mode 100644 include/cru/platform/geometry.hpp create mode 100644 include/cru/platform/graph_factory.hpp create mode 100644 include/cru/platform/painter.hpp create mode 100644 include/cru/platform/win/d2d_util.hpp create mode 100644 include/cru/platform/win/graph_manager.hpp create mode 100644 include/cru/platform/win/win_brush.hpp create mode 100644 include/cru/platform/win/win_geometry.hpp create mode 100644 include/cru/platform/win/win_graph_factory.hpp create mode 100644 include/cru/platform/win/window_render_target.hpp delete mode 100644 src/graph/graph_manager.cpp delete mode 100644 src/graph/graph_manager.hpp delete mode 100644 src/graph/graph_util.hpp delete mode 100644 src/graph/window_render_target.cpp delete mode 100644 src/graph/window_render_target.hpp create mode 100644 src/platform_win/dpi_util.hpp create mode 100644 src/platform_win/graph_manager.cpp create mode 100644 src/platform_win/win_brush.cpp create mode 100644 src/platform_win/win_geometry.cpp create mode 100644 src/platform_win/win_graph_factory.cpp create mode 100644 src/platform_win/window_render_target.cpp delete mode 100644 src/ui/d2d_util.hpp delete mode 100644 src/util/com_util.hpp diff --git a/include/cru/common/ui_base.hpp b/include/cru/common/ui_base.hpp index 42ed0eb4..0da18f22 100644 --- a/include/cru/common/ui_base.hpp +++ b/include/cru/common/ui_base.hpp @@ -1,8 +1,9 @@ #pragma once #include "pre_config.hpp" -#include +#include #include +#include namespace cru::ui { struct Point final { @@ -230,4 +231,11 @@ struct TextRange final { unsigned position = 0; unsigned count = 0; }; + +struct Color { + std::uint8_t red; + std::uint8_t green; + std::uint8_t blue; + std::uint8_t alpha; +}; } // namespace cru::ui diff --git a/include/cru/platform/basic_types.hpp b/include/cru/platform/basic_types.hpp index 67637b83..81ee3e34 100644 --- a/include/cru/platform/basic_types.hpp +++ b/include/cru/platform/basic_types.hpp @@ -7,5 +7,4 @@ struct Dpi { }; enum class MouseButton { Left, Right, Middle }; - } // namespace cru::platform diff --git a/include/cru/platform/brush.hpp b/include/cru/platform/brush.hpp new file mode 100644 index 00000000..713d302d --- /dev/null +++ b/include/cru/platform/brush.hpp @@ -0,0 +1,13 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform { +struct Brush : public virtual Interface {}; + +struct SolidColorBrush : public virtual Brush { + virtual ui::Color GetColor() = 0; + virtual void SetColor(const ui::Color& color) = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/debug.hpp b/include/cru/platform/debug.hpp index 24759ee1..21c2b646 100644 --- a/include/cru/platform/debug.hpp +++ b/include/cru/platform/debug.hpp @@ -4,5 +4,5 @@ #include namespace cru::platform::debug { -void DebugMessage(const std::string_view& message); +void DebugMessage(const std::wstring_view& message); } diff --git a/include/cru/platform/dpi_util.hpp b/include/cru/platform/dpi_util.hpp deleted file mode 100644 index 3c0ae6ca..00000000 --- a/include/cru/platform/dpi_util.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once -#include "ui_applicaition.hpp" - -namespace cru::platform { -inline Dpi GetDpi() { - return UiApplication::GetInstance()->GetDpi(); -} - -inline int DipToPixelInternal(const float dip, const float dpi) { - return static_cast(dip * dpi / 96.0f); -} - -inline int DipToPixelX(const float dip_x) { - return DipToPixelInternal(dip_x, GetDpi().x); -} - -inline int DipToPixelY(const float dip_y) { - return DipToPixelInternal(dip_y, GetDpi().y); -} - -inline float DipToPixelInternal(const int pixel, const float dpi) { - return static_cast(pixel) * 96.0f / dpi; -} - -inline float PixelToDipX(const int pixel_x) { - return DipToPixelInternal(pixel_x, GetDpi().x); -} - -inline float PixelToDipY(const int pixel_y) { - return DipToPixelInternal(pixel_y, GetDpi().y); -} -} // namespace cru::platform diff --git a/include/cru/platform/geometry.hpp b/include/cru/platform/geometry.hpp new file mode 100644 index 00000000..aa93fe37 --- /dev/null +++ b/include/cru/platform/geometry.hpp @@ -0,0 +1,26 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "basic_types.hpp" +#include "cru/common/ui_base.hpp" + +namespace cru::platform { +struct Geometry : virtual Interface { + virtual bool FillContains(const ui::Point& point) = 0; +}; + +struct GeometryBuilder : virtual Interface { + virtual bool IsValid() = 0; + virtual void BeginFigure(const ui::Point& point) = 0; + virtual void LineTo(const ui::Point& point) = 0; + virtual void QuadraticBezierTo(const ui::Point& control_point, + const ui::Point& end_point) = 0; + virtual void CloseFigure(bool close) = 0; + virtual Geometry* Build() = 0; + + Geometry* BuildAndDeleteThis() { + Build(); + delete this; + } +}; +} // namespace cru::platform diff --git a/include/cru/platform/graph_factory.hpp b/include/cru/platform/graph_factory.hpp new file mode 100644 index 00000000..f2e5f286 --- /dev/null +++ b/include/cru/platform/graph_factory.hpp @@ -0,0 +1,14 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform { +struct SolidColorBrush; +struct GeometryBuilder; + +struct GraphFactory : virtual Interface { + virtual SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) = 0; + virtual GeometryBuilder* CreateGeometryBuilder() = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/native_window.hpp b/include/cru/platform/native_window.hpp index 5e8897ab..3a8e27ad 100644 --- a/include/cru/platform/native_window.hpp +++ b/include/cru/platform/native_window.hpp @@ -32,7 +32,7 @@ struct NativeWindow : public virtual Interface { // The lefttop of the rect is relative to screen lefttop. virtual void SetWindowRect(const ui::Rect& rect) = 0; - virtual Painter* GetPainter() = 0; + virtual Painter* BeginDraw() = 0; virtual Event<>* DestroyEvent() = 0; virtual Event* ResizeEvent() = 0; diff --git a/include/cru/platform/painter.hpp b/include/cru/platform/painter.hpp new file mode 100644 index 00000000..4ad09247 --- /dev/null +++ b/include/cru/platform/painter.hpp @@ -0,0 +1,15 @@ +#pragma once +#include "cru/common/base.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform { +struct Brush; +struct Geometry; + +struct Painter : virtual Interface { + virtual void StrokeGeometry(Geometry* geometry, Brush* brush, float width) = 0; + virtual void FillGeometry(Geometry* geometry, Brush* brush) = 0; + virtual void EndDraw() = 0; +}; +} // namespace cru::platform diff --git a/include/cru/platform/ui_applicaition.hpp b/include/cru/platform/ui_applicaition.hpp index e149166b..1f30ce17 100644 --- a/include/cru/platform/ui_applicaition.hpp +++ b/include/cru/platform/ui_applicaition.hpp @@ -27,8 +27,6 @@ struct UiApplication : public virtual Interface { virtual std::vector GetAllWindow() = 0; virtual NativeWindow* CreateWindow() = 0; - virtual Dpi GetDpi() = 0; - virtual GraphFactory* GetGraphFactory() = 0; }; } // namespace cru::platform diff --git a/include/cru/platform/win/d2d_util.hpp b/include/cru/platform/win/d2d_util.hpp new file mode 100644 index 00000000..9ff6556a --- /dev/null +++ b/include/cru/platform/win/d2d_util.hpp @@ -0,0 +1,91 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "cru/common/ui_base.hpp" + +namespace cru::platform::win::util { +inline D2D1_COLOR_F Convert(const ui::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 ui::Point& point) { + return D2D1::Point2F(point.x, point.y); +} + +inline D2D1_RECT_F Convert(const ui::Rect& rect) { + return D2D1::RectF(rect.left, rect.top, rect.left + rect.width, + rect.top + rect.height); +} + +inline D2D1_ROUNDED_RECT Convert(const ui::RoundedRect& rounded_rect) { + return D2D1::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radius_x, + rounded_rect.radius_y); +} + +inline D2D1_ELLIPSE Convert(const ui::Ellipse& ellipse) { + return D2D1::Ellipse(Convert(ellipse.center), ellipse.radius_x, + ellipse.radius_y); +} + +inline ui::Color Convert(const D2D1_COLOR_F& color) { + auto floor = [](float n) { return static_cast(n + 0.5f); }; + return ui::Color{floor(color.r * 255.0f), floor(color.g * 255.0f), + floor(color.b * 255.0f), floor(color.a * 255.0f)}; +} + +inline ui::Point Convert(const D2D1_POINT_2F& point) { + return ui::Point(point.x, point.y); +} + +inline ui::Rect Convert(const D2D1_RECT_F& rect) { + return ui::Rect(rect.left, rect.top, rect.right - rect.left, + rect.bottom - rect.top); +} + +inline ui::RoundedRect Convert(const D2D1_ROUNDED_RECT& rounded_rect) { + return ui::RoundedRect(Convert(rounded_rect.rect), rounded_rect.radiusX, + rounded_rect.radiusY); +} + +inline ui::Ellipse Convert(const D2D1_ELLIPSE& ellipse) { + return ui::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::win::util diff --git a/include/cru/platform/win/graph_manager.hpp b/include/cru/platform/win/graph_manager.hpp new file mode 100644 index 00000000..f2e29c22 --- /dev/null +++ b/include/cru/platform/win/graph_manager.hpp @@ -0,0 +1,44 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "../basic_types.hpp" +#include "cru/common/base.hpp" + +namespace cru::platform::win { +class WindowRenderTarget; + +class GraphManager final : public Object { + public: + GraphManager(); + GraphManager(const GraphManager& other) = delete; + GraphManager(GraphManager&& other) = delete; + GraphManager& operator=(const GraphManager& other) = delete; + GraphManager& operator=(GraphManager&& other) = delete; + ~GraphManager() override = default; + + public: + ID2D1Factory1* GetD2D1Factory() const { return d2d1_factory_.Get(); } + + ID2D1DeviceContext* GetD2D1DeviceContext() const { + return d2d1_device_context_.Get(); + } + + ID3D11Device* GetD3D11Device() const { return d3d11_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(); + } + + private: + Microsoft::WRL::ComPtr d3d11_device_; + Microsoft::WRL::ComPtr d2d1_factory_; + Microsoft::WRL::ComPtr d2d1_device_context_; + Microsoft::WRL::ComPtr dxgi_factory_; + Microsoft::WRL::ComPtr dwrite_factory_; + Microsoft::WRL::ComPtr dwrite_system_font_collection_; +}; +} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_application.hpp b/include/cru/platform/win/win_application.hpp index fcc0a7c9..7defa703 100644 --- a/include/cru/platform/win/win_application.hpp +++ b/include/cru/platform/win/win_application.hpp @@ -10,6 +10,8 @@ namespace cru::platform::win { class GodWindow; class TimerManager; class WindowManager; +class GraphManager; +class WinGraphFactory; class WinApplication : public Object, public virtual UiApplication { public: @@ -39,13 +41,16 @@ class WinApplication : public Object, public virtual UiApplication { const std::function& action) override; void CancelTimer(unsigned long id) override; + GraphFactory* GetGraphFactory() override; + + HINSTANCE GetInstanceHandle() const { return h_instance_; } GodWindow* GetGodWindow() const { return god_window_.get(); } - TimerManager* GetTimerManager() const { return timer_manager_.get(); } - WindowManager* GetWindowManager() const { return window_manager_.get(); } + GraphManager* GetGraphManager() const { return graph_manager_.get(); } + WinGraphFactory* GetWinGraphFactory() const { return graph_factory_.get(); } private: HINSTANCE h_instance_; @@ -53,5 +58,7 @@ class WinApplication : public Object, public virtual UiApplication { std::shared_ptr god_window_; std::shared_ptr timer_manager_; std::shared_ptr window_manager_; + std::shared_ptr graph_manager_; + std::shared_ptr graph_factory_; }; } // namespace cru::platform::win diff --git a/include/cru/platform/win/win_brush.hpp b/include/cru/platform/win/win_brush.hpp new file mode 100644 index 00000000..2668215d --- /dev/null +++ b/include/cru/platform/win/win_brush.hpp @@ -0,0 +1,23 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "../brush.hpp" + +namespace cru::platform::win { +class WinSolidColorBrush : public Object, public virtual SolidColorBrush { + public: + explicit WinSolidColorBrush( + Microsoft::WRL::ComPtr brush); + WinSolidColorBrush(const WinSolidColorBrush& other) = delete; + WinSolidColorBrush(WinSolidColorBrush&& other) = delete; + WinSolidColorBrush& operator=(const WinSolidColorBrush& other) = delete; + WinSolidColorBrush& operator=(WinSolidColorBrush&& other) = delete; + ~WinSolidColorBrush() override = default; + + ui::Color GetColor() override; + void SetColor(const ui::Color& color) override; + + private: + Microsoft::WRL::ComPtr brush_; +}; +} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_geometry.hpp b/include/cru/platform/win/win_geometry.hpp new file mode 100644 index 00000000..e8ab7796 --- /dev/null +++ b/include/cru/platform/win/win_geometry.hpp @@ -0,0 +1,45 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "../geometry.hpp" + +namespace cru::platform::win { +class WinGeometryBuilder : public Object, public virtual GeometryBuilder { + public: + explicit WinGeometryBuilder(Microsoft::WRL::ComPtr geometry); + WinGeometryBuilder(const WinGeometryBuilder& other) = delete; + WinGeometryBuilder(WinGeometryBuilder&& other) = delete; + WinGeometryBuilder& operator=(const WinGeometryBuilder& other) = delete; + WinGeometryBuilder& operator=(WinGeometryBuilder&& other) = delete; + ~WinGeometryBuilder() override; + + bool IsValid() override { return geometry_ != nullptr; } + 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; + Geometry* Build() override; + + private: + Microsoft::WRL::ComPtr geometry_; + Microsoft::WRL::ComPtr geometry_sink_; +}; + +class WinGeometry : public Object, public virtual Geometry { + public: + explicit WinGeometry(Microsoft::WRL::ComPtr 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 geometry_; +}; +} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_graph_factory.hpp b/include/cru/platform/win/win_graph_factory.hpp new file mode 100644 index 00000000..b49413e4 --- /dev/null +++ b/include/cru/platform/win/win_graph_factory.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "../graph_factory.hpp" + +namespace cru::platform::win { +class GraphManager; + +class WinGraphFactory : public Object, public virtual GraphFactory { + public: + explicit WinGraphFactory(GraphManager* graph_manager); + WinGraphFactory(const WinGraphFactory& other) = delete; + WinGraphFactory(WinGraphFactory&& other) = delete; + WinGraphFactory& operator=(const WinGraphFactory& other) = delete; + WinGraphFactory& operator=(WinGraphFactory&& other) = delete; + ~WinGraphFactory() override = default; + + SolidColorBrush* CreateSolidColorBrush(const ui::Color& color) override; + GeometryBuilder* CreateGeometryBuilder() override; + + private: + GraphManager* graph_manager_; +}; +} // namespace cru::platform::win diff --git a/include/cru/platform/win/win_native_window.hpp b/include/cru/platform/win/win_native_window.hpp index 9deac767..8b787485 100644 --- a/include/cru/platform/win/win_native_window.hpp +++ b/include/cru/platform/win/win_native_window.hpp @@ -10,6 +10,7 @@ namespace cru::platform::win { class WinApplication; class WindowClass; class WindowManager; +class WindowRenderTarget; class WinNativeWindow : public Object, public virtual NativeWindow { public: @@ -70,6 +71,10 @@ class WinNativeWindow : public Object, public virtual NativeWindow { bool HandleNativeWindowMessage(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param, LRESULT* result); + WindowRenderTarget* GetWindowRenderTarget() const { + return window_render_target_.get(); + } + private: // Get the client rect in pixel. RECT GetClientRectPixel(); @@ -107,6 +112,8 @@ class WinNativeWindow : public Object, public virtual NativeWindow { bool has_focus_ = false; bool is_mouse_in_ = false; + std::shared_ptr window_render_target_; + Event<> destroy_event_; Event resize_event_; Event<> paint_event_; diff --git a/include/cru/platform/win/win_pre_config.hpp b/include/cru/platform/win/win_pre_config.hpp index 2e8bb80e..d6ba4ec7 100644 --- a/include/cru/platform/win/win_pre_config.hpp +++ b/include/cru/platform/win/win_pre_config.hpp @@ -4,3 +4,9 @@ #define WIN32_LEAN_AND_MEAN #include #undef CreateWindow + +#include +#include +#include +#include +#include diff --git a/include/cru/platform/win/window_render_target.hpp b/include/cru/platform/win/window_render_target.hpp new file mode 100644 index 00000000..f328a84a --- /dev/null +++ b/include/cru/platform/win/window_render_target.hpp @@ -0,0 +1,44 @@ +#pragma once +#include "win_pre_config.hpp" + +#include "cru/common/base.hpp" + + +namespace cru::platform::win { +class GraphManager; + +// Represents a window render target. +class WindowRenderTarget : public Object { + public: + WindowRenderTarget(GraphManager* graph_manager, HWND hwnd); + WindowRenderTarget(const WindowRenderTarget& other) = delete; + WindowRenderTarget(WindowRenderTarget&& other) = delete; + WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete; + WindowRenderTarget& operator=(WindowRenderTarget&& other) = delete; + ~WindowRenderTarget() override = default; + + public: + // Get the graph manager that created the render target. + GraphManager* GetGraphManager() const { return graph_manager_; } + + // Get the target bitmap which can be set as the ID2D1DeviceContext's target. + ID2D1Bitmap1* GetTargetBitmap() const { return target_bitmap_.Get(); } + + // Resize the underlying buffer. + void ResizeBuffer(int width, int height); + + // Set this render target as the d2d device context's target. + void SetAsTarget(); + + // Present the data of the underlying buffer to the window. + void Present(); + + private: + void CreateTargetBitmap(); + + private: + GraphManager* graph_manager_; + Microsoft::WRL::ComPtr dxgi_swap_chain_; + Microsoft::WRL::ComPtr target_bitmap_; +}; +} // namespace cru::graph diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5048020d..75e6a0c2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,4 @@ add_library(cru_ui STATIC - graph/graph_manager.cpp - graph/window_render_target.cpp ui/content_control.cpp ui/control.cpp ui/input_util.cpp diff --git a/src/graph/graph_manager.cpp b/src/graph/graph_manager.cpp deleted file mode 100644 index ecc60915..00000000 --- a/src/graph/graph_manager.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "graph_manager.hpp" - -#include -#include -#include -#include -#include - -#include "application.hpp" -#include "exception.hpp" -#include "util/com_util.hpp" -#include "window_render_target.hpp" - -namespace cru::graph { - -GraphManager* GraphManager::GetInstance() { - return Application::GetInstance()->ResolveSingleton( - [](auto) { return new GraphManager{}; }); -} - -GraphManager::GraphManager() { - UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; - -#ifdef CRU_DEBUG - creation_flags |= D3D11_CREATE_DEVICE_DEBUG; -#endif - - const D3D_FEATURE_LEVEL feature_levels[] = { - D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, - D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, - D3D_FEATURE_LEVEL_9_1}; - - Microsoft::WRL::ComPtr d3d11_device_context; - ID3D11Device* d3d11_device; - - ThrowIfFailed(D3D11CreateDevice( - nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, - feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, - &d3d11_device, nullptr, &d3d11_device_context)); - this->d3d11_device_ = util::CreateComSharedPtr(d3d11_device); - - Microsoft::WRL::ComPtr dxgi_device; - ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); - - ID2D1Factory1* d2d1_factory; - ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, - IID_PPV_ARGS(&d2d1_factory))); - this->d2d1_factory_ = util::CreateComSharedPtr(d2d1_factory); - - Microsoft::WRL::ComPtr d2d1_device; - - ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); - - ID2D1DeviceContext* d2d1_device_context; - ThrowIfFailed(d2d1_device->CreateDeviceContext( - D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context)); - this->d2d1_device_context_ = util::CreateComSharedPtr(d2d1_device_context); - - // Identify the physical adapter (GPU or card) this device is runs on. - Microsoft::WRL::ComPtr dxgi_adapter; - ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); - - IDXGIFactory2* dxgi_factory; - // Get the factory object that created the DXGI device. - ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory))); - this->dxgi_factory_ = util::CreateComSharedPtr(dxgi_factory); - - IDWriteFactory* dwrite_factory; - ThrowIfFailed( - DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), - reinterpret_cast(&dwrite_factory))); - this->dwrite_factory_ = util::CreateComSharedPtr(dwrite_factory); - - IDWriteFontCollection* font_collection; - ThrowIfFailed(dwrite_factory_->GetSystemFontCollection(&font_collection)); - this->dwrite_system_font_collection_ = - util::CreateComSharedPtr(font_collection); -} -} // namespace cru::graph diff --git a/src/graph/graph_manager.hpp b/src/graph/graph_manager.hpp deleted file mode 100644 index 4a1e7153..00000000 --- a/src/graph/graph_manager.hpp +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include - -#include "base.hpp" - -struct ID3D11Device; -struct ID3D11DeviceContext; -struct ID2D1Factory1; -struct ID2D1DeviceContext; -struct IDXGIFactory2; -struct IDWriteFontCollection; -struct IDWriteFactory; - -struct ID2D1RenderTarget; - -namespace cru::graph { -class WindowRenderTarget; - -class GraphManager final : public Object { - public: - static GraphManager* GetInstance(); - - private: - GraphManager(); - - public: - GraphManager(const GraphManager& other) = delete; - GraphManager(GraphManager&& other) = delete; - GraphManager& operator=(const GraphManager& other) = delete; - GraphManager& operator=(GraphManager&& other) = delete; - ~GraphManager() override = default; - - public: - ID2D1Factory1* GetD2D1Factory() const { return d2d1_factory_.get(); } - - ID2D1DeviceContext* GetD2D1DeviceContext() const { - return d2d1_device_context_.get(); - } - - ID3D11Device* GetD3D11Device() const { return d3d11_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(); - } - - private: - std::shared_ptr d3d11_device_; - std::shared_ptr d2d1_factory_; - std::shared_ptr d2d1_device_context_; - std::shared_ptr dxgi_factory_; - std::shared_ptr dwrite_factory_; - std::shared_ptr dwrite_system_font_collection_; -}; -} // namespace cru::graph diff --git a/src/graph/graph_util.hpp b/src/graph/graph_util.hpp deleted file mode 100644 index 2d5be5f3..00000000 --- a/src/graph/graph_util.hpp +++ /dev/null @@ -1,63 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include -#include - -#include "exception.hpp" -#include "graph_manager.hpp" - -namespace cru::graph { -struct Dpi { - float x; - float y; -}; - -inline Dpi GetDpi() { - Dpi dpi; - GraphManager::GetInstance()->GetD2D1Factory()->GetDesktopDpi(&dpi.x, &dpi.y); - return dpi; -} - -inline int DipToPixelInternal(const float dip, const float dpi) { - return static_cast(dip * dpi / 96.0f); -} - -inline int DipToPixelX(const float dip_x) { - return DipToPixelInternal(dip_x, GetDpi().x); -} - -inline int DipToPixelY(const float dip_y) { - return DipToPixelInternal(dip_y, GetDpi().y); -} - -inline float DipToPixelInternal(const int pixel, const float dpi) { - return static_cast(pixel) * 96.0f / dpi; -} - -inline float PixelToDipX(const int pixel_x) { - return DipToPixelInternal(pixel_x, GetDpi().x); -} - -inline float PixelToDipY(const int pixel_y) { - return DipToPixelInternal(pixel_y, GetDpi().y); -} - -inline void WithTransform( - ID2D1RenderTarget* render_target, const D2D1_MATRIX_3X2_F matrix, - const std::function& action) { - D2D1_MATRIX_3X2_F old_transform; - render_target->GetTransform(&old_transform); - render_target->SetTransform(old_transform * matrix); - action(render_target); - render_target->SetTransform(old_transform); -} - -inline ID2D1SolidColorBrush* CreateSolidColorBrush(const D2D1_COLOR_F& color) { - ID2D1SolidColorBrush* brush; - ThrowIfFailed(GraphManager::GetInstance() - ->GetD2D1DeviceContext() - ->CreateSolidColorBrush(color, &brush)); - return brush; -} -} // namespace cru::graph diff --git a/src/graph/window_render_target.cpp b/src/graph/window_render_target.cpp deleted file mode 100644 index a36e0faf..00000000 --- a/src/graph/window_render_target.cpp +++ /dev/null @@ -1,97 +0,0 @@ -#include "window_render_target.hpp" - -#include -#include -#include -#include - -#include "exception.hpp" -#include "graph_manager.hpp" -#include "graph_util.hpp" -#include "util/com_util.hpp" - -namespace cru::graph { -WindowRenderTarget::WindowRenderTarget(GraphManager* graph_manager, HWND hwnd) { - this->graph_manager_ = graph_manager; - - const auto d3d11_device = graph_manager->GetD3D11Device(); - const auto dxgi_factory = graph_manager->GetDxgiFactory(); - - // Allocate a descriptor. - DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0}; - 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.Flags = 0; - - IDXGISwapChain1* dxgi_swap_chain; - // 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)); - this->dxgi_swap_chain_ = util::CreateComSharedPtr(dxgi_swap_chain); - - CreateTargetBitmap(); -} - -void WindowRenderTarget::ResizeBuffer(const int width, const int height) { - const auto graph_manager = graph_manager_; - const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext(); - - ID2D1Image* old_target; - d2d1_device_context->GetTarget(&old_target); - const auto target_this = old_target == this->target_bitmap_.get(); - if (target_this) d2d1_device_context->SetTarget(nullptr); - - util::SafeRelease(old_target); - target_bitmap_.reset(); - - ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height, - DXGI_FORMAT_UNKNOWN, 0)); - - CreateTargetBitmap(); - - if (target_this) d2d1_device_context->SetTarget(target_bitmap_.get()); -} - -void WindowRenderTarget::SetAsTarget() { - graph_manager_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.get()); -} - -void WindowRenderTarget::Present() { - ThrowIfFailed(dxgi_swap_chain_->Present(1, 0)); -} - -void WindowRenderTarget::CreateTargetBitmap() { - assert(target_bitmap_ == nullptr); // target bitmap must not exist. - - // Direct2D needs the dxgi version of the backbuffer surface pointer. - Microsoft::WRL::ComPtr dxgi_back_buffer; - ThrowIfFailed( - dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer))); - - const auto dpi = GetDpi(); - - 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); - - ID2D1Bitmap1* bitmap; - // Get a D2D surface from the DXGI back buffer to use as the D2D render - // target. - ThrowIfFailed( - graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( - dxgi_back_buffer.Get(), &bitmap_properties, &bitmap)); - this->target_bitmap_ = util::CreateComSharedPtr(bitmap); -} -} // namespace cru::graph diff --git a/src/graph/window_render_target.hpp b/src/graph/window_render_target.hpp deleted file mode 100644 index 9b93df19..00000000 --- a/src/graph/window_render_target.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include -#include - -#include "base.hpp" - -struct IDXGISwapChain1; -struct ID2D1Bitmap1; - -namespace cru::graph { -class GraphManager; - -// Represents a window render target. -class WindowRenderTarget : public Object { - public: - WindowRenderTarget(GraphManager* graph_manager, HWND hwnd); - WindowRenderTarget(const WindowRenderTarget& other) = delete; - WindowRenderTarget(WindowRenderTarget&& other) = delete; - WindowRenderTarget& operator=(const WindowRenderTarget& other) = delete; - WindowRenderTarget& operator=(WindowRenderTarget&& other) = delete; - ~WindowRenderTarget() override = default; - - public: - // Get the graph manager that created the render target. - GraphManager* GetGraphManager() const { return graph_manager_; } - - // Get the target bitmap which can be set as the ID2D1DeviceContext's target. - ID2D1Bitmap1* GetTargetBitmap() const { return target_bitmap_.get(); } - - // Resize the underlying buffer. - void ResizeBuffer(int width, int height); - - // Set this render target as the d2d device context's target. - void SetAsTarget(); - - // Present the data of the underlying buffer to the window. - void Present(); - - private: - void CreateTargetBitmap(); - - private: - GraphManager* graph_manager_; - std::shared_ptr dxgi_swap_chain_; - std::shared_ptr target_bitmap_; -}; -} // namespace cru::graph diff --git a/src/platform_win/CMakeLists.txt b/src/platform_win/CMakeLists.txt index 9e7d8a89..40c9d85a 100644 --- a/src/platform_win/CMakeLists.txt +++ b/src/platform_win/CMakeLists.txt @@ -2,11 +2,16 @@ add_library(cru_platform_win STATIC debug.cpp exception.cpp god_window.cpp + graph_manager.cpp timer.cpp win_application.cpp + win_brush.cpp + win_geometry.cpp + win_graph_factory.cpp win_native_window.cpp window_class.cpp - window_manager.cpp) + window_manager.cpp + window_render_target.cpp) target_include_directories(cru_platform_win PUBLIC ${PROJECT_SOURCE_DIR}/include .) target_link_libraries(cru_platform_win PRIVATE D3D11 D2d1 DWrite) target_compile_definitions(cru_platform_win PUBLIC UNICODE _UNICODE) # use unicode diff --git a/src/platform_win/debug.cpp b/src/platform_win/debug.cpp index cdff7963..f52d41bd 100644 --- a/src/platform_win/debug.cpp +++ b/src/platform_win/debug.cpp @@ -3,7 +3,7 @@ #include "cru/platform/debug.hpp" -namespace cru::debug { +namespace cru::platform::debug { void DebugMessage(const std::wstring_view& message) { ::OutputDebugStringW(message.data()); } diff --git a/src/platform_win/dpi_util.hpp b/src/platform_win/dpi_util.hpp new file mode 100644 index 00000000..92819e0f --- /dev/null +++ b/src/platform_win/dpi_util.hpp @@ -0,0 +1,32 @@ +#pragma once + +// The dpi awareness needs to be implemented in the future. Currently we use 96 +// as default. + +namespace cru::platform { +inline Dpi GetDpi() { return Dpi{96.0f, 96.0f}; } + +inline int DipToPixelInternal(const float dip, const float dpi) { + return static_cast(dip * dpi / 96.0f); +} + +inline int DipToPixelX(const float dip_x) { + return DipToPixelInternal(dip_x, GetDpi().x); +} + +inline int DipToPixelY(const float dip_y) { + return DipToPixelInternal(dip_y, GetDpi().y); +} + +inline float DipToPixelInternal(const int pixel, const float dpi) { + return static_cast(pixel) * 96.0f / dpi; +} + +inline float PixelToDipX(const int pixel_x) { + return DipToPixelInternal(pixel_x, GetDpi().x); +} + +inline float PixelToDipY(const int pixel_y) { + return DipToPixelInternal(pixel_y, GetDpi().y); +} +} // namespace cru::platform diff --git a/src/platform_win/graph_manager.cpp b/src/platform_win/graph_manager.cpp new file mode 100644 index 00000000..4961867b --- /dev/null +++ b/src/platform_win/graph_manager.cpp @@ -0,0 +1,51 @@ +#include "cru/platform/win/graph_manager.hpp" + +#include "cru/platform/win/exception.hpp" + +namespace cru::platform::win { +GraphManager::GraphManager() { + UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; + +#ifdef CRU_DEBUG + creation_flags |= D3D11_CREATE_DEVICE_DEBUG; +#endif + + const D3D_FEATURE_LEVEL feature_levels[] = { + D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, + D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, + D3D_FEATURE_LEVEL_9_1}; + + Microsoft::WRL::ComPtr d3d11_device_context; + + ThrowIfFailed(D3D11CreateDevice( + nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, creation_flags, + feature_levels, ARRAYSIZE(feature_levels), D3D11_SDK_VERSION, + &d3d11_device_, nullptr, &d3d11_device_context)); + + Microsoft::WRL::ComPtr dxgi_device; + ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf())); + + ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, + IID_PPV_ARGS(&d2d1_factory_))); + + Microsoft::WRL::ComPtr d2d1_device; + + ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); + + ThrowIfFailed(d2d1_device->CreateDeviceContext( + D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_)); + + // Identify the physical adapter (GPU or card) this device is runs on. + Microsoft::WRL::ComPtr dxgi_adapter; + ThrowIfFailed(dxgi_device->GetAdapter(&dxgi_adapter)); + + // Get the factory object that created the DXGI device. + ThrowIfFailed(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory_))); + + ThrowIfFailed( + DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), + reinterpret_cast(dwrite_factory_.GetAddressOf()))); + + ThrowIfFailed(dwrite_factory_->GetSystemFontCollection(&dwrite_system_font_collection_)); +} +} // namespace cru::graph diff --git a/src/platform_win/win_application.cpp b/src/platform_win/win_application.cpp index d96fd8d1..ce85dc67 100644 --- a/src/platform_win/win_application.cpp +++ b/src/platform_win/win_application.cpp @@ -1,17 +1,20 @@ #include "cru/platform/win/win_application.hpp" -#include - #include "cru/platform/win/exception.hpp" #include "cru/platform/win/god_window.hpp" +#include "cru/platform/win/graph_manager.hpp" +#include "cru/platform/win/win_graph_factory.hpp" #include "god_window_message.hpp" #include "timer.hpp" +#include "window_manager.hpp" + +#include namespace cru::platform { UiApplication* UiApplication::GetInstance() { return win::WinApplication::GetInstance(); } -} +} // namespace cru::platform namespace cru::platform::win { WinApplication* WinApplication::instance_ = nullptr; @@ -33,6 +36,9 @@ WinApplication::WinApplication(HINSTANCE h_instance) : h_instance_(h_instance) { god_window_ = std::make_shared(this); timer_manager_ = std::make_shared(god_window_.get()); + window_manager_ = std::make_shared(this); + graph_manager_ = std::make_shared(); + graph_factory_ = std::make_shared(graph_manager_.get()); } WinApplication::~WinApplication() { instance_ = nullptr; } @@ -73,4 +79,5 @@ unsigned long WinApplication::SetInterval( void WinApplication::CancelTimer(unsigned long id) { timer_manager_->KillTimer(static_cast(id)); } +GraphFactory* WinApplication::GetGraphFactory() { return graph_factory_.get(); } } // namespace cru::platform::win diff --git a/src/platform_win/win_brush.cpp b/src/platform_win/win_brush.cpp new file mode 100644 index 00000000..189dfd57 --- /dev/null +++ b/src/platform_win/win_brush.cpp @@ -0,0 +1,21 @@ +#include "cru/platform/win/win_brush.hpp" + +#include "cru/platform/win/d2d_util.hpp" +#include "cru/platform/win/exception.hpp" + +#include + +namespace cru::platform::win { +WinSolidColorBrush::WinSolidColorBrush( + Microsoft::WRL::ComPtr brush) { + assert(brush); + brush_ = std::move(brush); +} + +ui::Color cru::platform::win::WinSolidColorBrush::GetColor() { + return util::Convert(brush_->GetColor()); +} +void WinSolidColorBrush::SetColor(const ui::Color& color) { + brush_->SetColor(util::Convert(color)); +} +} // namespace cru::platform::win diff --git a/src/platform_win/win_geometry.cpp b/src/platform_win/win_geometry.cpp new file mode 100644 index 00000000..1078700b --- /dev/null +++ b/src/platform_win/win_geometry.cpp @@ -0,0 +1,65 @@ +#include "cru/platform/win/win_geometry.hpp" + +#include "cru/platform/win/d2d_util.hpp" +#include "cru/platform/win/exception.hpp" + +#include + +namespace cru::platform::win { +WinGeometryBuilder::WinGeometryBuilder( + Microsoft::WRL::ComPtr geometry) { + assert(geometry); + ThrowIfFailed(geometry->Open(&geometry_sink_)); + geometry_ = std::move(geometry); +} + +WinGeometryBuilder::~WinGeometryBuilder() { + if (geometry_sink_) { + ThrowIfFailed(geometry_sink_->Close()); + } +} + +void WinGeometryBuilder::BeginFigure(const ui::Point& point) { + assert(IsValid()); + geometry_sink_->BeginFigure(util::Convert(point), D2D1_FIGURE_BEGIN_FILLED); +} + +void WinGeometryBuilder::LineTo(const ui::Point& point) { + assert(IsValid()); + geometry_sink_->AddLine(util::Convert(point)); +} + +void WinGeometryBuilder::QuadraticBezierTo(const ui::Point& control_point, + const ui::Point& end_point) { + assert(IsValid()); + geometry_sink_->AddQuadraticBezier(D2D1::QuadraticBezierSegment( + util::Convert(control_point), util::Convert(end_point))); +} + +void WinGeometryBuilder::CloseFigure(bool close) { + assert(IsValid()); + geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED + : D2D1_FIGURE_END_OPEN); +} + +Geometry* WinGeometryBuilder::Build() { + assert(IsValid()); + ThrowIfFailed(geometry_sink_->Close()); + geometry_sink_ = nullptr; + const auto geometry = new WinGeometry(geometry_); + geometry_ = nullptr; + return geometry; +} + +WinGeometry::WinGeometry(Microsoft::WRL::ComPtr geometry) { + assert(geometry); + geometry_ = std::move(geometry); +} + +bool WinGeometry::FillContains(const ui::Point& point) { + BOOL result; + ThrowIfFailed(geometry_->FillContainsPoint( + util::Convert(point), D2D1::Matrix3x2F::Identity(), &result)); + return result != 0; +} +} // namespace cru::platform::win diff --git a/src/platform_win/win_graph_factory.cpp b/src/platform_win/win_graph_factory.cpp new file mode 100644 index 00000000..b21c58d5 --- /dev/null +++ b/src/platform_win/win_graph_factory.cpp @@ -0,0 +1,31 @@ +#include "cru/platform/win/win_graph_factory.hpp" + +#include "cru/platform/win/d2d_util.hpp" +#include "cru/platform/win/exception.hpp" +#include "cru/platform/win/graph_manager.hpp" +#include "cru/platform/win/win_brush.hpp" +#include "cru/platform/win/win_geometry.hpp" + +#include + +namespace cru::platform::win { +WinGraphFactory::WinGraphFactory(GraphManager* graph_manager) { + assert(graph_manager); + graph_manager_ = graph_manager; +} + +SolidColorBrush* WinGraphFactory::CreateSolidColorBrush( + const ui::Color& color) { + Microsoft::WRL::ComPtr brush; + ThrowIfFailed(graph_manager_->GetD2D1DeviceContext()->CreateSolidColorBrush( + util::Convert(color), &brush)); + return new WinSolidColorBrush(std::move(brush)); +} + +GeometryBuilder* WinGraphFactory::CreateGeometryBuilder() { + Microsoft::WRL::ComPtr geometry; + ThrowIfFailed( + graph_manager_->GetD2D1Factory()->CreatePathGeometry(&geometry)); + return new WinGeometryBuilder(std::move(geometry)); +} +} // namespace cru::platform::win diff --git a/src/platform_win/win_native_window.cpp b/src/platform_win/win_native_window.cpp index 74d9466f..5c159290 100644 --- a/src/platform_win/win_native_window.cpp +++ b/src/platform_win/win_native_window.cpp @@ -1,9 +1,9 @@ #include "cru/platform/win/win_native_window.hpp" -#include "cru/platform/dpi_util.hpp" #include "cru/platform/win/exception.hpp" #include "cru/platform/win/win_application.hpp" #include "cru/platform/win/window_class.hpp" +#include "dpi_util.hpp" #include "window_manager.hpp" #include @@ -309,8 +309,7 @@ void WinNativeWindow::OnMouseUpInternal(MouseButton button, POINT point) { mouse_up_event_.Raise(button, dip_point); } -void WinNativeWindow::OnMouseWheelInternal(short delta, POINT point) { -} +void WinNativeWindow::OnMouseWheelInternal(short delta, POINT point) {} void WinNativeWindow::OnKeyDownInternal(int virtual_code) { key_down_event_.Raise(virtual_code); @@ -320,12 +319,9 @@ void WinNativeWindow::OnKeyUpInternal(int virtual_code) { key_up_event_.Raise(virtual_code); } -void WinNativeWindow::OnCharInternal(wchar_t c) { -} +void WinNativeWindow::OnCharInternal(wchar_t c) {} -void WinNativeWindow::OnActivatedInternal() { -} +void WinNativeWindow::OnActivatedInternal() {} -void WinNativeWindow::OnDeactivatedInternal() { -} +void WinNativeWindow::OnDeactivatedInternal() {} } // namespace cru::platform::win diff --git a/src/platform_win/window_render_target.cpp b/src/platform_win/window_render_target.cpp new file mode 100644 index 00000000..f4836d17 --- /dev/null +++ b/src/platform_win/window_render_target.cpp @@ -0,0 +1,89 @@ +#include "cru/platform/win/window_render_target.hpp" + +#include "cru/platform/win/exception.hpp" +#include "cru/platform/win/graph_manager.hpp" +#include "dpi_util.hpp" + +#include + +namespace cru::platform::win { +WindowRenderTarget::WindowRenderTarget(GraphManager* graph_manager, HWND hwnd) { + this->graph_manager_ = graph_manager; + + const auto d3d11_device = graph_manager->GetD3D11Device(); + const auto dxgi_factory = graph_manager->GetDxgiFactory(); + + // Allocate a descriptor. + DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0}; + 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.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 WindowRenderTarget::ResizeBuffer(const int width, const int height) { + const auto graph_manager = graph_manager_; + const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext(); + + Microsoft::WRL::ComPtr old_target; + d2d1_device_context->GetTarget(&old_target); + const auto target_this = old_target == this->target_bitmap_; + if (target_this) d2d1_device_context->SetTarget(nullptr); + + old_target = nullptr; + target_bitmap_ = nullptr; + + ThrowIfFailed(dxgi_swap_chain_->ResizeBuffers(0, width, height, + DXGI_FORMAT_UNKNOWN, 0)); + + CreateTargetBitmap(); + + if (target_this) d2d1_device_context->SetTarget(target_bitmap_.Get()); +} + +void WindowRenderTarget::SetAsTarget() { + graph_manager_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.Get()); +} + +void WindowRenderTarget::Present() { + ThrowIfFailed(dxgi_swap_chain_->Present(1, 0)); +} + +void WindowRenderTarget::CreateTargetBitmap() { + assert(target_bitmap_ == nullptr); // target bitmap must not exist. + + // Direct2D needs the dxgi version of the backbuffer surface pointer. + Microsoft::WRL::ComPtr dxgi_back_buffer; + ThrowIfFailed( + dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer))); + + const auto dpi = GetDpi(); + + 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( + graph_manager_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface( + dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_)); +} +} // namespace cru::platform::win diff --git a/src/ui/d2d_util.hpp b/src/ui/d2d_util.hpp deleted file mode 100644 index 2ec8ba98..00000000 --- a/src/ui/d2d_util.hpp +++ /dev/null @@ -1,82 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include - -#include "ui_base.hpp" - -namespace cru::ui { -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 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::ui diff --git a/src/util/com_util.hpp b/src/util/com_util.hpp deleted file mode 100644 index bbaf1c27..00000000 --- a/src/util/com_util.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once -#include "pre.hpp" - -#include - -namespace cru::util { - -template -std::shared_ptr CreateComSharedPtr(TInterface* p) { - return std::shared_ptr(p, [](TInterface* ptr) { - if (ptr != nullptr) ptr->Release(); - }); -} - -template -void SafeRelease(TInterface*& p) { - if (p != nullptr) { - p->Release(); - p = nullptr; - } -} -} // namespace cru::util -- cgit v1.2.3