2026-04-06 20:02:34 +08:00
|
|
|
#include "NativeRenderer.h"
|
2026-04-05 20:46:24 +08:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
|
2026-04-06 20:02:34 +08:00
|
|
|
namespace XCEngine::UI::Editor::Host {
|
2026-04-05 20:46:24 +08:00
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
constexpr float kBaseDpi = 96.0f;
|
|
|
|
|
constexpr float kDefaultFontSize = 16.0f;
|
2026-04-05 20:46:24 +08:00
|
|
|
|
|
|
|
|
std::string HrToString(const char* operation, HRESULT hr) {
|
|
|
|
|
char buffer[128] = {};
|
|
|
|
|
sprintf_s(buffer, "%s failed with hr=0x%08X.", operation, static_cast<unsigned int>(hr));
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
float ClampDpiScale(float dpiScale) {
|
|
|
|
|
return dpiScale > 0.0f ? dpiScale : 1.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float ResolveFontSize(float fontSize) {
|
|
|
|
|
return fontSize > 0.0f ? fontSize : kDefaultFontSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float SnapToPixel(float value, float dpiScale) {
|
|
|
|
|
const float scale = ClampDpiScale(dpiScale);
|
|
|
|
|
return std::round(value * scale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
D2D1_RECT_F ToD2DRect(const ::XCEngine::UI::UIRect& rect, float dpiScale) {
|
|
|
|
|
const float left = SnapToPixel(rect.x, dpiScale);
|
|
|
|
|
const float top = SnapToPixel(rect.y, dpiScale);
|
|
|
|
|
const float right = SnapToPixel(rect.x + rect.width, dpiScale);
|
|
|
|
|
const float bottom = SnapToPixel(rect.y + rect.height, dpiScale);
|
|
|
|
|
return D2D1::RectF(left, top, right, bottom);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 20:20:30 +08:00
|
|
|
D2D1_POINT_2F ToD2DPoint(
|
|
|
|
|
const ::XCEngine::UI::UIPoint& point,
|
|
|
|
|
float dpiScale,
|
|
|
|
|
float pixelOffset = 0.0f) {
|
|
|
|
|
return D2D1::Point2F(
|
|
|
|
|
SnapToPixel(point.x, dpiScale) + pixelOffset,
|
|
|
|
|
SnapToPixel(point.y, dpiScale) + pixelOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float ResolveStrokePixelOffset(float thickness) {
|
|
|
|
|
const float roundedThickness = std::round(thickness);
|
|
|
|
|
return std::fmod(roundedThickness, 2.0f) == 1.0f ? 0.5f : 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:46:24 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
bool NativeRenderer::Initialize(HWND hwnd) {
|
|
|
|
|
Shutdown();
|
|
|
|
|
|
|
|
|
|
if (hwnd == nullptr) {
|
2026-04-07 03:51:26 +08:00
|
|
|
m_lastRenderError = "Initialize rejected a null hwnd.";
|
2026-04-05 20:46:24 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_hwnd = hwnd;
|
2026-04-07 03:51:26 +08:00
|
|
|
HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, m_d2dFactory.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
m_lastRenderError = HrToString("D2D1CreateFactory", hr);
|
2026-04-05 20:46:24 +08:00
|
|
|
Shutdown();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 03:51:26 +08:00
|
|
|
hr = DWriteCreateFactory(
|
2026-04-05 20:46:24 +08:00
|
|
|
DWRITE_FACTORY_TYPE_SHARED,
|
|
|
|
|
__uuidof(IDWriteFactory),
|
2026-04-07 03:51:26 +08:00
|
|
|
reinterpret_cast<IUnknown**>(m_dwriteFactory.ReleaseAndGetAddressOf()));
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
m_lastRenderError = HrToString("DWriteCreateFactory", hr);
|
2026-04-05 20:46:24 +08:00
|
|
|
Shutdown();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 03:51:26 +08:00
|
|
|
m_lastRenderError.clear();
|
2026-04-05 20:46:24 +08:00
|
|
|
return EnsureRenderTarget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NativeRenderer::Shutdown() {
|
|
|
|
|
m_textFormats.clear();
|
|
|
|
|
m_solidBrush.Reset();
|
|
|
|
|
m_renderTarget.Reset();
|
|
|
|
|
m_wicFactory.Reset();
|
|
|
|
|
m_dwriteFactory.Reset();
|
|
|
|
|
m_d2dFactory.Reset();
|
|
|
|
|
if (m_wicComInitialized) {
|
|
|
|
|
CoUninitialize();
|
|
|
|
|
m_wicComInitialized = false;
|
|
|
|
|
}
|
|
|
|
|
m_hwnd = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
void NativeRenderer::SetDpiScale(float dpiScale) {
|
|
|
|
|
m_dpiScale = ClampDpiScale(dpiScale);
|
|
|
|
|
if (m_renderTarget) {
|
|
|
|
|
m_renderTarget->SetDpi(kBaseDpi, kBaseDpi);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float NativeRenderer::GetDpiScale() const {
|
|
|
|
|
return m_dpiScale;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:46:24 +08:00
|
|
|
void NativeRenderer::Resize(UINT width, UINT height) {
|
|
|
|
|
if (!m_renderTarget || width == 0 || height == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const HRESULT hr = m_renderTarget->Resize(D2D1::SizeU(width, height));
|
|
|
|
|
if (hr == D2DERR_RECREATE_TARGET) {
|
|
|
|
|
DiscardRenderTarget();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NativeRenderer::Render(const ::XCEngine::UI::UIDrawData& drawData) {
|
|
|
|
|
if (!EnsureRenderTarget()) {
|
2026-04-07 03:51:26 +08:00
|
|
|
if (m_lastRenderError.empty()) {
|
|
|
|
|
m_lastRenderError = "EnsureRenderTarget failed.";
|
|
|
|
|
}
|
2026-04-05 20:46:24 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool rendered = RenderToTarget(*m_renderTarget.Get(), *m_solidBrush.Get(), drawData);
|
|
|
|
|
const HRESULT hr = m_renderTarget->EndDraw();
|
|
|
|
|
if (hr == D2DERR_RECREATE_TARGET) {
|
2026-04-07 03:51:26 +08:00
|
|
|
m_lastRenderError = HrToString("ID2D1HwndRenderTarget::EndDraw", hr);
|
2026-04-05 20:46:24 +08:00
|
|
|
DiscardRenderTarget();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 03:51:26 +08:00
|
|
|
if (!rendered || FAILED(hr)) {
|
|
|
|
|
m_lastRenderError = HrToString("ID2D1HwndRenderTarget::EndDraw", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_lastRenderError.clear();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::string& NativeRenderer::GetLastRenderError() const {
|
|
|
|
|
return m_lastRenderError;
|
2026-04-05 20:46:24 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
float NativeRenderer::MeasureTextWidth(
|
|
|
|
|
const ::XCEngine::UI::Editor::UIEditorTextMeasureRequest& request) const {
|
|
|
|
|
if (!m_dwriteFactory || request.text.empty()) {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::wstring text = Utf8ToWide(request.text);
|
|
|
|
|
if (text.empty()) {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const float dpiScale = ClampDpiScale(m_dpiScale);
|
|
|
|
|
const float scaledFontSize = ResolveFontSize(request.fontSize) * dpiScale;
|
|
|
|
|
IDWriteTextFormat* textFormat = GetTextFormat(scaledFontSize);
|
|
|
|
|
if (textFormat == nullptr) {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<IDWriteTextLayout> textLayout;
|
|
|
|
|
HRESULT hr = m_dwriteFactory->CreateTextLayout(
|
|
|
|
|
text.c_str(),
|
|
|
|
|
static_cast<UINT32>(text.size()),
|
|
|
|
|
textFormat,
|
|
|
|
|
4096.0f,
|
|
|
|
|
scaledFontSize * 2.0f,
|
|
|
|
|
textLayout.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr) || !textLayout) {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DWRITE_TEXT_METRICS textMetrics = {};
|
|
|
|
|
hr = textLayout->GetMetrics(&textMetrics);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DWRITE_OVERHANG_METRICS overhangMetrics = {};
|
|
|
|
|
float width = textMetrics.widthIncludingTrailingWhitespace;
|
|
|
|
|
if (SUCCEEDED(textLayout->GetOverhangMetrics(&overhangMetrics))) {
|
|
|
|
|
width += (std::max)(overhangMetrics.left, 0.0f);
|
|
|
|
|
width += (std::max)(overhangMetrics.right, 0.0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::ceil(width) / dpiScale;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:46:24 +08:00
|
|
|
bool NativeRenderer::CaptureToPng(
|
|
|
|
|
const ::XCEngine::UI::UIDrawData& drawData,
|
|
|
|
|
UINT width,
|
|
|
|
|
UINT height,
|
|
|
|
|
const std::filesystem::path& outputPath,
|
|
|
|
|
std::string& outError) {
|
|
|
|
|
outError.clear();
|
|
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
|
outError = "CaptureToPng rejected an empty render size.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_d2dFactory || !m_dwriteFactory) {
|
|
|
|
|
outError = "CaptureToPng requires an initialized NativeRenderer.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!EnsureWicFactory(outError)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::error_code errorCode = {};
|
|
|
|
|
std::filesystem::create_directories(outputPath.parent_path(), errorCode);
|
|
|
|
|
if (errorCode) {
|
|
|
|
|
outError = "Failed to create screenshot directory: " + outputPath.parent_path().string();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<IWICBitmap> bitmap;
|
|
|
|
|
HRESULT hr = m_wicFactory->CreateBitmap(
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
GUID_WICPixelFormat32bppPBGRA,
|
|
|
|
|
WICBitmapCacheOnLoad,
|
|
|
|
|
bitmap.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICImagingFactory::CreateBitmap", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties(
|
|
|
|
|
D2D1_RENDER_TARGET_TYPE_DEFAULT,
|
2026-04-11 17:07:37 +08:00
|
|
|
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
|
|
|
|
|
kBaseDpi,
|
|
|
|
|
kBaseDpi);
|
2026-04-05 20:46:24 +08:00
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<ID2D1RenderTarget> offscreenRenderTarget;
|
|
|
|
|
hr = m_d2dFactory->CreateWicBitmapRenderTarget(
|
|
|
|
|
bitmap.Get(),
|
|
|
|
|
renderTargetProperties,
|
|
|
|
|
offscreenRenderTarget.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("ID2D1Factory::CreateWicBitmapRenderTarget", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> offscreenBrush;
|
|
|
|
|
hr = offscreenRenderTarget->CreateSolidColorBrush(
|
|
|
|
|
D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f),
|
|
|
|
|
offscreenBrush.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("ID2D1RenderTarget::CreateSolidColorBrush", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const bool rendered = RenderToTarget(*offscreenRenderTarget.Get(), *offscreenBrush.Get(), drawData);
|
|
|
|
|
hr = offscreenRenderTarget->EndDraw();
|
|
|
|
|
if (!rendered || FAILED(hr)) {
|
|
|
|
|
outError = HrToString("ID2D1RenderTarget::EndDraw", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::wstring wideOutputPath = outputPath.wstring();
|
|
|
|
|
Microsoft::WRL::ComPtr<IWICStream> stream;
|
|
|
|
|
hr = m_wicFactory->CreateStream(stream.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICImagingFactory::CreateStream", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = stream->InitializeFromFilename(wideOutputPath.c_str(), GENERIC_WRITE);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICStream::InitializeFromFilename", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<IWICBitmapEncoder> encoder;
|
|
|
|
|
hr = m_wicFactory->CreateEncoder(GUID_ContainerFormatPng, nullptr, encoder.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICImagingFactory::CreateEncoder", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = encoder->Initialize(stream.Get(), WICBitmapEncoderNoCache);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICBitmapEncoder::Initialize", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<IWICBitmapFrameEncode> frame;
|
|
|
|
|
Microsoft::WRL::ComPtr<IPropertyBag2> propertyBag;
|
|
|
|
|
hr = encoder->CreateNewFrame(frame.ReleaseAndGetAddressOf(), propertyBag.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICBitmapEncoder::CreateNewFrame", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = frame->Initialize(propertyBag.Get());
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICBitmapFrameEncode::Initialize", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = frame->SetSize(width, height);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICBitmapFrameEncode::SetSize", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WICPixelFormatGUID pixelFormat = GUID_WICPixelFormat32bppPBGRA;
|
|
|
|
|
hr = frame->SetPixelFormat(&pixelFormat);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICBitmapFrameEncode::SetPixelFormat", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = frame->WriteSource(bitmap.Get(), nullptr);
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICBitmapFrameEncode::WriteSource", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = frame->Commit();
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICBitmapFrameEncode::Commit", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hr = encoder->Commit();
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
outError = HrToString("IWICBitmapEncoder::Commit", hr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NativeRenderer::EnsureRenderTarget() {
|
|
|
|
|
if (!m_hwnd || !m_d2dFactory || !m_dwriteFactory) {
|
2026-04-07 03:51:26 +08:00
|
|
|
m_lastRenderError = "EnsureRenderTarget requires hwnd, D2D factory, and DWrite factory.";
|
2026-04-05 20:46:24 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CreateDeviceResources();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NativeRenderer::EnsureWicFactory(std::string& outError) {
|
|
|
|
|
outError.clear();
|
|
|
|
|
if (m_wicFactory) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const HRESULT initHr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
|
|
|
|
if (FAILED(initHr) && initHr != RPC_E_CHANGED_MODE) {
|
|
|
|
|
outError = HrToString("CoInitializeEx", initHr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (SUCCEEDED(initHr)) {
|
|
|
|
|
m_wicComInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const HRESULT factoryHr = CoCreateInstance(
|
|
|
|
|
CLSID_WICImagingFactory,
|
|
|
|
|
nullptr,
|
|
|
|
|
CLSCTX_INPROC_SERVER,
|
|
|
|
|
IID_PPV_ARGS(m_wicFactory.ReleaseAndGetAddressOf()));
|
|
|
|
|
if (FAILED(factoryHr)) {
|
|
|
|
|
outError = HrToString("CoCreateInstance(CLSID_WICImagingFactory)", factoryHr);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NativeRenderer::DiscardRenderTarget() {
|
|
|
|
|
m_solidBrush.Reset();
|
|
|
|
|
m_renderTarget.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NativeRenderer::CreateDeviceResources() {
|
|
|
|
|
if (m_renderTarget) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RECT clientRect = {};
|
|
|
|
|
GetClientRect(m_hwnd, &clientRect);
|
|
|
|
|
const UINT width = static_cast<UINT>((std::max)(clientRect.right - clientRect.left, 1L));
|
|
|
|
|
const UINT height = static_cast<UINT>((std::max)(clientRect.bottom - clientRect.top, 1L));
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
const D2D1_RENDER_TARGET_PROPERTIES renderTargetProps = D2D1::RenderTargetProperties(
|
|
|
|
|
D2D1_RENDER_TARGET_TYPE_DEFAULT,
|
|
|
|
|
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
|
|
|
|
|
kBaseDpi,
|
|
|
|
|
kBaseDpi);
|
2026-04-05 20:46:24 +08:00
|
|
|
const D2D1_HWND_RENDER_TARGET_PROPERTIES hwndProps = D2D1::HwndRenderTargetProperties(
|
|
|
|
|
m_hwnd,
|
|
|
|
|
D2D1::SizeU(width, height));
|
|
|
|
|
|
2026-04-07 03:51:26 +08:00
|
|
|
const HRESULT renderTargetHr = m_d2dFactory->CreateHwndRenderTarget(
|
2026-04-05 20:46:24 +08:00
|
|
|
renderTargetProps,
|
|
|
|
|
hwndProps,
|
2026-04-07 03:51:26 +08:00
|
|
|
m_renderTarget.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(renderTargetHr)) {
|
|
|
|
|
m_lastRenderError = HrToString("ID2D1Factory::CreateHwndRenderTarget", renderTargetHr);
|
2026-04-05 20:46:24 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 03:51:26 +08:00
|
|
|
const HRESULT brushHr = m_renderTarget->CreateSolidColorBrush(
|
2026-04-05 20:46:24 +08:00
|
|
|
D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f),
|
2026-04-07 03:51:26 +08:00
|
|
|
m_solidBrush.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(brushHr)) {
|
|
|
|
|
m_lastRenderError = HrToString("ID2D1HwndRenderTarget::CreateSolidColorBrush", brushHr);
|
2026-04-05 20:46:24 +08:00
|
|
|
DiscardRenderTarget();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
m_renderTarget->SetDpi(kBaseDpi, kBaseDpi);
|
2026-04-05 20:46:24 +08:00
|
|
|
m_renderTarget->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE);
|
2026-04-07 03:51:26 +08:00
|
|
|
m_lastRenderError.clear();
|
2026-04-05 20:46:24 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NativeRenderer::RenderToTarget(
|
|
|
|
|
ID2D1RenderTarget& renderTarget,
|
|
|
|
|
ID2D1SolidColorBrush& solidBrush,
|
|
|
|
|
const ::XCEngine::UI::UIDrawData& drawData) {
|
2026-04-11 17:07:37 +08:00
|
|
|
renderTarget.SetDpi(kBaseDpi, kBaseDpi);
|
2026-04-05 20:46:24 +08:00
|
|
|
renderTarget.SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE);
|
|
|
|
|
renderTarget.BeginDraw();
|
|
|
|
|
renderTarget.Clear(D2D1::ColorF(0.04f, 0.05f, 0.06f, 1.0f));
|
|
|
|
|
|
|
|
|
|
std::vector<D2D1_RECT_F> clipStack = {};
|
|
|
|
|
for (const ::XCEngine::UI::UIDrawList& drawList : drawData.GetDrawLists()) {
|
|
|
|
|
for (const ::XCEngine::UI::UIDrawCommand& command : drawList.GetCommands()) {
|
|
|
|
|
RenderCommand(renderTarget, solidBrush, command, clipStack);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (!clipStack.empty()) {
|
|
|
|
|
renderTarget.PopAxisAlignedClip();
|
|
|
|
|
clipStack.pop_back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NativeRenderer::RenderCommand(
|
|
|
|
|
ID2D1RenderTarget& renderTarget,
|
|
|
|
|
ID2D1SolidColorBrush& solidBrush,
|
|
|
|
|
const ::XCEngine::UI::UIDrawCommand& command,
|
|
|
|
|
std::vector<D2D1_RECT_F>& clipStack) {
|
|
|
|
|
solidBrush.SetColor(ToD2DColor(command.color));
|
2026-04-11 17:07:37 +08:00
|
|
|
const float dpiScale = ClampDpiScale(m_dpiScale);
|
2026-04-05 20:46:24 +08:00
|
|
|
|
|
|
|
|
switch (command.type) {
|
|
|
|
|
case ::XCEngine::UI::UIDrawCommandType::FilledRect: {
|
2026-04-11 17:07:37 +08:00
|
|
|
const D2D1_RECT_F rect = ToD2DRect(command.rect, dpiScale);
|
|
|
|
|
const float rounding = command.rounding > 0.0f ? command.rounding * dpiScale : 0.0f;
|
2026-04-05 20:46:24 +08:00
|
|
|
if (command.rounding > 0.0f) {
|
|
|
|
|
renderTarget.FillRoundedRectangle(
|
2026-04-11 17:07:37 +08:00
|
|
|
D2D1::RoundedRect(rect, rounding, rounding),
|
2026-04-05 20:46:24 +08:00
|
|
|
&solidBrush);
|
|
|
|
|
} else {
|
|
|
|
|
renderTarget.FillRectangle(rect, &solidBrush);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-04-11 20:20:30 +08:00
|
|
|
case ::XCEngine::UI::UIDrawCommandType::FilledRectLinearGradient: {
|
|
|
|
|
const D2D1_RECT_F rect = ToD2DRect(command.rect, dpiScale);
|
|
|
|
|
const float rounding = command.rounding > 0.0f ? command.rounding * dpiScale : 0.0f;
|
|
|
|
|
|
|
|
|
|
const D2D1_GRADIENT_STOP stops[2] = {
|
|
|
|
|
D2D1::GradientStop(0.0f, ToD2DColor(command.color)),
|
|
|
|
|
D2D1::GradientStop(1.0f, ToD2DColor(command.secondaryColor))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<ID2D1GradientStopCollection> stopCollection;
|
|
|
|
|
HRESULT hr = renderTarget.CreateGradientStopCollection(
|
|
|
|
|
stops,
|
|
|
|
|
2u,
|
|
|
|
|
stopCollection.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr) || !stopCollection) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const D2D1_POINT_2F startPoint =
|
|
|
|
|
command.gradientDirection == ::XCEngine::UI::UILinearGradientDirection::Vertical
|
|
|
|
|
? D2D1::Point2F((rect.left + rect.right) * 0.5f, rect.top)
|
|
|
|
|
: D2D1::Point2F(rect.left, (rect.top + rect.bottom) * 0.5f);
|
|
|
|
|
const D2D1_POINT_2F endPoint =
|
|
|
|
|
command.gradientDirection == ::XCEngine::UI::UILinearGradientDirection::Vertical
|
|
|
|
|
? D2D1::Point2F((rect.left + rect.right) * 0.5f, rect.bottom)
|
|
|
|
|
: D2D1::Point2F(rect.right, (rect.top + rect.bottom) * 0.5f);
|
|
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<ID2D1LinearGradientBrush> gradientBrush;
|
|
|
|
|
hr = renderTarget.CreateLinearGradientBrush(
|
|
|
|
|
D2D1::LinearGradientBrushProperties(startPoint, endPoint),
|
|
|
|
|
stopCollection.Get(),
|
|
|
|
|
gradientBrush.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr) || !gradientBrush) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (command.rounding > 0.0f) {
|
|
|
|
|
renderTarget.FillRoundedRectangle(
|
|
|
|
|
D2D1::RoundedRect(rect, rounding, rounding),
|
|
|
|
|
gradientBrush.Get());
|
|
|
|
|
} else {
|
|
|
|
|
renderTarget.FillRectangle(rect, gradientBrush.Get());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-04-05 20:46:24 +08:00
|
|
|
case ::XCEngine::UI::UIDrawCommandType::RectOutline: {
|
2026-04-11 17:07:37 +08:00
|
|
|
const D2D1_RECT_F rect = ToD2DRect(command.rect, dpiScale);
|
|
|
|
|
const float thickness = (command.thickness > 0.0f ? command.thickness : 1.0f) * dpiScale;
|
|
|
|
|
const float rounding = command.rounding > 0.0f ? command.rounding * dpiScale : 0.0f;
|
2026-04-05 20:46:24 +08:00
|
|
|
if (command.rounding > 0.0f) {
|
|
|
|
|
renderTarget.DrawRoundedRectangle(
|
2026-04-11 17:07:37 +08:00
|
|
|
D2D1::RoundedRect(rect, rounding, rounding),
|
2026-04-05 20:46:24 +08:00
|
|
|
&solidBrush,
|
|
|
|
|
thickness);
|
|
|
|
|
} else {
|
|
|
|
|
renderTarget.DrawRectangle(rect, &solidBrush, thickness);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-04-11 20:20:30 +08:00
|
|
|
case ::XCEngine::UI::UIDrawCommandType::Line: {
|
|
|
|
|
const float thickness = (command.thickness > 0.0f ? command.thickness : 1.0f) * dpiScale;
|
|
|
|
|
const float pixelOffset = ResolveStrokePixelOffset(thickness);
|
|
|
|
|
const D2D1_POINT_2F start = ToD2DPoint(command.position, dpiScale, pixelOffset);
|
|
|
|
|
const D2D1_POINT_2F end = ToD2DPoint(command.uvMin, dpiScale, pixelOffset);
|
|
|
|
|
renderTarget.DrawLine(start, end, &solidBrush, thickness);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ::XCEngine::UI::UIDrawCommandType::FilledTriangle: {
|
|
|
|
|
Microsoft::WRL::ComPtr<ID2D1PathGeometry> geometry;
|
|
|
|
|
HRESULT hr = m_d2dFactory->CreatePathGeometry(geometry.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr) || !geometry) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<ID2D1GeometrySink> sink;
|
|
|
|
|
hr = geometry->Open(sink.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr) || !sink) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const D2D1_POINT_2F a = ToD2DPoint(command.position, dpiScale);
|
|
|
|
|
const D2D1_POINT_2F b = ToD2DPoint(command.uvMin, dpiScale);
|
|
|
|
|
const D2D1_POINT_2F c = ToD2DPoint(command.uvMax, dpiScale);
|
|
|
|
|
const D2D1_POINT_2F points[2] = { b, c };
|
|
|
|
|
sink->BeginFigure(a, D2D1_FIGURE_BEGIN_FILLED);
|
|
|
|
|
sink->AddLines(points, 2u);
|
|
|
|
|
sink->EndFigure(D2D1_FIGURE_END_CLOSED);
|
|
|
|
|
hr = sink->Close();
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderTarget.FillGeometry(geometry.Get(), &solidBrush);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ::XCEngine::UI::UIDrawCommandType::FilledCircle: {
|
|
|
|
|
const float radius = command.radius * dpiScale;
|
|
|
|
|
renderTarget.FillEllipse(
|
|
|
|
|
D2D1::Ellipse(ToD2DPoint(command.position, dpiScale), radius, radius),
|
|
|
|
|
&solidBrush);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ::XCEngine::UI::UIDrawCommandType::CircleOutline: {
|
|
|
|
|
const float radius = command.radius * dpiScale;
|
|
|
|
|
const float thickness = (command.thickness > 0.0f ? command.thickness : 1.0f) * dpiScale;
|
|
|
|
|
renderTarget.DrawEllipse(
|
|
|
|
|
D2D1::Ellipse(ToD2DPoint(command.position, dpiScale), radius, radius),
|
|
|
|
|
&solidBrush,
|
|
|
|
|
thickness);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-04-05 20:46:24 +08:00
|
|
|
case ::XCEngine::UI::UIDrawCommandType::Text: {
|
|
|
|
|
if (command.text.empty()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
const float fontSize = ResolveFontSize(command.fontSize);
|
|
|
|
|
const float scaledFontSize = fontSize * dpiScale;
|
|
|
|
|
IDWriteTextFormat* textFormat = GetTextFormat(scaledFontSize);
|
2026-04-05 20:46:24 +08:00
|
|
|
if (textFormat == nullptr) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const std::wstring text = Utf8ToWide(command.text);
|
|
|
|
|
if (text.empty()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const D2D1_SIZE_F targetSize = renderTarget.GetSize();
|
2026-04-11 17:07:37 +08:00
|
|
|
const float originX = SnapToPixel(command.position.x, dpiScale);
|
|
|
|
|
const float originY = SnapToPixel(command.position.y, dpiScale);
|
|
|
|
|
const float lineHeight = std::ceil(scaledFontSize * 1.6f);
|
2026-04-05 20:46:24 +08:00
|
|
|
const D2D1_RECT_F layoutRect = D2D1::RectF(
|
2026-04-11 17:07:37 +08:00
|
|
|
originX,
|
|
|
|
|
originY,
|
2026-04-05 20:46:24 +08:00
|
|
|
targetSize.width,
|
2026-04-11 17:07:37 +08:00
|
|
|
originY + lineHeight);
|
2026-04-05 20:46:24 +08:00
|
|
|
renderTarget.DrawTextW(
|
|
|
|
|
text.c_str(),
|
|
|
|
|
static_cast<UINT32>(text.size()),
|
|
|
|
|
textFormat,
|
|
|
|
|
layoutRect,
|
|
|
|
|
&solidBrush,
|
|
|
|
|
D2D1_DRAW_TEXT_OPTIONS_CLIP,
|
2026-04-11 17:07:37 +08:00
|
|
|
DWRITE_MEASURING_MODE_GDI_NATURAL);
|
2026-04-05 20:46:24 +08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ::XCEngine::UI::UIDrawCommandType::Image: {
|
|
|
|
|
if (!command.texture.IsValid()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
const D2D1_RECT_F rect = ToD2DRect(command.rect, dpiScale);
|
2026-04-05 20:46:24 +08:00
|
|
|
renderTarget.DrawRectangle(rect, &solidBrush, 1.0f);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ::XCEngine::UI::UIDrawCommandType::PushClipRect: {
|
2026-04-11 17:07:37 +08:00
|
|
|
const D2D1_RECT_F rect = ToD2DRect(command.rect, dpiScale);
|
2026-04-05 20:46:24 +08:00
|
|
|
renderTarget.PushAxisAlignedClip(rect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
|
|
|
|
|
clipStack.push_back(rect);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ::XCEngine::UI::UIDrawCommandType::PopClipRect: {
|
|
|
|
|
if (!clipStack.empty()) {
|
|
|
|
|
renderTarget.PopAxisAlignedClip();
|
|
|
|
|
clipStack.pop_back();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
IDWriteTextFormat* NativeRenderer::GetTextFormat(float fontSize) const {
|
2026-04-05 20:46:24 +08:00
|
|
|
if (!m_dwriteFactory) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 17:07:37 +08:00
|
|
|
const float resolvedFontSize = ResolveFontSize(fontSize);
|
|
|
|
|
const int key = static_cast<int>(std::lround(resolvedFontSize * 10.0f));
|
2026-04-05 20:46:24 +08:00
|
|
|
const auto found = m_textFormats.find(key);
|
|
|
|
|
if (found != m_textFormats.end()) {
|
|
|
|
|
return found->second.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Microsoft::WRL::ComPtr<IDWriteTextFormat> textFormat;
|
|
|
|
|
const HRESULT hr = m_dwriteFactory->CreateTextFormat(
|
|
|
|
|
L"Segoe UI",
|
|
|
|
|
nullptr,
|
|
|
|
|
DWRITE_FONT_WEIGHT_REGULAR,
|
|
|
|
|
DWRITE_FONT_STYLE_NORMAL,
|
|
|
|
|
DWRITE_FONT_STRETCH_NORMAL,
|
2026-04-11 17:07:37 +08:00
|
|
|
resolvedFontSize,
|
2026-04-05 20:46:24 +08:00
|
|
|
L"",
|
|
|
|
|
textFormat.ReleaseAndGetAddressOf());
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
textFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
|
|
|
|
|
textFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR);
|
|
|
|
|
textFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
|
|
|
|
|
|
|
|
|
|
IDWriteTextFormat* result = textFormat.Get();
|
|
|
|
|
m_textFormats.emplace(key, std::move(textFormat));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
D2D1_COLOR_F NativeRenderer::ToD2DColor(const ::XCEngine::UI::UIColor& color) {
|
|
|
|
|
return D2D1::ColorF(color.r, color.g, color.b, color.a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::wstring NativeRenderer::Utf8ToWide(std::string_view text) {
|
|
|
|
|
if (text.empty()) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const int sizeNeeded = MultiByteToWideChar(
|
|
|
|
|
CP_UTF8,
|
|
|
|
|
0,
|
|
|
|
|
text.data(),
|
|
|
|
|
static_cast<int>(text.size()),
|
|
|
|
|
nullptr,
|
|
|
|
|
0);
|
|
|
|
|
if (sizeNeeded <= 0) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::wstring wideText(static_cast<size_t>(sizeNeeded), L'\0');
|
|
|
|
|
MultiByteToWideChar(
|
|
|
|
|
CP_UTF8,
|
|
|
|
|
0,
|
|
|
|
|
text.data(),
|
|
|
|
|
static_cast<int>(text.size()),
|
|
|
|
|
wideText.data(),
|
|
|
|
|
sizeNeeded);
|
|
|
|
|
return wideText;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-06 20:02:34 +08:00
|
|
|
} // namespace XCEngine::UI::Editor::Host
|