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 <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski 2024-04-10 14:01:53 +05:00
parent fd7afc0a66
commit 9bbecbaea3
6 changed files with 48 additions and 62 deletions

View file

@ -23,6 +23,8 @@
#include <d2d1.h>
#include <dwrite.h>
#include <systools/win32/comtools.hxx>
#include <win/winlayout.hxx>
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<ID2D1Factory> mpD2DFactory;
sal::systools::COMReference<ID2D1DCRenderTarget> mpRT;
const D2D1_RENDER_TARGET_PROPERTIES mRTProps;
bool mbRenderingModeNatural;

View file

@ -166,10 +166,6 @@ private:
RGNDATA* mpStdClipRgnData; // Cache Standard-ClipRegion-Data
int mnPenWidth; // line width
inline static sal::systools::COMReference<IDWriteFactory> mxDWriteFactory;
inline static sal::systools::COMReference<IDWriteGdiInterop> 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:

View file

@ -126,8 +126,7 @@ sk_sp<SkTypeface>
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);

View file

@ -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<void **>(&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<ID2D1SolidColorBrush> 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);

View file

@ -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))

View file

@ -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<IUnknown**>(&mxDWriteFactory));
if (FAILED(hr))
static sal::systools::COMReference<IDWriteFactory> pDWriteFactory(
[]()
{
SAL_WARN("vcl.fonts", "HRESULT 0x" << OUString::number(hr, 16) << ": "
<< WindowsErrorStringFromHRESULT(hr));
abort();
}
sal::systools::COMReference<IDWriteFactory> pResult;
HRESULT hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory),
reinterpret_cast<IUnknown**>(&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<IDWriteGdiInterop> 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<IDWriteGdiInterop> 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