From e07aa294a4dae03c101e48bc535b7fc9973acd6a Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Mon, 28 Mar 2016 21:22:06 +0200 Subject: [PATCH] Fixed drawing on wxMemoryDC with Direct2DRenderer. 32bpp wxBitmaps selected into wxMemoryDC can represent either 0RGB or ARGB bitmaps and hence there is necessary to instruct renderer how to interpret 32bpp contents while creating a wxGraphicsContext. wxBitmap::HasAlpha() flag is used for these purposes and its value is passed to wxD2DContext ctor (wxD2DContext is used internally as a backend D2D component) and next used to configure physical renderer created with ID2D1Factory::CreateDCRenderTarget. Closes #17465. --- src/msw/graphicsd2d.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/msw/graphicsd2d.cpp b/src/msw/graphicsd2d.cpp index cf16182ef6..2529383f86 100644 --- a/src/msw/graphicsd2d.cpp +++ b/src/msw/graphicsd2d.cpp @@ -2671,8 +2671,8 @@ private: class wxD2DDCRenderTargetResourceHolder : public wxD2DRenderTargetResourceHolder { public: - wxD2DDCRenderTargetResourceHolder(ID2D1Factory* factory, HDC hdc, const wxSize dcSize) : - m_factory(factory), m_hdc(hdc) + wxD2DDCRenderTargetResourceHolder(ID2D1Factory* factory, HDC hdc, const wxSize dcSize, bool hasAlpha) : + m_factory(factory), m_hdc(hdc), m_hasAlpha(hasAlpha) { m_dcSize.left = 0; m_dcSize.top = 0; @@ -2683,10 +2683,13 @@ public: protected: void DoAcquireResource() { + D2D1_ALPHA_MODE alphaMode = m_hasAlpha ? + D2D1_ALPHA_MODE_PREMULTIPLIED : D2D1_ALPHA_MODE_IGNORE; + wxCOMPtr renderTarget; D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties( D2D1_RENDER_TARGET_TYPE_DEFAULT, - D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)); + D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, alphaMode)); HRESULT hr = m_factory->CreateDCRenderTarget( &renderTargetProperties, @@ -2703,6 +2706,7 @@ private: ID2D1Factory* m_factory; HDC m_hdc; RECT m_dcSize; + bool m_hasAlpha; }; // The null context has no state of its own and does nothing. @@ -2802,7 +2806,8 @@ class wxD2DContext : public wxGraphicsContext, wxD2DResourceManager public: wxD2DContext(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, HWND hwnd); - wxD2DContext(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, HDC hdc, const wxSize& dcSize); + wxD2DContext(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, HDC hdc, const wxSize& dcSize, + bool hasAlpha = false); #if wxUSE_IMAGE wxD2DContext(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, wxImage& image); @@ -2948,9 +2953,9 @@ wxD2DContext::wxD2DContext(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dF Init(); } -wxD2DContext::wxD2DContext(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, HDC hdc, const wxSize& dcSize) : +wxD2DContext::wxD2DContext(wxGraphicsRenderer* renderer, ID2D1Factory* direct2dFactory, HDC hdc, const wxSize& dcSize, bool hasAlpha) : wxGraphicsContext(renderer), m_direct2dFactory(direct2dFactory), - m_renderTargetHolder(new wxD2DDCRenderTargetResourceHolder(direct2dFactory, hdc, dcSize)) + m_renderTargetHolder(new wxD2DDCRenderTargetResourceHolder(direct2dFactory, hdc, dcSize, hasAlpha)) { Init(); } @@ -3621,7 +3626,10 @@ wxGraphicsContext* wxD2DRenderer::CreateContext(const wxMemoryDC& dc) int width, height; dc.GetSize(&width, &height); - return new wxD2DContext(this, m_direct2dFactory, dc.GetHDC(), wxSize(width, height)); + wxBitmap bmp = dc.GetSelectedBitmap(); + wxASSERT_MSG( bmp.IsOk(), wxS("Should select a bitmap before creating wxGraphicsContext") ); + + return new wxD2DContext(this, m_direct2dFactory, dc.GetHDC(), wxSize(width, height), bmp.HasAlpha()); } #if wxUSE_PRINTING_ARCHITECTURE