From 5c9f7fe34fdb5da4e260fea9fedf5c4f016319eb Mon Sep 17 00:00:00 2001 From: Sviatoslav Zubar Date: Tue, 17 Nov 2026 20:32:02 +0100 Subject: [PATCH] egl: return WGL pbuffers to the pool when DC release fails WglDisplay::releasePbuffer() returns early when wglReleasePbufferDC() fails. At that point, releaseDC() has already set the DC as released. The pbuffer stays in mLivePbufs. It is not reused and not destroyed. createPbufferSurface() refills an empty pool with max(live, 8) new pbuffers. Each stuck pbuffer increases the live count, so the pool doubles again and again. On NVIDIA, each pbuffer has a hidden NVOpenGLPbuffer window. Window operations on the full desktop become slow. The surface is not in use after release. Thus, always move the pbuffer to the free list. When the pool gives the pbuffer again, refreshDC() gets a new DC. If that fails, the pbuffer goes to mDeletePbufs, as before. Log the failure, with a limit on the number of messages. Test: Android Emulator 37.2.13, Windows 11, NVIDIA RTX 4070 Laptop, -gpu host, 16 UI test flows, then a framework restart. The emulator DLL cannot be built from main, so the test used the shipped DLL with the early return removed (no log). NVOpenGLPbuffer windows: 68 -> 297 without the change, 43 -> 41 with it. GDI objects: 137 -> 367 without, 107 -> 109 with. Stuck pbuffers (live, no owner) after 8 flows: 76 without, 0 with. Assisted-by: Claude Code:Claude-Opus-5.5 --- host/gl/glestranslator/egl/egl_os_api_wgl.cpp | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/host/gl/glestranslator/egl/egl_os_api_wgl.cpp b/host/gl/glestranslator/egl/egl_os_api_wgl.cpp index 0b643be12..27d5c2587 100644 --- a/host/gl/glestranslator/egl/egl_os_api_wgl.cpp +++ b/host/gl/glestranslator/egl/egl_os_api_wgl.cpp @@ -1113,7 +1113,25 @@ class WglDisplay : public EglOS::Display { WinSurface* winpb = WinSurface::from(pb); - if (!winpb->releaseDC()) return false; + if (!winpb->releaseDC()) { + // The surface is unused either way. Returning here would keep it in + // mLivePbufs for good: it is never reused or destroyed, and every + // later refill (sized from the live count) grows the pool, one + // driver pbuffer (a hidden window on NVIDIA) each. Reuse re-gets its + // DC in refreshDC(); a pbuffer that fails that goes to mDeletePbufs. + // Failures recur with surface churn (about 10 per UI test flow seen in + // the emulator): log the first 8, then at powers of two. sGlobalLock + // guards the counter. + const DWORD error = GetLastError(); + static uint64_t sReleaseFailures = 0; + ++sReleaseFailures; + if (sReleaseFailures <= 8 || (sReleaseFailures & (sReleaseFailures - 1)) == 0) { + GFXSTREAM_WARNING("wglReleasePbufferDC failed for pbuffer %p (error 0x%lx, %llu " + "failures so far)", + winpb->getPbuffer(), error, + (unsigned long long)sReleaseFailures); + } + } const EglOS::PixelFormat* pixelFormat = winpb->getPixelFormat();