From f973291f936046f11e8f65f259219b01275bab4b Mon Sep 17 00:00:00 2001 From: narzoul Date: Mon, 16 May 2016 22:31:58 +0200 Subject: [PATCH] Replaced DirectDraw palette and clipper raw pointers with smart pointers --- DDrawCompat/CompatGdi.cpp | 1 - DDrawCompat/CompatGdiDcCache.cpp | 9 ++++----- DDrawCompat/CompatPaletteConverter.cpp | 3 +-- DDrawCompat/CompatPaletteConverter.h | 2 +- DDrawCompat/CompatPrimarySurface.cpp | 2 +- DDrawCompat/CompatPrimarySurface.h | 2 +- DDrawCompat/DllMain.cpp | 6 +++--- DDrawCompat/RealPrimarySurface.cpp | 22 +++++++--------------- DDrawCompat/RealPrimarySurface.h | 2 +- 9 files changed, 19 insertions(+), 30 deletions(-) diff --git a/DDrawCompat/CompatGdi.cpp b/DDrawCompat/CompatGdi.cpp index 44a95e9..4361486 100644 --- a/DDrawCompat/CompatGdi.cpp +++ b/DDrawCompat/CompatGdi.cpp @@ -1,6 +1,5 @@ #include -#include "CompatDirectDrawPalette.h" #include "CompatGdi.h" #include "CompatGdiCaret.h" #include "CompatGdiDcCache.h" diff --git a/DDrawCompat/CompatGdiDcCache.cpp b/DDrawCompat/CompatGdiDcCache.cpp index 970b416..7b3d302 100644 --- a/DDrawCompat/CompatGdiDcCache.cpp +++ b/DDrawCompat/CompatGdiDcCache.cpp @@ -1,7 +1,6 @@ #include #include -#include "CompatDirectDrawPalette.h" #include "CompatGdiDcCache.h" #include "CompatPrimarySurface.h" #include "CompatPtr.h" @@ -20,7 +19,7 @@ namespace DWORD g_maxUsedCacheSize = 0; DWORD g_ddLockThreadId = 0; - IDirectDrawPalette* g_palette = nullptr; + CompatWeakPtr g_palette; PALETTEENTRY g_paletteEntries[256] = {}; void* g_surfaceMemory = nullptr; LONG g_pitch = 0; @@ -167,7 +166,8 @@ namespace CompatGdiDcCache bool init() { auto dd(DDrawRepository::getDirectDraw()); - dd->CreatePalette(dd, DDPCAPS_8BIT | DDPCAPS_ALLOW256, g_paletteEntries, &g_palette, nullptr); + dd->CreatePalette(dd, + DDPCAPS_8BIT | DDPCAPS_ALLOW256, g_paletteEntries, &g_palette.getRef(), nullptr); return nullptr != g_palette; } @@ -221,8 +221,7 @@ namespace CompatGdiDcCache { std::memcpy(&g_paletteEntries[startingEntry], &entries[startingEntry], count * sizeof(PALETTEENTRY)); - CompatDirectDrawPalette::s_origVtable.SetEntries( - g_palette, 0, startingEntry, count, g_paletteEntries); + g_palette->SetEntries(g_palette, 0, startingEntry, count, g_paletteEntries); clear(); } } diff --git a/DDrawCompat/CompatPaletteConverter.cpp b/DDrawCompat/CompatPaletteConverter.cpp index 0bf503a..0f73331 100644 --- a/DDrawCompat/CompatPaletteConverter.cpp +++ b/DDrawCompat/CompatPaletteConverter.cpp @@ -1,7 +1,6 @@ #include #include -#include "CompatDirectDrawPalette.h" #include "CompatPaletteConverter.h" #include "CompatPrimarySurface.h" #include "CompatPtr.h" @@ -131,7 +130,7 @@ namespace CompatPaletteConverter g_dc = nullptr; } - void setClipper(IDirectDrawClipper* clipper) + void setClipper(CompatWeakPtr clipper) { if (g_surface) { diff --git a/DDrawCompat/CompatPaletteConverter.h b/DDrawCompat/CompatPaletteConverter.h index 765efb1..c5f2290 100644 --- a/DDrawCompat/CompatPaletteConverter.h +++ b/DDrawCompat/CompatPaletteConverter.h @@ -12,6 +12,6 @@ namespace CompatPaletteConverter HDC getDc(); CompatWeakPtr getSurface(); void release(); - void setClipper(IDirectDrawClipper* clipper); + void setClipper(CompatWeakPtr clipper); void updatePalette(DWORD startingEntry, DWORD count); } diff --git a/DDrawCompat/CompatPrimarySurface.cpp b/DDrawCompat/CompatPrimarySurface.cpp index 819c999..d293b2e 100644 --- a/DDrawCompat/CompatPrimarySurface.cpp +++ b/DDrawCompat/CompatPrimarySurface.cpp @@ -80,7 +80,7 @@ namespace CompatPrimarySurface DisplayMode displayMode = {}; bool isDisplayModeChanged = false; - LPDIRECTDRAWPALETTE palette = nullptr; + CompatWeakPtr palette; PALETTEENTRY paletteEntries[256] = {}; LONG width = 0; LONG height = 0; diff --git a/DDrawCompat/CompatPrimarySurface.h b/DDrawCompat/CompatPrimarySurface.h index dc62eab..b388a07 100644 --- a/DDrawCompat/CompatPrimarySurface.h +++ b/DDrawCompat/CompatPrimarySurface.h @@ -27,7 +27,7 @@ namespace CompatPrimarySurface extern DisplayMode displayMode; extern bool isDisplayModeChanged; - extern LPDIRECTDRAWPALETTE palette; + extern CompatWeakPtr palette; extern PALETTEENTRY paletteEntries[256]; extern LONG width; extern LONG height; diff --git a/DDrawCompat/DllMain.cpp b/DDrawCompat/DllMain.cpp index 39720af..4c4d219 100644 --- a/DDrawCompat/DllMain.cpp +++ b/DDrawCompat/DllMain.cpp @@ -82,12 +82,12 @@ namespace void hookDirectDrawPalette(CompatRef dd) { PALETTEENTRY paletteEntries[2] = {}; - IDirectDrawPalette* palette = nullptr; - HRESULT result = dd->CreatePalette(&dd, DDPCAPS_1BIT, paletteEntries, &palette, nullptr); + CompatPtr palette; + HRESULT result = dd->CreatePalette(&dd, + DDPCAPS_1BIT, paletteEntries, &palette.getRef(), nullptr); if (SUCCEEDED(result)) { CompatDirectDrawPalette::hookVtable(*palette); - palette->lpVtbl->Release(palette); } else { diff --git a/DDrawCompat/RealPrimarySurface.cpp b/DDrawCompat/RealPrimarySurface.cpp index 9a1aabf..2ea209f 100644 --- a/DDrawCompat/RealPrimarySurface.cpp +++ b/DDrawCompat/RealPrimarySurface.cpp @@ -1,6 +1,5 @@ #include -#include "CompatDirectDrawPalette.h" #include "CompatDirectDrawSurface.h" #include "CompatGdi.h" #include "CompatPaletteConverter.h" @@ -23,6 +22,7 @@ namespace CompatWeakPtr g_frontBuffer; CompatWeakPtr g_backBuffer; + CompatWeakPtr g_clipper; DDSURFACEDESC2 g_surfaceDesc = {}; IReleaseNotifier g_releaseNotifier(onRelease); @@ -47,16 +47,6 @@ namespace bool result = false; - if (!RealPrimarySurface::isFullScreen()) - { - IDirectDrawClipper* clipper = nullptr; - if (FAILED(g_frontBuffer->GetClipper(g_frontBuffer, &clipper))) - { - return false; - } - clipper->lpVtbl->Release(clipper); - } - auto primary(CompatPrimarySurface::getPrimary()); if (CompatPrimarySurface::pixelFormat.dwRGBBitCount <= 8) { @@ -161,6 +151,7 @@ namespace timeEndPeriod(1); g_frontBuffer = nullptr; g_backBuffer.release(); + g_clipper = nullptr; g_isFullScreen = false; CompatPaletteConverter::release(); @@ -347,15 +338,16 @@ HRESULT RealPrimarySurface::restore() return g_frontBuffer->Restore(g_frontBuffer); } -void RealPrimarySurface::setClipper(LPDIRECTDRAWCLIPPER clipper) +void RealPrimarySurface::setClipper(CompatWeakPtr clipper) { - CompatPaletteConverter::setClipper(clipper); - HRESULT result = g_frontBuffer->SetClipper(g_frontBuffer, clipper); if (FAILED(result)) { LOG_ONCE("Failed to set clipper on the real primary surface: " << result); + return; } + CompatPaletteConverter::setClipper(clipper); + g_clipper = clipper; } void RealPrimarySurface::setPalette() @@ -370,7 +362,7 @@ void RealPrimarySurface::setPalette() void RealPrimarySurface::update() { - if (!IsRectEmpty(&g_updateRect) && 0 == g_disableUpdateCount) + if (!IsRectEmpty(&g_updateRect) && 0 == g_disableUpdateCount && (g_isFullScreen || g_clipper)) { const long long qpcNow = Time::queryPerformanceCounter(); if (Time::qpcToMs(qpcNow - g_qpcNextUpdate) >= 0) diff --git a/DDrawCompat/RealPrimarySurface.h b/DDrawCompat/RealPrimarySurface.h index f123287..230ac6a 100644 --- a/DDrawCompat/RealPrimarySurface.h +++ b/DDrawCompat/RealPrimarySurface.h @@ -23,7 +23,7 @@ public: static void release(); static void removeUpdateThread(); static HRESULT restore(); - static void setClipper(LPDIRECTDRAWCLIPPER clipper); + static void setClipper(CompatWeakPtr clipper); static void setPalette(); static void update(); static void updatePalette(DWORD startingEntry, DWORD count);