Hash :
8bb3c827
Author :
Date :
2021-07-22T19:06:40
Fix Multithreaded eglDestroyContext()/eglTerminate()
The following EGL calls can lead to a crash in eglMakeCurrent():
Thread A: eglMakeCurrent(context A)
Thread B: eglDestroyContext(context A)
B: eglTerminate() <<--- this release context A
Thread A: eglMakeCurrent(context B)
The eglMakeCurrent(context B) call will assert when attempting to
unMakeCurrent(), since thread A doesn't know that context A was already
destroyed by thread B.
To fix this:
1.) A Context will only be released once there are no Threads that
currently have a reference to it (no longer have the Context current).
- Context::mIsCurrent is being removed, since it was inaccurate and not
thread-safe. For example, when eglTerminate() was called, the
eglTerminate()'ing-Thread would "steal" the Context that was current on
another Thread to destroy it.
2.) A Display will only be fully terminated and its resources released
once all Contexts have been destroyed and are no longer current.
Otherwise, Display::terminate() will return if any Contexts are still in
use by a Thread.
EGL 1.5 Specification
3.2 Initialization
If contexts or surfaces, created with respect to dpy are current (see
section 3.7.3) to any thread, then they are not actually destroyed
while they remain current. If other resources created with respect to
dpy are in use by any current context or surface, then they are also
not destroyed until the corresponding context or surface is no longer
current.
With this fix, the app com.netmarble.sknightsmmo can start.
This also exposed an issue with GlslangFinalize(), since glslang can
only be initialized/finalized once per process. Otherwise, the
following EGL commands will call GlslangFinalize() without ever being
able to GlslangInitialize() again, leading to crashes since
GlslangFinalize() cleans up glslang for the entire process.
dpy1 = eglGetPlatformDisplay() |
eglInitialize(dpy1) | GlslangInitialize()
dpy2 = eglGetPlatformDisplay() |
eglInitialize(dpy2) | GlslangInitialize()
eglTerminate(dpy2) | GlslangFinalize()
eglInitialize(dpy1) | isInitialized() == true
Since Display::isInitialized() == true, the rest of
Display::initialize() is skipped and GlslangInitialize() is not called.
Later, the next test that attempts to compile a program will crash due
to glslang no longer being initialized.
Finally, this exposed the following tests leaking EGLContext handles:
- EGLSurfaceTest::initializeContext()
- EGLContextSharingTest.DisplayShareGroupContextCreation
- EGLCreateContextAttribsTest.IMGContextPriorityExtension
- EGLMultiContextTest.TestContextDestroySimple
Other tests were failing to reset the context, preventing the Display
from being terminated since there were still references to Contexts
owned by the display:
eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
Bug: angleproject:6208
Bug: angleproject:6304
Bug: angleproject:6322
Test: EGLContextSharingTest.EglTerminateMultiThreaded
Test: EGLContextSharingTestNoFixture.EglDestoryContextManyTimesSameContext
Test: Load com.netmarble.sknightsmmo
Change-Id: I160922af93db6cabe0ed396be77762fa8dfc7656
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3046961
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Tim Van Patten <timvp@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
//
// Copyright 2018 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// EGLDirectCompositionTest.cpp:
// Tests pertaining to DirectComposition and WindowsUIComposition.
#ifdef ANGLE_ENABLE_D3D11_COMPOSITOR_NATIVE_WINDOW
# include <d3d11.h>
# include "test_utils/ANGLETest.h"
# include <DispatcherQueue.h>
# include <VersionHelpers.h>
# include <Windows.Foundation.h>
# include <windows.ui.composition.Desktop.h>
# include <windows.ui.composition.h>
# include <windows.ui.composition.interop.h>
# include <wrl.h>
# include <memory>
# include "libANGLE/renderer/d3d/d3d11/converged/CompositorNativeWindow11.h"
# include "util/OSWindow.h"
# include "util/com_utils.h"
# include "util/test_utils.h"
using namespace angle;
using namespace ABI::Windows::System;
using namespace ABI::Windows::UI::Composition;
using namespace ABI::Windows::UI::Composition::Desktop;
using namespace ABI::Windows::Foundation;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
const int WINDOWWIDTH = 200, WINDOWHEIGHT = 200;
class EGLDirectCompositionTest : public ANGLETest
{
protected:
EGLDirectCompositionTest() : mOSWindow(nullptr) {}
void testSetUp() override
{
if (!mRoHelper.SupportedWindowsRelease())
{
return;
}
// Create an OS Window
mOSWindow = OSWindow::New();
mOSWindow->initialize("EGLDirectCompositionTest", WINDOWWIDTH, WINDOWHEIGHT);
auto nativeWindow = mOSWindow->getNativeWindow();
setWindowVisible(mOSWindow, true);
// Create DispatcherQueue for window to process compositor callbacks
CreateDispatcherQueue(mDispatcherController);
HSTRING act;
HSTRING_HEADER header;
auto hr = mRoHelper.GetStringReference(RuntimeClass_Windows_UI_Composition_Compositor, &act,
&header);
ASSERT_TRUE(SUCCEEDED(hr));
void *fac = nullptr;
hr = mRoHelper.GetActivationFactory(act, __uuidof(IActivationFactory), &fac);
ASSERT_TRUE(SUCCEEDED(hr));
ComPtr<IActivationFactory> compositorFactory;
compositorFactory.Attach((IActivationFactory *)fac);
hr = compositorFactory->ActivateInstance(&mCompositor);
ASSERT_TRUE(SUCCEEDED(hr));
// Create a DesktopWindowTarget against native window (HWND)
CreateDesktopWindowTarget(mCompositor, static_cast<HWND>(nativeWindow), mDesktopTarget);
ASSERT_TRUE(SUCCEEDED(mCompositor->CreateSpriteVisual(mAngleHost.GetAddressOf())));
ComPtr<IVisual> angleVis;
ASSERT_TRUE(SUCCEEDED(mAngleHost.As(&angleVis)));
ASSERT_TRUE(SUCCEEDED(angleVis->put_Size(
{static_cast<FLOAT>(WINDOWWIDTH), static_cast<FLOAT>(WINDOWHEIGHT)})));
ASSERT_TRUE(SUCCEEDED(angleVis->put_Offset({0, 0, 0})));
ComPtr<ICompositionTarget> compTarget;
ASSERT_TRUE(SUCCEEDED(mDesktopTarget.As(&compTarget)));
ASSERT_TRUE(SUCCEEDED(compTarget->put_Root(angleVis.Get())));
Init();
}
void CreateDispatcherQueue(ComPtr<IDispatcherQueueController> &controller)
{
DispatcherQueueOptions options{sizeof(DispatcherQueueOptions), DQTYPE_THREAD_CURRENT,
DQTAT_COM_STA};
auto hr = mRoHelper.CreateDispatcherQueueController(options, controller.GetAddressOf());
ASSERT_TRUE(SUCCEEDED(hr));
}
void CreateDesktopWindowTarget(ComPtr<ICompositor> const &compositor,
const HWND window,
ComPtr<IDesktopWindowTarget> &target)
{
namespace abi = ABI::Windows::UI::Composition::Desktop;
ComPtr<ICompositorDesktopInterop> interop;
ASSERT_TRUE(SUCCEEDED(compositor.As(&interop)));
ASSERT_TRUE(SUCCEEDED(interop->CreateDesktopWindowTarget(
window, true, reinterpret_cast<abi::IDesktopWindowTarget **>(target.GetAddressOf()))));
}
void Init()
{
if (!mRoHelper.SupportedWindowsRelease())
{
return;
}
DPI_AWARENESS_CONTEXT
WINAPI
SetThreadDpiAwarenessContext(_In_ DPI_AWARENESS_CONTEXT dpiContext);
auto userModule = LoadLibraryA("user32.dll");
if (userModule == nullptr)
{
return;
}
auto temp = GetProcAddress(userModule, "SetThreadDpiAwarenessContext");
mFpSetThreadDpiAwarenessContext = reinterpret_cast<_SetThreadDpiAwarenessContext *>(temp);
const EGLint configAttributes[] = {
EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 8, EGL_STENCIL_SIZE, 8, EGL_NONE};
const EGLint defaultDisplayAttributes[] = {
EGL_PLATFORM_ANGLE_TYPE_ANGLE,
EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
EGL_NONE,
};
PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT =
reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(
eglGetProcAddress("eglGetPlatformDisplayEXT"));
ASSERT_TRUE(eglGetPlatformDisplayEXT != nullptr);
mEglDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,
reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY),
defaultDisplayAttributes);
ASSERT_TRUE(mEglDisplay != EGL_NO_DISPLAY);
ASSERT_EGL_TRUE(eglInitialize(mEglDisplay, nullptr, nullptr));
EGLint nConfigs = 0;
ASSERT_EGL_TRUE(eglGetConfigs(mEglDisplay, nullptr, 0, &nConfigs));
ASSERT_TRUE(nConfigs != 0);
ASSERT_EGL_TRUE(eglChooseConfig(mEglDisplay, configAttributes, &mEglConfig, 1, &nConfigs));
}
void CreateSurface(ComPtr<ABI::Windows::UI::Composition::ISpriteVisual> visual,
EGLSurface &surface)
{
auto displayExtensions = eglQueryString(mEglDisplay, EGL_EXTENSIONS);
// Check that the EGL_ANGLE_windows_ui_composition display extension is available
ASSERT_TRUE(strstr(displayExtensions, "EGL_ANGLE_windows_ui_composition") != nullptr);
const EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
// Use a spritevisual as the nativewindowtype
surface =
eglCreateWindowSurface(mEglDisplay, mEglConfig,
static_cast<EGLNativeWindowType>((void *)visual.Get()), nullptr);
ASSERT_TRUE(surface != EGL_NO_SURFACE);
mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, contextAttributes);
ASSERT_TRUE(mEglContext != EGL_NO_CONTEXT);
ASSERT_TRUE(eglMakeCurrent(mEglDisplay, surface, surface, mEglContext) != EGL_FALSE);
}
void testTearDown() override
{
if (!mRoHelper.SupportedWindowsRelease())
{
return;
}
if (mEglDisplay != EGL_NO_DISPLAY)
{
ASSERT_EGL_TRUE(eglTerminate(mEglDisplay));
mEglDisplay = EGL_NO_DISPLAY;
}
OSWindow::Delete(&mOSWindow);
}
OSWindow *mOSWindow;
ComPtr<ICompositor> mCompositor;
ComPtr<IDispatcherQueueController> mDispatcherController;
ComPtr<ICompositionColorBrush> mColorBrush;
ComPtr<IDesktopWindowTarget> mDesktopTarget;
ComPtr<ISpriteVisual> mAngleHost;
EGLDisplay mEglDisplay;
EGLContext mEglContext;
EGLConfig mEglConfig;
rx::RoHelper mRoHelper;
using _SetThreadDpiAwarenessContext =
DPI_AWARENESS_CONTEXT WINAPI(DPI_AWARENESS_CONTEXT dpiContext);
_SetThreadDpiAwarenessContext *mFpSetThreadDpiAwarenessContext;
};
// This tests that a surface created using a SpriteVisual as container has the expected dimensions
// which should match the dimensions of the SpriteVisual passed in
TEST_P(EGLDirectCompositionTest, SurfaceSizeFromSpriteSize)
{
// Only attempt this test when on Windows 10 1803+
ANGLE_SKIP_TEST_IF(!mRoHelper.SupportedWindowsRelease());
EGLSurface s{nullptr};
CreateSurface(mAngleHost, s);
EGLint surfacewidth = 0, surfaceheight = 0;
eglQuerySurface(mEglDisplay, s, EGL_WIDTH, &surfacewidth);
eglQuerySurface(mEglDisplay, s, EGL_HEIGHT, &surfaceheight);
ComPtr<IVisual> angleVis;
ASSERT_TRUE(SUCCEEDED(mAngleHost.As(&angleVis)));
ABI::Windows::Foundation::Numerics::Vector2 visualsize{0, 0};
ASSERT_TRUE(SUCCEEDED(angleVis->get_Size(&visualsize)));
ASSERT_TRUE(surfacewidth == static_cast<int>(visualsize.X));
ASSERT_TRUE(surfaceheight == static_cast<int>(visualsize.Y));
ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) !=
EGL_FALSE);
ASSERT_EGL_TRUE(eglDestroySurface(mEglDisplay, s));
ASSERT_EGL_TRUE(eglDestroyContext(mEglDisplay, mEglContext));
mEglContext = EGL_NO_CONTEXT;
}
// This tests that a WindowSurface can be created using a SpriteVisual as the containing window
// and that pixels can be successfully rendered into the resulting WindowSurface
TEST_P(EGLDirectCompositionTest, RenderSolidColor)
{
// Only attempt this test when on Windows 10 1803+
ANGLE_SKIP_TEST_IF(!mRoHelper.SupportedWindowsRelease());
// http://crbug.com/1063962
ANGLE_SKIP_TEST_IF(isD3D11Renderer() && IsIntel());
EGLSurface s{nullptr};
CreateSurface(mAngleHost, s);
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
glViewport(0, 0, WINDOWWIDTH, WINDOWHEIGHT);
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_EGL_TRUE(eglSwapBuffers(mEglDisplay, s));
// ensure user/DWM have a chance to paint the window and kick it to the top of the desktop
// zorder before we attempt to sample
angle::Sleep(200);
mOSWindow->messageLoop();
uint8_t *pixelBuffer = static_cast<uint8_t *>(malloc(WINDOWWIDTH * WINDOWHEIGHT * 4));
ZeroMemory(pixelBuffer, WINDOWWIDTH * WINDOWHEIGHT * 4);
// In order to accurately capture a bitmap, we need to temporarily shift into per-monitor DPI
// mode in order to get the window offset from desktop correct
auto previous = mFpSetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
bool success = mOSWindow->takeScreenshot(pixelBuffer);
mFpSetThreadDpiAwarenessContext(previous);
ASSERT_EGL_TRUE(success);
ASSERT_EGL_TRUE(pixelBuffer[(50 * 50 * 4)] == 255);
ASSERT_EGL_TRUE(pixelBuffer[(50 * 50 * 4) + 1] == 0);
ASSERT_EGL_TRUE(pixelBuffer[(50 * 50 * 4) + 2] == 0);
ASSERT_EGL_TRUE(pixelBuffer[(50 * 50 * 4) + 3] == 255);
ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT) !=
EGL_FALSE);
ASSERT_EGL_TRUE(eglDestroySurface(mEglDisplay, s));
ASSERT_EGL_TRUE(eglDestroyContext(mEglDisplay, mEglContext));
mEglContext = EGL_NO_CONTEXT;
}
ANGLE_INSTANTIATE_TEST(EGLDirectCompositionTest, WithNoFixture(ES2_D3D11()));
#endif // ANGLE_ENABLE_D3D11_COMPOSITOR_NATIVE_WINDOW