From 6ad6638adf64d958cdae44ce1df6a8a3787fed84 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 16 Dec 2019 00:09:52 +0800 Subject: ... --- src/win/graph/direct/brush.cpp | 2 +- src/win/graph/direct/factory.cpp | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) (limited to 'src/win/graph') diff --git a/src/win/graph/direct/brush.cpp b/src/win/graph/direct/brush.cpp index 17024a66..b30d8c24 100644 --- a/src/win/graph/direct/brush.cpp +++ b/src/win/graph/direct/brush.cpp @@ -9,7 +9,7 @@ namespace cru::platform::graph::win::direct { D2DSolidColorBrush::D2DSolidColorBrush(DirectGraphFactory* factory) : DirectGraphResource(factory) { - ThrowIfFailed(factory->GetD2D1DeviceContext()->CreateSolidColorBrush( + ThrowIfFailed(factory->GetDefaultD2D1DeviceContext()->CreateSolidColorBrush( Convert(color_), &brush_)); } diff --git a/src/win/graph/direct/factory.cpp b/src/win/graph/direct/factory.cpp index 1fff9fa9..e67bdafe 100644 --- a/src/win/graph/direct/factory.cpp +++ b/src/win/graph/direct/factory.cpp @@ -1,5 +1,6 @@ #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" @@ -11,10 +12,28 @@ #include 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( + "Try to call CoInitializeEx, but it seems COM is already " + "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 @@ -39,12 +58,9 @@ DirectGraphFactory::DirectGraphFactory() { 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_factory_->CreateDevice(dxgi_device.Get(), &d2d1_device)); - - ThrowIfFailed(d2d1_device->CreateDeviceContext( - D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context_)); + d2d1_device_context_ = CreateD2D1DeviceContext(); // Identify the physical adapter (GPU or card) this device is runs on. Microsoft::WRL::ComPtr dxgi_adapter; @@ -61,6 +77,16 @@ DirectGraphFactory::DirectGraphFactory() { &dwrite_system_font_collection_)); } +DirectGraphFactory::~DirectGraphFactory() { UninitializeCom(); } + +Microsoft::WRL::ComPtr +DirectGraphFactory::CreateD2D1DeviceContext() { + Microsoft::WRL::ComPtr d2d1_device_context; + ThrowIfFailed(d2d1_device_->CreateDeviceContext( + D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2d1_device_context)); + return d2d1_device_context; +} + std::unique_ptr DirectGraphFactory::CreateSolidColorBrush() { return std::make_unique(this); } -- cgit v1.2.3