Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 1 | #include "ANGLETest.h" |
| 2 | |
| 3 | ANGLETest::ANGLETest() |
Geoff Lang | f0955f1 | 2014-06-20 16:07:07 -0400 | [diff] [blame^] | 4 | : mTestPlatform(EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE), |
| 5 | mClientVersion(2), |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 6 | mWidth(1280), |
| 7 | mHeight(720), |
| 8 | mRedBits(-1), |
| 9 | mGreenBits(-1), |
| 10 | mBlueBits(-1), |
| 11 | mAlphaBits(-1), |
| 12 | mDepthBits(-1), |
| 13 | mStencilBits(-1), |
| 14 | mMultisample(false) |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 15 | { |
| 16 | } |
| 17 | |
| 18 | EGLDisplay ANGLETest::mDisplay = 0; |
| 19 | EGLNativeWindowType ANGLETest::mNativeWindow = 0; |
| 20 | EGLNativeDisplayType ANGLETest::mNativeDisplay = 0; |
| 21 | |
| 22 | void ANGLETest::SetUp() |
| 23 | { |
| 24 | ReizeWindow(mWidth, mHeight); |
| 25 | if (!createEGLContext()) |
| 26 | { |
| 27 | FAIL() << "egl context creation failed."; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void ANGLETest::TearDown() |
| 32 | { |
| 33 | swapBuffers(); |
| 34 | if (!destroyEGLContext()) |
| 35 | { |
| 36 | FAIL() << "egl context destruction failed."; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void ANGLETest::swapBuffers() |
| 41 | { |
| 42 | eglSwapBuffers(mDisplay, mSurface); |
| 43 | } |
| 44 | |
| 45 | void ANGLETest::drawQuad(GLuint program, const std::string& positionAttribName, GLfloat quadDepth) |
| 46 | { |
| 47 | GLint positionLocation = glGetAttribLocation(program, positionAttribName.c_str()); |
| 48 | |
| 49 | glUseProgram(program); |
| 50 | |
| 51 | const GLfloat vertices[] = |
| 52 | { |
| 53 | -1.0f, 1.0f, quadDepth, |
| 54 | -1.0f, -1.0f, quadDepth, |
| 55 | 1.0f, -1.0f, quadDepth, |
| 56 | |
| 57 | -1.0f, 1.0f, quadDepth, |
| 58 | 1.0f, -1.0f, quadDepth, |
| 59 | 1.0f, 1.0f, quadDepth, |
| 60 | }; |
| 61 | |
| 62 | glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, vertices); |
| 63 | glEnableVertexAttribArray(positionLocation); |
| 64 | |
| 65 | glDrawArrays(GL_TRIANGLES, 0, 6); |
| 66 | |
| 67 | glDisableVertexAttribArray(positionLocation); |
| 68 | glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL); |
| 69 | |
| 70 | glUseProgram(0); |
| 71 | } |
| 72 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 73 | GLuint ANGLETest::compileShader(GLenum type, const std::string &source) |
| 74 | { |
| 75 | GLuint shader = glCreateShader(type); |
| 76 | |
| 77 | const char *sourceArray[1] = { source.c_str() }; |
| 78 | glShaderSource(shader, 1, sourceArray, NULL); |
| 79 | glCompileShader(shader); |
| 80 | |
| 81 | GLint compileResult; |
| 82 | glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult); |
| 83 | |
| 84 | if (compileResult == 0) |
| 85 | { |
| 86 | GLint infoLogLength; |
| 87 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); |
| 88 | |
| 89 | std::vector<GLchar> infoLog(infoLogLength); |
| 90 | glGetShaderInfoLog(shader, infoLog.size(), NULL, infoLog.data()); |
| 91 | |
| 92 | std::cerr << "shader compilation failed: " << infoLog.data(); |
| 93 | |
| 94 | glDeleteShader(shader); |
| 95 | shader = 0; |
| 96 | } |
| 97 | |
| 98 | return shader; |
| 99 | } |
| 100 | |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 101 | GLuint ANGLETest::compileProgram(const std::string &vsSource, const std::string &fsSource) |
| 102 | { |
| 103 | GLuint program = glCreateProgram(); |
| 104 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 105 | GLuint vs = compileShader(GL_VERTEX_SHADER, vsSource); |
| 106 | GLuint fs = compileShader(GL_FRAGMENT_SHADER, fsSource); |
| 107 | |
| 108 | if (vs == 0 || fs == 0) |
| 109 | { |
| 110 | glDeleteShader(fs); |
| 111 | glDeleteShader(vs); |
| 112 | glDeleteProgram(program); |
| 113 | return 0; |
| 114 | } |
| 115 | |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 116 | glAttachShader(program, vs); |
| 117 | glDeleteShader(vs); |
| 118 | |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 119 | glAttachShader(program, fs); |
| 120 | glDeleteShader(fs); |
| 121 | |
| 122 | glLinkProgram(program); |
| 123 | |
| 124 | GLint linkStatus; |
| 125 | glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); |
| 126 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 127 | if (linkStatus == 0) |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 128 | { |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 129 | GLint infoLogLength; |
| 130 | glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength); |
| 131 | |
| 132 | std::vector<GLchar> infoLog(infoLogLength); |
| 133 | glGetProgramInfoLog(program, infoLog.size(), NULL, infoLog.data()); |
| 134 | |
| 135 | std::cerr << "program link failed: " << infoLog.data(); |
| 136 | |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 137 | glDeleteProgram(program); |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 138 | return 0; |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return program; |
| 142 | } |
| 143 | |
| 144 | bool ANGLETest::extensionEnabled(const std::string &extName) |
| 145 | { |
| 146 | const char* extString = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); |
| 147 | return strstr(extString, extName.c_str()) != NULL; |
| 148 | } |
| 149 | |
| 150 | void ANGLETest::setClientVersion(int clientVersion) |
| 151 | { |
| 152 | mClientVersion = clientVersion; |
| 153 | } |
| 154 | |
| 155 | void ANGLETest::setWindowWidth(int width) |
| 156 | { |
| 157 | mWidth = width; |
| 158 | } |
| 159 | |
| 160 | void ANGLETest::setWindowHeight(int height) |
| 161 | { |
| 162 | mHeight = height; |
| 163 | } |
| 164 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 165 | void ANGLETest::setConfigRedBits(int bits) |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 166 | { |
| 167 | mRedBits = bits; |
| 168 | } |
| 169 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 170 | void ANGLETest::setConfigGreenBits(int bits) |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 171 | { |
| 172 | mGreenBits = bits; |
| 173 | } |
| 174 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 175 | void ANGLETest::setConfigBlueBits(int bits) |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 176 | { |
| 177 | mBlueBits = bits; |
| 178 | } |
| 179 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 180 | void ANGLETest::setConfigAlphaBits(int bits) |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 181 | { |
| 182 | mAlphaBits = bits; |
| 183 | } |
| 184 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 185 | void ANGLETest::setConfigDepthBits(int bits) |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 186 | { |
| 187 | mDepthBits = bits; |
| 188 | } |
| 189 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 190 | void ANGLETest::setConfigStencilBits(int bits) |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 191 | { |
| 192 | mStencilBits = bits; |
| 193 | } |
| 194 | |
| 195 | void ANGLETest::setMultisampleEnabled(bool enabled) |
| 196 | { |
| 197 | mMultisample = enabled; |
| 198 | } |
| 199 | |
| 200 | int ANGLETest::getClientVersion() const |
| 201 | { |
| 202 | return mClientVersion; |
| 203 | } |
| 204 | |
| 205 | int ANGLETest::getWindowWidth() const |
| 206 | { |
| 207 | return mWidth; |
| 208 | } |
| 209 | |
| 210 | int ANGLETest::getWindowHeight() const |
| 211 | { |
| 212 | return mHeight; |
| 213 | } |
| 214 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 215 | int ANGLETest::getConfigRedBits() const |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 216 | { |
| 217 | return mRedBits; |
| 218 | } |
| 219 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 220 | int ANGLETest::getConfigGreenBits() const |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 221 | { |
| 222 | return mGreenBits; |
| 223 | } |
| 224 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 225 | int ANGLETest::getConfigBlueBits() const |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 226 | { |
| 227 | return mBlueBits; |
| 228 | } |
| 229 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 230 | int ANGLETest::getConfigAlphaBits() const |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 231 | { |
| 232 | return mAlphaBits; |
| 233 | } |
| 234 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 235 | int ANGLETest::getConfigDepthBits() const |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 236 | { |
| 237 | return mDepthBits; |
| 238 | } |
| 239 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 240 | int ANGLETest::getConfigStencilBits() const |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 241 | { |
| 242 | return mStencilBits; |
| 243 | } |
| 244 | |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 245 | bool ANGLETest::isMultisampleEnabled() const |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 246 | { |
| 247 | return mMultisample; |
| 248 | } |
| 249 | |
| 250 | bool ANGLETest::createEGLContext() |
| 251 | { |
Geoff Lang | f0955f1 | 2014-06-20 16:07:07 -0400 | [diff] [blame^] | 252 | PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT")); |
| 253 | if (!eglGetPlatformDisplayEXT) |
| 254 | { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | const EGLint displayAttributes[] = |
| 259 | { |
| 260 | EGL_PLATFORM_ANGLE_TYPE_ANGLE, mTestPlatform, |
| 261 | EGL_NONE, |
| 262 | }; |
| 263 | |
| 264 | mDisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, mNativeDisplay, displayAttributes); |
| 265 | if (mDisplay == EGL_NO_DISPLAY) |
| 266 | { |
| 267 | destroyEGLContext(); |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | EGLint majorVersion, minorVersion; |
| 272 | if (!eglInitialize(mDisplay, &majorVersion, &minorVersion)) |
| 273 | { |
| 274 | destroyEGLContext(); |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | eglBindAPI(EGL_OPENGL_ES_API); |
| 279 | if (eglGetError() != EGL_SUCCESS) |
| 280 | { |
| 281 | destroyEGLContext(); |
| 282 | return false; |
| 283 | } |
| 284 | |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 285 | const EGLint configAttributes[] = |
| 286 | { |
| 287 | EGL_RED_SIZE, (mRedBits >= 0) ? mRedBits : EGL_DONT_CARE, |
| 288 | EGL_GREEN_SIZE, (mGreenBits >= 0) ? mGreenBits : EGL_DONT_CARE, |
| 289 | EGL_BLUE_SIZE, (mBlueBits >= 0) ? mBlueBits : EGL_DONT_CARE, |
| 290 | EGL_ALPHA_SIZE, (mAlphaBits >= 0) ? mAlphaBits : EGL_DONT_CARE, |
| 291 | EGL_DEPTH_SIZE, (mDepthBits >= 0) ? mDepthBits : EGL_DONT_CARE, |
| 292 | EGL_STENCIL_SIZE, (mStencilBits >= 0) ? mStencilBits : EGL_DONT_CARE, |
| 293 | EGL_SAMPLE_BUFFERS, mMultisample ? 1 : 0, |
| 294 | EGL_NONE |
| 295 | }; |
| 296 | |
| 297 | EGLint configCount; |
| 298 | if (!eglChooseConfig(mDisplay, configAttributes, &mConfig, 1, &configCount) || (configCount != 1)) |
| 299 | { |
| 300 | destroyEGLContext(); |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | eglGetConfigAttrib(mDisplay, mConfig, EGL_RED_SIZE, &mRedBits); |
| 305 | eglGetConfigAttrib(mDisplay, mConfig, EGL_GREEN_SIZE, &mGreenBits); |
| 306 | eglGetConfigAttrib(mDisplay, mConfig, EGL_BLUE_SIZE, &mBlueBits); |
| 307 | eglGetConfigAttrib(mDisplay, mConfig, EGL_ALPHA_SIZE, &mBlueBits); |
| 308 | eglGetConfigAttrib(mDisplay, mConfig, EGL_DEPTH_SIZE, &mDepthBits); |
| 309 | eglGetConfigAttrib(mDisplay, mConfig, EGL_STENCIL_SIZE, &mStencilBits); |
| 310 | |
| 311 | EGLint samples; |
| 312 | eglGetConfigAttrib(mDisplay, mConfig, EGL_SAMPLE_BUFFERS, &samples); |
| 313 | mMultisample = (samples != 0); |
| 314 | |
| 315 | mSurface = eglCreateWindowSurface(mDisplay, mConfig, mNativeWindow, NULL); |
| 316 | if(mSurface == EGL_NO_SURFACE) |
| 317 | { |
| 318 | eglGetError(); // Clear error |
| 319 | mSurface = eglCreateWindowSurface(mDisplay, mConfig, NULL, NULL); |
| 320 | } |
| 321 | |
| 322 | if (eglGetError() != EGL_SUCCESS) |
| 323 | { |
| 324 | destroyEGLContext(); |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | EGLint contextAttibutes[] = |
| 329 | { |
| 330 | EGL_CONTEXT_CLIENT_VERSION, mClientVersion, |
| 331 | EGL_NONE |
| 332 | }; |
| 333 | mContext = eglCreateContext(mDisplay, mConfig, NULL, contextAttibutes); |
| 334 | if (eglGetError() != EGL_SUCCESS) |
| 335 | { |
| 336 | destroyEGLContext(); |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | eglMakeCurrent(mDisplay, mSurface, mSurface, mContext); |
| 341 | if (eglGetError() != EGL_SUCCESS) |
| 342 | { |
| 343 | destroyEGLContext(); |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | bool ANGLETest::destroyEGLContext() |
| 351 | { |
Geoff Lang | f0955f1 | 2014-06-20 16:07:07 -0400 | [diff] [blame^] | 352 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 353 | eglDestroySurface(mDisplay, mSurface); |
| 354 | eglDestroyContext(mDisplay, mContext); |
Geoff Lang | f0955f1 | 2014-06-20 16:07:07 -0400 | [diff] [blame^] | 355 | eglTerminate(mDisplay); |
Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 356 | |
| 357 | return true; |
| 358 | } |
Geoff Lang | bb13467 | 2013-10-23 13:06:46 -0400 | [diff] [blame] | 359 | |
| 360 | void ANGLETestEnvironment::SetUp() |
| 361 | { |
| 362 | if (!ANGLETest::InitTestWindow()) |
| 363 | { |
| 364 | FAIL() << "Failed to create ANGLE test window."; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | void ANGLETestEnvironment::TearDown() |
| 369 | { |
| 370 | ANGLETest::DestroyTestWindow(); |
| 371 | } |