Geoff Lang | 8a079e5 | 2013-10-18 16:13:33 -0400 | [diff] [blame] | 1 | #include "ANGLETest.h" |
| 2 | |
| 3 | ANGLETest::ANGLETest() |
| 4 | : mClientVersion(2) |
| 5 | , mWidth(1280) |
| 6 | , mHeight(720) |
| 7 | , mRedBits(-1) |
| 8 | , mGreenBits(-1) |
| 9 | , mBlueBits(-1) |
| 10 | , mAlphaBits(-1) |
| 11 | , mDepthBits(-1) |
| 12 | , mStencilBits(-1) |
| 13 | , mMultisample(false) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | EGLDisplay ANGLETest::mDisplay = 0; |
| 18 | EGLNativeWindowType ANGLETest::mNativeWindow = 0; |
| 19 | EGLNativeDisplayType ANGLETest::mNativeDisplay = 0; |
| 20 | |
| 21 | void ANGLETest::SetUp() |
| 22 | { |
| 23 | ReizeWindow(mWidth, mHeight); |
| 24 | if (!createEGLContext()) |
| 25 | { |
| 26 | FAIL() << "egl context creation failed."; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | void ANGLETest::TearDown() |
| 31 | { |
| 32 | swapBuffers(); |
| 33 | if (!destroyEGLContext()) |
| 34 | { |
| 35 | FAIL() << "egl context destruction failed."; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void ANGLETest::swapBuffers() |
| 40 | { |
| 41 | eglSwapBuffers(mDisplay, mSurface); |
| 42 | } |
| 43 | |
| 44 | void ANGLETest::drawQuad(GLuint program, const std::string& positionAttribName, GLfloat quadDepth) |
| 45 | { |
| 46 | GLint positionLocation = glGetAttribLocation(program, positionAttribName.c_str()); |
| 47 | |
| 48 | glUseProgram(program); |
| 49 | |
| 50 | const GLfloat vertices[] = |
| 51 | { |
| 52 | -1.0f, 1.0f, quadDepth, |
| 53 | -1.0f, -1.0f, quadDepth, |
| 54 | 1.0f, -1.0f, quadDepth, |
| 55 | |
| 56 | -1.0f, 1.0f, quadDepth, |
| 57 | 1.0f, -1.0f, quadDepth, |
| 58 | 1.0f, 1.0f, quadDepth, |
| 59 | }; |
| 60 | |
| 61 | glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, vertices); |
| 62 | glEnableVertexAttribArray(positionLocation); |
| 63 | |
| 64 | glDrawArrays(GL_TRIANGLES, 0, 6); |
| 65 | |
| 66 | glDisableVertexAttribArray(positionLocation); |
| 67 | glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL); |
| 68 | |
| 69 | glUseProgram(0); |
| 70 | } |
| 71 | |
| 72 | GLuint ANGLETest::compileProgram(const std::string &vsSource, const std::string &fsSource) |
| 73 | { |
| 74 | GLuint program = glCreateProgram(); |
| 75 | |
| 76 | GLuint vs = glCreateShader(GL_VERTEX_SHADER); |
| 77 | const char *vsSourceArray[1] = { vsSource.c_str() }; |
| 78 | glShaderSource(vs, 1, vsSourceArray, NULL); |
| 79 | glCompileShader(vs); |
| 80 | glAttachShader(program, vs); |
| 81 | glDeleteShader(vs); |
| 82 | |
| 83 | GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); |
| 84 | const char *fsSourceArray[1] = { fsSource.c_str() }; |
| 85 | glShaderSource(fs, 1, fsSourceArray, NULL); |
| 86 | glCompileShader(fs); |
| 87 | glAttachShader(program, fs); |
| 88 | glDeleteShader(fs); |
| 89 | |
| 90 | glLinkProgram(program); |
| 91 | |
| 92 | GLint linkStatus; |
| 93 | glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); |
| 94 | |
| 95 | if (!linkStatus) |
| 96 | { |
| 97 | glDeleteProgram(program); |
| 98 | program = 0; |
| 99 | } |
| 100 | |
| 101 | return program; |
| 102 | } |
| 103 | |
| 104 | bool ANGLETest::extensionEnabled(const std::string &extName) |
| 105 | { |
| 106 | const char* extString = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)); |
| 107 | return strstr(extString, extName.c_str()) != NULL; |
| 108 | } |
| 109 | |
| 110 | void ANGLETest::setClientVersion(int clientVersion) |
| 111 | { |
| 112 | mClientVersion = clientVersion; |
| 113 | } |
| 114 | |
| 115 | void ANGLETest::setWindowWidth(int width) |
| 116 | { |
| 117 | mWidth = width; |
| 118 | } |
| 119 | |
| 120 | void ANGLETest::setWindowHeight(int height) |
| 121 | { |
| 122 | mHeight = height; |
| 123 | } |
| 124 | |
| 125 | void ANGLETest::setRedBits(int bits) |
| 126 | { |
| 127 | mRedBits = bits; |
| 128 | } |
| 129 | |
| 130 | void ANGLETest::setGreenBits(int bits) |
| 131 | { |
| 132 | mGreenBits = bits; |
| 133 | } |
| 134 | |
| 135 | void ANGLETest::setBlueBits(int bits) |
| 136 | { |
| 137 | mBlueBits = bits; |
| 138 | } |
| 139 | |
| 140 | void ANGLETest::setAlphaBits(int bits) |
| 141 | { |
| 142 | mAlphaBits = bits; |
| 143 | } |
| 144 | |
| 145 | void ANGLETest::setDepthBits(int bits) |
| 146 | { |
| 147 | mDepthBits = bits; |
| 148 | } |
| 149 | |
| 150 | void ANGLETest::setStencilBits(int bits) |
| 151 | { |
| 152 | mStencilBits = bits; |
| 153 | } |
| 154 | |
| 155 | void ANGLETest::setMultisampleEnabled(bool enabled) |
| 156 | { |
| 157 | mMultisample = enabled; |
| 158 | } |
| 159 | |
| 160 | int ANGLETest::getClientVersion() const |
| 161 | { |
| 162 | return mClientVersion; |
| 163 | } |
| 164 | |
| 165 | int ANGLETest::getWindowWidth() const |
| 166 | { |
| 167 | return mWidth; |
| 168 | } |
| 169 | |
| 170 | int ANGLETest::getWindowHeight() const |
| 171 | { |
| 172 | return mHeight; |
| 173 | } |
| 174 | |
| 175 | int ANGLETest::getRedBits() const |
| 176 | { |
| 177 | return mRedBits; |
| 178 | } |
| 179 | |
| 180 | int ANGLETest::getGreenBits() const |
| 181 | { |
| 182 | return mGreenBits; |
| 183 | } |
| 184 | |
| 185 | int ANGLETest::getBlueBits() const |
| 186 | { |
| 187 | return mBlueBits; |
| 188 | } |
| 189 | |
| 190 | int ANGLETest::getAlphaBits() const |
| 191 | { |
| 192 | return mAlphaBits; |
| 193 | } |
| 194 | |
| 195 | int ANGLETest::getDepthBits() const |
| 196 | { |
| 197 | return mDepthBits; |
| 198 | } |
| 199 | |
| 200 | int ANGLETest::getStencilBits() const |
| 201 | { |
| 202 | return mStencilBits; |
| 203 | } |
| 204 | |
| 205 | bool ANGLETest::multisampleEnabled() const |
| 206 | { |
| 207 | return mMultisample; |
| 208 | } |
| 209 | |
| 210 | bool ANGLETest::createEGLContext() |
| 211 | { |
| 212 | const EGLint configAttributes[] = |
| 213 | { |
| 214 | EGL_RED_SIZE, (mRedBits >= 0) ? mRedBits : EGL_DONT_CARE, |
| 215 | EGL_GREEN_SIZE, (mGreenBits >= 0) ? mGreenBits : EGL_DONT_CARE, |
| 216 | EGL_BLUE_SIZE, (mBlueBits >= 0) ? mBlueBits : EGL_DONT_CARE, |
| 217 | EGL_ALPHA_SIZE, (mAlphaBits >= 0) ? mAlphaBits : EGL_DONT_CARE, |
| 218 | EGL_DEPTH_SIZE, (mDepthBits >= 0) ? mDepthBits : EGL_DONT_CARE, |
| 219 | EGL_STENCIL_SIZE, (mStencilBits >= 0) ? mStencilBits : EGL_DONT_CARE, |
| 220 | EGL_SAMPLE_BUFFERS, mMultisample ? 1 : 0, |
| 221 | EGL_NONE |
| 222 | }; |
| 223 | |
| 224 | EGLint configCount; |
| 225 | if (!eglChooseConfig(mDisplay, configAttributes, &mConfig, 1, &configCount) || (configCount != 1)) |
| 226 | { |
| 227 | destroyEGLContext(); |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | eglGetConfigAttrib(mDisplay, mConfig, EGL_RED_SIZE, &mRedBits); |
| 232 | eglGetConfigAttrib(mDisplay, mConfig, EGL_GREEN_SIZE, &mGreenBits); |
| 233 | eglGetConfigAttrib(mDisplay, mConfig, EGL_BLUE_SIZE, &mBlueBits); |
| 234 | eglGetConfigAttrib(mDisplay, mConfig, EGL_ALPHA_SIZE, &mBlueBits); |
| 235 | eglGetConfigAttrib(mDisplay, mConfig, EGL_DEPTH_SIZE, &mDepthBits); |
| 236 | eglGetConfigAttrib(mDisplay, mConfig, EGL_STENCIL_SIZE, &mStencilBits); |
| 237 | |
| 238 | EGLint samples; |
| 239 | eglGetConfigAttrib(mDisplay, mConfig, EGL_SAMPLE_BUFFERS, &samples); |
| 240 | mMultisample = (samples != 0); |
| 241 | |
| 242 | mSurface = eglCreateWindowSurface(mDisplay, mConfig, mNativeWindow, NULL); |
| 243 | if(mSurface == EGL_NO_SURFACE) |
| 244 | { |
| 245 | eglGetError(); // Clear error |
| 246 | mSurface = eglCreateWindowSurface(mDisplay, mConfig, NULL, NULL); |
| 247 | } |
| 248 | |
| 249 | if (eglGetError() != EGL_SUCCESS) |
| 250 | { |
| 251 | destroyEGLContext(); |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | EGLint contextAttibutes[] = |
| 256 | { |
| 257 | EGL_CONTEXT_CLIENT_VERSION, mClientVersion, |
| 258 | EGL_NONE |
| 259 | }; |
| 260 | mContext = eglCreateContext(mDisplay, mConfig, NULL, contextAttibutes); |
| 261 | if (eglGetError() != EGL_SUCCESS) |
| 262 | { |
| 263 | destroyEGLContext(); |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | eglMakeCurrent(mDisplay, mSurface, mSurface, mContext); |
| 268 | if (eglGetError() != EGL_SUCCESS) |
| 269 | { |
| 270 | destroyEGLContext(); |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | bool ANGLETest::destroyEGLContext() |
| 278 | { |
| 279 | eglDestroySurface(mDisplay, mSurface); |
| 280 | eglDestroyContext(mDisplay, mContext); |
| 281 | |
| 282 | return true; |
| 283 | } |