blob: cfe89bcf95b5b75104c789ef4cb075130b16362d [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
Mathias Agopian90ac7992012-02-25 18:48:35 -080027#include <gui/ISurfaceComposer.h>
28#include <gui/Surface.h>
29#include <gui/SurfaceComposerClient.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080030
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 Gennisfa5b40e2012-03-15 14:01:24 -070050 const ::testing::TestInfo* const testInfo =
51 ::testing::UnitTest::GetInstance()->current_test_info();
52 ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
53 testInfo->name());
54
Jamie Gennisd99c0882011-03-10 16:24:46 -080055 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
56 ASSERT_EQ(EGL_SUCCESS, eglGetError());
57 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
58
59 EGLint majorVersion;
60 EGLint minorVersion;
61 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
62 ASSERT_EQ(EGL_SUCCESS, eglGetError());
63 RecordProperty("EglVersionMajor", majorVersion);
64 RecordProperty("EglVersionMajor", minorVersion);
65
Jamie Gennisd99c0882011-03-10 16:24:46 -080066 EGLint numConfigs = 0;
Jamie Gennis1876d132011-03-17 16:32:52 -070067 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080068 1, &numConfigs));
69 ASSERT_EQ(EGL_SUCCESS, eglGetError());
70
71 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
72 if (displaySecsEnv != NULL) {
73 mDisplaySecs = atoi(displaySecsEnv);
74 if (mDisplaySecs < 0) {
75 mDisplaySecs = 0;
76 }
77 } else {
78 mDisplaySecs = 0;
79 }
80
81 if (mDisplaySecs > 0) {
82 mComposerClient = new SurfaceComposerClient;
83 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
84
Jamie Gennisfc850122011-04-25 16:40:05 -070085 mSurfaceControl = mComposerClient->createSurface(
Jamie Gennisd99c0882011-03-10 16:24:46 -080086 String8("Test Surface"), 0,
87 getSurfaceWidth(), getSurfaceHeight(),
88 PIXEL_FORMAT_RGB_888, 0);
89
90 ASSERT_TRUE(mSurfaceControl != NULL);
91 ASSERT_TRUE(mSurfaceControl->isValid());
92
Mathias Agopian698c0872011-06-28 19:09:31 -070093 SurfaceComposerClient::openGlobalTransaction();
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070094 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080095 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070096 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennisd99c0882011-03-10 16:24:46 -080097
98 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -070099 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800100 window.get(), NULL);
101 } else {
102 EGLint pbufferAttribs[] = {
103 EGL_WIDTH, getSurfaceWidth(),
104 EGL_HEIGHT, getSurfaceHeight(),
105 EGL_NONE };
106
Jamie Gennis1876d132011-03-17 16:32:52 -0700107 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800108 pbufferAttribs);
109 }
110 ASSERT_EQ(EGL_SUCCESS, eglGetError());
111 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
112
Jamie Gennis1876d132011-03-17 16:32:52 -0700113 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800114 getContextAttribs());
115 ASSERT_EQ(EGL_SUCCESS, eglGetError());
116 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
117
118 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
119 mEglContext));
120 ASSERT_EQ(EGL_SUCCESS, eglGetError());
121
122 EGLint w, h;
123 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
124 ASSERT_EQ(EGL_SUCCESS, eglGetError());
125 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
126 ASSERT_EQ(EGL_SUCCESS, eglGetError());
127 RecordProperty("EglSurfaceWidth", w);
128 RecordProperty("EglSurfaceHeight", h);
129
130 glViewport(0, 0, w, h);
131 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
132 }
133
134 virtual void TearDown() {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700135 // Display the result
Jamie Gennisd99c0882011-03-10 16:24:46 -0800136 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
137 eglSwapBuffers(mEglDisplay, mEglSurface);
138 sleep(mDisplaySecs);
139 }
140
141 if (mComposerClient != NULL) {
142 mComposerClient->dispose();
143 }
144 if (mEglContext != EGL_NO_CONTEXT) {
145 eglDestroyContext(mEglDisplay, mEglContext);
146 }
147 if (mEglSurface != EGL_NO_SURFACE) {
148 eglDestroySurface(mEglDisplay, mEglSurface);
149 }
150 if (mEglDisplay != EGL_NO_DISPLAY) {
151 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
152 EGL_NO_CONTEXT);
153 eglTerminate(mEglDisplay);
154 }
155 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700156
157 const ::testing::TestInfo* const testInfo =
158 ::testing::UnitTest::GetInstance()->current_test_info();
159 ALOGV("End test: %s.%s", testInfo->test_case_name(),
160 testInfo->name());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800161 }
162
163 virtual EGLint const* getConfigAttribs() {
164 static EGLint sDefaultConfigAttribs[] = {
165 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
166 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
167 EGL_RED_SIZE, 8,
168 EGL_GREEN_SIZE, 8,
169 EGL_BLUE_SIZE, 8,
170 EGL_ALPHA_SIZE, 8,
171 EGL_DEPTH_SIZE, 16,
172 EGL_STENCIL_SIZE, 8,
173 EGL_NONE };
174
175 return sDefaultConfigAttribs;
176 }
177
178 virtual EGLint const* getContextAttribs() {
179 static EGLint sDefaultContextAttribs[] = {
180 EGL_CONTEXT_CLIENT_VERSION, 2,
181 EGL_NONE };
182
183 return sDefaultContextAttribs;
184 }
185
186 virtual EGLint getSurfaceWidth() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700187 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800188 }
189
190 virtual EGLint getSurfaceHeight() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700191 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800192 }
193
Jamie Gennisd99c0882011-03-10 16:24:46 -0800194 ::testing::AssertionResult checkPixel(int x, int y, int r,
Jamie Gennis824efa72011-06-13 13:41:01 -0700195 int g, int b, int a, int tolerance=2) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800196 GLubyte pixel[4];
197 String8 msg;
198 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
199 GLenum err = glGetError();
200 if (err != GL_NO_ERROR) {
201 msg += String8::format("error reading pixel: %#x", err);
202 while ((err = glGetError()) != GL_NO_ERROR) {
203 msg += String8::format(", %#x", err);
204 }
205 fprintf(stderr, "pixel check failure: %s\n", msg.string());
206 return ::testing::AssertionFailure(
207 ::testing::Message(msg.string()));
208 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700209 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800210 msg += String8::format("r(%d isn't %d)", pixel[0], r);
211 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700212 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800213 if (!msg.isEmpty()) {
214 msg += " ";
215 }
216 msg += String8::format("g(%d isn't %d)", pixel[1], g);
217 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700218 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800219 if (!msg.isEmpty()) {
220 msg += " ";
221 }
222 msg += String8::format("b(%d isn't %d)", pixel[2], b);
223 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700224 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800225 if (!msg.isEmpty()) {
226 msg += " ";
227 }
228 msg += String8::format("a(%d isn't %d)", pixel[3], a);
229 }
230 if (!msg.isEmpty()) {
231 fprintf(stderr, "pixel check failure: %s\n", msg.string());
232 return ::testing::AssertionFailure(
233 ::testing::Message(msg.string()));
234 } else {
235 return ::testing::AssertionSuccess();
236 }
237 }
238
Daniel Lam016c8cb2012-04-03 15:54:58 -0700239 ::testing::AssertionResult assertRectEq(const Rect &r1,
240 const Rect &r2, int tolerance=1) {
241
242 String8 msg;
243
244 if (abs(r1.left - r2.left) > tolerance) {
245 msg += String8::format("left(%d isn't %d)", r1.left, r2.left);
246 }
247 if (abs(r1.top - r2.top) > tolerance) {
248 if (!msg.isEmpty()) {
249 msg += " ";
250 }
251 msg += String8::format("top(%d isn't %d)", r1.top, r2.top);
252 }
253 if (abs(r1.right - r2.right) > tolerance) {
254 if (!msg.isEmpty()) {
255 msg += " ";
256 }
257 msg += String8::format("right(%d isn't %d)", r1.right, r2.right);
258 }
259 if (abs(r1.bottom - r2.bottom) > tolerance) {
260 if (!msg.isEmpty()) {
261 msg += " ";
262 }
263 msg += String8::format("bottom(%d isn't %d)", r1.bottom, r2.bottom);
264 }
265 if (!msg.isEmpty()) {
266 msg += String8::format(" R1: [%d %d %d %d] R2: [%d %d %d %d]",
267 r1.left, r1.top, r1.right, r1.bottom,
268 r2.left, r2.top, r2.right, r2.bottom);
269 fprintf(stderr, "assertRectEq: %s\n", msg.string());
270 return ::testing::AssertionFailure(
271 ::testing::Message(msg.string()));
272 } else {
273 return ::testing::AssertionSuccess();
274 }
275 }
276
Jamie Gennisd99c0882011-03-10 16:24:46 -0800277 int mDisplaySecs;
278 sp<SurfaceComposerClient> mComposerClient;
279 sp<SurfaceControl> mSurfaceControl;
280
281 EGLDisplay mEglDisplay;
282 EGLSurface mEglSurface;
283 EGLContext mEglContext;
Jamie Gennis1876d132011-03-17 16:32:52 -0700284 EGLConfig mGlConfig;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800285};
286
Jamie Gennis74bed552012-03-28 19:05:54 -0700287static void loadShader(GLenum shaderType, const char* pSource,
288 GLuint* outShader) {
289 GLuint shader = glCreateShader(shaderType);
290 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
291 if (shader) {
292 glShaderSource(shader, 1, &pSource, NULL);
293 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
294 glCompileShader(shader);
295 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
296 GLint compiled = 0;
297 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
298 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
299 if (!compiled) {
300 GLint infoLen = 0;
301 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
302 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
303 if (infoLen) {
304 char* buf = (char*) malloc(infoLen);
305 if (buf) {
306 glGetShaderInfoLog(shader, infoLen, NULL, buf);
307 printf("Shader compile log:\n%s\n", buf);
308 free(buf);
309 FAIL();
310 }
311 } else {
312 char* buf = (char*) malloc(0x1000);
313 if (buf) {
314 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
315 printf("Shader compile log:\n%s\n", buf);
316 free(buf);
317 FAIL();
318 }
319 }
320 glDeleteShader(shader);
321 shader = 0;
322 }
323 }
324 ASSERT_TRUE(shader != 0);
325 *outShader = shader;
326}
327
328static void createProgram(const char* pVertexSource,
329 const char* pFragmentSource, GLuint* outPgm) {
330 GLuint vertexShader, fragmentShader;
331 {
332 SCOPED_TRACE("compiling vertex shader");
333 ASSERT_NO_FATAL_FAILURE(loadShader(GL_VERTEX_SHADER, pVertexSource,
334 &vertexShader));
335 }
336 {
337 SCOPED_TRACE("compiling fragment shader");
338 ASSERT_NO_FATAL_FAILURE(loadShader(GL_FRAGMENT_SHADER, pFragmentSource,
339 &fragmentShader));
340 }
341
342 GLuint program = glCreateProgram();
343 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
344 if (program) {
345 glAttachShader(program, vertexShader);
346 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
347 glAttachShader(program, fragmentShader);
348 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
349 glLinkProgram(program);
350 GLint linkStatus = GL_FALSE;
351 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
352 if (linkStatus != GL_TRUE) {
353 GLint bufLength = 0;
354 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
355 if (bufLength) {
356 char* buf = (char*) malloc(bufLength);
357 if (buf) {
358 glGetProgramInfoLog(program, bufLength, NULL, buf);
359 printf("Program link log:\n%s\n", buf);
360 free(buf);
361 FAIL();
362 }
363 }
364 glDeleteProgram(program);
365 program = 0;
366 }
367 }
368 glDeleteShader(vertexShader);
369 glDeleteShader(fragmentShader);
370 ASSERT_TRUE(program != 0);
371 *outPgm = program;
372}
373
374static int abs(int value) {
375 return value > 0 ? value : -value;
376}
377
378
Jamie Gennisd99c0882011-03-10 16:24:46 -0800379// XXX: Code above this point should live elsewhere
380
381class SurfaceTextureGLTest : public GLTest {
382protected:
Jamie Gennis79e31252011-10-19 15:19:19 -0700383 enum { TEX_ID = 123 };
Jamie Gennisd99c0882011-03-10 16:24:46 -0800384
385 virtual void SetUp() {
386 GLTest::SetUp();
387 mST = new SurfaceTexture(TEX_ID);
388 mSTC = new SurfaceTextureClient(mST);
389 mANW = mSTC;
Jamie Gennis74bed552012-03-28 19:05:54 -0700390 mTextureRenderer = new TextureRenderer(TEX_ID, mST);
391 ASSERT_NO_FATAL_FAILURE(mTextureRenderer->SetUp());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800392 }
393
Jamie Gennis2640bfd2011-07-14 17:11:47 -0700394 virtual void TearDown() {
395 mANW.clear();
396 mSTC.clear();
397 mST.clear();
398 GLTest::TearDown();
399 }
400
Jamie Gennisd99c0882011-03-10 16:24:46 -0800401 void drawTexture() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700402 mTextureRenderer->drawTexture();
Jamie Gennisd99c0882011-03-10 16:24:46 -0800403 }
404
Jamie Gennis74bed552012-03-28 19:05:54 -0700405 class TextureRenderer: public RefBase {
406 public:
407 TextureRenderer(GLuint texName, const sp<SurfaceTexture>& st):
408 mTexName(texName),
409 mST(st) {
410 }
411
412 void SetUp() {
413 const char vsrc[] =
414 "attribute vec4 vPosition;\n"
415 "varying vec2 texCoords;\n"
416 "uniform mat4 texMatrix;\n"
417 "void main() {\n"
418 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
419 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
420 " gl_Position = vPosition;\n"
421 "}\n";
422
423 const char fsrc[] =
424 "#extension GL_OES_EGL_image_external : require\n"
425 "precision mediump float;\n"
426 "uniform samplerExternalOES texSampler;\n"
427 "varying vec2 texCoords;\n"
428 "void main() {\n"
429 " gl_FragColor = texture2D(texSampler, texCoords);\n"
430 "}\n";
431
432 {
433 SCOPED_TRACE("creating shader program");
434 ASSERT_NO_FATAL_FAILURE(createProgram(vsrc, fsrc, &mPgm));
435 }
436
437 mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
438 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
439 ASSERT_NE(-1, mPositionHandle);
440 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
441 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
442 ASSERT_NE(-1, mTexSamplerHandle);
443 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
444 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
445 ASSERT_NE(-1, mTexMatrixHandle);
446 }
447
448 // drawTexture draws the SurfaceTexture over the entire GL viewport.
449 void drawTexture() {
Jamie Gennisa96b6bd2012-04-11 17:27:12 -0700450 static const GLfloat triangleVertices[] = {
Jamie Gennis74bed552012-03-28 19:05:54 -0700451 -1.0f, 1.0f,
452 -1.0f, -1.0f,
453 1.0f, -1.0f,
454 1.0f, 1.0f,
455 };
456
457 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
458 triangleVertices);
459 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
460 glEnableVertexAttribArray(mPositionHandle);
461 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
462
463 glUseProgram(mPgm);
464 glUniform1i(mTexSamplerHandle, 0);
465 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
466 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
467 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
468
469 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
470 // they're setting the defautls for that target, but when hacking
471 // things to use GL_TEXTURE_2D they are needed to achieve the same
472 // behavior.
473 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
474 GL_LINEAR);
475 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
476 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
477 GL_LINEAR);
478 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
479 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
480 GL_CLAMP_TO_EDGE);
481 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
482 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
483 GL_CLAMP_TO_EDGE);
484 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
485
486 GLfloat texMatrix[16];
487 mST->getTransformMatrix(texMatrix);
488 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
489
490 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
491 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
492 }
493
494 GLuint mTexName;
495 sp<SurfaceTexture> mST;
496 GLuint mPgm;
497 GLint mPositionHandle;
498 GLint mTexSamplerHandle;
499 GLint mTexMatrixHandle;
500 };
501
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700502 class FrameWaiter : public SurfaceTexture::FrameAvailableListener {
503 public:
504 FrameWaiter():
505 mPendingFrames(0) {
506 }
507
508 void waitForFrame() {
509 Mutex::Autolock lock(mMutex);
510 while (mPendingFrames == 0) {
511 mCondition.wait(mMutex);
512 }
513 mPendingFrames--;
514 }
515
516 virtual void onFrameAvailable() {
517 Mutex::Autolock lock(mMutex);
518 mPendingFrames++;
519 mCondition.signal();
520 }
521
522 int mPendingFrames;
523 Mutex mMutex;
524 Condition mCondition;
525 };
526
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700527 // Note that SurfaceTexture will lose the notifications
528 // onBuffersReleased and onFrameAvailable as there is currently
529 // no way to forward the events. This DisconnectWaiter will not let the
530 // disconnect finish until finishDisconnect() is called. It will
531 // also block until a disconnect is called
532 class DisconnectWaiter : public BufferQueue::ConsumerListener {
533 public:
534 DisconnectWaiter () :
535 mWaitForDisconnect(false),
536 mPendingFrames(0) {
537 }
538
539 void waitForFrame() {
540 Mutex::Autolock lock(mMutex);
541 while (mPendingFrames == 0) {
542 mFrameCondition.wait(mMutex);
543 }
544 mPendingFrames--;
545 }
546
547 virtual void onFrameAvailable() {
548 Mutex::Autolock lock(mMutex);
549 mPendingFrames++;
550 mFrameCondition.signal();
551 }
552
553 virtual void onBuffersReleased() {
554 Mutex::Autolock lock(mMutex);
555 while (!mWaitForDisconnect) {
556 mDisconnectCondition.wait(mMutex);
557 }
558 }
559
560 void finishDisconnect() {
561 Mutex::Autolock lock(mMutex);
562 mWaitForDisconnect = true;
563 mDisconnectCondition.signal();
564 }
565
566 private:
567 Mutex mMutex;
568
569 bool mWaitForDisconnect;
570 Condition mDisconnectCondition;
571
572 int mPendingFrames;
573 Condition mFrameCondition;
574 };
575
Jamie Gennisd99c0882011-03-10 16:24:46 -0800576 sp<SurfaceTexture> mST;
577 sp<SurfaceTextureClient> mSTC;
578 sp<ANativeWindow> mANW;
Jamie Gennis74bed552012-03-28 19:05:54 -0700579 sp<TextureRenderer> mTextureRenderer;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800580};
581
582// Fill a YV12 buffer with a multi-colored checkerboard pattern
583void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
584 const int blockWidth = w > 16 ? w / 16 : 1;
585 const int blockHeight = h > 16 ? h / 16 : 1;
586 const int yuvTexOffsetY = 0;
587 int yuvTexStrideY = stride;
588 int yuvTexOffsetV = yuvTexStrideY * h;
589 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
590 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
591 int yuvTexStrideU = yuvTexStrideV;
592 for (int x = 0; x < w; x++) {
593 for (int y = 0; y < h; y++) {
594 int parityX = (x / blockWidth) & 1;
595 int parityY = (y / blockHeight) & 1;
596 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
597 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
598 if (x < w / 2 && y < h / 2) {
599 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
600 if (x * 2 < w / 2 && y * 2 < h / 2) {
601 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
602 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
603 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
604 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
605 intensity;
606 }
607 }
608 }
609 }
610}
611
612// Fill a YV12 buffer with red outside a given rectangle and green inside it.
613void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
614 const android_native_rect_t& rect) {
615 const int yuvTexOffsetY = 0;
616 int yuvTexStrideY = stride;
617 int yuvTexOffsetV = yuvTexStrideY * h;
618 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
619 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
620 int yuvTexStrideU = yuvTexStrideV;
621 for (int x = 0; x < w; x++) {
622 for (int y = 0; y < h; y++) {
623 bool inside = rect.left <= x && x < rect.right &&
624 rect.top <= y && y < rect.bottom;
625 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
626 if (x < w / 2 && y < h / 2) {
627 bool inside = rect.left <= 2*x && 2*x < rect.right &&
628 rect.top <= 2*y && 2*y < rect.bottom;
629 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
630 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
631 inside ? 16 : 255;
632 }
633 }
634 }
635}
636
Jamie Gennis1876d132011-03-17 16:32:52 -0700637void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
638 const size_t PIXEL_SIZE = 4;
639 for (int x = 0; x < w; x++) {
640 for (int y = 0; y < h; y++) {
641 off_t offset = (y * stride + x) * PIXEL_SIZE;
642 for (int c = 0; c < 4; c++) {
643 int parityX = (x / (1 << (c+2))) & 1;
644 int parityY = (y / (1 << (c+2))) & 1;
645 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
646 }
647 }
648 }
649}
650
Jamie Gennisfe27e2f2011-11-11 18:05:11 -0800651void fillRGBA8BufferSolid(uint8_t* buf, int w, int h, int stride, uint8_t r,
652 uint8_t g, uint8_t b, uint8_t a) {
653 const size_t PIXEL_SIZE = 4;
654 for (int y = 0; y < h; y++) {
655 for (int x = 0; x < h; x++) {
656 off_t offset = (y * stride + x) * PIXEL_SIZE;
657 buf[offset + 0] = r;
658 buf[offset + 1] = g;
659 buf[offset + 2] = b;
660 buf[offset + 3] = a;
661 }
662 }
663}
664
Jamie Gennisce561372012-03-19 18:33:05 -0700665// Produce a single RGBA8 frame by filling a buffer with a checkerboard pattern
666// using the CPU. This assumes that the ANativeWindow is already configured to
667// allow this to be done (e.g. the format is set to RGBA8).
668//
669// Calls to this function should be wrapped in an ASSERT_NO_FATAL_FAILURE().
670void produceOneRGBA8Frame(const sp<ANativeWindow>& anw) {
671 android_native_buffer_t* anb;
672 ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &anb));
673 ASSERT_TRUE(anb != NULL);
674
675 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
676 ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf->getNativeBuffer()));
677
678 uint8_t* img = NULL;
679 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
680 (void**)(&img)));
681 fillRGBA8Buffer(img, buf->getWidth(), buf->getHeight(), buf->getStride());
682 ASSERT_EQ(NO_ERROR, buf->unlock());
683 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer()));
684}
685
Jamie Gennisd99c0882011-03-10 16:24:46 -0800686TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700687 const int texWidth = 64;
688 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800689
690 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700691 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800692 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
693 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
694
Iliyan Malchev697526b2011-05-01 11:33:26 -0700695 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800696 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
697 ASSERT_TRUE(anb != NULL);
698
699 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
700 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
701
702 // Fill the buffer with the a checkerboard pattern
703 uint8_t* img = NULL;
704 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700705 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800706 buf->unlock();
707 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
708
709 mST->updateTexImage();
710
711 glClearColor(0.2, 0.2, 0.2, 0.2);
712 glClear(GL_COLOR_BUFFER_BIT);
713
Jamie Gennisc8c51522011-06-15 14:24:38 -0700714 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800715 drawTexture();
716
717 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255));
718 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700719 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255));
720 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800721
Jamie Gennisc8c51522011-06-15 14:24:38 -0700722 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255));
723 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255));
724 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800725 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700726 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800727 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255));
728 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255));
729}
730
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700731TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700732 const int texWidth = 64;
733 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800734
735 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700736 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800737 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
738 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
739
Iliyan Malchev697526b2011-05-01 11:33:26 -0700740 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800741 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
742 ASSERT_TRUE(anb != NULL);
743
744 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
745 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
746
747 // Fill the buffer with the a checkerboard pattern
748 uint8_t* img = NULL;
749 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700750 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800751 buf->unlock();
752 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
753
754 mST->updateTexImage();
755
756 glClearColor(0.2, 0.2, 0.2, 0.2);
757 glClear(GL_COLOR_BUFFER_BIT);
758
Jamie Gennisc8c51522011-06-15 14:24:38 -0700759 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800760 drawTexture();
761
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700762 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
763 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800764 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
765 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
766
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700767 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
768 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
769 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
770 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
771 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
772 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
773 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800774}
775
776TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700777 const int texWidth = 64;
778 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800779
780 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700781 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800782 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
783 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
784
785 android_native_rect_t crops[] = {
786 {4, 6, 22, 36},
787 {0, 6, 22, 36},
788 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700789 {4, 6, texWidth, 36},
790 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800791 };
792
793 for (int i = 0; i < 5; i++) {
794 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800795 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
796 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800797
798 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
799
Iliyan Malchev697526b2011-05-01 11:33:26 -0700800 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800801 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
802 ASSERT_TRUE(anb != NULL);
803
804 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800805 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(),
806 buf->getNativeBuffer()));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800807
808 uint8_t* img = NULL;
809 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700810 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800811 buf->unlock();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800812 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
813 buf->getNativeBuffer()));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800814
815 mST->updateTexImage();
816
817 glClearColor(0.2, 0.2, 0.2, 0.2);
818 glClear(GL_COLOR_BUFFER_BIT);
819
Jamie Gennisc8c51522011-06-15 14:24:38 -0700820 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800821 drawTexture();
822
823 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
824 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
825 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
826 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
827
828 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
829 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
830 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
831 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
832 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
833 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
834 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
835 }
836}
837
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700838// This test is intended to catch synchronization bugs between the CPU-written
839// and GPU-read buffers.
840TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
841 enum { texWidth = 16 };
842 enum { texHeight = 16 };
843 enum { numFrames = 1024 };
844
845 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
846 ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2));
847 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
848 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
849 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
850 GRALLOC_USAGE_SW_WRITE_OFTEN));
851
852 struct TestPixel {
853 int x;
854 int y;
855 };
856 const TestPixel testPixels[] = {
857 { 4, 11 },
858 { 12, 14 },
859 { 7, 2 },
860 };
861 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
862
863 class ProducerThread : public Thread {
864 public:
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800865 ProducerThread(const sp<ANativeWindow>& anw,
866 const TestPixel* testPixels):
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700867 mANW(anw),
868 mTestPixels(testPixels) {
869 }
870
871 virtual ~ProducerThread() {
872 }
873
874 virtual bool threadLoop() {
875 for (int i = 0; i < numFrames; i++) {
876 ANativeWindowBuffer* anb;
877 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
878 return false;
879 }
880 if (anb == NULL) {
881 return false;
882 }
883
884 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
885 if (mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())
886 != NO_ERROR) {
887 return false;
888 }
889
890 const int yuvTexOffsetY = 0;
891 int stride = buf->getStride();
892 int yuvTexStrideY = stride;
893 int yuvTexOffsetV = yuvTexStrideY * texHeight;
894 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
895 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
896 int yuvTexStrideU = yuvTexStrideV;
897
898 uint8_t* img = NULL;
899 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
900
901 // Gray out all the test pixels first, so we're more likely to
902 // see a failure if GL is still texturing from the buffer we
903 // just dequeued.
904 for (int j = 0; j < numTestPixels; j++) {
905 int x = mTestPixels[j].x;
906 int y = mTestPixels[j].y;
907 uint8_t value = 128;
908 img[y*stride + x] = value;
909 }
910
911 // Fill the buffer with gray.
912 for (int y = 0; y < texHeight; y++) {
913 for (int x = 0; x < texWidth; x++) {
914 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
915 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
916 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
917 }
918 }
919
920 // Set the test pixels to either white or black.
921 for (int j = 0; j < numTestPixels; j++) {
922 int x = mTestPixels[j].x;
923 int y = mTestPixels[j].y;
924 uint8_t value = 0;
925 if (j == (i % numTestPixels)) {
926 value = 255;
927 }
928 img[y*stride + x] = value;
929 }
930
931 buf->unlock();
932 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())
933 != NO_ERROR) {
934 return false;
935 }
936 }
937 return false;
938 }
939
940 sp<ANativeWindow> mANW;
941 const TestPixel* mTestPixels;
942 };
943
944 sp<FrameWaiter> fw(new FrameWaiter);
945 mST->setFrameAvailableListener(fw);
946
947 sp<Thread> pt(new ProducerThread(mANW, testPixels));
948 pt->run();
949
950 glViewport(0, 0, texWidth, texHeight);
951
952 glClearColor(0.2, 0.2, 0.2, 0.2);
953 glClear(GL_COLOR_BUFFER_BIT);
954
955 // We wait for the first two frames up front so that the producer will be
956 // likely to dequeue the buffer that's currently being textured from.
957 fw->waitForFrame();
958 fw->waitForFrame();
959
960 for (int i = 0; i < numFrames; i++) {
961 SCOPED_TRACE(String8::format("frame %d", i).string());
962
963 // We must wait for each frame to come in because if we ever do an
964 // updateTexImage call that doesn't consume a newly available buffer
965 // then the producer and consumer will get out of sync, which will cause
966 // a deadlock.
967 if (i > 1) {
968 fw->waitForFrame();
969 }
970 mST->updateTexImage();
971 drawTexture();
972
973 for (int j = 0; j < numTestPixels; j++) {
974 int x = testPixels[j].x;
975 int y = testPixels[j].y;
976 uint8_t value = 0;
977 if (j == (i % numTestPixels)) {
978 // We must y-invert the texture coords
979 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
980 } else {
981 // We must y-invert the texture coords
982 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
983 }
984 }
985 }
986
987 pt->requestExitAndWait();
988}
989
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700990TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700991 const int texWidth = 64;
992 const int texHeight = 66;
993
994 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
995 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
996 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
997 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
998
Jamie Gennisce561372012-03-19 18:33:05 -0700999 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -07001000
1001 mST->updateTexImage();
1002
1003 glClearColor(0.2, 0.2, 0.2, 0.2);
1004 glClear(GL_COLOR_BUFFER_BIT);
1005
Jamie Gennisc8c51522011-06-15 14:24:38 -07001006 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001007 drawTexture();
1008
1009 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
1010 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001011 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
1012 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001013
1014 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001015 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001016 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001017 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
1018 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001019 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001020 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001021 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
1022 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
1023 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001024 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001025 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
1026 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001027 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001028 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001029 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
1030}
1031
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001032TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -07001033 const int texWidth = 64;
1034 const int texHeight = 64;
1035
1036 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
1037 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
1038 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
1039 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
1040
Jamie Gennisce561372012-03-19 18:33:05 -07001041 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -07001042
1043 mST->updateTexImage();
1044
1045 glClearColor(0.2, 0.2, 0.2, 0.2);
1046 glClear(GL_COLOR_BUFFER_BIT);
1047
Jamie Gennisc8c51522011-06-15 14:24:38 -07001048 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001049 drawTexture();
1050
1051 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
1052 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
1053 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
1054 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
1055
1056 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
1057 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
1058 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
1059 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
1060 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
1061 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
1062 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
1063 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
1064 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
1065 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
1066 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
1067 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
1068 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
1069 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
1070 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
1071 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
1072}
1073
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001074// Tests if SurfaceTexture and BufferQueue are robust enough
1075// to handle a special case where updateTexImage is called
1076// in the middle of disconnect. This ordering is enforced
1077// by blocking in the disconnect callback.
1078TEST_F(SurfaceTextureGLTest, DisconnectStressTest) {
1079
1080 class ProducerThread : public Thread {
1081 public:
1082 ProducerThread(const sp<ANativeWindow>& anw):
1083 mANW(anw) {
1084 }
1085
1086 virtual ~ProducerThread() {
1087 }
1088
1089 virtual bool threadLoop() {
1090 ANativeWindowBuffer* anb;
1091
1092 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL);
1093
1094 for (int numFrames =0 ; numFrames < 2; numFrames ++) {
1095
1096 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1097 return false;
1098 }
1099 if (anb == NULL) {
1100 return false;
1101 }
1102 if (mANW->queueBuffer(mANW.get(), anb)
1103 != NO_ERROR) {
1104 return false;
1105 }
1106 }
1107
1108 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_EGL);
1109
1110 return false;
1111 }
1112
1113 private:
1114 sp<ANativeWindow> mANW;
1115 };
1116
1117 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1118
1119 sp<DisconnectWaiter> dw(new DisconnectWaiter());
1120 mST->getBufferQueue()->consumerConnect(dw);
1121
1122
1123 sp<Thread> pt(new ProducerThread(mANW));
1124 pt->run();
1125
1126 // eat a frame so SurfaceTexture will own an at least one slot
1127 dw->waitForFrame();
1128 EXPECT_EQ(OK,mST->updateTexImage());
1129
1130 dw->waitForFrame();
1131 // Could fail here as SurfaceTexture thinks it still owns the slot
1132 // but bufferQueue has released all slots
1133 EXPECT_EQ(OK,mST->updateTexImage());
1134
1135 dw->finishDisconnect();
1136}
1137
1138
1139// This test ensures that the SurfaceTexture clears the mCurrentTexture
1140// when it is disconnected and reconnected. Otherwise it will
1141// attempt to release a buffer that it does not owned
1142TEST_F(SurfaceTextureGLTest, DisconnectClearsCurrentTexture) {
1143 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1144
1145 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL);
1146
1147 ANativeWindowBuffer *anb;
1148
1149 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1150 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1151
1152 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1153 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1154
1155 EXPECT_EQ(OK,mST->updateTexImage());
1156 EXPECT_EQ(OK,mST->updateTexImage());
1157
1158 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_EGL);
1159 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL);
1160
1161 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1162
1163 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1164 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1165
1166 // Will fail here if mCurrentTexture is not cleared properly
1167 EXPECT_EQ(OK,mST->updateTexImage());
1168}
1169
Daniel Lam016c8cb2012-04-03 15:54:58 -07001170TEST_F(SurfaceTextureGLTest, ScaleToWindowMode) {
1171 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1172
1173 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1174 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));
1175
1176 // The producer image size
1177 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1178
1179 // The consumer image size (16 x 9) ratio
1180 mST->setDefaultBufferSize(1280, 720);
1181
1182 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU);
1183
1184 ANativeWindowBuffer *anb;
1185
1186 android_native_rect_t odd = {23, 78, 123, 477};
1187 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &odd));
1188 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1189 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1190 EXPECT_EQ(OK,mST->updateTexImage());
1191 Rect r = mST->getCurrentCrop();
1192 assertRectEq(Rect(23, 78, 123, 477), r);
1193
1194 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_EGL);
1195}
1196
1197// This test ensures the scaling mode does the right thing
1198// ie NATIVE_WINDOW_SCALING_MODE_CROP should crop
1199// the image such that it has the same aspect ratio as the
1200// default buffer size
1201TEST_F(SurfaceTextureGLTest, CroppedScalingMode) {
1202 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1203
1204 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1205 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP));
1206
1207 // The producer image size
1208 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1209
1210 // The consumer image size (16 x 9) ratio
1211 mST->setDefaultBufferSize(1280, 720);
1212
1213 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU);
1214
1215 ANativeWindowBuffer *anb;
1216
1217 // The crop is in the shape of (320, 180) === 16 x 9
1218 android_native_rect_t standard = {10, 20, 330, 200};
1219 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &standard));
1220 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1221 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1222 EXPECT_EQ(OK,mST->updateTexImage());
1223 Rect r = mST->getCurrentCrop();
1224 // crop should be the same as crop (same aspect ratio)
1225 assertRectEq(Rect(10, 20, 330, 200), r);
1226
1227 // make this wider then desired aspect 239 x 100 (2.39:1)
1228 android_native_rect_t wide = {20, 30, 259, 130};
1229 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &wide));
1230 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1231 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1232 EXPECT_EQ(OK,mST->updateTexImage());
1233 r = mST->getCurrentCrop();
1234 // crop should be the same height, but have cropped left and right borders
1235 // offset is 30.6 px L+, R-
1236 assertRectEq(Rect(51, 30, 228, 130), r);
1237
1238 // This image is taller then desired aspect 400 x 300 (4:3)
1239 android_native_rect_t narrow = {0, 0, 400, 300};
1240 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &narrow));
1241 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1242 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1243 EXPECT_EQ(OK,mST->updateTexImage());
1244 r = mST->getCurrentCrop();
1245 // crop should be the same width, but have cropped top and bottom borders
1246 // offset is 37.5 px
1247 assertRectEq(Rect(0, 37, 400, 262), r);
1248
1249 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
1250}
1251
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001252TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
1253 class ProducerThread : public Thread {
1254 public:
1255 ProducerThread(const sp<ANativeWindow>& anw):
1256 mANW(anw),
1257 mDequeueError(NO_ERROR) {
1258 }
1259
1260 virtual ~ProducerThread() {
1261 }
1262
1263 virtual bool threadLoop() {
1264 Mutex::Autolock lock(mMutex);
1265 ANativeWindowBuffer* anb;
1266
1267 // Frame 1
1268 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1269 return false;
1270 }
1271 if (anb == NULL) {
1272 return false;
1273 }
1274 if (mANW->queueBuffer(mANW.get(), anb)
1275 != NO_ERROR) {
1276 return false;
1277 }
1278
1279 // Frame 2
1280 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1281 return false;
1282 }
1283 if (anb == NULL) {
1284 return false;
1285 }
1286 if (mANW->queueBuffer(mANW.get(), anb)
1287 != NO_ERROR) {
1288 return false;
1289 }
1290
1291 // Frame 3 - error expected
1292 mDequeueError = mANW->dequeueBuffer(mANW.get(), &anb);
1293 return false;
1294 }
1295
1296 status_t getDequeueError() {
1297 Mutex::Autolock lock(mMutex);
1298 return mDequeueError;
1299 }
1300
1301 private:
1302 sp<ANativeWindow> mANW;
1303 status_t mDequeueError;
1304 Mutex mMutex;
1305 };
1306
1307 sp<FrameWaiter> fw(new FrameWaiter);
1308 mST->setFrameAvailableListener(fw);
1309 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1310 ASSERT_EQ(OK, mST->setBufferCountServer(2));
1311
1312 sp<Thread> pt(new ProducerThread(mANW));
1313 pt->run();
1314
1315 fw->waitForFrame();
1316 fw->waitForFrame();
1317
1318 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1319 // block waiting for a buffer to become available.
1320 usleep(100000);
1321
1322 mST->abandon();
1323
1324 pt->requestExitAndWait();
1325 ASSERT_EQ(NO_INIT,
1326 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1327}
1328
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001329TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1330 int texHeight = 16;
1331 ANativeWindowBuffer* anb;
1332
1333 GLint maxTextureSize;
1334 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1335
1336 // make sure it works with small textures
1337 mST->setDefaultBufferSize(16, texHeight);
1338 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1339 EXPECT_EQ(16, anb->width);
1340 EXPECT_EQ(texHeight, anb->height);
1341 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1342 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1343
1344 // make sure it works with GL_MAX_TEXTURE_SIZE
1345 mST->setDefaultBufferSize(maxTextureSize, texHeight);
1346 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1347 EXPECT_EQ(maxTextureSize, anb->width);
1348 EXPECT_EQ(texHeight, anb->height);
1349 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1350 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1351
1352 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1353 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
1354 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1355 EXPECT_EQ(maxTextureSize+1, anb->width);
1356 EXPECT_EQ(texHeight, anb->height);
1357 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1358 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1359}
1360
Jamie Gennis5451d152011-06-08 09:40:45 -07001361/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001362 * This test fixture is for testing GL -> GL texture streaming. It creates an
1363 * EGLSurface and an EGLContext for the image producer to use.
1364 */
1365class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1366protected:
1367 SurfaceTextureGLToGLTest():
1368 mProducerEglSurface(EGL_NO_SURFACE),
1369 mProducerEglContext(EGL_NO_CONTEXT) {
1370 }
1371
1372 virtual void SetUp() {
1373 SurfaceTextureGLTest::SetUp();
1374
Jamie Gennisce561372012-03-19 18:33:05 -07001375 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001376 mANW.get(), NULL);
1377 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1378 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1379
Jamie Gennisce561372012-03-19 18:33:05 -07001380 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001381 EGL_NO_CONTEXT, getContextAttribs());
1382 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1383 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1384 }
1385
1386 virtual void TearDown() {
1387 if (mProducerEglContext != EGL_NO_CONTEXT) {
1388 eglDestroyContext(mEglDisplay, mProducerEglContext);
1389 }
1390 if (mProducerEglSurface != EGL_NO_SURFACE) {
1391 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1392 }
1393 SurfaceTextureGLTest::TearDown();
1394 }
1395
1396 EGLSurface mProducerEglSurface;
1397 EGLContext mProducerEglContext;
1398};
1399
1400TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1401 const int texWidth = 64;
1402 const int texHeight = 64;
1403
1404 mST->setDefaultBufferSize(texWidth, texHeight);
1405
1406 // Do the producer side of things
1407 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1408 mProducerEglSurface, mProducerEglContext));
1409 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1410
1411 // This is needed to ensure we pick up a buffer of the correct size.
1412 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1413
1414 glClearColor(0.6, 0.6, 0.6, 0.6);
1415 glClear(GL_COLOR_BUFFER_BIT);
1416
1417 glEnable(GL_SCISSOR_TEST);
1418 glScissor(4, 4, 4, 4);
1419 glClearColor(1.0, 0.0, 0.0, 1.0);
1420 glClear(GL_COLOR_BUFFER_BIT);
1421
1422 glScissor(24, 48, 4, 4);
1423 glClearColor(0.0, 1.0, 0.0, 1.0);
1424 glClear(GL_COLOR_BUFFER_BIT);
1425
1426 glScissor(37, 17, 4, 4);
1427 glClearColor(0.0, 0.0, 1.0, 1.0);
1428 glClear(GL_COLOR_BUFFER_BIT);
1429
1430 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1431
1432 // Do the consumer side of things
1433 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1434 mEglContext));
1435 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1436
1437 glDisable(GL_SCISSOR_TEST);
1438
1439 mST->updateTexImage(); // Skip the first frame, which was empty
1440 mST->updateTexImage();
1441
1442 glClearColor(0.2, 0.2, 0.2, 0.2);
1443 glClear(GL_COLOR_BUFFER_BIT);
1444
1445 glViewport(0, 0, texWidth, texHeight);
1446 drawTexture();
1447
1448 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1449 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1450 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1451 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1452
1453 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1454 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1455 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1456 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1457 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1458 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1459 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1460 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1461 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1462 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1463 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1464 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1465 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1466 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1467 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1468 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1469}
1470
1471TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001472 sp<GraphicBuffer> buffers[2];
1473
1474 sp<FrameWaiter> fw(new FrameWaiter);
1475 mST->setFrameAvailableListener(fw);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001476
Jamie Gennise3603d72011-11-19 21:20:17 -08001477 // This test requires async mode to run on a single thread.
1478 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1479 mProducerEglSurface, mProducerEglContext));
1480 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1481 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1482 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1483
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001484 for (int i = 0; i < 2; i++) {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001485 // Produce a frame
1486 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1487 mProducerEglSurface, mProducerEglContext));
1488 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1489 glClear(GL_COLOR_BUFFER_BIT);
1490 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1491
1492 // Consume a frame
1493 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1494 mEglContext));
1495 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001496 fw->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001497 mST->updateTexImage();
1498 buffers[i] = mST->getCurrentBuffer();
1499 }
1500
1501 // Destroy the GL texture object to release its ref on buffers[2].
1502 GLuint texID = TEX_ID;
1503 glDeleteTextures(1, &texID);
1504
1505 // Destroy the EGLSurface
1506 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1507 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001508 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001509
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001510 // This test should have the only reference to buffer 0.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001511 EXPECT_EQ(1, buffers[0]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001512
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001513 // The SurfaceTexture should hold a single reference to buffer 1 in its
1514 // mCurrentBuffer member. All of the references in the slots should have
1515 // been released.
1516 EXPECT_EQ(2, buffers[1]->getStrongCount());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001517}
1518
1519TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1520 sp<GraphicBuffer> buffers[3];
1521
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001522 sp<FrameWaiter> fw(new FrameWaiter);
1523 mST->setFrameAvailableListener(fw);
1524
Jamie Gennise3603d72011-11-19 21:20:17 -08001525 // This test requires async mode to run on a single thread.
1526 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1527 mProducerEglSurface, mProducerEglContext));
1528 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1529 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1530 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1531
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001532 for (int i = 0; i < 3; i++) {
1533 // Produce a frame
1534 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1535 mProducerEglSurface, mProducerEglContext));
1536 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1537 glClear(GL_COLOR_BUFFER_BIT);
1538 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1539 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1540
1541 // Consume a frame
1542 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1543 mEglContext));
1544 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001545 fw->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001546 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1547 buffers[i] = mST->getCurrentBuffer();
1548 }
1549
1550 // Abandon the SurfaceTexture, releasing the ref that the SurfaceTexture has
1551 // on buffers[2].
1552 mST->abandon();
1553
1554 // Destroy the GL texture object to release its ref on buffers[2].
1555 GLuint texID = TEX_ID;
1556 glDeleteTextures(1, &texID);
1557
1558 // Destroy the EGLSurface.
1559 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1560 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001561 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001562
1563 EXPECT_EQ(1, buffers[0]->getStrongCount());
1564 EXPECT_EQ(1, buffers[1]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001565
1566 // Depending on how lazily the GL driver dequeues buffers, we may end up
1567 // with either two or three total buffers. If there are three, make sure
1568 // the last one was properly down-ref'd.
1569 if (buffers[2] != buffers[0]) {
1570 EXPECT_EQ(1, buffers[2]->getStrongCount());
1571 }
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001572}
1573
Jamie Gennis59769462011-11-19 18:04:43 -08001574TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) {
1575 // This test requires 3 buffers to run on a single thread.
1576 mST->setBufferCountServer(3);
1577
1578 ASSERT_TRUE(mST->isSynchronousMode());
1579
1580 for (int i = 0; i < 10; i++) {
1581 // Produce a frame
1582 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1583 mProducerEglSurface, mProducerEglContext));
1584 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1585 glClear(GL_COLOR_BUFFER_BIT);
1586 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1587 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1588
1589 // Consume a frame
1590 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1591 mEglContext));
1592 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1593 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1594 }
1595
1596 ASSERT_TRUE(mST->isSynchronousMode());
1597}
1598
Jamie Gennisc2c38022012-04-11 17:20:03 -07001599TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
1600 enum { texWidth = 64 };
1601 enum { texHeight = 64 };
1602
1603 // Set the user buffer size.
1604 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1605
1606 // Do the producer side of things
1607 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1608 mProducerEglSurface, mProducerEglContext));
1609 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1610
1611 // This is needed to ensure we pick up a buffer of the correct size.
1612 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1613
1614 glClearColor(0.6, 0.6, 0.6, 0.6);
1615 glClear(GL_COLOR_BUFFER_BIT);
1616
1617 glEnable(GL_SCISSOR_TEST);
1618 glScissor(4, 4, 1, 1);
1619 glClearColor(1.0, 0.0, 0.0, 1.0);
1620 glClear(GL_COLOR_BUFFER_BIT);
1621
1622 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1623
1624 // Do the consumer side of things
1625 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1626 mEglContext));
1627 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1628
1629 glDisable(GL_SCISSOR_TEST);
1630
1631 mST->updateTexImage(); // Skip the first frame, which was empty
1632 mST->updateTexImage();
1633
1634 glClearColor(0.2, 0.2, 0.2, 0.2);
1635 glClear(GL_COLOR_BUFFER_BIT);
1636
1637 glViewport(0, 0, texWidth, texHeight);
1638 drawTexture();
1639
1640 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1641 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1642 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1643 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1644
1645 EXPECT_TRUE(checkPixel( 4, 4, 255, 0, 0, 255));
1646 EXPECT_TRUE(checkPixel( 5, 5, 153, 153, 153, 153));
1647 EXPECT_TRUE(checkPixel( 3, 3, 153, 153, 153, 153));
1648 EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153));
1649 EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153));
1650}
1651
1652TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
1653 enum { texWidth = 64 };
1654 enum { texHeight = 16 };
1655
1656 // Set the transform hint.
1657 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1658
1659 // Set the user buffer size.
1660 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1661
1662 // Do the producer side of things
1663 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1664 mProducerEglSurface, mProducerEglContext));
1665 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1666
1667 // This is needed to ensure we pick up a buffer of the correct size and the
1668 // new rotation hint.
1669 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1670
1671 glClearColor(0.6, 0.6, 0.6, 0.6);
1672 glClear(GL_COLOR_BUFFER_BIT);
1673
1674 glEnable(GL_SCISSOR_TEST);
1675 glScissor(24, 4, 1, 1);
1676 glClearColor(1.0, 0.0, 0.0, 1.0);
1677 glClear(GL_COLOR_BUFFER_BIT);
1678
1679 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1680
1681 // Do the consumer side of things
1682 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1683 mEglContext));
1684 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1685
1686 glDisable(GL_SCISSOR_TEST);
1687
1688 mST->updateTexImage(); // Skip the first frame, which was empty
1689 mST->updateTexImage();
1690
1691 glClearColor(0.2, 0.2, 0.2, 0.2);
1692 glClear(GL_COLOR_BUFFER_BIT);
1693
1694 glViewport(0, 0, texWidth, texHeight);
1695 drawTexture();
1696
1697 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1698 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1699 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1700 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1701
1702 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1703 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1704 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1705 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1706 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1707}
1708
1709TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
1710 enum { texWidth = 64 };
1711 enum { texHeight = 16 };
1712
1713 // Set the transform hint.
1714 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1715
1716 // Set the default buffer size.
1717 mST->setDefaultBufferSize(texWidth, texHeight);
1718
1719 // Do the producer side of things
1720 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1721 mProducerEglSurface, mProducerEglContext));
1722 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1723
1724 // This is needed to ensure we pick up a buffer of the correct size and the
1725 // new rotation hint.
1726 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1727
1728 glClearColor(0.6, 0.6, 0.6, 0.6);
1729 glClear(GL_COLOR_BUFFER_BIT);
1730
1731 glEnable(GL_SCISSOR_TEST);
1732 glScissor(24, 4, 1, 1);
1733 glClearColor(1.0, 0.0, 0.0, 1.0);
1734 glClear(GL_COLOR_BUFFER_BIT);
1735
1736 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1737
1738 // Do the consumer side of things
1739 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1740 mEglContext));
1741 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1742
1743 glDisable(GL_SCISSOR_TEST);
1744
1745 mST->updateTexImage(); // Skip the first frame, which was empty
1746 mST->updateTexImage();
1747
1748 glClearColor(0.2, 0.2, 0.2, 0.2);
1749 glClear(GL_COLOR_BUFFER_BIT);
1750
1751 glViewport(0, 0, texWidth, texHeight);
1752 drawTexture();
1753
1754 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1755 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1756 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1757 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1758
1759 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1760 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1761 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1762 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1763 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1764}
1765
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001766/*
1767 * This test fixture is for testing GL -> GL texture streaming from one thread
1768 * to another. It contains functionality to create a producer thread that will
1769 * perform GL rendering to an ANativeWindow that feeds frames to a
1770 * SurfaceTexture. Additionally it supports interlocking the producer and
1771 * consumer threads so that a specific sequence of calls can be
1772 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07001773 *
1774 * The intended usage is as follows:
1775 *
1776 * TEST_F(...) {
1777 * class PT : public ProducerThread {
1778 * virtual void render() {
1779 * ...
1780 * swapBuffers();
1781 * }
1782 * };
1783 *
1784 * runProducerThread(new PT());
1785 *
1786 * // The order of these calls will vary from test to test and may include
1787 * // multiple frames and additional operations (e.g. GL rendering from the
1788 * // texture).
1789 * fc->waitForFrame();
1790 * mST->updateTexImage();
1791 * fc->finishFrame();
1792 * }
1793 *
1794 */
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001795class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07001796protected:
1797
1798 // ProducerThread is an abstract base class to simplify the creation of
1799 // OpenGL ES frame producer threads.
1800 class ProducerThread : public Thread {
1801 public:
1802 virtual ~ProducerThread() {
1803 }
1804
1805 void setEglObjects(EGLDisplay producerEglDisplay,
1806 EGLSurface producerEglSurface,
1807 EGLContext producerEglContext) {
1808 mProducerEglDisplay = producerEglDisplay;
1809 mProducerEglSurface = producerEglSurface;
1810 mProducerEglContext = producerEglContext;
1811 }
1812
1813 virtual bool threadLoop() {
1814 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
1815 mProducerEglSurface, mProducerEglContext);
1816 render();
1817 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1818 EGL_NO_CONTEXT);
1819 return false;
1820 }
1821
1822 protected:
1823 virtual void render() = 0;
1824
1825 void swapBuffers() {
1826 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
1827 }
1828
1829 EGLDisplay mProducerEglDisplay;
1830 EGLSurface mProducerEglSurface;
1831 EGLContext mProducerEglContext;
1832 };
1833
1834 // FrameCondition is a utility class for interlocking between the producer
1835 // and consumer threads. The FrameCondition object should be created and
1836 // destroyed in the consumer thread only. The consumer thread should set
1837 // the FrameCondition as the FrameAvailableListener of the SurfaceTexture,
1838 // and should call both waitForFrame and finishFrame once for each expected
1839 // frame.
1840 //
1841 // This interlocking relies on the fact that onFrameAvailable gets called
1842 // synchronously from SurfaceTexture::queueBuffer.
1843 class FrameCondition : public SurfaceTexture::FrameAvailableListener {
1844 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001845 FrameCondition():
1846 mFrameAvailable(false),
1847 mFrameFinished(false) {
1848 }
1849
Jamie Gennis5451d152011-06-08 09:40:45 -07001850 // waitForFrame waits for the next frame to arrive. This should be
1851 // called from the consumer thread once for every frame expected by the
1852 // test.
1853 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001854 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001855 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001856 while (!mFrameAvailable) {
1857 mFrameAvailableCondition.wait(mMutex);
1858 }
1859 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01001860 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001861 }
1862
1863 // Allow the producer to return from its swapBuffers call and continue
1864 // on to produce the next frame. This should be called by the consumer
1865 // thread once for every frame expected by the test.
1866 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001867 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001868 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001869 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001870 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01001871 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001872 }
1873
1874 // This should be called by SurfaceTexture on the producer thread.
1875 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001876 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001877 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001878 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001879 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001880 while (!mFrameFinished) {
1881 mFrameFinishCondition.wait(mMutex);
1882 }
1883 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01001884 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07001885 }
1886
1887 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001888 bool mFrameAvailable;
1889 bool mFrameFinished;
1890
Jamie Gennis5451d152011-06-08 09:40:45 -07001891 Mutex mMutex;
1892 Condition mFrameAvailableCondition;
1893 Condition mFrameFinishCondition;
1894 };
1895
Jamie Gennis5451d152011-06-08 09:40:45 -07001896 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001897 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07001898 mFC = new FrameCondition();
1899 mST->setFrameAvailableListener(mFC);
1900 }
1901
1902 virtual void TearDown() {
1903 if (mProducerThread != NULL) {
1904 mProducerThread->requestExitAndWait();
1905 }
Jamie Gennis5451d152011-06-08 09:40:45 -07001906 mProducerThread.clear();
1907 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001908 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07001909 }
1910
1911 void runProducerThread(const sp<ProducerThread> producerThread) {
1912 ASSERT_TRUE(mProducerThread == NULL);
1913 mProducerThread = producerThread;
1914 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
1915 mProducerEglContext);
1916 producerThread->run();
1917 }
1918
Jamie Gennis5451d152011-06-08 09:40:45 -07001919 sp<ProducerThread> mProducerThread;
1920 sp<FrameCondition> mFC;
1921};
1922
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001923TEST_F(SurfaceTextureGLThreadToGLTest,
1924 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001925 class PT : public ProducerThread {
1926 virtual void render() {
1927 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1928 glClear(GL_COLOR_BUFFER_BIT);
1929 swapBuffers();
1930 }
1931 };
1932
1933 runProducerThread(new PT());
1934
1935 mFC->waitForFrame();
1936 mST->updateTexImage();
1937 mFC->finishFrame();
1938
1939 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08001940}
Jamie Gennis5451d152011-06-08 09:40:45 -07001941
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001942TEST_F(SurfaceTextureGLThreadToGLTest,
1943 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001944 class PT : public ProducerThread {
1945 virtual void render() {
1946 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1947 glClear(GL_COLOR_BUFFER_BIT);
1948 swapBuffers();
1949 }
1950 };
1951
1952 runProducerThread(new PT());
1953
1954 mFC->waitForFrame();
1955 mFC->finishFrame();
1956 mST->updateTexImage();
1957
1958 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1959}
1960
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001961TEST_F(SurfaceTextureGLThreadToGLTest,
1962 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001963 enum { NUM_ITERATIONS = 1024 };
1964
1965 class PT : public ProducerThread {
1966 virtual void render() {
1967 for (int i = 0; i < NUM_ITERATIONS; i++) {
1968 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1969 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001970 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001971 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001972 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001973 }
1974 }
1975 };
1976
1977 runProducerThread(new PT());
1978
1979 for (int i = 0; i < NUM_ITERATIONS; i++) {
1980 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01001981 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001982 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01001983 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001984 mFC->finishFrame();
1985
1986 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1987 }
1988}
1989
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001990TEST_F(SurfaceTextureGLThreadToGLTest,
1991 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001992 enum { NUM_ITERATIONS = 1024 };
1993
1994 class PT : public ProducerThread {
1995 virtual void render() {
1996 for (int i = 0; i < NUM_ITERATIONS; i++) {
1997 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1998 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001999 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002000 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002001 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002002 }
2003 }
2004 };
2005
2006 runProducerThread(new PT());
2007
2008 for (int i = 0; i < NUM_ITERATIONS; i++) {
2009 mFC->waitForFrame();
2010 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002011 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002012 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01002013 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002014
2015 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2016 }
2017}
2018
Jamie Gennis6e502192011-07-21 14:31:31 -07002019// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002020TEST_F(SurfaceTextureGLThreadToGLTest,
2021 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07002022 enum { NUM_ITERATIONS = 64 };
2023
2024 class PT : public ProducerThread {
2025 virtual void render() {
2026 for (int i = 0; i < NUM_ITERATIONS; i++) {
2027 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2028 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002029 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002030 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002031 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002032 }
2033 }
2034 };
2035
2036 ASSERT_EQ(OK, mST->setSynchronousMode(true));
2037 ASSERT_EQ(OK, mST->setBufferCountServer(2));
2038
2039 runProducerThread(new PT());
2040
2041 // Allow three frames to be rendered and queued before starting the
2042 // rendering in this thread. For the latter two frames we don't call
2043 // updateTexImage so the next dequeue from the producer thread will block
2044 // waiting for a frame to become available.
2045 mFC->waitForFrame();
2046 mFC->finishFrame();
2047
2048 // We must call updateTexImage to consume the first frame so that the
2049 // SurfaceTexture is able to reduce the buffer count to 2. This is because
2050 // the GL driver may dequeue a buffer when the EGLSurface is created, and
2051 // that happens before we call setBufferCountServer. It's possible that the
2052 // driver does not dequeue a buffer at EGLSurface creation time, so we
2053 // cannot rely on this to cause the second dequeueBuffer call to block.
2054 mST->updateTexImage();
2055
2056 mFC->waitForFrame();
2057 mFC->finishFrame();
2058 mFC->waitForFrame();
2059 mFC->finishFrame();
2060
2061 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
2062 // block waiting for a buffer to become available.
2063 usleep(100000);
2064
2065 // Render and present a number of images. This thread should not be blocked
2066 // by the fact that the producer thread is blocking in dequeue.
2067 for (int i = 0; i < NUM_ITERATIONS; i++) {
2068 glClear(GL_COLOR_BUFFER_BIT);
2069 eglSwapBuffers(mEglDisplay, mEglSurface);
2070 }
2071
2072 // Consume the two pending buffers to unblock the producer thread.
2073 mST->updateTexImage();
2074 mST->updateTexImage();
2075
2076 // Consume the remaining buffers from the producer thread.
2077 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
2078 mFC->waitForFrame();
2079 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002080 ALOGV("+updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002081 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01002082 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002083 }
2084}
2085
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002086class SurfaceTextureFBOTest : public SurfaceTextureGLTest {
2087protected:
2088
2089 virtual void SetUp() {
2090 SurfaceTextureGLTest::SetUp();
2091
2092 glGenFramebuffers(1, &mFbo);
2093 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2094
2095 glGenTextures(1, &mFboTex);
2096 glBindTexture(GL_TEXTURE_2D, mFboTex);
2097 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getSurfaceWidth(),
2098 getSurfaceHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2099 glBindTexture(GL_TEXTURE_2D, 0);
2100 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2101
2102 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2103 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2104 GL_TEXTURE_2D, mFboTex, 0);
2105 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2106 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2107 }
2108
2109 virtual void TearDown() {
2110 SurfaceTextureGLTest::TearDown();
2111
2112 glDeleteTextures(1, &mFboTex);
2113 glDeleteFramebuffers(1, &mFbo);
2114 }
2115
2116 GLuint mFbo;
2117 GLuint mFboTex;
2118};
2119
2120// This test is intended to verify that proper synchronization is done when
2121// rendering into an FBO.
2122TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) {
2123 const int texWidth = 64;
2124 const int texHeight = 64;
2125
2126 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
2127 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
2128 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
2129 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
2130
2131 android_native_buffer_t* anb;
2132 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
2133 ASSERT_TRUE(anb != NULL);
2134
2135 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
2136 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
2137
2138 // Fill the buffer with green
2139 uint8_t* img = NULL;
2140 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
2141 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255,
2142 0, 255);
2143 buf->unlock();
2144 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
2145
2146 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2147
2148 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2149 drawTexture();
2150 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2151
2152 for (int i = 0; i < 4; i++) {
2153 SCOPED_TRACE(String8::format("frame %d", i).string());
2154
2155 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
2156 ASSERT_TRUE(anb != NULL);
2157
2158 buf = new GraphicBuffer(anb, false);
2159 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(),
2160 buf->getNativeBuffer()));
2161
2162 // Fill the buffer with red
2163 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
2164 (void**)(&img)));
2165 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0,
2166 0, 255);
2167 ASSERT_EQ(NO_ERROR, buf->unlock());
2168 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
2169 buf->getNativeBuffer()));
2170
2171 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2172
2173 drawTexture();
2174
2175 EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255));
2176 }
2177
2178 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2179
2180 EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255));
2181}
2182
Jamie Gennisce561372012-03-19 18:33:05 -07002183class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest {
2184protected:
Jamie Gennis74bed552012-03-28 19:05:54 -07002185 enum { SECOND_TEX_ID = 123 };
2186 enum { THIRD_TEX_ID = 456 };
2187
Jamie Gennisce561372012-03-19 18:33:05 -07002188 SurfaceTextureMultiContextGLTest():
2189 mSecondEglContext(EGL_NO_CONTEXT) {
2190 }
2191
2192 virtual void SetUp() {
2193 SurfaceTextureGLTest::SetUp();
2194
Jamie Gennis74bed552012-03-28 19:05:54 -07002195 // Set up the secondary context and texture renderer.
Jamie Gennisce561372012-03-19 18:33:05 -07002196 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2197 EGL_NO_CONTEXT, getContextAttribs());
2198 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2199 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext);
Jamie Gennis74bed552012-03-28 19:05:54 -07002200
2201 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2202 mSecondEglContext));
2203 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2204 mSecondTextureRenderer = new TextureRenderer(SECOND_TEX_ID, mST);
2205 ASSERT_NO_FATAL_FAILURE(mSecondTextureRenderer->SetUp());
2206
2207 // Set up the tertiary context and texture renderer.
2208 mThirdEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2209 EGL_NO_CONTEXT, getContextAttribs());
2210 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2211 ASSERT_NE(EGL_NO_CONTEXT, mThirdEglContext);
2212
2213 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2214 mThirdEglContext));
2215 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2216 mThirdTextureRenderer = new TextureRenderer(THIRD_TEX_ID, mST);
2217 ASSERT_NO_FATAL_FAILURE(mThirdTextureRenderer->SetUp());
2218
2219 // Switch back to the primary context to start the tests.
2220 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2221 mEglContext));
Jamie Gennisce561372012-03-19 18:33:05 -07002222 }
2223
2224 virtual void TearDown() {
Jamie Gennis74bed552012-03-28 19:05:54 -07002225 if (mThirdEglContext != EGL_NO_CONTEXT) {
2226 eglDestroyContext(mEglDisplay, mThirdEglContext);
2227 }
Jamie Gennisce561372012-03-19 18:33:05 -07002228 if (mSecondEglContext != EGL_NO_CONTEXT) {
2229 eglDestroyContext(mEglDisplay, mSecondEglContext);
2230 }
2231 SurfaceTextureGLTest::TearDown();
2232 }
2233
2234 EGLContext mSecondEglContext;
Jamie Gennis74bed552012-03-28 19:05:54 -07002235 sp<TextureRenderer> mSecondTextureRenderer;
2236
2237 EGLContext mThirdEglContext;
2238 sp<TextureRenderer> mThirdTextureRenderer;
Jamie Gennisce561372012-03-19 18:33:05 -07002239};
2240
2241TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) {
2242 sp<FrameWaiter> fw(new FrameWaiter);
2243 mST->setFrameAvailableListener(fw);
2244
2245 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2246
2247 // Latch the texture contents on the primary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07002248 fw->waitForFrame();
2249 ASSERT_EQ(OK, mST->updateTexImage());
Jamie Gennisce561372012-03-19 18:33:05 -07002250
2251 // Attempt to latch the texture on the secondary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07002252 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
Jamie Gennisce561372012-03-19 18:33:05 -07002253 mSecondEglContext));
2254 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -07002255 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2256}
2257
2258TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) {
2259 sp<FrameWaiter> fw(new FrameWaiter);
2260 mST->setFrameAvailableListener(fw);
2261
2262 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2263
2264 // Latch the texture contents on the primary context.
2265 fw->waitForFrame();
2266 ASSERT_EQ(OK, mST->updateTexImage());
2267
2268 // Detach from the primary context.
2269 ASSERT_EQ(OK, mST->detachFromContext());
2270
2271 // Check that the GL texture was deleted.
2272 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2273}
2274
2275TEST_F(SurfaceTextureMultiContextGLTest,
2276 DetachFromContextSucceedsAfterProducerDisconnect) {
2277 sp<FrameWaiter> fw(new FrameWaiter);
2278 mST->setFrameAvailableListener(fw);
2279
2280 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2281
2282 // Latch the texture contents on the primary context.
2283 fw->waitForFrame();
2284 ASSERT_EQ(OK, mST->updateTexImage());
2285
2286 // Detach from the primary context.
2287 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2288 ASSERT_EQ(OK, mST->detachFromContext());
2289
2290 // Check that the GL texture was deleted.
2291 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2292}
2293
2294TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) {
2295 sp<FrameWaiter> fw(new FrameWaiter);
2296 mST->setFrameAvailableListener(fw);
2297
2298 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2299
2300 // Latch the texture contents on the primary context.
2301 fw->waitForFrame();
2302 ASSERT_EQ(OK, mST->updateTexImage());
2303
2304 // Attempt to detach from the primary context.
2305 mST->abandon();
2306 ASSERT_EQ(NO_INIT, mST->detachFromContext());
2307}
2308
2309TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) {
2310 sp<FrameWaiter> fw(new FrameWaiter);
2311 mST->setFrameAvailableListener(fw);
2312
2313 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2314
2315 // Latch the texture contents on the primary context.
2316 fw->waitForFrame();
2317 ASSERT_EQ(OK, mST->updateTexImage());
2318
2319 // Detach from the primary context.
2320 ASSERT_EQ(OK, mST->detachFromContext());
2321
2322 // Attempt to detach from the primary context again.
2323 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2324}
2325
2326TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) {
2327 sp<FrameWaiter> fw(new FrameWaiter);
2328 mST->setFrameAvailableListener(fw);
2329
2330 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2331
2332 // Latch the texture contents on the primary context.
2333 fw->waitForFrame();
2334 ASSERT_EQ(OK, mST->updateTexImage());
2335
2336 // Make there be no current display.
2337 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2338 EGL_NO_CONTEXT));
2339 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2340
2341 // Attempt to detach from the primary context.
2342 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2343}
2344
2345TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) {
2346 sp<FrameWaiter> fw(new FrameWaiter);
2347 mST->setFrameAvailableListener(fw);
2348
2349 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2350
2351 // Latch the texture contents on the primary context.
2352 fw->waitForFrame();
2353 ASSERT_EQ(OK, mST->updateTexImage());
2354
2355 // Make current context be incorrect.
2356 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2357 mSecondEglContext));
2358 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2359
2360 // Attempt to detach from the primary context.
2361 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2362}
2363
2364TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) {
2365 sp<FrameWaiter> fw(new FrameWaiter);
2366 mST->setFrameAvailableListener(fw);
2367
2368 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2369
2370 // Detach from the primary context.
2371 ASSERT_EQ(OK, mST->detachFromContext());
2372
2373 // Attempt to latch the texture contents on the primary context.
2374 fw->waitForFrame();
2375 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2376}
2377
2378TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) {
2379 sp<FrameWaiter> fw(new FrameWaiter);
2380 mST->setFrameAvailableListener(fw);
2381
2382 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2383
2384 // Latch the texture contents on the primary context.
2385 fw->waitForFrame();
2386 ASSERT_EQ(OK, mST->updateTexImage());
2387
2388 // Detach from the primary context.
2389 ASSERT_EQ(OK, mST->detachFromContext());
2390
2391 // Attach to the secondary context.
2392 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2393 mSecondEglContext));
2394 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2395
2396 // Verify that the texture object was created and bound.
2397 GLint texBinding = -1;
2398 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2399 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2400
2401 // Try to use the texture from the secondary context.
2402 glClearColor(0.2, 0.2, 0.2, 0.2);
2403 glClear(GL_COLOR_BUFFER_BIT);
2404 glViewport(0, 0, 1, 1);
2405 mSecondTextureRenderer->drawTexture();
2406 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2407 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2408}
2409
2410TEST_F(SurfaceTextureMultiContextGLTest,
2411 AttachToContextSucceedsAfterProducerDisconnect) {
2412 sp<FrameWaiter> fw(new FrameWaiter);
2413 mST->setFrameAvailableListener(fw);
2414
2415 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2416
2417 // Latch the texture contents on the primary context.
2418 fw->waitForFrame();
2419 ASSERT_EQ(OK, mST->updateTexImage());
2420
2421 // Detach from the primary context.
2422 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2423 ASSERT_EQ(OK, mST->detachFromContext());
2424
2425 // Attach to the secondary context.
2426 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2427 mSecondEglContext));
2428 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2429
2430 // Verify that the texture object was created and bound.
2431 GLint texBinding = -1;
2432 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2433 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2434
2435 // Try to use the texture from the secondary context.
2436 glClearColor(0.2, 0.2, 0.2, 0.2);
2437 glClear(GL_COLOR_BUFFER_BIT);
2438 glViewport(0, 0, 1, 1);
2439 mSecondTextureRenderer->drawTexture();
2440 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2441 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2442}
2443
2444TEST_F(SurfaceTextureMultiContextGLTest,
2445 AttachToContextSucceedsBeforeUpdateTexImage) {
2446 sp<FrameWaiter> fw(new FrameWaiter);
2447 mST->setFrameAvailableListener(fw);
2448
2449 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2450
2451 // Detach from the primary context.
2452 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2453 ASSERT_EQ(OK, mST->detachFromContext());
2454
2455 // Attach to the secondary context.
2456 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2457 mSecondEglContext));
2458 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2459
2460 // Verify that the texture object was created and bound.
2461 GLint texBinding = -1;
2462 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2463 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2464
2465 // Latch the texture contents on the primary context.
2466 fw->waitForFrame();
2467 ASSERT_EQ(OK, mST->updateTexImage());
2468
2469 // Try to use the texture from the secondary context.
2470 glClearColor(0.2, 0.2, 0.2, 0.2);
2471 glClear(GL_COLOR_BUFFER_BIT);
2472 glViewport(0, 0, 1, 1);
2473 mSecondTextureRenderer->drawTexture();
2474 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2475 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2476}
2477
2478TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) {
2479 sp<FrameWaiter> fw(new FrameWaiter);
2480 mST->setFrameAvailableListener(fw);
2481
2482 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2483
2484 // Latch the texture contents on the primary context.
2485 fw->waitForFrame();
2486 ASSERT_EQ(OK, mST->updateTexImage());
2487
2488 // Detach from the primary context.
2489 ASSERT_EQ(OK, mST->detachFromContext());
2490
2491 // Attempt to attach to the secondary context.
2492 mST->abandon();
2493
2494 // Attempt to attach to the primary context.
2495 ASSERT_EQ(NO_INIT, mST->attachToContext(SECOND_TEX_ID));
2496}
2497
2498TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) {
2499 sp<FrameWaiter> fw(new FrameWaiter);
2500 mST->setFrameAvailableListener(fw);
2501
2502 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2503
2504 // Latch the texture contents on the primary context.
2505 fw->waitForFrame();
2506 ASSERT_EQ(OK, mST->updateTexImage());
2507
2508 // Attempt to attach to the primary context.
2509 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2510}
2511
2512TEST_F(SurfaceTextureMultiContextGLTest,
2513 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) {
2514 sp<FrameWaiter> fw(new FrameWaiter);
2515 mST->setFrameAvailableListener(fw);
2516
2517 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2518
2519 // Attempt to attach to the primary context.
2520 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2521}
2522
2523TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) {
2524 sp<FrameWaiter> fw(new FrameWaiter);
2525 mST->setFrameAvailableListener(fw);
2526
2527 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2528
2529 // Latch the texture contents on the primary context.
2530 fw->waitForFrame();
2531 ASSERT_EQ(OK, mST->updateTexImage());
2532
2533 // Detach from the primary context.
2534 ASSERT_EQ(OK, mST->detachFromContext());
2535
2536 // Make there be no current display.
2537 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2538 EGL_NO_CONTEXT));
2539 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2540
2541 // Attempt to attach with no context current.
2542 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2543}
2544
2545TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) {
2546 sp<FrameWaiter> fw(new FrameWaiter);
2547 mST->setFrameAvailableListener(fw);
2548
2549 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2550
2551 // Latch the texture contents on the primary context.
2552 fw->waitForFrame();
2553 ASSERT_EQ(OK, mST->updateTexImage());
2554
2555 // Detach from the primary context.
2556 ASSERT_EQ(OK, mST->detachFromContext());
2557
2558 // Attach to the secondary context.
2559 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2560 mSecondEglContext));
2561 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2562
2563 // Detach from the secondary context.
2564 ASSERT_EQ(OK, mST->detachFromContext());
2565
2566 // Attach to the tertiary context.
2567 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2568 mThirdEglContext));
2569 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2570
2571 // Verify that the texture object was created and bound.
2572 GLint texBinding = -1;
2573 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2574 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2575
2576 // Try to use the texture from the tertiary context.
2577 glClearColor(0.2, 0.2, 0.2, 0.2);
2578 glClear(GL_COLOR_BUFFER_BIT);
2579 glViewport(0, 0, 1, 1);
2580 mThirdTextureRenderer->drawTexture();
2581 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2582 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2583}
2584
2585TEST_F(SurfaceTextureMultiContextGLTest,
2586 AttachToContextSucceedsTwiceBeforeUpdateTexImage) {
2587 sp<FrameWaiter> fw(new FrameWaiter);
2588 mST->setFrameAvailableListener(fw);
2589
2590 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2591
2592 // Detach from the primary context.
2593 ASSERT_EQ(OK, mST->detachFromContext());
2594
2595 // Attach to the secondary context.
2596 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2597 mSecondEglContext));
2598 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2599
2600 // Detach from the secondary context.
2601 ASSERT_EQ(OK, mST->detachFromContext());
2602
2603 // Attach to the tertiary context.
2604 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2605 mThirdEglContext));
2606 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2607
2608 // Verify that the texture object was created and bound.
2609 GLint texBinding = -1;
2610 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2611 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2612
2613 // Latch the texture contents on the tertiary context.
2614 fw->waitForFrame();
2615 ASSERT_EQ(OK, mST->updateTexImage());
2616
2617 // Try to use the texture from the tertiary context.
2618 glClearColor(0.2, 0.2, 0.2, 0.2);
2619 glClear(GL_COLOR_BUFFER_BIT);
2620 glViewport(0, 0, 1, 1);
2621 mThirdTextureRenderer->drawTexture();
2622 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2623 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
Jamie Gennisce561372012-03-19 18:33:05 -07002624}
2625
Jamie Gennis5451d152011-06-08 09:40:45 -07002626} // namespace android