blob: d97521aea5621f9de8aa3b24741ec55a60d89b81 [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>
Mathias Agopianf31510a2013-04-16 23:32:38 -070032#include <GLES/gl.h>
33#include <GLES/glext.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080034#include <GLES2/gl2.h>
35#include <GLES2/gl2ext.h>
36
37#include <ui/FramebufferNativeWindow.h>
Mathias Agopianf31510a2013-04-16 23:32:38 -070038#include <utils/UniquePtr.h>
39#include <android/native_window.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080040
41namespace android {
42
43class GLTest : public ::testing::Test {
44protected:
45
46 GLTest():
47 mEglDisplay(EGL_NO_DISPLAY),
48 mEglSurface(EGL_NO_SURFACE),
49 mEglContext(EGL_NO_CONTEXT) {
50 }
51
52 virtual void SetUp() {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070053 const ::testing::TestInfo* const testInfo =
54 ::testing::UnitTest::GetInstance()->current_test_info();
55 ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
56 testInfo->name());
57
Jamie Gennisd99c0882011-03-10 16:24:46 -080058 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
59 ASSERT_EQ(EGL_SUCCESS, eglGetError());
60 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
61
62 EGLint majorVersion;
63 EGLint minorVersion;
64 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
65 ASSERT_EQ(EGL_SUCCESS, eglGetError());
66 RecordProperty("EglVersionMajor", majorVersion);
67 RecordProperty("EglVersionMajor", minorVersion);
68
Jamie Gennisd99c0882011-03-10 16:24:46 -080069 EGLint numConfigs = 0;
Jamie Gennis1876d132011-03-17 16:32:52 -070070 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080071 1, &numConfigs));
72 ASSERT_EQ(EGL_SUCCESS, eglGetError());
73
74 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
75 if (displaySecsEnv != NULL) {
76 mDisplaySecs = atoi(displaySecsEnv);
77 if (mDisplaySecs < 0) {
78 mDisplaySecs = 0;
79 }
80 } else {
81 mDisplaySecs = 0;
82 }
83
84 if (mDisplaySecs > 0) {
85 mComposerClient = new SurfaceComposerClient;
86 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
87
Jamie Gennisfc850122011-04-25 16:40:05 -070088 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070089 String8("Test Surface"),
Jamie Gennisd99c0882011-03-10 16:24:46 -080090 getSurfaceWidth(), getSurfaceHeight(),
91 PIXEL_FORMAT_RGB_888, 0);
92
93 ASSERT_TRUE(mSurfaceControl != NULL);
94 ASSERT_TRUE(mSurfaceControl->isValid());
95
Mathias Agopian698c0872011-06-28 19:09:31 -070096 SurfaceComposerClient::openGlobalTransaction();
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070097 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080098 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070099 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennisd99c0882011-03-10 16:24:46 -0800100
101 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -0700102 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800103 window.get(), NULL);
104 } else {
105 EGLint pbufferAttribs[] = {
106 EGL_WIDTH, getSurfaceWidth(),
107 EGL_HEIGHT, getSurfaceHeight(),
108 EGL_NONE };
109
Jamie Gennis1876d132011-03-17 16:32:52 -0700110 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800111 pbufferAttribs);
112 }
113 ASSERT_EQ(EGL_SUCCESS, eglGetError());
114 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
115
Jamie Gennis1876d132011-03-17 16:32:52 -0700116 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800117 getContextAttribs());
118 ASSERT_EQ(EGL_SUCCESS, eglGetError());
119 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
120
121 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
122 mEglContext));
123 ASSERT_EQ(EGL_SUCCESS, eglGetError());
124
125 EGLint w, h;
126 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
127 ASSERT_EQ(EGL_SUCCESS, eglGetError());
128 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
129 ASSERT_EQ(EGL_SUCCESS, eglGetError());
130 RecordProperty("EglSurfaceWidth", w);
131 RecordProperty("EglSurfaceHeight", h);
132
133 glViewport(0, 0, w, h);
134 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
135 }
136
137 virtual void TearDown() {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700138 // Display the result
Jamie Gennisd99c0882011-03-10 16:24:46 -0800139 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
140 eglSwapBuffers(mEglDisplay, mEglSurface);
141 sleep(mDisplaySecs);
142 }
143
144 if (mComposerClient != NULL) {
145 mComposerClient->dispose();
146 }
147 if (mEglContext != EGL_NO_CONTEXT) {
148 eglDestroyContext(mEglDisplay, mEglContext);
149 }
150 if (mEglSurface != EGL_NO_SURFACE) {
151 eglDestroySurface(mEglDisplay, mEglSurface);
152 }
153 if (mEglDisplay != EGL_NO_DISPLAY) {
154 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
155 EGL_NO_CONTEXT);
156 eglTerminate(mEglDisplay);
157 }
158 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700159
160 const ::testing::TestInfo* const testInfo =
161 ::testing::UnitTest::GetInstance()->current_test_info();
162 ALOGV("End test: %s.%s", testInfo->test_case_name(),
163 testInfo->name());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800164 }
165
166 virtual EGLint const* getConfigAttribs() {
167 static EGLint sDefaultConfigAttribs[] = {
168 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
169 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
170 EGL_RED_SIZE, 8,
171 EGL_GREEN_SIZE, 8,
172 EGL_BLUE_SIZE, 8,
173 EGL_ALPHA_SIZE, 8,
174 EGL_DEPTH_SIZE, 16,
175 EGL_STENCIL_SIZE, 8,
176 EGL_NONE };
177
178 return sDefaultConfigAttribs;
179 }
180
181 virtual EGLint const* getContextAttribs() {
182 static EGLint sDefaultContextAttribs[] = {
183 EGL_CONTEXT_CLIENT_VERSION, 2,
184 EGL_NONE };
185
186 return sDefaultContextAttribs;
187 }
188
189 virtual EGLint getSurfaceWidth() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700190 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800191 }
192
193 virtual EGLint getSurfaceHeight() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700194 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800195 }
196
Jamie Gennisd99c0882011-03-10 16:24:46 -0800197 ::testing::AssertionResult checkPixel(int x, int y, int r,
Jamie Gennis824efa72011-06-13 13:41:01 -0700198 int g, int b, int a, int tolerance=2) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800199 GLubyte pixel[4];
200 String8 msg;
201 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
202 GLenum err = glGetError();
203 if (err != GL_NO_ERROR) {
204 msg += String8::format("error reading pixel: %#x", err);
205 while ((err = glGetError()) != GL_NO_ERROR) {
206 msg += String8::format(", %#x", err);
207 }
Jamie Gennisd99c0882011-03-10 16:24:46 -0800208 return ::testing::AssertionFailure(
209 ::testing::Message(msg.string()));
210 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700211 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800212 msg += String8::format("r(%d isn't %d)", pixel[0], r);
213 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700214 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800215 if (!msg.isEmpty()) {
216 msg += " ";
217 }
218 msg += String8::format("g(%d isn't %d)", pixel[1], g);
219 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700220 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800221 if (!msg.isEmpty()) {
222 msg += " ";
223 }
224 msg += String8::format("b(%d isn't %d)", pixel[2], b);
225 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700226 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800227 if (!msg.isEmpty()) {
228 msg += " ";
229 }
230 msg += String8::format("a(%d isn't %d)", pixel[3], a);
231 }
232 if (!msg.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800233 return ::testing::AssertionFailure(
234 ::testing::Message(msg.string()));
235 } else {
236 return ::testing::AssertionSuccess();
237 }
238 }
239
Daniel Lam016c8cb2012-04-03 15:54:58 -0700240 ::testing::AssertionResult assertRectEq(const Rect &r1,
241 const Rect &r2, int tolerance=1) {
242
243 String8 msg;
244
245 if (abs(r1.left - r2.left) > tolerance) {
246 msg += String8::format("left(%d isn't %d)", r1.left, r2.left);
247 }
248 if (abs(r1.top - r2.top) > tolerance) {
249 if (!msg.isEmpty()) {
250 msg += " ";
251 }
252 msg += String8::format("top(%d isn't %d)", r1.top, r2.top);
253 }
254 if (abs(r1.right - r2.right) > tolerance) {
255 if (!msg.isEmpty()) {
256 msg += " ";
257 }
258 msg += String8::format("right(%d isn't %d)", r1.right, r2.right);
259 }
260 if (abs(r1.bottom - r2.bottom) > tolerance) {
261 if (!msg.isEmpty()) {
262 msg += " ";
263 }
264 msg += String8::format("bottom(%d isn't %d)", r1.bottom, r2.bottom);
265 }
266 if (!msg.isEmpty()) {
267 msg += String8::format(" R1: [%d %d %d %d] R2: [%d %d %d %d]",
268 r1.left, r1.top, r1.right, r1.bottom,
269 r2.left, r2.top, r2.right, r2.bottom);
270 fprintf(stderr, "assertRectEq: %s\n", msg.string());
271 return ::testing::AssertionFailure(
272 ::testing::Message(msg.string()));
273 } else {
274 return ::testing::AssertionSuccess();
275 }
276 }
277
Jamie Gennisd99c0882011-03-10 16:24:46 -0800278 int mDisplaySecs;
279 sp<SurfaceComposerClient> mComposerClient;
280 sp<SurfaceControl> mSurfaceControl;
281
282 EGLDisplay mEglDisplay;
283 EGLSurface mEglSurface;
284 EGLContext mEglContext;
Jamie Gennis1876d132011-03-17 16:32:52 -0700285 EGLConfig mGlConfig;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800286};
287
Jamie Gennis74bed552012-03-28 19:05:54 -0700288static void loadShader(GLenum shaderType, const char* pSource,
289 GLuint* outShader) {
290 GLuint shader = glCreateShader(shaderType);
291 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
292 if (shader) {
293 glShaderSource(shader, 1, &pSource, NULL);
294 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
295 glCompileShader(shader);
296 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
297 GLint compiled = 0;
298 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
299 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
300 if (!compiled) {
301 GLint infoLen = 0;
302 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
303 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
304 if (infoLen) {
305 char* buf = (char*) malloc(infoLen);
306 if (buf) {
307 glGetShaderInfoLog(shader, infoLen, NULL, buf);
308 printf("Shader compile log:\n%s\n", buf);
309 free(buf);
310 FAIL();
311 }
312 } else {
313 char* buf = (char*) malloc(0x1000);
314 if (buf) {
315 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
316 printf("Shader compile log:\n%s\n", buf);
317 free(buf);
318 FAIL();
319 }
320 }
321 glDeleteShader(shader);
322 shader = 0;
323 }
324 }
325 ASSERT_TRUE(shader != 0);
326 *outShader = shader;
327}
328
329static void createProgram(const char* pVertexSource,
330 const char* pFragmentSource, GLuint* outPgm) {
331 GLuint vertexShader, fragmentShader;
332 {
333 SCOPED_TRACE("compiling vertex shader");
334 ASSERT_NO_FATAL_FAILURE(loadShader(GL_VERTEX_SHADER, pVertexSource,
335 &vertexShader));
336 }
337 {
338 SCOPED_TRACE("compiling fragment shader");
339 ASSERT_NO_FATAL_FAILURE(loadShader(GL_FRAGMENT_SHADER, pFragmentSource,
340 &fragmentShader));
341 }
342
343 GLuint program = glCreateProgram();
344 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
345 if (program) {
346 glAttachShader(program, vertexShader);
347 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
348 glAttachShader(program, fragmentShader);
349 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
350 glLinkProgram(program);
351 GLint linkStatus = GL_FALSE;
352 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
353 if (linkStatus != GL_TRUE) {
354 GLint bufLength = 0;
355 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
356 if (bufLength) {
357 char* buf = (char*) malloc(bufLength);
358 if (buf) {
359 glGetProgramInfoLog(program, bufLength, NULL, buf);
360 printf("Program link log:\n%s\n", buf);
361 free(buf);
362 FAIL();
363 }
364 }
365 glDeleteProgram(program);
366 program = 0;
367 }
368 }
369 glDeleteShader(vertexShader);
370 glDeleteShader(fragmentShader);
371 ASSERT_TRUE(program != 0);
372 *outPgm = program;
373}
374
375static int abs(int value) {
376 return value > 0 ? value : -value;
377}
378
379
Jamie Gennisd99c0882011-03-10 16:24:46 -0800380// XXX: Code above this point should live elsewhere
381
Mathias Agopianf31510a2013-04-16 23:32:38 -0700382class MultiTextureConsumerTest : public GLTest {
383protected:
384 enum { TEX_ID = 123 };
385
386 virtual void SetUp() {
387 GLTest::SetUp();
Mathias Agopian8f938a52013-07-12 22:06:26 -0700388 sp<BufferQueue> bq = new BufferQueue();
389 mGlConsumer = new GLConsumer(bq, TEX_ID);
Mathias Agopianf31510a2013-04-16 23:32:38 -0700390 mSurface = new Surface(mGlConsumer->getBufferQueue());
391 mANW = mSurface.get();
392
393 }
394 virtual void TearDown() {
395 GLTest::TearDown();
396 }
397 virtual EGLint const* getContextAttribs() {
398 return NULL;
399 }
400 virtual EGLint const* getConfigAttribs() {
401 static EGLint sDefaultConfigAttribs[] = {
402 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
403 EGL_RED_SIZE, 8,
404 EGL_GREEN_SIZE, 8,
405 EGL_BLUE_SIZE, 8,
406 EGL_ALPHA_SIZE, 8,
407 EGL_NONE };
408
409 return sDefaultConfigAttribs;
410 }
411 sp<GLConsumer> mGlConsumer;
412 sp<Surface> mSurface;
413 ANativeWindow* mANW;
414};
415
416
417TEST_F(MultiTextureConsumerTest, EGLImageTargetWorks) {
418 ANativeWindow_Buffer buffer;
419
420 ASSERT_EQ(native_window_set_usage(mANW, GRALLOC_USAGE_SW_WRITE_OFTEN), NO_ERROR);
421 ASSERT_EQ(native_window_set_buffers_format(mANW, HAL_PIXEL_FORMAT_RGBA_8888), NO_ERROR);
422
423 glShadeModel(GL_FLAT);
424 glDisable(GL_DITHER);
425 glDisable(GL_CULL_FACE);
426 glViewport(0, 0, getSurfaceWidth(), getSurfaceHeight());
427 glOrthof(0, getSurfaceWidth(), 0, getSurfaceHeight(), 0, 1);
428 glEnableClientState(GL_VERTEX_ARRAY);
429 glColor4f(1, 1, 1, 1);
430
431 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
432 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
433 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
434 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
435 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
436
437 uint32_t texel = 0x80808080;
438 glBindTexture(GL_TEXTURE_2D, TEX_ID+1);
439 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &texel);
440 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
441 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
442 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
443 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
444
445 glActiveTexture(GL_TEXTURE1);
446 glBindTexture(GL_TEXTURE_2D, TEX_ID+1);
447 glEnable(GL_TEXTURE_2D);
448 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
449
450 glActiveTexture(GL_TEXTURE0);
451 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
452 glEnable(GL_TEXTURE_EXTERNAL_OES);
453 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
454
455 glClear(GL_COLOR_BUFFER_BIT);
456 for (int i=0 ; i<8 ; i++) {
457 mSurface->lock(&buffer, NULL);
458 memset(buffer.bits, (i&7) * 0x20, buffer.stride * buffer.height * 4);
459 mSurface->unlockAndPost();
460
461 mGlConsumer->updateTexImage();
462
463 GLfloat vertices[][2] = { {i*16.0f, 0}, {(i+1)*16.0f, 0}, {(i+1)*16.0f, 16.0f}, {i*16.0f, 16.0f} };
464 glVertexPointer(2, GL_FLOAT, 0, vertices);
465 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
466
Jamie Gennisea2d9422013-04-23 15:00:45 -0700467 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
Mathias Agopianf31510a2013-04-16 23:32:38 -0700468 }
Mathias Agopianf31510a2013-04-16 23:32:38 -0700469
470 for (int i=0 ; i<8 ; i++) {
Jamie Gennisea2d9422013-04-23 15:00:45 -0700471 EXPECT_TRUE(checkPixel(i*16 + 8, 8, i*16, i*16, i*16, i*16, 0));
Mathias Agopianf31510a2013-04-16 23:32:38 -0700472 }
Mathias Agopianf31510a2013-04-16 23:32:38 -0700473}
474
475
476
Jamie Gennisd99c0882011-03-10 16:24:46 -0800477class SurfaceTextureGLTest : public GLTest {
478protected:
Jamie Gennis79e31252011-10-19 15:19:19 -0700479 enum { TEX_ID = 123 };
Jamie Gennisd99c0882011-03-10 16:24:46 -0800480
481 virtual void SetUp() {
482 GLTest::SetUp();
Mathias Agopian8f938a52013-07-12 22:06:26 -0700483 sp<BufferQueue> bq = new BufferQueue();
484 mST = new GLConsumer(bq, TEX_ID);
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800485 mSTC = new Surface(mST->getBufferQueue());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800486 mANW = mSTC;
Jamie Gennis74bed552012-03-28 19:05:54 -0700487 mTextureRenderer = new TextureRenderer(TEX_ID, mST);
488 ASSERT_NO_FATAL_FAILURE(mTextureRenderer->SetUp());
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700489 mFW = new FrameWaiter;
490 mST->setFrameAvailableListener(mFW);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800491 }
492
Jamie Gennis2640bfd2011-07-14 17:11:47 -0700493 virtual void TearDown() {
494 mANW.clear();
495 mSTC.clear();
496 mST.clear();
497 GLTest::TearDown();
498 }
499
Jamie Gennisd99c0882011-03-10 16:24:46 -0800500 void drawTexture() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700501 mTextureRenderer->drawTexture();
Jamie Gennisd99c0882011-03-10 16:24:46 -0800502 }
503
Jamie Gennis74bed552012-03-28 19:05:54 -0700504 class TextureRenderer: public RefBase {
505 public:
Andy McFadden2adaf042012-12-18 09:49:45 -0800506 TextureRenderer(GLuint texName, const sp<GLConsumer>& st):
Jamie Gennis74bed552012-03-28 19:05:54 -0700507 mTexName(texName),
508 mST(st) {
509 }
510
511 void SetUp() {
512 const char vsrc[] =
513 "attribute vec4 vPosition;\n"
514 "varying vec2 texCoords;\n"
515 "uniform mat4 texMatrix;\n"
516 "void main() {\n"
517 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
518 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
519 " gl_Position = vPosition;\n"
520 "}\n";
521
522 const char fsrc[] =
523 "#extension GL_OES_EGL_image_external : require\n"
524 "precision mediump float;\n"
525 "uniform samplerExternalOES texSampler;\n"
526 "varying vec2 texCoords;\n"
527 "void main() {\n"
528 " gl_FragColor = texture2D(texSampler, texCoords);\n"
529 "}\n";
530
531 {
532 SCOPED_TRACE("creating shader program");
533 ASSERT_NO_FATAL_FAILURE(createProgram(vsrc, fsrc, &mPgm));
534 }
535
536 mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
537 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
538 ASSERT_NE(-1, mPositionHandle);
539 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
540 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
541 ASSERT_NE(-1, mTexSamplerHandle);
542 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
543 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
544 ASSERT_NE(-1, mTexMatrixHandle);
545 }
546
Andy McFadden2adaf042012-12-18 09:49:45 -0800547 // drawTexture draws the GLConsumer over the entire GL viewport.
Jamie Gennis74bed552012-03-28 19:05:54 -0700548 void drawTexture() {
Jamie Gennisa96b6bd2012-04-11 17:27:12 -0700549 static const GLfloat triangleVertices[] = {
Jamie Gennis74bed552012-03-28 19:05:54 -0700550 -1.0f, 1.0f,
551 -1.0f, -1.0f,
552 1.0f, -1.0f,
553 1.0f, 1.0f,
554 };
555
556 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
557 triangleVertices);
558 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
559 glEnableVertexAttribArray(mPositionHandle);
560 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
561
562 glUseProgram(mPgm);
563 glUniform1i(mTexSamplerHandle, 0);
564 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
565 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
566 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
567
568 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
569 // they're setting the defautls for that target, but when hacking
570 // things to use GL_TEXTURE_2D they are needed to achieve the same
571 // behavior.
572 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
573 GL_LINEAR);
574 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
575 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
576 GL_LINEAR);
577 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
578 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
579 GL_CLAMP_TO_EDGE);
580 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
581 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
582 GL_CLAMP_TO_EDGE);
583 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
584
585 GLfloat texMatrix[16];
586 mST->getTransformMatrix(texMatrix);
587 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
588
589 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
590 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
591 }
592
593 GLuint mTexName;
Andy McFadden2adaf042012-12-18 09:49:45 -0800594 sp<GLConsumer> mST;
Jamie Gennis74bed552012-03-28 19:05:54 -0700595 GLuint mPgm;
596 GLint mPositionHandle;
597 GLint mTexSamplerHandle;
598 GLint mTexMatrixHandle;
599 };
600
Andy McFadden2adaf042012-12-18 09:49:45 -0800601 class FrameWaiter : public GLConsumer::FrameAvailableListener {
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700602 public:
603 FrameWaiter():
604 mPendingFrames(0) {
605 }
606
607 void waitForFrame() {
608 Mutex::Autolock lock(mMutex);
609 while (mPendingFrames == 0) {
610 mCondition.wait(mMutex);
611 }
612 mPendingFrames--;
613 }
614
615 virtual void onFrameAvailable() {
616 Mutex::Autolock lock(mMutex);
617 mPendingFrames++;
618 mCondition.signal();
619 }
620
621 int mPendingFrames;
622 Mutex mMutex;
623 Condition mCondition;
624 };
625
Andy McFadden2adaf042012-12-18 09:49:45 -0800626 // Note that GLConsumer will lose the notifications
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700627 // onBuffersReleased and onFrameAvailable as there is currently
628 // no way to forward the events. This DisconnectWaiter will not let the
629 // disconnect finish until finishDisconnect() is called. It will
630 // also block until a disconnect is called
631 class DisconnectWaiter : public BufferQueue::ConsumerListener {
632 public:
633 DisconnectWaiter () :
634 mWaitForDisconnect(false),
635 mPendingFrames(0) {
636 }
637
638 void waitForFrame() {
639 Mutex::Autolock lock(mMutex);
640 while (mPendingFrames == 0) {
641 mFrameCondition.wait(mMutex);
642 }
643 mPendingFrames--;
644 }
645
646 virtual void onFrameAvailable() {
647 Mutex::Autolock lock(mMutex);
648 mPendingFrames++;
649 mFrameCondition.signal();
650 }
651
652 virtual void onBuffersReleased() {
653 Mutex::Autolock lock(mMutex);
654 while (!mWaitForDisconnect) {
655 mDisconnectCondition.wait(mMutex);
656 }
657 }
658
659 void finishDisconnect() {
660 Mutex::Autolock lock(mMutex);
661 mWaitForDisconnect = true;
662 mDisconnectCondition.signal();
663 }
664
665 private:
666 Mutex mMutex;
667
668 bool mWaitForDisconnect;
669 Condition mDisconnectCondition;
670
671 int mPendingFrames;
672 Condition mFrameCondition;
673 };
674
Andy McFadden2adaf042012-12-18 09:49:45 -0800675 sp<GLConsumer> mST;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800676 sp<Surface> mSTC;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800677 sp<ANativeWindow> mANW;
Jamie Gennis74bed552012-03-28 19:05:54 -0700678 sp<TextureRenderer> mTextureRenderer;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700679 sp<FrameWaiter> mFW;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800680};
681
682// Fill a YV12 buffer with a multi-colored checkerboard pattern
683void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
684 const int blockWidth = w > 16 ? w / 16 : 1;
685 const int blockHeight = h > 16 ? h / 16 : 1;
686 const int yuvTexOffsetY = 0;
687 int yuvTexStrideY = stride;
688 int yuvTexOffsetV = yuvTexStrideY * h;
689 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
690 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
691 int yuvTexStrideU = yuvTexStrideV;
692 for (int x = 0; x < w; x++) {
693 for (int y = 0; y < h; y++) {
694 int parityX = (x / blockWidth) & 1;
695 int parityY = (y / blockHeight) & 1;
696 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
697 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
698 if (x < w / 2 && y < h / 2) {
699 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
700 if (x * 2 < w / 2 && y * 2 < h / 2) {
701 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
702 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
703 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
704 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
705 intensity;
706 }
707 }
708 }
709 }
710}
711
712// Fill a YV12 buffer with red outside a given rectangle and green inside it.
713void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
714 const android_native_rect_t& rect) {
715 const int yuvTexOffsetY = 0;
716 int yuvTexStrideY = stride;
717 int yuvTexOffsetV = yuvTexStrideY * h;
718 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
719 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
720 int yuvTexStrideU = yuvTexStrideV;
721 for (int x = 0; x < w; x++) {
722 for (int y = 0; y < h; y++) {
723 bool inside = rect.left <= x && x < rect.right &&
724 rect.top <= y && y < rect.bottom;
725 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
726 if (x < w / 2 && y < h / 2) {
727 bool inside = rect.left <= 2*x && 2*x < rect.right &&
728 rect.top <= 2*y && 2*y < rect.bottom;
729 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
730 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
731 inside ? 16 : 255;
732 }
733 }
734 }
735}
736
Jamie Gennis1876d132011-03-17 16:32:52 -0700737void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
738 const size_t PIXEL_SIZE = 4;
739 for (int x = 0; x < w; x++) {
740 for (int y = 0; y < h; y++) {
741 off_t offset = (y * stride + x) * PIXEL_SIZE;
742 for (int c = 0; c < 4; c++) {
743 int parityX = (x / (1 << (c+2))) & 1;
744 int parityY = (y / (1 << (c+2))) & 1;
745 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
746 }
747 }
748 }
749}
750
Jamie Gennisfe27e2f2011-11-11 18:05:11 -0800751void fillRGBA8BufferSolid(uint8_t* buf, int w, int h, int stride, uint8_t r,
752 uint8_t g, uint8_t b, uint8_t a) {
753 const size_t PIXEL_SIZE = 4;
754 for (int y = 0; y < h; y++) {
755 for (int x = 0; x < h; x++) {
756 off_t offset = (y * stride + x) * PIXEL_SIZE;
757 buf[offset + 0] = r;
758 buf[offset + 1] = g;
759 buf[offset + 2] = b;
760 buf[offset + 3] = a;
761 }
762 }
763}
764
Jamie Gennisce561372012-03-19 18:33:05 -0700765// Produce a single RGBA8 frame by filling a buffer with a checkerboard pattern
766// using the CPU. This assumes that the ANativeWindow is already configured to
767// allow this to be done (e.g. the format is set to RGBA8).
768//
769// Calls to this function should be wrapped in an ASSERT_NO_FATAL_FAILURE().
770void produceOneRGBA8Frame(const sp<ANativeWindow>& anw) {
771 android_native_buffer_t* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700772 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
773 &anb));
Jamie Gennisce561372012-03-19 18:33:05 -0700774 ASSERT_TRUE(anb != NULL);
775
776 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisce561372012-03-19 18:33:05 -0700777
778 uint8_t* img = NULL;
779 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
780 (void**)(&img)));
781 fillRGBA8Buffer(img, buf->getWidth(), buf->getHeight(), buf->getStride());
782 ASSERT_EQ(NO_ERROR, buf->unlock());
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700783 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer(),
784 -1));
Jamie Gennisce561372012-03-19 18:33:05 -0700785}
786
Jamie Gennisd99c0882011-03-10 16:24:46 -0800787TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700788 const int texWidth = 64;
789 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800790
791 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700792 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800793 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
794 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
795
Iliyan Malchev697526b2011-05-01 11:33:26 -0700796 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700797 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
798 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800799 ASSERT_TRUE(anb != NULL);
800
801 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800802
803 // Fill the buffer with the a checkerboard pattern
804 uint8_t* img = NULL;
805 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700806 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800807 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700808 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
809 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800810
Jamie Gennisd69097f2012-08-30 13:28:23 -0700811 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800812
813 glClearColor(0.2, 0.2, 0.2, 0.2);
814 glClear(GL_COLOR_BUFFER_BIT);
815
Jamie Gennisc8c51522011-06-15 14:24:38 -0700816 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800817 drawTexture();
818
Jamie Gennise6a0f502013-04-05 17:37:32 -0700819 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255, 3));
820 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255, 3));
821 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255, 3));
822 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255, 3));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800823
Jamie Gennise6a0f502013-04-05 17:37:32 -0700824 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255, 3));
825 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255, 3));
826 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255, 3));
827 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255, 3));
828 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255, 3));
829 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255, 3));
830 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255, 3));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800831}
832
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700833TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700834 const int texWidth = 64;
835 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800836
837 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700838 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800839 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
840 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
841
Iliyan Malchev697526b2011-05-01 11:33:26 -0700842 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700843 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
844 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800845 ASSERT_TRUE(anb != NULL);
846
847 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800848
849 // Fill the buffer with the a checkerboard pattern
850 uint8_t* img = NULL;
851 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700852 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800853 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700854 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
855 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800856
Jamie Gennisd69097f2012-08-30 13:28:23 -0700857 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800858
859 glClearColor(0.2, 0.2, 0.2, 0.2);
860 glClear(GL_COLOR_BUFFER_BIT);
861
Jamie Gennisc8c51522011-06-15 14:24:38 -0700862 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800863 drawTexture();
864
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700865 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
866 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800867 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
868 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
869
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700870 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
871 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
872 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
873 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
874 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
875 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
876 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800877}
878
879TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700880 const int texWidth = 64;
881 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800882
883 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700884 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800885 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
886 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
887
888 android_native_rect_t crops[] = {
889 {4, 6, 22, 36},
890 {0, 6, 22, 36},
891 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700892 {4, 6, texWidth, 36},
893 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800894 };
895
896 for (int i = 0; i < 5; i++) {
897 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800898 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
899 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800900
901 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
902
Iliyan Malchev697526b2011-05-01 11:33:26 -0700903 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700904 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
905 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800906 ASSERT_TRUE(anb != NULL);
907
908 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800909
910 uint8_t* img = NULL;
911 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700912 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800913 buf->unlock();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800914 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700915 buf->getNativeBuffer(), -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800916
Jamie Gennisd69097f2012-08-30 13:28:23 -0700917 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800918
919 glClearColor(0.2, 0.2, 0.2, 0.2);
920 glClear(GL_COLOR_BUFFER_BIT);
921
Jamie Gennisc8c51522011-06-15 14:24:38 -0700922 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800923 drawTexture();
924
925 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
926 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
927 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
928 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
929
930 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
931 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
932 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
933 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
934 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
935 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
936 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
937 }
938}
939
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700940// This test is intended to catch synchronization bugs between the CPU-written
941// and GPU-read buffers.
942TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
943 enum { texWidth = 16 };
944 enum { texHeight = 16 };
945 enum { numFrames = 1024 };
946
947 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -0700948 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2));
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700949 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
950 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
951 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
952 GRALLOC_USAGE_SW_WRITE_OFTEN));
953
954 struct TestPixel {
955 int x;
956 int y;
957 };
958 const TestPixel testPixels[] = {
959 { 4, 11 },
960 { 12, 14 },
961 { 7, 2 },
962 };
963 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
964
965 class ProducerThread : public Thread {
966 public:
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800967 ProducerThread(const sp<ANativeWindow>& anw,
968 const TestPixel* testPixels):
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700969 mANW(anw),
970 mTestPixels(testPixels) {
971 }
972
973 virtual ~ProducerThread() {
974 }
975
976 virtual bool threadLoop() {
977 for (int i = 0; i < numFrames; i++) {
978 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700979 if (native_window_dequeue_buffer_and_wait(mANW.get(),
980 &anb) != NO_ERROR) {
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700981 return false;
982 }
983 if (anb == NULL) {
984 return false;
985 }
986
987 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700988
989 const int yuvTexOffsetY = 0;
990 int stride = buf->getStride();
991 int yuvTexStrideY = stride;
992 int yuvTexOffsetV = yuvTexStrideY * texHeight;
993 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
994 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
995 int yuvTexStrideU = yuvTexStrideV;
996
997 uint8_t* img = NULL;
998 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
999
1000 // Gray out all the test pixels first, so we're more likely to
1001 // see a failure if GL is still texturing from the buffer we
1002 // just dequeued.
1003 for (int j = 0; j < numTestPixels; j++) {
1004 int x = mTestPixels[j].x;
1005 int y = mTestPixels[j].y;
1006 uint8_t value = 128;
1007 img[y*stride + x] = value;
1008 }
1009
1010 // Fill the buffer with gray.
1011 for (int y = 0; y < texHeight; y++) {
1012 for (int x = 0; x < texWidth; x++) {
1013 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
1014 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
1015 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
1016 }
1017 }
1018
1019 // Set the test pixels to either white or black.
1020 for (int j = 0; j < numTestPixels; j++) {
1021 int x = mTestPixels[j].x;
1022 int y = mTestPixels[j].y;
1023 uint8_t value = 0;
1024 if (j == (i % numTestPixels)) {
1025 value = 255;
1026 }
1027 img[y*stride + x] = value;
1028 }
1029
1030 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001031 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), -1)
Jamie Gennisdfcff4b2011-06-17 11:39:18 -07001032 != NO_ERROR) {
1033 return false;
1034 }
1035 }
1036 return false;
1037 }
1038
1039 sp<ANativeWindow> mANW;
1040 const TestPixel* mTestPixels;
1041 };
1042
Jamie Gennisdfcff4b2011-06-17 11:39:18 -07001043 sp<Thread> pt(new ProducerThread(mANW, testPixels));
1044 pt->run();
1045
1046 glViewport(0, 0, texWidth, texHeight);
1047
1048 glClearColor(0.2, 0.2, 0.2, 0.2);
1049 glClear(GL_COLOR_BUFFER_BIT);
1050
1051 // We wait for the first two frames up front so that the producer will be
1052 // likely to dequeue the buffer that's currently being textured from.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001053 mFW->waitForFrame();
1054 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -07001055
1056 for (int i = 0; i < numFrames; i++) {
1057 SCOPED_TRACE(String8::format("frame %d", i).string());
1058
1059 // We must wait for each frame to come in because if we ever do an
1060 // updateTexImage call that doesn't consume a newly available buffer
1061 // then the producer and consumer will get out of sync, which will cause
1062 // a deadlock.
1063 if (i > 1) {
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001064 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -07001065 }
Jamie Gennisd69097f2012-08-30 13:28:23 -07001066 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisdfcff4b2011-06-17 11:39:18 -07001067 drawTexture();
1068
1069 for (int j = 0; j < numTestPixels; j++) {
1070 int x = testPixels[j].x;
1071 int y = testPixels[j].y;
1072 uint8_t value = 0;
1073 if (j == (i % numTestPixels)) {
1074 // We must y-invert the texture coords
1075 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
1076 } else {
1077 // We must y-invert the texture coords
1078 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
1079 }
1080 }
1081 }
1082
1083 pt->requestExitAndWait();
1084}
1085
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001086TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -07001087 const int texWidth = 64;
1088 const int texHeight = 66;
1089
1090 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
1091 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
1092 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
1093 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
1094
Jamie Gennisce561372012-03-19 18:33:05 -07001095 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -07001096
Jamie Gennisd69097f2012-08-30 13:28:23 -07001097 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis1876d132011-03-17 16:32:52 -07001098
1099 glClearColor(0.2, 0.2, 0.2, 0.2);
1100 glClear(GL_COLOR_BUFFER_BIT);
1101
Jamie Gennisc8c51522011-06-15 14:24:38 -07001102 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001103 drawTexture();
1104
1105 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
1106 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001107 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
1108 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001109
1110 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001111 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001112 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001113 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
1114 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001115 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001116 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001117 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
1118 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
1119 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001120 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001121 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
1122 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001123 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001124 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001125 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
1126}
1127
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001128TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -07001129 const int texWidth = 64;
1130 const int texHeight = 64;
1131
1132 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
1133 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
1134 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
1135 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
1136
Jamie Gennisce561372012-03-19 18:33:05 -07001137 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -07001138
Jamie Gennisd69097f2012-08-30 13:28:23 -07001139 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis1876d132011-03-17 16:32:52 -07001140
1141 glClearColor(0.2, 0.2, 0.2, 0.2);
1142 glClear(GL_COLOR_BUFFER_BIT);
1143
Jamie Gennisc8c51522011-06-15 14:24:38 -07001144 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001145 drawTexture();
1146
1147 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
1148 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
1149 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
1150 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
1151
1152 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
1153 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
1154 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
1155 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
1156 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
1157 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
1158 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
1159 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
1160 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
1161 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
1162 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
1163 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
1164 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
1165 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
1166 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
1167 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
1168}
1169
Andy McFadden2adaf042012-12-18 09:49:45 -08001170// Tests if GLConsumer and BufferQueue are robust enough
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001171// to handle a special case where updateTexImage is called
1172// in the middle of disconnect. This ordering is enforced
1173// by blocking in the disconnect callback.
1174TEST_F(SurfaceTextureGLTest, DisconnectStressTest) {
1175
1176 class ProducerThread : public Thread {
1177 public:
1178 ProducerThread(const sp<ANativeWindow>& anw):
1179 mANW(anw) {
1180 }
1181
1182 virtual ~ProducerThread() {
1183 }
1184
1185 virtual bool threadLoop() {
1186 ANativeWindowBuffer* anb;
1187
1188 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL);
1189
1190 for (int numFrames =0 ; numFrames < 2; numFrames ++) {
1191
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001192 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1193 &anb) != NO_ERROR) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001194 return false;
1195 }
1196 if (anb == NULL) {
1197 return false;
1198 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001199 if (mANW->queueBuffer(mANW.get(), anb, -1)
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001200 != NO_ERROR) {
1201 return false;
1202 }
1203 }
1204
1205 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_EGL);
1206
1207 return false;
1208 }
1209
1210 private:
1211 sp<ANativeWindow> mANW;
1212 };
1213
1214 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1215
1216 sp<DisconnectWaiter> dw(new DisconnectWaiter());
1217 mST->getBufferQueue()->consumerConnect(dw);
1218
1219
1220 sp<Thread> pt(new ProducerThread(mANW));
1221 pt->run();
1222
Andy McFadden2adaf042012-12-18 09:49:45 -08001223 // eat a frame so GLConsumer will own an at least one slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001224 dw->waitForFrame();
1225 EXPECT_EQ(OK,mST->updateTexImage());
1226
1227 dw->waitForFrame();
Andy McFadden2adaf042012-12-18 09:49:45 -08001228 // Could fail here as GLConsumer thinks it still owns the slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001229 // but bufferQueue has released all slots
1230 EXPECT_EQ(OK,mST->updateTexImage());
1231
1232 dw->finishDisconnect();
1233}
1234
1235
Andy McFadden2adaf042012-12-18 09:49:45 -08001236// This test ensures that the GLConsumer clears the mCurrentTexture
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001237// when it is disconnected and reconnected. Otherwise it will
1238// attempt to release a buffer that it does not owned
1239TEST_F(SurfaceTextureGLTest, DisconnectClearsCurrentTexture) {
1240 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1241
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001242 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1243 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001244
1245 ANativeWindowBuffer *anb;
1246
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001247 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1248 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001249
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));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001252
1253 EXPECT_EQ(OK,mST->updateTexImage());
1254 EXPECT_EQ(OK,mST->updateTexImage());
1255
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001256 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1257 NATIVE_WINDOW_API_EGL));
1258 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1259 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001260
1261 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1262
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001263 EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1264 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001265
1266 // Will fail here if mCurrentTexture is not cleared properly
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001267 mFW->waitForFrame();
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001268 EXPECT_EQ(OK,mST->updateTexImage());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001269
1270 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1271 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001272}
1273
Daniel Lam016c8cb2012-04-03 15:54:58 -07001274TEST_F(SurfaceTextureGLTest, ScaleToWindowMode) {
1275 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1276
1277 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1278 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));
1279
1280 // The producer image size
1281 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1282
1283 // The consumer image size (16 x 9) ratio
1284 mST->setDefaultBufferSize(1280, 720);
1285
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001286 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1287 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001288
1289 ANativeWindowBuffer *anb;
1290
1291 android_native_rect_t odd = {23, 78, 123, 477};
1292 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &odd));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001293 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1294 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001295 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001296 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001297 Rect r = mST->getCurrentCrop();
1298 assertRectEq(Rect(23, 78, 123, 477), r);
1299
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001300 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1301 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001302}
1303
1304// This test ensures the scaling mode does the right thing
1305// ie NATIVE_WINDOW_SCALING_MODE_CROP should crop
1306// the image such that it has the same aspect ratio as the
1307// default buffer size
1308TEST_F(SurfaceTextureGLTest, CroppedScalingMode) {
1309 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1310
1311 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1312 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP));
1313
1314 // The producer image size
1315 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1316
1317 // The consumer image size (16 x 9) ratio
1318 mST->setDefaultBufferSize(1280, 720);
1319
1320 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU);
1321
1322 ANativeWindowBuffer *anb;
1323
1324 // The crop is in the shape of (320, 180) === 16 x 9
1325 android_native_rect_t standard = {10, 20, 330, 200};
1326 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &standard));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001327 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1328 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001329 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001330 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001331 Rect r = mST->getCurrentCrop();
1332 // crop should be the same as crop (same aspect ratio)
1333 assertRectEq(Rect(10, 20, 330, 200), r);
1334
1335 // make this wider then desired aspect 239 x 100 (2.39:1)
1336 android_native_rect_t wide = {20, 30, 259, 130};
1337 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &wide));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001338 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1339 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001340 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001341 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001342 r = mST->getCurrentCrop();
1343 // crop should be the same height, but have cropped left and right borders
1344 // offset is 30.6 px L+, R-
1345 assertRectEq(Rect(51, 30, 228, 130), r);
1346
1347 // This image is taller then desired aspect 400 x 300 (4:3)
1348 android_native_rect_t narrow = {0, 0, 400, 300};
1349 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &narrow));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001350 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1351 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001352 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001353 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001354 r = mST->getCurrentCrop();
1355 // crop should be the same width, but have cropped top and bottom borders
1356 // offset is 37.5 px
1357 assertRectEq(Rect(0, 37, 400, 262), r);
1358
1359 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
1360}
1361
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001362TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
1363 class ProducerThread : public Thread {
1364 public:
1365 ProducerThread(const sp<ANativeWindow>& anw):
1366 mANW(anw),
1367 mDequeueError(NO_ERROR) {
1368 }
1369
1370 virtual ~ProducerThread() {
1371 }
1372
1373 virtual bool threadLoop() {
1374 Mutex::Autolock lock(mMutex);
1375 ANativeWindowBuffer* anb;
1376
1377 // Frame 1
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001378 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1379 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001380 return false;
1381 }
1382 if (anb == NULL) {
1383 return false;
1384 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001385 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001386 != NO_ERROR) {
1387 return false;
1388 }
1389
1390 // Frame 2
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001391 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1392 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001393 return false;
1394 }
1395 if (anb == NULL) {
1396 return false;
1397 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001398 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001399 != NO_ERROR) {
1400 return false;
1401 }
1402
1403 // Frame 3 - error expected
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001404 mDequeueError = native_window_dequeue_buffer_and_wait(mANW.get(),
1405 &anb);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001406 return false;
1407 }
1408
1409 status_t getDequeueError() {
1410 Mutex::Autolock lock(mMutex);
1411 return mDequeueError;
1412 }
1413
1414 private:
1415 sp<ANativeWindow> mANW;
1416 status_t mDequeueError;
1417 Mutex mMutex;
1418 };
1419
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001420 ASSERT_EQ(OK, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -07001421 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001422
1423 sp<Thread> pt(new ProducerThread(mANW));
1424 pt->run();
1425
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001426 mFW->waitForFrame();
1427 mFW->waitForFrame();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001428
1429 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1430 // block waiting for a buffer to become available.
1431 usleep(100000);
1432
1433 mST->abandon();
1434
1435 pt->requestExitAndWait();
1436 ASSERT_EQ(NO_INIT,
1437 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1438}
1439
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001440TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1441 int texHeight = 16;
1442 ANativeWindowBuffer* anb;
1443
1444 GLint maxTextureSize;
1445 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1446
1447 // make sure it works with small textures
1448 mST->setDefaultBufferSize(16, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001449 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1450 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001451 EXPECT_EQ(16, anb->width);
1452 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001453 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001454 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1455
1456 // make sure it works with GL_MAX_TEXTURE_SIZE
1457 mST->setDefaultBufferSize(maxTextureSize, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001458 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1459 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001460 EXPECT_EQ(maxTextureSize, anb->width);
1461 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001462 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001463 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1464
1465 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1466 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001467 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1468 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001469 EXPECT_EQ(maxTextureSize+1, anb->width);
1470 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001471 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001472 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1473}
1474
Jamie Gennis5451d152011-06-08 09:40:45 -07001475/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001476 * This test fixture is for testing GL -> GL texture streaming. It creates an
1477 * EGLSurface and an EGLContext for the image producer to use.
1478 */
1479class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1480protected:
1481 SurfaceTextureGLToGLTest():
1482 mProducerEglSurface(EGL_NO_SURFACE),
1483 mProducerEglContext(EGL_NO_CONTEXT) {
1484 }
1485
1486 virtual void SetUp() {
1487 SurfaceTextureGLTest::SetUp();
1488
Jamie Gennisce561372012-03-19 18:33:05 -07001489 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001490 mANW.get(), NULL);
1491 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1492 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1493
Jamie Gennisce561372012-03-19 18:33:05 -07001494 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001495 EGL_NO_CONTEXT, getContextAttribs());
1496 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1497 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1498 }
1499
1500 virtual void TearDown() {
1501 if (mProducerEglContext != EGL_NO_CONTEXT) {
1502 eglDestroyContext(mEglDisplay, mProducerEglContext);
1503 }
1504 if (mProducerEglSurface != EGL_NO_SURFACE) {
1505 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1506 }
1507 SurfaceTextureGLTest::TearDown();
1508 }
1509
1510 EGLSurface mProducerEglSurface;
1511 EGLContext mProducerEglContext;
1512};
1513
Andy McFadden71f683a2012-09-14 17:21:46 -07001514TEST_F(SurfaceTextureGLToGLTest, TransformHintGetsRespected) {
1515 const uint32_t texWidth = 32;
1516 const uint32_t texHeight = 64;
1517
1518 mST->setDefaultBufferSize(texWidth, texHeight);
1519 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1520
1521 // This test requires 3 buffers to avoid deadlock because we're
1522 // both producer and consumer, and only using one thread.
1523 mST->setDefaultMaxBufferCount(3);
1524
1525 // Do the producer side of things
1526 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1527 mProducerEglSurface, mProducerEglContext));
1528 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1529
1530 // Start a buffer with our chosen size and transform hint moving
1531 // through the system.
1532 glClear(GL_COLOR_BUFFER_BIT); // give the driver something to do
1533 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1534 mST->updateTexImage(); // consume it
1535 // Swap again.
1536 glClear(GL_COLOR_BUFFER_BIT);
1537 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1538 mST->updateTexImage();
1539
1540 // The current buffer should either show the effects of the transform
1541 // hint (in the form of an inverse transform), or show that the
1542 // transform hint has been ignored.
1543 sp<GraphicBuffer> buf = mST->getCurrentBuffer();
1544 if (mST->getCurrentTransform() == NATIVE_WINDOW_TRANSFORM_ROT_270) {
1545 ASSERT_EQ(texWidth, buf->getHeight());
1546 ASSERT_EQ(texHeight, buf->getWidth());
1547 } else {
1548 ASSERT_EQ(texWidth, buf->getWidth());
1549 ASSERT_EQ(texHeight, buf->getHeight());
1550 }
1551
1552 // Reset the transform hint and confirm that it takes.
1553 mST->setTransformHint(0);
1554 glClear(GL_COLOR_BUFFER_BIT);
1555 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1556 mST->updateTexImage();
1557 glClear(GL_COLOR_BUFFER_BIT);
1558 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1559 mST->updateTexImage();
1560
1561 buf = mST->getCurrentBuffer();
1562 ASSERT_EQ((uint32_t) 0, mST->getCurrentTransform());
1563 ASSERT_EQ(texWidth, buf->getWidth());
1564 ASSERT_EQ(texHeight, buf->getHeight());
1565}
1566
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001567TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1568 const int texWidth = 64;
1569 const int texHeight = 64;
1570
1571 mST->setDefaultBufferSize(texWidth, texHeight);
1572
Jamie Gennis46975282012-08-30 18:35:50 -07001573 // This test requires 3 buffers to complete run on a single thread.
1574 mST->setDefaultMaxBufferCount(3);
1575
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001576 // Do the producer side of things
1577 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1578 mProducerEglSurface, mProducerEglContext));
1579 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1580
1581 // This is needed to ensure we pick up a buffer of the correct size.
1582 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1583
1584 glClearColor(0.6, 0.6, 0.6, 0.6);
1585 glClear(GL_COLOR_BUFFER_BIT);
1586
1587 glEnable(GL_SCISSOR_TEST);
1588 glScissor(4, 4, 4, 4);
1589 glClearColor(1.0, 0.0, 0.0, 1.0);
1590 glClear(GL_COLOR_BUFFER_BIT);
1591
1592 glScissor(24, 48, 4, 4);
1593 glClearColor(0.0, 1.0, 0.0, 1.0);
1594 glClear(GL_COLOR_BUFFER_BIT);
1595
1596 glScissor(37, 17, 4, 4);
1597 glClearColor(0.0, 0.0, 1.0, 1.0);
1598 glClear(GL_COLOR_BUFFER_BIT);
1599
1600 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1601
1602 // Do the consumer side of things
1603 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1604 mEglContext));
1605 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1606
1607 glDisable(GL_SCISSOR_TEST);
1608
Jamie Gennisd69097f2012-08-30 13:28:23 -07001609 // Skip the first frame, which was empty
1610 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1611 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001612
1613 glClearColor(0.2, 0.2, 0.2, 0.2);
1614 glClear(GL_COLOR_BUFFER_BIT);
1615
1616 glViewport(0, 0, texWidth, texHeight);
1617 drawTexture();
1618
1619 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1620 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1621 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1622 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1623
1624 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1625 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1626 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1627 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1628 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1629 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1630 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1631 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1632 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1633 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1634 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1635 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1636 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1637 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1638 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1639 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1640}
1641
1642TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001643 sp<GraphicBuffer> buffers[2];
1644
Jamie Gennise3603d72011-11-19 21:20:17 -08001645 // This test requires async mode to run on a single thread.
1646 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1647 mProducerEglSurface, mProducerEglContext));
1648 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1649 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1650 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1651
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001652 for (int i = 0; i < 2; i++) {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001653 // Produce a frame
1654 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1655 mProducerEglSurface, mProducerEglContext));
1656 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1657 glClear(GL_COLOR_BUFFER_BIT);
1658 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1659
1660 // Consume a frame
1661 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1662 mEglContext));
1663 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001664 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001665 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001666 buffers[i] = mST->getCurrentBuffer();
1667 }
1668
1669 // Destroy the GL texture object to release its ref on buffers[2].
1670 GLuint texID = TEX_ID;
1671 glDeleteTextures(1, &texID);
1672
1673 // Destroy the EGLSurface
1674 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1675 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001676 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001677
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001678 // This test should have the only reference to buffer 0.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001679 EXPECT_EQ(1, buffers[0]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001680
Andy McFadden2adaf042012-12-18 09:49:45 -08001681 // The GLConsumer should hold a single reference to buffer 1 in its
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001682 // mCurrentBuffer member. All of the references in the slots should have
1683 // been released.
1684 EXPECT_EQ(2, buffers[1]->getStrongCount());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001685}
1686
1687TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1688 sp<GraphicBuffer> buffers[3];
1689
Jamie Gennise3603d72011-11-19 21:20:17 -08001690 // This test requires async mode to run on a single thread.
1691 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1692 mProducerEglSurface, mProducerEglContext));
1693 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1694 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1695 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1696
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001697 for (int i = 0; i < 3; i++) {
1698 // Produce a frame
1699 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1700 mProducerEglSurface, mProducerEglContext));
1701 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1702 glClear(GL_COLOR_BUFFER_BIT);
1703 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1704 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1705
1706 // Consume a frame
1707 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1708 mEglContext));
1709 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001710 mFW->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001711 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1712 buffers[i] = mST->getCurrentBuffer();
1713 }
1714
Andy McFadden2adaf042012-12-18 09:49:45 -08001715 // Abandon the GLConsumer, releasing the ref that the GLConsumer has
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001716 // on buffers[2].
1717 mST->abandon();
1718
1719 // Destroy the GL texture object to release its ref on buffers[2].
1720 GLuint texID = TEX_ID;
1721 glDeleteTextures(1, &texID);
1722
1723 // Destroy the EGLSurface.
1724 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1725 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001726 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001727
1728 EXPECT_EQ(1, buffers[0]->getStrongCount());
1729 EXPECT_EQ(1, buffers[1]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001730
1731 // Depending on how lazily the GL driver dequeues buffers, we may end up
1732 // with either two or three total buffers. If there are three, make sure
1733 // the last one was properly down-ref'd.
1734 if (buffers[2] != buffers[0]) {
1735 EXPECT_EQ(1, buffers[2]->getStrongCount());
1736 }
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001737}
1738
Mathias Agopian7589b2a2013-03-08 13:18:52 -08001739TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentBeforeConsumerDeathUnrefsBuffers) {
1740 sp<GraphicBuffer> buffer;
1741
1742 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1743 mProducerEglSurface, mProducerEglContext));
1744
1745 // Produce a frame
1746 glClear(GL_COLOR_BUFFER_BIT);
1747 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1748 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1749
1750 // Destroy the EGLSurface.
1751 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1752 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1753 mProducerEglSurface = EGL_NO_SURFACE;
1754 mSTC.clear();
1755 mANW.clear();
1756 mTextureRenderer.clear();
1757
1758 // Consume a frame
1759 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1760 buffer = mST->getCurrentBuffer();
1761
1762 // Destroy the GL texture object to release its ref
1763 GLuint texID = TEX_ID;
1764 glDeleteTextures(1, &texID);
1765
1766 // make un-current, all references to buffer should be gone
1767 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
1768 EGL_NO_SURFACE, EGL_NO_CONTEXT));
1769
1770 // Destroy consumer
1771 mST.clear();
1772
1773 EXPECT_EQ(1, buffer->getStrongCount());
1774}
1775
1776TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentAfterConsumerDeathUnrefsBuffers) {
1777 sp<GraphicBuffer> buffer;
1778
1779 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1780 mProducerEglSurface, mProducerEglContext));
1781
1782 // Produce a frame
1783 glClear(GL_COLOR_BUFFER_BIT);
1784 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1785 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1786
1787 // Destroy the EGLSurface.
1788 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1789 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1790 mProducerEglSurface = EGL_NO_SURFACE;
1791 mSTC.clear();
1792 mANW.clear();
1793 mTextureRenderer.clear();
1794
1795 // Consume a frame
1796 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1797 buffer = mST->getCurrentBuffer();
1798
1799 // Destroy the GL texture object to release its ref
1800 GLuint texID = TEX_ID;
1801 glDeleteTextures(1, &texID);
1802
1803 // Destroy consumer
1804 mST.clear();
1805
1806 // make un-current, all references to buffer should be gone
1807 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
1808 EGL_NO_SURFACE, EGL_NO_CONTEXT));
1809
1810 EXPECT_EQ(1, buffer->getStrongCount());
1811}
1812
1813
Jamie Gennis59769462011-11-19 18:04:43 -08001814TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) {
1815 // This test requires 3 buffers to run on a single thread.
Jamie Gennis31a353d2012-08-24 17:25:13 -07001816 mST->setDefaultMaxBufferCount(3);
Jamie Gennis59769462011-11-19 18:04:43 -08001817
1818 ASSERT_TRUE(mST->isSynchronousMode());
1819
1820 for (int i = 0; i < 10; i++) {
1821 // Produce a frame
1822 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1823 mProducerEglSurface, mProducerEglContext));
1824 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1825 glClear(GL_COLOR_BUFFER_BIT);
1826 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1827 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1828
1829 // Consume a frame
1830 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1831 mEglContext));
1832 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1833 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1834 }
1835
1836 ASSERT_TRUE(mST->isSynchronousMode());
1837}
1838
Jamie Gennisc2c38022012-04-11 17:20:03 -07001839TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
1840 enum { texWidth = 64 };
1841 enum { texHeight = 64 };
1842
Jamie Gennis46975282012-08-30 18:35:50 -07001843 // This test requires 3 buffers to complete run on a single thread.
1844 mST->setDefaultMaxBufferCount(3);
1845
Jamie Gennisc2c38022012-04-11 17:20:03 -07001846 // Set the user buffer size.
1847 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1848
1849 // Do the producer side of things
1850 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1851 mProducerEglSurface, mProducerEglContext));
1852 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1853
1854 // This is needed to ensure we pick up a buffer of the correct size.
1855 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1856
1857 glClearColor(0.6, 0.6, 0.6, 0.6);
1858 glClear(GL_COLOR_BUFFER_BIT);
1859
1860 glEnable(GL_SCISSOR_TEST);
1861 glScissor(4, 4, 1, 1);
1862 glClearColor(1.0, 0.0, 0.0, 1.0);
1863 glClear(GL_COLOR_BUFFER_BIT);
1864
1865 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1866
1867 // Do the consumer side of things
1868 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1869 mEglContext));
1870 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1871
1872 glDisable(GL_SCISSOR_TEST);
1873
Jamie Gennisd69097f2012-08-30 13:28:23 -07001874 // Skip the first frame, which was empty
1875 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1876 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001877
1878 glClearColor(0.2, 0.2, 0.2, 0.2);
1879 glClear(GL_COLOR_BUFFER_BIT);
1880
1881 glViewport(0, 0, texWidth, texHeight);
1882 drawTexture();
1883
1884 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1885 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1886 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1887 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1888
1889 EXPECT_TRUE(checkPixel( 4, 4, 255, 0, 0, 255));
1890 EXPECT_TRUE(checkPixel( 5, 5, 153, 153, 153, 153));
1891 EXPECT_TRUE(checkPixel( 3, 3, 153, 153, 153, 153));
1892 EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153));
1893 EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153));
1894}
1895
1896TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
1897 enum { texWidth = 64 };
1898 enum { texHeight = 16 };
1899
Jamie Gennis46975282012-08-30 18:35:50 -07001900 // This test requires 3 buffers to complete run on a single thread.
1901 mST->setDefaultMaxBufferCount(3);
1902
Jamie Gennisc2c38022012-04-11 17:20:03 -07001903 // Set the transform hint.
1904 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1905
1906 // Set the user buffer size.
1907 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1908
1909 // Do the producer side of things
1910 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1911 mProducerEglSurface, mProducerEglContext));
1912 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1913
1914 // This is needed to ensure we pick up a buffer of the correct size and the
1915 // new rotation hint.
1916 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1917
1918 glClearColor(0.6, 0.6, 0.6, 0.6);
1919 glClear(GL_COLOR_BUFFER_BIT);
1920
1921 glEnable(GL_SCISSOR_TEST);
1922 glScissor(24, 4, 1, 1);
1923 glClearColor(1.0, 0.0, 0.0, 1.0);
1924 glClear(GL_COLOR_BUFFER_BIT);
1925
1926 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1927
1928 // Do the consumer side of things
1929 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1930 mEglContext));
1931 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1932
1933 glDisable(GL_SCISSOR_TEST);
1934
Jamie Gennisd69097f2012-08-30 13:28:23 -07001935 // Skip the first frame, which was empty
1936 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1937 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001938
1939 glClearColor(0.2, 0.2, 0.2, 0.2);
1940 glClear(GL_COLOR_BUFFER_BIT);
1941
1942 glViewport(0, 0, texWidth, texHeight);
1943 drawTexture();
1944
1945 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1946 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1947 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1948 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1949
1950 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1951 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1952 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1953 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1954 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1955}
1956
1957TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
1958 enum { texWidth = 64 };
1959 enum { texHeight = 16 };
1960
Jamie Gennis46975282012-08-30 18:35:50 -07001961 // This test requires 3 buffers to complete run on a single thread.
1962 mST->setDefaultMaxBufferCount(3);
1963
Jamie Gennisc2c38022012-04-11 17:20:03 -07001964 // Set the transform hint.
1965 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1966
1967 // Set the default buffer size.
1968 mST->setDefaultBufferSize(texWidth, texHeight);
1969
1970 // Do the producer side of things
1971 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1972 mProducerEglSurface, mProducerEglContext));
1973 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1974
1975 // This is needed to ensure we pick up a buffer of the correct size and the
1976 // new rotation hint.
1977 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1978
1979 glClearColor(0.6, 0.6, 0.6, 0.6);
1980 glClear(GL_COLOR_BUFFER_BIT);
1981
1982 glEnable(GL_SCISSOR_TEST);
1983 glScissor(24, 4, 1, 1);
1984 glClearColor(1.0, 0.0, 0.0, 1.0);
1985 glClear(GL_COLOR_BUFFER_BIT);
1986
1987 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1988
1989 // Do the consumer side of things
1990 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1991 mEglContext));
1992 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1993
1994 glDisable(GL_SCISSOR_TEST);
1995
Jamie Gennisd69097f2012-08-30 13:28:23 -07001996 // Skip the first frame, which was empty
1997 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1998 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001999
2000 glClearColor(0.2, 0.2, 0.2, 0.2);
2001 glClear(GL_COLOR_BUFFER_BIT);
2002
2003 glViewport(0, 0, texWidth, texHeight);
2004 drawTexture();
2005
2006 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
2007 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
2008 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
2009 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
2010
2011 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
2012 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
2013 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
2014 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
2015 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
2016}
2017
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002018/*
2019 * This test fixture is for testing GL -> GL texture streaming from one thread
2020 * to another. It contains functionality to create a producer thread that will
2021 * perform GL rendering to an ANativeWindow that feeds frames to a
Andy McFadden2adaf042012-12-18 09:49:45 -08002022 * GLConsumer. Additionally it supports interlocking the producer and
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002023 * consumer threads so that a specific sequence of calls can be
2024 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07002025 *
2026 * The intended usage is as follows:
2027 *
2028 * TEST_F(...) {
2029 * class PT : public ProducerThread {
2030 * virtual void render() {
2031 * ...
2032 * swapBuffers();
2033 * }
2034 * };
2035 *
2036 * runProducerThread(new PT());
2037 *
2038 * // The order of these calls will vary from test to test and may include
2039 * // multiple frames and additional operations (e.g. GL rendering from the
2040 * // texture).
2041 * fc->waitForFrame();
2042 * mST->updateTexImage();
2043 * fc->finishFrame();
2044 * }
2045 *
2046 */
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002047class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07002048protected:
2049
2050 // ProducerThread is an abstract base class to simplify the creation of
2051 // OpenGL ES frame producer threads.
2052 class ProducerThread : public Thread {
2053 public:
2054 virtual ~ProducerThread() {
2055 }
2056
2057 void setEglObjects(EGLDisplay producerEglDisplay,
2058 EGLSurface producerEglSurface,
2059 EGLContext producerEglContext) {
2060 mProducerEglDisplay = producerEglDisplay;
2061 mProducerEglSurface = producerEglSurface;
2062 mProducerEglContext = producerEglContext;
2063 }
2064
2065 virtual bool threadLoop() {
2066 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
2067 mProducerEglSurface, mProducerEglContext);
2068 render();
2069 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2070 EGL_NO_CONTEXT);
2071 return false;
2072 }
2073
2074 protected:
2075 virtual void render() = 0;
2076
2077 void swapBuffers() {
2078 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
2079 }
2080
2081 EGLDisplay mProducerEglDisplay;
2082 EGLSurface mProducerEglSurface;
2083 EGLContext mProducerEglContext;
2084 };
2085
2086 // FrameCondition is a utility class for interlocking between the producer
2087 // and consumer threads. The FrameCondition object should be created and
2088 // destroyed in the consumer thread only. The consumer thread should set
Andy McFadden2adaf042012-12-18 09:49:45 -08002089 // the FrameCondition as the FrameAvailableListener of the GLConsumer,
Jamie Gennis5451d152011-06-08 09:40:45 -07002090 // and should call both waitForFrame and finishFrame once for each expected
2091 // frame.
2092 //
2093 // This interlocking relies on the fact that onFrameAvailable gets called
Andy McFadden2adaf042012-12-18 09:49:45 -08002094 // synchronously from GLConsumer::queueBuffer.
2095 class FrameCondition : public GLConsumer::FrameAvailableListener {
Jamie Gennis5451d152011-06-08 09:40:45 -07002096 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002097 FrameCondition():
2098 mFrameAvailable(false),
2099 mFrameFinished(false) {
2100 }
2101
Jamie Gennis5451d152011-06-08 09:40:45 -07002102 // waitForFrame waits for the next frame to arrive. This should be
2103 // called from the consumer thread once for every frame expected by the
2104 // test.
2105 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07002106 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01002107 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002108 while (!mFrameAvailable) {
2109 mFrameAvailableCondition.wait(mMutex);
2110 }
2111 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01002112 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07002113 }
2114
2115 // Allow the producer to return from its swapBuffers call and continue
2116 // on to produce the next frame. This should be called by the consumer
2117 // thread once for every frame expected by the test.
2118 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07002119 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01002120 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002121 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07002122 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01002123 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07002124 }
2125
Andy McFadden2adaf042012-12-18 09:49:45 -08002126 // This should be called by GLConsumer on the producer thread.
Jamie Gennis5451d152011-06-08 09:40:45 -07002127 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07002128 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01002129 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002130 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07002131 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002132 while (!mFrameFinished) {
2133 mFrameFinishCondition.wait(mMutex);
2134 }
2135 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01002136 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07002137 }
2138
2139 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002140 bool mFrameAvailable;
2141 bool mFrameFinished;
2142
Jamie Gennis5451d152011-06-08 09:40:45 -07002143 Mutex mMutex;
2144 Condition mFrameAvailableCondition;
2145 Condition mFrameFinishCondition;
2146 };
2147
Jamie Gennis5451d152011-06-08 09:40:45 -07002148 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002149 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07002150 mFC = new FrameCondition();
2151 mST->setFrameAvailableListener(mFC);
2152 }
2153
2154 virtual void TearDown() {
2155 if (mProducerThread != NULL) {
2156 mProducerThread->requestExitAndWait();
2157 }
Jamie Gennis5451d152011-06-08 09:40:45 -07002158 mProducerThread.clear();
2159 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002160 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07002161 }
2162
2163 void runProducerThread(const sp<ProducerThread> producerThread) {
2164 ASSERT_TRUE(mProducerThread == NULL);
2165 mProducerThread = producerThread;
2166 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
2167 mProducerEglContext);
2168 producerThread->run();
2169 }
2170
Jamie Gennis5451d152011-06-08 09:40:45 -07002171 sp<ProducerThread> mProducerThread;
2172 sp<FrameCondition> mFC;
2173};
2174
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002175TEST_F(SurfaceTextureGLThreadToGLTest,
2176 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002177 class PT : public ProducerThread {
2178 virtual void render() {
2179 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2180 glClear(GL_COLOR_BUFFER_BIT);
2181 swapBuffers();
2182 }
2183 };
2184
2185 runProducerThread(new PT());
2186
2187 mFC->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07002188 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07002189 mFC->finishFrame();
2190
2191 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08002192}
Jamie Gennis5451d152011-06-08 09:40:45 -07002193
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002194TEST_F(SurfaceTextureGLThreadToGLTest,
2195 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002196 class PT : public ProducerThread {
2197 virtual void render() {
2198 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2199 glClear(GL_COLOR_BUFFER_BIT);
2200 swapBuffers();
2201 }
2202 };
2203
2204 runProducerThread(new PT());
2205
2206 mFC->waitForFrame();
2207 mFC->finishFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07002208 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07002209
2210 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2211}
2212
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002213TEST_F(SurfaceTextureGLThreadToGLTest,
2214 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002215 enum { NUM_ITERATIONS = 1024 };
2216
2217 class PT : public ProducerThread {
2218 virtual void render() {
2219 for (int i = 0; i < NUM_ITERATIONS; i++) {
2220 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2221 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002222 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002223 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002224 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002225 }
2226 }
2227 };
2228
2229 runProducerThread(new PT());
2230
2231 for (int i = 0; i < NUM_ITERATIONS; i++) {
2232 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01002233 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002234 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002235 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002236 mFC->finishFrame();
2237
2238 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2239 }
2240}
2241
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002242TEST_F(SurfaceTextureGLThreadToGLTest,
2243 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002244 enum { NUM_ITERATIONS = 1024 };
2245
2246 class PT : public ProducerThread {
2247 virtual void render() {
2248 for (int i = 0; i < NUM_ITERATIONS; i++) {
2249 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2250 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002251 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002252 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002253 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002254 }
2255 }
2256 };
2257
2258 runProducerThread(new PT());
2259
2260 for (int i = 0; i < NUM_ITERATIONS; i++) {
2261 mFC->waitForFrame();
2262 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002263 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002264 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002265 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002266
2267 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2268 }
2269}
2270
Jamie Gennis6e502192011-07-21 14:31:31 -07002271// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002272TEST_F(SurfaceTextureGLThreadToGLTest,
2273 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07002274 enum { NUM_ITERATIONS = 64 };
2275
2276 class PT : public ProducerThread {
2277 virtual void render() {
2278 for (int i = 0; i < NUM_ITERATIONS; i++) {
2279 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2280 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002281 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002282 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002283 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002284 }
2285 }
2286 };
2287
2288 ASSERT_EQ(OK, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -07002289 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis6e502192011-07-21 14:31:31 -07002290
2291 runProducerThread(new PT());
2292
2293 // Allow three frames to be rendered and queued before starting the
2294 // rendering in this thread. For the latter two frames we don't call
2295 // updateTexImage so the next dequeue from the producer thread will block
2296 // waiting for a frame to become available.
2297 mFC->waitForFrame();
2298 mFC->finishFrame();
2299
2300 // We must call updateTexImage to consume the first frame so that the
2301 // SurfaceTexture is able to reduce the buffer count to 2. This is because
2302 // the GL driver may dequeue a buffer when the EGLSurface is created, and
Jamie Gennis31a353d2012-08-24 17:25:13 -07002303 // that happens before we call setDefaultMaxBufferCount. It's possible that the
Jamie Gennis6e502192011-07-21 14:31:31 -07002304 // driver does not dequeue a buffer at EGLSurface creation time, so we
2305 // cannot rely on this to cause the second dequeueBuffer call to block.
Jamie Gennisd69097f2012-08-30 13:28:23 -07002306 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07002307
2308 mFC->waitForFrame();
2309 mFC->finishFrame();
2310 mFC->waitForFrame();
2311 mFC->finishFrame();
2312
2313 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
2314 // block waiting for a buffer to become available.
2315 usleep(100000);
2316
2317 // Render and present a number of images. This thread should not be blocked
2318 // by the fact that the producer thread is blocking in dequeue.
2319 for (int i = 0; i < NUM_ITERATIONS; i++) {
2320 glClear(GL_COLOR_BUFFER_BIT);
2321 eglSwapBuffers(mEglDisplay, mEglSurface);
2322 }
2323
2324 // Consume the two pending buffers to unblock the producer thread.
Jamie Gennisd69097f2012-08-30 13:28:23 -07002325 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2326 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07002327
2328 // Consume the remaining buffers from the producer thread.
2329 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
2330 mFC->waitForFrame();
2331 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002332 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002333 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002334 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002335 }
2336}
2337
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002338class SurfaceTextureFBOTest : public SurfaceTextureGLTest {
2339protected:
2340
2341 virtual void SetUp() {
2342 SurfaceTextureGLTest::SetUp();
2343
2344 glGenFramebuffers(1, &mFbo);
2345 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2346
2347 glGenTextures(1, &mFboTex);
2348 glBindTexture(GL_TEXTURE_2D, mFboTex);
2349 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getSurfaceWidth(),
2350 getSurfaceHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2351 glBindTexture(GL_TEXTURE_2D, 0);
2352 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2353
2354 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2355 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2356 GL_TEXTURE_2D, mFboTex, 0);
2357 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2358 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2359 }
2360
2361 virtual void TearDown() {
2362 SurfaceTextureGLTest::TearDown();
2363
2364 glDeleteTextures(1, &mFboTex);
2365 glDeleteFramebuffers(1, &mFbo);
2366 }
2367
2368 GLuint mFbo;
2369 GLuint mFboTex;
2370};
2371
2372// This test is intended to verify that proper synchronization is done when
2373// rendering into an FBO.
2374TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) {
2375 const int texWidth = 64;
2376 const int texHeight = 64;
2377
2378 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
2379 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
2380 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
2381 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
2382
2383 android_native_buffer_t* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002384 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2385 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002386 ASSERT_TRUE(anb != NULL);
2387
2388 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002389
2390 // Fill the buffer with green
2391 uint8_t* img = NULL;
2392 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
2393 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255,
2394 0, 255);
2395 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002396 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
2397 -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002398
2399 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2400
2401 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2402 drawTexture();
2403 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2404
2405 for (int i = 0; i < 4; i++) {
2406 SCOPED_TRACE(String8::format("frame %d", i).string());
2407
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002408 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2409 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002410 ASSERT_TRUE(anb != NULL);
2411
2412 buf = new GraphicBuffer(anb, false);
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002413
2414 // Fill the buffer with red
2415 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
2416 (void**)(&img)));
2417 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0,
2418 0, 255);
2419 ASSERT_EQ(NO_ERROR, buf->unlock());
2420 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002421 buf->getNativeBuffer(), -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002422
2423 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2424
2425 drawTexture();
2426
2427 EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255));
2428 }
2429
2430 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2431
2432 EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255));
2433}
2434
Jamie Gennisce561372012-03-19 18:33:05 -07002435class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest {
2436protected:
Jamie Gennis74bed552012-03-28 19:05:54 -07002437 enum { SECOND_TEX_ID = 123 };
2438 enum { THIRD_TEX_ID = 456 };
2439
Jamie Gennisce561372012-03-19 18:33:05 -07002440 SurfaceTextureMultiContextGLTest():
2441 mSecondEglContext(EGL_NO_CONTEXT) {
2442 }
2443
2444 virtual void SetUp() {
2445 SurfaceTextureGLTest::SetUp();
2446
Jamie Gennis74bed552012-03-28 19:05:54 -07002447 // Set up the secondary context and texture renderer.
Jamie Gennisce561372012-03-19 18:33:05 -07002448 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2449 EGL_NO_CONTEXT, getContextAttribs());
2450 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2451 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext);
Jamie Gennis74bed552012-03-28 19:05:54 -07002452
2453 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2454 mSecondEglContext));
2455 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2456 mSecondTextureRenderer = new TextureRenderer(SECOND_TEX_ID, mST);
2457 ASSERT_NO_FATAL_FAILURE(mSecondTextureRenderer->SetUp());
2458
2459 // Set up the tertiary context and texture renderer.
2460 mThirdEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2461 EGL_NO_CONTEXT, getContextAttribs());
2462 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2463 ASSERT_NE(EGL_NO_CONTEXT, mThirdEglContext);
2464
2465 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2466 mThirdEglContext));
2467 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2468 mThirdTextureRenderer = new TextureRenderer(THIRD_TEX_ID, mST);
2469 ASSERT_NO_FATAL_FAILURE(mThirdTextureRenderer->SetUp());
2470
2471 // Switch back to the primary context to start the tests.
2472 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2473 mEglContext));
Jamie Gennisce561372012-03-19 18:33:05 -07002474 }
2475
2476 virtual void TearDown() {
Jamie Gennis74bed552012-03-28 19:05:54 -07002477 if (mThirdEglContext != EGL_NO_CONTEXT) {
2478 eglDestroyContext(mEglDisplay, mThirdEglContext);
2479 }
Jamie Gennisce561372012-03-19 18:33:05 -07002480 if (mSecondEglContext != EGL_NO_CONTEXT) {
2481 eglDestroyContext(mEglDisplay, mSecondEglContext);
2482 }
2483 SurfaceTextureGLTest::TearDown();
2484 }
2485
2486 EGLContext mSecondEglContext;
Jamie Gennis74bed552012-03-28 19:05:54 -07002487 sp<TextureRenderer> mSecondTextureRenderer;
2488
2489 EGLContext mThirdEglContext;
2490 sp<TextureRenderer> mThirdTextureRenderer;
Jamie Gennisce561372012-03-19 18:33:05 -07002491};
2492
2493TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) {
Jamie Gennisce561372012-03-19 18:33:05 -07002494 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2495
2496 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002497 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002498 ASSERT_EQ(OK, mST->updateTexImage());
Jamie Gennisce561372012-03-19 18:33:05 -07002499
2500 // Attempt to latch the texture on the secondary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07002501 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
Jamie Gennisce561372012-03-19 18:33:05 -07002502 mSecondEglContext));
2503 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -07002504 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2505}
2506
2507TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002508 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2509
2510 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002511 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002512 ASSERT_EQ(OK, mST->updateTexImage());
2513
2514 // Detach from the primary context.
2515 ASSERT_EQ(OK, mST->detachFromContext());
2516
2517 // Check that the GL texture was deleted.
2518 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2519}
2520
2521TEST_F(SurfaceTextureMultiContextGLTest,
2522 DetachFromContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002523 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2524
2525 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002526 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002527 ASSERT_EQ(OK, mST->updateTexImage());
2528
2529 // Detach from the primary context.
2530 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2531 ASSERT_EQ(OK, mST->detachFromContext());
2532
2533 // Check that the GL texture was deleted.
2534 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2535}
2536
2537TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002538 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2539
2540 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002541 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002542 ASSERT_EQ(OK, mST->updateTexImage());
2543
2544 // Attempt to detach from the primary context.
2545 mST->abandon();
2546 ASSERT_EQ(NO_INIT, mST->detachFromContext());
2547}
2548
2549TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002550 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2551
2552 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002553 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002554 ASSERT_EQ(OK, mST->updateTexImage());
2555
2556 // Detach from the primary context.
2557 ASSERT_EQ(OK, mST->detachFromContext());
2558
2559 // Attempt to detach from the primary context again.
2560 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2561}
2562
2563TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002564 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2565
2566 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002567 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002568 ASSERT_EQ(OK, mST->updateTexImage());
2569
2570 // Make there be no current display.
2571 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2572 EGL_NO_CONTEXT));
2573 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2574
2575 // Attempt to detach from the primary context.
2576 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2577}
2578
2579TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002580 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2581
2582 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002583 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002584 ASSERT_EQ(OK, mST->updateTexImage());
2585
2586 // Make current context be incorrect.
2587 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2588 mSecondEglContext));
2589 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2590
2591 // Attempt to detach from the primary context.
2592 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2593}
2594
2595TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002596 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2597
2598 // Detach from the primary context.
2599 ASSERT_EQ(OK, mST->detachFromContext());
2600
2601 // Attempt to latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002602 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002603 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2604}
2605
2606TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002607 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2608
2609 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002610 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002611 ASSERT_EQ(OK, mST->updateTexImage());
2612
2613 // Detach from the primary context.
2614 ASSERT_EQ(OK, mST->detachFromContext());
2615
2616 // Attach to the secondary context.
2617 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2618 mSecondEglContext));
2619 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2620
2621 // Verify that the texture object was created and bound.
2622 GLint texBinding = -1;
2623 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2624 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2625
2626 // Try to use the texture from the secondary context.
2627 glClearColor(0.2, 0.2, 0.2, 0.2);
2628 glClear(GL_COLOR_BUFFER_BIT);
2629 glViewport(0, 0, 1, 1);
2630 mSecondTextureRenderer->drawTexture();
2631 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2632 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2633}
2634
2635TEST_F(SurfaceTextureMultiContextGLTest,
2636 AttachToContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002637 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2638
2639 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002640 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002641 ASSERT_EQ(OK, mST->updateTexImage());
2642
2643 // Detach from the primary context.
2644 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2645 ASSERT_EQ(OK, mST->detachFromContext());
2646
2647 // Attach to the secondary context.
2648 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2649 mSecondEglContext));
2650 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2651
2652 // Verify that the texture object was created and bound.
2653 GLint texBinding = -1;
2654 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2655 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2656
2657 // Try to use the texture from the secondary context.
2658 glClearColor(0.2, 0.2, 0.2, 0.2);
2659 glClear(GL_COLOR_BUFFER_BIT);
2660 glViewport(0, 0, 1, 1);
2661 mSecondTextureRenderer->drawTexture();
2662 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2663 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2664}
2665
2666TEST_F(SurfaceTextureMultiContextGLTest,
2667 AttachToContextSucceedsBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002668 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2669
2670 // Detach from the primary context.
2671 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2672 ASSERT_EQ(OK, mST->detachFromContext());
2673
2674 // Attach to the secondary context.
2675 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2676 mSecondEglContext));
2677 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2678
2679 // Verify that the texture object was created and bound.
2680 GLint texBinding = -1;
2681 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2682 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2683
2684 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002685 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002686 ASSERT_EQ(OK, mST->updateTexImage());
2687
2688 // Try to use the texture from the secondary context.
2689 glClearColor(0.2, 0.2, 0.2, 0.2);
2690 glClear(GL_COLOR_BUFFER_BIT);
2691 glViewport(0, 0, 1, 1);
2692 mSecondTextureRenderer->drawTexture();
2693 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2694 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2695}
2696
2697TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002698 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2699
2700 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002701 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002702 ASSERT_EQ(OK, mST->updateTexImage());
2703
2704 // Detach from the primary context.
2705 ASSERT_EQ(OK, mST->detachFromContext());
2706
2707 // Attempt to attach to the secondary context.
2708 mST->abandon();
2709
2710 // Attempt to attach to the primary context.
2711 ASSERT_EQ(NO_INIT, mST->attachToContext(SECOND_TEX_ID));
2712}
2713
2714TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002715 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2716
2717 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002718 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002719 ASSERT_EQ(OK, mST->updateTexImage());
2720
2721 // Attempt to attach to the primary context.
2722 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2723}
2724
2725TEST_F(SurfaceTextureMultiContextGLTest,
2726 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002727 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2728
2729 // Attempt to attach to the primary context.
2730 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2731}
2732
2733TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002734 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2735
2736 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002737 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002738 ASSERT_EQ(OK, mST->updateTexImage());
2739
2740 // Detach from the primary context.
2741 ASSERT_EQ(OK, mST->detachFromContext());
2742
2743 // Make there be no current display.
2744 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2745 EGL_NO_CONTEXT));
2746 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2747
2748 // Attempt to attach with no context current.
2749 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2750}
2751
2752TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002753 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2754
2755 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002756 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002757 ASSERT_EQ(OK, mST->updateTexImage());
2758
2759 // Detach from the primary context.
2760 ASSERT_EQ(OK, mST->detachFromContext());
2761
2762 // Attach to the secondary context.
2763 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2764 mSecondEglContext));
2765 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2766
2767 // Detach from the secondary context.
2768 ASSERT_EQ(OK, mST->detachFromContext());
2769
2770 // Attach to the tertiary context.
2771 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2772 mThirdEglContext));
2773 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2774
2775 // Verify that the texture object was created and bound.
2776 GLint texBinding = -1;
2777 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2778 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2779
2780 // Try to use the texture from the tertiary context.
2781 glClearColor(0.2, 0.2, 0.2, 0.2);
2782 glClear(GL_COLOR_BUFFER_BIT);
2783 glViewport(0, 0, 1, 1);
2784 mThirdTextureRenderer->drawTexture();
2785 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2786 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2787}
2788
2789TEST_F(SurfaceTextureMultiContextGLTest,
2790 AttachToContextSucceedsTwiceBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002791 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2792
2793 // Detach from the primary context.
2794 ASSERT_EQ(OK, mST->detachFromContext());
2795
2796 // Attach to the secondary context.
2797 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2798 mSecondEglContext));
2799 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2800
2801 // Detach from the secondary context.
2802 ASSERT_EQ(OK, mST->detachFromContext());
2803
2804 // Attach to the tertiary context.
2805 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2806 mThirdEglContext));
2807 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2808
2809 // Verify that the texture object was created and bound.
2810 GLint texBinding = -1;
2811 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2812 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2813
2814 // Latch the texture contents on the tertiary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002815 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002816 ASSERT_EQ(OK, mST->updateTexImage());
2817
2818 // Try to use the texture from the tertiary context.
2819 glClearColor(0.2, 0.2, 0.2, 0.2);
2820 glClear(GL_COLOR_BUFFER_BIT);
2821 glViewport(0, 0, 1, 1);
2822 mThirdTextureRenderer->drawTexture();
2823 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2824 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
Jamie Gennisce561372012-03-19 18:33:05 -07002825}
2826
Jesse Hall90ed8502012-05-16 23:44:34 -07002827TEST_F(SurfaceTextureMultiContextGLTest,
2828 UpdateTexImageSucceedsForBufferConsumedBeforeDetach) {
2829 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -07002830 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2));
Jesse Hall90ed8502012-05-16 23:44:34 -07002831
2832 // produce two frames and consume them both on the primary context
2833 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2834 mFW->waitForFrame();
2835 ASSERT_EQ(OK, mST->updateTexImage());
2836
2837 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2838 mFW->waitForFrame();
2839 ASSERT_EQ(OK, mST->updateTexImage());
2840
2841 // produce one more frame
2842 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2843
2844 // Detach from the primary context and attach to the secondary context
2845 ASSERT_EQ(OK, mST->detachFromContext());
2846 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2847 mSecondEglContext));
2848 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2849
2850 // Consume final frame on secondary context
2851 mFW->waitForFrame();
2852 ASSERT_EQ(OK, mST->updateTexImage());
2853}
2854
Jamie Gennis5451d152011-06-08 09:40:45 -07002855} // namespace android