blob: 0d0654f2aeec4b77aed515337b3d94ef05aab036 [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
399 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, triangleVertices);
400 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
401 glEnableVertexAttribArray(mPositionHandle);
402 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
403
404 glUseProgram(mPgm);
405 glUniform1i(mTexSamplerHandle, 0);
406 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
407 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
408 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
409
Jamie Gennis1876d132011-03-17 16:32:52 -0700410 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
411 // they're setting the defautls for that target, but when hacking things
412 // to use GL_TEXTURE_2D they are needed to achieve the same behavior.
413 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
414 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
415 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
416 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
417 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
418 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
419 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
420 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
421
Jamie Gennisd99c0882011-03-10 16:24:46 -0800422 GLfloat texMatrix[16];
423 mST->getTransformMatrix(texMatrix);
424 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
425
426 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
427 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
428 }
429
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700430 class FrameWaiter : public SurfaceTexture::FrameAvailableListener {
431 public:
432 FrameWaiter():
433 mPendingFrames(0) {
434 }
435
436 void waitForFrame() {
437 Mutex::Autolock lock(mMutex);
438 while (mPendingFrames == 0) {
439 mCondition.wait(mMutex);
440 }
441 mPendingFrames--;
442 }
443
444 virtual void onFrameAvailable() {
445 Mutex::Autolock lock(mMutex);
446 mPendingFrames++;
447 mCondition.signal();
448 }
449
450 int mPendingFrames;
451 Mutex mMutex;
452 Condition mCondition;
453 };
454
Jamie Gennisd99c0882011-03-10 16:24:46 -0800455 sp<SurfaceTexture> mST;
456 sp<SurfaceTextureClient> mSTC;
457 sp<ANativeWindow> mANW;
458
459 GLuint mPgm;
460 GLint mPositionHandle;
461 GLint mTexSamplerHandle;
462 GLint mTexMatrixHandle;
463};
464
465// Fill a YV12 buffer with a multi-colored checkerboard pattern
466void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
467 const int blockWidth = w > 16 ? w / 16 : 1;
468 const int blockHeight = h > 16 ? h / 16 : 1;
469 const int yuvTexOffsetY = 0;
470 int yuvTexStrideY = stride;
471 int yuvTexOffsetV = yuvTexStrideY * h;
472 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
473 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
474 int yuvTexStrideU = yuvTexStrideV;
475 for (int x = 0; x < w; x++) {
476 for (int y = 0; y < h; y++) {
477 int parityX = (x / blockWidth) & 1;
478 int parityY = (y / blockHeight) & 1;
479 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
480 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
481 if (x < w / 2 && y < h / 2) {
482 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
483 if (x * 2 < w / 2 && y * 2 < h / 2) {
484 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
485 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
486 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
487 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
488 intensity;
489 }
490 }
491 }
492 }
493}
494
495// Fill a YV12 buffer with red outside a given rectangle and green inside it.
496void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
497 const android_native_rect_t& rect) {
498 const int yuvTexOffsetY = 0;
499 int yuvTexStrideY = stride;
500 int yuvTexOffsetV = yuvTexStrideY * h;
501 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
502 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
503 int yuvTexStrideU = yuvTexStrideV;
504 for (int x = 0; x < w; x++) {
505 for (int y = 0; y < h; y++) {
506 bool inside = rect.left <= x && x < rect.right &&
507 rect.top <= y && y < rect.bottom;
508 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
509 if (x < w / 2 && y < h / 2) {
510 bool inside = rect.left <= 2*x && 2*x < rect.right &&
511 rect.top <= 2*y && 2*y < rect.bottom;
512 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
513 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
514 inside ? 16 : 255;
515 }
516 }
517 }
518}
519
Jamie Gennis1876d132011-03-17 16:32:52 -0700520void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
521 const size_t PIXEL_SIZE = 4;
522 for (int x = 0; x < w; x++) {
523 for (int y = 0; y < h; y++) {
524 off_t offset = (y * stride + x) * PIXEL_SIZE;
525 for (int c = 0; c < 4; c++) {
526 int parityX = (x / (1 << (c+2))) & 1;
527 int parityY = (y / (1 << (c+2))) & 1;
528 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
529 }
530 }
531 }
532}
533
Jamie Gennisd99c0882011-03-10 16:24:46 -0800534TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700535 const int texWidth = 64;
536 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800537
538 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700539 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800540 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
541 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
542
Iliyan Malchev697526b2011-05-01 11:33:26 -0700543 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800544 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
545 ASSERT_TRUE(anb != NULL);
546
547 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
548 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
549
550 // Fill the buffer with the a checkerboard pattern
551 uint8_t* img = NULL;
552 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700553 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800554 buf->unlock();
555 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
556
557 mST->updateTexImage();
558
559 glClearColor(0.2, 0.2, 0.2, 0.2);
560 glClear(GL_COLOR_BUFFER_BIT);
561
Jamie Gennisc8c51522011-06-15 14:24:38 -0700562 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800563 drawTexture();
564
565 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255));
566 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700567 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255));
568 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800569
Jamie Gennisc8c51522011-06-15 14:24:38 -0700570 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255));
571 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255));
572 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800573 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700574 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800575 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255));
576 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255));
577}
578
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700579TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700580 const int texWidth = 64;
581 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800582
583 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700584 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800585 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
586 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
587
Iliyan Malchev697526b2011-05-01 11:33:26 -0700588 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800589 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
590 ASSERT_TRUE(anb != NULL);
591
592 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
593 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
594
595 // Fill the buffer with the a checkerboard pattern
596 uint8_t* img = NULL;
597 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700598 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800599 buf->unlock();
600 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
601
602 mST->updateTexImage();
603
604 glClearColor(0.2, 0.2, 0.2, 0.2);
605 glClear(GL_COLOR_BUFFER_BIT);
606
Jamie Gennisc8c51522011-06-15 14:24:38 -0700607 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800608 drawTexture();
609
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700610 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
611 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800612 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
613 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
614
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700615 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
616 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
617 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
618 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
619 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
620 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
621 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800622}
623
624TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700625 const int texWidth = 64;
626 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800627
628 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700629 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800630 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
631 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
632
633 android_native_rect_t crops[] = {
634 {4, 6, 22, 36},
635 {0, 6, 22, 36},
636 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700637 {4, 6, texWidth, 36},
638 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800639 };
640
641 for (int i = 0; i < 5; i++) {
642 const android_native_rect_t& crop(crops[i]);
643 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }", crop.left,
644 crop.top, crop.right, crop.bottom).string());
645
646 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
647
Iliyan Malchev697526b2011-05-01 11:33:26 -0700648 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800649 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
650 ASSERT_TRUE(anb != NULL);
651
652 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
653 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
654
655 uint8_t* img = NULL;
656 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700657 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800658 buf->unlock();
659 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
660
661 mST->updateTexImage();
662
663 glClearColor(0.2, 0.2, 0.2, 0.2);
664 glClear(GL_COLOR_BUFFER_BIT);
665
Jamie Gennisc8c51522011-06-15 14:24:38 -0700666 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800667 drawTexture();
668
669 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
670 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
671 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
672 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
673
674 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
675 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
676 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
677 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
678 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
679 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
680 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
681 }
682}
683
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700684// This test is intended to catch synchronization bugs between the CPU-written
685// and GPU-read buffers.
686TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
687 enum { texWidth = 16 };
688 enum { texHeight = 16 };
689 enum { numFrames = 1024 };
690
691 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
692 ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2));
693 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
694 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
695 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
696 GRALLOC_USAGE_SW_WRITE_OFTEN));
697
698 struct TestPixel {
699 int x;
700 int y;
701 };
702 const TestPixel testPixels[] = {
703 { 4, 11 },
704 { 12, 14 },
705 { 7, 2 },
706 };
707 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
708
709 class ProducerThread : public Thread {
710 public:
711 ProducerThread(const sp<ANativeWindow>& anw, const TestPixel* testPixels):
712 mANW(anw),
713 mTestPixels(testPixels) {
714 }
715
716 virtual ~ProducerThread() {
717 }
718
719 virtual bool threadLoop() {
720 for (int i = 0; i < numFrames; i++) {
721 ANativeWindowBuffer* anb;
722 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
723 return false;
724 }
725 if (anb == NULL) {
726 return false;
727 }
728
729 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
730 if (mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())
731 != NO_ERROR) {
732 return false;
733 }
734
735 const int yuvTexOffsetY = 0;
736 int stride = buf->getStride();
737 int yuvTexStrideY = stride;
738 int yuvTexOffsetV = yuvTexStrideY * texHeight;
739 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
740 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
741 int yuvTexStrideU = yuvTexStrideV;
742
743 uint8_t* img = NULL;
744 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
745
746 // Gray out all the test pixels first, so we're more likely to
747 // see a failure if GL is still texturing from the buffer we
748 // just dequeued.
749 for (int j = 0; j < numTestPixels; j++) {
750 int x = mTestPixels[j].x;
751 int y = mTestPixels[j].y;
752 uint8_t value = 128;
753 img[y*stride + x] = value;
754 }
755
756 // Fill the buffer with gray.
757 for (int y = 0; y < texHeight; y++) {
758 for (int x = 0; x < texWidth; x++) {
759 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
760 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
761 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
762 }
763 }
764
765 // Set the test pixels to either white or black.
766 for (int j = 0; j < numTestPixels; j++) {
767 int x = mTestPixels[j].x;
768 int y = mTestPixels[j].y;
769 uint8_t value = 0;
770 if (j == (i % numTestPixels)) {
771 value = 255;
772 }
773 img[y*stride + x] = value;
774 }
775
776 buf->unlock();
777 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())
778 != NO_ERROR) {
779 return false;
780 }
781 }
782 return false;
783 }
784
785 sp<ANativeWindow> mANW;
786 const TestPixel* mTestPixels;
787 };
788
789 sp<FrameWaiter> fw(new FrameWaiter);
790 mST->setFrameAvailableListener(fw);
791
792 sp<Thread> pt(new ProducerThread(mANW, testPixels));
793 pt->run();
794
795 glViewport(0, 0, texWidth, texHeight);
796
797 glClearColor(0.2, 0.2, 0.2, 0.2);
798 glClear(GL_COLOR_BUFFER_BIT);
799
800 // We wait for the first two frames up front so that the producer will be
801 // likely to dequeue the buffer that's currently being textured from.
802 fw->waitForFrame();
803 fw->waitForFrame();
804
805 for (int i = 0; i < numFrames; i++) {
806 SCOPED_TRACE(String8::format("frame %d", i).string());
807
808 // We must wait for each frame to come in because if we ever do an
809 // updateTexImage call that doesn't consume a newly available buffer
810 // then the producer and consumer will get out of sync, which will cause
811 // a deadlock.
812 if (i > 1) {
813 fw->waitForFrame();
814 }
815 mST->updateTexImage();
816 drawTexture();
817
818 for (int j = 0; j < numTestPixels; j++) {
819 int x = testPixels[j].x;
820 int y = testPixels[j].y;
821 uint8_t value = 0;
822 if (j == (i % numTestPixels)) {
823 // We must y-invert the texture coords
824 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
825 } else {
826 // We must y-invert the texture coords
827 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
828 }
829 }
830 }
831
832 pt->requestExitAndWait();
833}
834
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700835TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700836 const int texWidth = 64;
837 const int texHeight = 66;
838
839 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
840 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
841 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
842 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
843
844 android_native_buffer_t* anb;
845 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
846 ASSERT_TRUE(anb != NULL);
847
848 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
849 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
850
851 // Fill the buffer with the a checkerboard pattern
852 uint8_t* img = NULL;
853 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
854 fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride());
855 buf->unlock();
856 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
857
858 mST->updateTexImage();
859
860 glClearColor(0.2, 0.2, 0.2, 0.2);
861 glClear(GL_COLOR_BUFFER_BIT);
862
Jamie Gennisc8c51522011-06-15 14:24:38 -0700863 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -0700864 drawTexture();
865
866 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
867 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700868 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
869 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700870
871 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700872 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700873 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700874 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
875 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700876 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -0700877 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700878 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
879 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
880 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700881 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700882 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
883 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -0700884 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700885 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700886 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
887}
888
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700889TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700890 const int texWidth = 64;
891 const int texHeight = 64;
892
893 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
894 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
895 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
896 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
897
898 android_native_buffer_t* anb;
899 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
900 ASSERT_TRUE(anb != NULL);
901
902 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
903 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
904
905 // Fill the buffer with the a checkerboard pattern
906 uint8_t* img = NULL;
907 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
908 fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride());
909 buf->unlock();
910 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
911
912 mST->updateTexImage();
913
914 glClearColor(0.2, 0.2, 0.2, 0.2);
915 glClear(GL_COLOR_BUFFER_BIT);
916
Jamie Gennisc8c51522011-06-15 14:24:38 -0700917 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -0700918 drawTexture();
919
920 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
921 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
922 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
923 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
924
925 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
926 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
927 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
928 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
929 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
930 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
931 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
932 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
933 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
934 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
935 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
936 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
937 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
938 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
939 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
940 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
941}
942
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700943TEST_F(SurfaceTextureGLTest, TexturingFromGLFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700944 const int texWidth = 64;
945 const int texHeight = 64;
946
947 mST->setDefaultBufferSize(texWidth, texHeight);
948
949 // Do the producer side of things
950 EGLSurface stcEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
951 mANW.get(), NULL);
952 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700953 ASSERT_NE(EGL_NO_SURFACE, stcEglSurface);
Jamie Gennis1876d132011-03-17 16:32:52 -0700954
955 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, stcEglSurface, stcEglSurface,
956 mEglContext));
957 ASSERT_EQ(EGL_SUCCESS, eglGetError());
958
959 glClearColor(0.6, 0.6, 0.6, 0.6);
960 glClear(GL_COLOR_BUFFER_BIT);
961
962 glEnable(GL_SCISSOR_TEST);
963 glScissor(4, 4, 4, 4);
964 glClearColor(1.0, 0.0, 0.0, 1.0);
965 glClear(GL_COLOR_BUFFER_BIT);
966
967 glScissor(24, 48, 4, 4);
968 glClearColor(0.0, 1.0, 0.0, 1.0);
969 glClear(GL_COLOR_BUFFER_BIT);
970
971 glScissor(37, 17, 4, 4);
972 glClearColor(0.0, 0.0, 1.0, 1.0);
973 glClear(GL_COLOR_BUFFER_BIT);
974
975 eglSwapBuffers(mEglDisplay, stcEglSurface);
976
977 // Do the consumer side of things
978 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
979 mEglContext));
980 ASSERT_EQ(EGL_SUCCESS, eglGetError());
981
982 glDisable(GL_SCISSOR_TEST);
983
984 mST->updateTexImage();
985
Jamie Gennise68a52b2011-08-30 19:04:42 -0700986 // We must wait until updateTexImage has been called to destroy the
987 // EGLSurface because we're in synchronous mode.
988 eglDestroySurface(mEglDisplay, stcEglSurface);
989
Jamie Gennis1876d132011-03-17 16:32:52 -0700990 glClearColor(0.2, 0.2, 0.2, 0.2);
991 glClear(GL_COLOR_BUFFER_BIT);
992
Jamie Gennisc8c51522011-06-15 14:24:38 -0700993 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -0700994 drawTexture();
995
996 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
997 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
998 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
999 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1000
1001 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1002 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1003 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1004 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1005 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1006 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1007 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1008 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1009 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1010 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1011 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1012 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1013 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1014 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1015 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1016 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1017}
1018
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001019TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
1020 class ProducerThread : public Thread {
1021 public:
1022 ProducerThread(const sp<ANativeWindow>& anw):
1023 mANW(anw),
1024 mDequeueError(NO_ERROR) {
1025 }
1026
1027 virtual ~ProducerThread() {
1028 }
1029
1030 virtual bool threadLoop() {
1031 Mutex::Autolock lock(mMutex);
1032 ANativeWindowBuffer* anb;
1033
1034 // Frame 1
1035 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1036 return false;
1037 }
1038 if (anb == NULL) {
1039 return false;
1040 }
1041 if (mANW->queueBuffer(mANW.get(), anb)
1042 != NO_ERROR) {
1043 return false;
1044 }
1045
1046 // Frame 2
1047 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1048 return false;
1049 }
1050 if (anb == NULL) {
1051 return false;
1052 }
1053 if (mANW->queueBuffer(mANW.get(), anb)
1054 != NO_ERROR) {
1055 return false;
1056 }
1057
1058 // Frame 3 - error expected
1059 mDequeueError = mANW->dequeueBuffer(mANW.get(), &anb);
1060 return false;
1061 }
1062
1063 status_t getDequeueError() {
1064 Mutex::Autolock lock(mMutex);
1065 return mDequeueError;
1066 }
1067
1068 private:
1069 sp<ANativeWindow> mANW;
1070 status_t mDequeueError;
1071 Mutex mMutex;
1072 };
1073
1074 sp<FrameWaiter> fw(new FrameWaiter);
1075 mST->setFrameAvailableListener(fw);
1076 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1077 ASSERT_EQ(OK, mST->setBufferCountServer(2));
1078
1079 sp<Thread> pt(new ProducerThread(mANW));
1080 pt->run();
1081
1082 fw->waitForFrame();
1083 fw->waitForFrame();
1084
1085 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1086 // block waiting for a buffer to become available.
1087 usleep(100000);
1088
1089 mST->abandon();
1090
1091 pt->requestExitAndWait();
1092 ASSERT_EQ(NO_INIT,
1093 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1094}
1095
Jamie Gennis5451d152011-06-08 09:40:45 -07001096/*
1097 * This test is for testing GL -> GL texture streaming via SurfaceTexture. It
1098 * contains functionality to create a producer thread that will perform GL
1099 * rendering to an ANativeWindow that feeds frames to a SurfaceTexture.
1100 * Additionally it supports interlocking the producer and consumer threads so
1101 * that a specific sequence of calls can be deterministically created by the
1102 * test.
1103 *
1104 * The intended usage is as follows:
1105 *
1106 * TEST_F(...) {
1107 * class PT : public ProducerThread {
1108 * virtual void render() {
1109 * ...
1110 * swapBuffers();
1111 * }
1112 * };
1113 *
1114 * runProducerThread(new PT());
1115 *
1116 * // The order of these calls will vary from test to test and may include
1117 * // multiple frames and additional operations (e.g. GL rendering from the
1118 * // texture).
1119 * fc->waitForFrame();
1120 * mST->updateTexImage();
1121 * fc->finishFrame();
1122 * }
1123 *
1124 */
1125class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1126protected:
1127
1128 // ProducerThread is an abstract base class to simplify the creation of
1129 // OpenGL ES frame producer threads.
1130 class ProducerThread : public Thread {
1131 public:
1132 virtual ~ProducerThread() {
1133 }
1134
1135 void setEglObjects(EGLDisplay producerEglDisplay,
1136 EGLSurface producerEglSurface,
1137 EGLContext producerEglContext) {
1138 mProducerEglDisplay = producerEglDisplay;
1139 mProducerEglSurface = producerEglSurface;
1140 mProducerEglContext = producerEglContext;
1141 }
1142
1143 virtual bool threadLoop() {
1144 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
1145 mProducerEglSurface, mProducerEglContext);
1146 render();
1147 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1148 EGL_NO_CONTEXT);
1149 return false;
1150 }
1151
1152 protected:
1153 virtual void render() = 0;
1154
1155 void swapBuffers() {
1156 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
1157 }
1158
1159 EGLDisplay mProducerEglDisplay;
1160 EGLSurface mProducerEglSurface;
1161 EGLContext mProducerEglContext;
1162 };
1163
1164 // FrameCondition is a utility class for interlocking between the producer
1165 // and consumer threads. The FrameCondition object should be created and
1166 // destroyed in the consumer thread only. The consumer thread should set
1167 // the FrameCondition as the FrameAvailableListener of the SurfaceTexture,
1168 // and should call both waitForFrame and finishFrame once for each expected
1169 // frame.
1170 //
1171 // This interlocking relies on the fact that onFrameAvailable gets called
1172 // synchronously from SurfaceTexture::queueBuffer.
1173 class FrameCondition : public SurfaceTexture::FrameAvailableListener {
1174 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001175 FrameCondition():
1176 mFrameAvailable(false),
1177 mFrameFinished(false) {
1178 }
1179
Jamie Gennis5451d152011-06-08 09:40:45 -07001180 // waitForFrame waits for the next frame to arrive. This should be
1181 // called from the consumer thread once for every frame expected by the
1182 // test.
1183 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001184 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001185 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001186 while (!mFrameAvailable) {
1187 mFrameAvailableCondition.wait(mMutex);
1188 }
1189 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01001190 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001191 }
1192
1193 // Allow the producer to return from its swapBuffers call and continue
1194 // on to produce the next frame. This should be called by the consumer
1195 // thread once for every frame expected by the test.
1196 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001197 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001198 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001199 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001200 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01001201 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001202 }
1203
1204 // This should be called by SurfaceTexture on the producer thread.
1205 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001206 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001207 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001208 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001209 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001210 while (!mFrameFinished) {
1211 mFrameFinishCondition.wait(mMutex);
1212 }
1213 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01001214 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07001215 }
1216
1217 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001218 bool mFrameAvailable;
1219 bool mFrameFinished;
1220
Jamie Gennis5451d152011-06-08 09:40:45 -07001221 Mutex mMutex;
1222 Condition mFrameAvailableCondition;
1223 Condition mFrameFinishCondition;
1224 };
1225
1226 SurfaceTextureGLToGLTest():
1227 mProducerEglSurface(EGL_NO_SURFACE),
1228 mProducerEglContext(EGL_NO_CONTEXT) {
1229 }
1230
1231 virtual void SetUp() {
1232 SurfaceTextureGLTest::SetUp();
1233
1234 EGLConfig myConfig = {0};
1235 EGLint numConfigs = 0;
1236 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &myConfig,
1237 1, &numConfigs));
1238 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1239
1240 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, myConfig,
1241 mANW.get(), NULL);
1242 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1243 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1244
1245 mProducerEglContext = eglCreateContext(mEglDisplay, myConfig,
1246 EGL_NO_CONTEXT, getContextAttribs());
1247 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1248 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1249
1250 mFC = new FrameCondition();
1251 mST->setFrameAvailableListener(mFC);
1252 }
1253
1254 virtual void TearDown() {
1255 if (mProducerThread != NULL) {
1256 mProducerThread->requestExitAndWait();
1257 }
1258 if (mProducerEglContext != EGL_NO_CONTEXT) {
1259 eglDestroyContext(mEglDisplay, mProducerEglContext);
1260 }
1261 if (mProducerEglSurface != EGL_NO_SURFACE) {
1262 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1263 }
1264 mProducerThread.clear();
1265 mFC.clear();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001266 SurfaceTextureGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07001267 }
1268
1269 void runProducerThread(const sp<ProducerThread> producerThread) {
1270 ASSERT_TRUE(mProducerThread == NULL);
1271 mProducerThread = producerThread;
1272 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
1273 mProducerEglContext);
1274 producerThread->run();
1275 }
1276
1277 EGLSurface mProducerEglSurface;
1278 EGLContext mProducerEglContext;
1279 sp<ProducerThread> mProducerThread;
1280 sp<FrameCondition> mFC;
1281};
1282
Jamie Gennis6e502192011-07-21 14:31:31 -07001283TEST_F(SurfaceTextureGLToGLTest, UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001284 class PT : public ProducerThread {
1285 virtual void render() {
1286 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1287 glClear(GL_COLOR_BUFFER_BIT);
1288 swapBuffers();
1289 }
1290 };
1291
1292 runProducerThread(new PT());
1293
1294 mFC->waitForFrame();
1295 mST->updateTexImage();
1296 mFC->finishFrame();
1297
1298 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08001299}
Jamie Gennis5451d152011-06-08 09:40:45 -07001300
Jamie Gennis6e502192011-07-21 14:31:31 -07001301TEST_F(SurfaceTextureGLToGLTest, UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001302 class PT : public ProducerThread {
1303 virtual void render() {
1304 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1305 glClear(GL_COLOR_BUFFER_BIT);
1306 swapBuffers();
1307 }
1308 };
1309
1310 runProducerThread(new PT());
1311
1312 mFC->waitForFrame();
1313 mFC->finishFrame();
1314 mST->updateTexImage();
1315
1316 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1317}
1318
Jamie Gennis6e502192011-07-21 14:31:31 -07001319TEST_F(SurfaceTextureGLToGLTest, RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001320 enum { NUM_ITERATIONS = 1024 };
1321
1322 class PT : public ProducerThread {
1323 virtual void render() {
1324 for (int i = 0; i < NUM_ITERATIONS; i++) {
1325 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1326 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001327 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001328 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001329 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001330 }
1331 }
1332 };
1333
1334 runProducerThread(new PT());
1335
1336 for (int i = 0; i < NUM_ITERATIONS; i++) {
1337 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01001338 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001339 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01001340 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001341 mFC->finishFrame();
1342
1343 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1344 }
1345}
1346
Jamie Gennis6e502192011-07-21 14:31:31 -07001347TEST_F(SurfaceTextureGLToGLTest, RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001348 enum { NUM_ITERATIONS = 1024 };
1349
1350 class PT : public ProducerThread {
1351 virtual void render() {
1352 for (int i = 0; i < NUM_ITERATIONS; i++) {
1353 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1354 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001355 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001356 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001357 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001358 }
1359 }
1360 };
1361
1362 runProducerThread(new PT());
1363
1364 for (int i = 0; i < NUM_ITERATIONS; i++) {
1365 mFC->waitForFrame();
1366 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01001367 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001368 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01001369 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001370
1371 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1372 }
1373}
1374
Jamie Gennis6e502192011-07-21 14:31:31 -07001375// XXX: This test is disabled because it is currently hanging on some devices.
1376TEST_F(SurfaceTextureGLToGLTest, DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
1377 enum { NUM_ITERATIONS = 64 };
1378
1379 class PT : public ProducerThread {
1380 virtual void render() {
1381 for (int i = 0; i < NUM_ITERATIONS; i++) {
1382 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1383 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001384 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07001385 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001386 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07001387 }
1388 }
1389 };
1390
1391 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1392 ASSERT_EQ(OK, mST->setBufferCountServer(2));
1393
1394 runProducerThread(new PT());
1395
1396 // Allow three frames to be rendered and queued before starting the
1397 // rendering in this thread. For the latter two frames we don't call
1398 // updateTexImage so the next dequeue from the producer thread will block
1399 // waiting for a frame to become available.
1400 mFC->waitForFrame();
1401 mFC->finishFrame();
1402
1403 // We must call updateTexImage to consume the first frame so that the
1404 // SurfaceTexture is able to reduce the buffer count to 2. This is because
1405 // the GL driver may dequeue a buffer when the EGLSurface is created, and
1406 // that happens before we call setBufferCountServer. It's possible that the
1407 // driver does not dequeue a buffer at EGLSurface creation time, so we
1408 // cannot rely on this to cause the second dequeueBuffer call to block.
1409 mST->updateTexImage();
1410
1411 mFC->waitForFrame();
1412 mFC->finishFrame();
1413 mFC->waitForFrame();
1414 mFC->finishFrame();
1415
1416 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1417 // block waiting for a buffer to become available.
1418 usleep(100000);
1419
1420 // Render and present a number of images. This thread should not be blocked
1421 // by the fact that the producer thread is blocking in dequeue.
1422 for (int i = 0; i < NUM_ITERATIONS; i++) {
1423 glClear(GL_COLOR_BUFFER_BIT);
1424 eglSwapBuffers(mEglDisplay, mEglSurface);
1425 }
1426
1427 // Consume the two pending buffers to unblock the producer thread.
1428 mST->updateTexImage();
1429 mST->updateTexImage();
1430
1431 // Consume the remaining buffers from the producer thread.
1432 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
1433 mFC->waitForFrame();
1434 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01001435 ALOGV("+updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07001436 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01001437 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07001438 }
1439}
1440
Jamie Gennis79e31252011-10-19 15:19:19 -07001441TEST_F(SurfaceTextureGLTest, EglDestroySurfaceUnrefsBuffers) {
1442 EGLSurface stcEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
1443 mANW.get(), NULL);
1444 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1445 ASSERT_NE(EGL_NO_SURFACE, stcEglSurface);
1446
1447 sp<GraphicBuffer> buffers[3];
1448
1449 for (int i = 0; i < 3; i++) {
1450 // Produce a frame
1451 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, stcEglSurface, stcEglSurface,
1452 mEglContext));
1453 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1454 glClear(GL_COLOR_BUFFER_BIT);
1455 eglSwapBuffers(mEglDisplay, stcEglSurface);
1456
1457 // Consume a frame
1458 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1459 mEglContext));
1460 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1461 mST->updateTexImage();
1462 buffers[i] = mST->getCurrentBuffer();
1463 }
1464
1465 // Destroy the GL texture object to release its ref on buffers[2].
1466 GLuint texID = TEX_ID;
1467 glDeleteTextures(1, &texID);
1468
1469 // Destroy the EGLSurface
1470 EXPECT_TRUE(eglDestroySurface(mEglDisplay, stcEglSurface));
1471 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1472
1473 // Release the ref that the SurfaceTexture has on buffers[2].
1474 mST->abandon();
1475
1476 EXPECT_EQ(1, buffers[0]->getStrongCount());
1477 EXPECT_EQ(1, buffers[1]->getStrongCount());
1478 EXPECT_EQ(1, buffers[2]->getStrongCount());
1479}
1480
1481TEST_F(SurfaceTextureGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1482 EGLSurface stcEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
1483 mANW.get(), NULL);
1484 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1485 ASSERT_NE(EGL_NO_SURFACE, stcEglSurface);
1486
1487 sp<GraphicBuffer> buffers[3];
1488
1489 for (int i = 0; i < 3; i++) {
1490 // Produce a frame
1491 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, stcEglSurface, stcEglSurface,
1492 mEglContext));
1493 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1494 glClear(GL_COLOR_BUFFER_BIT);
1495 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, stcEglSurface));
1496 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1497
1498 // Consume a frame
1499 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1500 mEglContext));
1501 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1502 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1503 buffers[i] = mST->getCurrentBuffer();
1504 }
1505
1506 // Abandon the SurfaceTexture, releasing the ref that the SurfaceTexture has
1507 // on buffers[2].
1508 mST->abandon();
1509
1510 // Destroy the GL texture object to release its ref on buffers[2].
1511 GLuint texID = TEX_ID;
1512 glDeleteTextures(1, &texID);
1513
1514 // Destroy the EGLSurface.
1515 EXPECT_TRUE(eglDestroySurface(mEglDisplay, stcEglSurface));
1516 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1517
1518 EXPECT_EQ(1, buffers[0]->getStrongCount());
1519 EXPECT_EQ(1, buffers[1]->getStrongCount());
1520 EXPECT_EQ(1, buffers[2]->getStrongCount());
1521}
1522
Jamie Gennis5451d152011-06-08 09:40:45 -07001523} // namespace android