From eb5044b6fbfe0714d09a725ac02a86802584e714 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Wed, 1 May 2013 14:10:31 +0300 Subject: [PATCH] Gfx: Change COM pointers to use WRL::ComPtr --- Common/Gfx/CanvasD2D.cpp | 82 ++++++++++++++---------------------- Common/Gfx/CanvasD2D.h | 11 ++--- Common/Gfx/TextFormatD2D.cpp | 43 +++++-------------- Common/Gfx/TextFormatD2D.h | 7 +-- 4 files changed, 52 insertions(+), 91 deletions(-) diff --git a/Common/Gfx/CanvasD2D.cpp b/Common/Gfx/CanvasD2D.cpp index 79089cfd..a109b0cd 100644 --- a/Common/Gfx/CanvasD2D.cpp +++ b/Common/Gfx/CanvasD2D.cpp @@ -22,16 +22,6 @@ #include "Util/WICBitmapLockGDIP.h" #include "../../Library/Litestep.h" -template -inline void SafeRelease(T** t) -{ - if (*t) - { - (*t)->Release(); - *t = nullptr; - } -} - namespace { D2D1_COLOR_F ToColorF(const Gdiplus::Color& color) @@ -54,13 +44,12 @@ D2D1_RECT_F ToRectF(const Gdiplus::RectF& rect) namespace Gfx { UINT CanvasD2D::c_Instances = 0; -ID2D1Factory* CanvasD2D::c_D2DFactory = nullptr; -IDWriteFactory* CanvasD2D::c_DWFactory = nullptr; -IDWriteGdiInterop* CanvasD2D::c_DWGDIInterop = nullptr; -IWICImagingFactory* CanvasD2D::c_WICFactory = nullptr; +Microsoft::WRL::ComPtr CanvasD2D::c_D2DFactory; +Microsoft::WRL::ComPtr CanvasD2D::c_DWFactory; +Microsoft::WRL::ComPtr CanvasD2D::c_DWGDIInterop; +Microsoft::WRL::ComPtr CanvasD2D::c_WICFactory; CanvasD2D::CanvasD2D() : Canvas(), - m_Target(), m_Bitmap(), m_GdipGraphics(), m_GdipBitmap(), @@ -88,7 +77,7 @@ bool CanvasD2D::Initialize() HRESULT hr = D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, fo, - &c_D2DFactory); + c_D2DFactory.GetAddressOf()); if (FAILED(hr)) return false; hr = CoCreateInstance( @@ -96,16 +85,16 @@ bool CanvasD2D::Initialize() nullptr, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, - (LPVOID*)&c_WICFactory); + (LPVOID*)c_WICFactory.GetAddressOf()); if (FAILED(hr)) return false; hr = DWriteCreateFactory( DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), - (IUnknown**)&c_DWFactory); + (IUnknown**)c_DWFactory.GetAddressOf()); if (FAILED(hr)) return false; - hr = c_DWFactory->GetGdiInterop(&c_DWGDIInterop); + hr = c_DWFactory->GetGdiInterop(c_DWGDIInterop.GetAddressOf()); if (FAILED(hr)) return false; hr = c_DWFactory->RegisterFontCollectionLoader(Util::DWriteFontCollectionLoader::GetInstance()); @@ -120,18 +109,18 @@ void CanvasD2D::Finalize() --c_Instances; if (c_Instances == 0) { - SafeRelease(&c_D2DFactory); - SafeRelease(&c_WICFactory); - SafeRelease(&c_DWGDIInterop); + c_D2DFactory.Reset(); + c_WICFactory.Reset(); + c_DWGDIInterop.Reset(); c_DWFactory->UnregisterFontCollectionLoader(Util::DWriteFontCollectionLoader::GetInstance()); - SafeRelease(&c_DWFactory); + c_DWFactory.Reset(); } } void CanvasD2D::Dispose() { - SafeRelease(&m_Target); + m_Target.Reset(); delete m_GdipGraphics; m_GdipGraphics = nullptr; @@ -205,8 +194,7 @@ void CanvasD2D::EndTargetDraw() if (m_Target) { m_Target->EndDraw(); - - SafeRelease(&m_Target); + m_Target.Reset(); } } @@ -330,8 +318,8 @@ void CanvasD2D::DrawTextW(const WCHAR* str, UINT strLen, const TextFormat& forma Gdiplus::Color color; brush.GetColor(&color); - ID2D1SolidColorBrush* solidBrush; - HRESULT hr = m_Target->CreateSolidColorBrush(ToColorF(color), &solidBrush); + Microsoft::WRL::ComPtr solidBrush; + HRESULT hr = m_Target->CreateSolidColorBrush(ToColorF(color), solidBrush.GetAddressOf()); if (SUCCEEDED(hr)) { TextFormatD2D& formatD2D = (TextFormatD2D&)format; @@ -341,31 +329,27 @@ void CanvasD2D::DrawTextW(const WCHAR* str, UINT strLen, const TextFormat& forma m_Target->DrawTextLayout( D2D1::Point2F(right ? rect.X - 2 : rect.X + 2.0f, rect.Y - 1.0f), - formatD2D.m_TextLayout, - solidBrush); - - solidBrush->Release(); + formatD2D.m_TextLayout.Get(), + solidBrush.Get()); } } bool CanvasD2D::MeasureTextW(const WCHAR* str, UINT strLen, const TextFormat& format, Gdiplus::RectF& rect) { - IDWriteTextLayout* textLayout; + Microsoft::WRL::ComPtr textLayout; HRESULT hr = c_DWFactory->CreateTextLayout( str, strLen, - ((TextFormatD2D&)format).m_TextFormat, + ((TextFormatD2D&)format).m_TextFormat.Get(), 10000, 10000, - &textLayout); + textLayout.GetAddressOf()); if (SUCCEEDED(hr)) { DWRITE_TEXT_METRICS metrics; textLayout->GetMetrics(&metrics); rect.Width = metrics.width + 5.0f; rect.Height = metrics.height + 1.0f; // 1.0f to get same result as GDI+. - - textLayout->Release(); return true; } @@ -376,14 +360,14 @@ bool CanvasD2D::MeasureTextLinesW(const WCHAR* str, UINT strLen, const TextForma { ((TextFormatD2D&)format).m_TextFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_WRAP); - IDWriteTextLayout* textLayout; + Microsoft::WRL::ComPtr textLayout; HRESULT hr = c_DWFactory->CreateTextLayout( str, strLen, - ((TextFormatD2D&)format).m_TextFormat, + ((TextFormatD2D&)format).m_TextFormat.Get(), rect.Width, 10000, - &textLayout); + textLayout.GetAddressOf()); if (SUCCEEDED(hr)) { DWRITE_TEXT_METRICS metrics; @@ -391,8 +375,6 @@ bool CanvasD2D::MeasureTextLinesW(const WCHAR* str, UINT strLen, const TextForma rect.Width = metrics.width + 5.0f; rect.Height = metrics.height + 1.0f; // 1.0f to get same result as GDI+. lines = metrics.lineCount; - - textLayout->Release(); return true; } @@ -419,15 +401,14 @@ void CanvasD2D::DrawBitmap(Gdiplus::Bitmap* bitmap, const Gdiplus::Rect& dstRect { D2D1_BITMAP_PROPERTIES props = D2D1::BitmapProperties( D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)); - ID2D1Bitmap* d2dBitmap; - HRESULT hr = m_Target->CreateSharedBitmap(__uuidof(IWICBitmapLock), bitmapLock, &props, &d2dBitmap); + Microsoft::WRL::ComPtr d2dBitmap; + HRESULT hr = m_Target->CreateSharedBitmap( + __uuidof(IWICBitmapLock), bitmapLock, &props, d2dBitmap.GetAddressOf()); if (SUCCEEDED(hr)) { auto rDst = ToRectF(dstRect); auto rSrc = ToRectF(srcRect); - m_Target->DrawBitmap(d2dBitmap, rDst, 1.0F, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, rSrc); - - d2dBitmap->Release(); + m_Target->DrawBitmap(d2dBitmap.Get(), rDst, 1.0F, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, rSrc); } // D2D will still use the pixel data after this call (at the next Flush() or EndDraw()). @@ -448,12 +429,11 @@ void CanvasD2D::FillRectangle(Gdiplus::Rect& rect, const Gdiplus::SolidBrush& br Gdiplus::Color color; brush.GetColor(&color); - ID2D1SolidColorBrush* solidBrush; - HRESULT hr = m_Target->CreateSolidColorBrush(ToColorF(color), &solidBrush); + Microsoft::WRL::ComPtr solidBrush; + HRESULT hr = m_Target->CreateSolidColorBrush(ToColorF(color), solidBrush.GetAddressOf()); if (SUCCEEDED(hr)) { - m_Target->FillRectangle(ToRectF(rect), solidBrush); - solidBrush->Release(); + m_Target->FillRectangle(ToRectF(rect), solidBrush.Get()); } } diff --git a/Common/Gfx/CanvasD2D.h b/Common/Gfx/CanvasD2D.h index ed044776..08eadf6b 100644 --- a/Common/Gfx/CanvasD2D.h +++ b/Common/Gfx/CanvasD2D.h @@ -29,6 +29,7 @@ #include #include #include +#include namespace Gfx { @@ -89,7 +90,7 @@ private: // Retrieves current GDI+ transform (if any) and converts to a D2D Matrix D2D1_MATRIX_3X2_F GetCurrentTransform(); - ID2D1RenderTarget* m_Target; + Microsoft::WRL::ComPtr m_Target; Util::WICBitmapDIB m_Bitmap; // GDI+ objects that share the pixel data of m_Bitmap. @@ -99,10 +100,10 @@ private: bool m_TextAntiAliasing; static UINT c_Instances; - static ID2D1Factory* c_D2DFactory; - static IDWriteFactory* c_DWFactory; - static IDWriteGdiInterop* c_DWGDIInterop; - static IWICImagingFactory* c_WICFactory; + static Microsoft::WRL::ComPtr c_D2DFactory; + static Microsoft::WRL::ComPtr c_DWFactory; + static Microsoft::WRL::ComPtr c_DWGDIInterop; + static Microsoft::WRL::ComPtr c_WICFactory; }; } // namespace Gfx diff --git a/Common/Gfx/TextFormatD2D.cpp b/Common/Gfx/TextFormatD2D.cpp index 333a0da0..3b2b9c19 100644 --- a/Common/Gfx/TextFormatD2D.cpp +++ b/Common/Gfx/TextFormatD2D.cpp @@ -22,10 +22,7 @@ namespace Gfx { -TextFormatD2D::TextFormatD2D() : - m_TextFormat(), - m_TextLayout(), - m_InlineEllipsis() +TextFormatD2D::TextFormatD2D() { } @@ -36,23 +33,9 @@ TextFormatD2D::~TextFormatD2D() void TextFormatD2D::Dispose() { - if (m_TextFormat) - { - m_TextFormat->Release(); - m_TextFormat = nullptr; - } - - if (m_TextLayout) - { - m_TextLayout->Release(); - m_TextLayout = nullptr; - } - - if (m_InlineEllipsis) - { - m_InlineEllipsis->Release(); - m_InlineEllipsis = nullptr; - } + m_TextFormat.Reset(); + m_TextLayout.Reset(); + m_InlineEllipsis.Reset(); } void TextFormatD2D::CreateLayout(const WCHAR* str, UINT strLen, float maxW, float maxH) @@ -79,13 +62,8 @@ void TextFormatD2D::CreateLayout(const WCHAR* str, UINT strLen, float maxW, floa } else { - if (m_TextLayout) - { - m_TextLayout->Release(); - m_TextLayout = nullptr; - } - - CanvasD2D::c_DWFactory->CreateTextLayout(str, strLen, m_TextFormat, maxW, maxH, &m_TextLayout); + CanvasD2D::c_DWFactory->CreateTextLayout( + str, strLen, m_TextFormat.Get(), maxW, maxH, m_TextLayout.ReleaseAndGetAddressOf()); } } @@ -108,7 +86,7 @@ void TextFormatD2D::SetProperties( // using the GDI family name and then create a text format using the DirectWrite family name // obtained from it. HRESULT hr = Util::GetDWritePropertiesFromGDIProperties( - CanvasD2D::c_DWFactory, fontFamily, bold, italic, dwriteFontWeight, dwriteFontStyle, + CanvasD2D::c_DWFactory.Get(), fontFamily, bold, italic, dwriteFontWeight, dwriteFontStyle, dwriteFontStretch, dwriteFamilyName, _countof(dwriteFamilyName)); if (SUCCEEDED(hr)) { @@ -129,7 +107,7 @@ void TextFormatD2D::SetProperties( // If |fontFamily| is not in the system collection, use the font collection from // |fontCollectionD2D| if possible. - if (!Util::IsFamilyInSystemFontCollection(CanvasD2D::c_DWFactory, fontFamily) && + if (!Util::IsFamilyInSystemFontCollection(CanvasD2D::c_DWFactory.Get(), fontFamily) && (fontCollectionD2D && fontCollectionD2D->InitializeCollection())) { IDWriteFont* dwriteFont = Util::FindDWriteFontInFontCollectionByGDIFamilyName( @@ -183,10 +161,11 @@ void TextFormatD2D::SetTrimming(bool trim) { if (!m_InlineEllipsis) { - CanvasD2D::c_DWFactory->CreateEllipsisTrimmingSign(m_TextFormat, &m_InlineEllipsis); + CanvasD2D::c_DWFactory->CreateEllipsisTrimmingSign( + m_TextFormat.Get(), m_InlineEllipsis.GetAddressOf()); } - inlineObject = m_InlineEllipsis; + inlineObject = m_InlineEllipsis.Get(); trimming.granularity = DWRITE_TRIMMING_GRANULARITY_CHARACTER; wordWrapping = DWRITE_WORD_WRAPPING_WRAP; } diff --git a/Common/Gfx/TextFormatD2D.h b/Common/Gfx/TextFormatD2D.h index ac473451..86fd791a 100644 --- a/Common/Gfx/TextFormatD2D.h +++ b/Common/Gfx/TextFormatD2D.h @@ -22,6 +22,7 @@ #include "TextFormat.h" #include #include +#include namespace Gfx { @@ -55,9 +56,9 @@ private: // changes. void CreateLayout(const WCHAR* str, UINT strLen, float maxW, float maxH); - IDWriteTextFormat* m_TextFormat; - IDWriteTextLayout* m_TextLayout; - IDWriteInlineObject* m_InlineEllipsis; + Microsoft::WRL::ComPtr m_TextFormat; + Microsoft::WRL::ComPtr m_TextLayout; + Microsoft::WRL::ComPtr m_InlineEllipsis; std::wstring m_LastString; };