aboutsummaryrefslogtreecommitdiff
path: root/src/win/graph/direct
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2019-06-27 00:18:48 +0800
committercrupest <crupest@outlook.com>2019-06-27 00:18:48 +0800
commitbaa7cf141b8121473edceae16c1a20a6d47bd202 (patch)
tree9349633a9d0bc286fe29f480bd70e4c2ad1f3075 /src/win/graph/direct
parentf404a3b2eb7bb9865d0c6f938538899996a53d8c (diff)
downloadcru-baa7cf141b8121473edceae16c1a20a6d47bd202.tar.gz
cru-baa7cf141b8121473edceae16c1a20a6d47bd202.tar.bz2
cru-baa7cf141b8121473edceae16c1a20a6d47bd202.zip
......
Diffstat (limited to 'src/win/graph/direct')
-rw-r--r--src/win/graph/direct/CMakeLists.txt24
-rw-r--r--src/win/graph/direct/brush.cpp19
-rw-r--r--src/win/graph/direct/exception.cpp29
-rw-r--r--src/win/graph/direct/font.cpp29
-rw-r--r--src/win/graph/direct/geometry.cpp65
-rw-r--r--src/win/graph/direct/graph_factory.cpp113
-rw-r--r--src/win/graph/direct/painter.cpp93
-rw-r--r--src/win/graph/direct/text_layout.cpp94
8 files changed, 466 insertions, 0 deletions
diff --git a/src/win/graph/direct/CMakeLists.txt b/src/win/graph/direct/CMakeLists.txt
new file mode 100644
index 00000000..7a04a778
--- /dev/null
+++ b/src/win/graph/direct/CMakeLists.txt
@@ -0,0 +1,24 @@
+set(CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/win/graph/direct)
+
+add_library(cru_win_graph_direct STATIC
+ brush.cpp
+ exception.cpp
+ font.cpp
+ geometry.cpp
+ graph_factory.cpp
+ painter.cpp
+ text_layout.cpp
+)
+target_sources(cru_win_graph_direct PUBLIC
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/brush.hpp
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/com_resource.hpp
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/convert_util.hpp
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/direct_factory.hpp
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/exception.hpp
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/font.hpp
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/geometry.hpp
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/graph_factory.hpp
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/painter.hpp
+ ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/text_layout.hpp
+)
+target_link_libraries(cru_win_graph_direct PUBLIC D3D11 D2d1 DWrite cru_win_base)
diff --git a/src/win/graph/direct/brush.cpp b/src/win/graph/direct/brush.cpp
new file mode 100644
index 00000000..7ddd14b4
--- /dev/null
+++ b/src/win/graph/direct/brush.cpp
@@ -0,0 +1,19 @@
+#include "cru/win/graph/direct/brush.hpp"
+
+#include "cru/win/graph/direct/convert_util.hpp"
+#include "cru/win/graph/direct/direct_factory.hpp"
+#include "cru/win/graph/direct/exception.hpp"
+
+#include <cassert>
+
+namespace cru::platform::graph::win::direct {
+D2DSolidColorBrush::D2DSolidColorBrush(IDirectFactory* factory) {
+ assert(factory);
+ ThrowIfFailed(factory->GetD2D1DeviceContext()->CreateSolidColorBrush(
+ Convert(color_), &brush_));
+}
+
+void D2DSolidColorBrush::OnSetColor(const Color& color) {
+ brush_->SetColor(Convert(color));
+}
+} // namespace cru::platform::graph::win::direct
diff --git a/src/win/graph/direct/exception.cpp b/src/win/graph/direct/exception.cpp
new file mode 100644
index 00000000..23c267c1
--- /dev/null
+++ b/src/win/graph/direct/exception.cpp
@@ -0,0 +1,29 @@
+#include "cru/win/graph/direct/exception.hpp"
+
+#include "cru/common/format.hpp"
+
+namespace cru::platform::graph::win::direct {
+using util::Format;
+
+inline std::string HResultMakeMessage(HRESULT h_result,
+ const std::string_view* message) {
+ char buffer[10];
+ sprintf_s(buffer, "%#08x", h_result);
+
+ if (message)
+ return Format(
+ "An HResultError is thrown. HRESULT: {}.\nAdditional message: {}\n",
+ buffer, *message);
+ else
+ return Format("An HResultError is thrown. HRESULT: {}.\n", buffer);
+}
+
+HResultError::HResultError(HRESULT h_result)
+ : PlatformException(HResultMakeMessage(h_result, nullptr)),
+ h_result_(h_result) {}
+
+HResultError::HResultError(HRESULT h_result,
+ const std::string_view& additional_message)
+ : PlatformException(HResultMakeMessage(h_result, &additional_message)),
+ h_result_(h_result) {}
+} // namespace cru::platform::graph::win::direct
diff --git a/src/win/graph/direct/font.cpp b/src/win/graph/direct/font.cpp
new file mode 100644
index 00000000..a359d73e
--- /dev/null
+++ b/src/win/graph/direct/font.cpp
@@ -0,0 +1,29 @@
+#include "cru/win/graph/win_font.hpp"
+
+#include "cru/win/exception.hpp"
+#include "cru/win/graph/win_native_factory.hpp"
+
+#include <array>
+#include <cassert>
+#include <utility>
+
+namespace cru::win::graph {
+WinFontDescriptor::WinFontDescriptor(IWinNativeFactory* factory,
+ const std::wstring_view& font_family,
+ float font_size) {
+ assert(factory);
+ std::array<wchar_t, LOCALE_NAME_MAX_LENGTH> buffer;
+ if (!::GetUserDefaultLocaleName(buffer.data(),
+ static_cast<int>(buffer.size())))
+ throw Win32Error(::GetLastError(), "Failed to get locale.");
+
+ 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_));
+
+ 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/direct/geometry.cpp b/src/win/graph/direct/geometry.cpp
new file mode 100644
index 00000000..a725eff6
--- /dev/null
+++ b/src/win/graph/direct/geometry.cpp
@@ -0,0 +1,65 @@
+#include "cru/win/graph/win_geometry.hpp"
+
+#include "cru/win/exception.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(IWinNativeFactory* factory) {
+ assert(factory);
+ ThrowIfFailed(factory->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(IsEnded());
+ geometry_sink_->BeginFigure(util::Convert(point), D2D1_FIGURE_BEGIN_FILLED);
+}
+
+void WinGeometryBuilder::LineTo(const ui::Point& point) {
+ assert(IsEnded());
+ geometry_sink_->AddLine(util::Convert(point));
+}
+
+void WinGeometryBuilder::QuadraticBezierTo(const ui::Point& control_point,
+ const ui::Point& end_point) {
+ assert(IsEnded());
+ geometry_sink_->AddQuadraticBezier(D2D1::QuadraticBezierSegment(
+ util::Convert(control_point), util::Convert(end_point)));
+}
+
+void WinGeometryBuilder::CloseFigure(bool close) {
+ assert(IsEnded());
+ geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED
+ : D2D1_FIGURE_END_OPEN);
+}
+
+platform::graph::IGeometry* WinGeometryBuilder::End() {
+ assert(IsEnded());
+ ThrowIfFailed(geometry_sink_->Close());
+ geometry_sink_ = nullptr;
+ const auto geometry = new WinGeometry(std::move(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/direct/graph_factory.cpp b/src/win/graph/direct/graph_factory.cpp
new file mode 100644
index 00000000..2f2bb7a5
--- /dev/null
+++ b/src/win/graph/direct/graph_factory.cpp
@@ -0,0 +1,113 @@
+#include "cru/win/graph/direct/graph_factory.hpp"
+
+#include "cru/win/graph/direct/brush.hpp"
+#include "cru/win/graph/direct/exception.hpp"
+#include "cru/win/graph/direct/font.hpp"
+#include "cru/win/graph/direct/geometry.hpp"
+#include "cru/win/graph/direct/text_layout.hpp"
+
+#include <cassert>
+#include <cstdlib>
+#include <utility>
+
+namespace cru::platform::graph::win::direct {
+namespace {
+DirectGraphFactory* instance = nullptr;
+}
+} // namespace cru::platform::graph::win::direct
+
+namespace cru::platform::graph {
+void GraphFactoryAutoDeleteExitHandler() {
+ const auto i =
+ ::cru::platform::graph::win::direct::instance; // avoid long namespace
+ // prefix
+ if (i == nullptr) return;
+ if (i->IsAutoDelete()) delete i;
+}
+
+GraphFactory* GraphFactory::CreateInstance() {
+ auto& i = ::cru::platform::graph::win::direct::instance; // avoid long
+ // namespace prefix
+ assert(i == nullptr);
+ i = new ::cru::platform::graph::win::direct::DirectGraphFactory();
+ std::atexit(&GraphFactoryAutoDeleteExitHandler);
+ return i;
+}
+
+GraphFactory* GraphFactory::GetInstance() {
+ return ::cru::platform::graph::win::direct::instance;
+}
+} // namespace cru::platform::graph
+
+namespace cru::platform::graph::win::direct {
+DirectGraphFactory* DirectGraphFactory::GetInstance() { return instance; }
+
+DirectGraphFactory::DirectGraphFactory() {
+ 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_));
+}
+
+DirectGraphFactory::~DirectGraphFactory() { instance = nullptr; }
+
+D2DSolidColorBrush* DirectGraphFactory::CreateSolidColorBrush() {
+ return new D2DSolidColorBrush(this);
+}
+
+platform::graph::IGeometryBuilder* WinGraphFactory::CreateGeometryBuilder() {
+ return new WinGeometryBuilder(this);
+}
+
+platform::graph::IFontDescriptor* WinGraphFactory::CreateFontDescriptor(
+ const std::wstring_view& font_family, float font_size) {
+ return new WinFontDescriptor(this, font_family, font_size);
+}
+
+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(this, std::move(f), std::move(text));
+}
+} // namespace cru::platform::graph::win::direct
diff --git a/src/win/graph/direct/painter.cpp b/src/win/graph/direct/painter.cpp
new file mode 100644
index 00000000..f75cf4e0
--- /dev/null
+++ b/src/win/graph/direct/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/direct/text_layout.cpp b/src/win/graph/direct/text_layout.cpp
new file mode 100644
index 00000000..997309ad
--- /dev/null
+++ b/src/win/graph/direct/text_layout.cpp
@@ -0,0 +1,94 @@
+#include "cru/win/graph/win_text_layout.hpp"
+
+#include "cru/win/exception.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(IWinNativeFactory* factory,
+ std::shared_ptr<WinFontDescriptor> font,
+ std::wstring text) {
+ assert(factory);
+ assert(font);
+ factory_ = factory;
+ text_.swap(text);
+ font_descriptor_.swap(font);
+
+ ThrowIfFailed(factory->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(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::IFontDescriptor> WinTextLayout::GetFont() {
+ return font_descriptor_;
+}
+
+void WinTextLayout::SetFont(
+ std::shared_ptr<platform::graph::IFontDescriptor> font) {
+ auto f = std::dynamic_pointer_cast<WinFontDescriptor>(font);
+ assert(f);
+ f.swap(font_descriptor_);
+ ThrowIfFailed(factory_->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