aboutsummaryrefslogtreecommitdiff
path: root/src/win/graph
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-04-10 19:42:46 +0800
committercrupest <crupest@outlook.com>2019-04-10 19:42:46 +0800
commit7351020a582d70a1495249fba87d342c8a1fb634 (patch)
treee80f225041dc3816b3dce21c7e15aadbb211602e /src/win/graph
parenta94a806f69586e08a30fff0cdb3e52b0ce7acfa5 (diff)
downloadcru-7351020a582d70a1495249fba87d342c8a1fb634.tar.gz
cru-7351020a582d70a1495249fba87d342c8a1fb634.tar.bz2
cru-7351020a582d70a1495249fba87d342c8a1fb634.zip
Refactor.
Diffstat (limited to 'src/win/graph')
-rw-r--r--src/win/graph/CMakeLists.txt9
-rw-r--r--src/win/graph/graph_manager.cpp59
-rw-r--r--src/win/graph/win_brush.cpp23
-rw-r--r--src/win/graph/win_font.cpp28
-rw-r--r--src/win/graph/win_geometry.cpp66
-rw-r--r--src/win/graph/win_graph_factory.cpp46
-rw-r--r--src/win/graph/win_painter.cpp99
-rw-r--r--src/win/graph/win_text_layout.cpp94
8 files changed, 424 insertions, 0 deletions
diff --git a/src/win/graph/CMakeLists.txt b/src/win/graph/CMakeLists.txt
new file mode 100644
index 00000000..7781e09a
--- /dev/null
+++ b/src/win/graph/CMakeLists.txt
@@ -0,0 +1,9 @@
+add_library(cru_win_graph STATIC
+ 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/graph_manager.cpp b/src/win/graph/graph_manager.cpp
new file mode 100644
index 00000000..a4306b1b
--- /dev/null
+++ b/src/win/graph/graph_manager.cpp
@@ -0,0 +1,59 @@
+#include "cru/win/graph/graph_manager.hpp"
+
+#include "cru/win/exception.hpp"
+
+namespace cru::win::graph {
+GraphManager* GraphManager::GetInstance() {
+ static GraphManager instance;
+ 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
new file mode 100644
index 00000000..262c3302
--- /dev/null
+++ b/src/win/graph/win_brush.cpp
@@ -0,0 +1,23 @@
+#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 <cassert>
+
+namespace cru::win::graph {
+WinSolidColorBrush::WinSolidColorBrush(GraphManager* graph_manager,
+ const ui::Color& color) {
+ assert(graph_manager);
+ ThrowIfFailed(graph_manager->GetD2D1DeviceContext()->CreateSolidColorBrush(
+ util::Convert(color), &brush_));
+}
+
+ui::Color WinSolidColorBrush::GetColor() {
+ return util::Convert(brush_->GetColor());
+}
+void WinSolidColorBrush::SetColor(const ui::Color& color) {
+ brush_->SetColor(util::Convert(color));
+}
+} // namespace cru::win::graph
diff --git a/src/win/graph/win_font.cpp b/src/win/graph/win_font.cpp
new file mode 100644
index 00000000..0eb5e6b2
--- /dev/null
+++ b/src/win/graph/win_font.cpp
@@ -0,0 +1,28 @@
+#include "cru/win/graph/win_font.hpp"
+
+#include "cru/win/exception.hpp"
+#include "cru/win/graph/graph_manager.hpp"
+
+#include <array>
+#include <cassert>
+#include <utility>
+
+namespace cru::win::graph {
+WinFontDescriptor::WinFontDescriptor(GraphManager* graph_manager,
+ const std::wstring_view& font_family,
+ float font_size) {
+ assert(graph_manager);
+ std::array<wchar_t, LOCALE_NAME_MAX_LENGTH> buffer;
+ if (!::GetUserDefaultLocaleName(buffer.data(), buffer.size()))
+ throw Win32Error(::GetLastError(), "Failed to get locale.");
+
+ ThrowIfFailed(graph_manager->GetDWriteFactory()->CreateTextFormat(
+ font_family.data(), nullptr, DWRITE_FONT_WEIGHT_NORMAL,
+ DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, font_size,
+ buffer.data(), &text_format_));
+
+ ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER));
+ ThrowIfFailed(
+ text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER));
+}
+} // namespace cru::win::graph
diff --git a/src/win/graph/win_geometry.cpp b/src/win/graph/win_geometry.cpp
new file mode 100644
index 00000000..22c4b8a7
--- /dev/null
+++ b/src/win/graph/win_geometry.cpp
@@ -0,0 +1,66 @@
+#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 <cassert>
+
+namespace cru::win::graph {
+WinGeometryBuilder::WinGeometryBuilder(GraphManager* graph_manager) {
+ assert(graph_manager);
+ ThrowIfFailed(
+ graph_manager->GetD2D1Factory()->CreatePathGeometry(&geometry_));
+ ThrowIfFailed(geometry_->Open(&geometry_sink_));
+}
+
+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);
+}
+
+platform::graph::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<ID2D1PathGeometry> 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::win::graph
diff --git a/src/win/graph/win_graph_factory.cpp b/src/win/graph/win_graph_factory.cpp
new file mode 100644
index 00000000..5ab905b6
--- /dev/null
+++ b/src/win/graph/win_graph_factory.cpp
@@ -0,0 +1,46 @@
+#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 <utility>
+
+namespace cru::platform::graph {
+GraphFactory* GraphFactory::GetInstance() {
+ return win::graph::GraphManager::GetInstance()->GetGraphFactory();
+}
+}
+
+namespace cru::win::graph {
+WinGraphFactory::WinGraphFactory(GraphManager* graph_manager) {
+ assert(graph_manager);
+ graph_manager_ = graph_manager;
+}
+
+platform::graph::SolidColorBrush* WinGraphFactory::CreateSolidColorBrush(
+ const ui::Color& color) {
+ return new WinSolidColorBrush(graph_manager_, color);
+}
+
+platform::graph::GeometryBuilder* WinGraphFactory::CreateGeometryBuilder() {
+ return new WinGeometryBuilder(graph_manager_);
+}
+
+platform::graph::FontDescriptor* WinGraphFactory::CreateFontDescriptor(
+ const std::wstring_view& font_family, float font_size) {
+ return new WinFontDescriptor(graph_manager_, font_family, font_size);
+}
+
+platform::graph::TextLayout* WinGraphFactory::CreateTextLayout(
+ std::shared_ptr<platform::graph::FontDescriptor> 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));
+}
+} // 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..69ce1141
--- /dev/null
+++ b/src/win/graph/win_painter.cpp
@@ -0,0 +1,99 @@
+#include "cru/win/graph/win_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 {
+WinPainter::WinPainter(ID2D1RenderTarget* render_target) {
+ assert(render_target);
+ render_target_ = render_target;
+}
+
+WinPainter::~WinPainter() { EndDraw(); }
+
+platform::Matrix WinPainter::GetTransform() {
+ assert(!IsDisposed());
+ D2D1_MATRIX_3X2_F m;
+ render_target_->GetTransform(&m);
+ return util::Convert(m);
+}
+
+void WinPainter::SetTransform(const platform::Matrix& matrix) {
+ assert(!IsDisposed());
+ render_target_->SetTransform(util::Convert(matrix));
+}
+
+void WinPainter::Clear(const ui::Color& color) {
+ assert(!IsDisposed());
+ render_target_->Clear(util::Convert(color));
+}
+
+void WinPainter::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 WinPainter::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 WinPainter::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 WinPainter::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 WinPainter::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 WinPainter::EndDraw() {
+ if (!IsDisposed()) {
+ DoEndDraw();
+ }
+}
+
+void WinPainter::DoEndDraw() {
+ ThrowIfFailed(render_target_->EndDraw());
+ is_disposed_ = true;
+}
+} // namespace cru::win::graph
diff --git a/src/win/graph/win_text_layout.cpp b/src/win/graph/win_text_layout.cpp
new file mode 100644
index 00000000..0506320a
--- /dev/null
+++ b/src/win/graph/win_text_layout.cpp
@@ -0,0 +1,94 @@
+#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 <cassert>
+#include <utility>
+
+namespace cru::win::graph {
+WinTextLayout::WinTextLayout(GraphManager* graph_manager,
+ std::shared_ptr<WinFontDescriptor> font,
+ std::wstring text) {
+ assert(graph_manager);
+ assert(font);
+ graph_manager_ = graph_manager;
+ text_.swap(text);
+ font_descriptor_.swap(font);
+
+ ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout(
+ text_.c_str(), static_cast<UINT32>(text_.size()),
+ font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_,
+ &text_layout_));
+}
+
+std::wstring WinTextLayout::GetText() { return text_; }
+
+void WinTextLayout::SetText(std::wstring new_text) {
+ text_.swap(new_text);
+ ThrowIfFailed(graph_manager_->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() {
+ return font_descriptor_;
+}
+
+void WinTextLayout::SetFont(
+ std::shared_ptr<platform::graph::FontDescriptor> font) {
+ auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font);
+ assert(f);
+ f.swap(font_descriptor_);
+ ThrowIfFailed(graph_manager_->GetDWriteFactory()->CreateTextLayout(
+ text_.c_str(), static_cast<UINT32>(text_.size()),
+ font_descriptor_->GetDWriteTextFormat(), max_width_, max_height_,
+ &text_layout_));
+}
+
+void WinTextLayout::SetMaxWidth(float max_width) {
+ max_width_ = max_width;
+ ThrowIfFailed(text_layout_->SetMaxWidth(max_width_));
+}
+
+void WinTextLayout::SetMaxHeight(float max_height) {
+ max_height_ = max_height;
+ ThrowIfFailed(text_layout_->SetMaxHeight(max_height_));
+}
+
+ui::Rect WinTextLayout::GetTextBounds() {
+ DWRITE_TEXT_METRICS metrics;
+ ThrowIfFailed(text_layout_->GetMetrics(&metrics));
+ return ui::Rect{metrics.left, metrics.top, metrics.width, metrics.height};
+}
+
+std::vector<ui::Rect> WinTextLayout::TextRangeRect(
+ const ui::TextRange& text_range) {
+ DWRITE_TEXT_METRICS text_metrics;
+ ThrowIfFailed(text_layout_->GetMetrics(&text_metrics));
+ const auto metrics_count =
+ text_metrics.lineCount * text_metrics.maxBidiReorderingDepth;
+
+ std::vector<DWRITE_HIT_TEST_METRICS> hit_test_metrics(metrics_count);
+ UINT32 actual_count;
+ text_layout_->HitTestTextRange(text_range.position, text_range.count, 0, 0,
+ hit_test_metrics.data(), metrics_count,
+ &actual_count);
+
+ hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count,
+ hit_test_metrics.cend());
+
+ std::vector<ui::Rect> result;
+ result.reserve(actual_count);
+
+ for (const auto& metrics : hit_test_metrics) {
+ result.push_back(ui::Rect{metrics.left, metrics.top,
+ metrics.left + metrics.width,
+ metrics.top + metrics.height});
+ }
+
+ return result;
+}
+} // namespace cru::win::graph