Hash :
d2c01d2c
Author :
Date :
2021-09-17T12:57:14
GL: Allow selecting virtualization groups at context creation Rewrite EGL_ANGLE_platform_angle_context_virtualization to EGL_ANGLE_context_virtualization, changing the context virtualization parameter to an identifier for what virtualization group the frontend context should be added to. This allows ANGLE's GL backend to be used by multiple threads if the user creates contexts with different virtualization groups. Bug: angleproject:6406 Change-Id: I7414d4705ce10bdf63a9b824043d5dd040dad875 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3169193 Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org>
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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
//
// Copyright 2016 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.
//
// EGLContextSharingTest.cpp:
// Tests relating to 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"
#include "util/OSWindow.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 EGLContextSharingTest : public ANGLETest
{
public:
EGLContextSharingTest() : 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();
}
EGLContext mContexts[2] = {EGL_NO_CONTEXT, EGL_NO_CONTEXT};
GLuint mTexture;
};
class EGLContextSharingTestNoFixture : public EGLContextSharingTest
{
public:
EGLContextSharingTestNoFixture() : EGLContextSharingTest() {}
void testSetUp() override
{
mOsWindow = OSWindow::New();
mMajorVersion = GetParam().majorVersion;
}
void testTearDown() override
{
if (mDisplay != EGL_NO_DISPLAY)
{
eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (mSurface != EGL_NO_SURFACE)
{
eglDestroySurface(mDisplay, mSurface);
ASSERT_EGL_SUCCESS();
mSurface = EGL_NO_SURFACE;
}
for (auto &context : mContexts)
{
SafeDestroyContext(mDisplay, context);
}
eglTerminate(mDisplay);
mDisplay = EGL_NO_DISPLAY;
ASSERT_EGL_SUCCESS();
eglReleaseThread();
ASSERT_EGL_SUCCESS();
}
mOsWindow->destroy();
OSWindow::Delete(&mOsWindow);
ASSERT_EGL_SUCCESS() << "Error during test TearDown";
}
bool chooseConfig(EGLConfig *config) const
{
bool result = false;
EGLint count = 0;
EGLint clientVersion = mMajorVersion == 3 ? EGL_OPENGL_ES3_BIT : EGL_OPENGL_ES2_BIT;
EGLint attribs[] = {EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_ALPHA_SIZE,
0,
EGL_RENDERABLE_TYPE,
clientVersion,
EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_NONE};
result = eglChooseConfig(mDisplay, attribs, config, 1, &count);
EXPECT_EGL_TRUE(result && (count > 0));
return result;
}
bool createContext(EGLConfig config, EGLContext *context)
{
bool result = false;
EGLint attribs[] = {EGL_CONTEXT_MAJOR_VERSION, mMajorVersion, EGL_NONE};
*context = eglCreateContext(mDisplay, config, nullptr, attribs);
result = (*context != EGL_NO_CONTEXT);
EXPECT_TRUE(result);
return result;
}
bool createWindowSurface(EGLConfig config, EGLNativeWindowType win, EGLSurface *surface)
{
bool result = false;
EGLint attribs[] = {EGL_NONE};
*surface = eglCreateWindowSurface(mDisplay, config, win, attribs);
result = (*surface != EGL_NO_SURFACE);
EXPECT_TRUE(result);
return result;
}
OSWindow *mOsWindow;
EGLDisplay mDisplay = EGL_NO_DISPLAY;
EGLSurface mSurface = EGL_NO_SURFACE;
const EGLint kWidth = 64;
const EGLint kHeight = 64;
EGLint mMajorVersion = 0;
};
// Tests that creating resources works after freeing the share context.
TEST_P(EGLContextSharingTest, BindTextureAfterShareContextFree)
{
EGLDisplay display = getEGLWindow()->getDisplay();
EGLConfig config = getEGLWindow()->getConfig();
EGLSurface surface = getEGLWindow()->getSurface();
const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION,
getEGLWindow()->getClientMajorVersion(), EGL_NONE};
mContexts[0] = eglCreateContext(display, config, nullptr, contextAttribs);
ASSERT_EGL_SUCCESS();
ASSERT_TRUE(mContexts[0] != EGL_NO_CONTEXT);
mContexts[1] = eglCreateContext(display, config, mContexts[1], contextAttribs);
ASSERT_EGL_SUCCESS();
ASSERT_TRUE(mContexts[1] != EGL_NO_CONTEXT);
ASSERT_EGL_TRUE(SafeDestroyContext(display, mContexts[0]));
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[1]));
ASSERT_EGL_SUCCESS();
glGenTextures(1, &mTexture);
glBindTexture(GL_TEXTURE_2D, mTexture);
ASSERT_GL_NO_ERROR();
}
// Tests the creation of contexts using EGL_ANGLE_display_texture_share_group
TEST_P(EGLContextSharingTest, DisplayShareGroupContextCreation)
{
EGLDisplay display = getEGLWindow()->getDisplay();
EGLConfig config = getEGLWindow()->getConfig();
const EGLint inShareGroupContextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2, EGL_DISPLAY_TEXTURE_SHARE_GROUP_ANGLE, EGL_TRUE, EGL_NONE};
// Check whether extension's supported to avoid clearing the EGL error state
// after failed context creation.
bool extensionEnabled =
IsEGLDisplayExtensionEnabled(display, "EGL_ANGLE_display_texture_share_group");
// Test creating two contexts in the global share group
mContexts[0] = eglCreateContext(display, config, nullptr, inShareGroupContextAttribs);
mContexts[1] = eglCreateContext(display, config, mContexts[1], inShareGroupContextAttribs);
if (!extensionEnabled)
{
// Make sure an error is generated and early-exit
ASSERT_EGL_ERROR(EGL_BAD_ATTRIBUTE);
ASSERT_EQ(EGL_NO_CONTEXT, mContexts[0]);
return;
}
ASSERT_EGL_SUCCESS();
ASSERT_NE(EGL_NO_CONTEXT, mContexts[0]);
ASSERT_NE(EGL_NO_CONTEXT, mContexts[1]);
eglDestroyContext(display, mContexts[0]);
mContexts[0] = EGL_NO_CONTEXT;
// Try creating a context that is not in the global share group but tries to share with a
// context that is
const EGLint notInShareGroupContextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2, EGL_DISPLAY_TEXTURE_SHARE_GROUP_ANGLE, EGL_FALSE, EGL_NONE};
mContexts[0] = eglCreateContext(display, config, mContexts[1], notInShareGroupContextAttribs);
ASSERT_EGL_ERROR(EGL_BAD_ATTRIBUTE);
ASSERT_TRUE(mContexts[0] == EGL_NO_CONTEXT);
}
// Tests the sharing of textures using EGL_ANGLE_display_texture_share_group
TEST_P(EGLContextSharingTest, DisplayShareGroupObjectSharing)
{
EGLDisplay display = getEGLWindow()->getDisplay();
if (!IsEGLDisplayExtensionEnabled(display, "EGL_ANGLE_display_texture_share_group"))
{
std::cout << "Test skipped because EGL_ANGLE_display_texture_share_group is not present."
<< std::endl;
return;
}
EGLConfig config = getEGLWindow()->getConfig();
EGLSurface surface = getEGLWindow()->getSurface();
const EGLint inShareGroupContextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2, EGL_DISPLAY_TEXTURE_SHARE_GROUP_ANGLE, EGL_TRUE, EGL_NONE};
// Create two contexts in the global share group but not in the same context share group
mContexts[0] = eglCreateContext(display, config, nullptr, inShareGroupContextAttribs);
mContexts[1] = eglCreateContext(display, config, nullptr, inShareGroupContextAttribs);
ASSERT_EGL_SUCCESS();
ASSERT_NE(EGL_NO_CONTEXT, mContexts[0]);
ASSERT_NE(EGL_NO_CONTEXT, mContexts[1]);
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[0]));
ASSERT_EGL_SUCCESS();
// Create a texture and buffer in ctx 0
GLTexture textureFromCtx0;
glBindTexture(GL_TEXTURE_2D, textureFromCtx0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
ASSERT_GL_TRUE(glIsTexture(textureFromCtx0));
GLBuffer bufferFromCtx0;
glBindBuffer(GL_ARRAY_BUFFER, bufferFromCtx0);
glBufferData(GL_ARRAY_BUFFER, 1, nullptr, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
ASSERT_GL_TRUE(glIsBuffer(bufferFromCtx0));
ASSERT_GL_NO_ERROR();
// Switch to context 1 and verify that the texture is accessible but the buffer is not
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[1]));
ASSERT_EGL_SUCCESS();
ASSERT_GL_TRUE(glIsTexture(textureFromCtx0));
ASSERT_GL_FALSE(glIsBuffer(bufferFromCtx0));
ASSERT_GL_NO_ERROR();
// Call readpixels on the texture to verify that the backend has proper support
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureFromCtx0, 0);
GLubyte pixel[4];
glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
ASSERT_GL_NO_ERROR();
// Switch back to context 0 and delete the buffer
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[0]));
ASSERT_EGL_SUCCESS();
}
// Tests that shared textures using EGL_ANGLE_display_texture_share_group are released when the last
// context is destroyed
TEST_P(EGLContextSharingTest, DisplayShareGroupReleasedWithLastContext)
{
EGLDisplay display = getEGLWindow()->getDisplay();
if (!IsEGLDisplayExtensionEnabled(display, "EGL_ANGLE_display_texture_share_group"))
{
std::cout << "Test skipped because EGL_ANGLE_display_texture_share_group is not present."
<< std::endl;
return;
}
EGLConfig config = getEGLWindow()->getConfig();
EGLSurface surface = getEGLWindow()->getSurface();
const EGLint inShareGroupContextAttribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2, EGL_DISPLAY_TEXTURE_SHARE_GROUP_ANGLE, EGL_TRUE, EGL_NONE};
// Create two contexts in the global share group but not in the same context share group
mContexts[0] = eglCreateContext(display, config, nullptr, inShareGroupContextAttribs);
mContexts[1] = eglCreateContext(display, config, nullptr, inShareGroupContextAttribs);
// Create a texture and buffer in ctx 0
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[0]));
GLTexture textureFromCtx0;
glBindTexture(GL_TEXTURE_2D, textureFromCtx0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
ASSERT_GL_TRUE(glIsTexture(textureFromCtx0));
// Switch to context 1 and verify that the texture is accessible
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[1]));
ASSERT_GL_TRUE(glIsTexture(textureFromCtx0));
// Destroy both contexts, the texture should be cleaned up automatically
ASSERT_EGL_TRUE(eglDestroyContext(display, mContexts[0]));
mContexts[0] = EGL_NO_CONTEXT;
ASSERT_EGL_TRUE(eglDestroyContext(display, mContexts[1]));
mContexts[1] = EGL_NO_CONTEXT;
// Unmake current, so the context can be released.
ASSERT_EGL_TRUE(eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
// Create a new context and verify it cannot access the texture previously created
mContexts[0] = eglCreateContext(display, config, nullptr, inShareGroupContextAttribs);
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[0]));
ASSERT_GL_FALSE(glIsTexture(textureFromCtx0));
}
// Tests that deleting an object on one Context doesn't destroy it ahead-of-time. Mostly focused
// on the Vulkan back-end where we manage object lifetime manually.
TEST_P(EGLContextSharingTest, TextureLifetime)
{
EGLWindow *eglWindow = getEGLWindow();
EGLConfig config = getEGLWindow()->getConfig();
EGLDisplay display = getEGLWindow()->getDisplay();
// Create a pbuffer surface for use with a shared context.
EGLSurface surface = eglWindow->getSurface();
EGLContext mainContext = eglWindow->getContext();
// Initialize a shared context.
mContexts[0] = eglCreateContext(display, config, mainContext, nullptr);
ASSERT_NE(mContexts[0], EGL_NO_CONTEXT);
// Create a Texture on the shared context.
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[0]));
constexpr GLsizei kTexSize = 2;
const GLColor kTexData[kTexSize * kTexSize] = {GLColor::red, GLColor::green, GLColor::blue,
GLColor::yellow};
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTexSize, kTexSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
kTexData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Make the main Context current and draw with the texture.
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mainContext));
glBindTexture(GL_TEXTURE_2D, tex);
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Texture2D(), essl1_shaders::fs::Texture2D());
glUseProgram(program);
// No uniform update because the update seems to hide the error on Vulkan.
// Enqueue the draw call.
drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
EXPECT_GL_NO_ERROR();
// Delete the texture in the main context to orphan it.
// Do not read back the data to keep the commands in the graph.
tex.reset();
// Bind and delete the test context. This should trigger texture garbage collection.
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[0]));
SafeDestroyContext(display, mContexts[0]);
// Bind the main context to clean up the test.
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mainContext));
}
// Tests that deleting an object on one Context doesn't destroy it ahead-of-time. Mostly focused
// on the Vulkan back-end where we manage object lifetime manually.
TEST_P(EGLContextSharingTest, SamplerLifetime)
{
EGLWindow *eglWindow = getEGLWindow();
EGLConfig config = getEGLWindow()->getConfig();
EGLDisplay display = getEGLWindow()->getDisplay();
ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3);
ANGLE_SKIP_TEST_IF(!IsEGLDisplayExtensionEnabled(display, "EGL_KHR_create_context"));
// Create a pbuffer surface for use with a shared context.
EGLSurface surface = eglWindow->getSurface();
EGLContext mainContext = eglWindow->getContext();
std::vector<EGLint> contextAttributes;
contextAttributes.push_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
contextAttributes.push_back(getClientMajorVersion());
contextAttributes.push_back(EGL_NONE);
// Initialize a shared context.
mContexts[0] = eglCreateContext(display, config, mainContext, contextAttributes.data());
ASSERT_NE(mContexts[0], EGL_NO_CONTEXT);
// Create a Texture on the shared context. Also create a Sampler object.
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[0]));
constexpr GLsizei kTexSize = 2;
const GLColor kTexData[kTexSize * kTexSize] = {GLColor::red, GLColor::green, GLColor::blue,
GLColor::yellow};
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTexSize, kTexSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
kTexData);
GLSampler sampler;
glBindSampler(0, sampler);
glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Make the main Context current and draw with the texture and sampler.
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mainContext));
glBindTexture(GL_TEXTURE_2D, tex);
glBindSampler(0, sampler);
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Texture2D(), essl1_shaders::fs::Texture2D());
glUseProgram(program);
// No uniform update because the update seems to hide the error on Vulkan.
// Enqueue the draw call.
drawQuad(program, essl1_shaders::PositionAttrib(), 0.5f);
EXPECT_GL_NO_ERROR();
// Delete the texture and sampler in the main context to orphan them.
// Do not read back the data to keep the commands in the graph.
tex.reset();
sampler.reset();
// Bind and delete the test context. This should trigger texture garbage collection.
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mContexts[0]));
SafeDestroyContext(display, mContexts[0]);
// Bind the main context to clean up the test.
ASSERT_EGL_TRUE(eglMakeCurrent(display, surface, surface, mainContext));
}
// Test that deleting an object reading from a shared object in one context doesn't cause the other
// context to crash. Mostly focused on the Vulkan back-end where we track resource dependencies in
// a graph.
TEST_P(EGLContextSharingTest, DeleteReaderOfSharedTexture)
{
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
// GL Fences require GLES 3.0+
ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3);
// 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(t == 0 ? EGL_NO_CONTEXT : ctx[0], nullptr);
EXPECT_NE(EGL_NO_CONTEXT, ctx[t]);
}
// Initialize test resources. They are done outside the threads to reduce the sources of
// errors and thus deadlock-free teardown.
ASSERT_EGL_TRUE(eglMakeCurrent(dpy, surface[1], surface[1], ctx[1]));
// Shared texture to read from.
constexpr GLsizei kTexSize = 1;
const GLColor kTexData = GLColor::red;
GLTexture sharedTex;
glBindTexture(GL_TEXTURE_2D, sharedTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTexSize, kTexSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
&kTexData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Resources for each context.
GLRenderbuffer renderbuffer[kThreadCount];
GLFramebuffer fbo[kThreadCount];
GLProgram program[kThreadCount];
for (size_t t = 0; t < kThreadCount; ++t)
{
ASSERT_EGL_TRUE(eglMakeCurrent(dpy, surface[t], surface[t], ctx[t]));
glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer[t]);
constexpr int kRenderbufferSize = 4;
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kRenderbufferSize, kRenderbufferSize);
glBindFramebuffer(GL_FRAMEBUFFER, fbo[t]);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
renderbuffer[t]);
glBindTexture(GL_TEXTURE_2D, sharedTex);
program[t].makeRaster(essl1_shaders::vs::Texture2D(), essl1_shaders::fs::Texture2D());
ASSERT_TRUE(program[t].valid());
}
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
// Synchronization tools to ensure the two threads are interleaved as designed by this test.
std::mutex mutex;
std::condition_variable condVar;
std::atomic<GLsync> deletingThreadSyncObj;
std::atomic<GLsync> continuingThreadSyncObj;
enum class Step
{
Start,
Thread0Draw,
Thread1Draw,
Thread0Delete,
Finish,
Abort,
};
Step currentStep = Step::Start;
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();
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
// Draw using the shared texture.
drawQuad(program[0].get(), essl1_shaders::PositionAttrib(), 0.5f);
deletingThreadSyncObj = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_GL_NO_ERROR();
// Force the fence to be created
glFlush();
// Wait for the other thread to also draw using the shared texture.
threadSynchronization.nextStep(Step::Thread0Draw);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1Draw));
ASSERT_TRUE(continuingThreadSyncObj != nullptr);
glWaitSync(continuingThreadSyncObj, 0, GL_TIMEOUT_IGNORED);
ASSERT_GL_NO_ERROR();
glDeleteSync(continuingThreadSyncObj);
ASSERT_GL_NO_ERROR();
continuingThreadSyncObj = nullptr;
// Delete this thread's framebuffer (reader of the shared texture).
fbo[0].reset();
// Wait for the other thread to use the shared texture again before unbinding the
// context (so no implicit flush happens).
threadSynchronization.nextStep(Step::Thread0Delete);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
EXPECT_GL_NO_ERROR();
EXPECT_EGL_TRUE(eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_EGL_SUCCESS();
});
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 draw using the shared texture.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0Draw));
ASSERT_TRUE(deletingThreadSyncObj != nullptr);
glWaitSync(deletingThreadSyncObj, 0, GL_TIMEOUT_IGNORED);
ASSERT_GL_NO_ERROR();
glDeleteSync(deletingThreadSyncObj);
ASSERT_GL_NO_ERROR();
deletingThreadSyncObj = nullptr;
// Draw using the shared texture.
drawQuad(program[0].get(), essl1_shaders::PositionAttrib(), 0.5f);
continuingThreadSyncObj = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
ASSERT_GL_NO_ERROR();
// Force the fence to be created
glFlush();
// Wait for the other thread to delete its framebuffer.
threadSynchronization.nextStep(Step::Thread1Draw);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0Delete));
// Write to the shared texture differently, so a dependency is created from the previous
// readers of the shared texture (the two framebuffers of the two threads) to the new
// commands being recorded for the shared texture.
//
// If the backend attempts to create a dependency from nodes associated with the
// previous readers of the texture to the new node that will contain the following
// commands, there will be a use-after-free error.
const GLColor kTexData2 = GLColor::green;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kTexSize, kTexSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
&kTexData2);
drawQuad(program[0].get(), essl1_shaders::PositionAttrib(), 0.5f);
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
for (size_t t = 0; t < kThreadCount; ++t)
{
eglDestroySurface(dpy, surface[t]);
eglDestroyContext(dpy, ctx[t]);
}
}
// Test that eglTerminate() with a thread doesn't cause other threads to crash.
TEST_P(EGLContextSharingTestNoFixture, EglTerminateMultiThreaded)
{
// http://anglebug.com/6208
// The following EGL calls led 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)
EGLint dispattrs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, GetParam().getRenderer(), EGL_NONE};
mDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,
reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY), dispattrs);
EXPECT_TRUE(mDisplay != EGL_NO_DISPLAY);
EXPECT_EGL_TRUE(eglInitialize(mDisplay, nullptr, nullptr));
EGLConfig config = EGL_NO_CONFIG_KHR;
EXPECT_TRUE(chooseConfig(&config));
mOsWindow->initialize("EGLContextSharingTestNoFixture", kWidth, kHeight);
EXPECT_TRUE(createWindowSurface(config, mOsWindow->getNativeWindow(), &mSurface));
ASSERT_EGL_SUCCESS() << "eglCreateWindowSurface failed.";
EXPECT_TRUE(createContext(config, &mContexts[0]));
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, mSurface, mSurface, mContexts[0]));
// Must be after the eglMakeCurrent() so renderer string is initialized.
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
// TODO(http://www.anglebug.com/6304): Fails with OpenGL ES backend.
ANGLE_SKIP_TEST_IF(IsOpenGLES());
// Synchronization tools to ensure the two threads are interleaved as designed by this test.
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0Clear,
Thread1Terminate,
Thread0MakeCurrentContext1,
Finish,
Abort,
};
Step currentStep = Step::Start;
std::thread thread0 = std::thread([&]() {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, mSurface, mSurface, mContexts[0]));
// Clear and read back to make sure thread 0 uses context 0.
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
// Wait for thread 1 to clear.
threadSynchronization.nextStep(Step::Thread0Clear);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1Terminate));
// First Display was terminated, so we need to create a new one to create a new Context.
mDisplay = eglGetPlatformDisplayEXT(
EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY), dispattrs);
EXPECT_TRUE(mDisplay != EGL_NO_DISPLAY);
EXPECT_EGL_TRUE(eglInitialize(mDisplay, nullptr, nullptr));
config = EGL_NO_CONFIG_KHR;
EXPECT_TRUE(chooseConfig(&config));
EXPECT_TRUE(createContext(config, &mContexts[1]));
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, mSurface, mSurface, mContexts[1]));
// Clear and read back to make sure thread 0 uses context 1.
glClearColor(1.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(0, 0, 255, 255, 0, 255);
// Cleanup
EXPECT_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_TRUE(SafeDestroyContext(mDisplay, mContexts[1]));
eglDestroySurface(mDisplay, mSurface);
mSurface = EGL_NO_SURFACE;
eglTerminate(mDisplay);
mDisplay = EGL_NO_DISPLAY;
EXPECT_EGL_SUCCESS();
threadSynchronization.nextStep(Step::Thread0MakeCurrentContext1);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
});
std::thread thread1 = std::thread([&]() {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
// Wait for thread 0 to clear.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0Clear));
// Destroy context 0 while thread1 has it current.
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_TRUE(SafeDestroyContext(mDisplay, mContexts[0]));
EXPECT_EGL_SUCCESS();
eglTerminate(mDisplay);
mDisplay = EGL_NO_DISPLAY;
EXPECT_EGL_SUCCESS();
// Wait for the thread 0 to make a new context and glClear().
threadSynchronization.nextStep(Step::Thread1Terminate);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0MakeCurrentContext1));
threadSynchronization.nextStep(Step::Finish);
});
thread0.join();
thread1.join();
ASSERT_NE(currentStep, Step::Abort);
}
// Test that eglDestoryContext() can be called multiple times on the same Context without causing
// errors.
TEST_P(EGLContextSharingTestNoFixture, EglDestoryContextManyTimesSameContext)
{
EGLint dispattrs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, GetParam().getRenderer(), EGL_NONE};
mDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,
reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY), dispattrs);
EXPECT_TRUE(mDisplay != EGL_NO_DISPLAY);
EXPECT_EGL_TRUE(eglInitialize(mDisplay, nullptr, nullptr));
EGLConfig config = EGL_NO_CONFIG_KHR;
EXPECT_TRUE(chooseConfig(&config));
mOsWindow->initialize("EGLContextSharingTestNoFixture", kWidth, kHeight);
EXPECT_TRUE(createWindowSurface(config, mOsWindow->getNativeWindow(), &mSurface));
ASSERT_EGL_SUCCESS() << "eglCreateWindowSurface failed.";
EXPECT_TRUE(createContext(config, &mContexts[0]));
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, mSurface, mSurface, mContexts[0]));
// Must be after the eglMakeCurrent() so renderer string is initialized.
ANGLE_SKIP_TEST_IF(!platformSupportsMultithreading());
// TODO(http://www.anglebug.com/6304): Fails with OpenGL ES backend.
ANGLE_SKIP_TEST_IF(IsOpenGLES());
// Synchronization tools to ensure the two threads are interleaved as designed by this test.
std::mutex mutex;
std::condition_variable condVar;
enum class Step
{
Start,
Thread0Clear,
Thread1Terminate,
Thread0MakeCurrentContext1,
Finish,
Abort,
};
Step currentStep = Step::Start;
std::thread thread0 = std::thread([&]() {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Start));
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, mSurface, mSurface, mContexts[0]));
// Clear and read back to make sure thread 0 uses context 0.
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
// Wait for thread 1 to clear.
threadSynchronization.nextStep(Step::Thread0Clear);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread1Terminate));
// First Display was terminated, so we need to create a new one to create a new Context.
mDisplay = eglGetPlatformDisplayEXT(
EGL_PLATFORM_ANGLE_ANGLE, reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY), dispattrs);
EXPECT_TRUE(mDisplay != EGL_NO_DISPLAY);
EXPECT_EGL_TRUE(eglInitialize(mDisplay, nullptr, nullptr));
config = EGL_NO_CONFIG_KHR;
EXPECT_TRUE(chooseConfig(&config));
EXPECT_TRUE(createContext(config, &mContexts[1]));
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, mSurface, mSurface, mContexts[1]));
// Clear and read back to make sure thread 0 uses context 1.
glClearColor(1.0, 1.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(0, 0, 255, 255, 0, 255);
// Cleanup
EXPECT_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
EXPECT_TRUE(SafeDestroyContext(mDisplay, mContexts[1]));
eglDestroySurface(mDisplay, mSurface);
mSurface = EGL_NO_SURFACE;
eglTerminate(mDisplay);
mDisplay = EGL_NO_DISPLAY;
EXPECT_EGL_SUCCESS();
threadSynchronization.nextStep(Step::Thread0MakeCurrentContext1);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Finish));
});
std::thread thread1 = std::thread([&]() {
ThreadSynchronization<Step> threadSynchronization(¤tStep, &mutex, &condVar);
// Wait for thread 0 to clear.
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0Clear));
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
// Destroy context 0 5 times while thread1 has it current.
EXPECT_EGL_TRUE(eglDestroyContext(mDisplay, mContexts[0]));
EXPECT_EGL_TRUE(eglDestroyContext(mDisplay, mContexts[0]));
EXPECT_EGL_TRUE(eglDestroyContext(mDisplay, mContexts[0]));
EXPECT_EGL_TRUE(eglDestroyContext(mDisplay, mContexts[0]));
EXPECT_EGL_TRUE(eglDestroyContext(mDisplay, mContexts[0]));
mContexts[0] = EGL_NO_CONTEXT;
eglTerminate(mDisplay);
mDisplay = EGL_NO_DISPLAY;
EXPECT_EGL_SUCCESS();
// Wait for the thread 0 to make a new context and glClear().
threadSynchronization.nextStep(Step::Thread1Terminate);
ASSERT_TRUE(threadSynchronization.waitForStep(Step::Thread0MakeCurrentContext1));
threadSynchronization.nextStep(Step::Finish);
});
thread0.join();
thread1.join();
ASSERT_NE(currentStep, Step::Abort);
}
// Test that eglTerminate() can be called multiple times on the same Display while Contexts are
// still current without causing errors.
TEST_P(EGLContextSharingTestNoFixture, EglTerminateMultipleTimes)
{
// https://bugs.chromium.org/p/skia/issues/detail?id=12413#c4
// The following sequence caused a crash with the D3D backend in the Skia infra:
// eglDestroyContext(ctx0)
// eglDestroySurface(srf0)
// eglTerminate(shared-display)
// eglDestroyContext(ctx1) // completes the cleanup from the above terminate
// eglDestroySurface(srf1)
// eglTerminate(shared-display)
EGLint dispattrs[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, GetParam().getRenderer(), EGL_NONE};
mDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE,
reinterpret_cast<void *>(EGL_DEFAULT_DISPLAY), dispattrs);
EXPECT_TRUE(mDisplay != EGL_NO_DISPLAY);
EXPECT_EGL_TRUE(eglInitialize(mDisplay, nullptr, nullptr));
EGLConfig config = EGL_NO_CONFIG_KHR;
EXPECT_TRUE(chooseConfig(&config));
mOsWindow->initialize("EGLContextSharingTestNoFixture", kWidth, kHeight);
EXPECT_TRUE(createWindowSurface(config, mOsWindow->getNativeWindow(), &mSurface));
EXPECT_TRUE(mSurface != EGL_NO_SURFACE);
ASSERT_EGL_SUCCESS() << "eglCreateWindowSurface failed.";
EXPECT_TRUE(createContext(config, &mContexts[0]));
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, mSurface, mSurface, mContexts[0]));
EXPECT_TRUE(createContext(config, &mContexts[1]));
EXPECT_EGL_TRUE(eglMakeCurrent(mDisplay, mSurface, mSurface, mContexts[1]));
// Must be after the eglMakeCurrent() so renderer string is initialized.
// TODO(http://www.anglebug.com/6304): Fails with Mac + OpenGL backend.
ANGLE_SKIP_TEST_IF(IsOSX() && IsOpenGL());
EXPECT_TRUE(eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
eglDestroySurface(mDisplay, mSurface);
mSurface = EGL_NO_SURFACE;
EXPECT_EGL_TRUE(eglDestroyContext(mDisplay, mContexts[0]));
mContexts[0] = EGL_NO_CONTEXT;
eglTerminate(mDisplay);
EXPECT_EGL_SUCCESS();
eglDestroyContext(mDisplay, mContexts[1]);
mContexts[1] = EGL_NO_CONTEXT;
ASSERT_EGL_ERROR(EGL_NOT_INITIALIZED);
eglTerminate(mDisplay);
EXPECT_EGL_SUCCESS();
mDisplay = EGL_NO_DISPLAY;
}
} // anonymous namespace
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(EGLContextSharingTest);
ANGLE_INSTANTIATE_TEST(EGLContextSharingTest,
ES2_D3D9(),
ES2_D3D11(),
ES3_D3D11(),
ES2_METAL(),
ES2_OPENGL(),
ES3_OPENGL(),
ES2_VULKAN(),
ES3_VULKAN());
ANGLE_INSTANTIATE_TEST(EGLContextSharingTestNoFixture,
WithNoFixture(ES2_OPENGLES()),
WithNoFixture(ES3_OPENGLES()),
WithNoFixture(ES2_OPENGL()),
WithNoFixture(ES3_OPENGL()),
WithNoFixture(ES2_VULKAN()),
WithNoFixture(ES3_VULKAN()));