• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "ANGLETest.h"
2 
ANGLETest()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 
SetUp()21 void ANGLETest::SetUp()
22 {
23     ReizeWindow(mWidth, mHeight);
24     if (!createEGLContext())
25     {
26         FAIL() << "egl context creation failed.";
27     }
28 }
29 
TearDown()30 void ANGLETest::TearDown()
31 {
32     swapBuffers();
33     if (!destroyEGLContext())
34     {
35         FAIL() << "egl context destruction failed.";
36     }
37 }
38 
swapBuffers()39 void ANGLETest::swapBuffers()
40 {
41     eglSwapBuffers(mDisplay, mSurface);
42 }
43 
drawQuad(GLuint program,const std::string & positionAttribName,GLfloat quadDepth)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 
compileShader(GLenum type,const std::string & source)72 GLuint ANGLETest::compileShader(GLenum type, const std::string &source)
73 {
74     GLuint shader = glCreateShader(type);
75 
76     const char *sourceArray[1] = { source.c_str() };
77     glShaderSource(shader, 1, sourceArray, NULL);
78     glCompileShader(shader);
79 
80     GLint compileResult;
81     glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
82 
83     if (compileResult == 0)
84     {
85         GLint infoLogLength;
86         glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
87 
88         std::vector<GLchar> infoLog(infoLogLength);
89         glGetShaderInfoLog(shader, infoLog.size(), NULL, infoLog.data());
90 
91         std::cerr << "shader compilation failed: " << infoLog.data();
92 
93         glDeleteShader(shader);
94         shader = 0;
95     }
96 
97     return shader;
98 }
99 
compileProgram(const std::string & vsSource,const std::string & fsSource)100 GLuint ANGLETest::compileProgram(const std::string &vsSource, const std::string &fsSource)
101 {
102     GLuint program = glCreateProgram();
103 
104     GLuint vs = compileShader(GL_VERTEX_SHADER, vsSource);
105     GLuint fs = compileShader(GL_FRAGMENT_SHADER, fsSource);
106 
107     if (vs == 0 || fs == 0)
108     {
109         glDeleteShader(fs);
110         glDeleteShader(vs);
111         glDeleteProgram(program);
112         return 0;
113     }
114 
115     glAttachShader(program, vs);
116     glDeleteShader(vs);
117 
118     glAttachShader(program, fs);
119     glDeleteShader(fs);
120 
121     glLinkProgram(program);
122 
123     GLint linkStatus;
124     glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
125 
126     if (linkStatus == 0)
127     {
128         GLint infoLogLength;
129         glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLength);
130 
131         std::vector<GLchar> infoLog(infoLogLength);
132         glGetProgramInfoLog(program, infoLog.size(), NULL, infoLog.data());
133 
134         std::cerr << "program link failed: " << infoLog.data();
135 
136         glDeleteProgram(program);
137         return 0;
138     }
139 
140     return program;
141 }
142 
extensionEnabled(const std::string & extName)143 bool ANGLETest::extensionEnabled(const std::string &extName)
144 {
145     const char* extString = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
146     return strstr(extString, extName.c_str()) != NULL;
147 }
148 
setClientVersion(int clientVersion)149 void ANGLETest::setClientVersion(int clientVersion)
150 {
151     mClientVersion = clientVersion;
152 }
153 
setWindowWidth(int width)154 void ANGLETest::setWindowWidth(int width)
155 {
156     mWidth = width;
157 }
158 
setWindowHeight(int height)159 void ANGLETest::setWindowHeight(int height)
160 {
161     mHeight = height;
162 }
163 
setConfigRedBits(int bits)164 void ANGLETest::setConfigRedBits(int bits)
165 {
166     mRedBits = bits;
167 }
168 
setConfigGreenBits(int bits)169 void ANGLETest::setConfigGreenBits(int bits)
170 {
171     mGreenBits = bits;
172 }
173 
setConfigBlueBits(int bits)174 void ANGLETest::setConfigBlueBits(int bits)
175 {
176     mBlueBits = bits;
177 }
178 
setConfigAlphaBits(int bits)179 void ANGLETest::setConfigAlphaBits(int bits)
180 {
181     mAlphaBits = bits;
182 }
183 
setConfigDepthBits(int bits)184 void ANGLETest::setConfigDepthBits(int bits)
185 {
186     mDepthBits = bits;
187 }
188 
setConfigStencilBits(int bits)189 void ANGLETest::setConfigStencilBits(int bits)
190 {
191     mStencilBits = bits;
192 }
193 
setMultisampleEnabled(bool enabled)194 void ANGLETest::setMultisampleEnabled(bool enabled)
195 {
196     mMultisample = enabled;
197 }
198 
getClientVersion() const199 int ANGLETest::getClientVersion() const
200 {
201     return mClientVersion;
202 }
203 
getWindowWidth() const204 int ANGLETest::getWindowWidth() const
205 {
206     return mWidth;
207 }
208 
getWindowHeight() const209 int ANGLETest::getWindowHeight() const
210 {
211     return mHeight;
212 }
213 
getConfigRedBits() const214 int ANGLETest::getConfigRedBits() const
215 {
216     return mRedBits;
217 }
218 
getConfigGreenBits() const219 int ANGLETest::getConfigGreenBits() const
220 {
221     return mGreenBits;
222 }
223 
getConfigBlueBits() const224 int ANGLETest::getConfigBlueBits() const
225 {
226     return mBlueBits;
227 }
228 
getConfigAlphaBits() const229 int ANGLETest::getConfigAlphaBits() const
230 {
231     return mAlphaBits;
232 }
233 
getConfigDepthBits() const234 int ANGLETest::getConfigDepthBits() const
235 {
236     return mDepthBits;
237 }
238 
getConfigStencilBits() const239 int ANGLETest::getConfigStencilBits() const
240 {
241     return mStencilBits;
242 }
243 
isMultisampleEnabled() const244 bool ANGLETest::isMultisampleEnabled() const
245 {
246     return mMultisample;
247 }
248 
createEGLContext()249 bool ANGLETest::createEGLContext()
250 {
251     const EGLint configAttributes[] =
252     {
253         EGL_RED_SIZE,       (mRedBits >= 0)     ? mRedBits     : EGL_DONT_CARE,
254         EGL_GREEN_SIZE,     (mGreenBits >= 0)   ? mGreenBits   : EGL_DONT_CARE,
255         EGL_BLUE_SIZE,      (mBlueBits >= 0)    ? mBlueBits    : EGL_DONT_CARE,
256         EGL_ALPHA_SIZE,     (mAlphaBits >= 0)   ? mAlphaBits   : EGL_DONT_CARE,
257         EGL_DEPTH_SIZE,     (mDepthBits >= 0)   ? mDepthBits   : EGL_DONT_CARE,
258         EGL_STENCIL_SIZE,   (mStencilBits >= 0) ? mStencilBits : EGL_DONT_CARE,
259         EGL_SAMPLE_BUFFERS, mMultisample ? 1 : 0,
260         EGL_NONE
261     };
262 
263     EGLint configCount;
264     if (!eglChooseConfig(mDisplay, configAttributes, &mConfig, 1, &configCount) || (configCount != 1))
265     {
266         destroyEGLContext();
267         return false;
268     }
269 
270     eglGetConfigAttrib(mDisplay, mConfig, EGL_RED_SIZE, &mRedBits);
271     eglGetConfigAttrib(mDisplay, mConfig, EGL_GREEN_SIZE, &mGreenBits);
272     eglGetConfigAttrib(mDisplay, mConfig, EGL_BLUE_SIZE, &mBlueBits);
273     eglGetConfigAttrib(mDisplay, mConfig, EGL_ALPHA_SIZE, &mBlueBits);
274     eglGetConfigAttrib(mDisplay, mConfig, EGL_DEPTH_SIZE, &mDepthBits);
275     eglGetConfigAttrib(mDisplay, mConfig, EGL_STENCIL_SIZE, &mStencilBits);
276 
277     EGLint samples;
278     eglGetConfigAttrib(mDisplay, mConfig, EGL_SAMPLE_BUFFERS, &samples);
279     mMultisample = (samples != 0);
280 
281     mSurface = eglCreateWindowSurface(mDisplay, mConfig, mNativeWindow, NULL);
282     if(mSurface == EGL_NO_SURFACE)
283     {
284         eglGetError(); // Clear error
285         mSurface = eglCreateWindowSurface(mDisplay, mConfig, NULL, NULL);
286     }
287 
288     if (eglGetError() != EGL_SUCCESS)
289     {
290         destroyEGLContext();
291         return false;
292     }
293 
294     EGLint contextAttibutes[] =
295     {
296         EGL_CONTEXT_CLIENT_VERSION, mClientVersion,
297         EGL_NONE
298     };
299     mContext = eglCreateContext(mDisplay, mConfig, NULL, contextAttibutes);
300     if (eglGetError() != EGL_SUCCESS)
301     {
302         destroyEGLContext();
303         return false;
304     }
305 
306     eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
307     if (eglGetError() != EGL_SUCCESS)
308     {
309         destroyEGLContext();
310         return false;
311     }
312 
313     return true;
314 }
315 
destroyEGLContext()316 bool ANGLETest::destroyEGLContext()
317 {
318     eglDestroySurface(mDisplay, mSurface);
319     eglDestroyContext(mDisplay, mContext);
320 
321     return true;
322 }
323 
SetUp()324 void ANGLETestEnvironment::SetUp()
325 {
326     if (!ANGLETest::InitTestWindow())
327     {
328         FAIL() << "Failed to create ANGLE test window.";
329     }
330 }
331 
TearDown()332 void ANGLETestEnvironment::TearDown()
333 {
334     ANGLETest::DestroyTestWindow();
335 }
336