From 02c289aa94757ddf95c6eb43313a923f56e65db1 Mon Sep 17 00:00:00 2001 From: Sviatoslav Zubar Date: Tue, 17 Nov 2026 20:54:23 +0100 Subject: [PATCH] egl: make the EglSurface handle counter atomic The EglSurface constructor sets m_hndl = ++s_nextSurfaceHndl. The counter is a plain unsigned int. eglCreatePbufferSurface() constructs the surface before it takes graphicsDriverLock(), and ~EglContext() can construct one outside the lock. Thus, two threads can increment the counter at the same time and get the same handle. EglDisplay::addSurface() does not add a surface with a handle that is already in the map. It returns the existing handle. The second caller then gets the handle of the first surface, and two emulated window surfaces share one host surface. Make the counter std::atomic. Test: Windows CMake build with clang-cl, target EGL_translator_static. Found by code review; not seen at run time. Assisted-by: Claude Code:Claude-Opus-5.5 --- host/gl/glestranslator/egl/egl_surface.cpp | 2 +- host/gl/glestranslator/egl/egl_surface.h | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/host/gl/glestranslator/egl/egl_surface.cpp b/host/gl/glestranslator/egl/egl_surface.cpp index 49779ae6e..59b57853d 100644 --- a/host/gl/glestranslator/egl/egl_surface.cpp +++ b/host/gl/glestranslator/egl/egl_surface.cpp @@ -18,7 +18,7 @@ #include "egl_display.h" #include "egl_os_api.h" -unsigned int EglSurface::s_nextSurfaceHndl = 0; +std::atomic EglSurface::s_nextSurfaceHndl{0}; EglSurface::~EglSurface(){ diff --git a/host/gl/glestranslator/egl/egl_surface.h b/host/gl/glestranslator/egl/egl_surface.h index f505ddee5..1e9f6bf1a 100644 --- a/host/gl/glestranslator/egl/egl_surface.h +++ b/host/gl/glestranslator/egl/egl_surface.h @@ -22,6 +22,7 @@ #include #include +#include #include class EglSurface; @@ -60,7 +61,9 @@ class EglSurface { GLuint glRboDepth = 0; private: - static unsigned int s_nextSurfaceHndl; + // Atomic: eglCreatePbufferSurface() constructs surfaces before it takes + // graphicsDriverLock(), so two threads can increment it at the same time. + static std::atomic s_nextSurfaceHndl; ESurfaceType m_type = WINDOW; unsigned int m_hndl = 0;