• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GLColor16UI = 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_ALPHA32F_EQ(x, y, a) EXPECT_EQ(a, angle::ReadColor32F(x, y).A)
248 
249 #define EXPECT_PIXEL_COLOR_EQ(x, y, angleColor) EXPECT_EQ(angleColor, angle::ReadColor(x, y))
250 #define EXPECT_PIXEL_COLOR_EQ_VEC2(vec2, angleColor) \
251     EXPECT_EQ(angleColor,                            \
252               angle::ReadColor(static_cast<GLint>(vec2.x()), static_cast<GLint>(vec2.y())))
253 
254 #define EXPECT_PIXEL_COLOR32F_EQ(x, y, angleColor) EXPECT_EQ(angleColor, angle::ReadColor32F(x, y))
255 
256 #define EXPECT_PIXEL_RECT_T_EQ(T, x, y, width, height, format, type, color)           \
257     do                                                                                \
258     {                                                                                 \
259         std::vector<T> actualColors((width) * (height));                              \
260         glReadPixels((x), (y), (width), (height), format, type, actualColors.data()); \
261         std::vector<T> expectedColors((width) * (height), color);                     \
262         EXPECT_EQ(expectedColors, actualColors);                                      \
263     } while (0)
264 
265 #define EXPECT_PIXEL_RECT_EQ(x, y, width, height, color) \
266     EXPECT_PIXEL_RECT_T_EQ(GLColor, x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, color)
267 
268 #define EXPECT_PIXEL_RECT32F_EQ(x, y, width, height, color) \
269     EXPECT_PIXEL_RECT_T_EQ(GLColor32F, x, y, width, height, GL_RGBA, GL_FLOAT, color)
270 
271 #define EXPECT_PIXEL_RECT32I_EQ(x, y, width, height, color) \
272     EXPECT_PIXEL_RECT_T_EQ(GLColor32I, x, y, width, height, GL_RGBA_INTEGER, GL_INT, color)
273 
274 #define EXPECT_PIXEL_RECT32UI_EQ(x, y, width, height, color)                                   \
275     EXPECT_PIXEL_RECT_T_EQ(GLColor32UI, x, y, width, height, GL_RGBA_INTEGER, GL_UNSIGNED_INT, \
276                            color)
277 
278 #define EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, ctype, format, type) \
279     do                                                                             \
280     {                                                                              \
281         ctype pixel[4];                                                            \
282         glReadPixels((x), (y), 1, 1, format, type, pixel);                         \
283         EXPECT_GL_NO_ERROR();                                                      \
284         EXPECT_NEAR((r), pixel[0], abs_error);                                     \
285         EXPECT_NEAR((g), pixel[1], abs_error);                                     \
286         EXPECT_NEAR((b), pixel[2], abs_error);                                     \
287         EXPECT_NEAR((a), pixel[3], abs_error);                                     \
288     } while (0)
289 
290 #define EXPECT_PIXEL_EQ_HELPER(x, y, r, g, b, a, ctype, format, type) \
291     do                                                                \
292     {                                                                 \
293         ctype pixel[4];                                               \
294         glReadPixels((x), (y), 1, 1, format, type, pixel);            \
295         EXPECT_GL_NO_ERROR();                                         \
296         EXPECT_EQ((r), pixel[0]);                                     \
297         EXPECT_EQ((g), pixel[1]);                                     \
298         EXPECT_EQ((b), pixel[2]);                                     \
299         EXPECT_EQ((a), pixel[3]);                                     \
300     } while (0)
301 
302 #define EXPECT_PIXEL_NEAR(x, y, r, g, b, a, abs_error) \
303     EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, GLubyte, GL_RGBA, GL_UNSIGNED_BYTE)
304 
305 #define EXPECT_PIXEL_8S_NEAR(x, y, r, g, b, a, abs_error) \
306     EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, GLbyte, GL_RGBA, GL_BYTE)
307 
308 #define EXPECT_PIXEL_16S_NEAR(x, y, r, g, b, a, abs_error) \
309     EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, GLshort, GL_RGBA, GL_SHORT)
310 
311 #define EXPECT_PIXEL_32F_NEAR(x, y, r, g, b, a, abs_error) \
312     EXPECT_PIXEL_NEAR_HELPER(x, y, r, g, b, a, abs_error, GLfloat, GL_RGBA, GL_FLOAT)
313 
314 #define EXPECT_PIXEL_8I(x, y, r, g, b, a) \
315     EXPECT_PIXEL_EQ_HELPER(x, y, r, g, b, a, GLbyte, GL_RGBA_INTEGER, GL_BYTE)
316 
317 #define EXPECT_PIXEL_8UI(x, y, r, g, b, a) \
318     EXPECT_PIXEL_EQ_HELPER(x, y, r, g, b, a, GLubyte, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE)
319 
320 #define EXPECT_PIXEL_16UI(x, y, r, g, b, a) \
321     EXPECT_PIXEL_EQ_HELPER(x, y, r, g, b, a, GLushort, GL_RGBA, GL_UNSIGNED_SHORT)
322 
323 #define EXPECT_PIXEL_16UI_COLOR(x, y, color) \
324     EXPECT_PIXEL_16UI(x, y, color.R, color.G, color.B, color.A)
325 
326 // TODO(jmadill): Figure out how we can use GLColor's nice printing with EXPECT_NEAR.
327 #define EXPECT_PIXEL_COLOR_NEAR(x, y, angleColor, abs_error) \
328     EXPECT_PIXEL_NEAR(x, y, angleColor.R, angleColor.G, angleColor.B, angleColor.A, abs_error)
329 
330 #define EXPECT_PIXEL_COLOR32F_NEAR(x, y, angleColor, abs_error) \
331     EXPECT_PIXEL32F_NEAR(x, y, angleColor.R, angleColor.G, angleColor.B, angleColor.A, abs_error)
332 
333 #define EXPECT_COLOR_NEAR(expected, actual, abs_error) \
334     do                                                 \
335     {                                                  \
336         EXPECT_NEAR(expected.R, actual.R, abs_error);  \
337         EXPECT_NEAR(expected.G, actual.G, abs_error);  \
338         EXPECT_NEAR(expected.B, actual.B, abs_error);  \
339         EXPECT_NEAR(expected.A, actual.A, abs_error);  \
340     } while (0)
341 #define EXPECT_PIXEL32F_NEAR(x, y, r, g, b, a, abs_error)       \
342     do                                                          \
343     {                                                           \
344         GLfloat pixel[4];                                       \
345         glReadPixels((x), (y), 1, 1, GL_RGBA, GL_FLOAT, pixel); \
346         EXPECT_GL_NO_ERROR();                                   \
347         EXPECT_NEAR((r), pixel[0], abs_error);                  \
348         EXPECT_NEAR((g), pixel[1], abs_error);                  \
349         EXPECT_NEAR((b), pixel[2], abs_error);                  \
350         EXPECT_NEAR((a), pixel[3], abs_error);                  \
351     } while (0)
352 
353 #define EXPECT_PIXEL_COLOR32F_NEAR(x, y, angleColor, abs_error) \
354     EXPECT_PIXEL32F_NEAR(x, y, angleColor.R, angleColor.G, angleColor.B, angleColor.A, abs_error)
355 
356 #define EXPECT_PIXEL_STENCIL_EQ(x, y, expected)                                    \
357     do                                                                             \
358     {                                                                              \
359         GLubyte actual;                                                            \
360         glReadPixels((x), (y), 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &actual); \
361         EXPECT_GL_NO_ERROR();                                                      \
362         EXPECT_EQ((expected), actual);                                             \
363     } while (0)
364 
365 class ANGLETestBase;
366 class EGLWindow;
367 class GLWindowBase;
368 class OSWindow;
369 class WGLWindow;
370 
371 struct TestPlatformContext final : private angle::NonCopyable
372 {
373     bool ignoreMessages        = false;
374     bool warningsAsErrors      = false;
375     ANGLETestBase *currentTest = nullptr;
376 };
377 
378 class ANGLETestBase
379 {
380   protected:
381     ANGLETestBase(const angle::PlatformParameters &params);
382     virtual ~ANGLETestBase();
383 
384   public:
385     void setWindowVisible(OSWindow *osWindow, bool isVisible);
386 
387     static void ReleaseFixtures();
388 
isSwiftshader()389     bool isSwiftshader() const
390     {
391         // Renderer might be swiftshader even if local swiftshader not used.
392         return mCurrentParams->isSwiftshader() || angle::IsSwiftshaderDevice();
393     }
394 
enableDebugLayers()395     bool enableDebugLayers() const
396     {
397         return mCurrentParams->eglParameters.debugLayersEnabled != EGL_FALSE;
398     }
399 
400   protected:
401     void ANGLETestSetUp();
402     void ANGLETestPreTearDown();
403     void ANGLETestTearDown();
404 
405     virtual void swapBuffers();
406 
407     void setupQuadVertexBuffer(GLfloat positionAttribZ, GLfloat positionAttribXYScale);
408     void setupIndexedQuadVertexBuffer(GLfloat positionAttribZ, GLfloat positionAttribXYScale);
409     void setupIndexedQuadIndexBuffer();
410 
411     void drawQuad(GLuint program, const std::string &positionAttribName, GLfloat positionAttribZ);
412     void drawQuad(GLuint program,
413                   const std::string &positionAttribName,
414                   GLfloat positionAttribZ,
415                   GLfloat positionAttribXYScale);
416     void drawQuad(GLuint program,
417                   const std::string &positionAttribName,
418                   GLfloat positionAttribZ,
419                   GLfloat positionAttribXYScale,
420                   bool useVertexBuffer);
421     void drawQuadInstanced(GLuint program,
422                            const std::string &positionAttribName,
423                            GLfloat positionAttribZ,
424                            GLfloat positionAttribXYScale,
425                            bool useVertexBuffer,
426                            GLuint numInstances);
427     void drawPatches(GLuint program,
428                      const std::string &positionAttribName,
429                      GLfloat positionAttribZ,
430                      GLfloat positionAttribXYScale,
431                      bool useVertexBuffer);
432 
433     void drawQuadPPO(GLuint vertProgram,
434                      const std::string &positionAttribName,
435                      const GLfloat positionAttribZ,
436                      const GLfloat positionAttribXYScale);
437 
438     static std::array<angle::Vector3, 6> GetQuadVertices();
439     static std::array<GLushort, 6> GetQuadIndices();
440     static std::array<angle::Vector3, 4> GetIndexedQuadVertices();
441 
442     void drawIndexedQuad(GLuint program,
443                          const std::string &positionAttribName,
444                          GLfloat positionAttribZ);
445     void drawIndexedQuad(GLuint program,
446                          const std::string &positionAttribName,
447                          GLfloat positionAttribZ,
448                          GLfloat positionAttribXYScale);
449     void drawIndexedQuad(GLuint program,
450                          const std::string &positionAttribName,
451                          GLfloat positionAttribZ,
452                          GLfloat positionAttribXYScale,
453                          bool useBufferObject);
454 
455     void drawIndexedQuad(GLuint program,
456                          const std::string &positionAttribName,
457                          GLfloat positionAttribZ,
458                          GLfloat positionAttribXYScale,
459                          bool useBufferObject,
460                          bool restrictedRange);
461 
462     void draw2DTexturedQuad(GLfloat positionAttribZ,
463                             GLfloat positionAttribXYScale,
464                             bool useVertexBuffer);
465 
466     // The layer parameter chooses the 3D texture layer to sample from.
467     void draw3DTexturedQuad(GLfloat positionAttribZ,
468                             GLfloat positionAttribXYScale,
469                             bool useVertexBuffer,
470                             float layer);
471 
472     void setWindowWidth(int width);
473     void setWindowHeight(int height);
474     void setConfigRedBits(int bits);
475     void setConfigGreenBits(int bits);
476     void setConfigBlueBits(int bits);
477     void setConfigAlphaBits(int bits);
478     void setConfigDepthBits(int bits);
479     void setConfigStencilBits(int bits);
480     void setConfigComponentType(EGLenum componentType);
481     void setMultisampleEnabled(bool enabled);
482     void setSamples(EGLint samples);
483     void setDebugEnabled(bool enabled);
484     void setNoErrorEnabled(bool enabled);
485     void setWebGLCompatibilityEnabled(bool webglCompatibility);
486     void setExtensionsEnabled(bool extensionsEnabled);
487     void setRobustAccess(bool enabled);
488     void setBindGeneratesResource(bool bindGeneratesResource);
489     void setClientArraysEnabled(bool enabled);
490     void setRobustResourceInit(bool enabled);
491     void setMutableRenderBuffer(bool enabled);
492     void setContextProgramCacheEnabled(bool enabled);
493     void setContextResetStrategy(EGLenum resetStrategy);
494     void forceNewDisplay();
495 
496     // Some EGL extension tests would like to defer the Context init until the test body.
497     void setDeferContextInit(bool enabled);
498 
499     int getClientMajorVersion() const;
500     int getClientMinorVersion() const;
501 
502     GLWindowBase *getGLWindow() const;
503     EGLWindow *getEGLWindow() const;
504     int getWindowWidth() const;
505     int getWindowHeight() const;
506 
507     EGLint getPlatformRenderer() const;
508 
509     void ignoreD3D11SDKLayersWarnings();
510 
getOSWindow()511     OSWindow *getOSWindow() { return mFixture->osWindow; }
512 
513     GLuint get2DTexturedQuadProgram();
514 
515     // Has a float uniform "u_layer" to choose the 3D texture layer.
516     GLuint get3DTexturedQuadProgram();
517 
518     class [[nodiscard]] ScopedIgnorePlatformMessages : angle::NonCopyable
519     {
520       public:
521         ScopedIgnorePlatformMessages();
522         ~ScopedIgnorePlatformMessages();
523     };
524 
525     // Can be used before we get a GL context.
isGLRenderer()526     bool isGLRenderer() const
527     {
528         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE;
529     }
530 
isGLESRenderer()531     bool isGLESRenderer() const
532     {
533         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
534     }
535 
isD3D11Renderer()536     bool isD3D11Renderer() const
537     {
538         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE;
539     }
540 
isVulkanRenderer()541     bool isVulkanRenderer() const
542     {
543         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE;
544     }
545 
isVulkanSwiftshaderRenderer()546     bool isVulkanSwiftshaderRenderer() const
547     {
548         return mCurrentParams->getRenderer() == EGL_PLATFORM_ANGLE_TYPE_VULKAN_ANGLE &&
549                mCurrentParams->isSwiftshader();
550     }
551 
552     bool platformSupportsMultithreading() const;
553 
554     bool mIsSetUp = false;
555 
556   private:
557     void checkD3D11SDKLayersMessages();
558 
559     void drawQuad(GLuint program,
560                   const std::string &positionAttribName,
561                   GLfloat positionAttribZ,
562                   GLfloat positionAttribXYScale,
563                   bool useVertexBuffer,
564                   bool useInstancedDrawCalls,
565                   bool useTessellationPatches,
566                   GLuint numInstances);
567 
568     void initOSWindow();
569 
570     struct TestFixture
571     {
572         TestFixture();
573         ~TestFixture();
574 
575         EGLWindow *eglWindow = nullptr;
576         WGLWindow *wglWindow = nullptr;
577         OSWindow *osWindow   = nullptr;
578         ConfigParameters configParams;
579         uint32_t reuseCounter = 0;
580     };
581 
582     int mWidth;
583     int mHeight;
584 
585     bool mIgnoreD3D11SDKLayersWarnings;
586 
587     // Used for indexed quad rendering
588     GLuint mQuadVertexBuffer;
589     GLuint mQuadIndexBuffer;
590 
591     // Used for texture rendering.
592     GLuint m2DTexturedQuadProgram;
593     GLuint m3DTexturedQuadProgram;
594 
595     bool mDeferContextInit;
596     bool mAlwaysForceNewDisplay;
597     bool mForceNewDisplay;
598 
599     bool mSetUpCalled;
600     bool mTearDownCalled;
601 
602     // Usually, we use an OS Window per "fixture" (a frontend and backend combination).
603     // This allows:
604     // 1. Reusing EGL Display on Windows.
605     //    Other platforms have issues with display reuse even if a window per fixture is used.
606     // 2. Hiding only SwiftShader OS Window on Linux.
607     //    OS Windows for other backends must be visible, to allow driver to communicate with X11.
608     // However, we must use a single OS Window for all backends on Android,
609     // since test Application can have only one window.
610     static OSWindow *mOSWindowSingleton;
611 
612     static std::map<angle::PlatformParameters, TestFixture> gFixtures;
613     const angle::PlatformParameters *mCurrentParams;
614     TestFixture *mFixture;
615 
616     RenderDoc mRenderDoc;
617 
618     // Workaround for NVIDIA not being able to share a window with OpenGL and Vulkan.
619     static Optional<EGLint> mLastRendererType;
620     static Optional<angle::GLESDriverType> mLastLoadedDriver;
621 };
622 
623 template <typename Params = angle::PlatformParameters>
624 class ANGLETest : public ANGLETestBase, public ::testing::TestWithParam<Params>
625 {
626   protected:
627     ANGLETest();
628 
testSetUp()629     virtual void testSetUp() {}
testTearDown()630     virtual void testTearDown() {}
631 
recreateTestFixture()632     void recreateTestFixture()
633     {
634         TearDown();
635         SetUp();
636     }
637 
638   private:
SetUp()639     void SetUp() final
640     {
641         ANGLETestBase::ANGLETestSetUp();
642         if (mIsSetUp)
643         {
644             testSetUp();
645         }
646     }
647 
TearDown()648     void TearDown() final
649     {
650         ANGLETestBase::ANGLETestPreTearDown();
651         if (mIsSetUp)
652         {
653             testTearDown();
654         }
655         ANGLETestBase::ANGLETestTearDown();
656     }
657 };
658 
659 template <typename Params>
ANGLETest()660 ANGLETest<Params>::ANGLETest()
661     : ANGLETestBase(std::get<angle::PlatformParameters>(this->GetParam()))
662 {}
663 
664 template <>
ANGLETest()665 inline ANGLETest<angle::PlatformParameters>::ANGLETest() : ANGLETestBase(this->GetParam())
666 {}
667 
668 class ANGLETestEnvironment : public testing::Environment
669 {
670   public:
671     void SetUp() override;
672     void TearDown() override;
673 
674     static angle::Library *GetDriverLibrary(angle::GLESDriverType driver);
675 
676   private:
677     static angle::Library *GetAngleEGLLibrary();
678     static angle::Library *GetAngleVulkanSecondariesEGLLibrary();
679     static angle::Library *GetMesaEGLLibrary();
680     static angle::Library *GetSystemEGLLibrary();
681     static angle::Library *GetSystemWGLLibrary();
682 
683     // For loading entry points.
684     static std::unique_ptr<angle::Library> gAngleEGLLibrary;
685     static std::unique_ptr<angle::Library> gAngleVulkanSecondariesEGLLibrary;
686     static std::unique_ptr<angle::Library> gMesaEGLLibrary;
687     static std::unique_ptr<angle::Library> gSystemEGLLibrary;
688     static std::unique_ptr<angle::Library> gSystemWGLLibrary;
689 };
690 
691 extern angle::PlatformMethods gDefaultPlatformMethods;
692 
693 #endif  // ANGLE_TESTS_ANGLE_TEST_H_
694