aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ui/render/border_render_object.cpp16
-rw-r--r--src/ui/render/flex_layout_render_object.cpp4
-rw-r--r--src/ui/render/text_render_object.cpp19
-rw-r--r--src/ui/render/window_render_object.cpp6
-rw-r--r--src/ui/ui_manager.cpp8
-rw-r--r--src/ui/window.cpp4
-rw-r--r--src/win/graph/CMakeLists.txt3
-rw-r--r--src/win/graph/d2d_painter.cpp93
-rw-r--r--src/win/graph/graph_manager.cpp61
-rw-r--r--src/win/graph/win_brush.cpp10
-rw-r--r--src/win/graph/win_font.cpp11
-rw-r--r--src/win/graph/win_geometry.cpp25
-rw-r--r--src/win/graph/win_graph_factory.cpp97
-rw-r--r--src/win/graph/win_painter.cpp93
-rw-r--r--src/win/graph/win_text_layout.cpp18
-rw-r--r--src/win/native/win_application.cpp9
-rw-r--r--src/win/native/win_native_window.cpp6
-rw-r--r--src/win/native/window_painter.cpp12
-rw-r--r--src/win/native/window_painter.hpp4
-rw-r--r--src/win/native/window_render_target.cpp21
20 files changed, 261 insertions, 259 deletions
diff --git a/src/ui/render/border_render_object.cpp b/src/ui/render/border_render_object.cpp
index 541be473..99c2cb4c 100644
--- a/src/ui/render/border_render_object.cpp
+++ b/src/ui/render/border_render_object.cpp
@@ -3,20 +3,20 @@
#include "cru/platform/debug.hpp"
#include "cru/platform/graph/geometry.hpp"
#include "cru/platform/graph/graph_factory.hpp"
-#include "cru/platform/graph/painter_util.hpp"
+#include "cru/platform/graph/util/painter_util.hpp"
#include <algorithm>
#include <cassert>
namespace cru::ui::render {
BorderRenderObject::BorderRenderObject(
- std::shared_ptr<platform::graph::Brush> brush) {
+ std::shared_ptr<platform::graph::IBrush> brush) {
assert(brush);
this->border_brush_ = std::move(brush);
RecreateGeometry();
}
-void BorderRenderObject::Draw(platform::graph::Painter* painter) {
+void BorderRenderObject::Draw(platform::graph::IPainter* painter) {
painter->FillGeometry(geometry_.get(), border_brush_.get());
if (const auto child = GetChild()) {
auto offset = child->GetOffset();
@@ -154,7 +154,7 @@ void BorderRenderObject::RecreateGeometry() {
geometry_.reset();
border_outer_geometry_.reset();
- auto f = [](platform::graph::GeometryBuilder* builder, const Rect& rect,
+ auto f = [](platform::graph::IGeometryBuilder* builder, const Rect& rect,
const CornerRadius& corner) {
builder->BeginFigure(Point(rect.left + corner.left_top.x, rect.top));
builder->LineTo(Point(rect.GetRight() - corner.right_top.x, rect.top));
@@ -181,18 +181,18 @@ void BorderRenderObject::RecreateGeometry() {
const Rect outer_rect{margin.left, margin.top,
size.width - margin.GetHorizontalTotal(),
size.height - margin.GetVerticalTotal()};
- const auto graph_factory = platform::graph::GraphFactory::GetInstance();
- std::unique_ptr<platform::graph::GeometryBuilder> builder{
+ const auto graph_factory = platform::graph::IGraphFactory::GetInstance();
+ std::unique_ptr<platform::graph::IGeometryBuilder> builder{
graph_factory->CreateGeometryBuilder()};
f(builder.get(), outer_rect, corner_radius_);
- border_outer_geometry_.reset(builder->Build());
+ border_outer_geometry_.reset(builder->End());
builder.reset();
const Rect inner_rect = outer_rect.Shrink(border_thickness_);
builder.reset(graph_factory->CreateGeometryBuilder());
f(builder.get(), outer_rect, corner_radius_);
f(builder.get(), inner_rect, corner_radius_);
- geometry_.reset(builder->Build());
+ geometry_.reset(builder->End());
builder.reset();
}
} // namespace cru::ui::render
diff --git a/src/ui/render/flex_layout_render_object.cpp b/src/ui/render/flex_layout_render_object.cpp
index 7528439f..0093f1ad 100644
--- a/src/ui/render/flex_layout_render_object.cpp
+++ b/src/ui/render/flex_layout_render_object.cpp
@@ -1,7 +1,7 @@
#include "cru/ui/render/flex_layout_render_object.hpp"
#include "cru/platform/debug.hpp"
-#include "cru/platform/graph/painter_util.hpp"
+#include "cru/platform/graph/util/painter_util.hpp"
#include <algorithm>
#include <cassert>
@@ -15,7 +15,7 @@ FlexChildLayoutData* FlexLayoutRenderObject::GetChildLayoutData(int position) {
return &child_layout_data_[position];
}
-void FlexLayoutRenderObject::Draw(platform::graph::Painter* painter) {
+void FlexLayoutRenderObject::Draw(platform::graph::IPainter* painter) {
for (const auto child : GetChildren()) {
auto offset = child->GetOffset();
platform::graph::util::WithTransform(
diff --git a/src/ui/render/text_render_object.cpp b/src/ui/render/text_render_object.cpp
index c886ee7a..849bff11 100644
--- a/src/ui/render/text_render_object.cpp
+++ b/src/ui/render/text_render_object.cpp
@@ -1,7 +1,7 @@
#include "cru/ui/render/text_render_object.hpp"
#include "cru/platform/graph/graph_factory.hpp"
-#include "cru/platform/graph/painter_util.hpp"
+#include "cru/platform/graph/util/painter_util.hpp"
#include "cru/platform/graph/text_layout.hpp"
#include <algorithm>
@@ -9,9 +9,9 @@
namespace cru::ui::render {
TextRenderObject::TextRenderObject(
- std::shared_ptr<platform::graph::Brush> brush,
- std::shared_ptr<platform::graph::FontDescriptor> font,
- std::shared_ptr<platform::graph::Brush> selection_brush) {
+ std::shared_ptr<platform::graph::IBrush> brush,
+ std::shared_ptr<platform::graph::IFontDescriptor> font,
+ std::shared_ptr<platform::graph::IBrush> selection_brush) {
assert(brush);
assert(font);
assert(selection_brush);
@@ -20,8 +20,7 @@ TextRenderObject::TextRenderObject(
font.swap(font_);
selection_brush.swap(selection_brush_);
- const auto graph_factory = platform::graph::GraphFactory::GetInstance();
-
+ const auto graph_factory = platform::graph::IGraphFactory::GetInstance();
text_layout_.reset(graph_factory->CreateTextLayout(font_, L""));
}
@@ -33,22 +32,22 @@ void TextRenderObject::SetText(std::wstring new_text) {
text_layout_->SetText(std::move(new_text));
}
-std::shared_ptr<platform::graph::FontDescriptor> TextRenderObject::GetFont()
+std::shared_ptr<platform::graph::IFontDescriptor> TextRenderObject::GetFont()
const {
return text_layout_->GetFont();
}
void TextRenderObject::SetFont(
- std::shared_ptr<platform::graph::FontDescriptor> font) {
+ std::shared_ptr<platform::graph::IFontDescriptor> font) {
text_layout_->SetFont(std::move(font));
}
-void TextRenderObject::Draw(platform::graph::Painter* painter) {
+void TextRenderObject::Draw(platform::graph::IPainter* painter) {
platform::graph::util::WithTransform(
painter,
platform::Matrix::Translation(GetMargin().left + GetPadding().left,
GetMargin().top + GetPadding().top),
- [this](platform::graph::Painter* p) {
+ [this](platform::graph::IPainter* p) {
if (this->selection_range_.has_value()) {
const auto&& rects =
text_layout_->TextRangeRect(this->selection_range_.value());
diff --git a/src/ui/render/window_render_object.cpp b/src/ui/render/window_render_object.cpp
index 4fc57ad1..f2e29603 100644
--- a/src/ui/render/window_render_object.cpp
+++ b/src/ui/render/window_render_object.cpp
@@ -1,6 +1,6 @@
#include "cru/ui/render/window_render_object.hpp"
-#include "cru/platform/graph/painter_util.hpp"
+#include "cru/platform/graph/util/painter_util.hpp"
#include "cru/platform/native/native_window.hpp"
#include "cru/ui/window.hpp"
@@ -13,13 +13,13 @@ void WindowRenderObject::MeasureAndLayout() {
Layout(Rect{Point{}, client_size});
}
-void WindowRenderObject::Draw(platform::graph::Painter* painter) {
+void WindowRenderObject::Draw(platform::graph::IPainter* painter) {
painter->Clear(colors::white);
if (const auto child = GetChild()) {
auto offset = child->GetOffset();
platform::graph::util::WithTransform(
painter, platform::Matrix::Translation(offset.x, offset.y),
- [child](auto rt) { child->Draw(rt); });
+ [child](platform::graph::IPainter* p) { child->Draw(p); });
}
}
diff --git a/src/ui/ui_manager.cpp b/src/ui/ui_manager.cpp
index 0c3a8f49..82b859c8 100644
--- a/src/ui/ui_manager.cpp
+++ b/src/ui/ui_manager.cpp
@@ -7,13 +7,13 @@
namespace cru::ui {
PredefineResources::PredefineResources() {
- const auto graph_factory = platform::graph::GraphFactory::GetInstance();
+ const auto graph_factory = platform::graph::IGraphFactory::GetInstance();
- button_normal_border_brush.reset(static_cast<platform::graph::Brush*>(
+ button_normal_border_brush.reset(static_cast<platform::graph::IBrush*>(
graph_factory->CreateSolidColorBrush(colors::black)));
- text_block_selection_brush.reset(static_cast<platform::graph::Brush*>(
+ text_block_selection_brush.reset(static_cast<platform::graph::IBrush*>(
graph_factory->CreateSolidColorBrush(colors::skyblue)));
- text_block_text_brush.reset(static_cast<platform::graph::Brush*>(
+ text_block_text_brush.reset(static_cast<platform::graph::IBrush*>(
graph_factory->CreateSolidColorBrush(colors::black)));
text_block_font.reset(graph_factory->CreateFontDescriptor(L"等线", 24.0f));
}
diff --git a/src/ui/window.cpp b/src/ui/window.cpp
index 291f6fb6..1c035081 100644
--- a/src/ui/window.cpp
+++ b/src/ui/window.cpp
@@ -178,9 +178,9 @@ void Window::OnNativeDestroy() { delete this; }
void Window::OnNativePaint() {
const auto painter =
- std::unique_ptr<platform::graph::Painter>(native_window_->BeginPaint());
+ std::unique_ptr<platform::graph::IPainter>(native_window_->BeginPaint());
render_object_->Draw(painter.get());
- painter->EndDraw();
+ painter->End();
}
void Window::OnNativeResize(const Size& size) {
diff --git a/src/win/graph/CMakeLists.txt b/src/win/graph/CMakeLists.txt
index 61749a6a..d326eb2b 100644
--- a/src/win/graph/CMakeLists.txt
+++ b/src/win/graph/CMakeLists.txt
@@ -1,9 +1,8 @@
add_library(cru_win_graph STATIC
- d2d_painter.cpp
- graph_manager.cpp
win_brush.cpp
win_font.cpp
win_geometry.cpp
win_graph_factory.cpp
+ win_painter.cpp
win_text_layout.cpp)
target_link_libraries(cru_win_graph PUBLIC D3D11 D2d1 DWrite cru_win_base)
diff --git a/src/win/graph/d2d_painter.cpp b/src/win/graph/d2d_painter.cpp
deleted file mode 100644
index 26f6bef0..00000000
--- a/src/win/graph/d2d_painter.cpp
+++ /dev/null
@@ -1,93 +0,0 @@
-#include "cru/win/graph/d2d_painter.hpp"
-
-#include "cru/win/exception.hpp"
-#include "cru/win/graph/d2d_util.hpp"
-#include "cru/win/graph/graph_manager.hpp"
-#include "cru/win/graph/win_brush.hpp"
-#include "cru/win/graph/win_geometry.hpp"
-#include "cru/win/graph/win_text_layout.hpp"
-
-#include <cassert>
-
-namespace cru::win::graph {
-D2DPainter::D2DPainter(ID2D1RenderTarget* render_target) {
- assert(render_target);
- render_target_ = render_target;
-}
-
-platform::Matrix D2DPainter::GetTransform() {
- assert(!IsDisposed());
- D2D1_MATRIX_3X2_F m;
- render_target_->GetTransform(&m);
- return util::Convert(m);
-}
-
-void D2DPainter::SetTransform(const platform::Matrix& matrix) {
- assert(!IsDisposed());
- render_target_->SetTransform(util::Convert(matrix));
-}
-
-void D2DPainter::Clear(const ui::Color& color) {
- assert(!IsDisposed());
- render_target_->Clear(util::Convert(color));
-}
-
-void D2DPainter::StrokeRectangle(const ui::Rect& rectangle,
- platform::graph::Brush* brush, float width) {
- assert(!IsDisposed());
- const auto b = dynamic_cast<WinBrush*>(brush);
- assert(b);
- render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(),
- width);
-}
-
-void D2DPainter::FillRectangle(const ui::Rect& rectangle,
- platform::graph::Brush* brush) {
- assert(!IsDisposed());
- const auto b = dynamic_cast<WinBrush*>(brush);
- assert(b);
- render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush());
-}
-
-void D2DPainter::StrokeGeometry(platform::graph::Geometry* geometry,
- platform::graph::Brush* brush, float width) {
- assert(!IsDisposed());
- const auto g = dynamic_cast<WinGeometry*>(geometry);
- assert(g);
- const auto b = dynamic_cast<WinBrush*>(brush);
- assert(b);
-
- render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width);
-}
-
-void D2DPainter::FillGeometry(platform::graph::Geometry* geometry,
- platform::graph::Brush* brush) {
- assert(!IsDisposed());
- const auto g = dynamic_cast<WinGeometry*>(geometry);
- assert(g);
- const auto b = dynamic_cast<WinBrush*>(brush);
- assert(b);
-
- render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush());
-}
-
-void D2DPainter::DrawText(const ui::Point& offset,
- platform::graph::TextLayout* text_layout,
- platform::graph::Brush* brush) {
- assert(!IsDisposed());
- const auto t = dynamic_cast<WinTextLayout*>(text_layout);
- assert(t);
- const auto b = dynamic_cast<WinBrush*>(brush);
- assert(b);
-
- render_target_->DrawTextLayout(util::Convert(offset),
- t->GetDWriteTextLayout(), b->GetD2DBrush());
-}
-
-void D2DPainter::EndDraw() {
- if (!is_disposed_) {
- is_disposed_ = true;
- DoEndDraw();
- }
-}
-} // namespace cru::win::graph
diff --git a/src/win/graph/graph_manager.cpp b/src/win/graph/graph_manager.cpp
deleted file mode 100644
index 30e1a14e..00000000
--- a/src/win/graph/graph_manager.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-#include "cru/win/graph/graph_manager.hpp"
-
-#include "cru/win/exception.hpp"
-
-#include <cassert>
-
-namespace cru::win::graph {
-GraphManager* GraphManager::GetInstance() {
- static GraphManager* instance = new GraphManager();
- return instance;
-}
-
-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<ID3D11DeviceContext> 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<IDXGIDevice> dxgi_device;
- ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf()));
-
- ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
- IID_PPV_ARGS(&d2d1_factory_)));
-
- Microsoft::WRL::ComPtr<ID2D1Device> 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<IDXGIAdapter> 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<IUnknown**>(dwrite_factory_.GetAddressOf())));
-
- ThrowIfFailed(dwrite_factory_->GetSystemFontCollection(
- &dwrite_system_font_collection_));
-
- graph_factory_.reset(new WinGraphFactory(this));
-}
-} // namespace cru::win::graph
diff --git a/src/win/graph/win_brush.cpp b/src/win/graph/win_brush.cpp
index 262c3302..0c783366 100644
--- a/src/win/graph/win_brush.cpp
+++ b/src/win/graph/win_brush.cpp
@@ -1,16 +1,16 @@
#include "cru/win/graph/win_brush.hpp"
#include "cru/win/exception.hpp"
-#include "cru/win/graph/d2d_util.hpp"
-#include "cru/win/graph/graph_manager.hpp"
+#include "cru/win/graph/win_native_factory.hpp"
+#include "cru/win/graph/util/convert_util.hpp"
#include <cassert>
namespace cru::win::graph {
-WinSolidColorBrush::WinSolidColorBrush(GraphManager* graph_manager,
+WinSolidColorBrush::WinSolidColorBrush(IWinNativeFactory* factory,
const ui::Color& color) {
- assert(graph_manager);
- ThrowIfFailed(graph_manager->GetD2D1DeviceContext()->CreateSolidColorBrush(
+ assert(factory);
+ ThrowIfFailed(factory->GetD2D1DeviceContext()->CreateSolidColorBrush(
util::Convert(color), &brush_));
}
diff --git a/src/win/graph/win_font.cpp b/src/win/graph/win_font.cpp
index 96983d3e..a359d73e 100644
--- a/src/win/graph/win_font.cpp
+++ b/src/win/graph/win_font.cpp
@@ -1,22 +1,23 @@
#include "cru/win/graph/win_font.hpp"
#include "cru/win/exception.hpp"
-#include "cru/win/graph/graph_manager.hpp"
+#include "cru/win/graph/win_native_factory.hpp"
#include <array>
#include <cassert>
#include <utility>
namespace cru::win::graph {
-WinFontDescriptor::WinFontDescriptor(GraphManager* graph_manager,
+WinFontDescriptor::WinFontDescriptor(IWinNativeFactory* factory,
const std::wstring_view& font_family,
float font_size) {
- assert(graph_manager);
+ assert(factory);
std::array<wchar_t, LOCALE_NAME_MAX_LENGTH> buffer;
- if (!::GetUserDefaultLocaleName(buffer.data(), static_cast<int>(buffer.size())))
+ if (!::GetUserDefaultLocaleName(buffer.data(),
+ static_cast<int>(buffer.size())))
throw Win32Error(::GetLastError(), "Failed to get locale.");
- ThrowIfFailed(graph_manager->GetDWriteFactory()->CreateTextFormat(
+ ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat(
font_family.data(), nullptr, DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, font_size,
buffer.data(), &text_format_));
diff --git a/src/win/graph/win_geometry.cpp b/src/win/graph/win_geometry.cpp
index 22c4b8a7..a725eff6 100644
--- a/src/win/graph/win_geometry.cpp
+++ b/src/win/graph/win_geometry.cpp
@@ -1,16 +1,15 @@
#include "cru/win/graph/win_geometry.hpp"
#include "cru/win/exception.hpp"
-#include "cru/win/graph/d2d_util.hpp"
-#include "cru/win/graph/graph_manager.hpp"
+#include "cru/win/graph/util/convert_util.hpp"
+#include "cru/win/graph/win_native_factory.hpp"
#include <cassert>
namespace cru::win::graph {
-WinGeometryBuilder::WinGeometryBuilder(GraphManager* graph_manager) {
- assert(graph_manager);
- ThrowIfFailed(
- graph_manager->GetD2D1Factory()->CreatePathGeometry(&geometry_));
+WinGeometryBuilder::WinGeometryBuilder(IWinNativeFactory* factory) {
+ assert(factory);
+ ThrowIfFailed(factory->GetD2D1Factory()->CreatePathGeometry(&geometry_));
ThrowIfFailed(geometry_->Open(&geometry_sink_));
}
@@ -21,33 +20,33 @@ WinGeometryBuilder::~WinGeometryBuilder() {
}
void WinGeometryBuilder::BeginFigure(const ui::Point& point) {
- assert(IsValid());
+ assert(IsEnded());
geometry_sink_->BeginFigure(util::Convert(point), D2D1_FIGURE_BEGIN_FILLED);
}
void WinGeometryBuilder::LineTo(const ui::Point& point) {
- assert(IsValid());
+ assert(IsEnded());
geometry_sink_->AddLine(util::Convert(point));
}
void WinGeometryBuilder::QuadraticBezierTo(const ui::Point& control_point,
const ui::Point& end_point) {
- assert(IsValid());
+ assert(IsEnded());
geometry_sink_->AddQuadraticBezier(D2D1::QuadraticBezierSegment(
util::Convert(control_point), util::Convert(end_point)));
}
void WinGeometryBuilder::CloseFigure(bool close) {
- assert(IsValid());
+ assert(IsEnded());
geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED
: D2D1_FIGURE_END_OPEN);
}
-platform::graph::Geometry* WinGeometryBuilder::Build() {
- assert(IsValid());
+platform::graph::IGeometry* WinGeometryBuilder::End() {
+ assert(IsEnded());
ThrowIfFailed(geometry_sink_->Close());
geometry_sink_ = nullptr;
- const auto geometry = new WinGeometry(geometry_);
+ const auto geometry = new WinGeometry(std::move(geometry_));
geometry_ = nullptr;
return geometry;
}
diff --git a/src/win/graph/win_graph_factory.cpp b/src/win/graph/win_graph_factory.cpp
index 5ab905b6..de1d28f9 100644
--- a/src/win/graph/win_graph_factory.cpp
+++ b/src/win/graph/win_graph_factory.cpp
@@ -1,46 +1,111 @@
#include "cru/win/graph/win_graph_factory.hpp"
#include "cru/win/exception.hpp"
-#include "cru/win/graph/d2d_util.hpp"
-#include "cru/win/graph/graph_manager.hpp"
#include "cru/win/graph/win_brush.hpp"
#include "cru/win/graph/win_font.hpp"
#include "cru/win/graph/win_geometry.hpp"
#include "cru/win/graph/win_text_layout.hpp"
#include <cassert>
+#include <cstdlib>
#include <utility>
+namespace cru::win::graph {
+namespace {
+WinGraphFactory* instance = nullptr;
+}
+} // namespace cru::win::graph
+
namespace cru::platform::graph {
-GraphFactory* GraphFactory::GetInstance() {
- return win::graph::GraphManager::GetInstance()->GetGraphFactory();
+void GraphFactoryAutoDeleteExitHandler() {
+ const auto i = ::cru::win::graph::instance; // avoid long namespace prefix
+ if (i == nullptr) return;
+ if (i->IsAutoDelete()) delete i;
}
+
+IGraphFactory* IGraphFactory::CreateInstance() {
+ auto& i = ::cru::win::graph::instance; // avoid long namespace prefix
+ assert(i == nullptr);
+ i = new cru::win::graph::WinGraphFactory();
+ std::atexit(&GraphFactoryAutoDeleteExitHandler);
+ return i;
}
+IGraphFactory* IGraphFactory::GetInstance() {
+ return ::cru::win::graph::instance;
+}
+} // namespace cru::platform::graph
+
namespace cru::win::graph {
-WinGraphFactory::WinGraphFactory(GraphManager* graph_manager) {
- assert(graph_manager);
- graph_manager_ = graph_manager;
+WinGraphFactory* WinGraphFactory::GetInstance() { return instance; }
+
+WinGraphFactory::WinGraphFactory() {
+ 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<ID3D11DeviceContext> 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<IDXGIDevice> dxgi_device;
+ ThrowIfFailed(d3d11_device_->QueryInterface(dxgi_device.GetAddressOf()));
+
+ ThrowIfFailed(D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
+ IID_PPV_ARGS(&d2d1_factory_)));
+
+ Microsoft::WRL::ComPtr<ID2D1Device> 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<IDXGIAdapter> 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<IUnknown**>(dwrite_factory_.GetAddressOf())));
+
+ ThrowIfFailed(dwrite_factory_->GetSystemFontCollection(
+ &dwrite_system_font_collection_));
}
-platform::graph::SolidColorBrush* WinGraphFactory::CreateSolidColorBrush(
+WinGraphFactory::~WinGraphFactory() { instance = nullptr; }
+
+platform::graph::ISolidColorBrush* WinGraphFactory::CreateSolidColorBrush(
const ui::Color& color) {
- return new WinSolidColorBrush(graph_manager_, color);
+ return new WinSolidColorBrush(this, color);
}
-platform::graph::GeometryBuilder* WinGraphFactory::CreateGeometryBuilder() {
- return new WinGeometryBuilder(graph_manager_);
+platform::graph::IGeometryBuilder* WinGraphFactory::CreateGeometryBuilder() {
+ return new WinGeometryBuilder(this);
}
-platform::graph::FontDescriptor* WinGraphFactory::CreateFontDescriptor(
+platform::graph::IFontDescriptor* WinGraphFactory::CreateFontDescriptor(
const std::wstring_view& font_family, float font_size) {
- return new WinFontDescriptor(graph_manager_, font_family, font_size);
+ return new WinFontDescriptor(this, font_family, font_size);
}
-platform::graph::TextLayout* WinGraphFactory::CreateTextLayout(
- std::shared_ptr<platform::graph::FontDescriptor> font, std::wstring text) {
+platform::graph::ITextLayout* WinGraphFactory::CreateTextLayout(
+ std::shared_ptr<platform::graph::IFontDescriptor> font, std::wstring text) {
const auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font);
assert(f);
- return new WinTextLayout(graph_manager_, std::move(f), std::move(text));
+ return new WinTextLayout(this, std::move(f), std::move(text));
}
} // namespace cru::win::graph
diff --git a/src/win/graph/win_painter.cpp b/src/win/graph/win_painter.cpp
new file mode 100644
index 00000000..f75cf4e0
--- /dev/null
+++ b/src/win/graph/win_painter.cpp
@@ -0,0 +1,93 @@
+#include "cru/win/graph/win_painter.hpp"
+
+#include "cru/win/exception.hpp"
+#include "cru/win/graph/win_native_factory.hpp"
+#include "cru/win/graph/util/convert_util.hpp"
+#include "cru/win/graph/win_brush.hpp"
+#include "cru/win/graph/win_geometry.hpp"
+#include "cru/win/graph/win_text_layout.hpp"
+
+#include <cassert>
+
+namespace cru::win::graph {
+WinPainter::WinPainter(ID2D1RenderTarget* render_target) {
+ assert(render_target);
+ render_target_ = render_target;
+}
+
+platform::Matrix WinPainter::GetTransform() {
+ assert(!IsEnded());
+ D2D1_MATRIX_3X2_F m;
+ render_target_->GetTransform(&m);
+ return util::Convert(m);
+}
+
+void WinPainter::SetTransform(const platform::Matrix& matrix) {
+ assert(!IsEnded());
+ render_target_->SetTransform(util::Convert(matrix));
+}
+
+void WinPainter::Clear(const ui::Color& color) {
+ assert(!IsEnded());
+ render_target_->Clear(util::Convert(color));
+}
+
+void WinPainter::StrokeRectangle(const ui::Rect& rectangle,
+ platform::graph::IBrush* brush, float width) {
+ assert(!IsEnded());
+ const auto b = dynamic_cast<IWinBrush*>(brush);
+ assert(b);
+ render_target_->DrawRectangle(util::Convert(rectangle), b->GetD2DBrush(),
+ width);
+}
+
+void WinPainter::FillRectangle(const ui::Rect& rectangle,
+ platform::graph::IBrush* brush) {
+ assert(!IsEnded());
+ const auto b = dynamic_cast<IWinBrush*>(brush);
+ assert(b);
+ render_target_->FillRectangle(util::Convert(rectangle), b->GetD2DBrush());
+}
+
+void WinPainter::StrokeGeometry(platform::graph::IGeometry* geometry,
+ platform::graph::IBrush* brush, float width) {
+ assert(!IsEnded());
+ const auto g = dynamic_cast<WinGeometry*>(geometry);
+ assert(g);
+ const auto b = dynamic_cast<IWinBrush*>(brush);
+ assert(b);
+
+ render_target_->DrawGeometry(g->GetNative(), b->GetD2DBrush(), width);
+}
+
+void WinPainter::FillGeometry(platform::graph::IGeometry* geometry,
+ platform::graph::IBrush* brush) {
+ assert(!IsEnded());
+ const auto g = dynamic_cast<WinGeometry*>(geometry);
+ assert(g);
+ const auto b = dynamic_cast<IWinBrush*>(brush);
+ assert(b);
+
+ render_target_->FillGeometry(g->GetNative(), b->GetD2DBrush());
+}
+
+void WinPainter::DrawText(const ui::Point& offset,
+ platform::graph::ITextLayout* text_layout,
+ platform::graph::IBrush* brush) {
+ assert(!IsEnded());
+ const auto t = dynamic_cast<WinTextLayout*>(text_layout);
+ assert(t);
+ const auto b = dynamic_cast<IWinBrush*>(brush);
+ assert(b);
+
+ render_target_->DrawTextLayout(util::Convert(offset),
+ t->GetDWriteTextLayout(), b->GetD2DBrush());
+}
+
+void WinPainter::End() {
+ if (!is_draw_ended_) {
+ is_draw_ended_ = true;
+ DoEndDraw();
+ }
+}
+} // namespace cru::win::graph
diff --git a/src/win/graph/win_text_layout.cpp b/src/win/graph/win_text_layout.cpp
index 0506320a..997309ad 100644
--- a/src/win/graph/win_text_layout.cpp
+++ b/src/win/graph/win_text_layout.cpp
@@ -1,23 +1,23 @@
#include "cru/win/graph/win_text_layout.hpp"
#include "cru/win/exception.hpp"
-#include "cru/win/graph/graph_manager.hpp"
#include "cru/win/graph/win_font.hpp"
+#include "cru/win/graph/win_native_factory.hpp"
#include <cassert>
#include <utility>
namespace cru::win::graph {
-WinTextLayout::WinTextLayout(GraphManager* graph_manager,
+WinTextLayout::WinTextLayout(IWinNativeFactory* factory,
std::shared_ptr<WinFontDescriptor> font,
std::wstring text) {
- assert(graph_manager);
+ assert(factory);
assert(font);
- graph_manager_ = graph_manager;
+ factory_ = factory;
text_.swap(text);
font_descriptor_.swap(font);
- ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout(
+ ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout(
text_.c_str(), static_cast<UINT32>(text_.size()),
font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_,
&text_layout_));
@@ -27,22 +27,22 @@ std::wstring WinTextLayout::GetText() { return text_; }
void WinTextLayout::SetText(std::wstring new_text) {
text_.swap(new_text);
- ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout(
+ ThrowIfFailed(factory_->GetDWriteFactory()->CreateTextLayout(
text_.c_str(), static_cast<UINT32>(text_.size()),
font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_,
&text_layout_));
}
-std::shared_ptr<platform::graph::FontDescriptor> WinTextLayout::GetFont() {
+std::shared_ptr<platform::graph::IFontDescriptor> WinTextLayout::GetFont() {
return font_descriptor_;
}
void WinTextLayout::SetFont(
- std::shared_ptr<platform::graph::FontDescriptor> font) {
+ std::shared_ptr<platform::graph::IFontDescriptor> font) {
auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font);
assert(f);
f.swap(font_descriptor_);
- ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout(
+ ThrowIfFailed(factory_->GetDWriteFactory()->CreateTextLayout(
text_.c_str(), static_cast<UINT32>(text_.size()),
font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_,
&text_layout_));
diff --git a/src/win/native/win_application.cpp b/src/win/native/win_application.cpp
index 6c39453f..00a16ff1 100644
--- a/src/win/native/win_application.cpp
+++ b/src/win/native/win_application.cpp
@@ -1,7 +1,7 @@
#include "cru/win/native/win_application.hpp"
#include "cru/win/exception.hpp"
-#include "cru/win/graph/graph_manager.hpp"
+#include "cru/win/graph/win_graph_factory.hpp"
#include "cru/win/native/god_window.hpp"
#include "cru/win/native/win_native_window.hpp"
#include "god_window_message.hpp"
@@ -38,13 +38,14 @@ WinApplication::WinApplication(HINSTANCE h_instance) : h_instance_(h_instance) {
if (!::IsWindows8OrGreater())
throw std::runtime_error("Must run on Windows 8 or later.");
+ graph::WinGraphFactory::CreateInstance();
+
god_window_ = std::make_shared<GodWindow>(this);
timer_manager_ = std::make_shared<TimerManager>(god_window_.get());
window_manager_ = std::make_shared<WindowManager>(this);
}
-WinApplication::~WinApplication() {
- instance = nullptr; }
+WinApplication::~WinApplication() { instance = nullptr; }
int WinApplication::Run() {
MSG msg;
@@ -55,7 +56,7 @@ int WinApplication::Run() {
for (const auto& handler : quit_handlers_) handler();
- delete graph::GraphManager::GetInstance();
+ delete graph::WinGraphFactory::GetInstance();
delete this;
return static_cast<int>(msg.wParam);
diff --git a/src/win/native/win_native_window.cpp b/src/win/native/win_native_window.cpp
index 1d3b15ba..a2f23a2c 100644
--- a/src/win/native/win_native_window.cpp
+++ b/src/win/native/win_native_window.cpp
@@ -1,7 +1,7 @@
#include "cru/win/native/win_native_window.hpp"
#include "cru/win/exception.hpp"
-#include "cru/win/graph/graph_manager.hpp"
+#include "cru/win/graph/win_graph_factory.hpp"
#include "cru/win/native/win_application.hpp"
#include "cru/win/native/window_class.hpp"
#include "cru/win/native/window_render_target.hpp"
@@ -37,7 +37,7 @@ WinNativeWindow::WinNativeWindow(WinApplication* application,
window_manager->RegisterWindow(hwnd_, this);
window_render_target_.reset(
- new WindowRenderTarget(graph::GraphManager::GetInstance(), hwnd_));
+ new WindowRenderTarget(graph::WinGraphFactory::GetInstance(), hwnd_));
}
WinNativeWindow::~WinNativeWindow() {
@@ -117,7 +117,7 @@ void WinNativeWindow::SetWindowRect(const ui::Rect& rect) {
}
}
-platform::graph::Painter* WinNativeWindow::BeginPaint() {
+platform::graph::IPainter* WinNativeWindow::BeginPaint() {
return new WindowPainter(this);
}
diff --git a/src/win/native/window_painter.cpp b/src/win/native/window_painter.cpp
index 4e98a7fd..463be128 100644
--- a/src/win/native/window_painter.cpp
+++ b/src/win/native/window_painter.cpp
@@ -1,29 +1,29 @@
#include "window_painter.hpp"
#include "cru/win/exception.hpp"
-#include "cru/win/graph/graph_manager.hpp"
+#include "cru/win/graph/win_native_factory.hpp"
#include "cru/win/native/window_render_target.hpp"
#include <cassert>
namespace cru::win::native {
WindowPainter::WindowPainter(WinNativeWindow* window)
- : D2DPainter(window->GetWindowRenderTarget()
- ->GetGraphManager()
+ : WinPainter(window->GetWindowRenderTarget()
+ ->GetWinNativeFactory()
->GetD2D1DeviceContext()),
window_(window) {
window->GetWindowRenderTarget()->SetAsTarget();
window->GetWindowRenderTarget()
- ->GetGraphManager()
+ ->GetWinNativeFactory()
->GetD2D1DeviceContext()
->BeginDraw();
}
-WindowPainter::~WindowPainter() { EndDraw(); }
+WindowPainter::~WindowPainter() { End(); }
void WindowPainter::DoEndDraw() {
ThrowIfFailed(window_->GetWindowRenderTarget()
- ->GetGraphManager()
+ ->GetWinNativeFactory()
->GetD2D1DeviceContext()
->EndDraw());
window_->GetWindowRenderTarget()->Present();
diff --git a/src/win/native/window_painter.hpp b/src/win/native/window_painter.hpp
index 78c0717c..78731ef3 100644
--- a/src/win/native/window_painter.hpp
+++ b/src/win/native/window_painter.hpp
@@ -1,9 +1,9 @@
#pragma once
-#include "cru/win/graph/d2d_painter.hpp"
+#include "cru/win/graph/win_painter.hpp"
#include "cru/win/native/win_native_window.hpp"
namespace cru::win::native {
-class WindowPainter : public graph::D2DPainter {
+class WindowPainter : public graph::WinPainter {
public:
explicit WindowPainter(WinNativeWindow* window);
WindowPainter(const WindowPainter& other) = delete;
diff --git a/src/win/native/window_render_target.cpp b/src/win/native/window_render_target.cpp
index c49a920e..606b51f8 100644
--- a/src/win/native/window_render_target.cpp
+++ b/src/win/native/window_render_target.cpp
@@ -1,18 +1,18 @@
#include "cru/win/native/window_render_target.hpp"
#include "cru/win/exception.hpp"
-#include "cru/win/graph/graph_manager.hpp"
+#include "cru/win/graph/win_native_factory.hpp"
#include "dpi_util.hpp"
#include <cassert>
namespace cru::win::native {
-WindowRenderTarget::WindowRenderTarget(graph::GraphManager* graph_manager,
+WindowRenderTarget::WindowRenderTarget(graph::IWinNativeFactory* factory,
HWND hwnd) {
- this->graph_manager_ = graph_manager;
+ this->factory_ = factory;
- const auto d3d11_device = graph_manager->GetD3D11Device();
- const auto dxgi_factory = graph_manager->GetDxgiFactory();
+ const auto d3d11_device = factory->GetD3D11Device();
+ const auto dxgi_factory = factory->GetDxgiFactory();
// Allocate a descriptor.
DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {0};
@@ -39,8 +39,8 @@ WindowRenderTarget::WindowRenderTarget(graph::GraphManager* graph_manager,
}
void WindowRenderTarget::ResizeBuffer(const int width, const int height) {
- const auto graph_manager = graph_manager_;
- const auto d2d1_device_context = graph_manager->GetD2D1DeviceContext();
+ const auto factory = factory_;
+ const auto d2d1_device_context = factory->GetD2D1DeviceContext();
Microsoft::WRL::ComPtr<ID2D1Image> old_target;
d2d1_device_context->GetTarget(&old_target);
@@ -59,7 +59,7 @@ void WindowRenderTarget::ResizeBuffer(const int width, const int height) {
}
void WindowRenderTarget::SetAsTarget() {
- graph_manager_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.Get());
+ factory_->GetD2D1DeviceContext()->SetTarget(target_bitmap_.Get());
}
void WindowRenderTarget::Present() {
@@ -83,8 +83,7 @@ void WindowRenderTarget::CreateTargetBitmap() {
// 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_));
+ ThrowIfFailed(factory_->GetD2D1DeviceContext()->CreateBitmapFromDxgiSurface(
+ dxgi_back_buffer.Get(), &bitmap_properties, &target_bitmap_));
}
} // namespace cru::win::native