From 9bbecbaea359350c2e0d78ebe4dcd10d16dccbee Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Wed, 10 Apr 2024 14:01:53 +0500 Subject: [PATCH] Use COMReference in D2DWriteTextOutRenderer ... and simplify the related code. Change-Id: Idaef7c9d725273e202948158e45ded7e7a2f85a0 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/165985 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- vcl/inc/win/DWriteTextRenderer.hxx | 8 ++--- vcl/inc/win/salgdi.h | 7 ++-- vcl/skia/win/gdiimpl.cxx | 3 +- vcl/win/gdi/DWriteTextRenderer.cxx | 31 ++++------------ vcl/win/gdi/salfont.cxx | 3 +- vcl/win/gdi/salgdi.cxx | 58 +++++++++++++++++------------- 6 files changed, 48 insertions(+), 62 deletions(-) diff --git a/vcl/inc/win/DWriteTextRenderer.hxx b/vcl/inc/win/DWriteTextRenderer.hxx index b822a6bca488..1cdf67d04a39 100644 --- a/vcl/inc/win/DWriteTextRenderer.hxx +++ b/vcl/inc/win/DWriteTextRenderer.hxx @@ -23,6 +23,8 @@ #include #include +#include + #include enum class D2DTextAntiAliasMode @@ -37,7 +39,6 @@ class D2DWriteTextOutRenderer : public TextOutRenderer { public: explicit D2DWriteTextOutRenderer(bool bRenderingModeNatural); - virtual ~D2DWriteTextOutRenderer() override; bool operator()(GenericSalLayout const &rLayout, SalGraphics &rGraphics, @@ -62,9 +63,8 @@ private: IDWriteFontFace* GetDWriteFace(const WinFontInstance& rWinFont, float * lfSize) const; bool performRender(GenericSalLayout const &rLayout, SalGraphics &rGraphics, HDC hDC, bool& bRetry, bool bRenderingModeNatural); - ID2D1Factory * mpD2DFactory; - IDWriteFactory * mpDWriteFactory; - ID2D1DCRenderTarget * mpRT; + sal::systools::COMReference mpD2DFactory; + sal::systools::COMReference mpRT; const D2D1_RENDER_TARGET_PROPERTIES mRTProps; bool mbRenderingModeNatural; diff --git a/vcl/inc/win/salgdi.h b/vcl/inc/win/salgdi.h index 80fafdeba56f..19edee0f4440 100644 --- a/vcl/inc/win/salgdi.h +++ b/vcl/inc/win/salgdi.h @@ -166,10 +166,6 @@ private: RGNDATA* mpStdClipRgnData; // Cache Standard-ClipRegion-Data int mnPenWidth; // line width - inline static sal::systools::COMReference mxDWriteFactory; - inline static sal::systools::COMReference mxDWriteGdiInterop; - inline static bool bDWriteDone = false; - // just call both from setHDC! void InitGraphics(); void DeInitGraphics(); @@ -198,7 +194,8 @@ public: SCREEN }; - static void getDWriteFactory(IDWriteFactory** pFactory, IDWriteGdiInterop** pInterop = nullptr); + static IDWriteFactory* getDWriteFactory(); + static IDWriteGdiInterop* getDWriteGdiInterop(); public: diff --git a/vcl/skia/win/gdiimpl.cxx b/vcl/skia/win/gdiimpl.cxx index 0451307b7bdf..1d48fb9bbe71 100644 --- a/vcl/skia/win/gdiimpl.cxx +++ b/vcl/skia/win/gdiimpl.cxx @@ -126,8 +126,7 @@ sk_sp WinSkiaSalGraphicsImpl::createDirectWriteTypeface(const WinFontInstance* pWinFont) try { using sal::systools::ThrowIfFailed; - IDWriteFactory* dwriteFactory; - WinSalGraphics::getDWriteFactory(&dwriteFactory); + IDWriteFactory* dwriteFactory = WinSalGraphics::getDWriteFactory(); if (!dwriteDone) { dwriteFontMgr = SkFontMgr_New_DirectWrite(dwriteFactory); diff --git a/vcl/win/gdi/DWriteTextRenderer.cxx b/vcl/win/gdi/DWriteTextRenderer.cxx index c167c44c0110..e0a50c4ed981 100644 --- a/vcl/win/gdi/DWriteTextRenderer.cxx +++ b/vcl/win/gdi/DWriteTextRenderer.cxx @@ -62,8 +62,10 @@ D2DTextAntiAliasMode lclGetSystemTextAntiAliasMode() return eMode; } -IDWriteRenderingParams* lclSetRenderingMode(IDWriteFactory* pDWriteFactory, DWRITE_RENDERING_MODE eRenderingMode) +IDWriteRenderingParams* lclSetRenderingMode(DWRITE_RENDERING_MODE eRenderingMode) { + IDWriteFactory* pDWriteFactory = WinSalGraphics::getDWriteFactory(); + IDWriteRenderingParams* pDefaultParameters = nullptr; pDWriteFactory->CreateRenderingParams(&pDefaultParameters); @@ -118,22 +120,12 @@ D2DWriteTextOutRenderer::D2DWriteTextOutRenderer(bool bRenderingModeNatural) mbRenderingModeNatural(bRenderingModeNatural), meTextAntiAliasMode(D2DTextAntiAliasMode::Default) { - WinSalGraphics::getDWriteFactory(&mpDWriteFactory); - HRESULT hr = S_OK; - hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory), nullptr, reinterpret_cast(&mpD2DFactory)); + HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory), nullptr, IID_PPV_ARGS_Helper(&mpD2DFactory)); if (SUCCEEDED(hr)) hr = CreateRenderTarget(bRenderingModeNatural); meTextAntiAliasMode = lclGetSystemTextAntiAliasMode(); } -D2DWriteTextOutRenderer::~D2DWriteTextOutRenderer() -{ - if (mpRT) - mpRT->Release(); - if (mpD2DFactory) - mpD2DFactory->Release(); -} - void D2DWriteTextOutRenderer::applyTextAntiAliasMode(bool bRenderingModeNatural) { D2D1_TEXT_ANTIALIAS_MODE eTextAAMode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT; @@ -163,17 +155,12 @@ void D2DWriteTextOutRenderer::applyTextAntiAliasMode(bool bRenderingModeNatural) if (bRenderingModeNatural) eRenderingMode = DWRITE_RENDERING_MODE_CLEARTYPE_NATURAL; - mpRT->SetTextRenderingParams(lclSetRenderingMode(mpDWriteFactory, eRenderingMode)); + mpRT->SetTextRenderingParams(lclSetRenderingMode(eRenderingMode)); mpRT->SetTextAntialiasMode(eTextAAMode); } HRESULT D2DWriteTextOutRenderer::CreateRenderTarget(bool bRenderingModeNatural) { - if (mpRT) - { - mpRT->Release(); - mpRT = nullptr; - } HRESULT hr = CHECKHR(mpD2DFactory->CreateDCRenderTarget(&mRTProps, &mpRT)); if (SUCCEEDED(hr)) applyTextAntiAliasMode(bRenderingModeNatural); @@ -212,8 +199,7 @@ bool D2DWriteTextOutRenderer::performRender(GenericSalLayout const & rLayout, Sa if (!Ready()) return false; - HRESULT hr = S_OK; - hr = BindDC(hDC); + HRESULT hr = BindDC(hDC); if (hr == D2DERR_RECREATE_TARGET) { @@ -242,7 +228,7 @@ bool D2DWriteTextOutRenderer::performRender(GenericSalLayout const & rLayout, Sa succeeded = SUCCEEDED(hr); } - ID2D1SolidColorBrush* pBrush = nullptr; + sal::systools::COMReference pBrush; if (succeeded) { COLORREF bgrTextColor = GetTextColor(hDC); @@ -282,9 +268,6 @@ bool D2DWriteTextOutRenderer::performRender(GenericSalLayout const & rLayout, Sa hr = CHECKHR(mpRT->EndDraw()); } - if (pBrush) - pBrush->Release(); - if (hr == D2DERR_RECREATE_TARGET) { CreateRenderTarget(bRenderingModeNatural); diff --git a/vcl/win/gdi/salfont.cxx b/vcl/win/gdi/salfont.cxx index a98d82c6b8a4..fb9e004b708d 100644 --- a/vcl/win/gdi/salfont.cxx +++ b/vcl/win/gdi/salfont.cxx @@ -1343,8 +1343,7 @@ IDWriteFontFace* WinFontInstance::GetDWFontFace() const SelectObject(hDC, hOrigFont); }); - IDWriteGdiInterop* pDWriteGdiInterop; - WinSalGraphics::getDWriteFactory(nullptr, &pDWriteGdiInterop); + IDWriteGdiInterop* pDWriteGdiInterop = WinSalGraphics::getDWriteGdiInterop(); HRESULT hr = pDWriteGdiInterop->CreateFontFaceFromHdc(hDC, &mxDWFontFace); if (FAILED(hr)) diff --git a/vcl/win/gdi/salgdi.cxx b/vcl/win/gdi/salgdi.cxx index cb4b500a4a43..8da350105afd 100644 --- a/vcl/win/gdi/salgdi.cxx +++ b/vcl/win/gdi/salgdi.cxx @@ -758,35 +758,43 @@ void WinSalGraphics::GetResolution( sal_Int32& rDPIX, sal_Int32& rDPIY ) rDPIX = rDPIY = 600; } -void WinSalGraphics::getDWriteFactory(IDWriteFactory** pFactory, IDWriteGdiInterop** pInterop) +// static +IDWriteFactory* WinSalGraphics::getDWriteFactory() { - if (!bDWriteDone) - { - HRESULT hr = S_OK; - hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), - reinterpret_cast(&mxDWriteFactory)); - if (FAILED(hr)) + static sal::systools::COMReference pDWriteFactory( + []() { - SAL_WARN("vcl.fonts", "HRESULT 0x" << OUString::number(hr, 16) << ": " - << WindowsErrorStringFromHRESULT(hr)); - abort(); - } + sal::systools::COMReference pResult; + HRESULT hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), + reinterpret_cast(&pResult)); + if (FAILED(hr)) + { + SAL_WARN("vcl.fonts", "HRESULT 0x" << OUString::number(hr, 16) << ": " + << WindowsErrorStringFromHRESULT(hr)); + abort(); + } + return pResult; + }()); + return pDWriteFactory.get(); +} - hr = mxDWriteFactory->GetGdiInterop(&mxDWriteGdiInterop); - if (FAILED(hr)) +// static +IDWriteGdiInterop* WinSalGraphics::getDWriteGdiInterop() +{ + static sal::systools::COMReference pDWriteGdiInterop( + []() { - SAL_WARN("vcl.fonts", "HRESULT 0x" << OUString::number(hr, 16) << ": " - << WindowsErrorStringFromHRESULT(hr)); - abort(); - } - - bDWriteDone = true; - } - - if (pFactory) - *pFactory = mxDWriteFactory.get(); - if (pInterop) - *pInterop = mxDWriteGdiInterop.get(); + sal::systools::COMReference pResult; + HRESULT hr = getDWriteFactory()->GetGdiInterop(&pResult); + if (FAILED(hr)) + { + SAL_WARN("vcl.fonts", "HRESULT 0x" << OUString::number(hr, 16) << ": " + << WindowsErrorStringFromHRESULT(hr)); + abort(); + } + return pResult; + }()); + return pDWriteGdiInterop.get(); } sal_uInt16 WinSalGraphics::GetBitCount() const