blob: 84ec7f230f45cf64d9226e9d0b05a11c6a75ac1c [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>
Andy McFadden2adaf042012-12-18 09:49:45 -080021#include <gui/GLConsumer.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080022#include <ui/GraphicBuffer.h>
23#include <utils/String8.h>
Jamie Gennis5451d152011-06-08 09:40:45 -070024#include <utils/threads.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080025
Mathias Agopian90ac7992012-02-25 18:48:35 -080026#include <gui/ISurfaceComposer.h>
27#include <gui/Surface.h>
28#include <gui/SurfaceComposerClient.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080029
30#include <EGL/egl.h>
31#include <EGL/eglext.h>
32#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
34
35#include <ui/FramebufferNativeWindow.h>
36
37namespace android {
38
39class GLTest : public ::testing::Test {
40protected:
41
42 GLTest():
43 mEglDisplay(EGL_NO_DISPLAY),
44 mEglSurface(EGL_NO_SURFACE),
45 mEglContext(EGL_NO_CONTEXT) {
46 }
47
48 virtual void SetUp() {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070049 const ::testing::TestInfo* const testInfo =
50 ::testing::UnitTest::GetInstance()->current_test_info();
51 ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
52 testInfo->name());
53
Jamie Gennisd99c0882011-03-10 16:24:46 -080054 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
55 ASSERT_EQ(EGL_SUCCESS, eglGetError());
56 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
57
58 EGLint majorVersion;
59 EGLint minorVersion;
60 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
61 ASSERT_EQ(EGL_SUCCESS, eglGetError());
62 RecordProperty("EglVersionMajor", majorVersion);
63 RecordProperty("EglVersionMajor", minorVersion);
64
Jamie Gennisd99c0882011-03-10 16:24:46 -080065 EGLint numConfigs = 0;
Jamie Gennis1876d132011-03-17 16:32:52 -070066 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080067 1, &numConfigs));
68 ASSERT_EQ(EGL_SUCCESS, eglGetError());
69
70 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
71 if (displaySecsEnv != NULL) {
72 mDisplaySecs = atoi(displaySecsEnv);
73 if (mDisplaySecs < 0) {
74 mDisplaySecs = 0;
75 }
76 } else {
77 mDisplaySecs = 0;
78 }
79
80 if (mDisplaySecs > 0) {
81 mComposerClient = new SurfaceComposerClient;
82 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
83
Jamie Gennisfc850122011-04-25 16:40:05 -070084 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070085 String8("Test Surface"),
Jamie Gennisd99c0882011-03-10 16:24:46 -080086 getSurfaceWidth(), getSurfaceHeight(),
87 PIXEL_FORMAT_RGB_888, 0);
88
89 ASSERT_TRUE(mSurfaceControl != NULL);
90 ASSERT_TRUE(mSurfaceControl->isValid());
91
Mathias Agopian698c0872011-06-28 19:09:31 -070092 SurfaceComposerClient::openGlobalTransaction();
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070093 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080094 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070095 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennisd99c0882011-03-10 16:24:46 -080096
97 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -070098 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080099 window.get(), NULL);
100 } else {
101 EGLint pbufferAttribs[] = {
102 EGL_WIDTH, getSurfaceWidth(),
103 EGL_HEIGHT, getSurfaceHeight(),
104 EGL_NONE };
105
Jamie Gennis1876d132011-03-17 16:32:52 -0700106 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800107 pbufferAttribs);
108 }
109 ASSERT_EQ(EGL_SUCCESS, eglGetError());
110 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
111
Jamie Gennis1876d132011-03-17 16:32:52 -0700112 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800113 getContextAttribs());
114 ASSERT_EQ(EGL_SUCCESS, eglGetError());
115 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
116
117 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
118 mEglContext));
119 ASSERT_EQ(EGL_SUCCESS, eglGetError());
120
121 EGLint w, h;
122 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
123 ASSERT_EQ(EGL_SUCCESS, eglGetError());
124 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
125 ASSERT_EQ(EGL_SUCCESS, eglGetError());
126 RecordProperty("EglSurfaceWidth", w);
127 RecordProperty("EglSurfaceHeight", h);
128
129 glViewport(0, 0, w, h);
130 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
131 }
132
133 virtual void TearDown() {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700134 // Display the result
Jamie Gennisd99c0882011-03-10 16:24:46 -0800135 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
136 eglSwapBuffers(mEglDisplay, mEglSurface);
137 sleep(mDisplaySecs);
138 }
139
140 if (mComposerClient != NULL) {
141 mComposerClient->dispose();
142 }
143 if (mEglContext != EGL_NO_CONTEXT) {
144 eglDestroyContext(mEglDisplay, mEglContext);
145 }
146 if (mEglSurface != EGL_NO_SURFACE) {
147 eglDestroySurface(mEglDisplay, mEglSurface);
148 }
149 if (mEglDisplay != EGL_NO_DISPLAY) {
150 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
151 EGL_NO_CONTEXT);
152 eglTerminate(mEglDisplay);
153 }
154 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700155
156 const ::testing::TestInfo* const testInfo =
157 ::testing::UnitTest::GetInstance()->current_test_info();
158 ALOGV("End test: %s.%s", testInfo->test_case_name(),
159 testInfo->name());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800160 }
161
162 virtual EGLint const* getConfigAttribs() {
163 static EGLint sDefaultConfigAttribs[] = {
164 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
165 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
166 EGL_RED_SIZE, 8,
167 EGL_GREEN_SIZE, 8,
168 EGL_BLUE_SIZE, 8,
169 EGL_ALPHA_SIZE, 8,
170 EGL_DEPTH_SIZE, 16,
171 EGL_STENCIL_SIZE, 8,
172 EGL_NONE };
173
174 return sDefaultConfigAttribs;
175 }
176
177 virtual EGLint const* getContextAttribs() {
178 static EGLint sDefaultContextAttribs[] = {
179 EGL_CONTEXT_CLIENT_VERSION, 2,
180 EGL_NONE };
181
182 return sDefaultContextAttribs;
183 }
184
185 virtual EGLint getSurfaceWidth() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700186 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800187 }
188
189 virtual EGLint getSurfaceHeight() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700190 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800191 }
192
Jamie Gennisd99c0882011-03-10 16:24:46 -0800193 ::testing::AssertionResult checkPixel(int x, int y, int r,
Jamie Gennis824efa72011-06-13 13:41:01 -0700194 int g, int b, int a, int tolerance=2) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800195 GLubyte pixel[4];
196 String8 msg;
197 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
198 GLenum err = glGetError();
199 if (err != GL_NO_ERROR) {
200 msg += String8::format("error reading pixel: %#x", err);
201 while ((err = glGetError()) != GL_NO_ERROR) {
202 msg += String8::format(", %#x", err);
203 }
Jamie Gennisd99c0882011-03-10 16:24:46 -0800204 return ::testing::AssertionFailure(
205 ::testing::Message(msg.string()));
206 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700207 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800208 msg += String8::format("r(%d isn't %d)", pixel[0], r);
209 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700210 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800211 if (!msg.isEmpty()) {
212 msg += " ";
213 }
214 msg += String8::format("g(%d isn't %d)", pixel[1], g);
215 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700216 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800217 if (!msg.isEmpty()) {
218 msg += " ";
219 }
220 msg += String8::format("b(%d isn't %d)", pixel[2], b);
221 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700222 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800223 if (!msg.isEmpty()) {
224 msg += " ";
225 }
226 msg += String8::format("a(%d isn't %d)", pixel[3], a);
227 }
228 if (!msg.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800229 return ::testing::AssertionFailure(
230 ::testing::Message(msg.string()));
231 } else {
232 return ::testing::AssertionSuccess();
233 }
234 }
235
Daniel Lam016c8cb2012-04-03 15:54:58 -0700236 ::testing::AssertionResult assertRectEq(const Rect &r1,
237 const Rect &r2, int tolerance=1) {
238
239 String8 msg;
240
241 if (abs(r1.left - r2.left) > tolerance) {
242 msg += String8::format("left(%d isn't %d)", r1.left, r2.left);
243 }
244 if (abs(r1.top - r2.top) > tolerance) {
245 if (!msg.isEmpty()) {
246 msg += " ";
247 }
248 msg += String8::format("top(%d isn't %d)", r1.top, r2.top);
249 }
250 if (abs(r1.right - r2.right) > tolerance) {
251 if (!msg.isEmpty()) {
252 msg += " ";
253 }
254 msg += String8::format("right(%d isn't %d)", r1.right, r2.right);
255 }
256 if (abs(r1.bottom - r2.bottom) > tolerance) {
257 if (!msg.isEmpty()) {
258 msg += " ";
259 }
260 msg += String8::format("bottom(%d isn't %d)", r1.bottom, r2.bottom);
261 }
262 if (!msg.isEmpty()) {
263 msg += String8::format(" R1: [%d %d %d %d] R2: [%d %d %d %d]",
264 r1.left, r1.top, r1.right, r1.bottom,
265 r2.left, r2.top, r2.right, r2.bottom);
266 fprintf(stderr, "assertRectEq: %s\n", msg.string());
267 return ::testing::AssertionFailure(
268 ::testing::Message(msg.string()));
269 } else {
270 return ::testing::AssertionSuccess();
271 }
272 }
273
Jamie Gennisd99c0882011-03-10 16:24:46 -0800274 int mDisplaySecs;
275 sp<SurfaceComposerClient> mComposerClient;
276 sp<SurfaceControl> mSurfaceControl;
277
278 EGLDisplay mEglDisplay;
279 EGLSurface mEglSurface;
280 EGLContext mEglContext;
Jamie Gennis1876d132011-03-17 16:32:52 -0700281 EGLConfig mGlConfig;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800282};
283
Jamie Gennis74bed552012-03-28 19:05:54 -0700284static void loadShader(GLenum shaderType, const char* pSource,
285 GLuint* outShader) {
286 GLuint shader = glCreateShader(shaderType);
287 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
288 if (shader) {
289 glShaderSource(shader, 1, &pSource, NULL);
290 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
291 glCompileShader(shader);
292 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
293 GLint compiled = 0;
294 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
295 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
296 if (!compiled) {
297 GLint infoLen = 0;
298 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
299 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
300 if (infoLen) {
301 char* buf = (char*) malloc(infoLen);
302 if (buf) {
303 glGetShaderInfoLog(shader, infoLen, NULL, buf);
304 printf("Shader compile log:\n%s\n", buf);
305 free(buf);
306 FAIL();
307 }
308 } else {
309 char* buf = (char*) malloc(0x1000);
310 if (buf) {
311 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
312 printf("Shader compile log:\n%s\n", buf);
313 free(buf);
314 FAIL();
315 }
316 }
317 glDeleteShader(shader);
318 shader = 0;
319 }
320 }
321 ASSERT_TRUE(shader != 0);
322 *outShader = shader;
323}
324
325static void createProgram(const char* pVertexSource,
326 const char* pFragmentSource, GLuint* outPgm) {
327 GLuint vertexShader, fragmentShader;
328 {
329 SCOPED_TRACE("compiling vertex shader");
330 ASSERT_NO_FATAL_FAILURE(loadShader(GL_VERTEX_SHADER, pVertexSource,
331 &vertexShader));
332 }
333 {
334 SCOPED_TRACE("compiling fragment shader");
335 ASSERT_NO_FATAL_FAILURE(loadShader(GL_FRAGMENT_SHADER, pFragmentSource,
336 &fragmentShader));
337 }
338
339 GLuint program = glCreateProgram();
340 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
341 if (program) {
342 glAttachShader(program, vertexShader);
343 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
344 glAttachShader(program, fragmentShader);
345 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
346 glLinkProgram(program);
347 GLint linkStatus = GL_FALSE;
348 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
349 if (linkStatus != GL_TRUE) {
350 GLint bufLength = 0;
351 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
352 if (bufLength) {
353 char* buf = (char*) malloc(bufLength);
354 if (buf) {
355 glGetProgramInfoLog(program, bufLength, NULL, buf);
356 printf("Program link log:\n%s\n", buf);
357 free(buf);
358 FAIL();
359 }
360 }
361 glDeleteProgram(program);
362 program = 0;
363 }
364 }
365 glDeleteShader(vertexShader);
366 glDeleteShader(fragmentShader);
367 ASSERT_TRUE(program != 0);
368 *outPgm = program;
369}
370
371static int abs(int value) {
372 return value > 0 ? value : -value;
373}
374
375
Jamie Gennisd99c0882011-03-10 16:24:46 -0800376// XXX: Code above this point should live elsewhere
377
378class SurfaceTextureGLTest : public GLTest {
379protected:
Jamie Gennis79e31252011-10-19 15:19:19 -0700380 enum { TEX_ID = 123 };
Jamie Gennisd99c0882011-03-10 16:24:46 -0800381
382 virtual void SetUp() {
383 GLTest::SetUp();
Andy McFadden2adaf042012-12-18 09:49:45 -0800384 mST = new GLConsumer(TEX_ID);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800385 mSTC = new Surface(mST->getBufferQueue());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800386 mANW = mSTC;
Jamie Gennis74bed552012-03-28 19:05:54 -0700387 mTextureRenderer = new TextureRenderer(TEX_ID, mST);
388 ASSERT_NO_FATAL_FAILURE(mTextureRenderer->SetUp());
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700389 mFW = new FrameWaiter;
390 mST->setFrameAvailableListener(mFW);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800391 }
392
Jamie Gennis2640bfd2011-07-14 17:11:47 -0700393 virtual void TearDown() {
394 mANW.clear();
395 mSTC.clear();
396 mST.clear();
397 GLTest::TearDown();
398 }
399
Jamie Gennisd99c0882011-03-10 16:24:46 -0800400 void drawTexture() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700401 mTextureRenderer->drawTexture();
Jamie Gennisd99c0882011-03-10 16:24:46 -0800402 }
403
Jamie Gennis74bed552012-03-28 19:05:54 -0700404 class TextureRenderer: public RefBase {
405 public:
Andy McFadden2adaf042012-12-18 09:49:45 -0800406 TextureRenderer(GLuint texName, const sp<GLConsumer>& st):
Jamie Gennis74bed552012-03-28 19:05:54 -0700407 mTexName(texName),
408 mST(st) {
409 }
410
411 void SetUp() {
412 const char vsrc[] =
413 "attribute vec4 vPosition;\n"
414 "varying vec2 texCoords;\n"
415 "uniform mat4 texMatrix;\n"
416 "void main() {\n"
417 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
418 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
419 " gl_Position = vPosition;\n"
420 "}\n";
421
422 const char fsrc[] =
423 "#extension GL_OES_EGL_image_external : require\n"
424 "precision mediump float;\n"
425 "uniform samplerExternalOES texSampler;\n"
426 "varying vec2 texCoords;\n"
427 "void main() {\n"
428 " gl_FragColor = texture2D(texSampler, texCoords);\n"
429 "}\n";
430
431 {
432 SCOPED_TRACE("creating shader program");
433 ASSERT_NO_FATAL_FAILURE(createProgram(vsrc, fsrc, &mPgm));
434 }
435
436 mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
437 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
438 ASSERT_NE(-1, mPositionHandle);
439 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
440 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
441 ASSERT_NE(-1, mTexSamplerHandle);
442 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
443 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
444 ASSERT_NE(-1, mTexMatrixHandle);
445 }
446
Andy McFadden2adaf042012-12-18 09:49:45 -0800447 // drawTexture draws the GLConsumer over the entire GL viewport.
Jamie Gennis74bed552012-03-28 19:05:54 -0700448 void drawTexture() {
Jamie Gennisa96b6bd2012-04-11 17:27:12 -0700449 static const GLfloat triangleVertices[] = {
Jamie Gennis74bed552012-03-28 19:05:54 -0700450 -1.0f, 1.0f,
451 -1.0f, -1.0f,
452 1.0f, -1.0f,
453 1.0f, 1.0f,
454 };
455
456 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
457 triangleVertices);
458 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
459 glEnableVertexAttribArray(mPositionHandle);
460 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
461
462 glUseProgram(mPgm);
463 glUniform1i(mTexSamplerHandle, 0);
464 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
465 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
466 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
467
468 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
469 // they're setting the defautls for that target, but when hacking
470 // things to use GL_TEXTURE_2D they are needed to achieve the same
471 // behavior.
472 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
473 GL_LINEAR);
474 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
475 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
476 GL_LINEAR);
477 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
478 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
479 GL_CLAMP_TO_EDGE);
480 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
481 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
482 GL_CLAMP_TO_EDGE);
483 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
484
485 GLfloat texMatrix[16];
486 mST->getTransformMatrix(texMatrix);
487 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
488
489 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
490 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
491 }
492
493 GLuint mTexName;
Andy McFadden2adaf042012-12-18 09:49:45 -0800494 sp<GLConsumer> mST;
Jamie Gennis74bed552012-03-28 19:05:54 -0700495 GLuint mPgm;
496 GLint mPositionHandle;
497 GLint mTexSamplerHandle;
498 GLint mTexMatrixHandle;
499 };
500
Andy McFadden2adaf042012-12-18 09:49:45 -0800501 class FrameWaiter : public GLConsumer::FrameAvailableListener {
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700502 public:
503 FrameWaiter():
504 mPendingFrames(0) {
505 }
506
507 void waitForFrame() {
508 Mutex::Autolock lock(mMutex);
509 while (mPendingFrames == 0) {
510 mCondition.wait(mMutex);
511 }
512 mPendingFrames--;
513 }
514
515 virtual void onFrameAvailable() {
516 Mutex::Autolock lock(mMutex);
517 mPendingFrames++;
518 mCondition.signal();
519 }
520
521 int mPendingFrames;
522 Mutex mMutex;
523 Condition mCondition;
524 };
525
Andy McFadden2adaf042012-12-18 09:49:45 -0800526 // Note that GLConsumer will lose the notifications
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700527 // onBuffersReleased and onFrameAvailable as there is currently
528 // no way to forward the events. This DisconnectWaiter will not let the
529 // disconnect finish until finishDisconnect() is called. It will
530 // also block until a disconnect is called
531 class DisconnectWaiter : public BufferQueue::ConsumerListener {
532 public:
533 DisconnectWaiter () :
534 mWaitForDisconnect(false),
535 mPendingFrames(0) {
536 }
537
538 void waitForFrame() {
539 Mutex::Autolock lock(mMutex);
540 while (mPendingFrames == 0) {
541 mFrameCondition.wait(mMutex);
542 }
543 mPendingFrames--;
544 }
545
546 virtual void onFrameAvailable() {
547 Mutex::Autolock lock(mMutex);
548 mPendingFrames++;
549 mFrameCondition.signal();
550 }
551
552 virtual void onBuffersReleased() {
553 Mutex::Autolock lock(mMutex);
554 while (!mWaitForDisconnect) {
555 mDisconnectCondition.wait(mMutex);
556 }
557 }
558
559 void finishDisconnect() {
560 Mutex::Autolock lock(mMutex);
561 mWaitForDisconnect = true;
562 mDisconnectCondition.signal();
563 }
564
565 private:
566 Mutex mMutex;
567
568 bool mWaitForDisconnect;
569 Condition mDisconnectCondition;
570
571 int mPendingFrames;
572 Condition mFrameCondition;
573 };
574
Andy McFadden2adaf042012-12-18 09:49:45 -0800575 sp<GLConsumer> mST;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800576 sp<Surface> mSTC;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800577 sp<ANativeWindow> mANW;
Jamie Gennis74bed552012-03-28 19:05:54 -0700578 sp<TextureRenderer> mTextureRenderer;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700579 sp<FrameWaiter> mFW;
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;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700672 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
673 &anb));
Jamie Gennisce561372012-03-19 18:33:05 -0700674 ASSERT_TRUE(anb != NULL);
675
676 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisce561372012-03-19 18:33:05 -0700677
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());
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700683 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer(),
684 -1));
Jamie Gennisce561372012-03-19 18:33:05 -0700685}
686
Jamie Gennisd99c0882011-03-10 16:24:46 -0800687TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700688 const int texWidth = 64;
689 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800690
691 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700692 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800693 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
694 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
695
Iliyan Malchev697526b2011-05-01 11:33:26 -0700696 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700697 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
698 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800699 ASSERT_TRUE(anb != NULL);
700
701 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800702
703 // Fill the buffer with the a checkerboard pattern
704 uint8_t* img = NULL;
705 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700706 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800707 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700708 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
709 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800710
Jamie Gennisd69097f2012-08-30 13:28:23 -0700711 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800712
713 glClearColor(0.2, 0.2, 0.2, 0.2);
714 glClear(GL_COLOR_BUFFER_BIT);
715
Jamie Gennisc8c51522011-06-15 14:24:38 -0700716 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800717 drawTexture();
718
Jamie Gennise6a0f502013-04-05 17:37:32 -0700719 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255, 3));
720 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255, 3));
721 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255, 3));
722 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255, 3));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800723
Jamie Gennise6a0f502013-04-05 17:37:32 -0700724 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255, 3));
725 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255, 3));
726 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255, 3));
727 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255, 3));
728 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255, 3));
729 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255, 3));
730 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255, 3));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800731}
732
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700733TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700734 const int texWidth = 64;
735 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800736
737 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700738 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800739 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
740 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
741
Iliyan Malchev697526b2011-05-01 11:33:26 -0700742 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700743 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
744 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800745 ASSERT_TRUE(anb != NULL);
746
747 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800748
749 // Fill the buffer with the a checkerboard pattern
750 uint8_t* img = NULL;
751 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700752 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800753 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700754 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
755 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800756
Jamie Gennisd69097f2012-08-30 13:28:23 -0700757 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800758
759 glClearColor(0.2, 0.2, 0.2, 0.2);
760 glClear(GL_COLOR_BUFFER_BIT);
761
Jamie Gennisc8c51522011-06-15 14:24:38 -0700762 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800763 drawTexture();
764
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700765 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
766 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800767 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
768 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
769
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700770 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
771 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
772 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
773 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
774 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
775 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
776 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800777}
778
779TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700780 const int texWidth = 64;
781 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800782
783 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700784 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800785 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
786 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
787
788 android_native_rect_t crops[] = {
789 {4, 6, 22, 36},
790 {0, 6, 22, 36},
791 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700792 {4, 6, texWidth, 36},
793 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800794 };
795
796 for (int i = 0; i < 5; i++) {
797 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800798 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
799 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800800
801 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
802
Iliyan Malchev697526b2011-05-01 11:33:26 -0700803 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700804 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
805 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800806 ASSERT_TRUE(anb != NULL);
807
808 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800809
810 uint8_t* img = NULL;
811 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700812 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800813 buf->unlock();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800814 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700815 buf->getNativeBuffer(), -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800816
Jamie Gennisd69097f2012-08-30 13:28:23 -0700817 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800818
819 glClearColor(0.2, 0.2, 0.2, 0.2);
820 glClear(GL_COLOR_BUFFER_BIT);
821
Jamie Gennisc8c51522011-06-15 14:24:38 -0700822 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800823 drawTexture();
824
825 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
826 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
827 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
828 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
829
830 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
831 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
832 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
833 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
834 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
835 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
836 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
837 }
838}
839
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700840// This test is intended to catch synchronization bugs between the CPU-written
841// and GPU-read buffers.
842TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
843 enum { texWidth = 16 };
844 enum { texHeight = 16 };
845 enum { numFrames = 1024 };
846
847 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -0700848 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2));
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700849 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
850 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
851 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
852 GRALLOC_USAGE_SW_WRITE_OFTEN));
853
854 struct TestPixel {
855 int x;
856 int y;
857 };
858 const TestPixel testPixels[] = {
859 { 4, 11 },
860 { 12, 14 },
861 { 7, 2 },
862 };
863 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
864
865 class ProducerThread : public Thread {
866 public:
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800867 ProducerThread(const sp<ANativeWindow>& anw,
868 const TestPixel* testPixels):
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700869 mANW(anw),
870 mTestPixels(testPixels) {
871 }
872
873 virtual ~ProducerThread() {
874 }
875
876 virtual bool threadLoop() {
877 for (int i = 0; i < numFrames; i++) {
878 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700879 if (native_window_dequeue_buffer_and_wait(mANW.get(),
880 &anb) != NO_ERROR) {
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700881 return false;
882 }
883 if (anb == NULL) {
884 return false;
885 }
886
887 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700888
889 const int yuvTexOffsetY = 0;
890 int stride = buf->getStride();
891 int yuvTexStrideY = stride;
892 int yuvTexOffsetV = yuvTexStrideY * texHeight;
893 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
894 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
895 int yuvTexStrideU = yuvTexStrideV;
896
897 uint8_t* img = NULL;
898 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
899
900 // Gray out all the test pixels first, so we're more likely to
901 // see a failure if GL is still texturing from the buffer we
902 // just dequeued.
903 for (int j = 0; j < numTestPixels; j++) {
904 int x = mTestPixels[j].x;
905 int y = mTestPixels[j].y;
906 uint8_t value = 128;
907 img[y*stride + x] = value;
908 }
909
910 // Fill the buffer with gray.
911 for (int y = 0; y < texHeight; y++) {
912 for (int x = 0; x < texWidth; x++) {
913 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
914 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
915 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
916 }
917 }
918
919 // Set the test pixels to either white or black.
920 for (int j = 0; j < numTestPixels; j++) {
921 int x = mTestPixels[j].x;
922 int y = mTestPixels[j].y;
923 uint8_t value = 0;
924 if (j == (i % numTestPixels)) {
925 value = 255;
926 }
927 img[y*stride + x] = value;
928 }
929
930 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700931 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), -1)
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700932 != NO_ERROR) {
933 return false;
934 }
935 }
936 return false;
937 }
938
939 sp<ANativeWindow> mANW;
940 const TestPixel* mTestPixels;
941 };
942
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700943 sp<Thread> pt(new ProducerThread(mANW, testPixels));
944 pt->run();
945
946 glViewport(0, 0, texWidth, texHeight);
947
948 glClearColor(0.2, 0.2, 0.2, 0.2);
949 glClear(GL_COLOR_BUFFER_BIT);
950
951 // We wait for the first two frames up front so that the producer will be
952 // likely to dequeue the buffer that's currently being textured from.
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700953 mFW->waitForFrame();
954 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700955
956 for (int i = 0; i < numFrames; i++) {
957 SCOPED_TRACE(String8::format("frame %d", i).string());
958
959 // We must wait for each frame to come in because if we ever do an
960 // updateTexImage call that doesn't consume a newly available buffer
961 // then the producer and consumer will get out of sync, which will cause
962 // a deadlock.
963 if (i > 1) {
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700964 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700965 }
Jamie Gennisd69097f2012-08-30 13:28:23 -0700966 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700967 drawTexture();
968
969 for (int j = 0; j < numTestPixels; j++) {
970 int x = testPixels[j].x;
971 int y = testPixels[j].y;
972 uint8_t value = 0;
973 if (j == (i % numTestPixels)) {
974 // We must y-invert the texture coords
975 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
976 } else {
977 // We must y-invert the texture coords
978 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
979 }
980 }
981 }
982
983 pt->requestExitAndWait();
984}
985
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700986TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700987 const int texWidth = 64;
988 const int texHeight = 66;
989
990 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
991 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
992 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
993 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
994
Jamie Gennisce561372012-03-19 18:33:05 -0700995 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -0700996
Jamie Gennisd69097f2012-08-30 13:28:23 -0700997 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis1876d132011-03-17 16:32:52 -0700998
999 glClearColor(0.2, 0.2, 0.2, 0.2);
1000 glClear(GL_COLOR_BUFFER_BIT);
1001
Jamie Gennisc8c51522011-06-15 14:24:38 -07001002 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001003 drawTexture();
1004
1005 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
1006 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001007 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
1008 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001009
1010 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001011 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001012 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001013 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
1014 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001015 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001016 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001017 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
1018 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
1019 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001020 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001021 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
1022 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001023 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001024 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001025 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
1026}
1027
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001028TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -07001029 const int texWidth = 64;
1030 const int texHeight = 64;
1031
1032 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
1033 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
1034 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
1035 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
1036
Jamie Gennisce561372012-03-19 18:33:05 -07001037 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -07001038
Jamie Gennisd69097f2012-08-30 13:28:23 -07001039 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis1876d132011-03-17 16:32:52 -07001040
1041 glClearColor(0.2, 0.2, 0.2, 0.2);
1042 glClear(GL_COLOR_BUFFER_BIT);
1043
Jamie Gennisc8c51522011-06-15 14:24:38 -07001044 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001045 drawTexture();
1046
1047 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
1048 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
1049 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
1050 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
1051
1052 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
1053 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
1054 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
1055 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
1056 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
1057 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
1058 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
1059 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
1060 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
1061 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
1062 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
1063 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
1064 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
1065 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
1066 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
1067 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
1068}
1069
Andy McFadden2adaf042012-12-18 09:49:45 -08001070// Tests if GLConsumer and BufferQueue are robust enough
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001071// to handle a special case where updateTexImage is called
1072// in the middle of disconnect. This ordering is enforced
1073// by blocking in the disconnect callback.
1074TEST_F(SurfaceTextureGLTest, DisconnectStressTest) {
1075
1076 class ProducerThread : public Thread {
1077 public:
1078 ProducerThread(const sp<ANativeWindow>& anw):
1079 mANW(anw) {
1080 }
1081
1082 virtual ~ProducerThread() {
1083 }
1084
1085 virtual bool threadLoop() {
1086 ANativeWindowBuffer* anb;
1087
1088 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL);
1089
1090 for (int numFrames =0 ; numFrames < 2; numFrames ++) {
1091
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001092 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1093 &anb) != NO_ERROR) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001094 return false;
1095 }
1096 if (anb == NULL) {
1097 return false;
1098 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001099 if (mANW->queueBuffer(mANW.get(), anb, -1)
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001100 != NO_ERROR) {
1101 return false;
1102 }
1103 }
1104
1105 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_EGL);
1106
1107 return false;
1108 }
1109
1110 private:
1111 sp<ANativeWindow> mANW;
1112 };
1113
1114 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1115
1116 sp<DisconnectWaiter> dw(new DisconnectWaiter());
1117 mST->getBufferQueue()->consumerConnect(dw);
1118
1119
1120 sp<Thread> pt(new ProducerThread(mANW));
1121 pt->run();
1122
Andy McFadden2adaf042012-12-18 09:49:45 -08001123 // eat a frame so GLConsumer will own an at least one slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001124 dw->waitForFrame();
1125 EXPECT_EQ(OK,mST->updateTexImage());
1126
1127 dw->waitForFrame();
Andy McFadden2adaf042012-12-18 09:49:45 -08001128 // Could fail here as GLConsumer thinks it still owns the slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001129 // but bufferQueue has released all slots
1130 EXPECT_EQ(OK,mST->updateTexImage());
1131
1132 dw->finishDisconnect();
1133}
1134
1135
Andy McFadden2adaf042012-12-18 09:49:45 -08001136// This test ensures that the GLConsumer clears the mCurrentTexture
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001137// when it is disconnected and reconnected. Otherwise it will
1138// attempt to release a buffer that it does not owned
1139TEST_F(SurfaceTextureGLTest, DisconnectClearsCurrentTexture) {
1140 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1141
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001142 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1143 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001144
1145 ANativeWindowBuffer *anb;
1146
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001147 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1148 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001149
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001150 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1151 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001152
1153 EXPECT_EQ(OK,mST->updateTexImage());
1154 EXPECT_EQ(OK,mST->updateTexImage());
1155
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001156 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1157 NATIVE_WINDOW_API_EGL));
1158 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1159 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001160
1161 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1162
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001163 EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1164 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001165
1166 // Will fail here if mCurrentTexture is not cleared properly
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001167 mFW->waitForFrame();
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001168 EXPECT_EQ(OK,mST->updateTexImage());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001169
1170 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1171 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001172}
1173
Daniel Lam016c8cb2012-04-03 15:54:58 -07001174TEST_F(SurfaceTextureGLTest, ScaleToWindowMode) {
1175 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1176
1177 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1178 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));
1179
1180 // The producer image size
1181 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1182
1183 // The consumer image size (16 x 9) ratio
1184 mST->setDefaultBufferSize(1280, 720);
1185
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001186 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1187 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001188
1189 ANativeWindowBuffer *anb;
1190
1191 android_native_rect_t odd = {23, 78, 123, 477};
1192 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &odd));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001193 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1194 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001195 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001196 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001197 Rect r = mST->getCurrentCrop();
1198 assertRectEq(Rect(23, 78, 123, 477), r);
1199
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001200 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1201 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001202}
1203
1204// This test ensures the scaling mode does the right thing
1205// ie NATIVE_WINDOW_SCALING_MODE_CROP should crop
1206// the image such that it has the same aspect ratio as the
1207// default buffer size
1208TEST_F(SurfaceTextureGLTest, CroppedScalingMode) {
1209 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1210
1211 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1212 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP));
1213
1214 // The producer image size
1215 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1216
1217 // The consumer image size (16 x 9) ratio
1218 mST->setDefaultBufferSize(1280, 720);
1219
1220 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU);
1221
1222 ANativeWindowBuffer *anb;
1223
1224 // The crop is in the shape of (320, 180) === 16 x 9
1225 android_native_rect_t standard = {10, 20, 330, 200};
1226 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &standard));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001227 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1228 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001229 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001230 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001231 Rect r = mST->getCurrentCrop();
1232 // crop should be the same as crop (same aspect ratio)
1233 assertRectEq(Rect(10, 20, 330, 200), r);
1234
1235 // make this wider then desired aspect 239 x 100 (2.39:1)
1236 android_native_rect_t wide = {20, 30, 259, 130};
1237 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &wide));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001238 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1239 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001240 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001241 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001242 r = mST->getCurrentCrop();
1243 // crop should be the same height, but have cropped left and right borders
1244 // offset is 30.6 px L+, R-
1245 assertRectEq(Rect(51, 30, 228, 130), r);
1246
1247 // This image is taller then desired aspect 400 x 300 (4:3)
1248 android_native_rect_t narrow = {0, 0, 400, 300};
1249 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &narrow));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001250 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1251 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001252 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001253 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001254 r = mST->getCurrentCrop();
1255 // crop should be the same width, but have cropped top and bottom borders
1256 // offset is 37.5 px
1257 assertRectEq(Rect(0, 37, 400, 262), r);
1258
1259 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
1260}
1261
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001262TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
1263 class ProducerThread : public Thread {
1264 public:
1265 ProducerThread(const sp<ANativeWindow>& anw):
1266 mANW(anw),
1267 mDequeueError(NO_ERROR) {
1268 }
1269
1270 virtual ~ProducerThread() {
1271 }
1272
1273 virtual bool threadLoop() {
1274 Mutex::Autolock lock(mMutex);
1275 ANativeWindowBuffer* anb;
1276
1277 // Frame 1
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001278 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1279 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001280 return false;
1281 }
1282 if (anb == NULL) {
1283 return false;
1284 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001285 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001286 != NO_ERROR) {
1287 return false;
1288 }
1289
1290 // Frame 2
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001291 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1292 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001293 return false;
1294 }
1295 if (anb == NULL) {
1296 return false;
1297 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001298 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001299 != NO_ERROR) {
1300 return false;
1301 }
1302
1303 // Frame 3 - error expected
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001304 mDequeueError = native_window_dequeue_buffer_and_wait(mANW.get(),
1305 &anb);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001306 return false;
1307 }
1308
1309 status_t getDequeueError() {
1310 Mutex::Autolock lock(mMutex);
1311 return mDequeueError;
1312 }
1313
1314 private:
1315 sp<ANativeWindow> mANW;
1316 status_t mDequeueError;
1317 Mutex mMutex;
1318 };
1319
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001320 ASSERT_EQ(OK, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -07001321 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001322
1323 sp<Thread> pt(new ProducerThread(mANW));
1324 pt->run();
1325
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001326 mFW->waitForFrame();
1327 mFW->waitForFrame();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001328
1329 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1330 // block waiting for a buffer to become available.
1331 usleep(100000);
1332
1333 mST->abandon();
1334
1335 pt->requestExitAndWait();
1336 ASSERT_EQ(NO_INIT,
1337 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1338}
1339
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001340TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1341 int texHeight = 16;
1342 ANativeWindowBuffer* anb;
1343
1344 GLint maxTextureSize;
1345 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1346
1347 // make sure it works with small textures
1348 mST->setDefaultBufferSize(16, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001349 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1350 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001351 EXPECT_EQ(16, anb->width);
1352 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001353 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001354 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1355
1356 // make sure it works with GL_MAX_TEXTURE_SIZE
1357 mST->setDefaultBufferSize(maxTextureSize, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001358 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1359 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001360 EXPECT_EQ(maxTextureSize, anb->width);
1361 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001362 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001363 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1364
1365 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1366 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001367 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1368 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001369 EXPECT_EQ(maxTextureSize+1, anb->width);
1370 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001371 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001372 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1373}
1374
Jamie Gennis5451d152011-06-08 09:40:45 -07001375/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001376 * This test fixture is for testing GL -> GL texture streaming. It creates an
1377 * EGLSurface and an EGLContext for the image producer to use.
1378 */
1379class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1380protected:
1381 SurfaceTextureGLToGLTest():
1382 mProducerEglSurface(EGL_NO_SURFACE),
1383 mProducerEglContext(EGL_NO_CONTEXT) {
1384 }
1385
1386 virtual void SetUp() {
1387 SurfaceTextureGLTest::SetUp();
1388
Jamie Gennisce561372012-03-19 18:33:05 -07001389 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001390 mANW.get(), NULL);
1391 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1392 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1393
Jamie Gennisce561372012-03-19 18:33:05 -07001394 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001395 EGL_NO_CONTEXT, getContextAttribs());
1396 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1397 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1398 }
1399
1400 virtual void TearDown() {
1401 if (mProducerEglContext != EGL_NO_CONTEXT) {
1402 eglDestroyContext(mEglDisplay, mProducerEglContext);
1403 }
1404 if (mProducerEglSurface != EGL_NO_SURFACE) {
1405 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1406 }
1407 SurfaceTextureGLTest::TearDown();
1408 }
1409
1410 EGLSurface mProducerEglSurface;
1411 EGLContext mProducerEglContext;
1412};
1413
Andy McFadden71f683a2012-09-14 17:21:46 -07001414TEST_F(SurfaceTextureGLToGLTest, TransformHintGetsRespected) {
1415 const uint32_t texWidth = 32;
1416 const uint32_t texHeight = 64;
1417
1418 mST->setDefaultBufferSize(texWidth, texHeight);
1419 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1420
1421 // This test requires 3 buffers to avoid deadlock because we're
1422 // both producer and consumer, and only using one thread.
1423 mST->setDefaultMaxBufferCount(3);
1424
1425 // Do the producer side of things
1426 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1427 mProducerEglSurface, mProducerEglContext));
1428 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1429
1430 // Start a buffer with our chosen size and transform hint moving
1431 // through the system.
1432 glClear(GL_COLOR_BUFFER_BIT); // give the driver something to do
1433 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1434 mST->updateTexImage(); // consume it
1435 // Swap again.
1436 glClear(GL_COLOR_BUFFER_BIT);
1437 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1438 mST->updateTexImage();
1439
1440 // The current buffer should either show the effects of the transform
1441 // hint (in the form of an inverse transform), or show that the
1442 // transform hint has been ignored.
1443 sp<GraphicBuffer> buf = mST->getCurrentBuffer();
1444 if (mST->getCurrentTransform() == NATIVE_WINDOW_TRANSFORM_ROT_270) {
1445 ASSERT_EQ(texWidth, buf->getHeight());
1446 ASSERT_EQ(texHeight, buf->getWidth());
1447 } else {
1448 ASSERT_EQ(texWidth, buf->getWidth());
1449 ASSERT_EQ(texHeight, buf->getHeight());
1450 }
1451
1452 // Reset the transform hint and confirm that it takes.
1453 mST->setTransformHint(0);
1454 glClear(GL_COLOR_BUFFER_BIT);
1455 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1456 mST->updateTexImage();
1457 glClear(GL_COLOR_BUFFER_BIT);
1458 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1459 mST->updateTexImage();
1460
1461 buf = mST->getCurrentBuffer();
1462 ASSERT_EQ((uint32_t) 0, mST->getCurrentTransform());
1463 ASSERT_EQ(texWidth, buf->getWidth());
1464 ASSERT_EQ(texHeight, buf->getHeight());
1465}
1466
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001467TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1468 const int texWidth = 64;
1469 const int texHeight = 64;
1470
1471 mST->setDefaultBufferSize(texWidth, texHeight);
1472
Jamie Gennis46975282012-08-30 18:35:50 -07001473 // This test requires 3 buffers to complete run on a single thread.
1474 mST->setDefaultMaxBufferCount(3);
1475
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001476 // Do the producer side of things
1477 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1478 mProducerEglSurface, mProducerEglContext));
1479 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1480
1481 // This is needed to ensure we pick up a buffer of the correct size.
1482 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1483
1484 glClearColor(0.6, 0.6, 0.6, 0.6);
1485 glClear(GL_COLOR_BUFFER_BIT);
1486
1487 glEnable(GL_SCISSOR_TEST);
1488 glScissor(4, 4, 4, 4);
1489 glClearColor(1.0, 0.0, 0.0, 1.0);
1490 glClear(GL_COLOR_BUFFER_BIT);
1491
1492 glScissor(24, 48, 4, 4);
1493 glClearColor(0.0, 1.0, 0.0, 1.0);
1494 glClear(GL_COLOR_BUFFER_BIT);
1495
1496 glScissor(37, 17, 4, 4);
1497 glClearColor(0.0, 0.0, 1.0, 1.0);
1498 glClear(GL_COLOR_BUFFER_BIT);
1499
1500 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1501
1502 // Do the consumer side of things
1503 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1504 mEglContext));
1505 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1506
1507 glDisable(GL_SCISSOR_TEST);
1508
Jamie Gennisd69097f2012-08-30 13:28:23 -07001509 // Skip the first frame, which was empty
1510 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1511 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001512
1513 glClearColor(0.2, 0.2, 0.2, 0.2);
1514 glClear(GL_COLOR_BUFFER_BIT);
1515
1516 glViewport(0, 0, texWidth, texHeight);
1517 drawTexture();
1518
1519 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1520 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1521 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1522 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1523
1524 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1525 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1526 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1527 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1528 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1529 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1530 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1531 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1532 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1533 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1534 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1535 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1536 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1537 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1538 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1539 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1540}
1541
1542TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001543 sp<GraphicBuffer> buffers[2];
1544
Jamie Gennise3603d72011-11-19 21:20:17 -08001545 // This test requires async mode to run on a single thread.
1546 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1547 mProducerEglSurface, mProducerEglContext));
1548 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1549 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1550 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1551
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001552 for (int i = 0; i < 2; i++) {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001553 // Produce a frame
1554 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1555 mProducerEglSurface, mProducerEglContext));
1556 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1557 glClear(GL_COLOR_BUFFER_BIT);
1558 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1559
1560 // Consume a frame
1561 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1562 mEglContext));
1563 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001564 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001565 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001566 buffers[i] = mST->getCurrentBuffer();
1567 }
1568
1569 // Destroy the GL texture object to release its ref on buffers[2].
1570 GLuint texID = TEX_ID;
1571 glDeleteTextures(1, &texID);
1572
1573 // Destroy the EGLSurface
1574 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1575 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001576 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001577
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001578 // This test should have the only reference to buffer 0.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001579 EXPECT_EQ(1, buffers[0]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001580
Andy McFadden2adaf042012-12-18 09:49:45 -08001581 // The GLConsumer should hold a single reference to buffer 1 in its
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001582 // mCurrentBuffer member. All of the references in the slots should have
1583 // been released.
1584 EXPECT_EQ(2, buffers[1]->getStrongCount());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001585}
1586
1587TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1588 sp<GraphicBuffer> buffers[3];
1589
Jamie Gennise3603d72011-11-19 21:20:17 -08001590 // This test requires async mode to run on a single thread.
1591 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1592 mProducerEglSurface, mProducerEglContext));
1593 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1594 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1595 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1596
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001597 for (int i = 0; i < 3; i++) {
1598 // Produce a frame
1599 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1600 mProducerEglSurface, mProducerEglContext));
1601 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1602 glClear(GL_COLOR_BUFFER_BIT);
1603 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1604 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1605
1606 // Consume a frame
1607 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1608 mEglContext));
1609 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001610 mFW->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001611 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1612 buffers[i] = mST->getCurrentBuffer();
1613 }
1614
Andy McFadden2adaf042012-12-18 09:49:45 -08001615 // Abandon the GLConsumer, releasing the ref that the GLConsumer has
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001616 // on buffers[2].
1617 mST->abandon();
1618
1619 // Destroy the GL texture object to release its ref on buffers[2].
1620 GLuint texID = TEX_ID;
1621 glDeleteTextures(1, &texID);
1622
1623 // Destroy the EGLSurface.
1624 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1625 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001626 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001627
1628 EXPECT_EQ(1, buffers[0]->getStrongCount());
1629 EXPECT_EQ(1, buffers[1]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001630
1631 // Depending on how lazily the GL driver dequeues buffers, we may end up
1632 // with either two or three total buffers. If there are three, make sure
1633 // the last one was properly down-ref'd.
1634 if (buffers[2] != buffers[0]) {
1635 EXPECT_EQ(1, buffers[2]->getStrongCount());
1636 }
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001637}
1638
Mathias Agopian7589b2a2013-03-08 13:18:52 -08001639TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentBeforeConsumerDeathUnrefsBuffers) {
1640 sp<GraphicBuffer> buffer;
1641
1642 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1643 mProducerEglSurface, mProducerEglContext));
1644
1645 // Produce a frame
1646 glClear(GL_COLOR_BUFFER_BIT);
1647 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1648 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1649
1650 // Destroy the EGLSurface.
1651 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1652 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1653 mProducerEglSurface = EGL_NO_SURFACE;
1654 mSTC.clear();
1655 mANW.clear();
1656 mTextureRenderer.clear();
1657
1658 // Consume a frame
1659 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1660 buffer = mST->getCurrentBuffer();
1661
1662 // Destroy the GL texture object to release its ref
1663 GLuint texID = TEX_ID;
1664 glDeleteTextures(1, &texID);
1665
1666 // make un-current, all references to buffer should be gone
1667 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
1668 EGL_NO_SURFACE, EGL_NO_CONTEXT));
1669
1670 // Destroy consumer
1671 mST.clear();
1672
1673 EXPECT_EQ(1, buffer->getStrongCount());
1674}
1675
1676TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentAfterConsumerDeathUnrefsBuffers) {
1677 sp<GraphicBuffer> buffer;
1678
1679 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1680 mProducerEglSurface, mProducerEglContext));
1681
1682 // Produce a frame
1683 glClear(GL_COLOR_BUFFER_BIT);
1684 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1685 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1686
1687 // Destroy the EGLSurface.
1688 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1689 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1690 mProducerEglSurface = EGL_NO_SURFACE;
1691 mSTC.clear();
1692 mANW.clear();
1693 mTextureRenderer.clear();
1694
1695 // Consume a frame
1696 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1697 buffer = mST->getCurrentBuffer();
1698
1699 // Destroy the GL texture object to release its ref
1700 GLuint texID = TEX_ID;
1701 glDeleteTextures(1, &texID);
1702
1703 // Destroy consumer
1704 mST.clear();
1705
1706 // make un-current, all references to buffer should be gone
1707 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
1708 EGL_NO_SURFACE, EGL_NO_CONTEXT));
1709
1710 EXPECT_EQ(1, buffer->getStrongCount());
1711}
1712
1713
Jamie Gennis59769462011-11-19 18:04:43 -08001714TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) {
1715 // This test requires 3 buffers to run on a single thread.
Jamie Gennis31a353d2012-08-24 17:25:13 -07001716 mST->setDefaultMaxBufferCount(3);
Jamie Gennis59769462011-11-19 18:04:43 -08001717
1718 ASSERT_TRUE(mST->isSynchronousMode());
1719
1720 for (int i = 0; i < 10; i++) {
1721 // Produce a frame
1722 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1723 mProducerEglSurface, mProducerEglContext));
1724 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1725 glClear(GL_COLOR_BUFFER_BIT);
1726 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1727 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1728
1729 // Consume a frame
1730 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1731 mEglContext));
1732 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1733 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1734 }
1735
1736 ASSERT_TRUE(mST->isSynchronousMode());
1737}
1738
Jamie Gennisc2c38022012-04-11 17:20:03 -07001739TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
1740 enum { texWidth = 64 };
1741 enum { texHeight = 64 };
1742
Jamie Gennis46975282012-08-30 18:35:50 -07001743 // This test requires 3 buffers to complete run on a single thread.
1744 mST->setDefaultMaxBufferCount(3);
1745
Jamie Gennisc2c38022012-04-11 17:20:03 -07001746 // Set the user buffer size.
1747 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1748
1749 // Do the producer side of things
1750 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1751 mProducerEglSurface, mProducerEglContext));
1752 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1753
1754 // This is needed to ensure we pick up a buffer of the correct size.
1755 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1756
1757 glClearColor(0.6, 0.6, 0.6, 0.6);
1758 glClear(GL_COLOR_BUFFER_BIT);
1759
1760 glEnable(GL_SCISSOR_TEST);
1761 glScissor(4, 4, 1, 1);
1762 glClearColor(1.0, 0.0, 0.0, 1.0);
1763 glClear(GL_COLOR_BUFFER_BIT);
1764
1765 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1766
1767 // Do the consumer side of things
1768 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1769 mEglContext));
1770 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1771
1772 glDisable(GL_SCISSOR_TEST);
1773
Jamie Gennisd69097f2012-08-30 13:28:23 -07001774 // Skip the first frame, which was empty
1775 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1776 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001777
1778 glClearColor(0.2, 0.2, 0.2, 0.2);
1779 glClear(GL_COLOR_BUFFER_BIT);
1780
1781 glViewport(0, 0, texWidth, texHeight);
1782 drawTexture();
1783
1784 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1785 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1786 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1787 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1788
1789 EXPECT_TRUE(checkPixel( 4, 4, 255, 0, 0, 255));
1790 EXPECT_TRUE(checkPixel( 5, 5, 153, 153, 153, 153));
1791 EXPECT_TRUE(checkPixel( 3, 3, 153, 153, 153, 153));
1792 EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153));
1793 EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153));
1794}
1795
1796TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
1797 enum { texWidth = 64 };
1798 enum { texHeight = 16 };
1799
Jamie Gennis46975282012-08-30 18:35:50 -07001800 // This test requires 3 buffers to complete run on a single thread.
1801 mST->setDefaultMaxBufferCount(3);
1802
Jamie Gennisc2c38022012-04-11 17:20:03 -07001803 // Set the transform hint.
1804 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1805
1806 // Set the user buffer size.
1807 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1808
1809 // Do the producer side of things
1810 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1811 mProducerEglSurface, mProducerEglContext));
1812 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1813
1814 // This is needed to ensure we pick up a buffer of the correct size and the
1815 // new rotation hint.
1816 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1817
1818 glClearColor(0.6, 0.6, 0.6, 0.6);
1819 glClear(GL_COLOR_BUFFER_BIT);
1820
1821 glEnable(GL_SCISSOR_TEST);
1822 glScissor(24, 4, 1, 1);
1823 glClearColor(1.0, 0.0, 0.0, 1.0);
1824 glClear(GL_COLOR_BUFFER_BIT);
1825
1826 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1827
1828 // Do the consumer side of things
1829 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1830 mEglContext));
1831 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1832
1833 glDisable(GL_SCISSOR_TEST);
1834
Jamie Gennisd69097f2012-08-30 13:28:23 -07001835 // Skip the first frame, which was empty
1836 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1837 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001838
1839 glClearColor(0.2, 0.2, 0.2, 0.2);
1840 glClear(GL_COLOR_BUFFER_BIT);
1841
1842 glViewport(0, 0, texWidth, texHeight);
1843 drawTexture();
1844
1845 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1846 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1847 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1848 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1849
1850 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1851 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1852 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1853 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1854 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1855}
1856
1857TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
1858 enum { texWidth = 64 };
1859 enum { texHeight = 16 };
1860
Jamie Gennis46975282012-08-30 18:35:50 -07001861 // This test requires 3 buffers to complete run on a single thread.
1862 mST->setDefaultMaxBufferCount(3);
1863
Jamie Gennisc2c38022012-04-11 17:20:03 -07001864 // Set the transform hint.
1865 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1866
1867 // Set the default buffer size.
1868 mST->setDefaultBufferSize(texWidth, texHeight);
1869
1870 // Do the producer side of things
1871 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1872 mProducerEglSurface, mProducerEglContext));
1873 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1874
1875 // This is needed to ensure we pick up a buffer of the correct size and the
1876 // new rotation hint.
1877 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1878
1879 glClearColor(0.6, 0.6, 0.6, 0.6);
1880 glClear(GL_COLOR_BUFFER_BIT);
1881
1882 glEnable(GL_SCISSOR_TEST);
1883 glScissor(24, 4, 1, 1);
1884 glClearColor(1.0, 0.0, 0.0, 1.0);
1885 glClear(GL_COLOR_BUFFER_BIT);
1886
1887 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1888
1889 // Do the consumer side of things
1890 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1891 mEglContext));
1892 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1893
1894 glDisable(GL_SCISSOR_TEST);
1895
Jamie Gennisd69097f2012-08-30 13:28:23 -07001896 // Skip the first frame, which was empty
1897 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1898 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001899
1900 glClearColor(0.2, 0.2, 0.2, 0.2);
1901 glClear(GL_COLOR_BUFFER_BIT);
1902
1903 glViewport(0, 0, texWidth, texHeight);
1904 drawTexture();
1905
1906 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1907 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1908 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1909 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1910
1911 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1912 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1913 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1914 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1915 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1916}
1917
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001918/*
1919 * This test fixture is for testing GL -> GL texture streaming from one thread
1920 * to another. It contains functionality to create a producer thread that will
1921 * perform GL rendering to an ANativeWindow that feeds frames to a
Andy McFadden2adaf042012-12-18 09:49:45 -08001922 * GLConsumer. Additionally it supports interlocking the producer and
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001923 * consumer threads so that a specific sequence of calls can be
1924 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07001925 *
1926 * The intended usage is as follows:
1927 *
1928 * TEST_F(...) {
1929 * class PT : public ProducerThread {
1930 * virtual void render() {
1931 * ...
1932 * swapBuffers();
1933 * }
1934 * };
1935 *
1936 * runProducerThread(new PT());
1937 *
1938 * // The order of these calls will vary from test to test and may include
1939 * // multiple frames and additional operations (e.g. GL rendering from the
1940 * // texture).
1941 * fc->waitForFrame();
1942 * mST->updateTexImage();
1943 * fc->finishFrame();
1944 * }
1945 *
1946 */
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001947class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07001948protected:
1949
1950 // ProducerThread is an abstract base class to simplify the creation of
1951 // OpenGL ES frame producer threads.
1952 class ProducerThread : public Thread {
1953 public:
1954 virtual ~ProducerThread() {
1955 }
1956
1957 void setEglObjects(EGLDisplay producerEglDisplay,
1958 EGLSurface producerEglSurface,
1959 EGLContext producerEglContext) {
1960 mProducerEglDisplay = producerEglDisplay;
1961 mProducerEglSurface = producerEglSurface;
1962 mProducerEglContext = producerEglContext;
1963 }
1964
1965 virtual bool threadLoop() {
1966 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
1967 mProducerEglSurface, mProducerEglContext);
1968 render();
1969 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1970 EGL_NO_CONTEXT);
1971 return false;
1972 }
1973
1974 protected:
1975 virtual void render() = 0;
1976
1977 void swapBuffers() {
1978 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
1979 }
1980
1981 EGLDisplay mProducerEglDisplay;
1982 EGLSurface mProducerEglSurface;
1983 EGLContext mProducerEglContext;
1984 };
1985
1986 // FrameCondition is a utility class for interlocking between the producer
1987 // and consumer threads. The FrameCondition object should be created and
1988 // destroyed in the consumer thread only. The consumer thread should set
Andy McFadden2adaf042012-12-18 09:49:45 -08001989 // the FrameCondition as the FrameAvailableListener of the GLConsumer,
Jamie Gennis5451d152011-06-08 09:40:45 -07001990 // and should call both waitForFrame and finishFrame once for each expected
1991 // frame.
1992 //
1993 // This interlocking relies on the fact that onFrameAvailable gets called
Andy McFadden2adaf042012-12-18 09:49:45 -08001994 // synchronously from GLConsumer::queueBuffer.
1995 class FrameCondition : public GLConsumer::FrameAvailableListener {
Jamie Gennis5451d152011-06-08 09:40:45 -07001996 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001997 FrameCondition():
1998 mFrameAvailable(false),
1999 mFrameFinished(false) {
2000 }
2001
Jamie Gennis5451d152011-06-08 09:40:45 -07002002 // waitForFrame waits for the next frame to arrive. This should be
2003 // called from the consumer thread once for every frame expected by the
2004 // test.
2005 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07002006 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01002007 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002008 while (!mFrameAvailable) {
2009 mFrameAvailableCondition.wait(mMutex);
2010 }
2011 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01002012 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07002013 }
2014
2015 // Allow the producer to return from its swapBuffers call and continue
2016 // on to produce the next frame. This should be called by the consumer
2017 // thread once for every frame expected by the test.
2018 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07002019 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01002020 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002021 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07002022 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01002023 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07002024 }
2025
Andy McFadden2adaf042012-12-18 09:49:45 -08002026 // This should be called by GLConsumer on the producer thread.
Jamie Gennis5451d152011-06-08 09:40:45 -07002027 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07002028 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01002029 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002030 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07002031 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002032 while (!mFrameFinished) {
2033 mFrameFinishCondition.wait(mMutex);
2034 }
2035 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01002036 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07002037 }
2038
2039 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002040 bool mFrameAvailable;
2041 bool mFrameFinished;
2042
Jamie Gennis5451d152011-06-08 09:40:45 -07002043 Mutex mMutex;
2044 Condition mFrameAvailableCondition;
2045 Condition mFrameFinishCondition;
2046 };
2047
Jamie Gennis5451d152011-06-08 09:40:45 -07002048 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002049 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07002050 mFC = new FrameCondition();
2051 mST->setFrameAvailableListener(mFC);
2052 }
2053
2054 virtual void TearDown() {
2055 if (mProducerThread != NULL) {
2056 mProducerThread->requestExitAndWait();
2057 }
Jamie Gennis5451d152011-06-08 09:40:45 -07002058 mProducerThread.clear();
2059 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002060 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07002061 }
2062
2063 void runProducerThread(const sp<ProducerThread> producerThread) {
2064 ASSERT_TRUE(mProducerThread == NULL);
2065 mProducerThread = producerThread;
2066 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
2067 mProducerEglContext);
2068 producerThread->run();
2069 }
2070
Jamie Gennis5451d152011-06-08 09:40:45 -07002071 sp<ProducerThread> mProducerThread;
2072 sp<FrameCondition> mFC;
2073};
2074
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002075TEST_F(SurfaceTextureGLThreadToGLTest,
2076 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002077 class PT : public ProducerThread {
2078 virtual void render() {
2079 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2080 glClear(GL_COLOR_BUFFER_BIT);
2081 swapBuffers();
2082 }
2083 };
2084
2085 runProducerThread(new PT());
2086
2087 mFC->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07002088 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07002089 mFC->finishFrame();
2090
2091 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08002092}
Jamie Gennis5451d152011-06-08 09:40:45 -07002093
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002094TEST_F(SurfaceTextureGLThreadToGLTest,
2095 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002096 class PT : public ProducerThread {
2097 virtual void render() {
2098 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2099 glClear(GL_COLOR_BUFFER_BIT);
2100 swapBuffers();
2101 }
2102 };
2103
2104 runProducerThread(new PT());
2105
2106 mFC->waitForFrame();
2107 mFC->finishFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07002108 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07002109
2110 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2111}
2112
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002113TEST_F(SurfaceTextureGLThreadToGLTest,
2114 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002115 enum { NUM_ITERATIONS = 1024 };
2116
2117 class PT : public ProducerThread {
2118 virtual void render() {
2119 for (int i = 0; i < NUM_ITERATIONS; i++) {
2120 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2121 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002122 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002123 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002124 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002125 }
2126 }
2127 };
2128
2129 runProducerThread(new PT());
2130
2131 for (int i = 0; i < NUM_ITERATIONS; i++) {
2132 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01002133 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002134 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002135 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002136 mFC->finishFrame();
2137
2138 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2139 }
2140}
2141
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002142TEST_F(SurfaceTextureGLThreadToGLTest,
2143 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002144 enum { NUM_ITERATIONS = 1024 };
2145
2146 class PT : public ProducerThread {
2147 virtual void render() {
2148 for (int i = 0; i < NUM_ITERATIONS; i++) {
2149 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2150 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002151 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002152 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002153 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002154 }
2155 }
2156 };
2157
2158 runProducerThread(new PT());
2159
2160 for (int i = 0; i < NUM_ITERATIONS; i++) {
2161 mFC->waitForFrame();
2162 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002163 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002164 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002165 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002166
2167 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2168 }
2169}
2170
Jamie Gennis6e502192011-07-21 14:31:31 -07002171// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002172TEST_F(SurfaceTextureGLThreadToGLTest,
2173 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07002174 enum { NUM_ITERATIONS = 64 };
2175
2176 class PT : public ProducerThread {
2177 virtual void render() {
2178 for (int i = 0; i < NUM_ITERATIONS; i++) {
2179 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2180 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002181 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002182 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002183 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002184 }
2185 }
2186 };
2187
2188 ASSERT_EQ(OK, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -07002189 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis6e502192011-07-21 14:31:31 -07002190
2191 runProducerThread(new PT());
2192
2193 // Allow three frames to be rendered and queued before starting the
2194 // rendering in this thread. For the latter two frames we don't call
2195 // updateTexImage so the next dequeue from the producer thread will block
2196 // waiting for a frame to become available.
2197 mFC->waitForFrame();
2198 mFC->finishFrame();
2199
2200 // We must call updateTexImage to consume the first frame so that the
2201 // SurfaceTexture is able to reduce the buffer count to 2. This is because
2202 // the GL driver may dequeue a buffer when the EGLSurface is created, and
Jamie Gennis31a353d2012-08-24 17:25:13 -07002203 // that happens before we call setDefaultMaxBufferCount. It's possible that the
Jamie Gennis6e502192011-07-21 14:31:31 -07002204 // driver does not dequeue a buffer at EGLSurface creation time, so we
2205 // cannot rely on this to cause the second dequeueBuffer call to block.
Jamie Gennisd69097f2012-08-30 13:28:23 -07002206 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07002207
2208 mFC->waitForFrame();
2209 mFC->finishFrame();
2210 mFC->waitForFrame();
2211 mFC->finishFrame();
2212
2213 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
2214 // block waiting for a buffer to become available.
2215 usleep(100000);
2216
2217 // Render and present a number of images. This thread should not be blocked
2218 // by the fact that the producer thread is blocking in dequeue.
2219 for (int i = 0; i < NUM_ITERATIONS; i++) {
2220 glClear(GL_COLOR_BUFFER_BIT);
2221 eglSwapBuffers(mEglDisplay, mEglSurface);
2222 }
2223
2224 // Consume the two pending buffers to unblock the producer thread.
Jamie Gennisd69097f2012-08-30 13:28:23 -07002225 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2226 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07002227
2228 // Consume the remaining buffers from the producer thread.
2229 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
2230 mFC->waitForFrame();
2231 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002232 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002233 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002234 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002235 }
2236}
2237
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002238class SurfaceTextureFBOTest : public SurfaceTextureGLTest {
2239protected:
2240
2241 virtual void SetUp() {
2242 SurfaceTextureGLTest::SetUp();
2243
2244 glGenFramebuffers(1, &mFbo);
2245 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2246
2247 glGenTextures(1, &mFboTex);
2248 glBindTexture(GL_TEXTURE_2D, mFboTex);
2249 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getSurfaceWidth(),
2250 getSurfaceHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2251 glBindTexture(GL_TEXTURE_2D, 0);
2252 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2253
2254 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2255 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2256 GL_TEXTURE_2D, mFboTex, 0);
2257 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2258 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2259 }
2260
2261 virtual void TearDown() {
2262 SurfaceTextureGLTest::TearDown();
2263
2264 glDeleteTextures(1, &mFboTex);
2265 glDeleteFramebuffers(1, &mFbo);
2266 }
2267
2268 GLuint mFbo;
2269 GLuint mFboTex;
2270};
2271
2272// This test is intended to verify that proper synchronization is done when
2273// rendering into an FBO.
2274TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) {
2275 const int texWidth = 64;
2276 const int texHeight = 64;
2277
2278 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
2279 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
2280 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
2281 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
2282
2283 android_native_buffer_t* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002284 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2285 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002286 ASSERT_TRUE(anb != NULL);
2287
2288 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002289
2290 // Fill the buffer with green
2291 uint8_t* img = NULL;
2292 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
2293 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255,
2294 0, 255);
2295 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002296 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
2297 -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002298
2299 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2300
2301 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2302 drawTexture();
2303 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2304
2305 for (int i = 0; i < 4; i++) {
2306 SCOPED_TRACE(String8::format("frame %d", i).string());
2307
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002308 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2309 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002310 ASSERT_TRUE(anb != NULL);
2311
2312 buf = new GraphicBuffer(anb, false);
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002313
2314 // Fill the buffer with red
2315 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
2316 (void**)(&img)));
2317 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0,
2318 0, 255);
2319 ASSERT_EQ(NO_ERROR, buf->unlock());
2320 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002321 buf->getNativeBuffer(), -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002322
2323 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2324
2325 drawTexture();
2326
2327 EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255));
2328 }
2329
2330 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2331
2332 EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255));
2333}
2334
Jamie Gennisce561372012-03-19 18:33:05 -07002335class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest {
2336protected:
Jamie Gennis74bed552012-03-28 19:05:54 -07002337 enum { SECOND_TEX_ID = 123 };
2338 enum { THIRD_TEX_ID = 456 };
2339
Jamie Gennisce561372012-03-19 18:33:05 -07002340 SurfaceTextureMultiContextGLTest():
2341 mSecondEglContext(EGL_NO_CONTEXT) {
2342 }
2343
2344 virtual void SetUp() {
2345 SurfaceTextureGLTest::SetUp();
2346
Jamie Gennis74bed552012-03-28 19:05:54 -07002347 // Set up the secondary context and texture renderer.
Jamie Gennisce561372012-03-19 18:33:05 -07002348 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2349 EGL_NO_CONTEXT, getContextAttribs());
2350 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2351 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext);
Jamie Gennis74bed552012-03-28 19:05:54 -07002352
2353 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2354 mSecondEglContext));
2355 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2356 mSecondTextureRenderer = new TextureRenderer(SECOND_TEX_ID, mST);
2357 ASSERT_NO_FATAL_FAILURE(mSecondTextureRenderer->SetUp());
2358
2359 // Set up the tertiary context and texture renderer.
2360 mThirdEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2361 EGL_NO_CONTEXT, getContextAttribs());
2362 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2363 ASSERT_NE(EGL_NO_CONTEXT, mThirdEglContext);
2364
2365 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2366 mThirdEglContext));
2367 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2368 mThirdTextureRenderer = new TextureRenderer(THIRD_TEX_ID, mST);
2369 ASSERT_NO_FATAL_FAILURE(mThirdTextureRenderer->SetUp());
2370
2371 // Switch back to the primary context to start the tests.
2372 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2373 mEglContext));
Jamie Gennisce561372012-03-19 18:33:05 -07002374 }
2375
2376 virtual void TearDown() {
Jamie Gennis74bed552012-03-28 19:05:54 -07002377 if (mThirdEglContext != EGL_NO_CONTEXT) {
2378 eglDestroyContext(mEglDisplay, mThirdEglContext);
2379 }
Jamie Gennisce561372012-03-19 18:33:05 -07002380 if (mSecondEglContext != EGL_NO_CONTEXT) {
2381 eglDestroyContext(mEglDisplay, mSecondEglContext);
2382 }
2383 SurfaceTextureGLTest::TearDown();
2384 }
2385
2386 EGLContext mSecondEglContext;
Jamie Gennis74bed552012-03-28 19:05:54 -07002387 sp<TextureRenderer> mSecondTextureRenderer;
2388
2389 EGLContext mThirdEglContext;
2390 sp<TextureRenderer> mThirdTextureRenderer;
Jamie Gennisce561372012-03-19 18:33:05 -07002391};
2392
2393TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) {
Jamie Gennisce561372012-03-19 18:33:05 -07002394 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2395
2396 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002397 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002398 ASSERT_EQ(OK, mST->updateTexImage());
Jamie Gennisce561372012-03-19 18:33:05 -07002399
2400 // Attempt to latch the texture on the secondary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07002401 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
Jamie Gennisce561372012-03-19 18:33:05 -07002402 mSecondEglContext));
2403 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -07002404 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2405}
2406
2407TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002408 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2409
2410 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002411 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002412 ASSERT_EQ(OK, mST->updateTexImage());
2413
2414 // Detach from the primary context.
2415 ASSERT_EQ(OK, mST->detachFromContext());
2416
2417 // Check that the GL texture was deleted.
2418 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2419}
2420
2421TEST_F(SurfaceTextureMultiContextGLTest,
2422 DetachFromContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002423 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2424
2425 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002426 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002427 ASSERT_EQ(OK, mST->updateTexImage());
2428
2429 // Detach from the primary context.
2430 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2431 ASSERT_EQ(OK, mST->detachFromContext());
2432
2433 // Check that the GL texture was deleted.
2434 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2435}
2436
2437TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002438 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2439
2440 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002441 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002442 ASSERT_EQ(OK, mST->updateTexImage());
2443
2444 // Attempt to detach from the primary context.
2445 mST->abandon();
2446 ASSERT_EQ(NO_INIT, mST->detachFromContext());
2447}
2448
2449TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002450 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2451
2452 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002453 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002454 ASSERT_EQ(OK, mST->updateTexImage());
2455
2456 // Detach from the primary context.
2457 ASSERT_EQ(OK, mST->detachFromContext());
2458
2459 // Attempt to detach from the primary context again.
2460 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2461}
2462
2463TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002464 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2465
2466 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002467 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002468 ASSERT_EQ(OK, mST->updateTexImage());
2469
2470 // Make there be no current display.
2471 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2472 EGL_NO_CONTEXT));
2473 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2474
2475 // Attempt to detach from the primary context.
2476 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2477}
2478
2479TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002480 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2481
2482 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002483 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002484 ASSERT_EQ(OK, mST->updateTexImage());
2485
2486 // Make current context be incorrect.
2487 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2488 mSecondEglContext));
2489 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2490
2491 // Attempt to detach from the primary context.
2492 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2493}
2494
2495TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002496 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2497
2498 // Detach from the primary context.
2499 ASSERT_EQ(OK, mST->detachFromContext());
2500
2501 // Attempt to latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002502 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002503 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2504}
2505
2506TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002507 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2508
2509 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002510 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002511 ASSERT_EQ(OK, mST->updateTexImage());
2512
2513 // Detach from the primary context.
2514 ASSERT_EQ(OK, mST->detachFromContext());
2515
2516 // Attach to the secondary context.
2517 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2518 mSecondEglContext));
2519 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2520
2521 // Verify that the texture object was created and bound.
2522 GLint texBinding = -1;
2523 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2524 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2525
2526 // Try to use the texture from the secondary context.
2527 glClearColor(0.2, 0.2, 0.2, 0.2);
2528 glClear(GL_COLOR_BUFFER_BIT);
2529 glViewport(0, 0, 1, 1);
2530 mSecondTextureRenderer->drawTexture();
2531 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2532 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2533}
2534
2535TEST_F(SurfaceTextureMultiContextGLTest,
2536 AttachToContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002537 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2538
2539 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002540 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002541 ASSERT_EQ(OK, mST->updateTexImage());
2542
2543 // Detach from the primary context.
2544 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2545 ASSERT_EQ(OK, mST->detachFromContext());
2546
2547 // Attach to the secondary context.
2548 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2549 mSecondEglContext));
2550 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2551
2552 // Verify that the texture object was created and bound.
2553 GLint texBinding = -1;
2554 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2555 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2556
2557 // Try to use the texture from the secondary context.
2558 glClearColor(0.2, 0.2, 0.2, 0.2);
2559 glClear(GL_COLOR_BUFFER_BIT);
2560 glViewport(0, 0, 1, 1);
2561 mSecondTextureRenderer->drawTexture();
2562 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2563 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2564}
2565
2566TEST_F(SurfaceTextureMultiContextGLTest,
2567 AttachToContextSucceedsBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002568 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2569
2570 // Detach from the primary context.
2571 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2572 ASSERT_EQ(OK, mST->detachFromContext());
2573
2574 // Attach to the secondary context.
2575 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2576 mSecondEglContext));
2577 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2578
2579 // Verify that the texture object was created and bound.
2580 GLint texBinding = -1;
2581 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2582 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2583
2584 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002585 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002586 ASSERT_EQ(OK, mST->updateTexImage());
2587
2588 // Try to use the texture from the secondary context.
2589 glClearColor(0.2, 0.2, 0.2, 0.2);
2590 glClear(GL_COLOR_BUFFER_BIT);
2591 glViewport(0, 0, 1, 1);
2592 mSecondTextureRenderer->drawTexture();
2593 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2594 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2595}
2596
2597TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002598 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2599
2600 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002601 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002602 ASSERT_EQ(OK, mST->updateTexImage());
2603
2604 // Detach from the primary context.
2605 ASSERT_EQ(OK, mST->detachFromContext());
2606
2607 // Attempt to attach to the secondary context.
2608 mST->abandon();
2609
2610 // Attempt to attach to the primary context.
2611 ASSERT_EQ(NO_INIT, mST->attachToContext(SECOND_TEX_ID));
2612}
2613
2614TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002615 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2616
2617 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002618 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002619 ASSERT_EQ(OK, mST->updateTexImage());
2620
2621 // Attempt to attach to the primary context.
2622 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2623}
2624
2625TEST_F(SurfaceTextureMultiContextGLTest,
2626 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002627 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2628
2629 // Attempt to attach to the primary context.
2630 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2631}
2632
2633TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002634 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2635
2636 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002637 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002638 ASSERT_EQ(OK, mST->updateTexImage());
2639
2640 // Detach from the primary context.
2641 ASSERT_EQ(OK, mST->detachFromContext());
2642
2643 // Make there be no current display.
2644 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2645 EGL_NO_CONTEXT));
2646 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2647
2648 // Attempt to attach with no context current.
2649 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2650}
2651
2652TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002653 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2654
2655 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002656 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002657 ASSERT_EQ(OK, mST->updateTexImage());
2658
2659 // Detach from the primary context.
2660 ASSERT_EQ(OK, mST->detachFromContext());
2661
2662 // Attach to the secondary context.
2663 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2664 mSecondEglContext));
2665 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2666
2667 // Detach from the secondary context.
2668 ASSERT_EQ(OK, mST->detachFromContext());
2669
2670 // Attach to the tertiary context.
2671 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2672 mThirdEglContext));
2673 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2674
2675 // Verify that the texture object was created and bound.
2676 GLint texBinding = -1;
2677 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2678 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2679
2680 // Try to use the texture from the tertiary context.
2681 glClearColor(0.2, 0.2, 0.2, 0.2);
2682 glClear(GL_COLOR_BUFFER_BIT);
2683 glViewport(0, 0, 1, 1);
2684 mThirdTextureRenderer->drawTexture();
2685 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2686 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2687}
2688
2689TEST_F(SurfaceTextureMultiContextGLTest,
2690 AttachToContextSucceedsTwiceBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002691 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2692
2693 // Detach from the primary context.
2694 ASSERT_EQ(OK, mST->detachFromContext());
2695
2696 // Attach to the secondary context.
2697 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2698 mSecondEglContext));
2699 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2700
2701 // Detach from the secondary context.
2702 ASSERT_EQ(OK, mST->detachFromContext());
2703
2704 // Attach to the tertiary context.
2705 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2706 mThirdEglContext));
2707 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2708
2709 // Verify that the texture object was created and bound.
2710 GLint texBinding = -1;
2711 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2712 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2713
2714 // Latch the texture contents on the tertiary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002715 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002716 ASSERT_EQ(OK, mST->updateTexImage());
2717
2718 // Try to use the texture from the tertiary context.
2719 glClearColor(0.2, 0.2, 0.2, 0.2);
2720 glClear(GL_COLOR_BUFFER_BIT);
2721 glViewport(0, 0, 1, 1);
2722 mThirdTextureRenderer->drawTexture();
2723 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2724 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
Jamie Gennisce561372012-03-19 18:33:05 -07002725}
2726
Jesse Hall90ed8502012-05-16 23:44:34 -07002727TEST_F(SurfaceTextureMultiContextGLTest,
2728 UpdateTexImageSucceedsForBufferConsumedBeforeDetach) {
2729 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -07002730 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2));
Jesse Hall90ed8502012-05-16 23:44:34 -07002731
2732 // produce two frames and consume them both on the primary context
2733 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2734 mFW->waitForFrame();
2735 ASSERT_EQ(OK, mST->updateTexImage());
2736
2737 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2738 mFW->waitForFrame();
2739 ASSERT_EQ(OK, mST->updateTexImage());
2740
2741 // produce one more frame
2742 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2743
2744 // Detach from the primary context and attach to the secondary context
2745 ASSERT_EQ(OK, mST->detachFromContext());
2746 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2747 mSecondEglContext));
2748 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2749
2750 // Consume final frame on secondary context
2751 mFW->waitForFrame();
2752 ASSERT_EQ(OK, mST->updateTexImage());
2753}
2754
Jamie Gennis5451d152011-06-08 09:40:45 -07002755} // namespace android