From 0004f29bf6ee3c6060a272c79f14993e92e053c7 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 27 Mar 2017 13:51:46 -0700 Subject: Refcount all CFX_DIBSources (and subclasses) all the time. There are currently several ownership models for these objects, including ad-hoc logic for sharing and deletion, and the now-redundant CFX_DIBitmapRef externally-counted handle to the DIBs. Replace them all with the internal refcount scheme. Change-Id: I2db399dfc19219eda384f94cc989353b78ce2872 Reviewed-on: https://pdfium-review.googlesource.com/3166 Reviewed-by: dsinclair Commit-Queue: dsinclair --- core/fxge/win32/fx_win32_dib.cpp | 52 +++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 27 deletions(-) (limited to 'core/fxge/win32/fx_win32_dib.cpp') diff --git a/core/fxge/win32/fx_win32_dib.cpp b/core/fxge/win32/fx_win32_dib.cpp index 9b85b57317..9815457bbd 100644 --- a/core/fxge/win32/fx_win32_dib.cpp +++ b/core/fxge/win32/fx_win32_dib.cpp @@ -11,7 +11,8 @@ #include "core/fxge/win32/cfx_windowsdib.h" #include "core/fxge/win32/win32_int.h" -CFX_ByteString CFX_WindowsDIB::GetBitmapInfo(const CFX_DIBitmap* pBitmap) { +CFX_ByteString CFX_WindowsDIB::GetBitmapInfo( + const CFX_RetainPtr& pBitmap) { CFX_ByteString result; int len = sizeof(BITMAPINFOHEADER); if (pBitmap->GetBPP() == 1 || pBitmap->GetBPP() == 8) { @@ -51,9 +52,9 @@ CFX_ByteString CFX_WindowsDIB::GetBitmapInfo(const CFX_DIBitmap* pBitmap) { return result; } -CFX_DIBitmap* _FX_WindowsDIB_LoadFromBuf(BITMAPINFO* pbmi, - LPVOID pData, - bool bAlpha) { +CFX_RetainPtr _FX_WindowsDIB_LoadFromBuf(BITMAPINFO* pbmi, + LPVOID pData, + bool bAlpha) { int width = pbmi->bmiHeader.biWidth; int height = pbmi->bmiHeader.biHeight; BOOL bBottomUp = true; @@ -62,15 +63,13 @@ CFX_DIBitmap* _FX_WindowsDIB_LoadFromBuf(BITMAPINFO* pbmi, bBottomUp = false; } int pitch = (width * pbmi->bmiHeader.biBitCount + 31) / 32 * 4; - CFX_DIBitmap* pBitmap = new CFX_DIBitmap; + auto pBitmap = pdfium::MakeRetain(); FXDIB_Format format = bAlpha ? (FXDIB_Format)(pbmi->bmiHeader.biBitCount + 0x200) : (FXDIB_Format)pbmi->bmiHeader.biBitCount; - bool ret = pBitmap->Create(width, height, format); - if (!ret) { - delete pBitmap; + if (!pBitmap->Create(width, height, format)) return nullptr; - } + FXSYS_memcpy(pBitmap->GetBuffer(), pData, pitch * height); if (bBottomUp) { uint8_t* temp_buf = FX_Alloc(uint8_t, pitch); @@ -98,11 +97,13 @@ CFX_DIBitmap* _FX_WindowsDIB_LoadFromBuf(BITMAPINFO* pbmi, return pBitmap; } -CFX_DIBitmap* CFX_WindowsDIB::LoadFromBuf(BITMAPINFO* pbmi, LPVOID pData) { +CFX_RetainPtr CFX_WindowsDIB::LoadFromBuf(BITMAPINFO* pbmi, + LPVOID pData) { return _FX_WindowsDIB_LoadFromBuf(pbmi, pData, false); } -HBITMAP CFX_WindowsDIB::GetDDBitmap(const CFX_DIBitmap* pBitmap, HDC hDC) { +HBITMAP CFX_WindowsDIB::GetDDBitmap(const CFX_RetainPtr& pBitmap, + HDC hDC) { CFX_ByteString info = GetBitmapInfo(pBitmap); return CreateDIBitmap(hDC, (BITMAPINFOHEADER*)info.c_str(), CBM_INIT, pBitmap->GetBuffer(), (BITMAPINFO*)info.c_str(), @@ -116,7 +117,8 @@ void GetBitmapSize(HBITMAP hBitmap, int& w, int& h) { h = bmp.bmHeight; } -CFX_DIBitmap* CFX_WindowsDIB::LoadFromFile(const wchar_t* filename) { +CFX_RetainPtr CFX_WindowsDIB::LoadFromFile( + const wchar_t* filename) { CWin32Platform* pPlatform = (CWin32Platform*)CFX_GEModule::Get()->GetPlatformData(); if (pPlatform->m_GdiplusExt.IsAvailable()) { @@ -131,30 +133,29 @@ CFX_DIBitmap* CFX_WindowsDIB::LoadFromFile(const wchar_t* filename) { return nullptr; } HDC hDC = CreateCompatibleDC(nullptr); - int width, height; + int width; + int height; GetBitmapSize(hBitmap, width, height); - CFX_DIBitmap* pDIBitmap = new CFX_DIBitmap; + auto pDIBitmap = pdfium::MakeRetain(); if (!pDIBitmap->Create(width, height, FXDIB_Rgb)) { - delete pDIBitmap; DeleteDC(hDC); return nullptr; } CFX_ByteString info = GetBitmapInfo(pDIBitmap); int ret = GetDIBits(hDC, hBitmap, 0, height, pDIBitmap->GetBuffer(), (BITMAPINFO*)info.c_str(), DIB_RGB_COLORS); - if (!ret) { - delete pDIBitmap; - pDIBitmap = nullptr; - } DeleteDC(hDC); + if (!ret) + return nullptr; return pDIBitmap; } -CFX_DIBitmap* CFX_WindowsDIB::LoadFromFile(const char* filename) { +CFX_RetainPtr CFX_WindowsDIB::LoadFromFile(const char* filename) { return LoadFromFile(CFX_WideString::FromLocal(filename).c_str()); } -CFX_DIBitmap* CFX_WindowsDIB::LoadDIBitmap(WINDIB_Open_Args_ args) { +CFX_RetainPtr CFX_WindowsDIB::LoadDIBitmap( + WINDIB_Open_Args_ args) { CWin32Platform* pPlatform = (CWin32Platform*)CFX_GEModule::Get()->GetPlatformData(); if (pPlatform->m_GdiplusExt.IsAvailable()) { @@ -171,20 +172,17 @@ CFX_DIBitmap* CFX_WindowsDIB::LoadDIBitmap(WINDIB_Open_Args_ args) { HDC hDC = CreateCompatibleDC(nullptr); int width, height; GetBitmapSize(hBitmap, width, height); - CFX_DIBitmap* pDIBitmap = new CFX_DIBitmap; + auto pDIBitmap = pdfium::MakeRetain(); if (!pDIBitmap->Create(width, height, FXDIB_Rgb)) { - delete pDIBitmap; DeleteDC(hDC); return nullptr; } CFX_ByteString info = GetBitmapInfo(pDIBitmap); int ret = GetDIBits(hDC, hBitmap, 0, height, pDIBitmap->GetBuffer(), (BITMAPINFO*)info.c_str(), DIB_RGB_COLORS); - if (!ret) { - delete pDIBitmap; - pDIBitmap = nullptr; - } DeleteDC(hDC); + if (!ret) + return nullptr; return pDIBitmap; } -- cgit v1.2.3