blob: ffc1ec17b003e638b3096ec61844ef08df3c78be [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
Austin Kinross4fd18b12014-12-22 12:32:05 -080097void ANGLETest::drawQuad(GLuint program, const std::string& positionAttribName, GLfloat quadDepth, GLfloat quadScale)
Geoff Lang8a079e52013-10-18 16:13:33 -040098{
99 GLint positionLocation = glGetAttribLocation(program, positionAttribName.c_str());
100
101 glUseProgram(program);
102
103 const GLfloat vertices[] =
104 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800105 -1.0f * quadScale, 1.0f * quadScale, quadDepth,
106 -1.0f * quadScale, -1.0f * quadScale, quadDepth,
107 1.0f * quadScale, -1.0f * quadScale, quadDepth,
Geoff Lang8a079e52013-10-18 16:13:33 -0400108
Austin Kinross4fd18b12014-12-22 12:32:05 -0800109 -1.0f * quadScale, 1.0f * quadScale, quadDepth,
110 1.0f * quadScale, -1.0f * quadScale, quadDepth,
111 1.0f * quadScale, 1.0f * quadScale, quadDepth,
Geoff Lang8a079e52013-10-18 16:13:33 -0400112 };
113
114 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, vertices);
115 glEnableVertexAttribArray(positionLocation);
116
117 glDrawArrays(GL_TRIANGLES, 0, 6);
118
119 glDisableVertexAttribArray(positionLocation);
120 glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL);
121
122 glUseProgram(0);
123}
124
Geoff Langefc551f2013-10-31 10:20:28 -0400125GLuint ANGLETest::compileShader(GLenum type, const std::string &source)
126{
127 GLuint shader = glCreateShader(type);
128
129 const char *sourceArray[1] = { source.c_str() };
130 glShaderSource(shader, 1, sourceArray, NULL);
131 glCompileShader(shader);
132
133 GLint compileResult;
134 glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
135
136 if (compileResult == 0)
137 {
138 GLint infoLogLength;
139 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
140
Jamie Madilld2c52e32015-10-14 17:07:05 -0400141 if (infoLogLength == 0)
142 {
143 std::cerr << "shader compilation failed with empty log." << std::endl;
144 }
145 else
146 {
147 std::vector<GLchar> infoLog(infoLogLength);
148 glGetShaderInfoLog(shader, static_cast<GLsizei>(infoLog.size()), NULL, &infoLog[0]);
Geoff Langefc551f2013-10-31 10:20:28 -0400149
Jamie Madilld2c52e32015-10-14 17:07:05 -0400150 std::cerr << "shader compilation failed: " << &infoLog[0];
151 }
Geoff Langefc551f2013-10-31 10:20:28 -0400152
153 glDeleteShader(shader);
154 shader = 0;
155 }
156
157 return shader;
158}
159
Geoff Lang63046e22015-07-21 12:43:50 -0400160static bool checkExtensionExists(const char *allExtensions, const std::string &extName)
161{
162 return strstr(allExtensions, extName.c_str()) != nullptr;
163}
164
Geoff Lang8a079e52013-10-18 16:13:33 -0400165bool ANGLETest::extensionEnabled(const std::string &extName)
166{
Geoff Lang63046e22015-07-21 12:43:50 -0400167 return checkExtensionExists(reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)),
168 extName);
169}
170
171bool ANGLETest::eglDisplayExtensionEnabled(EGLDisplay display, const std::string &extName)
172{
173 return checkExtensionExists(eglQueryString(display, EGL_EXTENSIONS), extName);
174}
175
176bool ANGLETest::eglClientExtensionEnabled(const std::string &extName)
177{
178 return checkExtensionExists(eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS), extName);
Geoff Lang8a079e52013-10-18 16:13:33 -0400179}
180
Geoff Lang8a079e52013-10-18 16:13:33 -0400181void ANGLETest::setWindowWidth(int width)
182{
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400183 mWidth = width;
Geoff Lang8a079e52013-10-18 16:13:33 -0400184}
185
186void ANGLETest::setWindowHeight(int height)
187{
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400188 mHeight = height;
Geoff Lang8a079e52013-10-18 16:13:33 -0400189}
190
Geoff Langefc551f2013-10-31 10:20:28 -0400191void ANGLETest::setConfigRedBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400192{
Jamie Madill62af5462014-08-26 13:16:37 -0400193 mEGLWindow->setConfigRedBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400194}
195
Geoff Langefc551f2013-10-31 10:20:28 -0400196void ANGLETest::setConfigGreenBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400197{
Jamie Madill62af5462014-08-26 13:16:37 -0400198 mEGLWindow->setConfigGreenBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400199}
200
Geoff Langefc551f2013-10-31 10:20:28 -0400201void ANGLETest::setConfigBlueBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400202{
Jamie Madill62af5462014-08-26 13:16:37 -0400203 mEGLWindow->setConfigBlueBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400204}
205
Geoff Langefc551f2013-10-31 10:20:28 -0400206void ANGLETest::setConfigAlphaBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400207{
Jamie Madill62af5462014-08-26 13:16:37 -0400208 mEGLWindow->setConfigAlphaBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400209}
210
Geoff Langefc551f2013-10-31 10:20:28 -0400211void ANGLETest::setConfigDepthBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400212{
Jamie Madill62af5462014-08-26 13:16:37 -0400213 mEGLWindow->setConfigDepthBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400214}
215
Geoff Langefc551f2013-10-31 10:20:28 -0400216void ANGLETest::setConfigStencilBits(int bits)
Geoff Lang8a079e52013-10-18 16:13:33 -0400217{
Jamie Madill62af5462014-08-26 13:16:37 -0400218 mEGLWindow->setConfigStencilBits(bits);
Geoff Lang8a079e52013-10-18 16:13:33 -0400219}
220
221void ANGLETest::setMultisampleEnabled(bool enabled)
222{
Jamie Madill62af5462014-08-26 13:16:37 -0400223 mEGLWindow->setMultisample(enabled);
Geoff Lang8a079e52013-10-18 16:13:33 -0400224}
225
226int ANGLETest::getClientVersion() const
227{
Geoff Lang5ade8452015-09-02 11:00:30 -0400228 return mEGLWindow->getClientMajorVersion();
Geoff Lang8a079e52013-10-18 16:13:33 -0400229}
230
Geoff Langb9266272015-01-29 13:25:14 +0000231EGLWindow *ANGLETest::getEGLWindow() const
232{
233 return mEGLWindow;
234}
235
Geoff Lang8a079e52013-10-18 16:13:33 -0400236int ANGLETest::getWindowWidth() const
237{
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400238 return mWidth;
Geoff Lang8a079e52013-10-18 16:13:33 -0400239}
240
241int ANGLETest::getWindowHeight() const
242{
Corentin Wallezf3357ee2015-07-22 14:10:19 -0400243 return mHeight;
Geoff Lang8a079e52013-10-18 16:13:33 -0400244}
245
Geoff Langefc551f2013-10-31 10:20:28 -0400246bool ANGLETest::isMultisampleEnabled() const
Geoff Lang8a079e52013-10-18 16:13:33 -0400247{
Jamie Madill62af5462014-08-26 13:16:37 -0400248 return mEGLWindow->isMultisample();
Geoff Lang8a079e52013-10-18 16:13:33 -0400249}
250
251bool ANGLETest::createEGLContext()
252{
Jamie Madill62af5462014-08-26 13:16:37 -0400253 return mEGLWindow->initializeGL(mOSWindow);
Geoff Lang8a079e52013-10-18 16:13:33 -0400254}
255
256bool ANGLETest::destroyEGLContext()
257{
Jamie Madill62af5462014-08-26 13:16:37 -0400258 mEGLWindow->destroyGL();
Geoff Lang8a079e52013-10-18 16:13:33 -0400259 return true;
260}
Geoff Langbb134672013-10-23 13:06:46 -0400261
Geoff Lang0d3683c2014-10-23 11:08:16 -0400262bool ANGLETest::InitTestWindow()
Jamie Madill8add0eb2014-08-26 13:16:35 -0400263{
264 mOSWindow = CreateOSWindow();
265 if (!mOSWindow->initialize("ANGLE_TEST", 128, 128))
266 {
267 return false;
268 }
269
Geoff Lang0d3683c2014-10-23 11:08:16 -0400270 mOSWindow->setVisible(true);
Jamie Madill8add0eb2014-08-26 13:16:35 -0400271
272 return true;
273}
274
Geoff Lang0d3683c2014-10-23 11:08:16 -0400275bool ANGLETest::DestroyTestWindow()
Jamie Madill8add0eb2014-08-26 13:16:35 -0400276{
277 if (mOSWindow)
278 {
279 mOSWindow->destroy();
280 delete mOSWindow;
281 mOSWindow = NULL;
282 }
283
284 return true;
285}
286
Geoff Lang0d3683c2014-10-23 11:08:16 -0400287void ANGLETest::SetWindowVisible(bool isVisible)
Geoff Langbb134672013-10-23 13:06:46 -0400288{
Jamie Madill4119ed32014-10-01 10:41:40 -0400289 mOSWindow->setVisible(isVisible);
Geoff Langbb134672013-10-23 13:06:46 -0400290}
Geoff Lang0d3683c2014-10-23 11:08:16 -0400291
Jamie Madillc3b9b262015-01-30 14:00:51 -0500292bool ANGLETest::isIntel() const
293{
294 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
295 return (rendererString.find("Intel") != std::string::npos);
296}
297
298bool ANGLETest::isAMD() const
299{
300 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
301 return (rendererString.find("AMD") != std::string::npos) ||
302 (rendererString.find("ATI") != std::string::npos);
303}
304
305bool ANGLETest::isNVidia() const
306{
307 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
308 return (rendererString.find("NVIDIA") != std::string::npos);
309}
310
Jamie Madilld55d2832015-10-27 13:59:19 -0400311bool ANGLETest::isD3D11() const
312{
313 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
314 return (rendererString.find("Direct3D11 vs_5_0") != std::string::npos);
315}
316
Jamie Madill9fc36822015-11-18 13:08:07 -0500317bool ANGLETest::isD3D11_FL93() const
318{
319 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
320 return (rendererString.find("Direct3D11 vs_4_0_") != std::string::npos);
321}
322
323bool ANGLETest::isD3D9() const
324{
325 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
326 return (rendererString.find("Direct3D9") != std::string::npos);
327}
328
329bool ANGLETest::isD3DSM3() const
330{
331 std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
332 return isD3D9() || isD3D11_FL93();
333}
334
Jamie Madillc3b9b262015-01-30 14:00:51 -0500335EGLint ANGLETest::getPlatformRenderer() const
336{
Jamie Madillf6859912015-01-30 17:05:35 -0500337 assert(mEGLWindow);
338 return mEGLWindow->getPlatform().renderer;
Jamie Madillc3b9b262015-01-30 14:00:51 -0500339}
340
Geoff Lang0d3683c2014-10-23 11:08:16 -0400341OSWindow *ANGLETest::mOSWindow = NULL;
342
343void ANGLETestEnvironment::SetUp()
344{
345 if (!ANGLETest::InitTestWindow())
346 {
347 FAIL() << "Failed to create ANGLE test window.";
348 }
349}
350
351void ANGLETestEnvironment::TearDown()
352{
353 ANGLETest::DestroyTestWindow();
354}