xref: /aosp_15_r20/external/angle/src/tests/test_utils/ANGLETest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2015 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // ANGLETest:
7 //   Implementation of common ANGLE testing fixture.
8 //
9 
10 #include "ANGLETest.h"
11 
12 #include <algorithm>
13 #include <cstdlib>
14 
15 #include "common/PackedEnums.h"
16 #include "common/platform.h"
17 #include "gpu_info_util/SystemInfo.h"
18 #include "test_expectations/GPUTestConfig.h"
19 #include "util/EGLWindow.h"
20 #include "util/OSWindow.h"
21 #include "util/random_utils.h"
22 #include "util/test_utils.h"
23 
24 #if defined(ANGLE_PLATFORM_WINDOWS)
25 #    include <VersionHelpers.h>
26 #endif  // defined(ANGLE_PLATFORM_WINDOWS)
27 
28 #if defined(ANGLE_HAS_RAPIDJSON)
29 #    include "test_utils/runner/TestSuite.h"
30 #endif  // defined(ANGLE_HAS_RAPIDJSON)
31 
32 namespace angle
33 {
34 
35 const GLColorRGB GLColorRGB::black(0u, 0u, 0u);
36 const GLColorRGB GLColorRGB::blue(0u, 0u, 255u);
37 const GLColorRGB GLColorRGB::green(0u, 255u, 0u);
38 const GLColorRGB GLColorRGB::red(255u, 0u, 0u);
39 const GLColorRGB GLColorRGB::yellow(255u, 255u, 0);
40 
41 const GLColor GLColor::black            = GLColor(0u, 0u, 0u, 255u);
42 const GLColor GLColor::blue             = GLColor(0u, 0u, 255u, 255u);
43 const GLColor GLColor::cyan             = GLColor(0u, 255u, 255u, 255u);
44 const GLColor GLColor::green            = GLColor(0u, 255u, 0u, 255u);
45 const GLColor GLColor::red              = GLColor(255u, 0u, 0u, 255u);
46 const GLColor GLColor::transparentBlack = GLColor(0u, 0u, 0u, 0u);
47 const GLColor GLColor::white            = GLColor(255u, 255u, 255u, 255u);
48 const GLColor GLColor::yellow           = GLColor(255u, 255u, 0, 255u);
49 const GLColor GLColor::magenta          = GLColor(255u, 0u, 255u, 255u);
50 
51 namespace
52 {
ColorNorm(GLubyte channelValue)53 float ColorNorm(GLubyte channelValue)
54 {
55     return static_cast<float>(channelValue) / 255.0f;
56 }
57 
ColorDenorm(float colorValue)58 GLubyte ColorDenorm(float colorValue)
59 {
60     return static_cast<GLubyte>(colorValue * 255.0f);
61 }
62 
TestPlatform_logError(PlatformMethods * platform,const char * errorMessage)63 void TestPlatform_logError(PlatformMethods *platform, const char *errorMessage)
64 {
65     auto *testPlatformContext = static_cast<TestPlatformContext *>(platform->context);
66     if (testPlatformContext->ignoreMessages)
67         return;
68 
69     GTEST_NONFATAL_FAILURE_(errorMessage);
70 
71     PrintStackBacktrace();
72 }
73 
TestPlatform_logWarning(PlatformMethods * platform,const char * warningMessage)74 void TestPlatform_logWarning(PlatformMethods *platform, const char *warningMessage)
75 {
76     auto *testPlatformContext = static_cast<TestPlatformContext *>(platform->context);
77     if (testPlatformContext->ignoreMessages)
78         return;
79 
80     if (testPlatformContext->warningsAsErrors)
81     {
82         FAIL() << warningMessage;
83     }
84     else
85     {
86 #if !defined(ANGLE_TRACE_ENABLED) && !defined(ANGLE_ENABLE_ASSERTS)
87         // LoggingAnnotator::logMessage() already logs via gl::Trace() under these defines:
88         // https://crsrc.org/c/third_party/angle/src/common/debug.cpp;drc=d7d69375c25df2dc3980e6a4edc5d032ec940efc;l=62
89         std::cerr << "Warning: " << warningMessage << std::endl;
90 #endif
91     }
92 }
93 
TestPlatform_logInfo(PlatformMethods * platform,const char * infoMessage)94 void TestPlatform_logInfo(PlatformMethods *platform, const char *infoMessage) {}
95 
96 const std::array<Vector3, 6> kQuadVertices = {{
97     Vector3(-1.0f, 1.0f, 0.5f),
98     Vector3(-1.0f, -1.0f, 0.5f),
99     Vector3(1.0f, -1.0f, 0.5f),
100     Vector3(-1.0f, 1.0f, 0.5f),
101     Vector3(1.0f, -1.0f, 0.5f),
102     Vector3(1.0f, 1.0f, 0.5f),
103 }};
104 
105 const std::array<Vector3, 4> kIndexedQuadVertices = {{
106     Vector3(-1.0f, 1.0f, 0.5f),
107     Vector3(-1.0f, -1.0f, 0.5f),
108     Vector3(1.0f, -1.0f, 0.5f),
109     Vector3(1.0f, 1.0f, 0.5f),
110 }};
111 
112 constexpr std::array<GLushort, 6> kIndexedQuadIndices = {{0, 1, 2, 0, 2, 3}};
113 
GetColorName(GLColor color)114 const char *GetColorName(GLColor color)
115 {
116     if (color == GLColor::red)
117     {
118         return "Red";
119     }
120 
121     if (color == GLColor::green)
122     {
123         return "Green";
124     }
125 
126     if (color == GLColor::blue)
127     {
128         return "Blue";
129     }
130 
131     if (color == GLColor::white)
132     {
133         return "White";
134     }
135 
136     if (color == GLColor::black)
137     {
138         return "Black";
139     }
140 
141     if (color == GLColor::transparentBlack)
142     {
143         return "Transparent Black";
144     }
145 
146     if (color == GLColor::yellow)
147     {
148         return "Yellow";
149     }
150 
151     if (color == GLColor::magenta)
152     {
153         return "Magenta";
154     }
155 
156     if (color == GLColor::cyan)
157     {
158         return "Cyan";
159     }
160 
161     return nullptr;
162 }
163 
GetColorName(GLColorRGB color)164 const char *GetColorName(GLColorRGB color)
165 {
166     return GetColorName(GLColor(color.R, color.G, color.B, 255));
167 }
168 
169 // Always re-use displays when using --bot-mode in the test runner.
170 bool gReuseDisplays = false;
171 
ShouldAlwaysForceNewDisplay(const PlatformParameters & params)172 bool ShouldAlwaysForceNewDisplay(const PlatformParameters &params)
173 {
174     // When running WebGPU tests on linux always force a new display. The underlying vulkan swap
175     // chain appears to fail to get a new image after swapping when rapidly creating new swap chains
176     // for an existing window.
177     if (params.isWebGPU() && IsLinux())
178     {
179         return true;
180     }
181 
182     if (gReuseDisplays)
183         return false;
184 
185     // We prefer to reuse config displays. This is faster and solves a driver issue where creating
186     // many displays causes crashes. However this exposes other driver bugs on many other platforms.
187     // Conservatively enable the feature only on Windows Intel and NVIDIA for now.
188     SystemInfo *systemInfo = GetTestSystemInfo();
189     return (!systemInfo || !IsWindows() || systemInfo->hasAMDGPU());
190 }
191 
GetTestConfigAPIFromRenderer(angle::GLESDriverType driverType,EGLenum renderer,EGLenum deviceType)192 GPUTestConfig::API GetTestConfigAPIFromRenderer(angle::GLESDriverType driverType,
193                                                 EGLenum renderer,
194                                                 EGLenum deviceType)
195 {
196     if (driverType != angle::GLESDriverType::AngleEGL &&
197         driverType != angle::GLESDriverType::AngleVulkanSecondariesEGL)
198     {
199         return GPUTestConfig::kAPIUnknown;
200     }
201 
202     switch (renderer)
203     {
204         case EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE:
205             return GPUTestConfig::kAPID3D11;
206         case EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE:
207             return GPUTestConfig::kAPID3D9;
208         case EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE:
209             return GPUTestConfig::kAPIGLDesktop;
210         case EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE:
211             return GPUTestConfig::kAPIGLES;
212         case EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE:
213             if (deviceType == EGL_PLATFORM_ANGLE_DEVICE_TYPE_SWIFTSHADER_ANGLE)
214             {
215                 return GPUTestConfig::kAPISwiftShader;
216             }
217             else
218             {
219                 return GPUTestConfig::kAPIVulkan;
220             }
221         case EGL_PLATFORM_ANGLE_TYPE_METAL_ANGLE:
222             return GPUTestConfig::kAPIMetal;
223         case EGL_PLATFORM_ANGLE_TYPE_WEBGPU_ANGLE:
224             return GPUTestConfig::kAPIWgpu;
225         default:
226             std::cerr << "Unknown Renderer enum: 0x" << std::hex << renderer << "\n";
227             return GPUTestConfig::kAPIUnknown;
228     }
229 }
230 }  // anonymous namespace
231 
GLColorRGB(const Vector3 & floatColor)232 GLColorRGB::GLColorRGB(const Vector3 &floatColor)
233     : R(ColorDenorm(floatColor.x())), G(ColorDenorm(floatColor.y())), B(ColorDenorm(floatColor.z()))
234 {}
235 
GLColor(const Vector3 & floatColor)236 GLColor::GLColor(const Vector3 &floatColor)
237     : R(ColorDenorm(floatColor.x())),
238       G(ColorDenorm(floatColor.y())),
239       B(ColorDenorm(floatColor.z())),
240       A(255)
241 {}
242 
GLColor(const Vector4 & floatColor)243 GLColor::GLColor(const Vector4 &floatColor)
244     : R(ColorDenorm(floatColor.x())),
245       G(ColorDenorm(floatColor.y())),
246       B(ColorDenorm(floatColor.z())),
247       A(ColorDenorm(floatColor.w()))
248 {}
249 
GLColor(GLuint colorValue)250 GLColor::GLColor(GLuint colorValue) : R(0), G(0), B(0), A(0)
251 {
252     memcpy(&R, &colorValue, sizeof(GLuint));
253 }
254 
asUint() const255 GLuint GLColor::asUint() const
256 {
257     GLuint uint = 0;
258     memcpy(&uint, &R, sizeof(GLuint));
259     return uint;
260 }
261 
ExpectNear(const GLColor & expected,const GLColor & err) const262 testing::AssertionResult GLColor::ExpectNear(const GLColor &expected, const GLColor &err) const
263 {
264     testing::AssertionResult result(
265         abs(int(expected.R) - this->R) <= err.R && abs(int(expected.G) - this->G) <= err.G &&
266         abs(int(expected.B) - this->B) <= err.B && abs(int(expected.A) - this->A) <= err.A);
267     if (!bool(result))
268     {
269         result << "Expected " << expected << "+/-" << err << ", was " << *this;
270     }
271     return result;
272 }
273 
CreatePixelCenterWindowCoords(const std::vector<Vector2> & pixelPoints,int windowWidth,int windowHeight,std::vector<Vector3> * outVertices)274 void CreatePixelCenterWindowCoords(const std::vector<Vector2> &pixelPoints,
275                                    int windowWidth,
276                                    int windowHeight,
277                                    std::vector<Vector3> *outVertices)
278 {
279     for (Vector2 pixelPoint : pixelPoints)
280     {
281         outVertices->emplace_back(Vector3((pixelPoint[0] + 0.5f) * 2.0f / windowWidth - 1.0f,
282                                           (pixelPoint[1] + 0.5f) * 2.0f / windowHeight - 1.0f,
283                                           0.0f));
284     }
285 }
286 
toNormalizedVector() const287 Vector4 GLColor::toNormalizedVector() const
288 {
289     return Vector4(ColorNorm(R), ColorNorm(G), ColorNorm(B), ColorNorm(A));
290 }
291 
RandomColor(angle::RNG * rng)292 GLColor RandomColor(angle::RNG *rng)
293 {
294     return GLColor(rng->randomIntBetween(0, 255), rng->randomIntBetween(0, 255),
295                    rng->randomIntBetween(0, 255), rng->randomIntBetween(0, 255));
296 }
297 
ReadColor(GLint x,GLint y)298 GLColor ReadColor(GLint x, GLint y)
299 {
300     GLColor actual;
301     glReadPixels((x), (y), 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &actual.R);
302     EXPECT_GL_NO_ERROR();
303     return actual;
304 }
305 
operator ==(const GLColor & a,const GLColor & b)306 bool operator==(const GLColor &a, const GLColor &b)
307 {
308     return a.R == b.R && a.G == b.G && a.B == b.B && a.A == b.A;
309 }
310 
operator !=(const GLColor & a,const GLColor & b)311 bool operator!=(const GLColor &a, const GLColor &b)
312 {
313     return !(a == b);
314 }
315 
operator <<(std::ostream & ostream,const GLColor & color)316 std::ostream &operator<<(std::ostream &ostream, const GLColor &color)
317 {
318     const char *colorName = GetColorName(color);
319     if (colorName)
320     {
321         return ostream << colorName;
322     }
323 
324     ostream << "(" << static_cast<unsigned int>(color.R) << ", "
325             << static_cast<unsigned int>(color.G) << ", " << static_cast<unsigned int>(color.B)
326             << ", " << static_cast<unsigned int>(color.A) << ")";
327     return ostream;
328 }
329 
operator ==(const GLColorRGB & a,const GLColorRGB & b)330 bool operator==(const GLColorRGB &a, const GLColorRGB &b)
331 {
332     return a.R == b.R && a.G == b.G && a.B == b.B;
333 }
334 
operator !=(const GLColorRGB & a,const GLColorRGB & b)335 bool operator!=(const GLColorRGB &a, const GLColorRGB &b)
336 {
337     return !(a == b);
338 }
339 
operator <<(std::ostream & ostream,const GLColorRGB & color)340 std::ostream &operator<<(std::ostream &ostream, const GLColorRGB &color)
341 {
342     const char *colorName = GetColorName(color);
343     if (colorName)
344     {
345         return ostream << colorName;
346     }
347 
348     ostream << "(" << static_cast<unsigned int>(color.R) << ", "
349             << static_cast<unsigned int>(color.G) << ", " << static_cast<unsigned int>(color.B)
350             << ")";
351     return ostream;
352 }
353 
operator <<(std::ostream & ostream,const GLColor32F & color)354 std::ostream &operator<<(std::ostream &ostream, const GLColor32F &color)
355 {
356     ostream << "(" << color.R << ", " << color.G << ", " << color.B << ", " << color.A << ")";
357     return ostream;
358 }
359 
ReadColor32F(GLint x,GLint y)360 GLColor32F ReadColor32F(GLint x, GLint y)
361 {
362     GLColor32F actual;
363     glReadPixels((x), (y), 1, 1, GL_RGBA, GL_FLOAT, &actual.R);
364     EXPECT_GL_NO_ERROR();
365     return actual;
366 }
367 
LoadEntryPointsWithUtilLoader(angle::GLESDriverType driverType)368 void LoadEntryPointsWithUtilLoader(angle::GLESDriverType driverType)
369 {
370 #if defined(ANGLE_USE_UTIL_LOADER)
371     PFNEGLGETPROCADDRESSPROC getProcAddress;
372     ANGLETestEnvironment::GetDriverLibrary(driverType)->getAs("eglGetProcAddress", &getProcAddress);
373     ASSERT(nullptr != getProcAddress);
374 
375     LoadUtilEGL(getProcAddress);
376     LoadUtilGLES(getProcAddress);
377 #endif  // defined(ANGLE_USE_UTIL_LOADER)
378 }
379 
IsFormatEmulated(GLenum target)380 bool IsFormatEmulated(GLenum target)
381 {
382     GLint readFormat;
383     glGetTexParameteriv(target, GL_IMPLEMENTATION_COLOR_READ_FORMAT, &readFormat);
384 
385     // This helper only works for compressed formats
386     return gl::IsEmulatedCompressedFormat(readFormat);
387 }
388 
389 }  // namespace angle
390 
391 using namespace angle;
392 
393 PlatformMethods gDefaultPlatformMethods;
394 
395 namespace
396 {
397 TestPlatformContext gPlatformContext;
398 
399 // After a fixed number of iterations we reset the test window. This works around some driver bugs.
400 constexpr uint32_t kWindowReuseLimit = 50;
401 
402 constexpr char kUseConfig[]                      = "--use-config=";
403 constexpr char kReuseDisplays[]                  = "--reuse-displays";
404 constexpr char kEnableANGLEPerTestCaptureLabel[] = "--angle-per-test-capture-label";
405 constexpr char kBatchId[]                        = "--batch-id=";
406 constexpr char kDelayTestStart[]                 = "--delay-test-start=";
407 constexpr char kRenderDoc[]                      = "--renderdoc";
408 constexpr char kNoRenderDoc[]                    = "--no-renderdoc";
409 
SetupEnvironmentVarsForCaptureReplay()410 void SetupEnvironmentVarsForCaptureReplay()
411 {
412     const ::testing::TestInfo *const testInfo =
413         ::testing::UnitTest::GetInstance()->current_test_info();
414     std::string testName = std::string{testInfo->name()};
415     std::replace(testName.begin(), testName.end(), '/', '_');
416     SetEnvironmentVar("ANGLE_CAPTURE_LABEL",
417                       (std::string{testInfo->test_suite_name()} + "_" + testName).c_str());
418 }
419 }  // anonymous namespace
420 
421 int gTestStartDelaySeconds = 0;
422 
GetTestStartDelaySeconds()423 int GetTestStartDelaySeconds()
424 {
425     return gTestStartDelaySeconds;
426 }
427 
SetTestStartDelay(const char * testStartDelay)428 void SetTestStartDelay(const char *testStartDelay)
429 {
430     gTestStartDelaySeconds = std::stoi(testStartDelay);
431 }
432 
433 #if defined(ANGLE_TEST_ENABLE_RENDERDOC_CAPTURE)
434 bool gEnableRenderDocCapture = true;
435 #else
436 bool gEnableRenderDocCapture = false;
437 #endif
438 
439 // static
GetQuadVertices()440 std::array<Vector3, 6> ANGLETestBase::GetQuadVertices()
441 {
442     return kQuadVertices;
443 }
444 
445 // static
GetQuadIndices()446 std::array<GLushort, 6> ANGLETestBase::GetQuadIndices()
447 {
448     return kIndexedQuadIndices;
449 }
450 
451 // static
GetIndexedQuadVertices()452 std::array<Vector3, 4> ANGLETestBase::GetIndexedQuadVertices()
453 {
454     return kIndexedQuadVertices;
455 }
456 
AssertEGLEnumsEqual(const char * lhsExpr,const char * rhsExpr,EGLenum lhs,EGLenum rhs)457 testing::AssertionResult AssertEGLEnumsEqual(const char *lhsExpr,
458                                              const char *rhsExpr,
459                                              EGLenum lhs,
460                                              EGLenum rhs)
461 {
462     if (lhs == rhs)
463     {
464         return testing::AssertionSuccess();
465     }
466     else
467     {
468         std::stringstream strstr;
469         strstr << std::hex << lhsExpr << " (0x" << int(lhs) << ") != " << rhsExpr << " (0x"
470                << int(rhs) << ")";
471         return testing::AssertionFailure() << strstr.str();
472     }
473 }
474 
operator new(size_t size)475 void *ANGLETestBase::operator new(size_t size)
476 {
477     void *ptr = malloc(size ? size : size + 1);
478     // Initialize integer primitives to large positive values to avoid tests relying
479     // on the assumption that primitives (e.g. GLuint) would be zero-initialized.
480     memset(ptr, 0x7f, size);
481     return ptr;
482 }
483 
operator delete(void * ptr)484 void ANGLETestBase::operator delete(void *ptr)
485 {
486     free(ptr);
487 }
488 
ANGLETestBase(const PlatformParameters & params)489 ANGLETestBase::ANGLETestBase(const PlatformParameters &params)
490     : mWidth(16),
491       mHeight(16),
492       mIgnoreD3D11SDKLayersWarnings(false),
493       mQuadVertexBuffer(0),
494       mQuadIndexBuffer(0),
495       m2DTexturedQuadProgram(0),
496       m3DTexturedQuadProgram(0),
497       m2DArrayTexturedQuadProgram(0),
498       mDeferContextInit(false),
499       mAlwaysForceNewDisplay(ShouldAlwaysForceNewDisplay(params)),
500       mForceNewDisplay(mAlwaysForceNewDisplay),
501       mSetUpCalled(false),
502       mTearDownCalled(false),
503       mCurrentParams(nullptr),
504       mFixture(nullptr)
505 {
506     // Override the default platform methods with the ANGLE test methods pointer.
507     PlatformParameters withMethods            = params;
508     withMethods.eglParameters.platformMethods = &gDefaultPlatformMethods;
509 
510     if (withMethods.getRenderer() == EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE)
511     {
512 #if defined(ANGLE_ENABLE_VULKAN_VALIDATION_LAYERS)
513         withMethods.eglParameters.debugLayersEnabled = true;
514 #else
515         withMethods.eglParameters.debugLayersEnabled = false;
516 #endif
517     }
518 
519     if (gEnableRenderDocCapture)
520     {
521         mRenderDoc.attach();
522     }
523 
524     auto iter = gFixtures.find(withMethods);
525     if (iter != gFixtures.end())
526     {
527         mCurrentParams = &iter->first;
528 
529         if (!params.noFixture)
530         {
531             mFixture = &iter->second;
532             mFixture->configParams.reset();
533         }
534         return;
535     }
536 
537     TestFixture platform;
538     auto insertIter = gFixtures.emplace(withMethods, platform);
539     mCurrentParams  = &insertIter.first->first;
540 
541     if (!params.noFixture)
542     {
543         mFixture = &insertIter.first->second;
544         initOSWindow();
545     }
546 }
547 
initOSWindow()548 void ANGLETestBase::initOSWindow()
549 {
550     std::stringstream windowNameStream;
551     windowNameStream << "ANGLE Tests - " << *mCurrentParams;
552     std::string windowName = windowNameStream.str();
553 
554     if (IsAndroid())
555     {
556         // Only one window per test application on Android, shared among all fixtures
557         mFixture->osWindow = mOSWindowSingleton;
558     }
559 
560     if (mFixture->osWindow == nullptr)
561     {
562         mFixture->osWindow = OSWindow::New();
563         if (mFixture->osWindow == nullptr)
564         {
565             FATAL() << "Failed to create a new window";
566         }
567         mFixture->osWindow->disableErrorMessageDialog();
568         if (!mFixture->osWindow->initialize(windowName.c_str(), 128, 128))
569         {
570             std::cerr << "Failed to initialize OS Window.\n";
571         }
572 
573         if (IsAndroid())
574         {
575             // Initialize the single window on Andoird only once
576             mOSWindowSingleton = mFixture->osWindow;
577         }
578     }
579 
580     if (!mFixture->osWindow->valid())
581     {
582         return;
583     }
584 
585     // On Linux we must keep the test windows visible. On Windows it doesn't seem to need it.
586     setWindowVisible(getOSWindow(), !IsWindows());
587 
588     switch (mCurrentParams->driver)
589     {
590         case GLESDriverType::AngleEGL:
591         case GLESDriverType::AngleVulkanSecondariesEGL:
592         case GLESDriverType::SystemEGL:
593         case GLESDriverType::ZinkEGL:
594         {
595             mFixture->eglWindow =
596                 EGLWindow::New(mCurrentParams->majorVersion, mCurrentParams->minorVersion);
597             break;
598         }
599 
600         case GLESDriverType::SystemWGL:
601         {
602             // WGL tests are currently disabled.
603             std::cerr << "Unsupported driver." << std::endl;
604             break;
605         }
606     }
607 }
608 
~ANGLETestBase()609 ANGLETestBase::~ANGLETestBase()
610 {
611     if (mQuadVertexBuffer)
612     {
613         glDeleteBuffers(1, &mQuadVertexBuffer);
614     }
615     if (mQuadIndexBuffer)
616     {
617         glDeleteBuffers(1, &mQuadIndexBuffer);
618     }
619     if (m2DTexturedQuadProgram)
620     {
621         glDeleteProgram(m2DTexturedQuadProgram);
622     }
623     if (m3DTexturedQuadProgram)
624     {
625         glDeleteProgram(m3DTexturedQuadProgram);
626     }
627     if (m2DArrayTexturedQuadProgram)
628     {
629         glDeleteProgram(m2DArrayTexturedQuadProgram);
630     }
631 
632     if (!mSetUpCalled)
633     {
634         GTEST_NONFATAL_FAILURE_("SetUp not called.");
635     }
636 
637     if (!mTearDownCalled)
638     {
639         GTEST_NONFATAL_FAILURE_("TearDown not called.");
640     }
641 }
642 
ANGLETestSetUp()643 void ANGLETestBase::ANGLETestSetUp()
644 {
645     mSetUpCalled = true;
646 
647     // Delay test startup to allow a debugger to attach.
648     if (GetTestStartDelaySeconds())
649     {
650         angle::Sleep(GetTestStartDelaySeconds() * 1000);
651     }
652 
653     gDefaultPlatformMethods.logError   = TestPlatform_logError;
654     gDefaultPlatformMethods.logWarning = TestPlatform_logWarning;
655     gDefaultPlatformMethods.logInfo    = TestPlatform_logInfo;
656     gDefaultPlatformMethods.context    = &gPlatformContext;
657 
658     gPlatformContext.ignoreMessages   = false;
659     gPlatformContext.warningsAsErrors = false;
660     gPlatformContext.currentTest      = this;
661 
662     const testing::TestInfo *testInfo = testing::UnitTest::GetInstance()->current_test_info();
663 
664     // Check the skip list.
665 
666     angle::GPUTestConfig::API api = GetTestConfigAPIFromRenderer(
667         mCurrentParams->driver, mCurrentParams->getRenderer(), mCurrentParams->getDeviceType());
668     GPUTestConfig testConfig = GPUTestConfig(api, 0);
669 
670     std::stringstream fullTestNameStr;
671     fullTestNameStr << testInfo->test_suite_name() << "." << testInfo->name();
672     std::string fullTestName = fullTestNameStr.str();
673 
674     // TODO(b/279980674): TestSuite depends on rapidjson which we don't have in aosp builds,
675     // for now disable both TestSuite and expectations.
676 #if defined(ANGLE_HAS_RAPIDJSON)
677     TestSuite *testSuite = TestSuite::GetInstance();
678     int32_t testExpectation =
679         testSuite->getTestExpectationWithConfigAndUpdateTimeout(testConfig, fullTestName);
680 
681     if (testExpectation == GPUTestExpectationsParser::kGpuTestSkip)
682     {
683         GTEST_SKIP() << "Test skipped on this config";
684     }
685 #endif
686 
687     if (IsWindows())
688     {
689         WriteDebugMessage("Entering %s\n", fullTestName.c_str());
690     }
691 
692     if (mCurrentParams->noFixture)
693     {
694         LoadEntryPointsWithUtilLoader(mCurrentParams->driver);
695         mIsSetUp = true;
696         return;
697     }
698 
699     if (!mLastLoadedDriver.valid() || mCurrentParams->driver != mLastLoadedDriver.value())
700     {
701         LoadEntryPointsWithUtilLoader(mCurrentParams->driver);
702         mLastLoadedDriver = mCurrentParams->driver;
703     }
704 
705     if (gEnableANGLEPerTestCaptureLabel)
706     {
707         SetupEnvironmentVarsForCaptureReplay();
708     }
709 
710     if (!mFixture->osWindow->valid())
711     {
712         mIsSetUp = true;
713         return;
714     }
715 
716     // Resize the window before creating the context so that the first make current
717     // sets the viewport and scissor box to the right size.
718     bool needSwap = false;
719 
720     int osWindowWidth  = mFixture->osWindow->getWidth();
721     int osWindowHeight = mFixture->osWindow->getHeight();
722 
723     const bool isRotated = mCurrentParams->isEnableRequested(Feature::EmulatedPrerotation90) ||
724                            mCurrentParams->isEnableRequested(Feature::EmulatedPrerotation270);
725     if (isRotated)
726     {
727         std::swap(osWindowWidth, osWindowHeight);
728     }
729 
730     if (osWindowWidth != mWidth || osWindowHeight != mHeight)
731     {
732         int newWindowWidth  = mWidth;
733         int newWindowHeight = mHeight;
734         if (isRotated)
735         {
736             std::swap(newWindowWidth, newWindowHeight);
737         }
738 
739         if (!mFixture->osWindow->resize(newWindowWidth, newWindowHeight))
740         {
741             FAIL() << "Failed to resize ANGLE test window.";
742         }
743         needSwap = true;
744     }
745     // WGL tests are currently disabled.
746     if (mFixture->wglWindow)
747     {
748         FAIL() << "Unsupported driver.";
749     }
750     else
751     {
752         Library *driverLib = ANGLETestEnvironment::GetDriverLibrary(mCurrentParams->driver);
753 
754         if (mForceNewDisplay || !mFixture->eglWindow->isDisplayInitialized())
755         {
756             mFixture->eglWindow->destroyGL();
757             if (!mFixture->eglWindow->initializeDisplay(mFixture->osWindow, driverLib,
758                                                         mCurrentParams->driver,
759                                                         mCurrentParams->eglParameters))
760             {
761                 FAIL() << "EGL Display init failed.";
762             }
763         }
764         else if (mCurrentParams->eglParameters != mFixture->eglWindow->getPlatform())
765         {
766             FAIL() << "Internal parameter conflict error.";
767         }
768 
769         const GLWindowResult windowResult = mFixture->eglWindow->initializeSurface(
770             mFixture->osWindow, driverLib, mFixture->configParams);
771 
772         if (windowResult != GLWindowResult::NoError)
773         {
774             if (windowResult != GLWindowResult::Error)
775             {
776                 // If the test requests an extension that isn't supported, automatically skip the
777                 // test.
778                 GTEST_SKIP() << "Test skipped due to missing extension";
779             }
780             else if (mFixture->configParams.multisample)
781             {
782                 // If the test requests a multisampled window that isn't supported, automatically
783                 // skip the test.
784                 GTEST_SKIP() << "Test skipped due to no multisampled configs available";
785             }
786             else
787             {
788                 // Otherwise fail the test.
789                 FAIL() << "egl surface init failed.";
790             }
791         }
792 
793         if (!mDeferContextInit && !mFixture->eglWindow->initializeContext())
794         {
795             FAIL() << "GL Context init failed.";
796         }
797     }
798 
799     if (needSwap)
800     {
801         // Swap the buffers so that the default framebuffer picks up the resize
802         // which will allow follow-up test code to assume the framebuffer covers
803         // the whole window.
804         swapBuffers();
805     }
806 
807     // This Viewport command is not strictly necessary but we add it so that programs
808     // taking OpenGL traces can guess the size of the default framebuffer and show it
809     // in their UIs
810     glViewport(0, 0, mWidth, mHeight);
811 
812     mIsSetUp = true;
813 
814     mRenderDoc.startFrame();
815 }
816 
ANGLETestPreTearDown()817 void ANGLETestBase::ANGLETestPreTearDown()
818 {
819     // We swap an extra time before we call "tearDown" to capture resources before they're freed.
820     if (gEnableANGLEPerTestCaptureLabel)
821     {
822         swapBuffers();
823     }
824 }
825 
ANGLETestTearDown()826 void ANGLETestBase::ANGLETestTearDown()
827 {
828     mTearDownCalled              = true;
829     gPlatformContext.currentTest = nullptr;
830 
831     if (IsWindows())
832     {
833         const testing::TestInfo *info = testing::UnitTest::GetInstance()->current_test_info();
834         WriteDebugMessage("Exiting %s.%s\n", info->test_suite_name(), info->name());
835     }
836 
837     if (mCurrentParams->noFixture || !mFixture->osWindow->valid())
838     {
839         mRenderDoc.endFrame();
840         return;
841     }
842 
843     swapBuffers();
844     mFixture->osWindow->messageLoop();
845 
846     mRenderDoc.endFrame();
847 
848     if (mFixture->eglWindow)
849     {
850         checkD3D11SDKLayersMessages();
851     }
852 
853     if (mFixture->reuseCounter++ >= kWindowReuseLimit || mForceNewDisplay)
854     {
855         if (!mForceNewDisplay)
856         {
857             printf("Recreating test window because of reuse limit of %d\n", kWindowReuseLimit);
858         }
859 
860         mFixture->reuseCounter = 0;
861         getGLWindow()->destroyGL();
862     }
863     else
864     {
865         mFixture->eglWindow->destroyContext();
866         mFixture->eglWindow->destroySurface();
867     }
868 
869     Event myEvent;
870     while (mFixture->osWindow->popEvent(&myEvent))
871     {
872         if (myEvent.Type == Event::EVENT_CLOSED)
873         {
874             exit(0);
875         }
876     }
877 }
878 
ReleaseFixtures()879 void ANGLETestBase::ReleaseFixtures()
880 {
881     for (auto it = gFixtures.begin(); it != gFixtures.end(); it++)
882     {
883         TestFixture &fixture = it->second;
884         if (fixture.eglWindow != nullptr)
885         {
886             fixture.eglWindow->destroyGL();
887             EGLWindow::Delete(&fixture.eglWindow);
888         }
889 
890         if (IsAndroid())
891         {
892             if (mOSWindowSingleton != nullptr)
893             {
894                 OSWindow::Delete(&mOSWindowSingleton);
895             }
896             fixture.osWindow = nullptr;
897         }
898         else
899         {
900             if (fixture.osWindow != nullptr)
901             {
902                 OSWindow::Delete(&fixture.osWindow);
903             }
904         }
905     }
906 
907     gFixtures.clear();
908 }
909 
swapBuffers()910 void ANGLETestBase::swapBuffers()
911 {
912     if (getGLWindow()->isGLInitialized())
913     {
914         getGLWindow()->swap();
915 
916         if (mFixture->eglWindow)
917         {
918             EXPECT_EGL_SUCCESS();
919         }
920     }
921 }
922 
setupQuadVertexBuffer(GLfloat positionAttribZ,GLfloat positionAttribXYScale)923 void ANGLETestBase::setupQuadVertexBuffer(GLfloat positionAttribZ, GLfloat positionAttribXYScale)
924 {
925     if (mQuadVertexBuffer == 0)
926     {
927         glGenBuffers(1, &mQuadVertexBuffer);
928     }
929 
930     auto quadVertices = GetQuadVertices();
931     for (Vector3 &vertex : quadVertices)
932     {
933         vertex.x() *= positionAttribXYScale;
934         vertex.y() *= positionAttribXYScale;
935         vertex.z() = positionAttribZ;
936     }
937 
938     glBindBuffer(GL_ARRAY_BUFFER, mQuadVertexBuffer);
939     glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, quadVertices.data(), GL_STATIC_DRAW);
940 }
941 
setupIndexedQuadVertexBuffer(GLfloat positionAttribZ,GLfloat positionAttribXYScale)942 void ANGLETestBase::setupIndexedQuadVertexBuffer(GLfloat positionAttribZ,
943                                                  GLfloat positionAttribXYScale)
944 {
945     if (mQuadVertexBuffer == 0)
946     {
947         glGenBuffers(1, &mQuadVertexBuffer);
948     }
949 
950     auto quadVertices = kIndexedQuadVertices;
951     for (Vector3 &vertex : quadVertices)
952     {
953         vertex.x() *= positionAttribXYScale;
954         vertex.y() *= positionAttribXYScale;
955         vertex.z() = positionAttribZ;
956     }
957 
958     glBindBuffer(GL_ARRAY_BUFFER, mQuadVertexBuffer);
959     glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 4, quadVertices.data(), GL_STATIC_DRAW);
960 }
961 
setupIndexedQuadIndexBuffer()962 void ANGLETestBase::setupIndexedQuadIndexBuffer()
963 {
964     if (mQuadIndexBuffer == 0)
965     {
966         glGenBuffers(1, &mQuadIndexBuffer);
967     }
968 
969     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mQuadIndexBuffer);
970     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(kIndexedQuadIndices), kIndexedQuadIndices.data(),
971                  GL_STATIC_DRAW);
972 }
973 
974 // static
drawQuad(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ)975 void ANGLETestBase::drawQuad(GLuint program,
976                              const std::string &positionAttribName,
977                              GLfloat positionAttribZ)
978 {
979     drawQuad(program, positionAttribName, positionAttribZ, 1.0f);
980 }
981 
982 // static
drawQuad(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ,GLfloat positionAttribXYScale)983 void ANGLETestBase::drawQuad(GLuint program,
984                              const std::string &positionAttribName,
985                              GLfloat positionAttribZ,
986                              GLfloat positionAttribXYScale)
987 {
988     drawQuad(program, positionAttribName, positionAttribZ, positionAttribXYScale, false);
989 }
990 
drawQuad(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ,GLfloat positionAttribXYScale,bool useVertexBuffer)991 void ANGLETestBase::drawQuad(GLuint program,
992                              const std::string &positionAttribName,
993                              GLfloat positionAttribZ,
994                              GLfloat positionAttribXYScale,
995                              bool useVertexBuffer)
996 {
997     drawQuad(program, positionAttribName, positionAttribZ, positionAttribXYScale, useVertexBuffer,
998              false, false, 0u);
999 }
1000 
drawQuadInstanced(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ,GLfloat positionAttribXYScale,bool useVertexBuffer,GLuint numInstances)1001 void ANGLETestBase::drawQuadInstanced(GLuint program,
1002                                       const std::string &positionAttribName,
1003                                       GLfloat positionAttribZ,
1004                                       GLfloat positionAttribXYScale,
1005                                       bool useVertexBuffer,
1006                                       GLuint numInstances)
1007 {
1008     drawQuad(program, positionAttribName, positionAttribZ, positionAttribXYScale, useVertexBuffer,
1009              true, false, numInstances);
1010 }
1011 
drawPatches(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ,GLfloat positionAttribXYScale,bool useVertexBuffer)1012 void ANGLETestBase::drawPatches(GLuint program,
1013                                 const std::string &positionAttribName,
1014                                 GLfloat positionAttribZ,
1015                                 GLfloat positionAttribXYScale,
1016                                 bool useVertexBuffer)
1017 {
1018     drawQuad(program, positionAttribName, positionAttribZ, positionAttribXYScale, useVertexBuffer,
1019              false, true, 0u);
1020 }
1021 
drawQuad(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ,GLfloat positionAttribXYScale,bool useVertexBuffer,bool useInstancedDrawCalls,bool useTessellationPatches,GLuint numInstances)1022 void ANGLETestBase::drawQuad(GLuint program,
1023                              const std::string &positionAttribName,
1024                              GLfloat positionAttribZ,
1025                              GLfloat positionAttribXYScale,
1026                              bool useVertexBuffer,
1027                              bool useInstancedDrawCalls,
1028                              bool useTessellationPatches,
1029                              GLuint numInstances)
1030 {
1031     GLint previousProgram = 0;
1032     glGetIntegerv(GL_CURRENT_PROGRAM, &previousProgram);
1033     if (previousProgram != static_cast<GLint>(program))
1034     {
1035         glUseProgram(program);
1036     }
1037 
1038     GLint previousBuffer = 0;
1039     glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &previousBuffer);
1040 
1041     GLint positionLocation = glGetAttribLocation(program, positionAttribName.c_str());
1042 
1043     std::array<Vector3, 6> quadVertices = GetQuadVertices();
1044 
1045     if (useVertexBuffer)
1046     {
1047         setupQuadVertexBuffer(positionAttribZ, positionAttribXYScale);
1048         glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
1049         glBindBuffer(GL_ARRAY_BUFFER, previousBuffer);
1050     }
1051     else
1052     {
1053         for (Vector3 &vertex : quadVertices)
1054         {
1055             vertex.x() *= positionAttribXYScale;
1056             vertex.y() *= positionAttribXYScale;
1057             vertex.z() = positionAttribZ;
1058         }
1059 
1060         if (previousBuffer != 0)
1061         {
1062             glBindBuffer(GL_ARRAY_BUFFER, 0);
1063         }
1064         glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, quadVertices.data());
1065         if (previousBuffer != 0)
1066         {
1067             glBindBuffer(GL_ARRAY_BUFFER, previousBuffer);
1068         }
1069     }
1070     glEnableVertexAttribArray(positionLocation);
1071     GLenum drawMode = (useTessellationPatches) ? GL_PATCHES : GL_TRIANGLES;
1072 
1073     if (useInstancedDrawCalls)
1074     {
1075         glDrawArraysInstanced(drawMode, 0, 6, numInstances);
1076     }
1077     else
1078     {
1079         glDrawArrays(drawMode, 0, 6);
1080     }
1081 
1082     glDisableVertexAttribArray(positionLocation);
1083     glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
1084 
1085     if (previousProgram != static_cast<GLint>(program))
1086     {
1087         glUseProgram(previousProgram);
1088     }
1089 }
1090 
drawQuadPPO(GLuint vertProgram,const std::string & positionAttribName,const GLfloat positionAttribZ,const GLfloat positionAttribXYScale)1091 void ANGLETestBase::drawQuadPPO(GLuint vertProgram,
1092                                 const std::string &positionAttribName,
1093                                 const GLfloat positionAttribZ,
1094                                 const GLfloat positionAttribXYScale)
1095 {
1096     GLint activeProgram = 0;
1097     glGetIntegerv(GL_CURRENT_PROGRAM, &activeProgram);
1098     if (activeProgram)
1099     {
1100         glUseProgram(0);
1101     }
1102 
1103     std::array<Vector3, 6> quadVertices = GetQuadVertices();
1104 
1105     for (Vector3 &vertex : quadVertices)
1106     {
1107         vertex.x() *= positionAttribXYScale;
1108         vertex.y() *= positionAttribXYScale;
1109         vertex.z() = positionAttribZ;
1110     }
1111 
1112     GLint positionLocation = glGetAttribLocation(vertProgram, positionAttribName.c_str());
1113 
1114     glBindBuffer(GL_ARRAY_BUFFER, 0);
1115     glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, quadVertices.data());
1116     glEnableVertexAttribArray(positionLocation);
1117 
1118     glDrawArrays(GL_TRIANGLES, 0, 6);
1119 
1120     glDisableVertexAttribArray(positionLocation);
1121     glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
1122 
1123     if (activeProgram)
1124     {
1125         glUseProgram(static_cast<GLuint>(activeProgram));
1126     }
1127 }
1128 
drawIndexedQuad(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ)1129 void ANGLETestBase::drawIndexedQuad(GLuint program,
1130                                     const std::string &positionAttribName,
1131                                     GLfloat positionAttribZ)
1132 {
1133     drawIndexedQuad(program, positionAttribName, positionAttribZ, 1.0f);
1134 }
1135 
drawIndexedQuad(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ,GLfloat positionAttribXYScale)1136 void ANGLETestBase::drawIndexedQuad(GLuint program,
1137                                     const std::string &positionAttribName,
1138                                     GLfloat positionAttribZ,
1139                                     GLfloat positionAttribXYScale)
1140 {
1141     ASSERT(!mFixture || !mFixture->configParams.webGLCompatibility.valid() ||
1142            !mFixture->configParams.webGLCompatibility.value());
1143     drawIndexedQuad(program, positionAttribName, positionAttribZ, positionAttribXYScale, false);
1144 }
1145 
drawIndexedQuad(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ,GLfloat positionAttribXYScale,bool useIndexBuffer)1146 void ANGLETestBase::drawIndexedQuad(GLuint program,
1147                                     const std::string &positionAttribName,
1148                                     GLfloat positionAttribZ,
1149                                     GLfloat positionAttribXYScale,
1150                                     bool useIndexBuffer)
1151 {
1152     drawIndexedQuad(program, positionAttribName, positionAttribZ, positionAttribXYScale,
1153                     useIndexBuffer, false);
1154 }
1155 
drawIndexedQuad(GLuint program,const std::string & positionAttribName,GLfloat positionAttribZ,GLfloat positionAttribXYScale,bool useIndexBuffer,bool restrictedRange)1156 void ANGLETestBase::drawIndexedQuad(GLuint program,
1157                                     const std::string &positionAttribName,
1158                                     GLfloat positionAttribZ,
1159                                     GLfloat positionAttribXYScale,
1160                                     bool useIndexBuffer,
1161                                     bool restrictedRange)
1162 {
1163     GLint positionLocation = glGetAttribLocation(program, positionAttribName.c_str());
1164 
1165     GLint activeProgram = 0;
1166     glGetIntegerv(GL_CURRENT_PROGRAM, &activeProgram);
1167     if (static_cast<GLuint>(activeProgram) != program)
1168     {
1169         glUseProgram(program);
1170     }
1171 
1172     GLuint prevCoordBinding = 0;
1173     glGetIntegerv(GL_ARRAY_BUFFER_BINDING, reinterpret_cast<GLint *>(&prevCoordBinding));
1174 
1175     setupIndexedQuadVertexBuffer(positionAttribZ, positionAttribXYScale);
1176 
1177     glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
1178     glEnableVertexAttribArray(positionLocation);
1179     glBindBuffer(GL_ARRAY_BUFFER, prevCoordBinding);
1180 
1181     GLuint prevIndexBinding = 0;
1182     const GLvoid *indices;
1183     if (useIndexBuffer)
1184     {
1185         glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING,
1186                       reinterpret_cast<GLint *>(&prevIndexBinding));
1187 
1188         setupIndexedQuadIndexBuffer();
1189         indices = 0;
1190     }
1191     else
1192     {
1193         indices = kIndexedQuadIndices.data();
1194     }
1195 
1196     if (!restrictedRange)
1197     {
1198         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
1199     }
1200     else
1201     {
1202         glDrawRangeElements(GL_TRIANGLES, 0, 3, 6, GL_UNSIGNED_SHORT, indices);
1203     }
1204 
1205     if (useIndexBuffer)
1206     {
1207         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, prevIndexBinding);
1208     }
1209 
1210     glDisableVertexAttribArray(positionLocation);
1211     glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, nullptr);
1212 
1213     if (static_cast<GLuint>(activeProgram) != program)
1214     {
1215         glUseProgram(static_cast<GLuint>(activeProgram));
1216     }
1217 }
1218 
get2DTexturedQuadProgram()1219 GLuint ANGLETestBase::get2DTexturedQuadProgram()
1220 {
1221     if (m2DTexturedQuadProgram)
1222     {
1223         return m2DTexturedQuadProgram;
1224     }
1225 
1226     constexpr char kVS[] =
1227         "attribute vec2 position;\n"
1228         "varying mediump vec2 texCoord;\n"
1229         "void main()\n"
1230         "{\n"
1231         "    gl_Position = vec4(position, 0, 1);\n"
1232         "    texCoord = position * 0.5 + vec2(0.5);\n"
1233         "}\n";
1234 
1235     constexpr char kFS[] =
1236         "varying mediump vec2 texCoord;\n"
1237         "uniform sampler2D tex;\n"
1238         "void main()\n"
1239         "{\n"
1240         "    gl_FragColor = texture2D(tex, texCoord);\n"
1241         "}\n";
1242 
1243     m2DTexturedQuadProgram = CompileProgram(kVS, kFS);
1244     return m2DTexturedQuadProgram;
1245 }
1246 
get3DTexturedQuadProgram()1247 GLuint ANGLETestBase::get3DTexturedQuadProgram()
1248 {
1249     if (m3DTexturedQuadProgram)
1250     {
1251         return m3DTexturedQuadProgram;
1252     }
1253 
1254     constexpr char kVS[] = R"(#version 300 es
1255 in vec2 position;
1256 out vec2 texCoord;
1257 void main()
1258 {
1259     gl_Position = vec4(position, 0, 1);
1260     texCoord = position * 0.5 + vec2(0.5);
1261 })";
1262 
1263     constexpr char kFS[] = R"(#version 300 es
1264 precision highp float;
1265 
1266 in vec2 texCoord;
1267 out vec4 my_FragColor;
1268 
1269 uniform highp sampler3D tex;
1270 uniform float u_layer;
1271 
1272 void main()
1273 {
1274     my_FragColor = texture(tex, vec3(texCoord, u_layer));
1275 })";
1276 
1277     m3DTexturedQuadProgram = CompileProgram(kVS, kFS);
1278     return m3DTexturedQuadProgram;
1279 }
1280 
get2DArrayTexturedQuadProgram()1281 GLuint ANGLETestBase::get2DArrayTexturedQuadProgram()
1282 {
1283     if (m2DArrayTexturedQuadProgram)
1284     {
1285         return m2DArrayTexturedQuadProgram;
1286     }
1287 
1288     constexpr char kVS[] = R"(#version 300 es
1289 in vec2 position;
1290 out vec2 texCoord;
1291 void main()
1292 {
1293     gl_Position = vec4(position, 0, 1);
1294     texCoord = position * 0.5 + vec2(0.5);
1295 })";
1296 
1297     constexpr char kFS[] = R"(#version 300 es
1298 precision highp float;
1299 
1300 in vec2 texCoord;
1301 out vec4 my_FragColor;
1302 
1303 uniform highp sampler2DArray tex;
1304 uniform float u_layer;
1305 
1306 void main()
1307 {
1308     my_FragColor = texture(tex, vec3(texCoord, u_layer));
1309 })";
1310 
1311     m2DArrayTexturedQuadProgram = CompileProgram(kVS, kFS);
1312     return m2DArrayTexturedQuadProgram;
1313 }
1314 
draw2DTexturedQuad(GLfloat positionAttribZ,GLfloat positionAttribXYScale,bool useVertexBuffer)1315 void ANGLETestBase::draw2DTexturedQuad(GLfloat positionAttribZ,
1316                                        GLfloat positionAttribXYScale,
1317                                        bool useVertexBuffer)
1318 {
1319     ASSERT_NE(0u, get2DTexturedQuadProgram());
1320     drawQuad(get2DTexturedQuadProgram(), "position", positionAttribZ, positionAttribXYScale,
1321              useVertexBuffer);
1322 }
1323 
draw3DTexturedQuad(GLfloat positionAttribZ,GLfloat positionAttribXYScale,bool useVertexBuffer,float layer)1324 void ANGLETestBase::draw3DTexturedQuad(GLfloat positionAttribZ,
1325                                        GLfloat positionAttribXYScale,
1326                                        bool useVertexBuffer,
1327                                        float layer)
1328 {
1329     GLuint program = get3DTexturedQuadProgram();
1330     ASSERT_NE(0u, program);
1331     GLint activeProgram = 0;
1332     glGetIntegerv(GL_CURRENT_PROGRAM, &activeProgram);
1333     if (static_cast<GLuint>(activeProgram) != program)
1334     {
1335         glUseProgram(program);
1336     }
1337     glUniform1f(glGetUniformLocation(program, "u_layer"), layer);
1338 
1339     drawQuad(program, "position", positionAttribZ, positionAttribXYScale, useVertexBuffer);
1340 
1341     if (static_cast<GLuint>(activeProgram) != program)
1342     {
1343         glUseProgram(static_cast<GLuint>(activeProgram));
1344     }
1345 }
1346 
draw2DArrayTexturedQuad(GLfloat positionAttribZ,GLfloat positionAttribXYScale,bool useVertexBuffer,float layer)1347 void ANGLETestBase::draw2DArrayTexturedQuad(GLfloat positionAttribZ,
1348                                             GLfloat positionAttribXYScale,
1349                                             bool useVertexBuffer,
1350                                             float layer)
1351 {
1352     GLuint program = get2DArrayTexturedQuadProgram();
1353     ASSERT_NE(0u, program);
1354     GLint activeProgram = 0;
1355     glGetIntegerv(GL_CURRENT_PROGRAM, &activeProgram);
1356     if (static_cast<GLuint>(activeProgram) != program)
1357     {
1358         glUseProgram(program);
1359     }
1360     glUniform1f(glGetUniformLocation(program, "u_layer"), layer);
1361 
1362     drawQuad(program, "position", positionAttribZ, positionAttribXYScale, useVertexBuffer);
1363 
1364     if (static_cast<GLuint>(activeProgram) != program)
1365     {
1366         glUseProgram(static_cast<GLuint>(activeProgram));
1367     }
1368 }
1369 
platformSupportsMultithreading() const1370 bool ANGLETestBase::platformSupportsMultithreading() const
1371 {
1372     return (mFixture && mFixture->eglWindow &&
1373             IsEGLDisplayExtensionEnabled(mFixture->eglWindow->getDisplay(),
1374                                          "EGL_ANGLE_context_virtualization")) ||
1375            IsVulkan();
1376 }
1377 
checkD3D11SDKLayersMessages()1378 void ANGLETestBase::checkD3D11SDKLayersMessages()
1379 {
1380 #if defined(ANGLE_ENABLE_D3D11)
1381     // On Windows D3D11, check ID3D11InfoQueue to see if any D3D11 SDK Layers messages
1382     // were outputted by the test. We enable the Debug layers in Release tests as well.
1383     if (mIgnoreD3D11SDKLayersWarnings ||
1384         mFixture->eglWindow->getPlatform().renderer != EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE ||
1385         mFixture->eglWindow->getDisplay() == EGL_NO_DISPLAY)
1386     {
1387         return;
1388     }
1389 
1390     const char *extensionString = static_cast<const char *>(
1391         eglQueryString(mFixture->eglWindow->getDisplay(), EGL_EXTENSIONS));
1392     if (!extensionString)
1393     {
1394         std::cout << "Error getting extension string from EGL Window." << std::endl;
1395         return;
1396     }
1397 
1398     if (!strstr(extensionString, "EGL_EXT_device_query"))
1399     {
1400         return;
1401     }
1402 
1403     EGLAttrib device      = 0;
1404     EGLAttrib angleDevice = 0;
1405 
1406     ASSERT_EGL_TRUE(
1407         eglQueryDisplayAttribEXT(mFixture->eglWindow->getDisplay(), EGL_DEVICE_EXT, &angleDevice));
1408     ASSERT_EGL_TRUE(eglQueryDeviceAttribEXT(reinterpret_cast<EGLDeviceEXT>(angleDevice),
1409                                             EGL_D3D11_DEVICE_ANGLE, &device));
1410     ID3D11Device *d3d11Device = reinterpret_cast<ID3D11Device *>(device);
1411 
1412     ID3D11InfoQueue *infoQueue = nullptr;
1413     HRESULT hr =
1414         d3d11Device->QueryInterface(__uuidof(infoQueue), reinterpret_cast<void **>(&infoQueue));
1415     if (SUCCEEDED(hr))
1416     {
1417         UINT64 numStoredD3DDebugMessages =
1418             infoQueue->GetNumStoredMessagesAllowedByRetrievalFilter();
1419 
1420         if (numStoredD3DDebugMessages > 0)
1421         {
1422             for (UINT64 i = 0; i < numStoredD3DDebugMessages; i++)
1423             {
1424                 SIZE_T messageLength = 0;
1425                 hr                   = infoQueue->GetMessage(i, nullptr, &messageLength);
1426 
1427                 if (SUCCEEDED(hr))
1428                 {
1429                     D3D11_MESSAGE *pMessage =
1430                         reinterpret_cast<D3D11_MESSAGE *>(malloc(messageLength));
1431                     infoQueue->GetMessage(i, pMessage, &messageLength);
1432 
1433                     std::cout << "Message " << i << ":" << " " << pMessage->pDescription << "\n";
1434                     free(pMessage);
1435                 }
1436             }
1437             // Clear the queue, so that previous failures are not reported
1438             // for subsequent, otherwise passing, tests
1439             infoQueue->ClearStoredMessages();
1440 
1441             FAIL() << numStoredD3DDebugMessages
1442                    << " D3D11 SDK Layers message(s) detected! Test Failed.\n";
1443         }
1444     }
1445 
1446     SafeRelease(infoQueue);
1447 #endif  // defined(ANGLE_ENABLE_D3D11)
1448 }
1449 
setWindowWidth(int width)1450 void ANGLETestBase::setWindowWidth(int width)
1451 {
1452     mWidth = width;
1453 }
1454 
setWindowHeight(int height)1455 void ANGLETestBase::setWindowHeight(int height)
1456 {
1457     mHeight = height;
1458 }
1459 
getGLWindow() const1460 GLWindowBase *ANGLETestBase::getGLWindow() const
1461 {
1462     // WGL tests are currently disabled.
1463     assert(!mFixture->wglWindow);
1464     return mFixture->eglWindow;
1465 }
1466 
setConfigRedBits(int bits)1467 void ANGLETestBase::setConfigRedBits(int bits)
1468 {
1469     mFixture->configParams.redBits = bits;
1470 }
1471 
setConfigGreenBits(int bits)1472 void ANGLETestBase::setConfigGreenBits(int bits)
1473 {
1474     mFixture->configParams.greenBits = bits;
1475 }
1476 
setConfigBlueBits(int bits)1477 void ANGLETestBase::setConfigBlueBits(int bits)
1478 {
1479     mFixture->configParams.blueBits = bits;
1480 }
1481 
setConfigAlphaBits(int bits)1482 void ANGLETestBase::setConfigAlphaBits(int bits)
1483 {
1484     mFixture->configParams.alphaBits = bits;
1485 }
1486 
setConfigDepthBits(int bits)1487 void ANGLETestBase::setConfigDepthBits(int bits)
1488 {
1489     mFixture->configParams.depthBits = bits;
1490 }
1491 
setConfigStencilBits(int bits)1492 void ANGLETestBase::setConfigStencilBits(int bits)
1493 {
1494     mFixture->configParams.stencilBits = bits;
1495 }
1496 
setConfigComponentType(EGLenum componentType)1497 void ANGLETestBase::setConfigComponentType(EGLenum componentType)
1498 {
1499     mFixture->configParams.componentType = componentType;
1500 }
1501 
setMultisampleEnabled(bool enabled)1502 void ANGLETestBase::setMultisampleEnabled(bool enabled)
1503 {
1504     mFixture->configParams.multisample = enabled;
1505 }
1506 
setSamples(EGLint samples)1507 void ANGLETestBase::setSamples(EGLint samples)
1508 {
1509     mFixture->configParams.samples = samples;
1510 }
1511 
setDebugEnabled(bool enabled)1512 void ANGLETestBase::setDebugEnabled(bool enabled)
1513 {
1514     mFixture->configParams.debug = enabled;
1515 }
1516 
setNoErrorEnabled(bool enabled)1517 void ANGLETestBase::setNoErrorEnabled(bool enabled)
1518 {
1519     mFixture->configParams.noError = enabled;
1520 }
1521 
setWebGLCompatibilityEnabled(bool webglCompatibility)1522 void ANGLETestBase::setWebGLCompatibilityEnabled(bool webglCompatibility)
1523 {
1524     mFixture->configParams.webGLCompatibility = webglCompatibility;
1525 }
1526 
setExtensionsEnabled(bool extensionsEnabled)1527 void ANGLETestBase::setExtensionsEnabled(bool extensionsEnabled)
1528 {
1529     mFixture->configParams.extensionsEnabled = extensionsEnabled;
1530 }
1531 
setRobustAccess(bool enabled)1532 void ANGLETestBase::setRobustAccess(bool enabled)
1533 {
1534     mFixture->configParams.robustAccess = enabled;
1535 }
1536 
setBindGeneratesResource(bool bindGeneratesResource)1537 void ANGLETestBase::setBindGeneratesResource(bool bindGeneratesResource)
1538 {
1539     mFixture->configParams.bindGeneratesResource = bindGeneratesResource;
1540 }
1541 
setClientArraysEnabled(bool enabled)1542 void ANGLETestBase::setClientArraysEnabled(bool enabled)
1543 {
1544     mFixture->configParams.clientArraysEnabled = enabled;
1545 }
1546 
setRobustResourceInit(bool enabled)1547 void ANGLETestBase::setRobustResourceInit(bool enabled)
1548 {
1549     mFixture->configParams.robustResourceInit = enabled;
1550 }
1551 
setMutableRenderBuffer(bool enabled)1552 void ANGLETestBase::setMutableRenderBuffer(bool enabled)
1553 {
1554     mFixture->configParams.mutableRenderBuffer = enabled;
1555 }
1556 
setContextProgramCacheEnabled(bool enabled)1557 void ANGLETestBase::setContextProgramCacheEnabled(bool enabled)
1558 {
1559     mFixture->configParams.contextProgramCacheEnabled = enabled;
1560 }
1561 
setContextResetStrategy(EGLenum resetStrategy)1562 void ANGLETestBase::setContextResetStrategy(EGLenum resetStrategy)
1563 {
1564     mFixture->configParams.resetStrategy = resetStrategy;
1565 }
1566 
forceNewDisplay()1567 void ANGLETestBase::forceNewDisplay()
1568 {
1569     mForceNewDisplay = true;
1570 }
1571 
setDeferContextInit(bool enabled)1572 void ANGLETestBase::setDeferContextInit(bool enabled)
1573 {
1574     mDeferContextInit = enabled;
1575 }
1576 
getClientMajorVersion() const1577 int ANGLETestBase::getClientMajorVersion() const
1578 {
1579     return getGLWindow()->getClientMajorVersion();
1580 }
1581 
getClientMinorVersion() const1582 int ANGLETestBase::getClientMinorVersion() const
1583 {
1584     return getGLWindow()->getClientMinorVersion();
1585 }
1586 
getEGLWindow() const1587 EGLWindow *ANGLETestBase::getEGLWindow() const
1588 {
1589     return mFixture->eglWindow;
1590 }
1591 
getWindowWidth() const1592 int ANGLETestBase::getWindowWidth() const
1593 {
1594     return mWidth;
1595 }
1596 
getWindowHeight() const1597 int ANGLETestBase::getWindowHeight() const
1598 {
1599     return mHeight;
1600 }
1601 
setWindowVisible(OSWindow * osWindow,bool isVisible)1602 void ANGLETestBase::setWindowVisible(OSWindow *osWindow, bool isVisible)
1603 {
1604     // SwiftShader windows are not required to be visible for test correctness,
1605     // moreover, making a SwiftShader window visible flaky hangs on Xvfb, so we keep them hidden.
1606     if (isSwiftshader())
1607     {
1608         return;
1609     }
1610     osWindow->setVisible(isVisible);
1611 }
1612 
1613 ANGLETestBase::TestFixture::TestFixture()  = default;
1614 ANGLETestBase::TestFixture::~TestFixture() = default;
1615 
getPlatformRenderer() const1616 EGLint ANGLETestBase::getPlatformRenderer() const
1617 {
1618     assert(mFixture->eglWindow);
1619     return mFixture->eglWindow->getPlatform().renderer;
1620 }
1621 
ignoreD3D11SDKLayersWarnings()1622 void ANGLETestBase::ignoreD3D11SDKLayersWarnings()
1623 {
1624     // Some tests may need to disable the D3D11 SDK Layers Warnings checks
1625     mIgnoreD3D11SDKLayersWarnings = true;
1626 }
1627 
ScopedIgnorePlatformMessages()1628 ANGLETestBase::ScopedIgnorePlatformMessages::ScopedIgnorePlatformMessages()
1629 {
1630     gPlatformContext.ignoreMessages = true;
1631 }
1632 
~ScopedIgnorePlatformMessages()1633 ANGLETestBase::ScopedIgnorePlatformMessages::~ScopedIgnorePlatformMessages()
1634 {
1635     gPlatformContext.ignoreMessages = false;
1636 }
1637 
1638 OSWindow *ANGLETestBase::mOSWindowSingleton = nullptr;
1639 std::map<angle::PlatformParameters, ANGLETestBase::TestFixture> ANGLETestBase::gFixtures;
1640 Optional<EGLint> ANGLETestBase::mLastRendererType;
1641 Optional<angle::GLESDriverType> ANGLETestBase::mLastLoadedDriver;
1642 
1643 std::unique_ptr<Library> ANGLETestEnvironment::gAngleEGLLibrary;
1644 std::unique_ptr<Library> ANGLETestEnvironment::gAngleVulkanSecondariesEGLLibrary;
1645 std::unique_ptr<Library> ANGLETestEnvironment::gMesaEGLLibrary;
1646 std::unique_ptr<Library> ANGLETestEnvironment::gSystemEGLLibrary;
1647 std::unique_ptr<Library> ANGLETestEnvironment::gSystemWGLLibrary;
1648 
SetUp()1649 void ANGLETestEnvironment::SetUp() {}
1650 
TearDown()1651 void ANGLETestEnvironment::TearDown()
1652 {
1653     ANGLETestBase::ReleaseFixtures();
1654 }
1655 
1656 // static
GetDriverLibrary(angle::GLESDriverType driver)1657 Library *ANGLETestEnvironment::GetDriverLibrary(angle::GLESDriverType driver)
1658 {
1659     switch (driver)
1660     {
1661         case angle::GLESDriverType::AngleEGL:
1662             return GetAngleEGLLibrary();
1663         case angle::GLESDriverType::AngleVulkanSecondariesEGL:
1664             return GetAngleVulkanSecondariesEGLLibrary();
1665         case angle::GLESDriverType::SystemEGL:
1666             return GetSystemEGLLibrary();
1667         case angle::GLESDriverType::SystemWGL:
1668             return GetSystemWGLLibrary();
1669         case angle::GLESDriverType::ZinkEGL:
1670             return GetMesaEGLLibrary();
1671         default:
1672             return nullptr;
1673     }
1674 }
1675 
1676 // static
GetAngleEGLLibrary()1677 Library *ANGLETestEnvironment::GetAngleEGLLibrary()
1678 {
1679 #if defined(ANGLE_USE_UTIL_LOADER)
1680     if (!gAngleEGLLibrary)
1681     {
1682         gAngleEGLLibrary.reset(OpenSharedLibrary(ANGLE_EGL_LIBRARY_NAME, SearchType::ModuleDir));
1683     }
1684 #endif  // defined(ANGLE_USE_UTIL_LOADER)
1685     return gAngleEGLLibrary.get();
1686 }
1687 
1688 // static
GetAngleVulkanSecondariesEGLLibrary()1689 Library *ANGLETestEnvironment::GetAngleVulkanSecondariesEGLLibrary()
1690 {
1691 #if defined(ANGLE_USE_UTIL_LOADER)
1692     if (!gAngleVulkanSecondariesEGLLibrary)
1693     {
1694         gAngleVulkanSecondariesEGLLibrary.reset(
1695             OpenSharedLibrary(ANGLE_VULKAN_SECONDARIES_EGL_LIBRARY_NAME, SearchType::ModuleDir));
1696     }
1697 #endif  // defined(ANGLE_USE_UTIL_LOADER)
1698     return gAngleVulkanSecondariesEGLLibrary.get();
1699 }
1700 
1701 // static
GetMesaEGLLibrary()1702 Library *ANGLETestEnvironment::GetMesaEGLLibrary()
1703 {
1704 #if defined(ANGLE_USE_UTIL_LOADER)
1705     if (!gMesaEGLLibrary)
1706     {
1707         gMesaEGLLibrary.reset(
1708             OpenSharedLibrary(ANGLE_MESA_EGL_LIBRARY_NAME, SearchType::ModuleDir));
1709     }
1710 #endif  // defined(ANGLE_USE_UTIL_LOADER)
1711     return gMesaEGLLibrary.get();
1712 }
1713 
1714 // static
GetSystemEGLLibrary()1715 Library *ANGLETestEnvironment::GetSystemEGLLibrary()
1716 {
1717 #if defined(ANGLE_USE_UTIL_LOADER)
1718     if (!gSystemEGLLibrary)
1719     {
1720         gSystemEGLLibrary.reset(OpenSharedLibraryWithExtension(
1721             GetNativeEGLLibraryNameWithExtension(), SearchType::SystemDir));
1722     }
1723 #endif  // defined(ANGLE_USE_UTIL_LOADER)
1724     return gSystemEGLLibrary.get();
1725 }
1726 
1727 // static
GetSystemWGLLibrary()1728 Library *ANGLETestEnvironment::GetSystemWGLLibrary()
1729 {
1730 #if defined(ANGLE_USE_UTIL_LOADER) && defined(ANGLE_PLATFORM_WINDOWS)
1731     if (!gSystemWGLLibrary)
1732     {
1733         gSystemWGLLibrary.reset(OpenSharedLibrary("opengl32", SearchType::SystemDir));
1734     }
1735 #endif  // defined(ANGLE_USE_UTIL_LOADER) && defined(ANGLE_PLATFORM_WINDOWS)
1736     return gSystemWGLLibrary.get();
1737 }
1738 
ANGLEProcessTestArgs(int * argc,char * argv[])1739 void ANGLEProcessTestArgs(int *argc, char *argv[])
1740 {
1741     testing::AddGlobalTestEnvironment(new ANGLETestEnvironment());
1742 
1743     for (int argIndex = 1; argIndex < *argc; argIndex++)
1744     {
1745         if (strncmp(argv[argIndex], kUseConfig, strlen(kUseConfig)) == 0)
1746         {
1747             SetSelectedConfig(argv[argIndex] + strlen(kUseConfig));
1748         }
1749         else if (strncmp(argv[argIndex], kReuseDisplays, strlen(kReuseDisplays)) == 0)
1750         {
1751             gReuseDisplays = true;
1752         }
1753         else if (strncmp(argv[argIndex], kBatchId, strlen(kBatchId)) == 0)
1754         {
1755             // Enable display reuse when running under --bot-mode.
1756             gReuseDisplays = true;
1757         }
1758         else if (strncmp(argv[argIndex], kEnableANGLEPerTestCaptureLabel,
1759                          strlen(kEnableANGLEPerTestCaptureLabel)) == 0)
1760         {
1761             gEnableANGLEPerTestCaptureLabel = true;
1762         }
1763         else if (strncmp(argv[argIndex], kDelayTestStart, strlen(kDelayTestStart)) == 0)
1764         {
1765             SetTestStartDelay(argv[argIndex] + strlen(kDelayTestStart));
1766         }
1767         else if (strncmp(argv[argIndex], kRenderDoc, strlen(kRenderDoc)) == 0)
1768         {
1769             gEnableRenderDocCapture = true;
1770         }
1771         else if (strncmp(argv[argIndex], kNoRenderDoc, strlen(kNoRenderDoc)) == 0)
1772         {
1773             gEnableRenderDocCapture = false;
1774         }
1775     }
1776 }
1777