blob: 9062f84c44983193ade847815270663afd7b9e50 [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 <gui/SurfaceTextureClient.h>
23#include <ui/GraphicBuffer.h>
24#include <utils/String8.h>
Jamie Gennis5451d152011-06-08 09:40:45 -070025#include <utils/threads.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080026
Mathias Agopian90ac7992012-02-25 18:48:35 -080027#include <gui/ISurfaceComposer.h>
28#include <gui/Surface.h>
29#include <gui/SurfaceComposerClient.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080030
31#include <EGL/egl.h>
32#include <EGL/eglext.h>
33#include <GLES2/gl2.h>
34#include <GLES2/gl2ext.h>
35
36#include <ui/FramebufferNativeWindow.h>
37
38namespace android {
39
40class GLTest : public ::testing::Test {
41protected:
42
43 GLTest():
44 mEglDisplay(EGL_NO_DISPLAY),
45 mEglSurface(EGL_NO_SURFACE),
46 mEglContext(EGL_NO_CONTEXT) {
47 }
48
49 virtual void SetUp() {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070050 const ::testing::TestInfo* const testInfo =
51 ::testing::UnitTest::GetInstance()->current_test_info();
52 ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
53 testInfo->name());
54
Jamie Gennisd99c0882011-03-10 16:24:46 -080055 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
56 ASSERT_EQ(EGL_SUCCESS, eglGetError());
57 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
58
59 EGLint majorVersion;
60 EGLint minorVersion;
61 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
62 ASSERT_EQ(EGL_SUCCESS, eglGetError());
63 RecordProperty("EglVersionMajor", majorVersion);
64 RecordProperty("EglVersionMajor", minorVersion);
65
Jamie Gennisd99c0882011-03-10 16:24:46 -080066 EGLint numConfigs = 0;
Jamie Gennis1876d132011-03-17 16:32:52 -070067 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080068 1, &numConfigs));
69 ASSERT_EQ(EGL_SUCCESS, eglGetError());
70
71 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
72 if (displaySecsEnv != NULL) {
73 mDisplaySecs = atoi(displaySecsEnv);
74 if (mDisplaySecs < 0) {
75 mDisplaySecs = 0;
76 }
77 } else {
78 mDisplaySecs = 0;
79 }
80
81 if (mDisplaySecs > 0) {
82 mComposerClient = new SurfaceComposerClient;
83 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
84
Jamie Gennisfc850122011-04-25 16:40:05 -070085 mSurfaceControl = mComposerClient->createSurface(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070086 String8("Test Surface"),
Jamie Gennisd99c0882011-03-10 16:24:46 -080087 getSurfaceWidth(), getSurfaceHeight(),
88 PIXEL_FORMAT_RGB_888, 0);
89
90 ASSERT_TRUE(mSurfaceControl != NULL);
91 ASSERT_TRUE(mSurfaceControl->isValid());
92
Mathias Agopian698c0872011-06-28 19:09:31 -070093 SurfaceComposerClient::openGlobalTransaction();
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070094 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080095 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070096 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennisd99c0882011-03-10 16:24:46 -080097
98 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -070099 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800100 window.get(), NULL);
101 } else {
102 EGLint pbufferAttribs[] = {
103 EGL_WIDTH, getSurfaceWidth(),
104 EGL_HEIGHT, getSurfaceHeight(),
105 EGL_NONE };
106
Jamie Gennis1876d132011-03-17 16:32:52 -0700107 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800108 pbufferAttribs);
109 }
110 ASSERT_EQ(EGL_SUCCESS, eglGetError());
111 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
112
Jamie Gennis1876d132011-03-17 16:32:52 -0700113 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800114 getContextAttribs());
115 ASSERT_EQ(EGL_SUCCESS, eglGetError());
116 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
117
118 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
119 mEglContext));
120 ASSERT_EQ(EGL_SUCCESS, eglGetError());
121
122 EGLint w, h;
123 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
124 ASSERT_EQ(EGL_SUCCESS, eglGetError());
125 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
126 ASSERT_EQ(EGL_SUCCESS, eglGetError());
127 RecordProperty("EglSurfaceWidth", w);
128 RecordProperty("EglSurfaceHeight", h);
129
130 glViewport(0, 0, w, h);
131 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
132 }
133
134 virtual void TearDown() {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700135 // Display the result
Jamie Gennisd99c0882011-03-10 16:24:46 -0800136 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
137 eglSwapBuffers(mEglDisplay, mEglSurface);
138 sleep(mDisplaySecs);
139 }
140
141 if (mComposerClient != NULL) {
142 mComposerClient->dispose();
143 }
144 if (mEglContext != EGL_NO_CONTEXT) {
145 eglDestroyContext(mEglDisplay, mEglContext);
146 }
147 if (mEglSurface != EGL_NO_SURFACE) {
148 eglDestroySurface(mEglDisplay, mEglSurface);
149 }
150 if (mEglDisplay != EGL_NO_DISPLAY) {
151 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
152 EGL_NO_CONTEXT);
153 eglTerminate(mEglDisplay);
154 }
155 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700156
157 const ::testing::TestInfo* const testInfo =
158 ::testing::UnitTest::GetInstance()->current_test_info();
159 ALOGV("End test: %s.%s", testInfo->test_case_name(),
160 testInfo->name());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800161 }
162
163 virtual EGLint const* getConfigAttribs() {
164 static EGLint sDefaultConfigAttribs[] = {
165 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
166 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
167 EGL_RED_SIZE, 8,
168 EGL_GREEN_SIZE, 8,
169 EGL_BLUE_SIZE, 8,
170 EGL_ALPHA_SIZE, 8,
171 EGL_DEPTH_SIZE, 16,
172 EGL_STENCIL_SIZE, 8,
173 EGL_NONE };
174
175 return sDefaultConfigAttribs;
176 }
177
178 virtual EGLint const* getContextAttribs() {
179 static EGLint sDefaultContextAttribs[] = {
180 EGL_CONTEXT_CLIENT_VERSION, 2,
181 EGL_NONE };
182
183 return sDefaultContextAttribs;
184 }
185
186 virtual EGLint getSurfaceWidth() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700187 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800188 }
189
190 virtual EGLint getSurfaceHeight() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700191 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800192 }
193
Jamie Gennisd99c0882011-03-10 16:24:46 -0800194 ::testing::AssertionResult checkPixel(int x, int y, int r,
Jamie Gennis824efa72011-06-13 13:41:01 -0700195 int g, int b, int a, int tolerance=2) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800196 GLubyte pixel[4];
197 String8 msg;
198 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
199 GLenum err = glGetError();
200 if (err != GL_NO_ERROR) {
201 msg += String8::format("error reading pixel: %#x", err);
202 while ((err = glGetError()) != GL_NO_ERROR) {
203 msg += String8::format(", %#x", err);
204 }
Jamie Gennisd99c0882011-03-10 16:24:46 -0800205 return ::testing::AssertionFailure(
206 ::testing::Message(msg.string()));
207 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700208 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800209 msg += String8::format("r(%d isn't %d)", pixel[0], r);
210 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700211 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800212 if (!msg.isEmpty()) {
213 msg += " ";
214 }
215 msg += String8::format("g(%d isn't %d)", pixel[1], g);
216 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700217 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800218 if (!msg.isEmpty()) {
219 msg += " ";
220 }
221 msg += String8::format("b(%d isn't %d)", pixel[2], b);
222 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700223 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800224 if (!msg.isEmpty()) {
225 msg += " ";
226 }
227 msg += String8::format("a(%d isn't %d)", pixel[3], a);
228 }
229 if (!msg.isEmpty()) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800230 return ::testing::AssertionFailure(
231 ::testing::Message(msg.string()));
232 } else {
233 return ::testing::AssertionSuccess();
234 }
235 }
236
Daniel Lam016c8cb2012-04-03 15:54:58 -0700237 ::testing::AssertionResult assertRectEq(const Rect &r1,
238 const Rect &r2, int tolerance=1) {
239
240 String8 msg;
241
242 if (abs(r1.left - r2.left) > tolerance) {
243 msg += String8::format("left(%d isn't %d)", r1.left, r2.left);
244 }
245 if (abs(r1.top - r2.top) > tolerance) {
246 if (!msg.isEmpty()) {
247 msg += " ";
248 }
249 msg += String8::format("top(%d isn't %d)", r1.top, r2.top);
250 }
251 if (abs(r1.right - r2.right) > tolerance) {
252 if (!msg.isEmpty()) {
253 msg += " ";
254 }
255 msg += String8::format("right(%d isn't %d)", r1.right, r2.right);
256 }
257 if (abs(r1.bottom - r2.bottom) > tolerance) {
258 if (!msg.isEmpty()) {
259 msg += " ";
260 }
261 msg += String8::format("bottom(%d isn't %d)", r1.bottom, r2.bottom);
262 }
263 if (!msg.isEmpty()) {
264 msg += String8::format(" R1: [%d %d %d %d] R2: [%d %d %d %d]",
265 r1.left, r1.top, r1.right, r1.bottom,
266 r2.left, r2.top, r2.right, r2.bottom);
267 fprintf(stderr, "assertRectEq: %s\n", msg.string());
268 return ::testing::AssertionFailure(
269 ::testing::Message(msg.string()));
270 } else {
271 return ::testing::AssertionSuccess();
272 }
273 }
274
Jamie Gennisd99c0882011-03-10 16:24:46 -0800275 int mDisplaySecs;
276 sp<SurfaceComposerClient> mComposerClient;
277 sp<SurfaceControl> mSurfaceControl;
278
279 EGLDisplay mEglDisplay;
280 EGLSurface mEglSurface;
281 EGLContext mEglContext;
Jamie Gennis1876d132011-03-17 16:32:52 -0700282 EGLConfig mGlConfig;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800283};
284
Jamie Gennis74bed552012-03-28 19:05:54 -0700285static void loadShader(GLenum shaderType, const char* pSource,
286 GLuint* outShader) {
287 GLuint shader = glCreateShader(shaderType);
288 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
289 if (shader) {
290 glShaderSource(shader, 1, &pSource, NULL);
291 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
292 glCompileShader(shader);
293 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
294 GLint compiled = 0;
295 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
296 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
297 if (!compiled) {
298 GLint infoLen = 0;
299 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
300 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
301 if (infoLen) {
302 char* buf = (char*) malloc(infoLen);
303 if (buf) {
304 glGetShaderInfoLog(shader, infoLen, NULL, buf);
305 printf("Shader compile log:\n%s\n", buf);
306 free(buf);
307 FAIL();
308 }
309 } else {
310 char* buf = (char*) malloc(0x1000);
311 if (buf) {
312 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
313 printf("Shader compile log:\n%s\n", buf);
314 free(buf);
315 FAIL();
316 }
317 }
318 glDeleteShader(shader);
319 shader = 0;
320 }
321 }
322 ASSERT_TRUE(shader != 0);
323 *outShader = shader;
324}
325
326static void createProgram(const char* pVertexSource,
327 const char* pFragmentSource, GLuint* outPgm) {
328 GLuint vertexShader, fragmentShader;
329 {
330 SCOPED_TRACE("compiling vertex shader");
331 ASSERT_NO_FATAL_FAILURE(loadShader(GL_VERTEX_SHADER, pVertexSource,
332 &vertexShader));
333 }
334 {
335 SCOPED_TRACE("compiling fragment shader");
336 ASSERT_NO_FATAL_FAILURE(loadShader(GL_FRAGMENT_SHADER, pFragmentSource,
337 &fragmentShader));
338 }
339
340 GLuint program = glCreateProgram();
341 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
342 if (program) {
343 glAttachShader(program, vertexShader);
344 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
345 glAttachShader(program, fragmentShader);
346 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
347 glLinkProgram(program);
348 GLint linkStatus = GL_FALSE;
349 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
350 if (linkStatus != GL_TRUE) {
351 GLint bufLength = 0;
352 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
353 if (bufLength) {
354 char* buf = (char*) malloc(bufLength);
355 if (buf) {
356 glGetProgramInfoLog(program, bufLength, NULL, buf);
357 printf("Program link log:\n%s\n", buf);
358 free(buf);
359 FAIL();
360 }
361 }
362 glDeleteProgram(program);
363 program = 0;
364 }
365 }
366 glDeleteShader(vertexShader);
367 glDeleteShader(fragmentShader);
368 ASSERT_TRUE(program != 0);
369 *outPgm = program;
370}
371
372static int abs(int value) {
373 return value > 0 ? value : -value;
374}
375
376
Jamie Gennisd99c0882011-03-10 16:24:46 -0800377// XXX: Code above this point should live elsewhere
378
379class SurfaceTextureGLTest : public GLTest {
380protected:
Jamie Gennis79e31252011-10-19 15:19:19 -0700381 enum { TEX_ID = 123 };
Jamie Gennisd99c0882011-03-10 16:24:46 -0800382
383 virtual void SetUp() {
384 GLTest::SetUp();
Andy McFadden2adaf042012-12-18 09:49:45 -0800385 mST = new GLConsumer(TEX_ID);
Jamie Gennisc911ea52012-12-12 12:38:28 -0800386 mSTC = new SurfaceTextureClient(mST->getBufferQueue());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800387 mANW = mSTC;
Jamie Gennis74bed552012-03-28 19:05:54 -0700388 mTextureRenderer = new TextureRenderer(TEX_ID, mST);
389 ASSERT_NO_FATAL_FAILURE(mTextureRenderer->SetUp());
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700390 mFW = new FrameWaiter;
391 mST->setFrameAvailableListener(mFW);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800392 }
393
Jamie Gennis2640bfd2011-07-14 17:11:47 -0700394 virtual void TearDown() {
395 mANW.clear();
396 mSTC.clear();
397 mST.clear();
398 GLTest::TearDown();
399 }
400
Jamie Gennisd99c0882011-03-10 16:24:46 -0800401 void drawTexture() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700402 mTextureRenderer->drawTexture();
Jamie Gennisd99c0882011-03-10 16:24:46 -0800403 }
404
Jamie Gennis74bed552012-03-28 19:05:54 -0700405 class TextureRenderer: public RefBase {
406 public:
Andy McFadden2adaf042012-12-18 09:49:45 -0800407 TextureRenderer(GLuint texName, const sp<GLConsumer>& st):
Jamie Gennis74bed552012-03-28 19:05:54 -0700408 mTexName(texName),
409 mST(st) {
410 }
411
412 void SetUp() {
413 const char vsrc[] =
414 "attribute vec4 vPosition;\n"
415 "varying vec2 texCoords;\n"
416 "uniform mat4 texMatrix;\n"
417 "void main() {\n"
418 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
419 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
420 " gl_Position = vPosition;\n"
421 "}\n";
422
423 const char fsrc[] =
424 "#extension GL_OES_EGL_image_external : require\n"
425 "precision mediump float;\n"
426 "uniform samplerExternalOES texSampler;\n"
427 "varying vec2 texCoords;\n"
428 "void main() {\n"
429 " gl_FragColor = texture2D(texSampler, texCoords);\n"
430 "}\n";
431
432 {
433 SCOPED_TRACE("creating shader program");
434 ASSERT_NO_FATAL_FAILURE(createProgram(vsrc, fsrc, &mPgm));
435 }
436
437 mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
438 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
439 ASSERT_NE(-1, mPositionHandle);
440 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
441 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
442 ASSERT_NE(-1, mTexSamplerHandle);
443 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
444 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
445 ASSERT_NE(-1, mTexMatrixHandle);
446 }
447
Andy McFadden2adaf042012-12-18 09:49:45 -0800448 // drawTexture draws the GLConsumer over the entire GL viewport.
Jamie Gennis74bed552012-03-28 19:05:54 -0700449 void drawTexture() {
Jamie Gennisa96b6bd2012-04-11 17:27:12 -0700450 static const GLfloat triangleVertices[] = {
Jamie Gennis74bed552012-03-28 19:05:54 -0700451 -1.0f, 1.0f,
452 -1.0f, -1.0f,
453 1.0f, -1.0f,
454 1.0f, 1.0f,
455 };
456
457 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
458 triangleVertices);
459 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
460 glEnableVertexAttribArray(mPositionHandle);
461 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
462
463 glUseProgram(mPgm);
464 glUniform1i(mTexSamplerHandle, 0);
465 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
466 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
467 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
468
469 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
470 // they're setting the defautls for that target, but when hacking
471 // things to use GL_TEXTURE_2D they are needed to achieve the same
472 // behavior.
473 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
474 GL_LINEAR);
475 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
476 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
477 GL_LINEAR);
478 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
479 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
480 GL_CLAMP_TO_EDGE);
481 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
482 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
483 GL_CLAMP_TO_EDGE);
484 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
485
486 GLfloat texMatrix[16];
487 mST->getTransformMatrix(texMatrix);
488 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
489
490 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
491 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
492 }
493
494 GLuint mTexName;
Andy McFadden2adaf042012-12-18 09:49:45 -0800495 sp<GLConsumer> mST;
Jamie Gennis74bed552012-03-28 19:05:54 -0700496 GLuint mPgm;
497 GLint mPositionHandle;
498 GLint mTexSamplerHandle;
499 GLint mTexMatrixHandle;
500 };
501
Andy McFadden2adaf042012-12-18 09:49:45 -0800502 class FrameWaiter : public GLConsumer::FrameAvailableListener {
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700503 public:
504 FrameWaiter():
505 mPendingFrames(0) {
506 }
507
508 void waitForFrame() {
509 Mutex::Autolock lock(mMutex);
510 while (mPendingFrames == 0) {
511 mCondition.wait(mMutex);
512 }
513 mPendingFrames--;
514 }
515
516 virtual void onFrameAvailable() {
517 Mutex::Autolock lock(mMutex);
518 mPendingFrames++;
519 mCondition.signal();
520 }
521
522 int mPendingFrames;
523 Mutex mMutex;
524 Condition mCondition;
525 };
526
Andy McFadden2adaf042012-12-18 09:49:45 -0800527 // Note that GLConsumer will lose the notifications
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700528 // onBuffersReleased and onFrameAvailable as there is currently
529 // no way to forward the events. This DisconnectWaiter will not let the
530 // disconnect finish until finishDisconnect() is called. It will
531 // also block until a disconnect is called
532 class DisconnectWaiter : public BufferQueue::ConsumerListener {
533 public:
534 DisconnectWaiter () :
535 mWaitForDisconnect(false),
536 mPendingFrames(0) {
537 }
538
539 void waitForFrame() {
540 Mutex::Autolock lock(mMutex);
541 while (mPendingFrames == 0) {
542 mFrameCondition.wait(mMutex);
543 }
544 mPendingFrames--;
545 }
546
547 virtual void onFrameAvailable() {
548 Mutex::Autolock lock(mMutex);
549 mPendingFrames++;
550 mFrameCondition.signal();
551 }
552
553 virtual void onBuffersReleased() {
554 Mutex::Autolock lock(mMutex);
555 while (!mWaitForDisconnect) {
556 mDisconnectCondition.wait(mMutex);
557 }
558 }
559
560 void finishDisconnect() {
561 Mutex::Autolock lock(mMutex);
562 mWaitForDisconnect = true;
563 mDisconnectCondition.signal();
564 }
565
566 private:
567 Mutex mMutex;
568
569 bool mWaitForDisconnect;
570 Condition mDisconnectCondition;
571
572 int mPendingFrames;
573 Condition mFrameCondition;
574 };
575
Andy McFadden2adaf042012-12-18 09:49:45 -0800576 sp<GLConsumer> mST;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800577 sp<SurfaceTextureClient> mSTC;
578 sp<ANativeWindow> mANW;
Jamie Gennis74bed552012-03-28 19:05:54 -0700579 sp<TextureRenderer> mTextureRenderer;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700580 sp<FrameWaiter> mFW;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800581};
582
583// Fill a YV12 buffer with a multi-colored checkerboard pattern
584void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
585 const int blockWidth = w > 16 ? w / 16 : 1;
586 const int blockHeight = h > 16 ? h / 16 : 1;
587 const int yuvTexOffsetY = 0;
588 int yuvTexStrideY = stride;
589 int yuvTexOffsetV = yuvTexStrideY * h;
590 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
591 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
592 int yuvTexStrideU = yuvTexStrideV;
593 for (int x = 0; x < w; x++) {
594 for (int y = 0; y < h; y++) {
595 int parityX = (x / blockWidth) & 1;
596 int parityY = (y / blockHeight) & 1;
597 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
598 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
599 if (x < w / 2 && y < h / 2) {
600 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
601 if (x * 2 < w / 2 && y * 2 < h / 2) {
602 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
603 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
604 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
605 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
606 intensity;
607 }
608 }
609 }
610 }
611}
612
613// Fill a YV12 buffer with red outside a given rectangle and green inside it.
614void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
615 const android_native_rect_t& rect) {
616 const int yuvTexOffsetY = 0;
617 int yuvTexStrideY = stride;
618 int yuvTexOffsetV = yuvTexStrideY * h;
619 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
620 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
621 int yuvTexStrideU = yuvTexStrideV;
622 for (int x = 0; x < w; x++) {
623 for (int y = 0; y < h; y++) {
624 bool inside = rect.left <= x && x < rect.right &&
625 rect.top <= y && y < rect.bottom;
626 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
627 if (x < w / 2 && y < h / 2) {
628 bool inside = rect.left <= 2*x && 2*x < rect.right &&
629 rect.top <= 2*y && 2*y < rect.bottom;
630 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
631 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
632 inside ? 16 : 255;
633 }
634 }
635 }
636}
637
Jamie Gennis1876d132011-03-17 16:32:52 -0700638void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
639 const size_t PIXEL_SIZE = 4;
640 for (int x = 0; x < w; x++) {
641 for (int y = 0; y < h; y++) {
642 off_t offset = (y * stride + x) * PIXEL_SIZE;
643 for (int c = 0; c < 4; c++) {
644 int parityX = (x / (1 << (c+2))) & 1;
645 int parityY = (y / (1 << (c+2))) & 1;
646 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
647 }
648 }
649 }
650}
651
Jamie Gennisfe27e2f2011-11-11 18:05:11 -0800652void fillRGBA8BufferSolid(uint8_t* buf, int w, int h, int stride, uint8_t r,
653 uint8_t g, uint8_t b, uint8_t a) {
654 const size_t PIXEL_SIZE = 4;
655 for (int y = 0; y < h; y++) {
656 for (int x = 0; x < h; x++) {
657 off_t offset = (y * stride + x) * PIXEL_SIZE;
658 buf[offset + 0] = r;
659 buf[offset + 1] = g;
660 buf[offset + 2] = b;
661 buf[offset + 3] = a;
662 }
663 }
664}
665
Jamie Gennisce561372012-03-19 18:33:05 -0700666// Produce a single RGBA8 frame by filling a buffer with a checkerboard pattern
667// using the CPU. This assumes that the ANativeWindow is already configured to
668// allow this to be done (e.g. the format is set to RGBA8).
669//
670// Calls to this function should be wrapped in an ASSERT_NO_FATAL_FAILURE().
671void produceOneRGBA8Frame(const sp<ANativeWindow>& anw) {
672 android_native_buffer_t* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700673 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
674 &anb));
Jamie Gennisce561372012-03-19 18:33:05 -0700675 ASSERT_TRUE(anb != NULL);
676
677 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisce561372012-03-19 18:33:05 -0700678
679 uint8_t* img = NULL;
680 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
681 (void**)(&img)));
682 fillRGBA8Buffer(img, buf->getWidth(), buf->getHeight(), buf->getStride());
683 ASSERT_EQ(NO_ERROR, buf->unlock());
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700684 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer(),
685 -1));
Jamie Gennisce561372012-03-19 18:33:05 -0700686}
687
Jamie Gennisd99c0882011-03-10 16:24:46 -0800688TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700689 const int texWidth = 64;
690 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800691
692 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700693 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800694 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
695 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
696
Iliyan Malchev697526b2011-05-01 11:33:26 -0700697 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700698 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
699 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800700 ASSERT_TRUE(anb != NULL);
701
702 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800703
704 // Fill the buffer with the a checkerboard pattern
705 uint8_t* img = NULL;
706 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700707 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800708 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700709 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
710 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800711
Jamie Gennisd69097f2012-08-30 13:28:23 -0700712 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800713
714 glClearColor(0.2, 0.2, 0.2, 0.2);
715 glClear(GL_COLOR_BUFFER_BIT);
716
Jamie Gennisc8c51522011-06-15 14:24:38 -0700717 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800718 drawTexture();
719
720 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255));
721 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700722 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255));
723 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800724
Jamie Gennisc8c51522011-06-15 14:24:38 -0700725 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255));
726 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255));
727 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800728 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700729 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800730 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255));
731 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255));
732}
733
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700734TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700735 const int texWidth = 64;
736 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800737
738 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700739 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800740 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
741 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
742
Iliyan Malchev697526b2011-05-01 11:33:26 -0700743 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700744 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
745 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800746 ASSERT_TRUE(anb != NULL);
747
748 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800749
750 // Fill the buffer with the a checkerboard pattern
751 uint8_t* img = NULL;
752 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700753 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800754 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700755 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
756 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800757
Jamie Gennisd69097f2012-08-30 13:28:23 -0700758 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800759
760 glClearColor(0.2, 0.2, 0.2, 0.2);
761 glClear(GL_COLOR_BUFFER_BIT);
762
Jamie Gennisc8c51522011-06-15 14:24:38 -0700763 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800764 drawTexture();
765
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700766 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
767 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800768 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
769 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
770
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700771 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
772 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
773 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
774 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
775 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
776 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
777 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800778}
779
780TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700781 const int texWidth = 64;
782 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800783
784 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700785 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800786 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
787 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
788
789 android_native_rect_t crops[] = {
790 {4, 6, 22, 36},
791 {0, 6, 22, 36},
792 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700793 {4, 6, texWidth, 36},
794 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800795 };
796
797 for (int i = 0; i < 5; i++) {
798 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800799 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
800 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800801
802 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
803
Iliyan Malchev697526b2011-05-01 11:33:26 -0700804 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700805 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
806 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800807 ASSERT_TRUE(anb != NULL);
808
809 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800810
811 uint8_t* img = NULL;
812 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700813 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800814 buf->unlock();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800815 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700816 buf->getNativeBuffer(), -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800817
Jamie Gennisd69097f2012-08-30 13:28:23 -0700818 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800819
820 glClearColor(0.2, 0.2, 0.2, 0.2);
821 glClear(GL_COLOR_BUFFER_BIT);
822
Jamie Gennisc8c51522011-06-15 14:24:38 -0700823 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800824 drawTexture();
825
826 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
827 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
828 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
829 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
830
831 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
832 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
833 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
834 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
835 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
836 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
837 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
838 }
839}
840
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700841// This test is intended to catch synchronization bugs between the CPU-written
842// and GPU-read buffers.
843TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
844 enum { texWidth = 16 };
845 enum { texHeight = 16 };
846 enum { numFrames = 1024 };
847
848 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -0700849 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2));
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700850 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
851 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
852 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
853 GRALLOC_USAGE_SW_WRITE_OFTEN));
854
855 struct TestPixel {
856 int x;
857 int y;
858 };
859 const TestPixel testPixels[] = {
860 { 4, 11 },
861 { 12, 14 },
862 { 7, 2 },
863 };
864 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
865
866 class ProducerThread : public Thread {
867 public:
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800868 ProducerThread(const sp<ANativeWindow>& anw,
869 const TestPixel* testPixels):
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700870 mANW(anw),
871 mTestPixels(testPixels) {
872 }
873
874 virtual ~ProducerThread() {
875 }
876
877 virtual bool threadLoop() {
878 for (int i = 0; i < numFrames; i++) {
879 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700880 if (native_window_dequeue_buffer_and_wait(mANW.get(),
881 &anb) != NO_ERROR) {
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700882 return false;
883 }
884 if (anb == NULL) {
885 return false;
886 }
887
888 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700889
890 const int yuvTexOffsetY = 0;
891 int stride = buf->getStride();
892 int yuvTexStrideY = stride;
893 int yuvTexOffsetV = yuvTexStrideY * texHeight;
894 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
895 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
896 int yuvTexStrideU = yuvTexStrideV;
897
898 uint8_t* img = NULL;
899 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
900
901 // Gray out all the test pixels first, so we're more likely to
902 // see a failure if GL is still texturing from the buffer we
903 // just dequeued.
904 for (int j = 0; j < numTestPixels; j++) {
905 int x = mTestPixels[j].x;
906 int y = mTestPixels[j].y;
907 uint8_t value = 128;
908 img[y*stride + x] = value;
909 }
910
911 // Fill the buffer with gray.
912 for (int y = 0; y < texHeight; y++) {
913 for (int x = 0; x < texWidth; x++) {
914 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
915 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
916 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
917 }
918 }
919
920 // Set the test pixels to either white or black.
921 for (int j = 0; j < numTestPixels; j++) {
922 int x = mTestPixels[j].x;
923 int y = mTestPixels[j].y;
924 uint8_t value = 0;
925 if (j == (i % numTestPixels)) {
926 value = 255;
927 }
928 img[y*stride + x] = value;
929 }
930
931 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700932 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), -1)
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700933 != NO_ERROR) {
934 return false;
935 }
936 }
937 return false;
938 }
939
940 sp<ANativeWindow> mANW;
941 const TestPixel* mTestPixels;
942 };
943
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700944 sp<Thread> pt(new ProducerThread(mANW, testPixels));
945 pt->run();
946
947 glViewport(0, 0, texWidth, texHeight);
948
949 glClearColor(0.2, 0.2, 0.2, 0.2);
950 glClear(GL_COLOR_BUFFER_BIT);
951
952 // We wait for the first two frames up front so that the producer will be
953 // likely to dequeue the buffer that's currently being textured from.
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700954 mFW->waitForFrame();
955 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700956
957 for (int i = 0; i < numFrames; i++) {
958 SCOPED_TRACE(String8::format("frame %d", i).string());
959
960 // We must wait for each frame to come in because if we ever do an
961 // updateTexImage call that doesn't consume a newly available buffer
962 // then the producer and consumer will get out of sync, which will cause
963 // a deadlock.
964 if (i > 1) {
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700965 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700966 }
Jamie Gennisd69097f2012-08-30 13:28:23 -0700967 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700968 drawTexture();
969
970 for (int j = 0; j < numTestPixels; j++) {
971 int x = testPixels[j].x;
972 int y = testPixels[j].y;
973 uint8_t value = 0;
974 if (j == (i % numTestPixels)) {
975 // We must y-invert the texture coords
976 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
977 } else {
978 // We must y-invert the texture coords
979 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
980 }
981 }
982 }
983
984 pt->requestExitAndWait();
985}
986
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700987TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700988 const int texWidth = 64;
989 const int texHeight = 66;
990
991 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
992 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
993 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
994 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
995
Jamie Gennisce561372012-03-19 18:33:05 -0700996 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -0700997
Jamie Gennisd69097f2012-08-30 13:28:23 -0700998 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis1876d132011-03-17 16:32:52 -0700999
1000 glClearColor(0.2, 0.2, 0.2, 0.2);
1001 glClear(GL_COLOR_BUFFER_BIT);
1002
Jamie Gennisc8c51522011-06-15 14:24:38 -07001003 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001004 drawTexture();
1005
1006 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
1007 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001008 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
1009 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001010
1011 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001012 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001013 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001014 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
1015 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001016 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001017 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001018 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
1019 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
1020 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001021 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001022 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
1023 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001024 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001025 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001026 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
1027}
1028
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001029TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -07001030 const int texWidth = 64;
1031 const int texHeight = 64;
1032
1033 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
1034 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
1035 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
1036 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
1037
Jamie Gennisce561372012-03-19 18:33:05 -07001038 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -07001039
Jamie Gennisd69097f2012-08-30 13:28:23 -07001040 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis1876d132011-03-17 16:32:52 -07001041
1042 glClearColor(0.2, 0.2, 0.2, 0.2);
1043 glClear(GL_COLOR_BUFFER_BIT);
1044
Jamie Gennisc8c51522011-06-15 14:24:38 -07001045 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001046 drawTexture();
1047
1048 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
1049 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
1050 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
1051 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
1052
1053 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
1054 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
1055 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
1056 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
1057 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
1058 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
1059 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
1060 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
1061 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
1062 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
1063 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
1064 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
1065 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
1066 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
1067 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
1068 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
1069}
1070
Andy McFadden2adaf042012-12-18 09:49:45 -08001071// Tests if GLConsumer and BufferQueue are robust enough
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001072// to handle a special case where updateTexImage is called
1073// in the middle of disconnect. This ordering is enforced
1074// by blocking in the disconnect callback.
1075TEST_F(SurfaceTextureGLTest, DisconnectStressTest) {
1076
1077 class ProducerThread : public Thread {
1078 public:
1079 ProducerThread(const sp<ANativeWindow>& anw):
1080 mANW(anw) {
1081 }
1082
1083 virtual ~ProducerThread() {
1084 }
1085
1086 virtual bool threadLoop() {
1087 ANativeWindowBuffer* anb;
1088
1089 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL);
1090
1091 for (int numFrames =0 ; numFrames < 2; numFrames ++) {
1092
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001093 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1094 &anb) != NO_ERROR) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001095 return false;
1096 }
1097 if (anb == NULL) {
1098 return false;
1099 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001100 if (mANW->queueBuffer(mANW.get(), anb, -1)
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001101 != NO_ERROR) {
1102 return false;
1103 }
1104 }
1105
1106 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_EGL);
1107
1108 return false;
1109 }
1110
1111 private:
1112 sp<ANativeWindow> mANW;
1113 };
1114
1115 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1116
1117 sp<DisconnectWaiter> dw(new DisconnectWaiter());
1118 mST->getBufferQueue()->consumerConnect(dw);
1119
1120
1121 sp<Thread> pt(new ProducerThread(mANW));
1122 pt->run();
1123
Andy McFadden2adaf042012-12-18 09:49:45 -08001124 // eat a frame so GLConsumer will own an at least one slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001125 dw->waitForFrame();
1126 EXPECT_EQ(OK,mST->updateTexImage());
1127
1128 dw->waitForFrame();
Andy McFadden2adaf042012-12-18 09:49:45 -08001129 // Could fail here as GLConsumer thinks it still owns the slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001130 // but bufferQueue has released all slots
1131 EXPECT_EQ(OK,mST->updateTexImage());
1132
1133 dw->finishDisconnect();
1134}
1135
1136
Andy McFadden2adaf042012-12-18 09:49:45 -08001137// This test ensures that the GLConsumer clears the mCurrentTexture
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001138// when it is disconnected and reconnected. Otherwise it will
1139// attempt to release a buffer that it does not owned
1140TEST_F(SurfaceTextureGLTest, DisconnectClearsCurrentTexture) {
1141 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1142
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001143 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1144 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001145
1146 ANativeWindowBuffer *anb;
1147
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001148 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1149 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001150
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001151 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1152 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001153
1154 EXPECT_EQ(OK,mST->updateTexImage());
1155 EXPECT_EQ(OK,mST->updateTexImage());
1156
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001157 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1158 NATIVE_WINDOW_API_EGL));
1159 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1160 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001161
1162 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1163
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001164 EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1165 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001166
1167 // Will fail here if mCurrentTexture is not cleared properly
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001168 mFW->waitForFrame();
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001169 EXPECT_EQ(OK,mST->updateTexImage());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001170
1171 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1172 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001173}
1174
Daniel Lam016c8cb2012-04-03 15:54:58 -07001175TEST_F(SurfaceTextureGLTest, ScaleToWindowMode) {
1176 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1177
1178 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1179 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));
1180
1181 // The producer image size
1182 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1183
1184 // The consumer image size (16 x 9) ratio
1185 mST->setDefaultBufferSize(1280, 720);
1186
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001187 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1188 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001189
1190 ANativeWindowBuffer *anb;
1191
1192 android_native_rect_t odd = {23, 78, 123, 477};
1193 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &odd));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001194 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1195 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001196 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001197 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001198 Rect r = mST->getCurrentCrop();
1199 assertRectEq(Rect(23, 78, 123, 477), r);
1200
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001201 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1202 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001203}
1204
1205// This test ensures the scaling mode does the right thing
1206// ie NATIVE_WINDOW_SCALING_MODE_CROP should crop
1207// the image such that it has the same aspect ratio as the
1208// default buffer size
1209TEST_F(SurfaceTextureGLTest, CroppedScalingMode) {
1210 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1211
1212 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1213 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP));
1214
1215 // The producer image size
1216 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1217
1218 // The consumer image size (16 x 9) ratio
1219 mST->setDefaultBufferSize(1280, 720);
1220
1221 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU);
1222
1223 ANativeWindowBuffer *anb;
1224
1225 // The crop is in the shape of (320, 180) === 16 x 9
1226 android_native_rect_t standard = {10, 20, 330, 200};
1227 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &standard));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001228 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1229 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001230 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001231 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001232 Rect r = mST->getCurrentCrop();
1233 // crop should be the same as crop (same aspect ratio)
1234 assertRectEq(Rect(10, 20, 330, 200), r);
1235
1236 // make this wider then desired aspect 239 x 100 (2.39:1)
1237 android_native_rect_t wide = {20, 30, 259, 130};
1238 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &wide));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001239 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1240 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001241 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001242 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001243 r = mST->getCurrentCrop();
1244 // crop should be the same height, but have cropped left and right borders
1245 // offset is 30.6 px L+, R-
1246 assertRectEq(Rect(51, 30, 228, 130), r);
1247
1248 // This image is taller then desired aspect 400 x 300 (4:3)
1249 android_native_rect_t narrow = {0, 0, 400, 300};
1250 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &narrow));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001251 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1252 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001253 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001254 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001255 r = mST->getCurrentCrop();
1256 // crop should be the same width, but have cropped top and bottom borders
1257 // offset is 37.5 px
1258 assertRectEq(Rect(0, 37, 400, 262), r);
1259
1260 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
1261}
1262
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001263TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
1264 class ProducerThread : public Thread {
1265 public:
1266 ProducerThread(const sp<ANativeWindow>& anw):
1267 mANW(anw),
1268 mDequeueError(NO_ERROR) {
1269 }
1270
1271 virtual ~ProducerThread() {
1272 }
1273
1274 virtual bool threadLoop() {
1275 Mutex::Autolock lock(mMutex);
1276 ANativeWindowBuffer* anb;
1277
1278 // Frame 1
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001279 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1280 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001281 return false;
1282 }
1283 if (anb == NULL) {
1284 return false;
1285 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001286 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001287 != NO_ERROR) {
1288 return false;
1289 }
1290
1291 // Frame 2
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001292 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1293 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001294 return false;
1295 }
1296 if (anb == NULL) {
1297 return false;
1298 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001299 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001300 != NO_ERROR) {
1301 return false;
1302 }
1303
1304 // Frame 3 - error expected
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001305 mDequeueError = native_window_dequeue_buffer_and_wait(mANW.get(),
1306 &anb);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001307 return false;
1308 }
1309
1310 status_t getDequeueError() {
1311 Mutex::Autolock lock(mMutex);
1312 return mDequeueError;
1313 }
1314
1315 private:
1316 sp<ANativeWindow> mANW;
1317 status_t mDequeueError;
1318 Mutex mMutex;
1319 };
1320
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001321 ASSERT_EQ(OK, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -07001322 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001323
1324 sp<Thread> pt(new ProducerThread(mANW));
1325 pt->run();
1326
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001327 mFW->waitForFrame();
1328 mFW->waitForFrame();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001329
1330 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1331 // block waiting for a buffer to become available.
1332 usleep(100000);
1333
1334 mST->abandon();
1335
1336 pt->requestExitAndWait();
1337 ASSERT_EQ(NO_INIT,
1338 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1339}
1340
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001341TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1342 int texHeight = 16;
1343 ANativeWindowBuffer* anb;
1344
1345 GLint maxTextureSize;
1346 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1347
1348 // make sure it works with small textures
1349 mST->setDefaultBufferSize(16, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001350 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1351 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001352 EXPECT_EQ(16, anb->width);
1353 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001354 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001355 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1356
1357 // make sure it works with GL_MAX_TEXTURE_SIZE
1358 mST->setDefaultBufferSize(maxTextureSize, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001359 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1360 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001361 EXPECT_EQ(maxTextureSize, anb->width);
1362 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001363 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001364 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1365
1366 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1367 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001368 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1369 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001370 EXPECT_EQ(maxTextureSize+1, anb->width);
1371 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001372 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001373 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1374}
1375
Jamie Gennis5451d152011-06-08 09:40:45 -07001376/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001377 * This test fixture is for testing GL -> GL texture streaming. It creates an
1378 * EGLSurface and an EGLContext for the image producer to use.
1379 */
1380class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1381protected:
1382 SurfaceTextureGLToGLTest():
1383 mProducerEglSurface(EGL_NO_SURFACE),
1384 mProducerEglContext(EGL_NO_CONTEXT) {
1385 }
1386
1387 virtual void SetUp() {
1388 SurfaceTextureGLTest::SetUp();
1389
Jamie Gennisce561372012-03-19 18:33:05 -07001390 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001391 mANW.get(), NULL);
1392 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1393 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1394
Jamie Gennisce561372012-03-19 18:33:05 -07001395 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001396 EGL_NO_CONTEXT, getContextAttribs());
1397 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1398 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1399 }
1400
1401 virtual void TearDown() {
1402 if (mProducerEglContext != EGL_NO_CONTEXT) {
1403 eglDestroyContext(mEglDisplay, mProducerEglContext);
1404 }
1405 if (mProducerEglSurface != EGL_NO_SURFACE) {
1406 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1407 }
1408 SurfaceTextureGLTest::TearDown();
1409 }
1410
1411 EGLSurface mProducerEglSurface;
1412 EGLContext mProducerEglContext;
1413};
1414
Andy McFadden71f683a2012-09-14 17:21:46 -07001415TEST_F(SurfaceTextureGLToGLTest, TransformHintGetsRespected) {
1416 const uint32_t texWidth = 32;
1417 const uint32_t texHeight = 64;
1418
1419 mST->setDefaultBufferSize(texWidth, texHeight);
1420 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1421
1422 // This test requires 3 buffers to avoid deadlock because we're
1423 // both producer and consumer, and only using one thread.
1424 mST->setDefaultMaxBufferCount(3);
1425
1426 // Do the producer side of things
1427 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1428 mProducerEglSurface, mProducerEglContext));
1429 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1430
1431 // Start a buffer with our chosen size and transform hint moving
1432 // through the system.
1433 glClear(GL_COLOR_BUFFER_BIT); // give the driver something to do
1434 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1435 mST->updateTexImage(); // consume it
1436 // Swap again.
1437 glClear(GL_COLOR_BUFFER_BIT);
1438 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1439 mST->updateTexImage();
1440
1441 // The current buffer should either show the effects of the transform
1442 // hint (in the form of an inverse transform), or show that the
1443 // transform hint has been ignored.
1444 sp<GraphicBuffer> buf = mST->getCurrentBuffer();
1445 if (mST->getCurrentTransform() == NATIVE_WINDOW_TRANSFORM_ROT_270) {
1446 ASSERT_EQ(texWidth, buf->getHeight());
1447 ASSERT_EQ(texHeight, buf->getWidth());
1448 } else {
1449 ASSERT_EQ(texWidth, buf->getWidth());
1450 ASSERT_EQ(texHeight, buf->getHeight());
1451 }
1452
1453 // Reset the transform hint and confirm that it takes.
1454 mST->setTransformHint(0);
1455 glClear(GL_COLOR_BUFFER_BIT);
1456 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1457 mST->updateTexImage();
1458 glClear(GL_COLOR_BUFFER_BIT);
1459 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1460 mST->updateTexImage();
1461
1462 buf = mST->getCurrentBuffer();
1463 ASSERT_EQ((uint32_t) 0, mST->getCurrentTransform());
1464 ASSERT_EQ(texWidth, buf->getWidth());
1465 ASSERT_EQ(texHeight, buf->getHeight());
1466}
1467
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001468TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1469 const int texWidth = 64;
1470 const int texHeight = 64;
1471
1472 mST->setDefaultBufferSize(texWidth, texHeight);
1473
Jamie Gennis46975282012-08-30 18:35:50 -07001474 // This test requires 3 buffers to complete run on a single thread.
1475 mST->setDefaultMaxBufferCount(3);
1476
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001477 // Do the producer side of things
1478 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1479 mProducerEglSurface, mProducerEglContext));
1480 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1481
1482 // This is needed to ensure we pick up a buffer of the correct size.
1483 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1484
1485 glClearColor(0.6, 0.6, 0.6, 0.6);
1486 glClear(GL_COLOR_BUFFER_BIT);
1487
1488 glEnable(GL_SCISSOR_TEST);
1489 glScissor(4, 4, 4, 4);
1490 glClearColor(1.0, 0.0, 0.0, 1.0);
1491 glClear(GL_COLOR_BUFFER_BIT);
1492
1493 glScissor(24, 48, 4, 4);
1494 glClearColor(0.0, 1.0, 0.0, 1.0);
1495 glClear(GL_COLOR_BUFFER_BIT);
1496
1497 glScissor(37, 17, 4, 4);
1498 glClearColor(0.0, 0.0, 1.0, 1.0);
1499 glClear(GL_COLOR_BUFFER_BIT);
1500
1501 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1502
1503 // Do the consumer side of things
1504 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1505 mEglContext));
1506 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1507
1508 glDisable(GL_SCISSOR_TEST);
1509
Jamie Gennisd69097f2012-08-30 13:28:23 -07001510 // Skip the first frame, which was empty
1511 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1512 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001513
1514 glClearColor(0.2, 0.2, 0.2, 0.2);
1515 glClear(GL_COLOR_BUFFER_BIT);
1516
1517 glViewport(0, 0, texWidth, texHeight);
1518 drawTexture();
1519
1520 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1521 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1522 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1523 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1524
1525 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1526 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1527 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1528 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1529 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1530 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1531 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1532 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1533 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1534 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1535 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1536 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1537 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1538 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1539 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1540 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1541}
1542
1543TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001544 sp<GraphicBuffer> buffers[2];
1545
Jamie Gennise3603d72011-11-19 21:20:17 -08001546 // This test requires async mode to run on a single thread.
1547 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1548 mProducerEglSurface, mProducerEglContext));
1549 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1550 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1551 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1552
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001553 for (int i = 0; i < 2; i++) {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001554 // Produce a frame
1555 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1556 mProducerEglSurface, mProducerEglContext));
1557 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1558 glClear(GL_COLOR_BUFFER_BIT);
1559 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1560
1561 // Consume a frame
1562 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1563 mEglContext));
1564 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001565 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001566 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001567 buffers[i] = mST->getCurrentBuffer();
1568 }
1569
1570 // Destroy the GL texture object to release its ref on buffers[2].
1571 GLuint texID = TEX_ID;
1572 glDeleteTextures(1, &texID);
1573
1574 // Destroy the EGLSurface
1575 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1576 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001577 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001578
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001579 // This test should have the only reference to buffer 0.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001580 EXPECT_EQ(1, buffers[0]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001581
Andy McFadden2adaf042012-12-18 09:49:45 -08001582 // The GLConsumer should hold a single reference to buffer 1 in its
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001583 // mCurrentBuffer member. All of the references in the slots should have
1584 // been released.
1585 EXPECT_EQ(2, buffers[1]->getStrongCount());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001586}
1587
1588TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1589 sp<GraphicBuffer> buffers[3];
1590
Jamie Gennise3603d72011-11-19 21:20:17 -08001591 // This test requires async mode to run on a single thread.
1592 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1593 mProducerEglSurface, mProducerEglContext));
1594 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1595 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1596 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1597
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001598 for (int i = 0; i < 3; i++) {
1599 // Produce a frame
1600 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1601 mProducerEglSurface, mProducerEglContext));
1602 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1603 glClear(GL_COLOR_BUFFER_BIT);
1604 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1605 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1606
1607 // Consume a frame
1608 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1609 mEglContext));
1610 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001611 mFW->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001612 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1613 buffers[i] = mST->getCurrentBuffer();
1614 }
1615
Andy McFadden2adaf042012-12-18 09:49:45 -08001616 // Abandon the GLConsumer, releasing the ref that the GLConsumer has
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001617 // on buffers[2].
1618 mST->abandon();
1619
1620 // Destroy the GL texture object to release its ref on buffers[2].
1621 GLuint texID = TEX_ID;
1622 glDeleteTextures(1, &texID);
1623
1624 // Destroy the EGLSurface.
1625 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1626 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001627 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001628
1629 EXPECT_EQ(1, buffers[0]->getStrongCount());
1630 EXPECT_EQ(1, buffers[1]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001631
1632 // Depending on how lazily the GL driver dequeues buffers, we may end up
1633 // with either two or three total buffers. If there are three, make sure
1634 // the last one was properly down-ref'd.
1635 if (buffers[2] != buffers[0]) {
1636 EXPECT_EQ(1, buffers[2]->getStrongCount());
1637 }
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001638}
1639
Jamie Gennis59769462011-11-19 18:04:43 -08001640TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) {
1641 // This test requires 3 buffers to run on a single thread.
Jamie Gennis31a353d2012-08-24 17:25:13 -07001642 mST->setDefaultMaxBufferCount(3);
Jamie Gennis59769462011-11-19 18:04:43 -08001643
1644 ASSERT_TRUE(mST->isSynchronousMode());
1645
1646 for (int i = 0; i < 10; i++) {
1647 // Produce a frame
1648 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1649 mProducerEglSurface, mProducerEglContext));
1650 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1651 glClear(GL_COLOR_BUFFER_BIT);
1652 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1653 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1654
1655 // Consume a frame
1656 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1657 mEglContext));
1658 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1659 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1660 }
1661
1662 ASSERT_TRUE(mST->isSynchronousMode());
1663}
1664
Jamie Gennisc2c38022012-04-11 17:20:03 -07001665TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
1666 enum { texWidth = 64 };
1667 enum { texHeight = 64 };
1668
Jamie Gennis46975282012-08-30 18:35:50 -07001669 // This test requires 3 buffers to complete run on a single thread.
1670 mST->setDefaultMaxBufferCount(3);
1671
Jamie Gennisc2c38022012-04-11 17:20:03 -07001672 // Set the user buffer size.
1673 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1674
1675 // Do the producer side of things
1676 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1677 mProducerEglSurface, mProducerEglContext));
1678 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1679
1680 // This is needed to ensure we pick up a buffer of the correct size.
1681 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1682
1683 glClearColor(0.6, 0.6, 0.6, 0.6);
1684 glClear(GL_COLOR_BUFFER_BIT);
1685
1686 glEnable(GL_SCISSOR_TEST);
1687 glScissor(4, 4, 1, 1);
1688 glClearColor(1.0, 0.0, 0.0, 1.0);
1689 glClear(GL_COLOR_BUFFER_BIT);
1690
1691 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1692
1693 // Do the consumer side of things
1694 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1695 mEglContext));
1696 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1697
1698 glDisable(GL_SCISSOR_TEST);
1699
Jamie Gennisd69097f2012-08-30 13:28:23 -07001700 // Skip the first frame, which was empty
1701 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1702 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001703
1704 glClearColor(0.2, 0.2, 0.2, 0.2);
1705 glClear(GL_COLOR_BUFFER_BIT);
1706
1707 glViewport(0, 0, texWidth, texHeight);
1708 drawTexture();
1709
1710 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1711 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1712 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1713 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1714
1715 EXPECT_TRUE(checkPixel( 4, 4, 255, 0, 0, 255));
1716 EXPECT_TRUE(checkPixel( 5, 5, 153, 153, 153, 153));
1717 EXPECT_TRUE(checkPixel( 3, 3, 153, 153, 153, 153));
1718 EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153));
1719 EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153));
1720}
1721
1722TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
1723 enum { texWidth = 64 };
1724 enum { texHeight = 16 };
1725
Jamie Gennis46975282012-08-30 18:35:50 -07001726 // This test requires 3 buffers to complete run on a single thread.
1727 mST->setDefaultMaxBufferCount(3);
1728
Jamie Gennisc2c38022012-04-11 17:20:03 -07001729 // Set the transform hint.
1730 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1731
1732 // Set the user buffer size.
1733 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1734
1735 // Do the producer side of things
1736 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1737 mProducerEglSurface, mProducerEglContext));
1738 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1739
1740 // This is needed to ensure we pick up a buffer of the correct size and the
1741 // new rotation hint.
1742 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1743
1744 glClearColor(0.6, 0.6, 0.6, 0.6);
1745 glClear(GL_COLOR_BUFFER_BIT);
1746
1747 glEnable(GL_SCISSOR_TEST);
1748 glScissor(24, 4, 1, 1);
1749 glClearColor(1.0, 0.0, 0.0, 1.0);
1750 glClear(GL_COLOR_BUFFER_BIT);
1751
1752 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1753
1754 // Do the consumer side of things
1755 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1756 mEglContext));
1757 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1758
1759 glDisable(GL_SCISSOR_TEST);
1760
Jamie Gennisd69097f2012-08-30 13:28:23 -07001761 // Skip the first frame, which was empty
1762 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1763 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001764
1765 glClearColor(0.2, 0.2, 0.2, 0.2);
1766 glClear(GL_COLOR_BUFFER_BIT);
1767
1768 glViewport(0, 0, texWidth, texHeight);
1769 drawTexture();
1770
1771 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1772 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1773 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1774 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1775
1776 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1777 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1778 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1779 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1780 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1781}
1782
1783TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
1784 enum { texWidth = 64 };
1785 enum { texHeight = 16 };
1786
Jamie Gennis46975282012-08-30 18:35:50 -07001787 // This test requires 3 buffers to complete run on a single thread.
1788 mST->setDefaultMaxBufferCount(3);
1789
Jamie Gennisc2c38022012-04-11 17:20:03 -07001790 // Set the transform hint.
1791 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1792
1793 // Set the default buffer size.
1794 mST->setDefaultBufferSize(texWidth, texHeight);
1795
1796 // Do the producer side of things
1797 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1798 mProducerEglSurface, mProducerEglContext));
1799 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1800
1801 // This is needed to ensure we pick up a buffer of the correct size and the
1802 // new rotation hint.
1803 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1804
1805 glClearColor(0.6, 0.6, 0.6, 0.6);
1806 glClear(GL_COLOR_BUFFER_BIT);
1807
1808 glEnable(GL_SCISSOR_TEST);
1809 glScissor(24, 4, 1, 1);
1810 glClearColor(1.0, 0.0, 0.0, 1.0);
1811 glClear(GL_COLOR_BUFFER_BIT);
1812
1813 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1814
1815 // Do the consumer side of things
1816 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1817 mEglContext));
1818 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1819
1820 glDisable(GL_SCISSOR_TEST);
1821
Jamie Gennisd69097f2012-08-30 13:28:23 -07001822 // Skip the first frame, which was empty
1823 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1824 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001825
1826 glClearColor(0.2, 0.2, 0.2, 0.2);
1827 glClear(GL_COLOR_BUFFER_BIT);
1828
1829 glViewport(0, 0, texWidth, texHeight);
1830 drawTexture();
1831
1832 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1833 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1834 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1835 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1836
1837 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1838 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1839 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1840 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1841 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1842}
1843
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001844/*
1845 * This test fixture is for testing GL -> GL texture streaming from one thread
1846 * to another. It contains functionality to create a producer thread that will
1847 * perform GL rendering to an ANativeWindow that feeds frames to a
Andy McFadden2adaf042012-12-18 09:49:45 -08001848 * GLConsumer. Additionally it supports interlocking the producer and
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001849 * consumer threads so that a specific sequence of calls can be
1850 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07001851 *
1852 * The intended usage is as follows:
1853 *
1854 * TEST_F(...) {
1855 * class PT : public ProducerThread {
1856 * virtual void render() {
1857 * ...
1858 * swapBuffers();
1859 * }
1860 * };
1861 *
1862 * runProducerThread(new PT());
1863 *
1864 * // The order of these calls will vary from test to test and may include
1865 * // multiple frames and additional operations (e.g. GL rendering from the
1866 * // texture).
1867 * fc->waitForFrame();
1868 * mST->updateTexImage();
1869 * fc->finishFrame();
1870 * }
1871 *
1872 */
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001873class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07001874protected:
1875
1876 // ProducerThread is an abstract base class to simplify the creation of
1877 // OpenGL ES frame producer threads.
1878 class ProducerThread : public Thread {
1879 public:
1880 virtual ~ProducerThread() {
1881 }
1882
1883 void setEglObjects(EGLDisplay producerEglDisplay,
1884 EGLSurface producerEglSurface,
1885 EGLContext producerEglContext) {
1886 mProducerEglDisplay = producerEglDisplay;
1887 mProducerEglSurface = producerEglSurface;
1888 mProducerEglContext = producerEglContext;
1889 }
1890
1891 virtual bool threadLoop() {
1892 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
1893 mProducerEglSurface, mProducerEglContext);
1894 render();
1895 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1896 EGL_NO_CONTEXT);
1897 return false;
1898 }
1899
1900 protected:
1901 virtual void render() = 0;
1902
1903 void swapBuffers() {
1904 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
1905 }
1906
1907 EGLDisplay mProducerEglDisplay;
1908 EGLSurface mProducerEglSurface;
1909 EGLContext mProducerEglContext;
1910 };
1911
1912 // FrameCondition is a utility class for interlocking between the producer
1913 // and consumer threads. The FrameCondition object should be created and
1914 // destroyed in the consumer thread only. The consumer thread should set
Andy McFadden2adaf042012-12-18 09:49:45 -08001915 // the FrameCondition as the FrameAvailableListener of the GLConsumer,
Jamie Gennis5451d152011-06-08 09:40:45 -07001916 // and should call both waitForFrame and finishFrame once for each expected
1917 // frame.
1918 //
1919 // This interlocking relies on the fact that onFrameAvailable gets called
Andy McFadden2adaf042012-12-18 09:49:45 -08001920 // synchronously from GLConsumer::queueBuffer.
1921 class FrameCondition : public GLConsumer::FrameAvailableListener {
Jamie Gennis5451d152011-06-08 09:40:45 -07001922 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001923 FrameCondition():
1924 mFrameAvailable(false),
1925 mFrameFinished(false) {
1926 }
1927
Jamie Gennis5451d152011-06-08 09:40:45 -07001928 // waitForFrame waits for the next frame to arrive. This should be
1929 // called from the consumer thread once for every frame expected by the
1930 // test.
1931 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001932 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001933 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001934 while (!mFrameAvailable) {
1935 mFrameAvailableCondition.wait(mMutex);
1936 }
1937 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01001938 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001939 }
1940
1941 // Allow the producer to return from its swapBuffers call and continue
1942 // on to produce the next frame. This should be called by the consumer
1943 // thread once for every frame expected by the test.
1944 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001945 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001946 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001947 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001948 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01001949 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001950 }
1951
Andy McFadden2adaf042012-12-18 09:49:45 -08001952 // This should be called by GLConsumer on the producer thread.
Jamie Gennis5451d152011-06-08 09:40:45 -07001953 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001954 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001955 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001956 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001957 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001958 while (!mFrameFinished) {
1959 mFrameFinishCondition.wait(mMutex);
1960 }
1961 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01001962 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07001963 }
1964
1965 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001966 bool mFrameAvailable;
1967 bool mFrameFinished;
1968
Jamie Gennis5451d152011-06-08 09:40:45 -07001969 Mutex mMutex;
1970 Condition mFrameAvailableCondition;
1971 Condition mFrameFinishCondition;
1972 };
1973
Jamie Gennis5451d152011-06-08 09:40:45 -07001974 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001975 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07001976 mFC = new FrameCondition();
1977 mST->setFrameAvailableListener(mFC);
1978 }
1979
1980 virtual void TearDown() {
1981 if (mProducerThread != NULL) {
1982 mProducerThread->requestExitAndWait();
1983 }
Jamie Gennis5451d152011-06-08 09:40:45 -07001984 mProducerThread.clear();
1985 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001986 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07001987 }
1988
1989 void runProducerThread(const sp<ProducerThread> producerThread) {
1990 ASSERT_TRUE(mProducerThread == NULL);
1991 mProducerThread = producerThread;
1992 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
1993 mProducerEglContext);
1994 producerThread->run();
1995 }
1996
Jamie Gennis5451d152011-06-08 09:40:45 -07001997 sp<ProducerThread> mProducerThread;
1998 sp<FrameCondition> mFC;
1999};
2000
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002001TEST_F(SurfaceTextureGLThreadToGLTest,
2002 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002003 class PT : public ProducerThread {
2004 virtual void render() {
2005 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2006 glClear(GL_COLOR_BUFFER_BIT);
2007 swapBuffers();
2008 }
2009 };
2010
2011 runProducerThread(new PT());
2012
2013 mFC->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07002014 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07002015 mFC->finishFrame();
2016
2017 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08002018}
Jamie Gennis5451d152011-06-08 09:40:45 -07002019
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002020TEST_F(SurfaceTextureGLThreadToGLTest,
2021 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002022 class PT : public ProducerThread {
2023 virtual void render() {
2024 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2025 glClear(GL_COLOR_BUFFER_BIT);
2026 swapBuffers();
2027 }
2028 };
2029
2030 runProducerThread(new PT());
2031
2032 mFC->waitForFrame();
2033 mFC->finishFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07002034 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07002035
2036 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2037}
2038
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002039TEST_F(SurfaceTextureGLThreadToGLTest,
2040 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002041 enum { NUM_ITERATIONS = 1024 };
2042
2043 class PT : public ProducerThread {
2044 virtual void render() {
2045 for (int i = 0; i < NUM_ITERATIONS; i++) {
2046 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2047 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002048 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002049 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002050 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002051 }
2052 }
2053 };
2054
2055 runProducerThread(new PT());
2056
2057 for (int i = 0; i < NUM_ITERATIONS; i++) {
2058 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01002059 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002060 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002061 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002062 mFC->finishFrame();
2063
2064 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2065 }
2066}
2067
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002068TEST_F(SurfaceTextureGLThreadToGLTest,
2069 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002070 enum { NUM_ITERATIONS = 1024 };
2071
2072 class PT : public ProducerThread {
2073 virtual void render() {
2074 for (int i = 0; i < NUM_ITERATIONS; i++) {
2075 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2076 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002077 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002078 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002079 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002080 }
2081 }
2082 };
2083
2084 runProducerThread(new PT());
2085
2086 for (int i = 0; i < NUM_ITERATIONS; i++) {
2087 mFC->waitForFrame();
2088 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002089 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002090 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002091 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002092
2093 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2094 }
2095}
2096
Jamie Gennis6e502192011-07-21 14:31:31 -07002097// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002098TEST_F(SurfaceTextureGLThreadToGLTest,
2099 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07002100 enum { NUM_ITERATIONS = 64 };
2101
2102 class PT : public ProducerThread {
2103 virtual void render() {
2104 for (int i = 0; i < NUM_ITERATIONS; i++) {
2105 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2106 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002107 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002108 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002109 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002110 }
2111 }
2112 };
2113
2114 ASSERT_EQ(OK, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -07002115 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis6e502192011-07-21 14:31:31 -07002116
2117 runProducerThread(new PT());
2118
2119 // Allow three frames to be rendered and queued before starting the
2120 // rendering in this thread. For the latter two frames we don't call
2121 // updateTexImage so the next dequeue from the producer thread will block
2122 // waiting for a frame to become available.
2123 mFC->waitForFrame();
2124 mFC->finishFrame();
2125
2126 // We must call updateTexImage to consume the first frame so that the
2127 // SurfaceTexture is able to reduce the buffer count to 2. This is because
2128 // the GL driver may dequeue a buffer when the EGLSurface is created, and
Jamie Gennis31a353d2012-08-24 17:25:13 -07002129 // that happens before we call setDefaultMaxBufferCount. It's possible that the
Jamie Gennis6e502192011-07-21 14:31:31 -07002130 // driver does not dequeue a buffer at EGLSurface creation time, so we
2131 // cannot rely on this to cause the second dequeueBuffer call to block.
Jamie Gennisd69097f2012-08-30 13:28:23 -07002132 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07002133
2134 mFC->waitForFrame();
2135 mFC->finishFrame();
2136 mFC->waitForFrame();
2137 mFC->finishFrame();
2138
2139 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
2140 // block waiting for a buffer to become available.
2141 usleep(100000);
2142
2143 // Render and present a number of images. This thread should not be blocked
2144 // by the fact that the producer thread is blocking in dequeue.
2145 for (int i = 0; i < NUM_ITERATIONS; i++) {
2146 glClear(GL_COLOR_BUFFER_BIT);
2147 eglSwapBuffers(mEglDisplay, mEglSurface);
2148 }
2149
2150 // Consume the two pending buffers to unblock the producer thread.
Jamie Gennisd69097f2012-08-30 13:28:23 -07002151 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2152 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07002153
2154 // Consume the remaining buffers from the producer thread.
2155 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
2156 mFC->waitForFrame();
2157 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002158 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07002159 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01002160 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002161 }
2162}
2163
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002164class SurfaceTextureFBOTest : public SurfaceTextureGLTest {
2165protected:
2166
2167 virtual void SetUp() {
2168 SurfaceTextureGLTest::SetUp();
2169
2170 glGenFramebuffers(1, &mFbo);
2171 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2172
2173 glGenTextures(1, &mFboTex);
2174 glBindTexture(GL_TEXTURE_2D, mFboTex);
2175 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getSurfaceWidth(),
2176 getSurfaceHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2177 glBindTexture(GL_TEXTURE_2D, 0);
2178 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2179
2180 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2181 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2182 GL_TEXTURE_2D, mFboTex, 0);
2183 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2184 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2185 }
2186
2187 virtual void TearDown() {
2188 SurfaceTextureGLTest::TearDown();
2189
2190 glDeleteTextures(1, &mFboTex);
2191 glDeleteFramebuffers(1, &mFbo);
2192 }
2193
2194 GLuint mFbo;
2195 GLuint mFboTex;
2196};
2197
2198// This test is intended to verify that proper synchronization is done when
2199// rendering into an FBO.
2200TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) {
2201 const int texWidth = 64;
2202 const int texHeight = 64;
2203
2204 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
2205 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
2206 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
2207 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
2208
2209 android_native_buffer_t* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002210 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2211 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002212 ASSERT_TRUE(anb != NULL);
2213
2214 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002215
2216 // Fill the buffer with green
2217 uint8_t* img = NULL;
2218 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
2219 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255,
2220 0, 255);
2221 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002222 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
2223 -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002224
2225 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2226
2227 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2228 drawTexture();
2229 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2230
2231 for (int i = 0; i < 4; i++) {
2232 SCOPED_TRACE(String8::format("frame %d", i).string());
2233
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002234 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2235 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002236 ASSERT_TRUE(anb != NULL);
2237
2238 buf = new GraphicBuffer(anb, false);
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002239
2240 // Fill the buffer with red
2241 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
2242 (void**)(&img)));
2243 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0,
2244 0, 255);
2245 ASSERT_EQ(NO_ERROR, buf->unlock());
2246 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002247 buf->getNativeBuffer(), -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002248
2249 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2250
2251 drawTexture();
2252
2253 EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255));
2254 }
2255
2256 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2257
2258 EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255));
2259}
2260
Jamie Gennisce561372012-03-19 18:33:05 -07002261class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest {
2262protected:
Jamie Gennis74bed552012-03-28 19:05:54 -07002263 enum { SECOND_TEX_ID = 123 };
2264 enum { THIRD_TEX_ID = 456 };
2265
Jamie Gennisce561372012-03-19 18:33:05 -07002266 SurfaceTextureMultiContextGLTest():
2267 mSecondEglContext(EGL_NO_CONTEXT) {
2268 }
2269
2270 virtual void SetUp() {
2271 SurfaceTextureGLTest::SetUp();
2272
Jamie Gennis74bed552012-03-28 19:05:54 -07002273 // Set up the secondary context and texture renderer.
Jamie Gennisce561372012-03-19 18:33:05 -07002274 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2275 EGL_NO_CONTEXT, getContextAttribs());
2276 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2277 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext);
Jamie Gennis74bed552012-03-28 19:05:54 -07002278
2279 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2280 mSecondEglContext));
2281 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2282 mSecondTextureRenderer = new TextureRenderer(SECOND_TEX_ID, mST);
2283 ASSERT_NO_FATAL_FAILURE(mSecondTextureRenderer->SetUp());
2284
2285 // Set up the tertiary context and texture renderer.
2286 mThirdEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2287 EGL_NO_CONTEXT, getContextAttribs());
2288 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2289 ASSERT_NE(EGL_NO_CONTEXT, mThirdEglContext);
2290
2291 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2292 mThirdEglContext));
2293 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2294 mThirdTextureRenderer = new TextureRenderer(THIRD_TEX_ID, mST);
2295 ASSERT_NO_FATAL_FAILURE(mThirdTextureRenderer->SetUp());
2296
2297 // Switch back to the primary context to start the tests.
2298 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2299 mEglContext));
Jamie Gennisce561372012-03-19 18:33:05 -07002300 }
2301
2302 virtual void TearDown() {
Jamie Gennis74bed552012-03-28 19:05:54 -07002303 if (mThirdEglContext != EGL_NO_CONTEXT) {
2304 eglDestroyContext(mEglDisplay, mThirdEglContext);
2305 }
Jamie Gennisce561372012-03-19 18:33:05 -07002306 if (mSecondEglContext != EGL_NO_CONTEXT) {
2307 eglDestroyContext(mEglDisplay, mSecondEglContext);
2308 }
2309 SurfaceTextureGLTest::TearDown();
2310 }
2311
2312 EGLContext mSecondEglContext;
Jamie Gennis74bed552012-03-28 19:05:54 -07002313 sp<TextureRenderer> mSecondTextureRenderer;
2314
2315 EGLContext mThirdEglContext;
2316 sp<TextureRenderer> mThirdTextureRenderer;
Jamie Gennisce561372012-03-19 18:33:05 -07002317};
2318
2319TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) {
Jamie Gennisce561372012-03-19 18:33:05 -07002320 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2321
2322 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002323 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002324 ASSERT_EQ(OK, mST->updateTexImage());
Jamie Gennisce561372012-03-19 18:33:05 -07002325
2326 // Attempt to latch the texture on the secondary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07002327 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
Jamie Gennisce561372012-03-19 18:33:05 -07002328 mSecondEglContext));
2329 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -07002330 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2331}
2332
2333TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002334 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2335
2336 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002337 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002338 ASSERT_EQ(OK, mST->updateTexImage());
2339
2340 // Detach from the primary context.
2341 ASSERT_EQ(OK, mST->detachFromContext());
2342
2343 // Check that the GL texture was deleted.
2344 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2345}
2346
2347TEST_F(SurfaceTextureMultiContextGLTest,
2348 DetachFromContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002349 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2350
2351 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002352 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002353 ASSERT_EQ(OK, mST->updateTexImage());
2354
2355 // Detach from the primary context.
2356 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2357 ASSERT_EQ(OK, mST->detachFromContext());
2358
2359 // Check that the GL texture was deleted.
2360 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2361}
2362
2363TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002364 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2365
2366 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002367 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002368 ASSERT_EQ(OK, mST->updateTexImage());
2369
2370 // Attempt to detach from the primary context.
2371 mST->abandon();
2372 ASSERT_EQ(NO_INIT, mST->detachFromContext());
2373}
2374
2375TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002376 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2377
2378 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002379 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002380 ASSERT_EQ(OK, mST->updateTexImage());
2381
2382 // Detach from the primary context.
2383 ASSERT_EQ(OK, mST->detachFromContext());
2384
2385 // Attempt to detach from the primary context again.
2386 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2387}
2388
2389TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002390 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2391
2392 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002393 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002394 ASSERT_EQ(OK, mST->updateTexImage());
2395
2396 // Make there be no current display.
2397 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2398 EGL_NO_CONTEXT));
2399 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2400
2401 // Attempt to detach from the primary context.
2402 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2403}
2404
2405TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002406 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2407
2408 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002409 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002410 ASSERT_EQ(OK, mST->updateTexImage());
2411
2412 // Make current context be incorrect.
2413 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2414 mSecondEglContext));
2415 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2416
2417 // Attempt to detach from the primary context.
2418 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2419}
2420
2421TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002422 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2423
2424 // Detach from the primary context.
2425 ASSERT_EQ(OK, mST->detachFromContext());
2426
2427 // Attempt to latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002428 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002429 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2430}
2431
2432TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002433 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2434
2435 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002436 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002437 ASSERT_EQ(OK, mST->updateTexImage());
2438
2439 // Detach from the primary context.
2440 ASSERT_EQ(OK, mST->detachFromContext());
2441
2442 // Attach to the secondary context.
2443 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2444 mSecondEglContext));
2445 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2446
2447 // Verify that the texture object was created and bound.
2448 GLint texBinding = -1;
2449 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2450 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2451
2452 // Try to use the texture from the secondary context.
2453 glClearColor(0.2, 0.2, 0.2, 0.2);
2454 glClear(GL_COLOR_BUFFER_BIT);
2455 glViewport(0, 0, 1, 1);
2456 mSecondTextureRenderer->drawTexture();
2457 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2458 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2459}
2460
2461TEST_F(SurfaceTextureMultiContextGLTest,
2462 AttachToContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002463 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2464
2465 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002466 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002467 ASSERT_EQ(OK, mST->updateTexImage());
2468
2469 // Detach from the primary context.
2470 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2471 ASSERT_EQ(OK, mST->detachFromContext());
2472
2473 // Attach to the secondary context.
2474 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2475 mSecondEglContext));
2476 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2477
2478 // Verify that the texture object was created and bound.
2479 GLint texBinding = -1;
2480 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2481 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2482
2483 // Try to use the texture from the secondary context.
2484 glClearColor(0.2, 0.2, 0.2, 0.2);
2485 glClear(GL_COLOR_BUFFER_BIT);
2486 glViewport(0, 0, 1, 1);
2487 mSecondTextureRenderer->drawTexture();
2488 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2489 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2490}
2491
2492TEST_F(SurfaceTextureMultiContextGLTest,
2493 AttachToContextSucceedsBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002494 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2495
2496 // Detach from the primary context.
2497 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2498 ASSERT_EQ(OK, mST->detachFromContext());
2499
2500 // Attach to the secondary context.
2501 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2502 mSecondEglContext));
2503 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2504
2505 // Verify that the texture object was created and bound.
2506 GLint texBinding = -1;
2507 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2508 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2509
2510 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002511 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002512 ASSERT_EQ(OK, mST->updateTexImage());
2513
2514 // Try to use the texture from the secondary context.
2515 glClearColor(0.2, 0.2, 0.2, 0.2);
2516 glClear(GL_COLOR_BUFFER_BIT);
2517 glViewport(0, 0, 1, 1);
2518 mSecondTextureRenderer->drawTexture();
2519 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2520 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2521}
2522
2523TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002524 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2525
2526 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002527 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002528 ASSERT_EQ(OK, mST->updateTexImage());
2529
2530 // Detach from the primary context.
2531 ASSERT_EQ(OK, mST->detachFromContext());
2532
2533 // Attempt to attach to the secondary context.
2534 mST->abandon();
2535
2536 // Attempt to attach to the primary context.
2537 ASSERT_EQ(NO_INIT, mST->attachToContext(SECOND_TEX_ID));
2538}
2539
2540TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002541 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2542
2543 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002544 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002545 ASSERT_EQ(OK, mST->updateTexImage());
2546
2547 // Attempt to attach to the primary context.
2548 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2549}
2550
2551TEST_F(SurfaceTextureMultiContextGLTest,
2552 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002553 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2554
2555 // Attempt to attach to the primary context.
2556 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2557}
2558
2559TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002560 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2561
2562 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002563 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002564 ASSERT_EQ(OK, mST->updateTexImage());
2565
2566 // Detach from the primary context.
2567 ASSERT_EQ(OK, mST->detachFromContext());
2568
2569 // Make there be no current display.
2570 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2571 EGL_NO_CONTEXT));
2572 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2573
2574 // Attempt to attach with no context current.
2575 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2576}
2577
2578TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002579 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2580
2581 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002582 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002583 ASSERT_EQ(OK, mST->updateTexImage());
2584
2585 // Detach from the primary context.
2586 ASSERT_EQ(OK, mST->detachFromContext());
2587
2588 // Attach to the secondary context.
2589 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2590 mSecondEglContext));
2591 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2592
2593 // Detach from the secondary context.
2594 ASSERT_EQ(OK, mST->detachFromContext());
2595
2596 // Attach to the tertiary context.
2597 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2598 mThirdEglContext));
2599 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2600
2601 // Verify that the texture object was created and bound.
2602 GLint texBinding = -1;
2603 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2604 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2605
2606 // Try to use the texture from the tertiary context.
2607 glClearColor(0.2, 0.2, 0.2, 0.2);
2608 glClear(GL_COLOR_BUFFER_BIT);
2609 glViewport(0, 0, 1, 1);
2610 mThirdTextureRenderer->drawTexture();
2611 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2612 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2613}
2614
2615TEST_F(SurfaceTextureMultiContextGLTest,
2616 AttachToContextSucceedsTwiceBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002617 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2618
2619 // Detach from the primary context.
2620 ASSERT_EQ(OK, mST->detachFromContext());
2621
2622 // Attach to the secondary context.
2623 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2624 mSecondEglContext));
2625 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2626
2627 // Detach from the secondary context.
2628 ASSERT_EQ(OK, mST->detachFromContext());
2629
2630 // Attach to the tertiary context.
2631 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2632 mThirdEglContext));
2633 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2634
2635 // Verify that the texture object was created and bound.
2636 GLint texBinding = -1;
2637 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2638 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2639
2640 // Latch the texture contents on the tertiary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002641 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002642 ASSERT_EQ(OK, mST->updateTexImage());
2643
2644 // Try to use the texture from the tertiary context.
2645 glClearColor(0.2, 0.2, 0.2, 0.2);
2646 glClear(GL_COLOR_BUFFER_BIT);
2647 glViewport(0, 0, 1, 1);
2648 mThirdTextureRenderer->drawTexture();
2649 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2650 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
Jamie Gennisce561372012-03-19 18:33:05 -07002651}
2652
Jesse Hall90ed8502012-05-16 23:44:34 -07002653TEST_F(SurfaceTextureMultiContextGLTest,
2654 UpdateTexImageSucceedsForBufferConsumedBeforeDetach) {
2655 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
Jamie Gennis31a353d2012-08-24 17:25:13 -07002656 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2));
Jesse Hall90ed8502012-05-16 23:44:34 -07002657
2658 // produce two frames and consume them both on the primary context
2659 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2660 mFW->waitForFrame();
2661 ASSERT_EQ(OK, mST->updateTexImage());
2662
2663 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2664 mFW->waitForFrame();
2665 ASSERT_EQ(OK, mST->updateTexImage());
2666
2667 // produce one more frame
2668 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2669
2670 // Detach from the primary context and attach to the secondary context
2671 ASSERT_EQ(OK, mST->detachFromContext());
2672 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2673 mSecondEglContext));
2674 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2675
2676 // Consume final frame on secondary context
2677 mFW->waitForFrame();
2678 ASSERT_EQ(OK, mST->updateTexImage());
2679}
2680
Jamie Gennis5451d152011-06-08 09:40:45 -07002681} // namespace android