blob: cce18ae950d7f6210e0ecf8350f0fb20bdc23124 [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 Gennis2640bfd2011-07-14 17:11:47 -070017#define LOG_TAG "SurfaceTexture_test"
Jamie Gennis5451d152011-06-08 09:40:45 -070018//#define LOG_NDEBUG 0
19
Jamie Gennisd99c0882011-03-10 16:24:46 -080020#include <gtest/gtest.h>
21#include <gui/SurfaceTexture.h>
22#include <gui/SurfaceTextureClient.h>
23#include <ui/GraphicBuffer.h>
24#include <utils/String8.h>
Jamie Gennis5451d152011-06-08 09:40:45 -070025#include <utils/threads.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080026
27#include <surfaceflinger/ISurfaceComposer.h>
28#include <surfaceflinger/Surface.h>
29#include <surfaceflinger/SurfaceComposerClient.h>
30
31#include <EGL/egl.h>
32#include <EGL/eglext.h>
33#include <GLES2/gl2.h>
34#include <GLES2/gl2ext.h>
35
36#include <ui/FramebufferNativeWindow.h>
37
38namespace android {
39
40class GLTest : public ::testing::Test {
41protected:
42
43 GLTest():
44 mEglDisplay(EGL_NO_DISPLAY),
45 mEglSurface(EGL_NO_SURFACE),
46 mEglContext(EGL_NO_CONTEXT) {
47 }
48
49 virtual void SetUp() {
Jamie Gennisd99c0882011-03-10 16:24:46 -080050 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
51 ASSERT_EQ(EGL_SUCCESS, eglGetError());
52 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
53
54 EGLint majorVersion;
55 EGLint minorVersion;
56 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
57 ASSERT_EQ(EGL_SUCCESS, eglGetError());
58 RecordProperty("EglVersionMajor", majorVersion);
59 RecordProperty("EglVersionMajor", minorVersion);
60
Jamie Gennisd99c0882011-03-10 16:24:46 -080061 EGLint numConfigs = 0;
Jamie Gennis1876d132011-03-17 16:32:52 -070062 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080063 1, &numConfigs));
64 ASSERT_EQ(EGL_SUCCESS, eglGetError());
65
66 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
67 if (displaySecsEnv != NULL) {
68 mDisplaySecs = atoi(displaySecsEnv);
69 if (mDisplaySecs < 0) {
70 mDisplaySecs = 0;
71 }
72 } else {
73 mDisplaySecs = 0;
74 }
75
76 if (mDisplaySecs > 0) {
77 mComposerClient = new SurfaceComposerClient;
78 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
79
Jamie Gennisfc850122011-04-25 16:40:05 -070080 mSurfaceControl = mComposerClient->createSurface(
Jamie Gennisd99c0882011-03-10 16:24:46 -080081 String8("Test Surface"), 0,
82 getSurfaceWidth(), getSurfaceHeight(),
83 PIXEL_FORMAT_RGB_888, 0);
84
85 ASSERT_TRUE(mSurfaceControl != NULL);
86 ASSERT_TRUE(mSurfaceControl->isValid());
87
Mathias Agopian698c0872011-06-28 19:09:31 -070088 SurfaceComposerClient::openGlobalTransaction();
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070089 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080090 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070091 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennisd99c0882011-03-10 16:24:46 -080092
93 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -070094 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080095 window.get(), NULL);
96 } else {
97 EGLint pbufferAttribs[] = {
98 EGL_WIDTH, getSurfaceWidth(),
99 EGL_HEIGHT, getSurfaceHeight(),
100 EGL_NONE };
101
Jamie Gennis1876d132011-03-17 16:32:52 -0700102 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800103 pbufferAttribs);
104 }
105 ASSERT_EQ(EGL_SUCCESS, eglGetError());
106 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
107
Jamie Gennis1876d132011-03-17 16:32:52 -0700108 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800109 getContextAttribs());
110 ASSERT_EQ(EGL_SUCCESS, eglGetError());
111 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
112
113 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
114 mEglContext));
115 ASSERT_EQ(EGL_SUCCESS, eglGetError());
116
117 EGLint w, h;
118 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
119 ASSERT_EQ(EGL_SUCCESS, eglGetError());
120 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
121 ASSERT_EQ(EGL_SUCCESS, eglGetError());
122 RecordProperty("EglSurfaceWidth", w);
123 RecordProperty("EglSurfaceHeight", h);
124
125 glViewport(0, 0, w, h);
126 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
127 }
128
129 virtual void TearDown() {
130 // Display the result
131 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
132 eglSwapBuffers(mEglDisplay, mEglSurface);
133 sleep(mDisplaySecs);
134 }
135
136 if (mComposerClient != NULL) {
137 mComposerClient->dispose();
138 }
139 if (mEglContext != EGL_NO_CONTEXT) {
140 eglDestroyContext(mEglDisplay, mEglContext);
141 }
142 if (mEglSurface != EGL_NO_SURFACE) {
143 eglDestroySurface(mEglDisplay, mEglSurface);
144 }
145 if (mEglDisplay != EGL_NO_DISPLAY) {
146 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
147 EGL_NO_CONTEXT);
148 eglTerminate(mEglDisplay);
149 }
150 ASSERT_EQ(EGL_SUCCESS, eglGetError());
151 }
152
153 virtual EGLint const* getConfigAttribs() {
154 static EGLint sDefaultConfigAttribs[] = {
155 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
156 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
157 EGL_RED_SIZE, 8,
158 EGL_GREEN_SIZE, 8,
159 EGL_BLUE_SIZE, 8,
160 EGL_ALPHA_SIZE, 8,
161 EGL_DEPTH_SIZE, 16,
162 EGL_STENCIL_SIZE, 8,
163 EGL_NONE };
164
165 return sDefaultConfigAttribs;
166 }
167
168 virtual EGLint const* getContextAttribs() {
169 static EGLint sDefaultContextAttribs[] = {
170 EGL_CONTEXT_CLIENT_VERSION, 2,
171 EGL_NONE };
172
173 return sDefaultContextAttribs;
174 }
175
176 virtual EGLint getSurfaceWidth() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700177 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800178 }
179
180 virtual EGLint getSurfaceHeight() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700181 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800182 }
183
184 void loadShader(GLenum shaderType, const char* pSource, GLuint* outShader) {
185 GLuint shader = glCreateShader(shaderType);
186 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
187 if (shader) {
188 glShaderSource(shader, 1, &pSource, NULL);
189 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
190 glCompileShader(shader);
191 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
192 GLint compiled = 0;
193 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
194 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
195 if (!compiled) {
196 GLint infoLen = 0;
197 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
198 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
199 if (infoLen) {
200 char* buf = (char*) malloc(infoLen);
201 if (buf) {
202 glGetShaderInfoLog(shader, infoLen, NULL, buf);
203 printf("Shader compile log:\n%s\n", buf);
204 free(buf);
205 FAIL();
206 }
207 } else {
208 char* buf = (char*) malloc(0x1000);
209 if (buf) {
210 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
211 printf("Shader compile log:\n%s\n", buf);
212 free(buf);
213 FAIL();
214 }
215 }
216 glDeleteShader(shader);
217 shader = 0;
218 }
219 }
220 ASSERT_TRUE(shader != 0);
221 *outShader = shader;
222 }
223
224 void createProgram(const char* pVertexSource, const char* pFragmentSource,
225 GLuint* outPgm) {
226 GLuint vertexShader, fragmentShader;
227 {
228 SCOPED_TRACE("compiling vertex shader");
229 loadShader(GL_VERTEX_SHADER, pVertexSource, &vertexShader);
230 if (HasFatalFailure()) {
231 return;
232 }
233 }
234 {
235 SCOPED_TRACE("compiling fragment shader");
236 loadShader(GL_FRAGMENT_SHADER, pFragmentSource, &fragmentShader);
237 if (HasFatalFailure()) {
238 return;
239 }
240 }
241
242 GLuint program = glCreateProgram();
243 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
244 if (program) {
245 glAttachShader(program, vertexShader);
246 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
247 glAttachShader(program, fragmentShader);
248 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
249 glLinkProgram(program);
250 GLint linkStatus = GL_FALSE;
251 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
252 if (linkStatus != GL_TRUE) {
253 GLint bufLength = 0;
254 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
255 if (bufLength) {
256 char* buf = (char*) malloc(bufLength);
257 if (buf) {
258 glGetProgramInfoLog(program, bufLength, NULL, buf);
259 printf("Program link log:\n%s\n", buf);
260 free(buf);
261 FAIL();
262 }
263 }
264 glDeleteProgram(program);
265 program = 0;
266 }
267 }
268 glDeleteShader(vertexShader);
269 glDeleteShader(fragmentShader);
270 ASSERT_TRUE(program != 0);
271 *outPgm = program;
272 }
273
Jamie Gennis824efa72011-06-13 13:41:01 -0700274 static int abs(int value) {
275 return value > 0 ? value : -value;
276 }
277
Jamie Gennisd99c0882011-03-10 16:24:46 -0800278 ::testing::AssertionResult checkPixel(int x, int y, int r,
Jamie Gennis824efa72011-06-13 13:41:01 -0700279 int g, int b, int a, int tolerance=2) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800280 GLubyte pixel[4];
281 String8 msg;
282 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
283 GLenum err = glGetError();
284 if (err != GL_NO_ERROR) {
285 msg += String8::format("error reading pixel: %#x", err);
286 while ((err = glGetError()) != GL_NO_ERROR) {
287 msg += String8::format(", %#x", err);
288 }
289 fprintf(stderr, "pixel check failure: %s\n", msg.string());
290 return ::testing::AssertionFailure(
291 ::testing::Message(msg.string()));
292 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700293 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800294 msg += String8::format("r(%d isn't %d)", pixel[0], r);
295 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700296 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800297 if (!msg.isEmpty()) {
298 msg += " ";
299 }
300 msg += String8::format("g(%d isn't %d)", pixel[1], g);
301 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700302 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800303 if (!msg.isEmpty()) {
304 msg += " ";
305 }
306 msg += String8::format("b(%d isn't %d)", pixel[2], b);
307 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700308 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800309 if (!msg.isEmpty()) {
310 msg += " ";
311 }
312 msg += String8::format("a(%d isn't %d)", pixel[3], a);
313 }
314 if (!msg.isEmpty()) {
315 fprintf(stderr, "pixel check failure: %s\n", msg.string());
316 return ::testing::AssertionFailure(
317 ::testing::Message(msg.string()));
318 } else {
319 return ::testing::AssertionSuccess();
320 }
321 }
322
323 int mDisplaySecs;
324 sp<SurfaceComposerClient> mComposerClient;
325 sp<SurfaceControl> mSurfaceControl;
326
327 EGLDisplay mEglDisplay;
328 EGLSurface mEglSurface;
329 EGLContext mEglContext;
Jamie Gennis1876d132011-03-17 16:32:52 -0700330 EGLConfig mGlConfig;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800331};
332
333// XXX: Code above this point should live elsewhere
334
335class SurfaceTextureGLTest : public GLTest {
336protected:
Jamie Gennis79e31252011-10-19 15:19:19 -0700337 enum { TEX_ID = 123 };
Jamie Gennisd99c0882011-03-10 16:24:46 -0800338
339 virtual void SetUp() {
340 GLTest::SetUp();
341 mST = new SurfaceTexture(TEX_ID);
342 mSTC = new SurfaceTextureClient(mST);
343 mANW = mSTC;
344
345 const char vsrc[] =
346 "attribute vec4 vPosition;\n"
347 "varying vec2 texCoords;\n"
348 "uniform mat4 texMatrix;\n"
349 "void main() {\n"
350 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
351 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
352 " gl_Position = vPosition;\n"
353 "}\n";
354
355 const char fsrc[] =
356 "#extension GL_OES_EGL_image_external : require\n"
357 "precision mediump float;\n"
358 "uniform samplerExternalOES texSampler;\n"
359 "varying vec2 texCoords;\n"
360 "void main() {\n"
361 " gl_FragColor = texture2D(texSampler, texCoords);\n"
362 "}\n";
363
364 {
365 SCOPED_TRACE("creating shader program");
366 createProgram(vsrc, fsrc, &mPgm);
367 if (HasFatalFailure()) {
368 return;
369 }
370 }
371
372 mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
373 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
374 ASSERT_NE(-1, mPositionHandle);
375 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
376 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
377 ASSERT_NE(-1, mTexSamplerHandle);
378 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
379 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
380 ASSERT_NE(-1, mTexMatrixHandle);
381 }
382
Jamie Gennis2640bfd2011-07-14 17:11:47 -0700383 virtual void TearDown() {
384 mANW.clear();
385 mSTC.clear();
386 mST.clear();
387 GLTest::TearDown();
388 }
389
Jamie Gennisd99c0882011-03-10 16:24:46 -0800390 // drawTexture draws the SurfaceTexture over the entire GL viewport.
391 void drawTexture() {
392 const GLfloat triangleVertices[] = {
393 -1.0f, 1.0f,
394 -1.0f, -1.0f,
395 1.0f, -1.0f,
396 1.0f, 1.0f,
397 };
398
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800399 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
400 triangleVertices);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800401 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
402 glEnableVertexAttribArray(mPositionHandle);
403 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
404
405 glUseProgram(mPgm);
406 glUniform1i(mTexSamplerHandle, 0);
407 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
408 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
409 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
410
Jamie Gennis1876d132011-03-17 16:32:52 -0700411 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
412 // they're setting the defautls for that target, but when hacking things
413 // to use GL_TEXTURE_2D they are needed to achieve the same behavior.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800414 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
415 GL_LINEAR);
Jamie Gennis1876d132011-03-17 16:32:52 -0700416 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800417 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
418 GL_LINEAR);
Jamie Gennis1876d132011-03-17 16:32:52 -0700419 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800420 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
421 GL_CLAMP_TO_EDGE);
Jamie Gennis1876d132011-03-17 16:32:52 -0700422 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800423 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
424 GL_CLAMP_TO_EDGE);
Jamie Gennis1876d132011-03-17 16:32:52 -0700425 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
426
Jamie Gennisd99c0882011-03-10 16:24:46 -0800427 GLfloat texMatrix[16];
428 mST->getTransformMatrix(texMatrix);
429 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
430
431 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
432 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
433 }
434
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700435 class FrameWaiter : public SurfaceTexture::FrameAvailableListener {
436 public:
437 FrameWaiter():
438 mPendingFrames(0) {
439 }
440
441 void waitForFrame() {
442 Mutex::Autolock lock(mMutex);
443 while (mPendingFrames == 0) {
444 mCondition.wait(mMutex);
445 }
446 mPendingFrames--;
447 }
448
449 virtual void onFrameAvailable() {
450 Mutex::Autolock lock(mMutex);
451 mPendingFrames++;
452 mCondition.signal();
453 }
454
455 int mPendingFrames;
456 Mutex mMutex;
457 Condition mCondition;
458 };
459
Jamie Gennisd99c0882011-03-10 16:24:46 -0800460 sp<SurfaceTexture> mST;
461 sp<SurfaceTextureClient> mSTC;
462 sp<ANativeWindow> mANW;
463
464 GLuint mPgm;
465 GLint mPositionHandle;
466 GLint mTexSamplerHandle;
467 GLint mTexMatrixHandle;
468};
469
470// Fill a YV12 buffer with a multi-colored checkerboard pattern
471void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
472 const int blockWidth = w > 16 ? w / 16 : 1;
473 const int blockHeight = h > 16 ? h / 16 : 1;
474 const int yuvTexOffsetY = 0;
475 int yuvTexStrideY = stride;
476 int yuvTexOffsetV = yuvTexStrideY * h;
477 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
478 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
479 int yuvTexStrideU = yuvTexStrideV;
480 for (int x = 0; x < w; x++) {
481 for (int y = 0; y < h; y++) {
482 int parityX = (x / blockWidth) & 1;
483 int parityY = (y / blockHeight) & 1;
484 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
485 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
486 if (x < w / 2 && y < h / 2) {
487 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
488 if (x * 2 < w / 2 && y * 2 < h / 2) {
489 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
490 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
491 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
492 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
493 intensity;
494 }
495 }
496 }
497 }
498}
499
500// Fill a YV12 buffer with red outside a given rectangle and green inside it.
501void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
502 const android_native_rect_t& rect) {
503 const int yuvTexOffsetY = 0;
504 int yuvTexStrideY = stride;
505 int yuvTexOffsetV = yuvTexStrideY * h;
506 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
507 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
508 int yuvTexStrideU = yuvTexStrideV;
509 for (int x = 0; x < w; x++) {
510 for (int y = 0; y < h; y++) {
511 bool inside = rect.left <= x && x < rect.right &&
512 rect.top <= y && y < rect.bottom;
513 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
514 if (x < w / 2 && y < h / 2) {
515 bool inside = rect.left <= 2*x && 2*x < rect.right &&
516 rect.top <= 2*y && 2*y < rect.bottom;
517 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
518 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
519 inside ? 16 : 255;
520 }
521 }
522 }
523}
524
Jamie Gennis1876d132011-03-17 16:32:52 -0700525void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
526 const size_t PIXEL_SIZE = 4;
527 for (int x = 0; x < w; x++) {
528 for (int y = 0; y < h; y++) {
529 off_t offset = (y * stride + x) * PIXEL_SIZE;
530 for (int c = 0; c < 4; c++) {
531 int parityX = (x / (1 << (c+2))) & 1;
532 int parityY = (y / (1 << (c+2))) & 1;
533 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
534 }
535 }
536 }
537}
538
Jamie Gennisd99c0882011-03-10 16:24:46 -0800539TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700540 const int texWidth = 64;
541 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800542
543 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700544 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800545 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
546 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
547
Iliyan Malchev697526b2011-05-01 11:33:26 -0700548 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800549 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
550 ASSERT_TRUE(anb != NULL);
551
552 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
553 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
554
555 // Fill the buffer with the a checkerboard pattern
556 uint8_t* img = NULL;
557 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700558 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800559 buf->unlock();
560 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
561
562 mST->updateTexImage();
563
564 glClearColor(0.2, 0.2, 0.2, 0.2);
565 glClear(GL_COLOR_BUFFER_BIT);
566
Jamie Gennisc8c51522011-06-15 14:24:38 -0700567 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800568 drawTexture();
569
570 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255));
571 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700572 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255));
573 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800574
Jamie Gennisc8c51522011-06-15 14:24:38 -0700575 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255));
576 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255));
577 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800578 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700579 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800580 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255));
581 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255));
582}
583
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700584TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700585 const int texWidth = 64;
586 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800587
588 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700589 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800590 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
591 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
592
Iliyan Malchev697526b2011-05-01 11:33:26 -0700593 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800594 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
595 ASSERT_TRUE(anb != NULL);
596
597 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
598 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
599
600 // Fill the buffer with the a checkerboard pattern
601 uint8_t* img = NULL;
602 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700603 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800604 buf->unlock();
605 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
606
607 mST->updateTexImage();
608
609 glClearColor(0.2, 0.2, 0.2, 0.2);
610 glClear(GL_COLOR_BUFFER_BIT);
611
Jamie Gennisc8c51522011-06-15 14:24:38 -0700612 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800613 drawTexture();
614
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700615 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
616 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800617 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
618 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
619
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700620 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
621 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
622 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
623 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
624 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
625 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
626 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800627}
628
629TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700630 const int texWidth = 64;
631 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800632
633 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700634 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800635 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
636 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
637
638 android_native_rect_t crops[] = {
639 {4, 6, 22, 36},
640 {0, 6, 22, 36},
641 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700642 {4, 6, texWidth, 36},
643 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800644 };
645
646 for (int i = 0; i < 5; i++) {
647 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800648 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
649 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800650
651 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
652
Iliyan Malchev697526b2011-05-01 11:33:26 -0700653 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800654 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
655 ASSERT_TRUE(anb != NULL);
656
657 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800658 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(),
659 buf->getNativeBuffer()));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800660
661 uint8_t* img = NULL;
662 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700663 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800664 buf->unlock();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800665 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
666 buf->getNativeBuffer()));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800667
668 mST->updateTexImage();
669
670 glClearColor(0.2, 0.2, 0.2, 0.2);
671 glClear(GL_COLOR_BUFFER_BIT);
672
Jamie Gennisc8c51522011-06-15 14:24:38 -0700673 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800674 drawTexture();
675
676 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
677 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
678 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
679 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
680
681 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
682 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
683 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
684 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
685 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
686 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
687 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
688 }
689}
690
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700691// This test is intended to catch synchronization bugs between the CPU-written
692// and GPU-read buffers.
693TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
694 enum { texWidth = 16 };
695 enum { texHeight = 16 };
696 enum { numFrames = 1024 };
697
698 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
699 ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2));
700 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
701 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
702 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
703 GRALLOC_USAGE_SW_WRITE_OFTEN));
704
705 struct TestPixel {
706 int x;
707 int y;
708 };
709 const TestPixel testPixels[] = {
710 { 4, 11 },
711 { 12, 14 },
712 { 7, 2 },
713 };
714 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
715
716 class ProducerThread : public Thread {
717 public:
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800718 ProducerThread(const sp<ANativeWindow>& anw,
719 const TestPixel* testPixels):
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700720 mANW(anw),
721 mTestPixels(testPixels) {
722 }
723
724 virtual ~ProducerThread() {
725 }
726
727 virtual bool threadLoop() {
728 for (int i = 0; i < numFrames; i++) {
729 ANativeWindowBuffer* anb;
730 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
731 return false;
732 }
733 if (anb == NULL) {
734 return false;
735 }
736
737 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
738 if (mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())
739 != NO_ERROR) {
740 return false;
741 }
742
743 const int yuvTexOffsetY = 0;
744 int stride = buf->getStride();
745 int yuvTexStrideY = stride;
746 int yuvTexOffsetV = yuvTexStrideY * texHeight;
747 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
748 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
749 int yuvTexStrideU = yuvTexStrideV;
750
751 uint8_t* img = NULL;
752 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
753
754 // Gray out all the test pixels first, so we're more likely to
755 // see a failure if GL is still texturing from the buffer we
756 // just dequeued.
757 for (int j = 0; j < numTestPixels; j++) {
758 int x = mTestPixels[j].x;
759 int y = mTestPixels[j].y;
760 uint8_t value = 128;
761 img[y*stride + x] = value;
762 }
763
764 // Fill the buffer with gray.
765 for (int y = 0; y < texHeight; y++) {
766 for (int x = 0; x < texWidth; x++) {
767 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
768 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
769 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
770 }
771 }
772
773 // Set the test pixels to either white or black.
774 for (int j = 0; j < numTestPixels; j++) {
775 int x = mTestPixels[j].x;
776 int y = mTestPixels[j].y;
777 uint8_t value = 0;
778 if (j == (i % numTestPixels)) {
779 value = 255;
780 }
781 img[y*stride + x] = value;
782 }
783
784 buf->unlock();
785 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())
786 != NO_ERROR) {
787 return false;
788 }
789 }
790 return false;
791 }
792
793 sp<ANativeWindow> mANW;
794 const TestPixel* mTestPixels;
795 };
796
797 sp<FrameWaiter> fw(new FrameWaiter);
798 mST->setFrameAvailableListener(fw);
799
800 sp<Thread> pt(new ProducerThread(mANW, testPixels));
801 pt->run();
802
803 glViewport(0, 0, texWidth, texHeight);
804
805 glClearColor(0.2, 0.2, 0.2, 0.2);
806 glClear(GL_COLOR_BUFFER_BIT);
807
808 // We wait for the first two frames up front so that the producer will be
809 // likely to dequeue the buffer that's currently being textured from.
810 fw->waitForFrame();
811 fw->waitForFrame();
812
813 for (int i = 0; i < numFrames; i++) {
814 SCOPED_TRACE(String8::format("frame %d", i).string());
815
816 // We must wait for each frame to come in because if we ever do an
817 // updateTexImage call that doesn't consume a newly available buffer
818 // then the producer and consumer will get out of sync, which will cause
819 // a deadlock.
820 if (i > 1) {
821 fw->waitForFrame();
822 }
823 mST->updateTexImage();
824 drawTexture();
825
826 for (int j = 0; j < numTestPixels; j++) {
827 int x = testPixels[j].x;
828 int y = testPixels[j].y;
829 uint8_t value = 0;
830 if (j == (i % numTestPixels)) {
831 // We must y-invert the texture coords
832 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
833 } else {
834 // We must y-invert the texture coords
835 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
836 }
837 }
838 }
839
840 pt->requestExitAndWait();
841}
842
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700843TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700844 const int texWidth = 64;
845 const int texHeight = 66;
846
847 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
848 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
849 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
850 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
851
852 android_native_buffer_t* anb;
853 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
854 ASSERT_TRUE(anb != NULL);
855
856 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
857 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
858
859 // Fill the buffer with the a checkerboard pattern
860 uint8_t* img = NULL;
861 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
862 fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride());
863 buf->unlock();
864 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
865
866 mST->updateTexImage();
867
868 glClearColor(0.2, 0.2, 0.2, 0.2);
869 glClear(GL_COLOR_BUFFER_BIT);
870
Jamie Gennisc8c51522011-06-15 14:24:38 -0700871 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -0700872 drawTexture();
873
874 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
875 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700876 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
877 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700878
879 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700880 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700881 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700882 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
883 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700884 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -0700885 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700886 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
887 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
888 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700889 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700890 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
891 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -0700892 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700893 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700894 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
895}
896
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700897TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700898 const int texWidth = 64;
899 const int texHeight = 64;
900
901 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
902 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
903 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
904 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
905
906 android_native_buffer_t* anb;
907 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
908 ASSERT_TRUE(anb != NULL);
909
910 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
911 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
912
913 // Fill the buffer with the a checkerboard pattern
914 uint8_t* img = NULL;
915 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
916 fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride());
917 buf->unlock();
918 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
919
920 mST->updateTexImage();
921
922 glClearColor(0.2, 0.2, 0.2, 0.2);
923 glClear(GL_COLOR_BUFFER_BIT);
924
Jamie Gennisc8c51522011-06-15 14:24:38 -0700925 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -0700926 drawTexture();
927
928 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
929 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
930 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
931 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
932
933 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
934 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
935 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
936 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
937 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
938 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
939 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
940 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
941 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
942 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
943 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
944 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
945 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
946 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
947 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
948 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
949}
950
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700951TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
952 class ProducerThread : public Thread {
953 public:
954 ProducerThread(const sp<ANativeWindow>& anw):
955 mANW(anw),
956 mDequeueError(NO_ERROR) {
957 }
958
959 virtual ~ProducerThread() {
960 }
961
962 virtual bool threadLoop() {
963 Mutex::Autolock lock(mMutex);
964 ANativeWindowBuffer* anb;
965
966 // Frame 1
967 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
968 return false;
969 }
970 if (anb == NULL) {
971 return false;
972 }
973 if (mANW->queueBuffer(mANW.get(), anb)
974 != NO_ERROR) {
975 return false;
976 }
977
978 // Frame 2
979 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
980 return false;
981 }
982 if (anb == NULL) {
983 return false;
984 }
985 if (mANW->queueBuffer(mANW.get(), anb)
986 != NO_ERROR) {
987 return false;
988 }
989
990 // Frame 3 - error expected
991 mDequeueError = mANW->dequeueBuffer(mANW.get(), &anb);
992 return false;
993 }
994
995 status_t getDequeueError() {
996 Mutex::Autolock lock(mMutex);
997 return mDequeueError;
998 }
999
1000 private:
1001 sp<ANativeWindow> mANW;
1002 status_t mDequeueError;
1003 Mutex mMutex;
1004 };
1005
1006 sp<FrameWaiter> fw(new FrameWaiter);
1007 mST->setFrameAvailableListener(fw);
1008 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1009 ASSERT_EQ(OK, mST->setBufferCountServer(2));
1010
1011 sp<Thread> pt(new ProducerThread(mANW));
1012 pt->run();
1013
1014 fw->waitForFrame();
1015 fw->waitForFrame();
1016
1017 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1018 // block waiting for a buffer to become available.
1019 usleep(100000);
1020
1021 mST->abandon();
1022
1023 pt->requestExitAndWait();
1024 ASSERT_EQ(NO_INIT,
1025 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1026}
1027
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001028TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1029 int texHeight = 16;
1030 ANativeWindowBuffer* anb;
1031
1032 GLint maxTextureSize;
1033 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1034
1035 // make sure it works with small textures
1036 mST->setDefaultBufferSize(16, texHeight);
1037 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1038 EXPECT_EQ(16, anb->width);
1039 EXPECT_EQ(texHeight, anb->height);
1040 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1041 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1042
1043 // make sure it works with GL_MAX_TEXTURE_SIZE
1044 mST->setDefaultBufferSize(maxTextureSize, texHeight);
1045 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1046 EXPECT_EQ(maxTextureSize, anb->width);
1047 EXPECT_EQ(texHeight, anb->height);
1048 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1049 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1050
1051 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1052 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
1053 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1054 EXPECT_EQ(maxTextureSize+1, anb->width);
1055 EXPECT_EQ(texHeight, anb->height);
1056 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1057 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1058}
1059
Jamie Gennis5451d152011-06-08 09:40:45 -07001060/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001061 * This test fixture is for testing GL -> GL texture streaming. It creates an
1062 * EGLSurface and an EGLContext for the image producer to use.
1063 */
1064class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1065protected:
1066 SurfaceTextureGLToGLTest():
1067 mProducerEglSurface(EGL_NO_SURFACE),
1068 mProducerEglContext(EGL_NO_CONTEXT) {
1069 }
1070
1071 virtual void SetUp() {
1072 SurfaceTextureGLTest::SetUp();
1073
1074 EGLConfig myConfig = {0};
1075 EGLint numConfigs = 0;
1076 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &myConfig,
1077 1, &numConfigs));
1078 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1079
1080 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, myConfig,
1081 mANW.get(), NULL);
1082 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1083 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1084
1085 mProducerEglContext = eglCreateContext(mEglDisplay, myConfig,
1086 EGL_NO_CONTEXT, getContextAttribs());
1087 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1088 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1089 }
1090
1091 virtual void TearDown() {
1092 if (mProducerEglContext != EGL_NO_CONTEXT) {
1093 eglDestroyContext(mEglDisplay, mProducerEglContext);
1094 }
1095 if (mProducerEglSurface != EGL_NO_SURFACE) {
1096 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1097 }
1098 SurfaceTextureGLTest::TearDown();
1099 }
1100
1101 EGLSurface mProducerEglSurface;
1102 EGLContext mProducerEglContext;
1103};
1104
1105TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1106 const int texWidth = 64;
1107 const int texHeight = 64;
1108
1109 mST->setDefaultBufferSize(texWidth, texHeight);
1110
1111 // Do the producer side of things
1112 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1113 mProducerEglSurface, mProducerEglContext));
1114 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1115
1116 // This is needed to ensure we pick up a buffer of the correct size.
1117 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1118
1119 glClearColor(0.6, 0.6, 0.6, 0.6);
1120 glClear(GL_COLOR_BUFFER_BIT);
1121
1122 glEnable(GL_SCISSOR_TEST);
1123 glScissor(4, 4, 4, 4);
1124 glClearColor(1.0, 0.0, 0.0, 1.0);
1125 glClear(GL_COLOR_BUFFER_BIT);
1126
1127 glScissor(24, 48, 4, 4);
1128 glClearColor(0.0, 1.0, 0.0, 1.0);
1129 glClear(GL_COLOR_BUFFER_BIT);
1130
1131 glScissor(37, 17, 4, 4);
1132 glClearColor(0.0, 0.0, 1.0, 1.0);
1133 glClear(GL_COLOR_BUFFER_BIT);
1134
1135 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1136
1137 // Do the consumer side of things
1138 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1139 mEglContext));
1140 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1141
1142 glDisable(GL_SCISSOR_TEST);
1143
1144 mST->updateTexImage(); // Skip the first frame, which was empty
1145 mST->updateTexImage();
1146
1147 glClearColor(0.2, 0.2, 0.2, 0.2);
1148 glClear(GL_COLOR_BUFFER_BIT);
1149
1150 glViewport(0, 0, texWidth, texHeight);
1151 drawTexture();
1152
1153 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1154 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1155 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1156 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1157
1158 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1159 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1160 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1161 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1162 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1163 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1164 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1165 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1166 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1167 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1168 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1169 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1170 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1171 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1172 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1173 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1174}
1175
1176TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
1177 sp<GraphicBuffer> buffers[3];
1178
1179 for (int i = 0; i < 3; i++) {
1180 // Produce a frame
1181 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1182 mProducerEglSurface, mProducerEglContext));
1183 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1184 glClear(GL_COLOR_BUFFER_BIT);
1185 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1186
1187 // Consume a frame
1188 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1189 mEglContext));
1190 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1191 mST->updateTexImage();
1192 buffers[i] = mST->getCurrentBuffer();
1193 }
1194
1195 // Destroy the GL texture object to release its ref on buffers[2].
1196 GLuint texID = TEX_ID;
1197 glDeleteTextures(1, &texID);
1198
1199 // Destroy the EGLSurface
1200 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1201 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1202
1203 // Release the ref that the SurfaceTexture has on buffers[2].
1204 mST->abandon();
1205
1206 EXPECT_EQ(1, buffers[0]->getStrongCount());
1207 EXPECT_EQ(1, buffers[1]->getStrongCount());
1208 EXPECT_EQ(1, buffers[2]->getStrongCount());
1209}
1210
1211TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1212 sp<GraphicBuffer> buffers[3];
1213
1214 for (int i = 0; i < 3; i++) {
1215 // Produce a frame
1216 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1217 mProducerEglSurface, mProducerEglContext));
1218 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1219 glClear(GL_COLOR_BUFFER_BIT);
1220 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1221 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1222
1223 // Consume a frame
1224 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1225 mEglContext));
1226 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1227 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1228 buffers[i] = mST->getCurrentBuffer();
1229 }
1230
1231 // Abandon the SurfaceTexture, releasing the ref that the SurfaceTexture has
1232 // on buffers[2].
1233 mST->abandon();
1234
1235 // Destroy the GL texture object to release its ref on buffers[2].
1236 GLuint texID = TEX_ID;
1237 glDeleteTextures(1, &texID);
1238
1239 // Destroy the EGLSurface.
1240 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1241 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1242
1243 EXPECT_EQ(1, buffers[0]->getStrongCount());
1244 EXPECT_EQ(1, buffers[1]->getStrongCount());
1245 EXPECT_EQ(1, buffers[2]->getStrongCount());
1246}
1247
Jamie Gennis59769462011-11-19 18:04:43 -08001248TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) {
1249 // This test requires 3 buffers to run on a single thread.
1250 mST->setBufferCountServer(3);
1251
1252 ASSERT_TRUE(mST->isSynchronousMode());
1253
1254 for (int i = 0; i < 10; i++) {
1255 // Produce a frame
1256 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1257 mProducerEglSurface, mProducerEglContext));
1258 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1259 glClear(GL_COLOR_BUFFER_BIT);
1260 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1261 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1262
1263 // Consume a frame
1264 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1265 mEglContext));
1266 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1267 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1268 }
1269
1270 ASSERT_TRUE(mST->isSynchronousMode());
1271}
1272
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001273/*
1274 * This test fixture is for testing GL -> GL texture streaming from one thread
1275 * to another. It contains functionality to create a producer thread that will
1276 * perform GL rendering to an ANativeWindow that feeds frames to a
1277 * SurfaceTexture. Additionally it supports interlocking the producer and
1278 * consumer threads so that a specific sequence of calls can be
1279 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07001280 *
1281 * The intended usage is as follows:
1282 *
1283 * TEST_F(...) {
1284 * class PT : public ProducerThread {
1285 * virtual void render() {
1286 * ...
1287 * swapBuffers();
1288 * }
1289 * };
1290 *
1291 * runProducerThread(new PT());
1292 *
1293 * // The order of these calls will vary from test to test and may include
1294 * // multiple frames and additional operations (e.g. GL rendering from the
1295 * // texture).
1296 * fc->waitForFrame();
1297 * mST->updateTexImage();
1298 * fc->finishFrame();
1299 * }
1300 *
1301 */
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001302class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07001303protected:
1304
1305 // ProducerThread is an abstract base class to simplify the creation of
1306 // OpenGL ES frame producer threads.
1307 class ProducerThread : public Thread {
1308 public:
1309 virtual ~ProducerThread() {
1310 }
1311
1312 void setEglObjects(EGLDisplay producerEglDisplay,
1313 EGLSurface producerEglSurface,
1314 EGLContext producerEglContext) {
1315 mProducerEglDisplay = producerEglDisplay;
1316 mProducerEglSurface = producerEglSurface;
1317 mProducerEglContext = producerEglContext;
1318 }
1319
1320 virtual bool threadLoop() {
1321 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
1322 mProducerEglSurface, mProducerEglContext);
1323 render();
1324 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1325 EGL_NO_CONTEXT);
1326 return false;
1327 }
1328
1329 protected:
1330 virtual void render() = 0;
1331
1332 void swapBuffers() {
1333 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
1334 }
1335
1336 EGLDisplay mProducerEglDisplay;
1337 EGLSurface mProducerEglSurface;
1338 EGLContext mProducerEglContext;
1339 };
1340
1341 // FrameCondition is a utility class for interlocking between the producer
1342 // and consumer threads. The FrameCondition object should be created and
1343 // destroyed in the consumer thread only. The consumer thread should set
1344 // the FrameCondition as the FrameAvailableListener of the SurfaceTexture,
1345 // and should call both waitForFrame and finishFrame once for each expected
1346 // frame.
1347 //
1348 // This interlocking relies on the fact that onFrameAvailable gets called
1349 // synchronously from SurfaceTexture::queueBuffer.
1350 class FrameCondition : public SurfaceTexture::FrameAvailableListener {
1351 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001352 FrameCondition():
1353 mFrameAvailable(false),
1354 mFrameFinished(false) {
1355 }
1356
Jamie Gennis5451d152011-06-08 09:40:45 -07001357 // waitForFrame waits for the next frame to arrive. This should be
1358 // called from the consumer thread once for every frame expected by the
1359 // test.
1360 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001361 Mutex::Autolock lock(mMutex);
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001362 LOGV("+waitForFrame");
1363 while (!mFrameAvailable) {
1364 mFrameAvailableCondition.wait(mMutex);
1365 }
1366 mFrameAvailable = false;
Jamie Gennis5451d152011-06-08 09:40:45 -07001367 LOGV("-waitForFrame");
1368 }
1369
1370 // Allow the producer to return from its swapBuffers call and continue
1371 // on to produce the next frame. This should be called by the consumer
1372 // thread once for every frame expected by the test.
1373 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001374 Mutex::Autolock lock(mMutex);
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001375 LOGV("+finishFrame");
1376 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001377 mFrameFinishCondition.signal();
1378 LOGV("-finishFrame");
1379 }
1380
1381 // This should be called by SurfaceTexture on the producer thread.
1382 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001383 Mutex::Autolock lock(mMutex);
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001384 LOGV("+onFrameAvailable");
1385 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001386 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001387 while (!mFrameFinished) {
1388 mFrameFinishCondition.wait(mMutex);
1389 }
1390 mFrameFinished = false;
Jamie Gennis5451d152011-06-08 09:40:45 -07001391 LOGV("-onFrameAvailable");
1392 }
1393
1394 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001395 bool mFrameAvailable;
1396 bool mFrameFinished;
1397
Jamie Gennis5451d152011-06-08 09:40:45 -07001398 Mutex mMutex;
1399 Condition mFrameAvailableCondition;
1400 Condition mFrameFinishCondition;
1401 };
1402
Jamie Gennis5451d152011-06-08 09:40:45 -07001403 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001404 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07001405 mFC = new FrameCondition();
1406 mST->setFrameAvailableListener(mFC);
1407 }
1408
1409 virtual void TearDown() {
1410 if (mProducerThread != NULL) {
1411 mProducerThread->requestExitAndWait();
1412 }
Jamie Gennis5451d152011-06-08 09:40:45 -07001413 mProducerThread.clear();
1414 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001415 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07001416 }
1417
1418 void runProducerThread(const sp<ProducerThread> producerThread) {
1419 ASSERT_TRUE(mProducerThread == NULL);
1420 mProducerThread = producerThread;
1421 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
1422 mProducerEglContext);
1423 producerThread->run();
1424 }
1425
Jamie Gennis5451d152011-06-08 09:40:45 -07001426 sp<ProducerThread> mProducerThread;
1427 sp<FrameCondition> mFC;
1428};
1429
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001430TEST_F(SurfaceTextureGLThreadToGLTest,
1431 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001432 class PT : public ProducerThread {
1433 virtual void render() {
1434 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1435 glClear(GL_COLOR_BUFFER_BIT);
1436 swapBuffers();
1437 }
1438 };
1439
1440 runProducerThread(new PT());
1441
1442 mFC->waitForFrame();
1443 mST->updateTexImage();
1444 mFC->finishFrame();
1445
1446 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08001447}
Jamie Gennis5451d152011-06-08 09:40:45 -07001448
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001449TEST_F(SurfaceTextureGLThreadToGLTest,
1450 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001451 class PT : public ProducerThread {
1452 virtual void render() {
1453 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1454 glClear(GL_COLOR_BUFFER_BIT);
1455 swapBuffers();
1456 }
1457 };
1458
1459 runProducerThread(new PT());
1460
1461 mFC->waitForFrame();
1462 mFC->finishFrame();
1463 mST->updateTexImage();
1464
1465 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1466}
1467
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001468TEST_F(SurfaceTextureGLThreadToGLTest,
1469 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001470 enum { NUM_ITERATIONS = 1024 };
1471
1472 class PT : public ProducerThread {
1473 virtual void render() {
1474 for (int i = 0; i < NUM_ITERATIONS; i++) {
1475 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1476 glClear(GL_COLOR_BUFFER_BIT);
1477 LOGV("+swapBuffers");
1478 swapBuffers();
1479 LOGV("-swapBuffers");
1480 }
1481 }
1482 };
1483
1484 runProducerThread(new PT());
1485
1486 for (int i = 0; i < NUM_ITERATIONS; i++) {
1487 mFC->waitForFrame();
1488 LOGV("+updateTexImage");
1489 mST->updateTexImage();
1490 LOGV("-updateTexImage");
1491 mFC->finishFrame();
1492
1493 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1494 }
1495}
1496
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001497TEST_F(SurfaceTextureGLThreadToGLTest,
1498 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001499 enum { NUM_ITERATIONS = 1024 };
1500
1501 class PT : public ProducerThread {
1502 virtual void render() {
1503 for (int i = 0; i < NUM_ITERATIONS; i++) {
1504 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1505 glClear(GL_COLOR_BUFFER_BIT);
1506 LOGV("+swapBuffers");
1507 swapBuffers();
1508 LOGV("-swapBuffers");
1509 }
1510 }
1511 };
1512
1513 runProducerThread(new PT());
1514
1515 for (int i = 0; i < NUM_ITERATIONS; i++) {
1516 mFC->waitForFrame();
1517 mFC->finishFrame();
1518 LOGV("+updateTexImage");
1519 mST->updateTexImage();
1520 LOGV("-updateTexImage");
1521
1522 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1523 }
1524}
1525
Jamie Gennis6e502192011-07-21 14:31:31 -07001526// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001527TEST_F(SurfaceTextureGLThreadToGLTest,
1528 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07001529 enum { NUM_ITERATIONS = 64 };
1530
1531 class PT : public ProducerThread {
1532 virtual void render() {
1533 for (int i = 0; i < NUM_ITERATIONS; i++) {
1534 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1535 glClear(GL_COLOR_BUFFER_BIT);
1536 LOGV("+swapBuffers");
1537 swapBuffers();
1538 LOGV("-swapBuffers");
1539 }
1540 }
1541 };
1542
1543 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1544 ASSERT_EQ(OK, mST->setBufferCountServer(2));
1545
1546 runProducerThread(new PT());
1547
1548 // Allow three frames to be rendered and queued before starting the
1549 // rendering in this thread. For the latter two frames we don't call
1550 // updateTexImage so the next dequeue from the producer thread will block
1551 // waiting for a frame to become available.
1552 mFC->waitForFrame();
1553 mFC->finishFrame();
1554
1555 // We must call updateTexImage to consume the first frame so that the
1556 // SurfaceTexture is able to reduce the buffer count to 2. This is because
1557 // the GL driver may dequeue a buffer when the EGLSurface is created, and
1558 // that happens before we call setBufferCountServer. It's possible that the
1559 // driver does not dequeue a buffer at EGLSurface creation time, so we
1560 // cannot rely on this to cause the second dequeueBuffer call to block.
1561 mST->updateTexImage();
1562
1563 mFC->waitForFrame();
1564 mFC->finishFrame();
1565 mFC->waitForFrame();
1566 mFC->finishFrame();
1567
1568 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1569 // block waiting for a buffer to become available.
1570 usleep(100000);
1571
1572 // Render and present a number of images. This thread should not be blocked
1573 // by the fact that the producer thread is blocking in dequeue.
1574 for (int i = 0; i < NUM_ITERATIONS; i++) {
1575 glClear(GL_COLOR_BUFFER_BIT);
1576 eglSwapBuffers(mEglDisplay, mEglSurface);
1577 }
1578
1579 // Consume the two pending buffers to unblock the producer thread.
1580 mST->updateTexImage();
1581 mST->updateTexImage();
1582
1583 // Consume the remaining buffers from the producer thread.
1584 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
1585 mFC->waitForFrame();
1586 mFC->finishFrame();
1587 LOGV("+updateTexImage");
1588 mST->updateTexImage();
1589 LOGV("-updateTexImage");
1590 }
1591}
1592
Jamie Gennis5451d152011-06-08 09:40:45 -07001593} // namespace android