From 4e3debf7769836363faa08247af548270d93ee83 Mon Sep 17 00:00:00 2001 From: narzoul Date: Sat, 20 Feb 2021 12:10:05 +0100 Subject: [PATCH] Merged Gdi::DcCache into Gdi::Dc --- DDrawCompat/DDrawCompat.vcxproj | 2 - DDrawCompat/DDrawCompat.vcxproj.filters | 6 --- DDrawCompat/Gdi/Dc.cpp | 53 ++++++++++++++------- DDrawCompat/Gdi/DcCache.cpp | 61 ------------------------- DDrawCompat/Gdi/DcCache.h | 14 ------ DDrawCompat/Gdi/Gdi.cpp | 3 -- 6 files changed, 37 insertions(+), 102 deletions(-) delete mode 100644 DDrawCompat/Gdi/DcCache.cpp delete mode 100644 DDrawCompat/Gdi/DcCache.h diff --git a/DDrawCompat/DDrawCompat.vcxproj b/DDrawCompat/DDrawCompat.vcxproj index d31e5ca..dd3ec3a 100644 --- a/DDrawCompat/DDrawCompat.vcxproj +++ b/DDrawCompat/DDrawCompat.vcxproj @@ -218,7 +218,6 @@ - @@ -293,7 +292,6 @@ - diff --git a/DDrawCompat/DDrawCompat.vcxproj.filters b/DDrawCompat/DDrawCompat.vcxproj.filters index c6da761..8682499 100644 --- a/DDrawCompat/DDrawCompat.vcxproj.filters +++ b/DDrawCompat/DDrawCompat.vcxproj.filters @@ -90,9 +90,6 @@ Header Files\Gdi - - Header Files\Gdi - Header Files\Gdi @@ -416,9 +413,6 @@ Source Files\Gdi - - Source Files\Gdi - Source Files\Gdi diff --git a/DDrawCompat/Gdi/Dc.cpp b/DDrawCompat/Gdi/Dc.cpp index 3e834a3..463f96b 100644 --- a/DDrawCompat/Gdi/Dc.cpp +++ b/DDrawCompat/Gdi/Dc.cpp @@ -1,20 +1,25 @@ #include -#include +#include #include -#include "Common/Hook.h" -#include "Common/Log.h" -#include "Common/ScopedCriticalSection.h" -#include "D3dDdi/ScopedCriticalSection.h" -#include "Gdi/Dc.h" -#include "Gdi/DcCache.h" -#include "Gdi/Gdi.h" -#include "Gdi/Region.h" -#include "Gdi/VirtualScreen.h" -#include "Gdi/Window.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include namespace { + struct Cache + { + std::vector> cache; + std::vector> defPalCache; + }; + struct CompatDc { HDC dc; @@ -25,10 +30,11 @@ namespace bool useDefaultPalette; }; - typedef std::unordered_map CompatDcMap; + typedef std::map CompatDcMap; Compat::CriticalSection g_cs; CompatDcMap g_origDcToCompatDc; + std::map g_threadIdToDcCache; void restoreDc(const CompatDc& compatDc); @@ -137,6 +143,7 @@ namespace Gdi Gdi::VirtualScreen::deleteDc(origDcToCompatDc.second.dc); } g_origDcToCompatDc.clear(); + g_threadIdToDcCache.clear(); } void dllThreadDetach() @@ -157,6 +164,7 @@ namespace Gdi ++it; } } + g_threadIdToDcCache.erase(GetCurrentThreadId()); } HDC getDc(HDC origDc) @@ -179,10 +187,21 @@ namespace Gdi CompatDc compatDc; compatDc.useDefaultPalette = GetStockObject(DEFAULT_PALETTE) == GetCurrentObject(origDc, OBJ_PAL); - compatDc.dc = Gdi::DcCache::getDc(compatDc.useDefaultPalette); - if (!compatDc.dc) + + auto& cache = g_threadIdToDcCache[GetCurrentThreadId()]; + auto& dcCache = compatDc.useDefaultPalette ? cache.defPalCache : cache.cache; + if (dcCache.empty()) { - return nullptr; + compatDc.dc = Gdi::VirtualScreen::createDc(compatDc.useDefaultPalette); + if (!compatDc.dc) + { + return nullptr; + } + } + else + { + compatDc.dc = dcCache.back().release(); + dcCache.pop_back(); } POINT origin = {}; @@ -235,7 +254,9 @@ namespace Gdi if (0 == compatDc.refCount) { restoreDc(compatDc); - Gdi::DcCache::releaseDc(compatDc.dc, compatDc.useDefaultPalette); + auto& cache = g_threadIdToDcCache[GetCurrentThreadId()]; + auto& dcCache = compatDc.useDefaultPalette ? cache.defPalCache : cache.cache; + dcCache.emplace_back(compatDc.dc, Gdi::VirtualScreen::deleteDc); g_origDcToCompatDc.erase(origDc); } } diff --git a/DDrawCompat/Gdi/DcCache.cpp b/DDrawCompat/Gdi/DcCache.cpp deleted file mode 100644 index 5273c28..0000000 --- a/DDrawCompat/Gdi/DcCache.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include -#include -#include - -#include -#include -#include - -namespace -{ - struct Cache - { - std::vector> cache; - std::vector> defPalCache; - }; - - Compat::CriticalSection g_cs; - std::map g_threadIdToDcCache; -} - -namespace Gdi -{ - namespace DcCache - { - void dllProcessDetach() - { - Compat::ScopedCriticalSection lock(g_cs); - g_threadIdToDcCache.clear(); - } - - void dllThreadDetach() - { - Compat::ScopedCriticalSection lock(g_cs); - g_threadIdToDcCache.erase(GetCurrentThreadId()); - } - - HDC getDc(bool useDefaultPalette) - { - Compat::ScopedCriticalSection lock(g_cs); - auto& cache = g_threadIdToDcCache[GetCurrentThreadId()]; - auto& dcCache = useDefaultPalette ? cache.defPalCache : cache.cache; - - if (dcCache.empty()) - { - return Gdi::VirtualScreen::createDc(useDefaultPalette); - } - - HDC dc = dcCache.back().release(); - dcCache.pop_back(); - return dc; - } - - void releaseDc(HDC cachedDc, bool useDefaultPalette) - { - Compat::ScopedCriticalSection lock(g_cs); - auto& cache = g_threadIdToDcCache[GetCurrentThreadId()]; - auto& dcCache = useDefaultPalette ? cache.defPalCache : cache.cache; - dcCache.emplace_back(cachedDc, Gdi::VirtualScreen::deleteDc); - } - } -} diff --git a/DDrawCompat/Gdi/DcCache.h b/DDrawCompat/Gdi/DcCache.h deleted file mode 100644 index 6406c48..0000000 --- a/DDrawCompat/Gdi/DcCache.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include - -namespace Gdi -{ - namespace DcCache - { - void dllProcessDetach(); - void dllThreadDetach(); - HDC getDc(bool useDefaultPalette); - void releaseDc(HDC cachedDc, bool useDefaultPalette); - } -} diff --git a/DDrawCompat/Gdi/Gdi.cpp b/DDrawCompat/Gdi/Gdi.cpp index d051f66..94959a0 100644 --- a/DDrawCompat/Gdi/Gdi.cpp +++ b/DDrawCompat/Gdi/Gdi.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -33,7 +32,6 @@ namespace Gdi { WinProc::dllThreadDetach(); Dc::dllThreadDetach(); - DcCache::dllThreadDetach(); } void installHooks() @@ -98,7 +96,6 @@ namespace Gdi PresentationWindow::uninstallHooks(); WinProc::uninstallHooks(); Dc::dllProcessDetach(); - DcCache::dllProcessDetach(); } void watchWindowPosChanges(WindowPosChangeNotifyFunc notifyFunc)