aboutsummaryrefslogtreecommitdiff
path: root/src/win/graph/direct
diff options
context:
space:
mode:
Diffstat (limited to 'src/win/graph/direct')
-rw-r--r--src/win/graph/direct/Brush.cpp17
-rw-r--r--src/win/graph/direct/CMakeLists.txt25
-rw-r--r--src/win/graph/direct/Factory.cpp107
-rw-r--r--src/win/graph/direct/Font.cpp31
-rw-r--r--src/win/graph/direct/Geometry.cpp62
-rw-r--r--src/win/graph/direct/Painter.cpp104
-rw-r--r--src/win/graph/direct/Resource.cpp12
-rw-r--r--src/win/graph/direct/TextLayout.cpp124
8 files changed, 0 insertions, 482 deletions
diff --git a/src/win/graph/direct/Brush.cpp b/src/win/graph/direct/Brush.cpp
deleted file mode 100644
index 2b9d4ac3..00000000
--- a/src/win/graph/direct/Brush.cpp
+++ /dev/null
@@ -1,17 +0,0 @@
-#include "cru/win/graph/direct/Brush.hpp"
-
-#include "cru/win/graph/direct/ConvertUtil.hpp"
-#include "cru/win/graph/direct/Exception.hpp"
-#include "cru/win/graph/direct/Factory.hpp"
-
-namespace cru::platform::graph::win::direct {
-D2DSolidColorBrush::D2DSolidColorBrush(DirectGraphFactory* factory)
- : DirectGraphResource(factory) {
- ThrowIfFailed(factory->GetDefaultD2D1DeviceContext()->CreateSolidColorBrush(
- Convert(color_), &brush_));
-}
-
-void D2DSolidColorBrush::SetColor(const Color& color) {
- brush_->SetColor(Convert(color));
-}
-} // namespace cru::platform::graph::win::direct
diff --git a/src/win/graph/direct/CMakeLists.txt b/src/win/graph/direct/CMakeLists.txt
deleted file mode 100644
index 5505b0b5..00000000
--- a/src/win/graph/direct/CMakeLists.txt
+++ /dev/null
@@ -1,25 +0,0 @@
-set(CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR ${CRU_INCLUDE_DIR}/cru/win/graph/direct)
-
-add_library(cru_win_graph_direct STATIC
- Brush.cpp
- Font.cpp
- Geometry.cpp
- Factory.cpp
- Painter.cpp
- Resource.cpp
- TextLayout.cpp
-)
-target_sources(cru_win_graph_direct PUBLIC
- ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Brush.hpp
- ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/ComResource.hpp
- ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/ConvertUtil.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}/Factory.hpp
- ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Painter.hpp
- ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/Resource.hpp
- ${CRU_WIN_GRAPH_DIRECT_INCLUDE_DIR}/TextLayout.hpp
-)
-target_link_libraries(cru_win_graph_direct PUBLIC D3D11 D2d1 DWrite)
-target_link_libraries(cru_win_graph_direct PUBLIC cru_win_base cru_platform_graph)
diff --git a/src/win/graph/direct/Factory.cpp b/src/win/graph/direct/Factory.cpp
deleted file mode 100644
index 03e64e13..00000000
--- a/src/win/graph/direct/Factory.cpp
+++ /dev/null
@@ -1,107 +0,0 @@
-#include "cru/win/graph/direct/Factory.hpp"
-
-#include "cru/common/Logger.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/TextLayout.hpp"
-
-#include <cstdlib>
-#include <utility>
-
-namespace cru::platform::graph::win::direct {
-namespace {
-void InitializeCom() {
- const auto hresult = ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
- if (FAILED(hresult)) {
- throw HResultError(hresult, "Failed to call CoInitializeEx.");
- }
- if (hresult == S_FALSE) {
- log::Debug(
- u"Try to call CoInitializeEx, but it seems COM is already "
- u"initialized.");
- }
-}
-
-void UninitializeCom() { ::CoUninitialize(); }
-} // namespace
-
-DirectGraphFactory::DirectGraphFactory() {
- // TODO! Detect repeated creation. Because I don't think we can create two d2d
- // and dwrite factory so we need to prevent the "probably dangerous" behavior.
-
- InitializeCom();
-
- 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_)));
-
- ThrowIfFailed(d2d1_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device_));
-
- d2d1_device_context_ = CreateD2D1DeviceContext();
-
- // 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() { UninitializeCom(); }
-
-Microsoft::WRL::ComPtr<ID2D1DeviceContext>
-DirectGraphFactory::CreateD2D1DeviceContext() {
- Microsoft::WRL::ComPtr<ID2D1DeviceContext> d2d1_device_context;
- ThrowIfFailed(d2d1_device_->CreateDeviceContext(
- D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context));
- return d2d1_device_context;
-}
-
-std::unique_ptr<ISolidColorBrush> DirectGraphFactory::CreateSolidColorBrush() {
- return std::make_unique<D2DSolidColorBrush>(this);
-}
-
-std::unique_ptr<IGeometryBuilder> DirectGraphFactory::CreateGeometryBuilder() {
- return std::make_unique<D2DGeometryBuilder>(this);
-}
-
-std::unique_ptr<IFont> DirectGraphFactory::CreateFont(
- std::u16string font_family, float font_size) {
- return std::make_unique<DWriteFont>(this, std::move(font_family), font_size);
-}
-
-std::unique_ptr<ITextLayout> DirectGraphFactory::CreateTextLayout(
- std::shared_ptr<IFont> font, std::u16string text) {
- return std::make_unique<DWriteTextLayout>(this, std::move(font),
- std::move(text));
-}
-} // namespace cru::platform::graph::win::direct
diff --git a/src/win/graph/direct/Font.cpp b/src/win/graph/direct/Font.cpp
deleted file mode 100644
index 34de5b71..00000000
--- a/src/win/graph/direct/Font.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-#include "cru/win/graph/direct/Font.hpp"
-
-#include "cru/win/graph/direct/Exception.hpp"
-#include "cru/win/graph/direct/Factory.hpp"
-
-#include <array>
-#include <utility>
-
-namespace cru::platform::graph::win::direct {
-DWriteFont::DWriteFont(DirectGraphFactory* factory, std::u16string font_family,
- float font_size)
- : DirectGraphResource(factory), font_family_(std::move(font_family)) {
- // Get locale
- std::array<wchar_t, LOCALE_NAME_MAX_LENGTH> buffer;
- if (!::GetUserDefaultLocaleName(buffer.data(),
- static_cast<int>(buffer.size())))
- throw platform::win::Win32Error(
- ::GetLastError(), "Failed to get locale when create dwrite font.");
-
- ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat(
- reinterpret_cast<const wchar_t*>(font_family_.c_str()), 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_LEADING));
- ThrowIfFailed(
- text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR));
-}
-
-float DWriteFont::GetFontSize() { return text_format_->GetFontSize(); }
-} // namespace cru::platform::graph::win::direct
diff --git a/src/win/graph/direct/Geometry.cpp b/src/win/graph/direct/Geometry.cpp
deleted file mode 100644
index e77b4749..00000000
--- a/src/win/graph/direct/Geometry.cpp
+++ /dev/null
@@ -1,62 +0,0 @@
-#include "cru/win/graph/direct/Geometry.hpp"
-
-#include "cru/win/graph/direct/ConvertUtil.hpp"
-#include "cru/win/graph/direct/Exception.hpp"
-#include "cru/win/graph/direct/Factory.hpp"
-
-namespace cru::platform::graph::win::direct {
-D2DGeometryBuilder::D2DGeometryBuilder(DirectGraphFactory* factory)
- : DirectGraphResource(factory) {
- ThrowIfFailed(factory->GetD2D1Factory()->CreatePathGeometry(&geometry_));
- ThrowIfFailed(geometry_->Open(&geometry_sink_));
-}
-
-void D2DGeometryBuilder::CheckValidation() {
- if (!IsValid())
- throw ReuseException("The geometry builder is already disposed.");
-}
-
-void D2DGeometryBuilder::BeginFigure(const Point& point) {
- CheckValidation();
- geometry_sink_->BeginFigure(Convert(point), D2D1_FIGURE_BEGIN_FILLED);
-}
-
-void D2DGeometryBuilder::LineTo(const Point& point) {
- CheckValidation();
- geometry_sink_->AddLine(Convert(point));
-}
-
-void D2DGeometryBuilder::QuadraticBezierTo(const Point& control_point,
- const Point& end_point) {
- CheckValidation();
- geometry_sink_->AddQuadraticBezier(
- D2D1::QuadraticBezierSegment(Convert(control_point), Convert(end_point)));
-}
-
-void D2DGeometryBuilder::CloseFigure(bool close) {
- CheckValidation();
- geometry_sink_->EndFigure(close ? D2D1_FIGURE_END_CLOSED
- : D2D1_FIGURE_END_OPEN);
-}
-
-std::unique_ptr<IGeometry> D2DGeometryBuilder::Build() {
- CheckValidation();
- ThrowIfFailed(geometry_sink_->Close());
- geometry_sink_ = nullptr;
- auto geometry =
- std::make_unique<D2DGeometry>(GetDirectFactory(), std::move(geometry_));
- geometry_ = nullptr;
- return geometry;
-}
-
-D2DGeometry::D2DGeometry(DirectGraphFactory* factory,
- Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry)
- : DirectGraphResource(factory), geometry_(std::move(geometry)) {}
-
-bool D2DGeometry::FillContains(const Point& point) {
- BOOL result;
- ThrowIfFailed(geometry_->FillContainsPoint(
- Convert(point), D2D1::Matrix3x2F::Identity(), &result));
- return result != 0;
-}
-} // namespace cru::platform::graph::win::direct
diff --git a/src/win/graph/direct/Painter.cpp b/src/win/graph/direct/Painter.cpp
deleted file mode 100644
index 3ffb5208..00000000
--- a/src/win/graph/direct/Painter.cpp
+++ /dev/null
@@ -1,104 +0,0 @@
-#include "cru/win/graph/direct/Painter.hpp"
-
-#include "cru/platform/Check.hpp"
-#include "cru/win/graph/direct/Brush.hpp"
-#include "cru/win/graph/direct/ConvertUtil.hpp"
-#include "cru/win/graph/direct/Exception.hpp"
-#include "cru/win/graph/direct/Geometry.hpp"
-#include "cru/win/graph/direct/TextLayout.hpp"
-
-#include <type_traits>
-
-namespace cru::platform::graph::win::direct {
-D2DPainter::D2DPainter(ID2D1RenderTarget* render_target) {
- Expects(render_target);
- render_target_ = render_target;
-}
-
-platform::Matrix D2DPainter::GetTransform() {
- CheckValidation();
- D2D1_MATRIX_3X2_F m;
- render_target_->GetTransform(&m);
- return Convert(m);
-}
-
-void D2DPainter::SetTransform(const platform::Matrix& matrix) {
- CheckValidation();
- render_target_->SetTransform(Convert(matrix));
-}
-
-void D2DPainter::Clear(const Color& color) {
- CheckValidation();
- render_target_->Clear(Convert(color));
-}
-
-void D2DPainter::StrokeRectangle(const Rect& rectangle, IBrush* brush,
- float width) {
- CheckValidation();
- const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
- render_target_->DrawRectangle(Convert(rectangle), b->GetD2DBrushInterface(),
- width);
-}
-
-void D2DPainter::FillRectangle(const Rect& rectangle, IBrush* brush) {
- CheckValidation();
- const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
- render_target_->FillRectangle(Convert(rectangle), b->GetD2DBrushInterface());
-}
-
-void D2DPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush,
- float width) {
- CheckValidation();
- const auto g = CheckPlatform<D2DGeometry>(geometry, GetPlatformId());
- const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
- render_target_->DrawGeometry(g->GetComInterface(), b->GetD2DBrushInterface(),
- width);
-}
-
-void D2DPainter::FillGeometry(IGeometry* geometry, IBrush* brush) {
- CheckValidation();
- const auto g = CheckPlatform<D2DGeometry>(geometry, GetPlatformId());
- const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
- render_target_->FillGeometry(g->GetComInterface(), b->GetD2DBrushInterface());
-}
-
-void D2DPainter::DrawText(const Point& offset, ITextLayout* text_layout,
- IBrush* brush) {
- CheckValidation();
- const auto t = CheckPlatform<DWriteTextLayout>(text_layout, GetPlatformId());
- const auto b = CheckPlatform<ID2DBrush>(brush, GetPlatformId());
- render_target_->DrawTextLayout(Convert(offset), t->GetComInterface(),
- b->GetD2DBrushInterface());
-}
-
-void D2DPainter::PushLayer(const Rect& bounds) {
- CheckValidation();
-
- Microsoft::WRL::ComPtr<ID2D1Layer> layer;
- ThrowIfFailed(render_target_->CreateLayer(&layer));
-
- render_target_->PushLayer(D2D1::LayerParameters(Convert(bounds)),
- layer.Get());
-
- layers_.push_back(std::move(layer));
-}
-
-void D2DPainter::PopLayer() {
- render_target_->PopLayer();
- layers_.pop_back();
-}
-
-void D2DPainter::EndDraw() {
- if (is_drawing_) {
- is_drawing_ = false;
- DoEndDraw();
- }
-}
-
-void D2DPainter::CheckValidation() {
- if (!is_drawing_) {
- throw cru::platform::ReuseException(
- "Can't do that on painter after end drawing.");
- }
-}
-} // namespace cru::platform::graph::win::direct
diff --git a/src/win/graph/direct/Resource.cpp b/src/win/graph/direct/Resource.cpp
deleted file mode 100644
index 772bb74b..00000000
--- a/src/win/graph/direct/Resource.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#include "cru/win/graph/direct/Resource.hpp"
-
-#include "cru/win/graph/direct/Factory.hpp"
-
-namespace cru::platform::graph::win::direct {
-DirectGraphResource::DirectGraphResource(DirectGraphFactory* factory)
- : factory_(factory) {
- Expects(factory);
-}
-
-IGraphFactory* DirectGraphResource::GetGraphFactory() { return factory_; }
-} // namespace cru::platform::graph::win::direct
diff --git a/src/win/graph/direct/TextLayout.cpp b/src/win/graph/direct/TextLayout.cpp
deleted file mode 100644
index 0d4a6392..00000000
--- a/src/win/graph/direct/TextLayout.cpp
+++ /dev/null
@@ -1,124 +0,0 @@
-#include "cru/win/graph/direct/TextLayout.hpp"
-
-#include "cru/common/Logger.hpp"
-#include "cru/platform/Check.hpp"
-#include "cru/win/graph/direct/Exception.hpp"
-#include "cru/win/graph/direct/Factory.hpp"
-#include "cru/win/graph/direct/Font.hpp"
-
-#include <utility>
-
-namespace cru::platform::graph::win::direct {
-DWriteTextLayout::DWriteTextLayout(DirectGraphFactory* factory,
- std::shared_ptr<IFont> font,
- std::u16string text)
- : DirectGraphResource(factory), text_(std::move(text)) {
- Expects(font);
- font_ = CheckPlatform<DWriteFont>(font, GetPlatformId());
-
- ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout(
- reinterpret_cast<const wchar_t*>(text_.c_str()),
- static_cast<UINT32>(text_.size()), font_->GetComInterface(), max_width_,
- max_height_, &text_layout_));
-}
-
-DWriteTextLayout::~DWriteTextLayout() = default;
-
-std::u16string DWriteTextLayout::GetText() { return text_; }
-
-std::u16string_view DWriteTextLayout::GetTextView() { return text_; }
-
-void DWriteTextLayout::SetText(std::u16string new_text) {
- text_.swap(new_text);
- ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout(
- reinterpret_cast<const wchar_t*>(text_.c_str()),
- static_cast<UINT32>(text_.size()), font_->GetComInterface(), max_width_,
- max_height_, &text_layout_));
-}
-
-std::shared_ptr<IFont> DWriteTextLayout::GetFont() {
- return std::dynamic_pointer_cast<IFont>(font_);
-}
-
-void DWriteTextLayout::SetFont(std::shared_ptr<IFont> font) {
- font_ = CheckPlatform<DWriteFont>(font, GetPlatformId());
- ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout(
- reinterpret_cast<const wchar_t*>(text_.c_str()),
- static_cast<UINT32>(text_.size()), font_->GetComInterface(), max_width_,
- max_height_, &text_layout_));
-}
-
-void DWriteTextLayout::SetMaxWidth(float max_width) {
- max_width_ = max_width;
- ThrowIfFailed(text_layout_->SetMaxWidth(max_width_));
-}
-
-void DWriteTextLayout::SetMaxHeight(float max_height) {
- max_height_ = max_height;
- ThrowIfFailed(text_layout_->SetMaxHeight(max_height_));
-}
-
-Rect DWriteTextLayout::GetTextBounds() {
- DWRITE_TEXT_METRICS metrics;
- ThrowIfFailed(text_layout_->GetMetrics(&metrics));
- return Rect{metrics.left, metrics.top, metrics.width, metrics.height};
-}
-
-std::vector<Rect> DWriteTextLayout::TextRangeRect(
- const TextRange& text_range_arg) {
- if (text_range_arg.count == 0) {
- return {};
- }
- const auto text_range = text_range_arg.Normalize();
-
- 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;
- ThrowIfFailed(text_layout_->HitTestTextRange(
- static_cast<UINT32>(text_range.position),
- static_cast<UINT32>(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<Rect> result;
- result.reserve(actual_count);
-
- for (const auto& metrics : hit_test_metrics) {
- result.push_back(
- Rect{metrics.left, metrics.top, metrics.width, metrics.height});
- }
-
- return result;
-}
-
-Point DWriteTextLayout::TextSinglePoint(Index position, bool trailing) {
- DWRITE_HIT_TEST_METRICS metrics;
- FLOAT left;
- FLOAT top;
- ThrowIfFailed(text_layout_->HitTestTextPosition(static_cast<UINT32>(position),
- static_cast<BOOL>(trailing),
- &left, &top, &metrics));
- return Point{left, top};
-}
-
-TextHitTestResult DWriteTextLayout::HitTest(const Point& point) {
- BOOL trailing;
- BOOL inside;
- DWRITE_HIT_TEST_METRICS metrics;
-
- ThrowIfFailed(text_layout_->HitTestPoint(point.x, point.y, &trailing, &inside,
- &metrics));
-
- TextHitTestResult result;
- result.position = metrics.textPosition;
- result.trailing = trailing != 0;
- result.insideText = inside != 0;
- return result;
-}
-} // namespace cru::platform::graph::win::direct