blob: e4fba1561d4a183cd8ac2b9550a81313edc5d2af [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 <android/native_window.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080039
40namespace android {
41
42class GLTest : public ::testing::Test {
43protected:
44
45 GLTest():
46 mEglDisplay(EGL_NO_DISPLAY),
47 mEglSurface(EGL_NO_SURFACE),
48 mEglContext(EGL_NO_CONTEXT) {
49 }
50
51 virtual void SetUp() {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070052 const ::testing::TestInfo* const testInfo =
53 ::testing::UnitTest::GetInstance()->current_test_info();
54 ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
55 testInfo->name());
56
Jamie Gennisd99c0882011-03-10 16:24:46 -080057 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
58 ASSERT_EQ(EGL_SUCCESS, eglGetError());
59 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
60
61 EGLint majorVersion;
62 EGLint minorVersion;
63 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
64 ASSERT_EQ(EGL_SUCCESS, eglGetError());
65 RecordProperty("EglVersionMajor", majorVersion);
66 RecordProperty("EglVersionMajor", minorVersion);
67
Jamie Gennisd99c0882011-03-10 16:24:46 -080068 EGLint numConfigs = 0;
Jamie Gennis1876d132011-03-17 16:32:52 -070069 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080070 1, &numConfigs));
71 ASSERT_EQ(EGL_SUCCESS, eglGetError());
72
73 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
74 if (displaySecsEnv != NULL) {
75 mDisplaySecs = atoi(displaySecsEnv);
76 if (mDisplaySecs < 0) {
77 mDisplaySecs = 0;
78 }
79 } else {
80 mDisplaySecs = 0;
81 }
82
83 if (mDisplaySecs > 0) {
84 mComposerClient = new SurfaceComposerClient;
85 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
86
Jamie Gennisfc850122011-04-25 16:40:05 -070087 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070088 String8("Test Surface"),
Jamie Gennisd99c0882011-03-10 16:24:46 -080089 getSurfaceWidth(), getSurfaceHeight(),
90 PIXEL_FORMAT_RGB_888, 0);
91
92 ASSERT_TRUE(mSurfaceControl != NULL);
93 ASSERT_TRUE(mSurfaceControl->isValid());
94
Mathias Agopian698c0872011-06-28 19:09:31 -070095 SurfaceComposerClient::openGlobalTransaction();
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070096 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080097 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070098 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennisd99c0882011-03-10 16:24:46 -080099
100 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -0700101 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800102 window.get(), NULL);
103 } else {
104 EGLint pbufferAttribs[] = {
105 EGL_WIDTH, getSurfaceWidth(),
106 EGL_HEIGHT, getSurfaceHeight(),
107 EGL_NONE };
108
Jamie Gennis1876d132011-03-17 16:32:52 -0700109 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800110 pbufferAttribs);
111 }
112 ASSERT_EQ(EGL_SUCCESS, eglGetError());
113 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
114
Jamie Gennis1876d132011-03-17 16:32:52 -0700115 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800116 getContextAttribs());
117 ASSERT_EQ(EGL_SUCCESS, eglGetError());
118 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
119
120 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
121 mEglContext));
122 ASSERT_EQ(EGL_SUCCESS, eglGetError());
123
124 EGLint w, h;
125 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
126 ASSERT_EQ(EGL_SUCCESS, eglGetError());
127 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
128 ASSERT_EQ(EGL_SUCCESS, eglGetError());
129 RecordProperty("EglSurfaceWidth", w);
130 RecordProperty("EglSurfaceHeight", h);
131
132 glViewport(0, 0, w, h);
133 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
134 }
135
136 virtual void TearDown() {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700137 // Display the result
Jamie Gennisd99c0882011-03-10 16:24:46 -0800138 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
139 eglSwapBuffers(mEglDisplay, mEglSurface);
140 sleep(mDisplaySecs);
141 }
142
143 if (mComposerClient != NULL) {
144 mComposerClient->dispose();
145 }
146 if (mEglContext != EGL_NO_CONTEXT) {
147 eglDestroyContext(mEglDisplay, mEglContext);
148 }
149 if (mEglSurface != EGL_NO_SURFACE) {
150 eglDestroySurface(mEglDisplay, mEglSurface);
151 }
152 if (mEglDisplay != EGL_NO_DISPLAY) {
153 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
154 EGL_NO_CONTEXT);
155 eglTerminate(mEglDisplay);
156 }
157 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700158
159 const ::testing::TestInfo* const testInfo =
160 ::testing::UnitTest::GetInstance()->current_test_info();
161 ALOGV("End test: %s.%s", testInfo->test_case_name(),
162 testInfo->name());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800163 }
164
165 virtual EGLint const* getConfigAttribs() {
166 static EGLint sDefaultConfigAttribs[] = {
167 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
168 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
169 EGL_RED_SIZE, 8,
170 EGL_GREEN_SIZE, 8,
171 EGL_BLUE_SIZE, 8,
172 EGL_ALPHA_SIZE, 8,
173 EGL_DEPTH_SIZE, 16,
174 EGL_STENCIL_SIZE, 8,
175 EGL_NONE };
176
177 return sDefaultConfigAttribs;
178 }
179
180 virtual EGLint const* getContextAttribs() {
181 static EGLint sDefaultContextAttribs[] = {
182 EGL_CONTEXT_CLIENT_VERSION, 2,
183 EGL_NONE };
184
185 return sDefaultContextAttribs;
186 }
187
188 virtual EGLint getSurfaceWidth() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700189 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800190 }
191
192 virtual EGLint getSurfaceHeight() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700193 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800194 }
195
Jamie Gennisd99c0882011-03-10 16:24:46 -0800196 ::testing::AssertionResult checkPixel(int x, int y, int r,
Jamie Gennis824efa72011-06-13 13:41:01 -0700197 int g, int b, int a, int tolerance=2) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800198 GLubyte pixel[4];
199 String8 msg;
200 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
201 GLenum err = glGetError();
202 if (err != GL_NO_ERROR) {
203 msg += String8::format("error reading pixel: %#x", err);
204 while ((err = glGetError()) != GL_NO_ERROR) {
205 msg += String8::format(", %#x", err);
206 }
Jamie Gennisd99c0882011-03-10 16:24:46 -0800207 return ::testing::AssertionFailure(
208 ::testing::Message(msg.string()));
209 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700210 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800211 msg += String8::format("r(%d isn't %d)", pixel[0], r);
212 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700213 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800214 if (!msg.isEmpty()) {
215 msg += " ";
216 }
217 msg += String8::format("g(%d isn't %d)", pixel[1], g);
218 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700219 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800220 if (!msg.isEmpty()) {
221 msg += " ";
222 }
223 msg += String8::format("b(%d isn't %d)", pixel[2], b);
224 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700225 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800226 if (!msg.isEmpty()) {
227 msg += " ";
228 }
229 msg += String8::format("a(%d isn't %d)", pixel[3], a);
230 }
231 if (!msg.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800232 return ::testing::AssertionFailure(
233 ::testing::Message(msg.string()));
234 } else {
235 return ::testing::AssertionSuccess();
236 }
237 }
238
Daniel Lam016c8cb2012-04-03 15:54:58 -0700239 ::testing::AssertionResult assertRectEq(const Rect &r1,
240 const Rect &r2, int tolerance=1) {
241
242 String8 msg;
243
244 if (abs(r1.left - r2.left) > tolerance) {
245 msg += String8::format("left(%d isn't %d)", r1.left, r2.left);
246 }
247 if (abs(r1.top - r2.top) > tolerance) {
248 if (!msg.isEmpty()) {
249 msg += " ";
250 }
251 msg += String8::format("top(%d isn't %d)", r1.top, r2.top);
252 }
253 if (abs(r1.right - r2.right) > tolerance) {
254 if (!msg.isEmpty()) {
255 msg += " ";
256 }
257 msg += String8::format("right(%d isn't %d)", r1.right, r2.right);
258 }
259 if (abs(r1.bottom - r2.bottom) > tolerance) {
260 if (!msg.isEmpty()) {
261 msg += " ";
262 }
263 msg += String8::format("bottom(%d isn't %d)", r1.bottom, r2.bottom);
264 }
265 if (!msg.isEmpty()) {
266 msg += String8::format(" R1: [%d %d %d %d] R2: [%d %d %d %d]",
267 r1.left, r1.top, r1.right, r1.bottom,
268 r2.left, r2.top, r2.right, r2.bottom);
269 fprintf(stderr, "assertRectEq: %s\n", msg.string());
270 return ::testing::AssertionFailure(
271 ::testing::Message(msg.string()));
272 } else {
273 return ::testing::AssertionSuccess();
274 }
275 }
276
Jamie Gennisd99c0882011-03-10 16:24:46 -0800277 int mDisplaySecs;
278 sp<SurfaceComposerClient> mComposerClient;
279 sp<SurfaceControl> mSurfaceControl;
280
281 EGLDisplay mEglDisplay;
282 EGLSurface mEglSurface;
283 EGLContext mEglContext;
Jamie Gennis1876d132011-03-17 16:32:52 -0700284 EGLConfig mGlConfig;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800285};
286
Jamie Gennis74bed552012-03-28 19:05:54 -0700287static void loadShader(GLenum shaderType, const char* pSource,
288 GLuint* outShader) {
289 GLuint shader = glCreateShader(shaderType);
290 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
291 if (shader) {
292 glShaderSource(shader, 1, &pSource, NULL);
293 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
294 glCompileShader(shader);
295 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
296 GLint compiled = 0;
297 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
298 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
299 if (!compiled) {
300 GLint infoLen = 0;
301 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
302 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
303 if (infoLen) {
304 char* buf = (char*) malloc(infoLen);
305 if (buf) {
306 glGetShaderInfoLog(shader, infoLen, NULL, buf);
307 printf("Shader compile log:\n%s\n", buf);
308 free(buf);
309 FAIL();
310 }
311 } else {
312 char* buf = (char*) malloc(0x1000);
313 if (buf) {
314 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
315 printf("Shader compile log:\n%s\n", buf);
316 free(buf);
317 FAIL();
318 }
319 }
320 glDeleteShader(shader);
321 shader = 0;
322 }
323 }
324 ASSERT_TRUE(shader != 0);
325 *outShader = shader;
326}
327
328static void createProgram(const char* pVertexSource,
329 const char* pFragmentSource, GLuint* outPgm) {
330 GLuint vertexShader, fragmentShader;
331 {
332 SCOPED_TRACE("compiling vertex shader");
333 ASSERT_NO_FATAL_FAILURE(loadShader(GL_VERTEX_SHADER, pVertexSource,
334 &vertexShader));
335 }
336 {
337 SCOPED_TRACE("compiling fragment shader");
338 ASSERT_NO_FATAL_FAILURE(loadShader(GL_FRAGMENT_SHADER, pFragmentSource,
339 &fragmentShader));
340 }
341
342 GLuint program = glCreateProgram();
343 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
344 if (program) {
345 glAttachShader(program, vertexShader);
346 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
347 glAttachShader(program, fragmentShader);
348 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
349 glLinkProgram(program);
350 GLint linkStatus = GL_FALSE;
351 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
352 if (linkStatus != GL_TRUE) {
353 GLint bufLength = 0;
354 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
355 if (bufLength) {
356 char* buf = (char*) malloc(bufLength);
357 if (buf) {
358 glGetProgramInfoLog(program, bufLength, NULL, buf);
359 printf("Program link log:\n%s\n", buf);
360 free(buf);
361 FAIL();
362 }
363 }
364 glDeleteProgram(program);
365 program = 0;
366 }
367 }
368 glDeleteShader(vertexShader);
369 glDeleteShader(fragmentShader);
370 ASSERT_TRUE(program != 0);
371 *outPgm = program;
372}
373
374static int abs(int value) {
375 return value > 0 ? value : -value;
376}
377
378
Jamie Gennisd99c0882011-03-10 16:24:46 -0800379// XXX: Code above this point should live elsewhere
380
Mathias Agopianf31510a2013-04-16 23:32:38 -0700381class MultiTextureConsumerTest : public GLTest {
382protected:
383 enum { TEX_ID = 123 };
384
385 virtual void SetUp() {
386 GLTest::SetUp();
Mathias Agopian8f938a52013-07-12 22:06:26 -0700387 sp<BufferQueue> bq = new BufferQueue();
388 mGlConsumer = new GLConsumer(bq, TEX_ID);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700389 mSurface = new Surface(bq);
Mathias Agopianf31510a2013-04-16 23:32:38 -0700390 mANW = mSurface.get();
391
392 }
393 virtual void TearDown() {
394 GLTest::TearDown();
395 }
396 virtual EGLint const* getContextAttribs() {
397 return NULL;
398 }
399 virtual EGLint const* getConfigAttribs() {
400 static EGLint sDefaultConfigAttribs[] = {
401 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
402 EGL_RED_SIZE, 8,
403 EGL_GREEN_SIZE, 8,
404 EGL_BLUE_SIZE, 8,
405 EGL_ALPHA_SIZE, 8,
406 EGL_NONE };
407
408 return sDefaultConfigAttribs;
409 }
410 sp<GLConsumer> mGlConsumer;
411 sp<Surface> mSurface;
412 ANativeWindow* mANW;
413};
414
415
416TEST_F(MultiTextureConsumerTest, EGLImageTargetWorks) {
417 ANativeWindow_Buffer buffer;
418
419 ASSERT_EQ(native_window_set_usage(mANW, GRALLOC_USAGE_SW_WRITE_OFTEN), NO_ERROR);
420 ASSERT_EQ(native_window_set_buffers_format(mANW, HAL_PIXEL_FORMAT_RGBA_8888), NO_ERROR);
421
422 glShadeModel(GL_FLAT);
423 glDisable(GL_DITHER);
424 glDisable(GL_CULL_FACE);
425 glViewport(0, 0, getSurfaceWidth(), getSurfaceHeight());
426 glOrthof(0, getSurfaceWidth(), 0, getSurfaceHeight(), 0, 1);
427 glEnableClientState(GL_VERTEX_ARRAY);
428 glColor4f(1, 1, 1, 1);
429
430 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
431 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
432 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
433 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
434 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
435
436 uint32_t texel = 0x80808080;
437 glBindTexture(GL_TEXTURE_2D, TEX_ID+1);
438 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &texel);
439 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
440 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
441 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
442 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
443
444 glActiveTexture(GL_TEXTURE1);
445 glBindTexture(GL_TEXTURE_2D, TEX_ID+1);
446 glEnable(GL_TEXTURE_2D);
447 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
448
449 glActiveTexture(GL_TEXTURE0);
450 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
451 glEnable(GL_TEXTURE_EXTERNAL_OES);
452 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
453
454 glClear(GL_COLOR_BUFFER_BIT);
455 for (int i=0 ; i<8 ; i++) {
456 mSurface->lock(&buffer, NULL);
457 memset(buffer.bits, (i&7) * 0x20, buffer.stride * buffer.height * 4);
458 mSurface->unlockAndPost();
459
460 mGlConsumer->updateTexImage();
461
462 GLfloat vertices[][2] = { {i*16.0f, 0}, {(i+1)*16.0f, 0}, {(i+1)*16.0f, 16.0f}, {i*16.0f, 16.0f} };
463 glVertexPointer(2, GL_FLOAT, 0, vertices);
464 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
465
Jamie Gennisea2d9422013-04-23 15:00:45 -0700466 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
Mathias Agopianf31510a2013-04-16 23:32:38 -0700467 }
Mathias Agopianf31510a2013-04-16 23:32:38 -0700468
469 for (int i=0 ; i<8 ; i++) {
Jamie Gennisea2d9422013-04-23 15:00:45 -0700470 EXPECT_TRUE(checkPixel(i*16 + 8, 8, i*16, i*16, i*16, i*16, 0));
Mathias Agopianf31510a2013-04-16 23:32:38 -0700471 }
Mathias Agopianf31510a2013-04-16 23:32:38 -0700472}
473
474
475
Jamie Gennisd99c0882011-03-10 16:24:46 -0800476class SurfaceTextureGLTest : public GLTest {
477protected:
Jamie Gennis79e31252011-10-19 15:19:19 -0700478 enum { TEX_ID = 123 };
Jamie Gennisd99c0882011-03-10 16:24:46 -0800479
480 virtual void SetUp() {
481 GLTest::SetUp();
Mathias Agopian8f938a52013-07-12 22:06:26 -0700482 sp<BufferQueue> bq = new BufferQueue();
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700483 mBQ = bq;
Mathias Agopian8f938a52013-07-12 22:06:26 -0700484 mST = new GLConsumer(bq, TEX_ID);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700485 mSTC = new Surface(bq);
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
Mathias Agopiana4e19522013-07-31 20:09:53 -0700631 class DisconnectWaiter : public BnConsumerListener {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700632 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
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700675 sp<BufferQueue> mBQ;
Andy McFadden2adaf042012-12-18 09:49:45 -0800676 sp<GLConsumer> mST;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800677 sp<Surface> mSTC;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800678 sp<ANativeWindow> mANW;
Jamie Gennis74bed552012-03-28 19:05:54 -0700679 sp<TextureRenderer> mTextureRenderer;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700680 sp<FrameWaiter> mFW;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800681};
682
683// Fill a YV12 buffer with a multi-colored checkerboard pattern
684void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
685 const int blockWidth = w > 16 ? w / 16 : 1;
686 const int blockHeight = h > 16 ? h / 16 : 1;
687 const int yuvTexOffsetY = 0;
688 int yuvTexStrideY = stride;
689 int yuvTexOffsetV = yuvTexStrideY * h;
690 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
691 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
692 int yuvTexStrideU = yuvTexStrideV;
693 for (int x = 0; x < w; x++) {
694 for (int y = 0; y < h; y++) {
695 int parityX = (x / blockWidth) & 1;
696 int parityY = (y / blockHeight) & 1;
697 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
698 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
699 if (x < w / 2 && y < h / 2) {
700 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
701 if (x * 2 < w / 2 && y * 2 < h / 2) {
702 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
703 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
704 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
705 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
706 intensity;
707 }
708 }
709 }
710 }
711}
712
713// Fill a YV12 buffer with red outside a given rectangle and green inside it.
714void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
715 const android_native_rect_t& rect) {
716 const int yuvTexOffsetY = 0;
717 int yuvTexStrideY = stride;
718 int yuvTexOffsetV = yuvTexStrideY * h;
719 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
720 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
721 int yuvTexStrideU = yuvTexStrideV;
722 for (int x = 0; x < w; x++) {
723 for (int y = 0; y < h; y++) {
724 bool inside = rect.left <= x && x < rect.right &&
725 rect.top <= y && y < rect.bottom;
726 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
727 if (x < w / 2 && y < h / 2) {
728 bool inside = rect.left <= 2*x && 2*x < rect.right &&
729 rect.top <= 2*y && 2*y < rect.bottom;
730 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
731 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
732 inside ? 16 : 255;
733 }
734 }
735 }
736}
737
Jamie Gennis1876d132011-03-17 16:32:52 -0700738void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
739 const size_t PIXEL_SIZE = 4;
740 for (int x = 0; x < w; x++) {
741 for (int y = 0; y < h; y++) {
742 off_t offset = (y * stride + x) * PIXEL_SIZE;
743 for (int c = 0; c < 4; c++) {
744 int parityX = (x / (1 << (c+2))) & 1;
745 int parityY = (y / (1 << (c+2))) & 1;
746 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
747 }
748 }
749 }
750}
751
Jamie Gennisfe27e2f2011-11-11 18:05:11 -0800752void fillRGBA8BufferSolid(uint8_t* buf, int w, int h, int stride, uint8_t r,
753 uint8_t g, uint8_t b, uint8_t a) {
754 const size_t PIXEL_SIZE = 4;
755 for (int y = 0; y < h; y++) {
756 for (int x = 0; x < h; x++) {
757 off_t offset = (y * stride + x) * PIXEL_SIZE;
758 buf[offset + 0] = r;
759 buf[offset + 1] = g;
760 buf[offset + 2] = b;
761 buf[offset + 3] = a;
762 }
763 }
764}
765
Jamie Gennisce561372012-03-19 18:33:05 -0700766// Produce a single RGBA8 frame by filling a buffer with a checkerboard pattern
767// using the CPU. This assumes that the ANativeWindow is already configured to
768// allow this to be done (e.g. the format is set to RGBA8).
769//
770// Calls to this function should be wrapped in an ASSERT_NO_FATAL_FAILURE().
771void produceOneRGBA8Frame(const sp<ANativeWindow>& anw) {
772 android_native_buffer_t* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700773 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
774 &anb));
Jamie Gennisce561372012-03-19 18:33:05 -0700775 ASSERT_TRUE(anb != NULL);
776
777 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisce561372012-03-19 18:33:05 -0700778
779 uint8_t* img = NULL;
780 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
781 (void**)(&img)));
782 fillRGBA8Buffer(img, buf->getWidth(), buf->getHeight(), buf->getStride());
783 ASSERT_EQ(NO_ERROR, buf->unlock());
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700784 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer(),
785 -1));
Jamie Gennisce561372012-03-19 18:33:05 -0700786}
787
Jamie Gennisd99c0882011-03-10 16:24:46 -0800788TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700789 const int texWidth = 64;
790 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800791
792 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700793 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800794 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
795 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
796
Iliyan Malchev697526b2011-05-01 11:33:26 -0700797 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700798 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
799 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800800 ASSERT_TRUE(anb != NULL);
801
802 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800803
804 // Fill the buffer with the a checkerboard pattern
805 uint8_t* img = NULL;
806 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700807 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800808 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700809 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
810 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800811
Jamie Gennisd69097f2012-08-30 13:28:23 -0700812 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800813
814 glClearColor(0.2, 0.2, 0.2, 0.2);
815 glClear(GL_COLOR_BUFFER_BIT);
816
Jamie Gennisc8c51522011-06-15 14:24:38 -0700817 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800818 drawTexture();
819
Jamie Gennise6a0f502013-04-05 17:37:32 -0700820 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255, 3));
821 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255, 3));
822 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255, 3));
823 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255, 3));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800824
Jamie Gennise6a0f502013-04-05 17:37:32 -0700825 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255, 3));
826 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255, 3));
827 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255, 3));
828 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255, 3));
829 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255, 3));
830 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255, 3));
831 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255, 3));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800832}
833
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700834TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700835 const int texWidth = 64;
836 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800837
838 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700839 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800840 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
841 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
842
Iliyan Malchev697526b2011-05-01 11:33:26 -0700843 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700844 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
845 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800846 ASSERT_TRUE(anb != NULL);
847
848 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800849
850 // Fill the buffer with the a checkerboard pattern
851 uint8_t* img = NULL;
852 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700853 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800854 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700855 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
856 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800857
Jamie Gennisd69097f2012-08-30 13:28:23 -0700858 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800859
860 glClearColor(0.2, 0.2, 0.2, 0.2);
861 glClear(GL_COLOR_BUFFER_BIT);
862
Jamie Gennisc8c51522011-06-15 14:24:38 -0700863 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800864 drawTexture();
865
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700866 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
867 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800868 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
869 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
870
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700871 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
872 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
873 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
874 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
875 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
876 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
877 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800878}
879
880TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700881 const int texWidth = 64;
882 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800883
884 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700885 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800886 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
887 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
888
889 android_native_rect_t crops[] = {
890 {4, 6, 22, 36},
891 {0, 6, 22, 36},
892 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700893 {4, 6, texWidth, 36},
894 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800895 };
896
897 for (int i = 0; i < 5; i++) {
898 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800899 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
900 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800901
902 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
903
Iliyan Malchev697526b2011-05-01 11:33:26 -0700904 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700905 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
906 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800907 ASSERT_TRUE(anb != NULL);
908
909 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800910
911 uint8_t* img = NULL;
912 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700913 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800914 buf->unlock();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800915 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700916 buf->getNativeBuffer(), -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800917
Jamie Gennisd69097f2012-08-30 13:28:23 -0700918 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800919
920 glClearColor(0.2, 0.2, 0.2, 0.2);
921 glClear(GL_COLOR_BUFFER_BIT);
922
Jamie Gennisc8c51522011-06-15 14:24:38 -0700923 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800924 drawTexture();
925
926 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
927 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
928 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
929 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
930
931 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
932 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
933 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
934 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
935 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
936 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
937 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
938 }
939}
940
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700941// This test is intended to catch synchronization bugs between the CPU-written
942// and GPU-read buffers.
943TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
944 enum { texWidth = 16 };
945 enum { texHeight = 16 };
946 enum { numFrames = 1024 };
947
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
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001214 sp<DisconnectWaiter> dw(new DisconnectWaiter());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001215 mBQ->consumerConnect(dw, false);
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001216
1217
1218 sp<Thread> pt(new ProducerThread(mANW));
1219 pt->run();
1220
Andy McFadden2adaf042012-12-18 09:49:45 -08001221 // eat a frame so GLConsumer will own an at least one slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001222 dw->waitForFrame();
1223 EXPECT_EQ(OK,mST->updateTexImage());
1224
1225 dw->waitForFrame();
Andy McFadden2adaf042012-12-18 09:49:45 -08001226 // Could fail here as GLConsumer thinks it still owns the slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001227 // but bufferQueue has released all slots
1228 EXPECT_EQ(OK,mST->updateTexImage());
1229
1230 dw->finishDisconnect();
1231}
1232
1233
Andy McFadden2adaf042012-12-18 09:49:45 -08001234// This test ensures that the GLConsumer clears the mCurrentTexture
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001235// when it is disconnected and reconnected. Otherwise it will
1236// attempt to release a buffer that it does not owned
1237TEST_F(SurfaceTextureGLTest, DisconnectClearsCurrentTexture) {
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001238 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1239 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001240
1241 ANativeWindowBuffer *anb;
1242
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001243 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1244 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001245
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001246 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1247 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001248
1249 EXPECT_EQ(OK,mST->updateTexImage());
1250 EXPECT_EQ(OK,mST->updateTexImage());
1251
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001252 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1253 NATIVE_WINDOW_API_EGL));
1254 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1255 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001256
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001257 EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1258 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001259
1260 // Will fail here if mCurrentTexture is not cleared properly
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001261 mFW->waitForFrame();
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001262 EXPECT_EQ(OK,mST->updateTexImage());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001263
1264 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1265 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001266}
1267
Daniel Lam016c8cb2012-04-03 15:54:58 -07001268TEST_F(SurfaceTextureGLTest, ScaleToWindowMode) {
Daniel Lam016c8cb2012-04-03 15:54:58 -07001269 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1270 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));
1271
1272 // The producer image size
1273 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1274
1275 // The consumer image size (16 x 9) ratio
1276 mST->setDefaultBufferSize(1280, 720);
1277
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001278 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1279 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001280
1281 ANativeWindowBuffer *anb;
1282
1283 android_native_rect_t odd = {23, 78, 123, 477};
1284 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &odd));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001285 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1286 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001287 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001288 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001289 Rect r = mST->getCurrentCrop();
1290 assertRectEq(Rect(23, 78, 123, 477), r);
1291
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001292 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1293 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001294}
1295
1296// This test ensures the scaling mode does the right thing
1297// ie NATIVE_WINDOW_SCALING_MODE_CROP should crop
1298// the image such that it has the same aspect ratio as the
1299// default buffer size
1300TEST_F(SurfaceTextureGLTest, CroppedScalingMode) {
Daniel Lam016c8cb2012-04-03 15:54:58 -07001301 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1302 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP));
1303
1304 // The producer image size
1305 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1306
1307 // The consumer image size (16 x 9) ratio
1308 mST->setDefaultBufferSize(1280, 720);
1309
1310 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU);
1311
1312 ANativeWindowBuffer *anb;
1313
1314 // The crop is in the shape of (320, 180) === 16 x 9
1315 android_native_rect_t standard = {10, 20, 330, 200};
1316 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &standard));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001317 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1318 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001319 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001320 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001321 Rect r = mST->getCurrentCrop();
1322 // crop should be the same as crop (same aspect ratio)
1323 assertRectEq(Rect(10, 20, 330, 200), r);
1324
1325 // make this wider then desired aspect 239 x 100 (2.39:1)
1326 android_native_rect_t wide = {20, 30, 259, 130};
1327 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &wide));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001328 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1329 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001330 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001331 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001332 r = mST->getCurrentCrop();
1333 // crop should be the same height, but have cropped left and right borders
1334 // offset is 30.6 px L+, R-
1335 assertRectEq(Rect(51, 30, 228, 130), r);
1336
1337 // This image is taller then desired aspect 400 x 300 (4:3)
1338 android_native_rect_t narrow = {0, 0, 400, 300};
1339 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &narrow));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001340 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1341 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001342 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001343 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001344 r = mST->getCurrentCrop();
1345 // crop should be the same width, but have cropped top and bottom borders
1346 // offset is 37.5 px
1347 assertRectEq(Rect(0, 37, 400, 262), r);
1348
1349 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
1350}
1351
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001352TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
1353 class ProducerThread : public Thread {
1354 public:
1355 ProducerThread(const sp<ANativeWindow>& anw):
1356 mANW(anw),
1357 mDequeueError(NO_ERROR) {
1358 }
1359
1360 virtual ~ProducerThread() {
1361 }
1362
1363 virtual bool threadLoop() {
1364 Mutex::Autolock lock(mMutex);
1365 ANativeWindowBuffer* anb;
1366
1367 // Frame 1
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001368 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1369 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001370 return false;
1371 }
1372 if (anb == NULL) {
1373 return false;
1374 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001375 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001376 != NO_ERROR) {
1377 return false;
1378 }
1379
1380 // Frame 2
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001381 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1382 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001383 return false;
1384 }
1385 if (anb == NULL) {
1386 return false;
1387 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001388 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001389 != NO_ERROR) {
1390 return false;
1391 }
1392
1393 // Frame 3 - error expected
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001394 mDequeueError = native_window_dequeue_buffer_and_wait(mANW.get(),
1395 &anb);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001396 return false;
1397 }
1398
1399 status_t getDequeueError() {
1400 Mutex::Autolock lock(mMutex);
1401 return mDequeueError;
1402 }
1403
1404 private:
1405 sp<ANativeWindow> mANW;
1406 status_t mDequeueError;
1407 Mutex mMutex;
1408 };
1409
Jamie Gennis31a353d2012-08-24 17:25:13 -07001410 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001411
1412 sp<Thread> pt(new ProducerThread(mANW));
1413 pt->run();
1414
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001415 mFW->waitForFrame();
1416 mFW->waitForFrame();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001417
1418 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1419 // block waiting for a buffer to become available.
1420 usleep(100000);
1421
1422 mST->abandon();
1423
1424 pt->requestExitAndWait();
1425 ASSERT_EQ(NO_INIT,
1426 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1427}
1428
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001429TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1430 int texHeight = 16;
1431 ANativeWindowBuffer* anb;
1432
1433 GLint maxTextureSize;
1434 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1435
1436 // make sure it works with small textures
1437 mST->setDefaultBufferSize(16, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001438 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1439 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001440 EXPECT_EQ(16, anb->width);
1441 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001442 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001443 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1444
1445 // make sure it works with GL_MAX_TEXTURE_SIZE
1446 mST->setDefaultBufferSize(maxTextureSize, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001447 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1448 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001449 EXPECT_EQ(maxTextureSize, anb->width);
1450 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001451 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001452 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1453
1454 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1455 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001456 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1457 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001458 EXPECT_EQ(maxTextureSize+1, anb->width);
1459 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001460 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001461 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1462}
1463
Jamie Gennis5451d152011-06-08 09:40:45 -07001464/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001465 * This test fixture is for testing GL -> GL texture streaming. It creates an
1466 * EGLSurface and an EGLContext for the image producer to use.
1467 */
1468class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1469protected:
1470 SurfaceTextureGLToGLTest():
1471 mProducerEglSurface(EGL_NO_SURFACE),
1472 mProducerEglContext(EGL_NO_CONTEXT) {
1473 }
1474
1475 virtual void SetUp() {
1476 SurfaceTextureGLTest::SetUp();
1477
Jamie Gennisce561372012-03-19 18:33:05 -07001478 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001479 mANW.get(), NULL);
1480 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1481 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1482
Jamie Gennisce561372012-03-19 18:33:05 -07001483 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001484 EGL_NO_CONTEXT, getContextAttribs());
1485 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1486 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1487 }
1488
1489 virtual void TearDown() {
1490 if (mProducerEglContext != EGL_NO_CONTEXT) {
1491 eglDestroyContext(mEglDisplay, mProducerEglContext);
1492 }
1493 if (mProducerEglSurface != EGL_NO_SURFACE) {
1494 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1495 }
1496 SurfaceTextureGLTest::TearDown();
1497 }
1498
1499 EGLSurface mProducerEglSurface;
1500 EGLContext mProducerEglContext;
1501};
1502
Andy McFadden71f683a2012-09-14 17:21:46 -07001503TEST_F(SurfaceTextureGLToGLTest, TransformHintGetsRespected) {
1504 const uint32_t texWidth = 32;
1505 const uint32_t texHeight = 64;
1506
1507 mST->setDefaultBufferSize(texWidth, texHeight);
1508 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1509
1510 // This test requires 3 buffers to avoid deadlock because we're
1511 // both producer and consumer, and only using one thread.
1512 mST->setDefaultMaxBufferCount(3);
1513
1514 // Do the producer side of things
1515 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1516 mProducerEglSurface, mProducerEglContext));
1517 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1518
1519 // Start a buffer with our chosen size and transform hint moving
1520 // through the system.
1521 glClear(GL_COLOR_BUFFER_BIT); // give the driver something to do
1522 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1523 mST->updateTexImage(); // consume it
1524 // Swap again.
1525 glClear(GL_COLOR_BUFFER_BIT);
1526 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1527 mST->updateTexImage();
1528
1529 // The current buffer should either show the effects of the transform
1530 // hint (in the form of an inverse transform), or show that the
1531 // transform hint has been ignored.
1532 sp<GraphicBuffer> buf = mST->getCurrentBuffer();
1533 if (mST->getCurrentTransform() == NATIVE_WINDOW_TRANSFORM_ROT_270) {
1534 ASSERT_EQ(texWidth, buf->getHeight());
1535 ASSERT_EQ(texHeight, buf->getWidth());
1536 } else {
1537 ASSERT_EQ(texWidth, buf->getWidth());
1538 ASSERT_EQ(texHeight, buf->getHeight());
1539 }
1540
1541 // Reset the transform hint and confirm that it takes.
1542 mST->setTransformHint(0);
1543 glClear(GL_COLOR_BUFFER_BIT);
1544 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1545 mST->updateTexImage();
1546 glClear(GL_COLOR_BUFFER_BIT);
1547 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1548 mST->updateTexImage();
1549
1550 buf = mST->getCurrentBuffer();
1551 ASSERT_EQ((uint32_t) 0, mST->getCurrentTransform());
1552 ASSERT_EQ(texWidth, buf->getWidth());
1553 ASSERT_EQ(texHeight, buf->getHeight());
1554}
1555
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001556TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1557 const int texWidth = 64;
1558 const int texHeight = 64;
1559
1560 mST->setDefaultBufferSize(texWidth, texHeight);
1561
Jamie Gennis46975282012-08-30 18:35:50 -07001562 // This test requires 3 buffers to complete run on a single thread.
1563 mST->setDefaultMaxBufferCount(3);
1564
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001565 // Do the producer side of things
1566 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1567 mProducerEglSurface, mProducerEglContext));
1568 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1569
1570 // This is needed to ensure we pick up a buffer of the correct size.
1571 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1572
1573 glClearColor(0.6, 0.6, 0.6, 0.6);
1574 glClear(GL_COLOR_BUFFER_BIT);
1575
1576 glEnable(GL_SCISSOR_TEST);
1577 glScissor(4, 4, 4, 4);
1578 glClearColor(1.0, 0.0, 0.0, 1.0);
1579 glClear(GL_COLOR_BUFFER_BIT);
1580
1581 glScissor(24, 48, 4, 4);
1582 glClearColor(0.0, 1.0, 0.0, 1.0);
1583 glClear(GL_COLOR_BUFFER_BIT);
1584
1585 glScissor(37, 17, 4, 4);
1586 glClearColor(0.0, 0.0, 1.0, 1.0);
1587 glClear(GL_COLOR_BUFFER_BIT);
1588
1589 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1590
1591 // Do the consumer side of things
1592 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1593 mEglContext));
1594 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1595
1596 glDisable(GL_SCISSOR_TEST);
1597
Jamie Gennisd69097f2012-08-30 13:28:23 -07001598 // Skip the first frame, which was empty
1599 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1600 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001601
1602 glClearColor(0.2, 0.2, 0.2, 0.2);
1603 glClear(GL_COLOR_BUFFER_BIT);
1604
1605 glViewport(0, 0, texWidth, texHeight);
1606 drawTexture();
1607
1608 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1609 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1610 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1611 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1612
1613 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1614 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1615 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1616 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1617 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1618 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1619 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1620 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1621 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1622 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1623 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1624 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1625 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1626 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1627 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1628 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1629}
1630
1631TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001632 sp<GraphicBuffer> buffers[2];
1633
Jamie Gennise3603d72011-11-19 21:20:17 -08001634 // This test requires async mode to run on a single thread.
1635 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1636 mProducerEglSurface, mProducerEglContext));
1637 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1638 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1639 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1640
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001641 for (int i = 0; i < 2; i++) {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001642 // Produce a frame
1643 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1644 mProducerEglSurface, mProducerEglContext));
1645 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1646 glClear(GL_COLOR_BUFFER_BIT);
1647 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1648
1649 // Consume a frame
1650 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1651 mEglContext));
1652 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001653 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001654 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001655 buffers[i] = mST->getCurrentBuffer();
1656 }
1657
1658 // Destroy the GL texture object to release its ref on buffers[2].
1659 GLuint texID = TEX_ID;
1660 glDeleteTextures(1, &texID);
1661
1662 // Destroy the EGLSurface
1663 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1664 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001665 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001666
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001667 // This test should have the only reference to buffer 0.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001668 EXPECT_EQ(1, buffers[0]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001669
Andy McFadden2adaf042012-12-18 09:49:45 -08001670 // The GLConsumer should hold a single reference to buffer 1 in its
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001671 // mCurrentBuffer member. All of the references in the slots should have
1672 // been released.
1673 EXPECT_EQ(2, buffers[1]->getStrongCount());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001674}
1675
1676TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1677 sp<GraphicBuffer> buffers[3];
1678
Jamie Gennise3603d72011-11-19 21:20:17 -08001679 // This test requires async mode to run on a single thread.
1680 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1681 mProducerEglSurface, mProducerEglContext));
1682 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1683 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1684 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1685
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001686 for (int i = 0; i < 3; i++) {
1687 // Produce a frame
1688 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1689 mProducerEglSurface, mProducerEglContext));
1690 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1691 glClear(GL_COLOR_BUFFER_BIT);
1692 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1693 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1694
1695 // Consume a frame
1696 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1697 mEglContext));
1698 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001699 mFW->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001700 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1701 buffers[i] = mST->getCurrentBuffer();
1702 }
1703
Andy McFadden2adaf042012-12-18 09:49:45 -08001704 // Abandon the GLConsumer, releasing the ref that the GLConsumer has
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001705 // on buffers[2].
1706 mST->abandon();
1707
1708 // Destroy the GL texture object to release its ref on buffers[2].
1709 GLuint texID = TEX_ID;
1710 glDeleteTextures(1, &texID);
1711
1712 // Destroy the EGLSurface.
1713 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1714 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001715 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001716
1717 EXPECT_EQ(1, buffers[0]->getStrongCount());
1718 EXPECT_EQ(1, buffers[1]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001719
1720 // Depending on how lazily the GL driver dequeues buffers, we may end up
1721 // with either two or three total buffers. If there are three, make sure
1722 // the last one was properly down-ref'd.
1723 if (buffers[2] != buffers[0]) {
1724 EXPECT_EQ(1, buffers[2]->getStrongCount());
1725 }
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001726}
1727
Mathias Agopian7589b2a2013-03-08 13:18:52 -08001728TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentBeforeConsumerDeathUnrefsBuffers) {
1729 sp<GraphicBuffer> buffer;
1730
1731 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1732 mProducerEglSurface, mProducerEglContext));
1733
1734 // Produce a frame
1735 glClear(GL_COLOR_BUFFER_BIT);
1736 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1737 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1738
1739 // Destroy the EGLSurface.
1740 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1741 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1742 mProducerEglSurface = EGL_NO_SURFACE;
1743 mSTC.clear();
1744 mANW.clear();
1745 mTextureRenderer.clear();
1746
1747 // Consume a frame
1748 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1749 buffer = mST->getCurrentBuffer();
1750
1751 // Destroy the GL texture object to release its ref
1752 GLuint texID = TEX_ID;
1753 glDeleteTextures(1, &texID);
1754
1755 // make un-current, all references to buffer should be gone
1756 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
1757 EGL_NO_SURFACE, EGL_NO_CONTEXT));
1758
1759 // Destroy consumer
1760 mST.clear();
1761
1762 EXPECT_EQ(1, buffer->getStrongCount());
1763}
1764
1765TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentAfterConsumerDeathUnrefsBuffers) {
1766 sp<GraphicBuffer> buffer;
1767
1768 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1769 mProducerEglSurface, mProducerEglContext));
1770
1771 // Produce a frame
1772 glClear(GL_COLOR_BUFFER_BIT);
1773 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1774 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1775
1776 // Destroy the EGLSurface.
1777 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1778 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1779 mProducerEglSurface = EGL_NO_SURFACE;
1780 mSTC.clear();
1781 mANW.clear();
1782 mTextureRenderer.clear();
1783
1784 // Consume a frame
1785 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1786 buffer = mST->getCurrentBuffer();
1787
1788 // Destroy the GL texture object to release its ref
1789 GLuint texID = TEX_ID;
1790 glDeleteTextures(1, &texID);
1791
1792 // Destroy consumer
1793 mST.clear();
1794
1795 // make un-current, all references to buffer should be gone
1796 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE,
1797 EGL_NO_SURFACE, EGL_NO_CONTEXT));
1798
1799 EXPECT_EQ(1, buffer->getStrongCount());
1800}
1801
Jamie Gennisc2c38022012-04-11 17:20:03 -07001802TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
1803 enum { texWidth = 64 };
1804 enum { texHeight = 64 };
1805
Jamie Gennis46975282012-08-30 18:35:50 -07001806 // This test requires 3 buffers to complete run on a single thread.
1807 mST->setDefaultMaxBufferCount(3);
1808
Jamie Gennisc2c38022012-04-11 17:20:03 -07001809 // Set the user buffer size.
1810 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1811
1812 // Do the producer side of things
1813 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1814 mProducerEglSurface, mProducerEglContext));
1815 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1816
1817 // This is needed to ensure we pick up a buffer of the correct size.
1818 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1819
1820 glClearColor(0.6, 0.6, 0.6, 0.6);
1821 glClear(GL_COLOR_BUFFER_BIT);
1822
1823 glEnable(GL_SCISSOR_TEST);
1824 glScissor(4, 4, 1, 1);
1825 glClearColor(1.0, 0.0, 0.0, 1.0);
1826 glClear(GL_COLOR_BUFFER_BIT);
1827
1828 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1829
1830 // Do the consumer side of things
1831 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1832 mEglContext));
1833 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1834
1835 glDisable(GL_SCISSOR_TEST);
1836
Jamie Gennisd69097f2012-08-30 13:28:23 -07001837 // Skip the first frame, which was empty
1838 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1839 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001840
1841 glClearColor(0.2, 0.2, 0.2, 0.2);
1842 glClear(GL_COLOR_BUFFER_BIT);
1843
1844 glViewport(0, 0, texWidth, texHeight);
1845 drawTexture();
1846
1847 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1848 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1849 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1850 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1851
1852 EXPECT_TRUE(checkPixel( 4, 4, 255, 0, 0, 255));
1853 EXPECT_TRUE(checkPixel( 5, 5, 153, 153, 153, 153));
1854 EXPECT_TRUE(checkPixel( 3, 3, 153, 153, 153, 153));
1855 EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153));
1856 EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153));
1857}
1858
1859TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
1860 enum { texWidth = 64 };
1861 enum { texHeight = 16 };
1862
Jamie Gennis46975282012-08-30 18:35:50 -07001863 // This test requires 3 buffers to complete run on a single thread.
1864 mST->setDefaultMaxBufferCount(3);
1865
Jamie Gennisc2c38022012-04-11 17:20:03 -07001866 // Set the transform hint.
1867 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1868
1869 // Set the user buffer size.
1870 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1871
1872 // Do the producer side of things
1873 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1874 mProducerEglSurface, mProducerEglContext));
1875 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1876
1877 // This is needed to ensure we pick up a buffer of the correct size and the
1878 // new rotation hint.
1879 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1880
1881 glClearColor(0.6, 0.6, 0.6, 0.6);
1882 glClear(GL_COLOR_BUFFER_BIT);
1883
1884 glEnable(GL_SCISSOR_TEST);
1885 glScissor(24, 4, 1, 1);
1886 glClearColor(1.0, 0.0, 0.0, 1.0);
1887 glClear(GL_COLOR_BUFFER_BIT);
1888
1889 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1890
1891 // Do the consumer side of things
1892 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1893 mEglContext));
1894 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1895
1896 glDisable(GL_SCISSOR_TEST);
1897
Jamie Gennisd69097f2012-08-30 13:28:23 -07001898 // Skip the first frame, which was empty
1899 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1900 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001901
1902 glClearColor(0.2, 0.2, 0.2, 0.2);
1903 glClear(GL_COLOR_BUFFER_BIT);
1904
1905 glViewport(0, 0, texWidth, texHeight);
1906 drawTexture();
1907
1908 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1909 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1910 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1911 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1912
1913 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1914 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1915 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1916 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1917 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1918}
1919
1920TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
1921 enum { texWidth = 64 };
1922 enum { texHeight = 16 };
1923
Jamie Gennis46975282012-08-30 18:35:50 -07001924 // This test requires 3 buffers to complete run on a single thread.
1925 mST->setDefaultMaxBufferCount(3);
1926
Jamie Gennisc2c38022012-04-11 17:20:03 -07001927 // Set the transform hint.
1928 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1929
1930 // Set the default buffer size.
1931 mST->setDefaultBufferSize(texWidth, texHeight);
1932
1933 // Do the producer side of things
1934 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1935 mProducerEglSurface, mProducerEglContext));
1936 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1937
1938 // This is needed to ensure we pick up a buffer of the correct size and the
1939 // new rotation hint.
1940 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1941
1942 glClearColor(0.6, 0.6, 0.6, 0.6);
1943 glClear(GL_COLOR_BUFFER_BIT);
1944
1945 glEnable(GL_SCISSOR_TEST);
1946 glScissor(24, 4, 1, 1);
1947 glClearColor(1.0, 0.0, 0.0, 1.0);
1948 glClear(GL_COLOR_BUFFER_BIT);
1949
1950 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1951
1952 // Do the consumer side of things
1953 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1954 mEglContext));
1955 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1956
1957 glDisable(GL_SCISSOR_TEST);
1958
Jamie Gennisd69097f2012-08-30 13:28:23 -07001959 // Skip the first frame, which was empty
1960 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1961 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001962
1963 glClearColor(0.2, 0.2, 0.2, 0.2);
1964 glClear(GL_COLOR_BUFFER_BIT);
1965
1966 glViewport(0, 0, texWidth, texHeight);
1967 drawTexture();
1968
1969 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1970 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1971 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1972 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1973
1974 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1975 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1976 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1977 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1978 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1979}
1980
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001981/*
1982 * This test fixture is for testing GL -> GL texture streaming from one thread
1983 * to another. It contains functionality to create a producer thread that will
1984 * perform GL rendering to an ANativeWindow that feeds frames to a
Andy McFadden2adaf042012-12-18 09:49:45 -08001985 * GLConsumer. Additionally it supports interlocking the producer and
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001986 * consumer threads so that a specific sequence of calls can be
1987 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07001988 *
1989 * The intended usage is as follows:
1990 *
1991 * TEST_F(...) {
1992 * class PT : public ProducerThread {
1993 * virtual void render() {
1994 * ...
1995 * swapBuffers();
1996 * }
1997 * };
1998 *
1999 * runProducerThread(new PT());
2000 *
2001 * // The order of these calls will vary from test to test and may include
2002 * // multiple frames and additional operations (e.g. GL rendering from the
2003 * // texture).
2004 * fc->waitForFrame();
2005 * mST->updateTexImage();
2006 * fc->finishFrame();
2007 * }
2008 *
2009 */
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002010class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07002011protected:
2012
2013 // ProducerThread is an abstract base class to simplify the creation of
2014 // OpenGL ES frame producer threads.
2015 class ProducerThread : public Thread {
2016 public:
2017 virtual ~ProducerThread() {
2018 }
2019
2020 void setEglObjects(EGLDisplay producerEglDisplay,
2021 EGLSurface producerEglSurface,
2022 EGLContext producerEglContext) {
2023 mProducerEglDisplay = producerEglDisplay;
2024 mProducerEglSurface = producerEglSurface;
2025 mProducerEglContext = producerEglContext;
2026 }
2027
2028 virtual bool threadLoop() {
2029 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
2030 mProducerEglSurface, mProducerEglContext);
2031 render();
2032 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2033 EGL_NO_CONTEXT);
2034 return false;
2035 }
2036
2037 protected:
2038 virtual void render() = 0;
2039
2040 void swapBuffers() {
2041 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
2042 }
2043
2044 EGLDisplay mProducerEglDisplay;
2045 EGLSurface mProducerEglSurface;
2046 EGLContext mProducerEglContext;
2047 };
2048
2049 // FrameCondition is a utility class for interlocking between the producer
2050 // and consumer threads. The FrameCondition object should be created and
2051 // destroyed in the consumer thread only. The consumer thread should set
Andy McFadden2adaf042012-12-18 09:49:45 -08002052 // the FrameCondition as the FrameAvailableListener of the GLConsumer,
Jamie Gennis5451d152011-06-08 09:40:45 -07002053 // and should call both waitForFrame and finishFrame once for each expected
2054 // frame.
2055 //
2056 // This interlocking relies on the fact that onFrameAvailable gets called
Andy McFadden2adaf042012-12-18 09:49:45 -08002057 // synchronously from GLConsumer::queueBuffer.
2058 class FrameCondition : public GLConsumer::FrameAvailableListener {
Jamie Gennis5451d152011-06-08 09:40:45 -07002059 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002060 FrameCondition():
2061 mFrameAvailable(false),
2062 mFrameFinished(false) {
2063 }
2064
Jamie Gennis5451d152011-06-08 09:40:45 -07002065 // waitForFrame waits for the next frame to arrive. This should be
2066 // called from the consumer thread once for every frame expected by the
2067 // test.
2068 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07002069 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01002070 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002071 while (!mFrameAvailable) {
2072 mFrameAvailableCondition.wait(mMutex);
2073 }
2074 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01002075 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07002076 }
2077
2078 // Allow the producer to return from its swapBuffers call and continue
2079 // on to produce the next frame. This should be called by the consumer
2080 // thread once for every frame expected by the test.
2081 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07002082 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01002083 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002084 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07002085 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01002086 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07002087 }
2088
Andy McFadden2adaf042012-12-18 09:49:45 -08002089 // This should be called by GLConsumer on the producer thread.
Jamie Gennis5451d152011-06-08 09:40:45 -07002090 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07002091 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01002092 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002093 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07002094 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002095 while (!mFrameFinished) {
2096 mFrameFinishCondition.wait(mMutex);
2097 }
2098 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01002099 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07002100 }
2101
2102 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07002103 bool mFrameAvailable;
2104 bool mFrameFinished;
2105
Jamie Gennis5451d152011-06-08 09:40:45 -07002106 Mutex mMutex;
2107 Condition mFrameAvailableCondition;
2108 Condition mFrameFinishCondition;
2109 };
2110
Jamie Gennis5451d152011-06-08 09:40:45 -07002111 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002112 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07002113 mFC = new FrameCondition();
2114 mST->setFrameAvailableListener(mFC);
2115 }
2116
2117 virtual void TearDown() {
2118 if (mProducerThread != NULL) {
2119 mProducerThread->requestExitAndWait();
2120 }
Jamie Gennis5451d152011-06-08 09:40:45 -07002121 mProducerThread.clear();
2122 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002123 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07002124 }
2125
2126 void runProducerThread(const sp<ProducerThread> producerThread) {
2127 ASSERT_TRUE(mProducerThread == NULL);
2128 mProducerThread = producerThread;
2129 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
2130 mProducerEglContext);
2131 producerThread->run();
2132 }
2133
Jamie Gennis5451d152011-06-08 09:40:45 -07002134 sp<ProducerThread> mProducerThread;
2135 sp<FrameCondition> mFC;
2136};
2137
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002138TEST_F(SurfaceTextureGLThreadToGLTest,
2139 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002140 class PT : public ProducerThread {
2141 virtual void render() {
2142 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2143 glClear(GL_COLOR_BUFFER_BIT);
2144 swapBuffers();
2145 }
2146 };
2147
2148 runProducerThread(new PT());
2149
2150 mFC->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07002151 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07002152 mFC->finishFrame();
2153
2154 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08002155}
Jamie Gennis5451d152011-06-08 09:40:45 -07002156
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002157TEST_F(SurfaceTextureGLThreadToGLTest,
2158 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002159 class PT : public ProducerThread {
2160 virtual void render() {
2161 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2162 glClear(GL_COLOR_BUFFER_BIT);
2163 swapBuffers();
2164 }
2165 };
2166
2167 runProducerThread(new PT());
2168
2169 mFC->waitForFrame();
2170 mFC->finishFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07002171 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07002172
2173 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2174}
2175
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002176TEST_F(SurfaceTextureGLThreadToGLTest,
2177 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002178 enum { NUM_ITERATIONS = 1024 };
2179
2180 class PT : public ProducerThread {
2181 virtual void render() {
2182 for (int i = 0; i < NUM_ITERATIONS; i++) {
2183 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2184 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002185 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002186 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002187 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002188 }
2189 }
2190 };
2191
2192 runProducerThread(new PT());
2193
2194 for (int i = 0; i < NUM_ITERATIONS; i++) {
2195 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01002196 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002197 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002198 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002199 mFC->finishFrame();
2200
2201 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2202 }
2203}
2204
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002205TEST_F(SurfaceTextureGLThreadToGLTest,
2206 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002207 enum { NUM_ITERATIONS = 1024 };
2208
2209 class PT : public ProducerThread {
2210 virtual void render() {
2211 for (int i = 0; i < NUM_ITERATIONS; i++) {
2212 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2213 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002214 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002215 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002216 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002217 }
2218 }
2219 };
2220
2221 runProducerThread(new PT());
2222
2223 for (int i = 0; i < NUM_ITERATIONS; i++) {
2224 mFC->waitForFrame();
2225 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002226 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002227 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002228 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002229
2230 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2231 }
2232}
2233
Jamie Gennis6e502192011-07-21 14:31:31 -07002234// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002235TEST_F(SurfaceTextureGLThreadToGLTest,
2236 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07002237 enum { NUM_ITERATIONS = 64 };
2238
2239 class PT : public ProducerThread {
2240 virtual void render() {
2241 for (int i = 0; i < NUM_ITERATIONS; i++) {
2242 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2243 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002244 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002245 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002246 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002247 }
2248 }
2249 };
2250
Jamie Gennis31a353d2012-08-24 17:25:13 -07002251 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis6e502192011-07-21 14:31:31 -07002252
2253 runProducerThread(new PT());
2254
2255 // Allow three frames to be rendered and queued before starting the
2256 // rendering in this thread. For the latter two frames we don't call
2257 // updateTexImage so the next dequeue from the producer thread will block
2258 // waiting for a frame to become available.
2259 mFC->waitForFrame();
2260 mFC->finishFrame();
2261
2262 // We must call updateTexImage to consume the first frame so that the
2263 // SurfaceTexture is able to reduce the buffer count to 2. This is because
2264 // the GL driver may dequeue a buffer when the EGLSurface is created, and
Jamie Gennis31a353d2012-08-24 17:25:13 -07002265 // that happens before we call setDefaultMaxBufferCount. It's possible that the
Jamie Gennis6e502192011-07-21 14:31:31 -07002266 // driver does not dequeue a buffer at EGLSurface creation time, so we
2267 // cannot rely on this to cause the second dequeueBuffer call to block.
Jamie Gennisd69097f2012-08-30 13:28:23 -07002268 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07002269
2270 mFC->waitForFrame();
2271 mFC->finishFrame();
2272 mFC->waitForFrame();
2273 mFC->finishFrame();
2274
2275 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
2276 // block waiting for a buffer to become available.
2277 usleep(100000);
2278
2279 // Render and present a number of images. This thread should not be blocked
2280 // by the fact that the producer thread is blocking in dequeue.
2281 for (int i = 0; i < NUM_ITERATIONS; i++) {
2282 glClear(GL_COLOR_BUFFER_BIT);
2283 eglSwapBuffers(mEglDisplay, mEglSurface);
2284 }
2285
2286 // Consume the two pending buffers to unblock the producer thread.
Jamie Gennisd69097f2012-08-30 13:28:23 -07002287 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2288 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07002289
2290 // Consume the remaining buffers from the producer thread.
2291 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
2292 mFC->waitForFrame();
2293 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002294 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002295 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002296 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002297 }
2298}
2299
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002300class SurfaceTextureFBOTest : public SurfaceTextureGLTest {
2301protected:
2302
2303 virtual void SetUp() {
2304 SurfaceTextureGLTest::SetUp();
2305
2306 glGenFramebuffers(1, &mFbo);
2307 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2308
2309 glGenTextures(1, &mFboTex);
2310 glBindTexture(GL_TEXTURE_2D, mFboTex);
2311 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getSurfaceWidth(),
2312 getSurfaceHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2313 glBindTexture(GL_TEXTURE_2D, 0);
2314 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2315
2316 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2317 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2318 GL_TEXTURE_2D, mFboTex, 0);
2319 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2320 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2321 }
2322
2323 virtual void TearDown() {
2324 SurfaceTextureGLTest::TearDown();
2325
2326 glDeleteTextures(1, &mFboTex);
2327 glDeleteFramebuffers(1, &mFbo);
2328 }
2329
2330 GLuint mFbo;
2331 GLuint mFboTex;
2332};
2333
2334// This test is intended to verify that proper synchronization is done when
2335// rendering into an FBO.
2336TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) {
2337 const int texWidth = 64;
2338 const int texHeight = 64;
2339
2340 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
2341 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
2342 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
2343 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
2344
2345 android_native_buffer_t* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002346 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2347 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002348 ASSERT_TRUE(anb != NULL);
2349
2350 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002351
2352 // Fill the buffer with green
2353 uint8_t* img = NULL;
2354 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
2355 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255,
2356 0, 255);
2357 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002358 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
2359 -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002360
2361 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2362
2363 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2364 drawTexture();
2365 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2366
2367 for (int i = 0; i < 4; i++) {
2368 SCOPED_TRACE(String8::format("frame %d", i).string());
2369
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002370 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2371 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002372 ASSERT_TRUE(anb != NULL);
2373
2374 buf = new GraphicBuffer(anb, false);
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002375
2376 // Fill the buffer with red
2377 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
2378 (void**)(&img)));
2379 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0,
2380 0, 255);
2381 ASSERT_EQ(NO_ERROR, buf->unlock());
2382 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002383 buf->getNativeBuffer(), -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002384
2385 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2386
2387 drawTexture();
2388
2389 EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255));
2390 }
2391
2392 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2393
2394 EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255));
2395}
2396
Jamie Gennisce561372012-03-19 18:33:05 -07002397class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest {
2398protected:
Jamie Gennis74bed552012-03-28 19:05:54 -07002399 enum { SECOND_TEX_ID = 123 };
2400 enum { THIRD_TEX_ID = 456 };
2401
Jamie Gennisce561372012-03-19 18:33:05 -07002402 SurfaceTextureMultiContextGLTest():
2403 mSecondEglContext(EGL_NO_CONTEXT) {
2404 }
2405
2406 virtual void SetUp() {
2407 SurfaceTextureGLTest::SetUp();
2408
Jamie Gennis74bed552012-03-28 19:05:54 -07002409 // Set up the secondary context and texture renderer.
Jamie Gennisce561372012-03-19 18:33:05 -07002410 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2411 EGL_NO_CONTEXT, getContextAttribs());
2412 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2413 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext);
Jamie Gennis74bed552012-03-28 19:05:54 -07002414
2415 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2416 mSecondEglContext));
2417 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2418 mSecondTextureRenderer = new TextureRenderer(SECOND_TEX_ID, mST);
2419 ASSERT_NO_FATAL_FAILURE(mSecondTextureRenderer->SetUp());
2420
2421 // Set up the tertiary context and texture renderer.
2422 mThirdEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2423 EGL_NO_CONTEXT, getContextAttribs());
2424 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2425 ASSERT_NE(EGL_NO_CONTEXT, mThirdEglContext);
2426
2427 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2428 mThirdEglContext));
2429 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2430 mThirdTextureRenderer = new TextureRenderer(THIRD_TEX_ID, mST);
2431 ASSERT_NO_FATAL_FAILURE(mThirdTextureRenderer->SetUp());
2432
2433 // Switch back to the primary context to start the tests.
2434 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2435 mEglContext));
Jamie Gennisce561372012-03-19 18:33:05 -07002436 }
2437
2438 virtual void TearDown() {
Jamie Gennis74bed552012-03-28 19:05:54 -07002439 if (mThirdEglContext != EGL_NO_CONTEXT) {
2440 eglDestroyContext(mEglDisplay, mThirdEglContext);
2441 }
Jamie Gennisce561372012-03-19 18:33:05 -07002442 if (mSecondEglContext != EGL_NO_CONTEXT) {
2443 eglDestroyContext(mEglDisplay, mSecondEglContext);
2444 }
2445 SurfaceTextureGLTest::TearDown();
2446 }
2447
2448 EGLContext mSecondEglContext;
Jamie Gennis74bed552012-03-28 19:05:54 -07002449 sp<TextureRenderer> mSecondTextureRenderer;
2450
2451 EGLContext mThirdEglContext;
2452 sp<TextureRenderer> mThirdTextureRenderer;
Jamie Gennisce561372012-03-19 18:33:05 -07002453};
2454
2455TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) {
Jamie Gennisce561372012-03-19 18:33:05 -07002456 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2457
2458 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002459 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002460 ASSERT_EQ(OK, mST->updateTexImage());
Jamie Gennisce561372012-03-19 18:33:05 -07002461
2462 // Attempt to latch the texture on the secondary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07002463 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
Jamie Gennisce561372012-03-19 18:33:05 -07002464 mSecondEglContext));
2465 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -07002466 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2467}
2468
2469TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002470 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2471
2472 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002473 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002474 ASSERT_EQ(OK, mST->updateTexImage());
2475
2476 // Detach from the primary context.
2477 ASSERT_EQ(OK, mST->detachFromContext());
2478
2479 // Check that the GL texture was deleted.
2480 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2481}
2482
2483TEST_F(SurfaceTextureMultiContextGLTest,
2484 DetachFromContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002485 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2486
2487 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002488 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002489 ASSERT_EQ(OK, mST->updateTexImage());
2490
2491 // Detach from the primary context.
2492 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2493 ASSERT_EQ(OK, mST->detachFromContext());
2494
2495 // Check that the GL texture was deleted.
2496 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2497}
2498
2499TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002500 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2501
2502 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002503 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002504 ASSERT_EQ(OK, mST->updateTexImage());
2505
2506 // Attempt to detach from the primary context.
2507 mST->abandon();
2508 ASSERT_EQ(NO_INIT, mST->detachFromContext());
2509}
2510
2511TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002512 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2513
2514 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002515 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002516 ASSERT_EQ(OK, mST->updateTexImage());
2517
2518 // Detach from the primary context.
2519 ASSERT_EQ(OK, mST->detachFromContext());
2520
2521 // Attempt to detach from the primary context again.
2522 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2523}
2524
2525TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002526 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2527
2528 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002529 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002530 ASSERT_EQ(OK, mST->updateTexImage());
2531
2532 // Make there be no current display.
2533 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2534 EGL_NO_CONTEXT));
2535 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2536
2537 // Attempt to detach from the primary context.
2538 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2539}
2540
2541TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002542 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2543
2544 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002545 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002546 ASSERT_EQ(OK, mST->updateTexImage());
2547
2548 // Make current context be incorrect.
2549 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2550 mSecondEglContext));
2551 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2552
2553 // Attempt to detach from the primary context.
2554 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2555}
2556
2557TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002558 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2559
2560 // Detach from the primary context.
2561 ASSERT_EQ(OK, mST->detachFromContext());
2562
2563 // Attempt to latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002564 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002565 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2566}
2567
2568TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002569 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2570
2571 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002572 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002573 ASSERT_EQ(OK, mST->updateTexImage());
2574
2575 // Detach from the primary context.
2576 ASSERT_EQ(OK, mST->detachFromContext());
2577
2578 // Attach to the secondary context.
2579 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2580 mSecondEglContext));
2581 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2582
2583 // Verify that the texture object was created and bound.
2584 GLint texBinding = -1;
2585 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2586 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2587
2588 // Try to use the texture from the secondary context.
2589 glClearColor(0.2, 0.2, 0.2, 0.2);
2590 glClear(GL_COLOR_BUFFER_BIT);
2591 glViewport(0, 0, 1, 1);
2592 mSecondTextureRenderer->drawTexture();
2593 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2594 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2595}
2596
2597TEST_F(SurfaceTextureMultiContextGLTest,
2598 AttachToContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002599 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2600
2601 // 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(OK, mST->updateTexImage());
2604
2605 // Detach from the primary context.
2606 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2607 ASSERT_EQ(OK, mST->detachFromContext());
2608
2609 // Attach to the secondary context.
2610 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2611 mSecondEglContext));
2612 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2613
2614 // Verify that the texture object was created and bound.
2615 GLint texBinding = -1;
2616 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2617 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2618
2619 // Try to use the texture from the secondary context.
2620 glClearColor(0.2, 0.2, 0.2, 0.2);
2621 glClear(GL_COLOR_BUFFER_BIT);
2622 glViewport(0, 0, 1, 1);
2623 mSecondTextureRenderer->drawTexture();
2624 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2625 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2626}
2627
2628TEST_F(SurfaceTextureMultiContextGLTest,
2629 AttachToContextSucceedsBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002630 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2631
2632 // Detach from the primary context.
2633 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2634 ASSERT_EQ(OK, mST->detachFromContext());
2635
2636 // Attach to the secondary context.
2637 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2638 mSecondEglContext));
2639 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2640
2641 // Verify that the texture object was created and bound.
2642 GLint texBinding = -1;
2643 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2644 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2645
2646 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002647 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002648 ASSERT_EQ(OK, mST->updateTexImage());
2649
2650 // Try to use the texture from the secondary context.
2651 glClearColor(0.2, 0.2, 0.2, 0.2);
2652 glClear(GL_COLOR_BUFFER_BIT);
2653 glViewport(0, 0, 1, 1);
2654 mSecondTextureRenderer->drawTexture();
2655 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2656 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2657}
2658
2659TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002660 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2661
2662 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002663 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002664 ASSERT_EQ(OK, mST->updateTexImage());
2665
2666 // Detach from the primary context.
2667 ASSERT_EQ(OK, mST->detachFromContext());
2668
2669 // Attempt to attach to the secondary context.
2670 mST->abandon();
2671
2672 // Attempt to attach to the primary context.
2673 ASSERT_EQ(NO_INIT, mST->attachToContext(SECOND_TEX_ID));
2674}
2675
2676TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002677 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2678
2679 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002680 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002681 ASSERT_EQ(OK, mST->updateTexImage());
2682
2683 // Attempt to attach to the primary context.
2684 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2685}
2686
2687TEST_F(SurfaceTextureMultiContextGLTest,
2688 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002689 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2690
2691 // Attempt to attach to the primary context.
2692 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2693}
2694
2695TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002696 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2697
2698 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002699 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002700 ASSERT_EQ(OK, mST->updateTexImage());
2701
2702 // Detach from the primary context.
2703 ASSERT_EQ(OK, mST->detachFromContext());
2704
2705 // Make there be no current display.
2706 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2707 EGL_NO_CONTEXT));
2708 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2709
2710 // Attempt to attach with no context current.
2711 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2712}
2713
2714TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) {
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 // Detach from the primary context.
2722 ASSERT_EQ(OK, mST->detachFromContext());
2723
2724 // Attach to the secondary context.
2725 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2726 mSecondEglContext));
2727 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2728
2729 // Detach from the secondary context.
2730 ASSERT_EQ(OK, mST->detachFromContext());
2731
2732 // Attach to the tertiary context.
2733 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2734 mThirdEglContext));
2735 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2736
2737 // Verify that the texture object was created and bound.
2738 GLint texBinding = -1;
2739 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2740 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2741
2742 // Try to use the texture from the tertiary context.
2743 glClearColor(0.2, 0.2, 0.2, 0.2);
2744 glClear(GL_COLOR_BUFFER_BIT);
2745 glViewport(0, 0, 1, 1);
2746 mThirdTextureRenderer->drawTexture();
2747 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2748 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2749}
2750
2751TEST_F(SurfaceTextureMultiContextGLTest,
2752 AttachToContextSucceedsTwiceBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002753 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2754
2755 // Detach from the primary context.
2756 ASSERT_EQ(OK, mST->detachFromContext());
2757
2758 // Attach to the secondary context.
2759 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2760 mSecondEglContext));
2761 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2762
2763 // Detach from the secondary context.
2764 ASSERT_EQ(OK, mST->detachFromContext());
2765
2766 // Attach to the tertiary context.
2767 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2768 mThirdEglContext));
2769 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2770
2771 // Verify that the texture object was created and bound.
2772 GLint texBinding = -1;
2773 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2774 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2775
2776 // Latch the texture contents on the tertiary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002777 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002778 ASSERT_EQ(OK, mST->updateTexImage());
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));
Jamie Gennisce561372012-03-19 18:33:05 -07002787}
2788
Jesse Hall90ed8502012-05-16 23:44:34 -07002789TEST_F(SurfaceTextureMultiContextGLTest,
2790 UpdateTexImageSucceedsForBufferConsumedBeforeDetach) {
Jamie Gennis31a353d2012-08-24 17:25:13 -07002791 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2));
Jesse Hall90ed8502012-05-16 23:44:34 -07002792
2793 // produce two frames and consume them both on the primary context
2794 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2795 mFW->waitForFrame();
2796 ASSERT_EQ(OK, mST->updateTexImage());
2797
2798 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2799 mFW->waitForFrame();
2800 ASSERT_EQ(OK, mST->updateTexImage());
2801
2802 // produce one more frame
2803 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2804
2805 // Detach from the primary context and attach to the secondary context
2806 ASSERT_EQ(OK, mST->detachFromContext());
2807 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2808 mSecondEglContext));
2809 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2810
2811 // Consume final frame on secondary context
2812 mFW->waitForFrame();
2813 ASSERT_EQ(OK, mST->updateTexImage());
2814}
2815
Jamie Gennis5451d152011-06-08 09:40:45 -07002816} // namespace android