xref: /aosp_15_r20/external/angle/src/tests/test_utils/ANGLETest.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2012 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 #ifndef ANGLE_TESTS_ANGLE_TEST_H_
11 #define ANGLE_TESTS_ANGLE_TEST_H_
12 
13 #include <gtest/gtest.h>
14 #include <algorithm>
15 #include <array>
16 
17 #include "RenderDoc.h"
18 #include "angle_test_configs.h"
19 #include "angle_test_platform.h"
20 #include "common/angleutils.h"
21 #include "common/system_utils.h"
22 #include "common/vector_utils.h"
23 #include "platform/PlatformMethods.h"
24 #include "util/EGLWindow.h"
25 #include "util/shader_utils.h"
26 #include "util/util_gl.h"
27 
28 namespace angle
29 {
30 struct SystemInfo;
31 class RNG;
32 }  // namespace angle
33 
34 #define ASSERT_GL_TRUE(a) ASSERT_EQ(static_cast<GLboolean>(GL_TRUE), (a))
35 #define ASSERT_GL_FALSE(a) ASSERT_EQ(static_cast<GLboolean>(GL_FALSE), (a))
36 #define EXPECT_GL_TRUE(a) EXPECT_EQ(static_cast<GLboolean>(GL_TRUE), (a))
37 #define EXPECT_GL_FALSE(a) EXPECT_EQ(static_cast<GLboolean>(GL_FALSE), (a))
38 
39 #define EXPECT_GL_ERROR(err) EXPECT_EQ(static_cast<GLenum>(err), glGetError())
40 #define EXPECT_GL_NO_ERROR() EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError())
41 
42 #define ASSERT_GL_ERROR(err) ASSERT_EQ(static_cast<GLenum>(err), glGetError())
43 #define ASSERT_GL_NO_ERROR() ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError())
44 
45 #define EXPECT_EGL_ERROR(err) EXPECT_EQ((err), eglGetError())
46 #define EXPECT_EGL_SUCCESS() EXPECT_EGL_ERROR(EGL_SUCCESS)
47 
48 // EGLBoolean is |unsigned int| but EGL_TRUE is 0, not 0u.
49 #define ASSERT_EGL_TRUE(a) ASSERT_EQ(static_cast<EGLBoolean>(EGL_TRUE), static_cast<EGLBoolean>(a))
50 #define ASSERT_EGL_FALSE(a) \
51     ASSERT_EQ(static_cast<EGLBoolean>(EGL_FALSE), static_cast<EGLBoolean>(a))
52 #define EXPECT_EGL_TRUE(a) EXPECT_EQ(static_cast<EGLBoolean>(EGL_TRUE), static_cast<EGLBoolean>(a))
53 #define EXPECT_EGL_FALSE(a) \
54     EXPECT_EQ(static_cast<EGLBoolean>(EGL_FALSE), static_cast<EGLBoolean>(a))
55 
56 #define ASSERT_EGL_ERROR(err) ASSERT_EQ((err), eglGetError())
57 #define ASSERT_EGL_SUCCESS() ASSERT_EGL_ERROR(EGL_SUCCESS)
58 
59 #define ASSERT_GLENUM_EQ(expected, actual) \
60     ASSERT_EQ(static_cast<GLenum>(expected), static_cast<GLenum>(actual))
61 #define EXPECT_GLENUM_EQ(expected, actual) \
62     EXPECT_EQ(static_cast<GLenum>(expected), static_cast<GLenum>(actual))
63 #define ASSERT_GLENUM_NE(expected, actual) \
64     ASSERT_NE(static_cast<GLenum>(expected), static_cast<GLenum>(actual))
65 #define EXPECT_GLENUM_NE(expected, actual) \
66     EXPECT_NE(static_cast<GLenum>(expected), static_cast<GLenum>(actual))
67 
68 testing::AssertionResult AssertEGLEnumsEqual(const char *lhsExpr,
69                                              const char *rhsExpr,
70                                              EGLenum lhs,
71                                              EGLenum rhs);
72 
73 #define ASSERT_EGLENUM_EQ(expected, actual)                                  \
74     ASSERT_PRED_FORMAT2(AssertEGLEnumsEqual, static_cast<EGLenum>(expected), \
75                         static_cast<EGLenum>(actual))
76 #define EXPECT_EGLENUM_EQ(expected, actual)                                  \
77     EXPECT_PRED_FORMAT2(AssertEGLEnumsEqual, static_cast<EGLenum>(expected), \
78                         static_cast<EGLenum>(actual))
79 
80 #define ASSERT_GL_FRAMEBUFFER_COMPLETE(framebuffer) \
81     ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(framebuffer))
82 #define EXPECT_GL_FRAMEBUFFER_COMPLETE(framebuffer) \
83     EXPECT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(framebuffer))
84 
85 namespace angle
86 {
87 struct GLColorRGB
88 {
GLColorRGBGLColorRGB89     constexpr GLColorRGB() : R(0), G(0), B(0) {}
GLColorRGBGLColorRGB90     constexpr GLColorRGB(GLubyte r, GLubyte g, GLubyte b) : R(r), G(g), B(b) {}
91     GLColorRGB(const angle::Vector3 &floatColor);
92 
dataGLColorRGB93     const GLubyte *data() const { return &R; }
dataGLColorRGB94     GLubyte *data() { return &R; }
95 
96     GLubyte R, G, B;
97 
98     static const GLColorRGB black;
99     static const GLColorRGB blue;
100     static const GLColorRGB green;
101     static const GLColorRGB red;
102     static const GLColorRGB yellow;
103 };
104 
105 struct GLColorRG
106 {
GLColorRGGLColorRG107     constexpr GLColorRG() : R(0), G(0) {}
GLColorRGGLColorRG108     constexpr GLColorRG(GLubyte r, GLubyte g) : R(r), G(g) {}
109     GLColorRG(const angle::Vector2 &floatColor);
110 
dataGLColorRG111     const GLubyte *data() const { return &R; }
dataGLColorRG112     GLubyte *data() { return &R; }
113 
114     GLubyte R, G;
115 };
116 
117 struct GLColorR
118 {
GLColorRGLColorR119     constexpr GLColorR() : R(0) {}
GLColorRGLColorR120     constexpr GLColorR(GLubyte r) : R(r) {}
121     GLColorR(const float floatColor);
122 
dataGLColorR123     const GLubyte *data() const { return &R; }
dataGLColorR124     GLubyte *data() { return &R; }
125 
126     GLubyte R;
127 };
128 
129 struct GLColor
130 {
GLColorGLColor131     constexpr GLColor() : R(0), G(0), B(0), A(0) {}
GLColorGLColor132     constexpr GLColor(GLubyte r, GLubyte g, GLubyte b, GLubyte a) : R(r), G(g), B(b), A(a) {}
133     GLColor(const angle::Vector3 &floatColor);
134     GLColor(const angle::Vector4 &floatColor);
135     GLColor(GLuint colorValue);
136 
137     angle::Vector4 toNormalizedVector() const;
138 
139     GLubyte &operator[](size_t index) { return (&R)[index]; }
140 
141     const GLubyte &operator[](size_t index) const { return (&R)[index]; }
142 
dataGLColor143     const GLubyte *data() const { return &R; }
dataGLColor144     GLubyte *data() { return &R; }
145 
146     GLuint asUint() const;
147 
148     testing::AssertionResult ExpectNear(const GLColor &expected, const GLColor &err) const;
149 
150     GLubyte R, G, B, A;
151 
152     static const GLColor black;
153     static const GLColor blue;
154     static const GLColor cyan;
155     static const GLColor green;
156     static const GLColor red;
157     static const GLColor transparentBlack;
158     static const GLColor white;
159     static const GLColor yellow;
160     static const GLColor magenta;
161 };
162 
163 template <typename T>
164 struct GLColorT
165 {
GLColorTGLColorT166     constexpr GLColorT() : GLColorT(0, 0, 0, 0) {}
GLColorTGLColorT167     constexpr GLColorT(T r, T g, T b, T a) : R(r), G(g), B(b), A(a) {}
168 
169     T R, G, B, A;
170 };
171 
172 using GLColor16   = GLColorT<uint16_t>;
173 using GLColor32F  = GLColorT<float>;
174 using GLColor32I  = GLColorT<int32_t>;
175 using GLColor32UI = GLColorT<uint32_t>;
176 
177 static constexpr GLColor32F kFloatBlack = {0.0f, 0.0f, 0.0f, 1.0f};
178 static constexpr GLColor32F kFloatRed   = {1.0f, 0.0f, 0.0f, 1.0f};
179 static constexpr GLColor32F kFloatGreen = {0.0f, 1.0f, 0.0f, 1.0f};
180 static constexpr GLColor32F kFloatBlue  = {0.0f, 0.0f, 1.0f, 1.0f};
181 
182 // The input here for pixelPoints are the expected integer window coordinates, we add .5 to every
183 // one of them and re-scale the numbers to be between [-1,1]. Using this technique, we can make
184 // sure the rasterization stage will end up drawing pixels at the expected locations.
185 void CreatePixelCenterWindowCoords(const std::vector<Vector2> &pixelPoints,
186                                    int windowWidth,
187                                    int windowHeight,
188                                    std::vector<Vector3> *outVertices);
189 
190 // Useful to cast any type to GLubyte.
191 template <typename TR, typename TG, typename TB, typename TA>
MakeGLColor(TR r,TG g,TB b,TA a)192 GLColor MakeGLColor(TR r, TG g, TB b, TA a)
193 {
194     return GLColor(static_cast<GLubyte>(r), static_cast<GLubyte>(g), static_cast<GLubyte>(b),
195                    static_cast<GLubyte>(a));
196 }
197 
198 GLColor RandomColor(angle::RNG *rng);
199 
200 bool operator==(const GLColor &a, const GLColor &b);
201 bool operator!=(const GLColor &a, const GLColor &b);
202 std::ostream &operator<<(std::ostream &ostream, const GLColor &color);
203 GLColor ReadColor(GLint x, GLint y);
204 
205 bool operator==(const GLColorRGB &a, const GLColorRGB &b);
206 bool operator!=(const GLColorRGB &a, const GLColorRGB &b);
207 std::ostream &operator<<(std::ostream &ostream, const GLColorRGB &color);
208 
209 // Useful to cast any type to GLfloat.
210 template <typename TR, typename TG, typename TB, typename TA>
MakeGLColor32F(TR r,TG g,TB b,TA a)211 GLColor32F MakeGLColor32F(TR r, TG g, TB b, TA a)
212 {
213     return GLColor32F(static_cast<GLfloat>(r), static_cast<GLfloat>(g), static_cast<GLfloat>(b),
214                       static_cast<GLfloat>(a));
215 }
216 
217 template <typename T>
218 bool operator==(const GLColorT<T> &a, const GLColorT<T> &b)
219 {
220     return a.R == b.R && a.G == b.G && a.B == b.B && a.A == b.A;
221 }
222 
223 std::ostream &operator<<(std::ostream &ostream, const GLColor32F &color);
224 GLColor32F ReadColor32F(GLint x, GLint y);
225 
226 constexpr std::array<GLenum, 6> kCubeFaces = {
227     {GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
228      GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
229      GL_TEXTURE_CUBE_MAP_NEGATIVE_Z}};
230 
231 void LoadEntryPointsWithUtilLoader(angle::GLESDriverType driver);
232 
233 bool IsFormatEmulated(GLenum target);
234 }  // namespace angle
235 
236 #define EXPECT_PIXEL_EQ(x, y, r, g, b, a) \
237     EXPECT_EQ(angle::MakeGLColor(r, g, b, a), angle::ReadColor(x, y))
238 
239 #define EXPECT_PIXEL_NE(x, y, r, g, b, a) \
240     EXPECT_NE(angle::MakeGLColor(r, g, b, a), angle::ReadColor(x, y))
241 
242 #define EXPECT_PIXEL_32F_EQ(x, y, r, g, b, a) \
243     EXPECT_EQ(angle::MakeGLColor32F(r, g, b, a), angle::ReadColor32F(x, y))
244 
245 #define EXPECT_PIXEL_ALPHA_EQ(x, y, a) EXPECT_EQ(a, angle::ReadColor(x, y).A)
246 
247 #define EXPECT_PIXEL_ALPHA_NEAR(x, y, a, abs_error) \
248     EXPECT_NEAR(a, angle::ReadColor(x, y).A, abs_error);
249 
250 #define EXPECT_PIXEL_ALPHA32F_EQ(x, y, a) EXPECT_EQ(a, angle::ReadColor32F(x, y).A)
251 
252 #define EXPECT_PIXEL_COLOR_EQ(x, y, angleColor) EXPECT_EQ(angleColor, angle::ReadColor(x, y))
253 #define EXPECT_PIXEL_COLOR_EQ_VEC2(vec2, angleColor) \
254     EXPECT_EQ(angleColor,                            \
255               angle::ReadColor(static_cast<GLint>(vec2.x()), static_cast<GLint>(vec2.y())))
256 
257 #define EXPECT_PIXEL_COLOR32F_EQ(x, y, angleColor) EXPECT_EQ(angleColor, angle::ReadColor32F(x, y))
258 
259 #define EXPECT_PIXEL_RECT_T_EQ(T, x, y, width, height, format, type, color)           \
260     do                                                                                \
261     {                                                                                 \
262         std::vector<T> actualColors((width) * (height));                              \
263         glReadPixels((x), (y), (width), (height), format, type, actualColors.data()); \
264         std::vector<T> expectedColors((width) * (height), color);                     \
265         EXPECT_EQ(expectedColors, actualColors);                                      \
266     } while (0)
267 
268 #define EXPECT_PIXEL_RECT_EQ(x, y, width, height, color) \
269     EXPECT_PIXEL_RECT_T_EQ(GLColor, x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, color)
270 
271 #define EXPECT_PIXEL_RECT32F_EQ(x, y, width, height, color) \
272     EXPECT_PIXEL_RECT_T_EQ(GLColor32F, x, y, width, height, GL_RGBA, GL_FLOAT, color)
273 
274 #define EXPECT_PIXEL_RECT32I_EQ(x, y, width, height, color) \
275     EXPECT_PIXEL_RECT_T_EQ(GLColor32I, x, y, width, height, GL_RGBA_INTEGER, GL_INT, color)
276 
277 #define EXPECT_PIXEL_RECT32UI_EQ(x, y, width, height, color)                                   \
278     EXPECT_PIXEL_RECT_T_EQ(GLColor32UI, x, y, width, height, GL_RGBA_INTEGER, GL_UNSIGNED_INT, \
279                            color)
280 
281 #define EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, ctype, format, type) \
282     do                                                                             \
283     {                                                                              \
284         ctype pixel[4];                                                            \
285         glReadPixels((x), (y), 1, 1, format, type, pixel);                         \
286         EXPECT_GL_NO_ERROR();                                                      \
287         EXPECT_NEAR((r), pixel[0], abs_error);                                     \
288         EXPECT_NEAR((g), pixel[1], abs_error);                                     \
289         EXPECT_NEAR((b), pixel[2], abs_error);                                     \
290         EXPECT_NEAR((a), pixel[3], abs_error);                                     \
291     } while (0)
292 
293 #define EXPECT_PIXEL_EQ_HELPER(x, y, r, g, b, a, ctype, format, type) \
294     do                                                                \
295     {                                                                 \
296         ctype pixel[4];                                               \
297         glReadPixels((x), (y), 1, 1, format, type, pixel);            \
298         EXPECT_GL_NO_ERROR();                                         \
299         EXPECT_EQ((r), pixel[0]);                                     \
300         EXPECT_EQ((g), pixel[1]);                                     \
301         EXPECT_EQ((b), pixel[2]);                                     \
302         EXPECT_EQ((a), pixel[3]);                                     \
303     } while (0)
304 
305 #define EXPECT_PIXEL_NEAR(x, y, r, g, b, a, abs_error) \
306     EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, GLubyte, GL_RGBA, GL_UNSIGNED_BYTE)
307 
308 #define EXPECT_PIXEL_16_NEAR(x, y, r, g, b, a, abs_error) \
309     EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, GLushort, GL_RGBA, GL_UNSIGNED_SHORT)
310 
311 #define EXPECT_PIXEL_8S_NEAR(x, y, r, g, b, a, abs_error) \
312     EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, GLbyte, GL_RGBA, GL_BYTE)
313 
314 #define EXPECT_PIXEL_16S_NEAR(x, y, r, g, b, a, abs_error) \
315     EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, GLshort, GL_RGBA, GL_SHORT)
316 
317 #define EXPECT_PIXEL_32F_NEAR(x, y, r, g, b, a, abs_error) \
318     EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, GLfloat, GL_RGBA, GL_FLOAT)
319 
320 #define EXPECT_PIXEL_8I(x, y, r, g, b, a) \
321     EXPECT_PIXEL_EQ_HELPER(x, y, r, g, b, a, GLbyte, GL_RGBA_INTEGER, GL_BYTE)
322 
323 #define EXPECT_PIXEL_8UI(x, y, r, g, b, a) \
324     EXPECT_PIXEL_EQ_HELPER(x, y, r, g, b, a, GLubyte, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE)
325 
326 #define EXPECT_PIXEL_32UI(x, y, r, g, b, a) \
327     EXPECT_PIXEL_EQ_HELPER(x, y, r, g, b, a, GLuint, GL_RGBA_INTEGER, GL_UNSIGNED_INT)
328 
329 #define EXPECT_PIXEL_32I(x, y, r, g, b, a) \
330     EXPECT_PIXEL_EQ_HELPER(x, y, r, g, b, a, GLint, GL_RGBA_INTEGER, GL_INT)
331 
332 #define EXPECT_PIXEL_32UI_COLOR(x, y, color) \
333     EXPECT_PIXEL_32UI(x, y, color.R, color.G, color.B, color.A)
334 
335 #define EXPECT_PIXEL_32I_COLOR(x, y, color) \
336     EXPECT_PIXEL_32I(x, y, color.R, color.G, color.B, color.A)
337 
338 // TODO(jmadill): Figure out how we can use GLColor's nice printing with EXPECT_NEAR.
339 #define EXPECT_PIXEL_COLOR_NEAR(x, y, angleColor, abs_error) \
340     EXPECT_PIXEL_NEAR(x, y, angleColor.R, angleColor.G, angleColor.B, angleColor.A, abs_error)
341 
342 #define EXPECT_PIXEL_COLOR16_NEAR(x, y, angleColor, abs_error) \
343     EXPECT_PIXEL_16_NEAR(x, y, angleColor.R, angleColor.G, angleColor.B, angleColor.A, abs_error)
344 
345 #define EXPECT_PIXEL_COLOR32F_NEAR(x, y, angleColor, abs_error) \
346     EXPECT_PIXEL32F_NEAR(x, y, angleColor.R, angleColor.G, angleColor.B, angleColor.A, abs_error)
347 
348 #define EXPECT_COLOR_NEAR(expected, actual, abs_error) \
349     do                                                 \
350     {                                                  \
351         EXPECT_NEAR(expected.R, actual.R, abs_error);  \
352         EXPECT_NEAR(expected.G, actual.G, abs_error);  \
353         EXPECT_NEAR(expected.B, actual.B, abs_error);  \
354         EXPECT_NEAR(expected.A, actual.A, abs_error);  \
355     } while (0)
356 #define EXPECT_PIXEL32F_NEAR(x, y, r, g, b, a, abs_error)       \
357     do                                                          \
358     {                                                           \
359         GLfloat pixel[4];                                       \
360         glReadPixels((x), (y), 1, 1, GL_RGBA, GL_FLOAT, pixel); \
361         EXPECT_GL_NO_ERROR();                                   \
362         EXPECT_NEAR((r), pixel[0], abs_error);                  \
363         EXPECT_NEAR((g), pixel[1], abs_error);                  \
364         EXPECT_NEAR((b), pixel[2], abs_error);                  \
365         EXPECT_NEAR((a), pixel[3], abs_error);                  \
366     } while (0)
367 
368 #define EXPECT_PIXEL_COLOR32F_NEAR(x, y, angleColor, abs_error) \
369     EXPECT_PIXEL32F_NEAR(x, y, angleColor.R, angleColor.G, angleColor.B, angleColor.A, abs_error)
370 
371 #define EXPECT_PIXEL_STENCIL_EQ(x, y, expected)                                    \
372     do                                                                             \
373     {                                                                              \
374         GLubyte actual;                                                            \
375         glReadPixels((x), (y), 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &actual); \
376         EXPECT_GL_NO_ERROR();                                                      \
377         EXPECT_EQ((expected), actual);                                             \
378     } while (0)
379 
380 class ANGLETestBase;
381 class EGLWindow;
382 class GLWindowBase;
383 class OSWindow;
384 class WGLWindow;
385 
386 struct TestPlatformContext final : private angle::NonCopyable
387 {
388     bool ignoreMessages        = false;
389     bool warningsAsErrors      = false;
390     ANGLETestBase *currentTest = nullptr;
391 };
392 
393 class ANGLETestBase
394 {
395   protected:
396     ANGLETestBase(const angle::PlatformParameters &params);
397     virtual ~ANGLETestBase();
398 
399   public:
400     void setWindowVisible(OSWindow *osWindow, bool isVisible);
401 
402     static void ReleaseFixtures();
403 
isSwiftshader()404     bool isSwiftshader() const
405     {
406         // Renderer might be swiftshader even if local swiftshader not used.
407         return mCurrentParams->isSwiftshader() || angle::IsSwiftshaderDevice();
408     }
409 
enableDebugLayers()410     bool enableDebugLayers() const
411     {
412         return mCurrentParams->eglParameters.debugLayersEnabled != EGL_FALSE;
413     }
414 
415     void *operator new(size_t size);
416     void operator delete(void *ptr);
417 
418   protected:
419     void ANGLETestSetUp();
420     void ANGLETestPreTearDown();
421     void ANGLETestTearDown();
422 
423     virtual void swapBuffers();
424 
425     void setupQuadVertexBuffer(GLfloat positionAttribZ, GLfloat positionAttribXYScale);
426     void setupIndexedQuadVertexBuffer(GLfloat positionAttribZ, GLfloat positionAttribXYScale);
427     void setupIndexedQuadIndexBuffer();
428 
429     void drawQuad(GLuint program, const std::string &positionAttribName, GLfloat positionAttribZ);
430     void drawQuad(GLuint program,
431                   const std::string &positionAttribName,
432                   GLfloat positionAttribZ,
433                   GLfloat positionAttribXYScale);
434     void drawQuad(GLuint program,
435                   const std::string &positionAttribName,
436                   GLfloat positionAttribZ,
437                   GLfloat positionAttribXYScale,
438                   bool useVertexBuffer);
439     void drawQuadInstanced(GLuint program,
440                            const std::string &positionAttribName,
441                            GLfloat positionAttribZ,
442                            GLfloat positionAttribXYScale,
443                            bool useVertexBuffer,
444                            GLuint numInstances);
445     void drawPatches(GLuint program,
446                      const std::string &positionAttribName,
447                      GLfloat positionAttribZ,
448                      GLfloat positionAttribXYScale,
449                      bool useVertexBuffer);
450 
451     void drawQuadPPO(GLuint vertProgram,
452                      const std::string &positionAttribName,
453                      const GLfloat positionAttribZ,
454                      const GLfloat positionAttribXYScale);
455 
456     static std::array<angle::Vector3, 6> GetQuadVertices();
457     static std::array<GLushort, 6> GetQuadIndices();
458     static std::array<angle::Vector3, 4> GetIndexedQuadVertices();
459 
460     void drawIndexedQuad(GLuint program,
461                          const std::string &positionAttribName,
462                          GLfloat positionAttribZ);
463     void drawIndexedQuad(GLuint program,
464                          const std::string &positionAttribName,
465                          GLfloat positionAttribZ,
466                          GLfloat positionAttribXYScale);
467     void drawIndexedQuad(GLuint program,
468                          const std::string &positionAttribName,
469                          GLfloat positionAttribZ,
470                          GLfloat positionAttribXYScale,
471                          bool useBufferObject);
472 
473     void drawIndexedQuad(GLuint program,
474                          const std::string &positionAttribName,
475                          GLfloat positionAttribZ,
476                          GLfloat positionAttribXYScale,
477                          bool useBufferObject,
478                          bool restrictedRange);
479 
480     void draw2DTexturedQuad(GLfloat positionAttribZ,
481                             GLfloat positionAttribXYScale,
482                             bool useVertexBuffer);
483 
484     // The layer parameter chooses the 3D texture layer to sample from.
485     void draw3DTexturedQuad(GLfloat positionAttribZ,
486                             GLfloat positionAttribXYScale,
487                             bool useVertexBuffer,
488                             float layer);
489 
490     // The layer parameter chooses the 2DArray texture layer to sample from.
491     void draw2DArrayTexturedQuad(GLfloat positionAttribZ,
492                                  GLfloat positionAttribXYScale,
493                                  bool useVertexBuffer,
494                                  float layer);
495 
496     void setWindowWidth(int width);
497     void setWindowHeight(int height);
498     void setConfigRedBits(int bits);
499     void setConfigGreenBits(int bits);
500     void setConfigBlueBits(int bits);
501     void setConfigAlphaBits(int bits);
502     void setConfigDepthBits(int bits);
503     void setConfigStencilBits(int bits);
504     void setConfigComponentType(EGLenum componentType);
505     void setMultisampleEnabled(bool enabled);
506     void setSamples(EGLint samples);
507     void setDebugEnabled(bool enabled);
508     void setNoErrorEnabled(bool enabled);
509     void setWebGLCompatibilityEnabled(bool webglCompatibility);
510     void setExtensionsEnabled(bool extensionsEnabled);
511     void setRobustAccess(bool enabled);
512     void setBindGeneratesResource(bool bindGeneratesResource);
513     void setClientArraysEnabled(bool enabled);
514     void setRobustResourceInit(bool enabled);
515     void setMutableRenderBuffer(bool enabled);
516     void setContextProgramCacheEnabled(bool enabled);
517     void setContextResetStrategy(EGLenum resetStrategy);
518     void forceNewDisplay();
519 
520     // Some EGL extension tests would like to defer the Context init until the test body.
521     void setDeferContextInit(bool enabled);
522 
523     int getClientMajorVersion() const;
524     int getClientMinorVersion() const;
525 
526     GLWindowBase *getGLWindow() const;
527     EGLWindow *getEGLWindow() const;
528     int getWindowWidth() const;
529     int getWindowHeight() const;
530 
531     EGLint getPlatformRenderer() const;
532 
533     void ignoreD3D11SDKLayersWarnings();
534 
getOSWindow()535     OSWindow *getOSWindow() { return mFixture->osWindow; }
536 
537     GLuint get2DTexturedQuadProgram();
538 
539     // Has a float uniform "u_layer" to choose the 3D texture layer.
540     GLuint get3DTexturedQuadProgram();
541 
542     // Has a float uniform "u_layer" to choose the 2DArray texture layer.
543     GLuint get2DArrayTexturedQuadProgram();
544 
545     class [[nodiscard]] ScopedIgnorePlatformMessages : angle::NonCopyable
546     {
547       public:
548         ScopedIgnorePlatformMessages();
549         ~ScopedIgnorePlatformMessages();
550     };
551 
552     // Can be used before we get a GL context.
isGLRenderer()553     bool isGLRenderer() const
554     {
555         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE;
556     }
557 
isGLESRenderer()558     bool isGLESRenderer() const
559     {
560         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
561     }
562 
isD3D11Renderer()563     bool isD3D11Renderer() const
564     {
565         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE;
566     }
567 
isVulkanRenderer()568     bool isVulkanRenderer() const
569     {
570         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
571     }
572 
isVulkanSwiftshaderRenderer()573     bool isVulkanSwiftshaderRenderer() const
574     {
575         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE &&
576                mCurrentParams->isSwiftshader();
577     }
578 
579     bool platformSupportsMultithreading() const;
580 
581     bool mIsSetUp = false;
582 
583   private:
584     void checkD3D11SDKLayersMessages();
585 
586     void drawQuad(GLuint program,
587                   const std::string &positionAttribName,
588                   GLfloat positionAttribZ,
589                   GLfloat positionAttribXYScale,
590                   bool useVertexBuffer,
591                   bool useInstancedDrawCalls,
592                   bool useTessellationPatches,
593                   GLuint numInstances);
594 
595     void initOSWindow();
596 
597     struct TestFixture
598     {
599         TestFixture();
600         ~TestFixture();
601 
602         EGLWindow *eglWindow = nullptr;
603         WGLWindow *wglWindow = nullptr;
604         OSWindow *osWindow   = nullptr;
605         ConfigParameters configParams;
606         uint32_t reuseCounter = 0;
607     };
608 
609     int mWidth;
610     int mHeight;
611 
612     bool mIgnoreD3D11SDKLayersWarnings;
613 
614     // Used for indexed quad rendering
615     GLuint mQuadVertexBuffer;
616     GLuint mQuadIndexBuffer;
617 
618     // Used for texture rendering.
619     GLuint m2DTexturedQuadProgram;
620     GLuint m3DTexturedQuadProgram;
621     GLuint m2DArrayTexturedQuadProgram;
622 
623     bool mDeferContextInit;
624     bool mAlwaysForceNewDisplay;
625     bool mForceNewDisplay;
626 
627     bool mSetUpCalled;
628     bool mTearDownCalled;
629 
630     // Usually, we use an OS Window per "fixture" (a frontend and backend combination).
631     // This allows:
632     // 1. Reusing EGL Display on Windows.
633     //    Other platforms have issues with display reuse even if a window per fixture is used.
634     // 2. Hiding only SwiftShader OS Window on Linux.
635     //    OS Windows for other backends must be visible, to allow driver to communicate with X11.
636     // However, we must use a single OS Window for all backends on Android,
637     // since test Application can have only one window.
638     static OSWindow *mOSWindowSingleton;
639 
640     static std::map<angle::PlatformParameters, TestFixture> gFixtures;
641     const angle::PlatformParameters *mCurrentParams;
642     TestFixture *mFixture;
643 
644     RenderDoc mRenderDoc;
645 
646     // Workaround for NVIDIA not being able to share a window with OpenGL and Vulkan.
647     static Optional<EGLint> mLastRendererType;
648     static Optional<angle::GLESDriverType> mLastLoadedDriver;
649 };
650 
651 template <typename Params = angle::PlatformParameters>
652 class ANGLETest : public ANGLETestBase, public ::testing::TestWithParam<Params>
653 {
654   protected:
655     ANGLETest();
656 
testSetUp()657     virtual void testSetUp() {}
testTearDown()658     virtual void testTearDown() {}
659 
recreateTestFixture()660     void recreateTestFixture()
661     {
662         TearDown();
663         SetUp();
664     }
665 
666   private:
SetUp()667     void SetUp() final
668     {
669         ANGLETestBase::ANGLETestSetUp();
670         if (mIsSetUp)
671         {
672             testSetUp();
673         }
674     }
675 
TearDown()676     void TearDown() final
677     {
678         ANGLETestBase::ANGLETestPreTearDown();
679         if (mIsSetUp)
680         {
681             testTearDown();
682         }
683         ANGLETestBase::ANGLETestTearDown();
684     }
685 };
686 
687 enum class APIExtensionVersion
688 {
689     Core,
690     OES,
691     EXT,
692     KHR,
693 };
694 
695 template <typename Params>
ANGLETest()696 ANGLETest<Params>::ANGLETest()
697     : ANGLETestBase(std::get<angle::PlatformParameters>(this->GetParam()))
698 {}
699 
700 template <>
ANGLETest()701 inline ANGLETest<angle::PlatformParameters>::ANGLETest() : ANGLETestBase(this->GetParam())
702 {}
703 
704 class ANGLETestEnvironment : public testing::Environment
705 {
706   public:
707     void SetUp() override;
708     void TearDown() override;
709 
710     static angle::Library *GetDriverLibrary(angle::GLESDriverType driver);
711 
712   private:
713     static angle::Library *GetAngleEGLLibrary();
714     static angle::Library *GetAngleVulkanSecondariesEGLLibrary();
715     static angle::Library *GetMesaEGLLibrary();
716     static angle::Library *GetSystemEGLLibrary();
717     static angle::Library *GetSystemWGLLibrary();
718 
719     // For loading entry points.
720     static std::unique_ptr<angle::Library> gAngleEGLLibrary;
721     static std::unique_ptr<angle::Library> gAngleVulkanSecondariesEGLLibrary;
722     static std::unique_ptr<angle::Library> gMesaEGLLibrary;
723     static std::unique_ptr<angle::Library> gSystemEGLLibrary;
724     static std::unique_ptr<angle::Library> gSystemWGLLibrary;
725 };
726 
727 extern angle::PlatformMethods gDefaultPlatformMethods;
728 
729 #endif  // ANGLE_TESTS_ANGLE_TEST_H_
730