blob: 780f57aeeec98853740fd9f86a3ea0a88c06f102 [file] [log] [blame]
Jamie Madill508a5b72015-12-08 11:26:14 -05001//
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
Geoff Lang8a079e52013-10-18 16:13:33 -040010#include "ANGLETest.h"
Jamie Madill62af5462014-08-26 13:16:37 -040011#include "EGLWindow.h"
Jamie Madill8add0eb2014-08-26 13:16:35 -040012#include "OSWindow.h"
Jamie Madill508a5b72015-12-08 11:26:14 -050013#include "system_utils.h"
Jamie Madill8add0eb2014-08-26 13:16:35 -040014
Jamie Madillfa05f602015-05-07 13:47:11 -040015ANGLETest::ANGLETest()
Corentin Wallezf3357ee2015-07-22 14:10:19 -040016 : mEGLWindow(nullptr),
Jamie Madill2511d062015-09-11 15:19:56 -040017 mWidth(16),
18 mHeight(16)
Geoff Lang8a079e52013-10-18 16:13:33 -040019{
Geoff Lang5ade8452015-09-02 11:00:30 -040020 mEGLWindow =
21 new EGLWindow(GetParam().majorVersion, GetParam().minorVersion, GetParam().eglParameters);
Geoff Lang0d3683c2014-10-23 11:08:16 -040022}
23
24ANGLETest::~ANGLETest()
25{
Jamie Madill77a72f62015-04-14 11:18:32 -040026 SafeDelete(mEGLWindow);
Geoff Lang8a079e52013-10-18 16:13:33 -040027}
28
Geoff Lang8a079e52013-10-18 16:13:33 -040029void ANGLETest::SetUp()
30{
Corentin Wallezb44440d2015-07-22 17:54:20 -040031 // Resize the window before creating the context so that the first make current
32 // sets the viewport and scissor box to the right size.
33 bool needSwap = false;
34 if (mOSWindow->getWidth() != mWidth || mOSWindow->getHeight() != mHeight)
Geoff Lang7f8dc492015-07-23 21:29:33 +000035 {
Corentin Wallezb44440d2015-07-22 17:54:20 -040036 if (!mOSWindow->resize(mWidth, mHeight))
37 {
38 FAIL() << "Failed to resize ANGLE test window.";
39 }
40 needSwap = true;
Geoff Lang7f8dc492015-07-23 21:29:33 +000041 }
42
Geoff Lang8a079e52013-10-18 16:13:33 -040043 if (!createEGLContext())
44 {
45 FAIL() << "egl context creation failed.";
46 }
Corentin Wallezb828b322015-07-16 17:51:30 -040047
Corentin Wallezb44440d2015-07-22 17:54:20 -040048 if (needSwap)
49 {
50 // Swap the buffers so that the default framebuffer picks up the resize
51 // which will allow follow-up test code to assume the framebuffer covers
52 // the whole window.
53 swapBuffers();
54 }
Corentin Wallez096725b2015-07-20 16:58:57 -040055
Geoff Lang7f8dc492015-07-23 21:29:33 +000056 // This Viewport command is not strictly necessary but we add it so that programs
57 // taking OpenGL traces can guess the size of the default framebuffer and show it
58 // in their UIs
59 glViewport(0, 0, mWidth, mHeight);
Jamie Madill508a5b72015-12-08 11:26:14 -050060
61 const auto &info = testing::UnitTest::GetInstance()->current_test_info();
62 angle::WriteDebugMessage("Entering %s.%s\n", info->test_case_name(), info->name());
Geoff Lang8a079e52013-10-18 16:13:33 -040063}
64
65void ANGLETest::TearDown()
66{
Jamie Madill508a5b72015-12-08 11:26:14 -050067 const auto &info = testing::UnitTest::GetInstance()->current_test_info();
68 angle::WriteDebugMessage("Exiting %s.%s\n", info->test_case_name(), info->name());
69
Geoff Lang8a079e52013-10-18 16:13:33 -040070 swapBuffers();
Jamie Madill9e16d402014-09-08 17:36:33 -040071 mOSWindow->messageLoop();
72
Geoff Lang8a079e52013-10-18 16:13:33 -040073 if (!destroyEGLContext())
74 {
75 FAIL() << "egl context destruction failed.";
76 }
Jamie Madill8add0eb2014-08-26 13:16:35 -040077
78 // Check for quit message
79 Event myEvent;
80 while (mOSWindow->popEvent(&myEvent))
81 {
82 if (myEvent.Type == Event::EVENT_CLOSED)
83 {
84 exit(0);
85 }
86 }
Geoff Lang8a079e52013-10-18 16:13:33 -040087}
88
89void ANGLETest::swapBuffers()
90{
Jamie Madill77a72f62015-04-14 11:18:32 -040091 if (mEGLWindow->isGLInitialized())
92 {
93 mEGLWindow->swap();
94 }
Geoff Lang8a079e52013-10-18 16:13:33 -040095}
96
Olli Etuaho4a8329f2016-01-11 17:12:57 +020097void ANGLETest::drawQuad(GLuint program,
98 const std::string &positionAttribName,
99 GLfloat positionAttribZ)
100{
101 drawQuad(program, positionAttribName, positionAttribZ, 1.0f);
102}
103
104void ANGLETest::drawQuad(GLuint program,
105 const std::string &positionAttribName,
106 GLfloat positionAttribZ,
107 GLfloat positionAttribXYScale)
Geoff Lang8a079e52013-10-18 16:13:33 -0400108{
109 GLint positionLocation = glGetAttribLocation(program, positionAttribName.c_str());
110
111 glUseProgram(program);
112
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200113 const GLfloat vertices[] = {
114 -1.0f * positionAttribXYScale, 1.0f * positionAttribXYScale, positionAttribZ,
115 -1.0f * positionAttribXYScale, -1.0f * positionAttribXYScale, positionAttribZ,
116 1.0f * positionAttribXYScale, -1.0f * positionAttribXYScale, positionAttribZ,
Geoff Lang8a079e52013-10-18 16:13:33 -0400117
Olli Etuaho4a8329f2016-01-11 17:12:57 +0200118 -1.0f * positionAttribXYScale, 1.0f * positionAttribXYScale, positionAttribZ,
119 1.0f * positionAttribXYScale, -1.0f * positionAttribXYScale, positionAttribZ,
120 1.0f * positionAttribXYScale, 1.0f * positionAttribXYScale, positionAttribZ,
Geoff Lang8a079e52013-10-18 16:13:33 -0400121 };
122
123 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, vertices);
124 glEnableVertexAttribArray(positionLocation);
125
126 glDrawArrays(GL_TRIANGLES, 0, 6);
127
128 glDisableVertexAttribArray(positionLocation);
129 glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL);
130
131 glUseProgram(0);
132}
133
Geoff Langefc551f2013-10-31 10:20:28 -0400134GLuint ANGLETest::compileShader(GLenum type, const std::string &source)
135{
136 GLuint shader = glCreateShader(type);
137
138 const char *sourceArray[1] = { source.c_str() };
139 glShaderSource(shader, 1, sourceArray, NULL);
140 glCompileShader(shader);
141
142 GLint compileResult;
143 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
144
145 if (compileResult == 0)
146 {
147 GLint infoLogLength;
148 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
149
Jamie Madilld2c52e32015-10-14 17:07:05 -0400150 if (infoLogLength == 0)
151 {
152 std::cerr << "shader compilation failed with empty log." << std::endl;
153 }
154 else
155 {
156 std::vector<GLchar> infoLog(infoLogLength);
157 glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]);
Geoff Langefc551f2013-10-31 10:20:28 -0400158
Jamie Madilld2c52e32015-10-14 17:07:05 -0400159 std::cerr << "shader compilation failed: " << &infoLog[0];
160 }
Geoff Langefc551f2013-10-31 10:20:28 -0400161
162 glDeleteShader(shader);
163 shader = 0;
164 }
165
166 return shader;
167}
168
Geoff Lang63046e22015-07-21 12:43:50 -0400169static bool checkExtensionExists(const char *allExtensions, const std::string &extName)
170{
171 return strstr(allExtensions, extName.c_str()) != nullptr;
172}
173
Geoff Lang8a079e52013-10-18 16:13:33 -0400174bool ANGLETest::extensionEnabled(const std::string &extName)
175{
Geoff Lang63046e22015-07-21 12:43:50 -0400176 return checkExtensionExists(reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)),
177 extName);
178}
179
180bool ANGLETest::eglDisplayExtensionEnabled(EGLDisplay display, const std::string &extName)
181{
182 return checkExtensionExists(eglQueryString(display, EGL_EXTENSIONS), extName);
183}
184
185bool ANGLETest::eglClientExtensionEnabled(const std::string &extName)
186{
187 return checkExtensionExists(eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS), extName);
Geoff Lang8a079e52013-10-18 16:13:33 -0400188}
189
Geoff Lang8a079e52013-10-18 16:13:33 -0400190void ANGLETest::setWindowWidth(int width)
191{
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400192 mWidth = width;
Geoff Lang8a079e52013-10-18 16:13:33 -0400193}
194
195void ANGLETest::setWindowHeight(int height)
196{
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400197 mHeight = height;
Geoff Lang8a079e52013-10-18 16:13:33 -0400198}
199
Geoff Langefc551f2013-10-31 10:20:28 -0400200void ANGLETest::setConfigRedBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400201{
Jamie Madill62af5462014-08-26 13:16:37 -0400202 mEGLWindow->setConfigRedBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400203}
204
Geoff Langefc551f2013-10-31 10:20:28 -0400205void ANGLETest::setConfigGreenBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400206{
Jamie Madill62af5462014-08-26 13:16:37 -0400207 mEGLWindow->setConfigGreenBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400208}
209
Geoff Langefc551f2013-10-31 10:20:28 -0400210void ANGLETest::setConfigBlueBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400211{
Jamie Madill62af5462014-08-26 13:16:37 -0400212 mEGLWindow->setConfigBlueBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400213}
214
Geoff Langefc551f2013-10-31 10:20:28 -0400215void ANGLETest::setConfigAlphaBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400216{
Jamie Madill62af5462014-08-26 13:16:37 -0400217 mEGLWindow->setConfigAlphaBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400218}
219
Geoff Langefc551f2013-10-31 10:20:28 -0400220void ANGLETest::setConfigDepthBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400221{
Jamie Madill62af5462014-08-26 13:16:37 -0400222 mEGLWindow->setConfigDepthBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400223}
224
Geoff Langefc551f2013-10-31 10:20:28 -0400225void ANGLETest::setConfigStencilBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400226{
Jamie Madill62af5462014-08-26 13:16:37 -0400227 mEGLWindow->setConfigStencilBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400228}
229
230void ANGLETest::setMultisampleEnabled(bool enabled)
231{
Jamie Madill62af5462014-08-26 13:16:37 -0400232 mEGLWindow->setMultisample(enabled);
Geoff Lang8a079e52013-10-18 16:13:33 -0400233}
234
Geoff Lang70d0f492015-12-10 17:45:46 -0500235void ANGLETest::setDebugEnabled(bool enabled)
236{
237 mEGLWindow->setDebugEnabled(enabled);
238}
239
Geoff Lang8a079e52013-10-18 16:13:33 -0400240int ANGLETest::getClientVersion() const
241{
Geoff Lang5ade8452015-09-02 11:00:30 -0400242 return mEGLWindow->getClientMajorVersion();
Geoff Lang8a079e52013-10-18 16:13:33 -0400243}
244
Geoff Langb9266272015-01-29 13:25:14 +0000245EGLWindow *ANGLETest::getEGLWindow() const
246{
247 return mEGLWindow;
248}
249
Geoff Lang8a079e52013-10-18 16:13:33 -0400250int ANGLETest::getWindowWidth() const
251{
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400252 return mWidth;
Geoff Lang8a079e52013-10-18 16:13:33 -0400253}
254
255int ANGLETest::getWindowHeight() const
256{
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400257 return mHeight;
Geoff Lang8a079e52013-10-18 16:13:33 -0400258}
259
Geoff Langefc551f2013-10-31 10:20:28 -0400260bool ANGLETest::isMultisampleEnabled() const
Geoff Lang8a079e52013-10-18 16:13:33 -0400261{
Jamie Madill62af5462014-08-26 13:16:37 -0400262 return mEGLWindow->isMultisample();
Geoff Lang8a079e52013-10-18 16:13:33 -0400263}
264
265bool ANGLETest::createEGLContext()
266{
Jamie Madill62af5462014-08-26 13:16:37 -0400267 return mEGLWindow->initializeGL(mOSWindow);
Geoff Lang8a079e52013-10-18 16:13:33 -0400268}
269
270bool ANGLETest::destroyEGLContext()
271{
Jamie Madill62af5462014-08-26 13:16:37 -0400272 mEGLWindow->destroyGL();
Geoff Lang8a079e52013-10-18 16:13:33 -0400273 return true;
274}
Geoff Langbb134672013-10-23 13:06:46 -0400275
Geoff Lang0d3683c2014-10-23 11:08:16 -0400276bool ANGLETest::InitTestWindow()
Jamie Madill8add0eb2014-08-26 13:16:35 -0400277{
278 mOSWindow = CreateOSWindow();
279 if (!mOSWindow->initialize("ANGLE_TEST", 128, 128))
280 {
281 return false;
282 }
283
Geoff Lang0d3683c2014-10-23 11:08:16 -0400284 mOSWindow->setVisible(true);
Jamie Madill8add0eb2014-08-26 13:16:35 -0400285
286 return true;
287}
288
Geoff Lang0d3683c2014-10-23 11:08:16 -0400289bool ANGLETest::DestroyTestWindow()
Jamie Madill8add0eb2014-08-26 13:16:35 -0400290{
291 if (mOSWindow)
292 {
293 mOSWindow->destroy();
294 delete mOSWindow;
295 mOSWindow = NULL;
296 }
297
298 return true;
299}
300
Geoff Lang0d3683c2014-10-23 11:08:16 -0400301void ANGLETest::SetWindowVisible(bool isVisible)
Geoff Langbb134672013-10-23 13:06:46 -0400302{
Jamie Madill4119ed32014-10-01 10:41:40 -0400303 mOSWindow->setVisible(isVisible);
Geoff Langbb134672013-10-23 13:06:46 -0400304}
Geoff Lang0d3683c2014-10-23 11:08:16 -0400305
Jamie Madillc3b9b262015-01-30 14:00:51 -0500306bool ANGLETest::isIntel() const
307{
308 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
309 return (rendererString.find("Intel") != std::string::npos);
310}
311
312bool ANGLETest::isAMD() const
313{
314 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
315 return (rendererString.find("AMD") != std::string::npos) ||
316 (rendererString.find("ATI") != std::string::npos);
317}
318
319bool ANGLETest::isNVidia() const
320{
321 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
322 return (rendererString.find("NVIDIA") != std::string::npos);
323}
324
Jamie Madilld55d2832015-10-27 13:59:19 -0400325bool ANGLETest::isD3D11() const
326{
327 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
328 return (rendererString.find("Direct3D11 vs_5_0") != std::string::npos);
329}
330
Jamie Madill9fc36822015-11-18 13:08:07 -0500331bool ANGLETest::isD3D11_FL93() const
332{
333 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
334 return (rendererString.find("Direct3D11 vs_4_0_") != std::string::npos);
335}
336
337bool ANGLETest::isD3D9() const
338{
339 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
340 return (rendererString.find("Direct3D9") != std::string::npos);
341}
342
343bool ANGLETest::isD3DSM3() const
344{
345 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
346 return isD3D9() || isD3D11_FL93();
347}
348
Jamie Madillc3b9b262015-01-30 14:00:51 -0500349EGLint ANGLETest::getPlatformRenderer() const
350{
Jamie Madillf6859912015-01-30 17:05:35 -0500351 assert(mEGLWindow);
352 return mEGLWindow->getPlatform().renderer;
Jamie Madillc3b9b262015-01-30 14:00:51 -0500353}
354
Geoff Lang0d3683c2014-10-23 11:08:16 -0400355OSWindow *ANGLETest::mOSWindow = NULL;
356
357void ANGLETestEnvironment::SetUp()
358{
359 if (!ANGLETest::InitTestWindow())
360 {
361 FAIL() << "Failed to create ANGLE test window.";
362 }
363}
364
365void ANGLETestEnvironment::TearDown()
366{
367 ANGLETest::DestroyTestWindow();
368}