blob: f6cefa66c3d055045ed3bf6604f9e2a7d56d804b [file] [log] [blame]
Jamie Gennisd99c0882011-03-10 16:24:46 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis5451d152011-06-08 09:40:45 -070017//#define LOG_NDEBUG 0
18
Jamie Gennisd99c0882011-03-10 16:24:46 -080019#include <gtest/gtest.h>
20#include <gui/SurfaceTexture.h>
21#include <gui/SurfaceTextureClient.h>
22#include <ui/GraphicBuffer.h>
23#include <utils/String8.h>
Jamie Gennis5451d152011-06-08 09:40:45 -070024#include <utils/threads.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080025
26#include <surfaceflinger/ISurfaceComposer.h>
27#include <surfaceflinger/Surface.h>
28#include <surfaceflinger/SurfaceComposerClient.h>
29
30#include <EGL/egl.h>
31#include <EGL/eglext.h>
32#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
34
35#include <ui/FramebufferNativeWindow.h>
36
37namespace android {
38
39class GLTest : public ::testing::Test {
40protected:
41
42 GLTest():
43 mEglDisplay(EGL_NO_DISPLAY),
44 mEglSurface(EGL_NO_SURFACE),
45 mEglContext(EGL_NO_CONTEXT) {
46 }
47
48 virtual void SetUp() {
Jamie Gennisd99c0882011-03-10 16:24:46 -080049 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
50 ASSERT_EQ(EGL_SUCCESS, eglGetError());
51 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
52
53 EGLint majorVersion;
54 EGLint minorVersion;
55 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
56 ASSERT_EQ(EGL_SUCCESS, eglGetError());
57 RecordProperty("EglVersionMajor", majorVersion);
58 RecordProperty("EglVersionMajor", minorVersion);
59
Jamie Gennisd99c0882011-03-10 16:24:46 -080060 EGLint numConfigs = 0;
Jamie Gennis1876d132011-03-17 16:32:52 -070061 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080062 1, &numConfigs));
63 ASSERT_EQ(EGL_SUCCESS, eglGetError());
64
65 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
66 if (displaySecsEnv != NULL) {
67 mDisplaySecs = atoi(displaySecsEnv);
68 if (mDisplaySecs < 0) {
69 mDisplaySecs = 0;
70 }
71 } else {
72 mDisplaySecs = 0;
73 }
74
75 if (mDisplaySecs > 0) {
76 mComposerClient = new SurfaceComposerClient;
77 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
78
Jamie Gennisfc850122011-04-25 16:40:05 -070079 mSurfaceControl = mComposerClient->createSurface(
Jamie Gennisd99c0882011-03-10 16:24:46 -080080 String8("Test Surface"), 0,
81 getSurfaceWidth(), getSurfaceHeight(),
82 PIXEL_FORMAT_RGB_888, 0);
83
84 ASSERT_TRUE(mSurfaceControl != NULL);
85 ASSERT_TRUE(mSurfaceControl->isValid());
86
87 ASSERT_EQ(NO_ERROR, mComposerClient->openTransaction());
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070088 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080089 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
90 ASSERT_EQ(NO_ERROR, mComposerClient->closeTransaction());
91
92 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -070093 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080094 window.get(), NULL);
95 } else {
96 EGLint pbufferAttribs[] = {
97 EGL_WIDTH, getSurfaceWidth(),
98 EGL_HEIGHT, getSurfaceHeight(),
99 EGL_NONE };
100
Jamie Gennis1876d132011-03-17 16:32:52 -0700101 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800102 pbufferAttribs);
103 }
104 ASSERT_EQ(EGL_SUCCESS, eglGetError());
105 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
106
Jamie Gennis1876d132011-03-17 16:32:52 -0700107 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800108 getContextAttribs());
109 ASSERT_EQ(EGL_SUCCESS, eglGetError());
110 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
111
112 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
113 mEglContext));
114 ASSERT_EQ(EGL_SUCCESS, eglGetError());
115
116 EGLint w, h;
117 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
118 ASSERT_EQ(EGL_SUCCESS, eglGetError());
119 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
120 ASSERT_EQ(EGL_SUCCESS, eglGetError());
121 RecordProperty("EglSurfaceWidth", w);
122 RecordProperty("EglSurfaceHeight", h);
123
124 glViewport(0, 0, w, h);
125 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
126 }
127
128 virtual void TearDown() {
129 // Display the result
130 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
131 eglSwapBuffers(mEglDisplay, mEglSurface);
132 sleep(mDisplaySecs);
133 }
134
135 if (mComposerClient != NULL) {
136 mComposerClient->dispose();
137 }
138 if (mEglContext != EGL_NO_CONTEXT) {
139 eglDestroyContext(mEglDisplay, mEglContext);
140 }
141 if (mEglSurface != EGL_NO_SURFACE) {
142 eglDestroySurface(mEglDisplay, mEglSurface);
143 }
144 if (mEglDisplay != EGL_NO_DISPLAY) {
145 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
146 EGL_NO_CONTEXT);
147 eglTerminate(mEglDisplay);
148 }
149 ASSERT_EQ(EGL_SUCCESS, eglGetError());
150 }
151
152 virtual EGLint const* getConfigAttribs() {
153 static EGLint sDefaultConfigAttribs[] = {
154 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
155 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
156 EGL_RED_SIZE, 8,
157 EGL_GREEN_SIZE, 8,
158 EGL_BLUE_SIZE, 8,
159 EGL_ALPHA_SIZE, 8,
160 EGL_DEPTH_SIZE, 16,
161 EGL_STENCIL_SIZE, 8,
162 EGL_NONE };
163
164 return sDefaultConfigAttribs;
165 }
166
167 virtual EGLint const* getContextAttribs() {
168 static EGLint sDefaultContextAttribs[] = {
169 EGL_CONTEXT_CLIENT_VERSION, 2,
170 EGL_NONE };
171
172 return sDefaultContextAttribs;
173 }
174
175 virtual EGLint getSurfaceWidth() {
176 return 64;
177 }
178
179 virtual EGLint getSurfaceHeight() {
180 return 64;
181 }
182
183 void loadShader(GLenum shaderType, const char* pSource, GLuint* outShader) {
184 GLuint shader = glCreateShader(shaderType);
185 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
186 if (shader) {
187 glShaderSource(shader, 1, &pSource, NULL);
188 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
189 glCompileShader(shader);
190 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
191 GLint compiled = 0;
192 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
193 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
194 if (!compiled) {
195 GLint infoLen = 0;
196 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
197 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
198 if (infoLen) {
199 char* buf = (char*) malloc(infoLen);
200 if (buf) {
201 glGetShaderInfoLog(shader, infoLen, NULL, buf);
202 printf("Shader compile log:\n%s\n", buf);
203 free(buf);
204 FAIL();
205 }
206 } else {
207 char* buf = (char*) malloc(0x1000);
208 if (buf) {
209 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
210 printf("Shader compile log:\n%s\n", buf);
211 free(buf);
212 FAIL();
213 }
214 }
215 glDeleteShader(shader);
216 shader = 0;
217 }
218 }
219 ASSERT_TRUE(shader != 0);
220 *outShader = shader;
221 }
222
223 void createProgram(const char* pVertexSource, const char* pFragmentSource,
224 GLuint* outPgm) {
225 GLuint vertexShader, fragmentShader;
226 {
227 SCOPED_TRACE("compiling vertex shader");
228 loadShader(GL_VERTEX_SHADER, pVertexSource, &vertexShader);
229 if (HasFatalFailure()) {
230 return;
231 }
232 }
233 {
234 SCOPED_TRACE("compiling fragment shader");
235 loadShader(GL_FRAGMENT_SHADER, pFragmentSource, &fragmentShader);
236 if (HasFatalFailure()) {
237 return;
238 }
239 }
240
241 GLuint program = glCreateProgram();
242 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
243 if (program) {
244 glAttachShader(program, vertexShader);
245 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
246 glAttachShader(program, fragmentShader);
247 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
248 glLinkProgram(program);
249 GLint linkStatus = GL_FALSE;
250 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
251 if (linkStatus != GL_TRUE) {
252 GLint bufLength = 0;
253 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
254 if (bufLength) {
255 char* buf = (char*) malloc(bufLength);
256 if (buf) {
257 glGetProgramInfoLog(program, bufLength, NULL, buf);
258 printf("Program link log:\n%s\n", buf);
259 free(buf);
260 FAIL();
261 }
262 }
263 glDeleteProgram(program);
264 program = 0;
265 }
266 }
267 glDeleteShader(vertexShader);
268 glDeleteShader(fragmentShader);
269 ASSERT_TRUE(program != 0);
270 *outPgm = program;
271 }
272
Jamie Gennis824efa72011-06-13 13:41:01 -0700273 static int abs(int value) {
274 return value > 0 ? value : -value;
275 }
276
Jamie Gennisd99c0882011-03-10 16:24:46 -0800277 ::testing::AssertionResult checkPixel(int x, int y, int r,
Jamie Gennis824efa72011-06-13 13:41:01 -0700278 int g, int b, int a, int tolerance=2) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800279 GLubyte pixel[4];
280 String8 msg;
281 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
282 GLenum err = glGetError();
283 if (err != GL_NO_ERROR) {
284 msg += String8::format("error reading pixel: %#x", err);
285 while ((err = glGetError()) != GL_NO_ERROR) {
286 msg += String8::format(", %#x", err);
287 }
288 fprintf(stderr, "pixel check failure: %s\n", msg.string());
289 return ::testing::AssertionFailure(
290 ::testing::Message(msg.string()));
291 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700292 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800293 msg += String8::format("r(%d isn't %d)", pixel[0], r);
294 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700295 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800296 if (!msg.isEmpty()) {
297 msg += " ";
298 }
299 msg += String8::format("g(%d isn't %d)", pixel[1], g);
300 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700301 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800302 if (!msg.isEmpty()) {
303 msg += " ";
304 }
305 msg += String8::format("b(%d isn't %d)", pixel[2], b);
306 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700307 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800308 if (!msg.isEmpty()) {
309 msg += " ";
310 }
311 msg += String8::format("a(%d isn't %d)", pixel[3], a);
312 }
313 if (!msg.isEmpty()) {
314 fprintf(stderr, "pixel check failure: %s\n", msg.string());
315 return ::testing::AssertionFailure(
316 ::testing::Message(msg.string()));
317 } else {
318 return ::testing::AssertionSuccess();
319 }
320 }
321
322 int mDisplaySecs;
323 sp<SurfaceComposerClient> mComposerClient;
324 sp<SurfaceControl> mSurfaceControl;
325
326 EGLDisplay mEglDisplay;
327 EGLSurface mEglSurface;
328 EGLContext mEglContext;
Jamie Gennis1876d132011-03-17 16:32:52 -0700329 EGLConfig mGlConfig;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800330};
331
332// XXX: Code above this point should live elsewhere
333
334class SurfaceTextureGLTest : public GLTest {
335protected:
336 static const GLint TEX_ID = 123;
337
338 virtual void SetUp() {
339 GLTest::SetUp();
340 mST = new SurfaceTexture(TEX_ID);
341 mSTC = new SurfaceTextureClient(mST);
342 mANW = mSTC;
343
344 const char vsrc[] =
345 "attribute vec4 vPosition;\n"
346 "varying vec2 texCoords;\n"
347 "uniform mat4 texMatrix;\n"
348 "void main() {\n"
349 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
350 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
351 " gl_Position = vPosition;\n"
352 "}\n";
353
354 const char fsrc[] =
355 "#extension GL_OES_EGL_image_external : require\n"
356 "precision mediump float;\n"
357 "uniform samplerExternalOES texSampler;\n"
358 "varying vec2 texCoords;\n"
359 "void main() {\n"
360 " gl_FragColor = texture2D(texSampler, texCoords);\n"
361 "}\n";
362
363 {
364 SCOPED_TRACE("creating shader program");
365 createProgram(vsrc, fsrc, &mPgm);
366 if (HasFatalFailure()) {
367 return;
368 }
369 }
370
371 mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
372 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
373 ASSERT_NE(-1, mPositionHandle);
374 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
375 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
376 ASSERT_NE(-1, mTexSamplerHandle);
377 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
378 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
379 ASSERT_NE(-1, mTexMatrixHandle);
380 }
381
382 // drawTexture draws the SurfaceTexture over the entire GL viewport.
383 void drawTexture() {
384 const GLfloat triangleVertices[] = {
385 -1.0f, 1.0f,
386 -1.0f, -1.0f,
387 1.0f, -1.0f,
388 1.0f, 1.0f,
389 };
390
391 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, triangleVertices);
392 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
393 glEnableVertexAttribArray(mPositionHandle);
394 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
395
396 glUseProgram(mPgm);
397 glUniform1i(mTexSamplerHandle, 0);
398 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
399 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
400 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
401
Jamie Gennis1876d132011-03-17 16:32:52 -0700402 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
403 // they're setting the defautls for that target, but when hacking things
404 // to use GL_TEXTURE_2D they are needed to achieve the same behavior.
405 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
406 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
407 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
408 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
409 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
410 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
411 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
412 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
413
Jamie Gennisd99c0882011-03-10 16:24:46 -0800414 GLfloat texMatrix[16];
415 mST->getTransformMatrix(texMatrix);
416 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
417
418 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
419 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
420 }
421
422 sp<SurfaceTexture> mST;
423 sp<SurfaceTextureClient> mSTC;
424 sp<ANativeWindow> mANW;
425
426 GLuint mPgm;
427 GLint mPositionHandle;
428 GLint mTexSamplerHandle;
429 GLint mTexMatrixHandle;
430};
431
432// Fill a YV12 buffer with a multi-colored checkerboard pattern
433void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
434 const int blockWidth = w > 16 ? w / 16 : 1;
435 const int blockHeight = h > 16 ? h / 16 : 1;
436 const int yuvTexOffsetY = 0;
437 int yuvTexStrideY = stride;
438 int yuvTexOffsetV = yuvTexStrideY * h;
439 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
440 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
441 int yuvTexStrideU = yuvTexStrideV;
442 for (int x = 0; x < w; x++) {
443 for (int y = 0; y < h; y++) {
444 int parityX = (x / blockWidth) & 1;
445 int parityY = (y / blockHeight) & 1;
446 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
447 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
448 if (x < w / 2 && y < h / 2) {
449 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
450 if (x * 2 < w / 2 && y * 2 < h / 2) {
451 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
452 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
453 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
454 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
455 intensity;
456 }
457 }
458 }
459 }
460}
461
462// Fill a YV12 buffer with red outside a given rectangle and green inside it.
463void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
464 const android_native_rect_t& rect) {
465 const int yuvTexOffsetY = 0;
466 int yuvTexStrideY = stride;
467 int yuvTexOffsetV = yuvTexStrideY * h;
468 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
469 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
470 int yuvTexStrideU = yuvTexStrideV;
471 for (int x = 0; x < w; x++) {
472 for (int y = 0; y < h; y++) {
473 bool inside = rect.left <= x && x < rect.right &&
474 rect.top <= y && y < rect.bottom;
475 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
476 if (x < w / 2 && y < h / 2) {
477 bool inside = rect.left <= 2*x && 2*x < rect.right &&
478 rect.top <= 2*y && 2*y < rect.bottom;
479 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
480 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
481 inside ? 16 : 255;
482 }
483 }
484 }
485}
486
Jamie Gennis1876d132011-03-17 16:32:52 -0700487void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
488 const size_t PIXEL_SIZE = 4;
489 for (int x = 0; x < w; x++) {
490 for (int y = 0; y < h; y++) {
491 off_t offset = (y * stride + x) * PIXEL_SIZE;
492 for (int c = 0; c < 4; c++) {
493 int parityX = (x / (1 << (c+2))) & 1;
494 int parityY = (y / (1 << (c+2))) & 1;
495 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
496 }
497 }
498 }
499}
500
Jamie Gennisd99c0882011-03-10 16:24:46 -0800501TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700502 const int texWidth = 64;
503 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800504
505 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700506 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800507 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
508 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
509
Iliyan Malchev697526b2011-05-01 11:33:26 -0700510 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800511 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
512 ASSERT_TRUE(anb != NULL);
513
514 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
515 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
516
517 // Fill the buffer with the a checkerboard pattern
518 uint8_t* img = NULL;
519 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700520 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800521 buf->unlock();
522 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
523
524 mST->updateTexImage();
525
526 glClearColor(0.2, 0.2, 0.2, 0.2);
527 glClear(GL_COLOR_BUFFER_BIT);
528
529 drawTexture();
530
531 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255));
532 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255));
533 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
534 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
535
536 EXPECT_TRUE(checkPixel(22, 44, 247, 70, 255, 255));
537 EXPECT_TRUE(checkPixel(45, 52, 209, 32, 235, 255));
538 EXPECT_TRUE(checkPixel(52, 51, 100, 255, 73, 255));
539 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255));
540 EXPECT_TRUE(checkPixel(31, 9, 148, 71, 110, 255));
541 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255));
542 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255));
543}
544
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700545TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700546 const int texWidth = 64;
547 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800548
549 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700550 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800551 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
552 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
553
Iliyan Malchev697526b2011-05-01 11:33:26 -0700554 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800555 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
556 ASSERT_TRUE(anb != NULL);
557
558 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
559 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
560
561 // Fill the buffer with the a checkerboard pattern
562 uint8_t* img = NULL;
563 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700564 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800565 buf->unlock();
566 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
567
568 mST->updateTexImage();
569
570 glClearColor(0.2, 0.2, 0.2, 0.2);
571 glClear(GL_COLOR_BUFFER_BIT);
572
573 drawTexture();
574
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700575 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
576 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800577 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
578 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
579
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700580 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
581 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
582 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
583 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
584 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
585 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
586 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800587}
588
589TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700590 const int texWidth = 64;
591 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800592
593 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700594 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800595 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
596 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
597
598 android_native_rect_t crops[] = {
599 {4, 6, 22, 36},
600 {0, 6, 22, 36},
601 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700602 {4, 6, texWidth, 36},
603 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800604 };
605
606 for (int i = 0; i < 5; i++) {
607 const android_native_rect_t& crop(crops[i]);
608 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }", crop.left,
609 crop.top, crop.right, crop.bottom).string());
610
611 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
612
Iliyan Malchev697526b2011-05-01 11:33:26 -0700613 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800614 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
615 ASSERT_TRUE(anb != NULL);
616
617 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
618 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
619
620 uint8_t* img = NULL;
621 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700622 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800623 buf->unlock();
624 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
625
626 mST->updateTexImage();
627
628 glClearColor(0.2, 0.2, 0.2, 0.2);
629 glClear(GL_COLOR_BUFFER_BIT);
630
631 drawTexture();
632
633 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
634 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
635 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
636 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
637
638 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
639 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
640 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
641 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
642 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
643 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
644 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
645 }
646}
647
Jamie Gennis1876d132011-03-17 16:32:52 -0700648// XXX: This test is disabled because there are currently no drivers that can
649// handle RGBA textures with the GL_TEXTURE_EXTERNAL_OES target.
650TEST_F(SurfaceTextureGLTest, DISABLED_TexturingFromCpuFilledRGBABufferNpot) {
651 const int texWidth = 64;
652 const int texHeight = 66;
653
654 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
655 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
656 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
657 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
658
659 android_native_buffer_t* anb;
660 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
661 ASSERT_TRUE(anb != NULL);
662
663 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
664 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
665
666 // Fill the buffer with the a checkerboard pattern
667 uint8_t* img = NULL;
668 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
669 fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride());
670 buf->unlock();
671 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
672
673 mST->updateTexImage();
674
675 glClearColor(0.2, 0.2, 0.2, 0.2);
676 glClear(GL_COLOR_BUFFER_BIT);
677
678 drawTexture();
679
680 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
681 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
682 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
683 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
684
685 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
686 EXPECT_TRUE(checkPixel(24, 63, 35, 231, 231, 35));
687 EXPECT_TRUE(checkPixel(19, 40, 87, 179, 35, 35));
688 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
689 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
690 EXPECT_TRUE(checkPixel(37, 33, 35, 231, 231, 231));
691 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
692 EXPECT_TRUE(checkPixel(36, 47, 231, 35, 231, 231));
693 EXPECT_TRUE(checkPixel(24, 63, 35, 231, 231, 35));
694 EXPECT_TRUE(checkPixel(48, 3, 231, 231, 35, 35));
695 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
696 EXPECT_TRUE(checkPixel(24, 25, 191, 191, 231, 231));
697 EXPECT_TRUE(checkPixel(10, 9, 93, 93, 231, 231));
698 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
699 EXPECT_TRUE(checkPixel(56, 31, 35, 231, 231, 35));
700 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
701}
702
703// XXX: This test is disabled because there are currently no drivers that can
704// handle RGBA textures with the GL_TEXTURE_EXTERNAL_OES target.
705TEST_F(SurfaceTextureGLTest, DISABLED_TexturingFromCpuFilledRGBABufferPow2) {
706 const int texWidth = 64;
707 const int texHeight = 64;
708
709 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
710 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
711 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
712 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
713
714 android_native_buffer_t* anb;
715 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
716 ASSERT_TRUE(anb != NULL);
717
718 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
719 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
720
721 // Fill the buffer with the a checkerboard pattern
722 uint8_t* img = NULL;
723 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
724 fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride());
725 buf->unlock();
726 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
727
728 mST->updateTexImage();
729
730 glClearColor(0.2, 0.2, 0.2, 0.2);
731 glClear(GL_COLOR_BUFFER_BIT);
732
733 drawTexture();
734
735 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
736 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
737 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
738 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
739
740 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
741 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
742 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
743 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
744 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
745 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
746 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
747 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
748 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
749 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
750 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
751 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
752 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
753 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
754 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
755 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
756}
757
758// XXX: This test is disabled because there are currently no drivers that can
759// handle RGBA textures with the GL_TEXTURE_EXTERNAL_OES target.
760TEST_F(SurfaceTextureGLTest, DISABLED_TexturingFromGLFilledRGBABufferPow2) {
761 const int texWidth = 64;
762 const int texHeight = 64;
763
764 mST->setDefaultBufferSize(texWidth, texHeight);
765
766 // Do the producer side of things
767 EGLSurface stcEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
768 mANW.get(), NULL);
769 ASSERT_EQ(EGL_SUCCESS, eglGetError());
770 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
771
772 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, stcEglSurface, stcEglSurface,
773 mEglContext));
774 ASSERT_EQ(EGL_SUCCESS, eglGetError());
775
776 glClearColor(0.6, 0.6, 0.6, 0.6);
777 glClear(GL_COLOR_BUFFER_BIT);
778
779 glEnable(GL_SCISSOR_TEST);
780 glScissor(4, 4, 4, 4);
781 glClearColor(1.0, 0.0, 0.0, 1.0);
782 glClear(GL_COLOR_BUFFER_BIT);
783
784 glScissor(24, 48, 4, 4);
785 glClearColor(0.0, 1.0, 0.0, 1.0);
786 glClear(GL_COLOR_BUFFER_BIT);
787
788 glScissor(37, 17, 4, 4);
789 glClearColor(0.0, 0.0, 1.0, 1.0);
790 glClear(GL_COLOR_BUFFER_BIT);
791
792 eglSwapBuffers(mEglDisplay, stcEglSurface);
793
794 // Do the consumer side of things
795 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
796 mEglContext));
797 ASSERT_EQ(EGL_SUCCESS, eglGetError());
798
799 glDisable(GL_SCISSOR_TEST);
800
801 mST->updateTexImage();
802
803 glClearColor(0.2, 0.2, 0.2, 0.2);
804 glClear(GL_COLOR_BUFFER_BIT);
805
806 drawTexture();
807
808 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
809 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
810 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
811 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
812
813 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
814 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
815 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
816 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
817 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
818 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
819 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
820 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
821 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
822 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
823 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
824 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
825 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
826 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
827 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
828 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
829}
830
Jamie Gennis5451d152011-06-08 09:40:45 -0700831/*
832 * This test is for testing GL -> GL texture streaming via SurfaceTexture. It
833 * contains functionality to create a producer thread that will perform GL
834 * rendering to an ANativeWindow that feeds frames to a SurfaceTexture.
835 * Additionally it supports interlocking the producer and consumer threads so
836 * that a specific sequence of calls can be deterministically created by the
837 * test.
838 *
839 * The intended usage is as follows:
840 *
841 * TEST_F(...) {
842 * class PT : public ProducerThread {
843 * virtual void render() {
844 * ...
845 * swapBuffers();
846 * }
847 * };
848 *
849 * runProducerThread(new PT());
850 *
851 * // The order of these calls will vary from test to test and may include
852 * // multiple frames and additional operations (e.g. GL rendering from the
853 * // texture).
854 * fc->waitForFrame();
855 * mST->updateTexImage();
856 * fc->finishFrame();
857 * }
858 *
859 */
860class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
861protected:
862
863 // ProducerThread is an abstract base class to simplify the creation of
864 // OpenGL ES frame producer threads.
865 class ProducerThread : public Thread {
866 public:
867 virtual ~ProducerThread() {
868 }
869
870 void setEglObjects(EGLDisplay producerEglDisplay,
871 EGLSurface producerEglSurface,
872 EGLContext producerEglContext) {
873 mProducerEglDisplay = producerEglDisplay;
874 mProducerEglSurface = producerEglSurface;
875 mProducerEglContext = producerEglContext;
876 }
877
878 virtual bool threadLoop() {
879 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
880 mProducerEglSurface, mProducerEglContext);
881 render();
882 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
883 EGL_NO_CONTEXT);
884 return false;
885 }
886
887 protected:
888 virtual void render() = 0;
889
890 void swapBuffers() {
891 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
892 }
893
894 EGLDisplay mProducerEglDisplay;
895 EGLSurface mProducerEglSurface;
896 EGLContext mProducerEglContext;
897 };
898
899 // FrameCondition is a utility class for interlocking between the producer
900 // and consumer threads. The FrameCondition object should be created and
901 // destroyed in the consumer thread only. The consumer thread should set
902 // the FrameCondition as the FrameAvailableListener of the SurfaceTexture,
903 // and should call both waitForFrame and finishFrame once for each expected
904 // frame.
905 //
906 // This interlocking relies on the fact that onFrameAvailable gets called
907 // synchronously from SurfaceTexture::queueBuffer.
908 class FrameCondition : public SurfaceTexture::FrameAvailableListener {
909 public:
910 // waitForFrame waits for the next frame to arrive. This should be
911 // called from the consumer thread once for every frame expected by the
912 // test.
913 void waitForFrame() {
914 LOGV("+waitForFrame");
915 Mutex::Autolock lock(mMutex);
916 status_t result = mFrameAvailableCondition.wait(mMutex);
917 LOGV("-waitForFrame");
918 }
919
920 // Allow the producer to return from its swapBuffers call and continue
921 // on to produce the next frame. This should be called by the consumer
922 // thread once for every frame expected by the test.
923 void finishFrame() {
924 LOGV("+finishFrame");
925 Mutex::Autolock lock(mMutex);
926 mFrameFinishCondition.signal();
927 LOGV("-finishFrame");
928 }
929
930 // This should be called by SurfaceTexture on the producer thread.
931 virtual void onFrameAvailable() {
932 LOGV("+onFrameAvailable");
933 Mutex::Autolock lock(mMutex);
934 mFrameAvailableCondition.signal();
935 mFrameFinishCondition.wait(mMutex);
936 LOGV("-onFrameAvailable");
937 }
938
939 protected:
940 Mutex mMutex;
941 Condition mFrameAvailableCondition;
942 Condition mFrameFinishCondition;
943 };
944
945 SurfaceTextureGLToGLTest():
946 mProducerEglSurface(EGL_NO_SURFACE),
947 mProducerEglContext(EGL_NO_CONTEXT) {
948 }
949
950 virtual void SetUp() {
951 SurfaceTextureGLTest::SetUp();
952
953 EGLConfig myConfig = {0};
954 EGLint numConfigs = 0;
955 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &myConfig,
956 1, &numConfigs));
957 ASSERT_EQ(EGL_SUCCESS, eglGetError());
958
959 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, myConfig,
960 mANW.get(), NULL);
961 ASSERT_EQ(EGL_SUCCESS, eglGetError());
962 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
963
964 mProducerEglContext = eglCreateContext(mEglDisplay, myConfig,
965 EGL_NO_CONTEXT, getContextAttribs());
966 ASSERT_EQ(EGL_SUCCESS, eglGetError());
967 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
968
969 mFC = new FrameCondition();
970 mST->setFrameAvailableListener(mFC);
971 }
972
973 virtual void TearDown() {
974 if (mProducerThread != NULL) {
975 mProducerThread->requestExitAndWait();
976 }
977 if (mProducerEglContext != EGL_NO_CONTEXT) {
978 eglDestroyContext(mEglDisplay, mProducerEglContext);
979 }
980 if (mProducerEglSurface != EGL_NO_SURFACE) {
981 eglDestroySurface(mEglDisplay, mProducerEglSurface);
982 }
983 mProducerThread.clear();
984 mFC.clear();
985 }
986
987 void runProducerThread(const sp<ProducerThread> producerThread) {
988 ASSERT_TRUE(mProducerThread == NULL);
989 mProducerThread = producerThread;
990 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
991 mProducerEglContext);
992 producerThread->run();
993 }
994
995 EGLSurface mProducerEglSurface;
996 EGLContext mProducerEglContext;
997 sp<ProducerThread> mProducerThread;
998 sp<FrameCondition> mFC;
999};
1000
1001// XXX: This test is disabled because it causes hangs on some devices.
1002TEST_F(SurfaceTextureGLToGLTest, DISABLED_UpdateTexImageBeforeFrameFinishedWorks) {
1003 class PT : public ProducerThread {
1004 virtual void render() {
1005 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1006 glClear(GL_COLOR_BUFFER_BIT);
1007 swapBuffers();
1008 }
1009 };
1010
1011 runProducerThread(new PT());
1012
1013 mFC->waitForFrame();
1014 mST->updateTexImage();
1015 mFC->finishFrame();
1016
1017 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08001018}
Jamie Gennis5451d152011-06-08 09:40:45 -07001019
Jamie Gennis2510d952011-06-13 13:43:51 -07001020// XXX: This test is disabled because it causes hangs on some devices.
1021TEST_F(SurfaceTextureGLToGLTest, DISABLED_UpdateTexImageAfterFrameFinishedWorks) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001022 class PT : public ProducerThread {
1023 virtual void render() {
1024 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1025 glClear(GL_COLOR_BUFFER_BIT);
1026 swapBuffers();
1027 }
1028 };
1029
1030 runProducerThread(new PT());
1031
1032 mFC->waitForFrame();
1033 mFC->finishFrame();
1034 mST->updateTexImage();
1035
1036 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1037}
1038
1039// XXX: This test is disabled because it causes hangs on some devices.
1040TEST_F(SurfaceTextureGLToGLTest, DISABLED_RepeatedUpdateTexImageBeforeFrameFinishedWorks) {
1041 enum { NUM_ITERATIONS = 1024 };
1042
1043 class PT : public ProducerThread {
1044 virtual void render() {
1045 for (int i = 0; i < NUM_ITERATIONS; i++) {
1046 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1047 glClear(GL_COLOR_BUFFER_BIT);
1048 LOGV("+swapBuffers");
1049 swapBuffers();
1050 LOGV("-swapBuffers");
1051 }
1052 }
1053 };
1054
1055 runProducerThread(new PT());
1056
1057 for (int i = 0; i < NUM_ITERATIONS; i++) {
1058 mFC->waitForFrame();
1059 LOGV("+updateTexImage");
1060 mST->updateTexImage();
1061 LOGV("-updateTexImage");
1062 mFC->finishFrame();
1063
1064 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1065 }
1066}
1067
1068// XXX: This test is disabled because it causes hangs on some devices.
1069TEST_F(SurfaceTextureGLToGLTest, DISABLED_RepeatedUpdateTexImageAfterFrameFinishedWorks) {
1070 enum { NUM_ITERATIONS = 1024 };
1071
1072 class PT : public ProducerThread {
1073 virtual void render() {
1074 for (int i = 0; i < NUM_ITERATIONS; i++) {
1075 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1076 glClear(GL_COLOR_BUFFER_BIT);
1077 LOGV("+swapBuffers");
1078 swapBuffers();
1079 LOGV("-swapBuffers");
1080 }
1081 }
1082 };
1083
1084 runProducerThread(new PT());
1085
1086 for (int i = 0; i < NUM_ITERATIONS; i++) {
1087 mFC->waitForFrame();
1088 mFC->finishFrame();
1089 LOGV("+updateTexImage");
1090 mST->updateTexImage();
1091 LOGV("-updateTexImage");
1092
1093 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1094 }
1095}
1096
1097} // namespace android