Hash :
feb599ad
Author :
Date :
2021-12-09T20:17:14
Invalidate EGL handles during eglTerminate
EGL 1.5 spec says -
Termination marks all EGL-specific resources, such as contexts
and surfaces, associated with the specified display for deletion.
Handles to all such resources are invalid as soon as eglTerminate
returns
Move EGL object handles to another set, tracking invalid objects,
during display terminate. Destroy these invalid objects during
eglReleaseThread
Bug: angleproject:6798
Test: EGLMultiContextTest.NegativeTestAfterEglTerminate*
Test: EGLMultiContextTest.RepeatedEglInitAndTerminate*
Test: Android CTS WrapperTest.testThreadCleanup
Change-Id: Ie160212c98367493e645d9d1c8260e7a30649386
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3329273
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Reviewed-by: Tim Van Patten <timvp@google.com>
Commit-Queue: mohan maiya <m.maiya@samsung.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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
//
// Copyright 2021 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.
//
// EGLMultiContextTest.cpp:
// Tests relating to multiple non-shared Contexts.
#include <gtest/gtest.h>
#include "test_utils/ANGLETest.h"
#include "test_utils/MultiThreadSteps.h"
#include "test_utils/angle_test_configs.h"
#include "test_utils/gl_raii.h"
#include "util/EGLWindow.h"
using namespace angle;
namespace
{
EGLBoolean SafeDestroyContext(EGLDisplay display, EGLContext &context)
{
EGLBoolean result = EGL_TRUE;
if (context != EGL_NO_CONTEXT)
{
result = eglDestroyContext(display, context);
context = EGL_NO_CONTEXT;
}
return result;
}
class EGLMultiContextTest : public ANGLETest
{
public:
EGLMultiContextTest() : mContexts{EGL_NO_CONTEXT, EGL_NO_CONTEXT}, mTexture(0) {}
void testTearDown() override
{
glDeleteTextures(1, &mTexture);
EGLDisplay display = getEGLWindow()->getDisplay();
if (display != EGL_NO_DISPLAY)
{
for (auto &context : mContexts)
{
SafeDestroyContext(display, context);
}
}
// Set default test state to not give an error on shutdown.
getEGLWindow()->makeCurrent();
}
bool chooseConfig(EGLDisplay dpy, EGLConfig *config) const
{
bool result = false;
EGLint count = 0;
EGLint clientVersion = EGL_OPENGL_ES3_BIT;
EGLint attribs[] = {EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_ALPHA_SIZE,
8,
EGL_RENDERABLE_TYPE,
clientVersion,
EGL_SURFACE_TYPE,
EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
EGL_NONE};
result = eglChooseConfig(dpy, attribs, config, 1, &count);
EXPECT_EGL_TRUE(result && (count > 0));
return result;
}
bool createContext(EGLDisplay dpy, EGLConfig config, EGLContext *context)
{
bool result = false;
EGLint attribs[] = {EGL_CONTEXT_MAJOR_VERSION, 3, EGL_NONE};
*context = eglCreateContext(dpy, config, nullptr, attribs);
result = (*context != EGL_NO_CONTEXT);
EXPECT_TRUE(result);
return result;
}
bool createPbufferSurface(EGLDisplay dpy,
EGLConfig config,
EGLint width,
EGLint height,
EGLSurface *surface)
{
bool result = false;
EGLint attribs[] = {EGL_WIDTH, width, EGL_HEIGHT, height, EGL_NONE};
*surface = eglCreatePbufferSurface(dpy, config, attribs);
result = (*surface != EGL_NO_SURFACE);
EXPECT_TRUE(result);
return result;
}
EGLContext mContexts[2];
GLuint mTexture;
};
// Test that calling eglDeleteContext on a context that is not current succeeds.
TEST_P(EGLMultiContextTest, TestContextDestroySimple)
{
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLContext context1 = window->createContext(EGL_NO_CONTEXT, nullptr);
EGLContext context2 = window->createContext(EGL_NO_CONTEXT, nullptr);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, context1));
EXPECT_EGL_TRUE(eglDestroyContext(dpy, context2));
EXPECT_EGL_SUCCESS();
// Cleanup
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_TRUE(eglDestroyContext(dpy, context1));
EXPECT_EGL_SUCCESS();
}
// Test that an error is generated when using EGL objects after calling eglTerminate.
TEST_P(EGLMultiContextTest, NegativeTestAfterEglTerminate)
{
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLConfig config = EGL_NO_CONFIG_KHR;
EXPECT_TRUE(chooseConfig(dpy, &config));
EGLContext context = EGL_NO_CONTEXT;
EXPECT_TRUE(createContext(dpy, config, &context));
ASSERT_EGL_SUCCESS() << "eglCreateContext failed.";
EGLSurface drawSurface = EGL_NO_SURFACE;
EXPECT_TRUE(createPbufferSurface(dpy, config, 2560, 1080, &drawSurface));
ASSERT_EGL_SUCCESS() << "eglCreatePbufferSurface failed.";
EGLSurface readSurface = EGL_NO_SURFACE;
EXPECT_TRUE(createPbufferSurface(dpy, config, 2560, 1080, &readSurface));
ASSERT_EGL_SUCCESS() << "eglCreatePbufferSurface failed.";
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, drawSurface, readSurface, context));
EXPECT_EGL_SUCCESS();
// Terminate the display
EXPECT_EGL_TRUE(eglTerminate(dpy));
EXPECT_EGL_SUCCESS();
// Try to use invalid handles
EGLint value;
eglQuerySurface(dpy, drawSurface, EGL_SWAP_BEHAVIOR, &value);
EXPECT_EGL_ERROR(EGL_BAD_SURFACE);
eglQuerySurface(dpy, readSurface, EGL_HEIGHT, &value);
EXPECT_EGL_ERROR(EGL_BAD_SURFACE);
// Cleanup
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
window->destroyGL();
}
// Test that a compute shader running in one thread will still work when rendering is happening in
// another thread (with non-shared contexts). The non-shared context will still share a Vulkan
// command buffer.
TEST_P(EGLMultiContextTest, ComputeShaderOkayWithRendering)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
ANGLE_SKIP_TEST_IF(!isVulkanRenderer());
ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 || getClientMinorVersion() < 1);
// Initialize contexts
EGLWindow *window = getEGLWindow();
EGLDisplay dpy = window->getDisplay();
EGLConfig config = window->getConfig();
constexpr size_t kThreadCount = 2;
EGLSurface surface[kThreadCount] = {EGL_NO_SURFACE, EGL_NO_SURFACE};
EGLContext ctx[kThreadCount] = {EGL_NO_CONTEXT, EGL_NO_CONTEXT};
EGLint pbufferAttributes[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
for (size_t t = 0; t < kThreadCount; ++t)
{
surface[t] = eglCreatePbufferSurface(dpy, config, pbufferAttributes);
EXPECT_EGL_SUCCESS();
ctx[t] = window->createContext(EGL_NO_CONTEXT, nullptr);
EXPECT_NE(EGL_NO_CONTEXT, ctx[t]);
}
// Synchronization tools to ensure the two threads are interleaved as designed by this test.
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Thread0Start,
Thread0DispatchedCompute,
Thread1Drew,
Thread0DispatchedComputeAgain,
Finish,
Abort,
};
Step currentStep = Step::Thread0Start;
// This first thread dispatches a compute shader. It immediately starts.
std::thread deletingThread = std::thread([&]() {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface[0], surface[0], ctx[0]));
EXPECT_EGL_SUCCESS();
// Potentially wait to be signalled to start.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0Start));
// Wake up and do next step: Create, detach, and dispatch a compute shader program.
constexpr char kCS[] = R"(#version 310 es
layout(local_size_x=1) in;
void main()
{
})";
GLuint computeProgram = glCreateProgram();
GLuint cs = CompileShader(GL_COMPUTE_SHADER, kCS);
EXPECT_NE(0u, cs);
glAttachShader(computeProgram, cs);
glDeleteShader(cs);
glLinkProgram(computeProgram);
GLint linkStatus;
glGetProgramiv(computeProgram, GL_LINK_STATUS, &linkStatus);
EXPECT_GL_TRUE(linkStatus);
glDetachShader(computeProgram, cs);
EXPECT_GL_NO_ERROR();
glUseProgram(computeProgram);
glDispatchCompute(8, 4, 2);
EXPECT_GL_NO_ERROR();
// Signal the second thread and wait for it to draw and flush.
threadSynchronization.nextStep(Step::Thread0DispatchedCompute);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1Drew));
// Wake up and do next step: Dispatch the same compute shader again.
glDispatchCompute(8, 4, 2);
// Signal the second thread and wait for it to draw and flush again.
threadSynchronization.nextStep(Step::Thread0DispatchedComputeAgain);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
// Wake up and do next step: Dispatch the same compute shader again, and force flush the
// underlying command buffer.
glDispatchCompute(8, 4, 2);
glFinish();
// Clean-up and exit this thread.
EXPECT_GL_NO_ERROR();
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
});
// This second thread renders. It starts once the other thread does its first nextStep()
std::thread continuingThread = std::thread([&]() {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, surface[1], surface[1], ctx[1]));
EXPECT_EGL_SUCCESS();
// Wait for first thread to create and dispatch a compute shader.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0DispatchedCompute));
// Wake up and do next step: Create graphics resources, draw, and force flush the
// underlying command buffer.
GLTexture texture;
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
GLRenderbuffer renderbuffer;
GLFramebuffer fbo;
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
constexpr int kRenderbufferSize = 4;
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kRenderbufferSize, kRenderbufferSize);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
renderbuffer);
glBindTexture(GL_TEXTURE_2D, texture);
GLProgram graphicsProgram;
graphicsProgram.makeRaster(essl1_shaders::vs::Texture2D(), essl1_shaders::fs::Texture2D());
ASSERT_TRUE(graphicsProgram.valid());
drawQuad(graphicsProgram.get(), essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
// Signal the first thread and wait for it to dispatch a compute shader again.
threadSynchronization.nextStep(Step::Thread1Drew);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0DispatchedComputeAgain));
// Wake up and do next step: Draw and force flush the underlying command buffer again.
drawQuad(graphicsProgram.get(), essl1_shaders::PositionAttrib(), 0.5f);
glFinish();
// Signal the first thread and wait exit this thread.
threadSynchronization.nextStep(Step::Finish);
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
});
deletingThread.join();
continuingThread.join();
ASSERT_NE(currentStep, Step::Abort);
// Clean up
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
for (size_t t = 0; t < kThreadCount; ++t)
{
eglDestroySurface(dpy, surface[t]);
eglDestroyContext(dpy, ctx[t]);
}
}
// Test that repeated EGL init + terminate with improper cleanup doesn't cause an OOM crash.
// To reproduce the OOM error -
// 1. Increase the loop count to a large number
// 2. Run the test without the rest of the code in change 3329273
TEST_P(EGLMultiContextTest, RepeatedEglInitAndTerminate)
{
// GL and GLES drivers don't seem to perform appropriate cleanup
// SwiftShader fails with "Extension not supported" error on the bots
ANGLE_SKIP_TEST_IF(!IsVulkan() || isSwiftshader());
// Release all resources in parent thread
getEGLWindow()->destroyGL();
EGLDisplay dpy;
EGLSurface srf;
EGLContext ctx;
EGLint dispattrs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, GetParam().getRenderer(), EGL_NONE};
for (int i = 0; i < 100; i++)
{
std::thread thread = std::thread([&]() {
dpy = eglGetPlatformDisplayEXT(
EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY), dispattrs);
EXPECT_TRUE(dpy != EGL_NO_DISPLAY);
EXPECT_EGL_TRUE(eglInitialize(dpy, nullptr, nullptr));
EGLConfig config = EGL_NO_CONFIG_KHR;
EXPECT_TRUE(chooseConfig(dpy, &config));
EXPECT_TRUE(createPbufferSurface(dpy, config, 2560, 1080, &srf));
ASSERT_EGL_SUCCESS() << "eglCreatePbufferSurface failed.";
EXPECT_TRUE(createContext(dpy, config, &ctx));
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, srf, srf, ctx));
// Clear and read back to make sure thread uses context.
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
eglTerminate(dpy);
EXPECT_EGL_SUCCESS();
eglReleaseThread();
EXPECT_EGL_SUCCESS();
dpy = EGL_NO_DISPLAY;
srf = EGL_NO_SURFACE;
ctx = EGL_NO_CONTEXT;
});
thread.join();
}
}
} // anonymous namespace
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EGLMultiContextTest);
ANGLE_INSTANTIATE_TEST_ES31(EGLMultiContextTest);