blob: 078c17b7a9a2d0c935daee9a55a616305579f44c [file] [log] [blame]
Jamie Gennisd99c0882011-03-10 16:24:46 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis2640bfd2011-07-14 17:11:47 -070017#define LOG_TAG "SurfaceTexture_test"
Jamie Gennis5451d152011-06-08 09:40:45 -070018//#define LOG_NDEBUG 0
19
Jamie Gennisd99c0882011-03-10 16:24:46 -080020#include <gtest/gtest.h>
21#include <gui/SurfaceTexture.h>
22#include <gui/SurfaceTextureClient.h>
23#include <ui/GraphicBuffer.h>
24#include <utils/String8.h>
Jamie Gennis5451d152011-06-08 09:40:45 -070025#include <utils/threads.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080026
Mathias Agopian90ac7992012-02-25 18:48:35 -080027#include <gui/ISurfaceComposer.h>
28#include <gui/Surface.h>
29#include <gui/SurfaceComposerClient.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080030
31#include <EGL/egl.h>
32#include <EGL/eglext.h>
33#include <GLES2/gl2.h>
34#include <GLES2/gl2ext.h>
35
36#include <ui/FramebufferNativeWindow.h>
37
38namespace android {
39
40class GLTest : public ::testing::Test {
41protected:
42
43 GLTest():
44 mEglDisplay(EGL_NO_DISPLAY),
45 mEglSurface(EGL_NO_SURFACE),
46 mEglContext(EGL_NO_CONTEXT) {
47 }
48
49 virtual void SetUp() {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070050 const ::testing::TestInfo* const testInfo =
51 ::testing::UnitTest::GetInstance()->current_test_info();
52 ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
53 testInfo->name());
54
Jamie Gennisd99c0882011-03-10 16:24:46 -080055 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
56 ASSERT_EQ(EGL_SUCCESS, eglGetError());
57 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
58
59 EGLint majorVersion;
60 EGLint minorVersion;
61 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
62 ASSERT_EQ(EGL_SUCCESS, eglGetError());
63 RecordProperty("EglVersionMajor", majorVersion);
64 RecordProperty("EglVersionMajor", minorVersion);
65
Jamie Gennisd99c0882011-03-10 16:24:46 -080066 EGLint numConfigs = 0;
Jamie Gennis1876d132011-03-17 16:32:52 -070067 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080068 1, &numConfigs));
69 ASSERT_EQ(EGL_SUCCESS, eglGetError());
70
71 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
72 if (displaySecsEnv != NULL) {
73 mDisplaySecs = atoi(displaySecsEnv);
74 if (mDisplaySecs < 0) {
75 mDisplaySecs = 0;
76 }
77 } else {
78 mDisplaySecs = 0;
79 }
80
81 if (mDisplaySecs > 0) {
82 mComposerClient = new SurfaceComposerClient;
83 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
84
Jamie Gennisfc850122011-04-25 16:40:05 -070085 mSurfaceControl = mComposerClient->createSurface(
Jamie Gennisd99c0882011-03-10 16:24:46 -080086 String8("Test Surface"), 0,
87 getSurfaceWidth(), getSurfaceHeight(),
88 PIXEL_FORMAT_RGB_888, 0);
89
90 ASSERT_TRUE(mSurfaceControl != NULL);
91 ASSERT_TRUE(mSurfaceControl->isValid());
92
Mathias Agopian698c0872011-06-28 19:09:31 -070093 SurfaceComposerClient::openGlobalTransaction();
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070094 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080095 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070096 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennisd99c0882011-03-10 16:24:46 -080097
98 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -070099 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800100 window.get(), NULL);
101 } else {
102 EGLint pbufferAttribs[] = {
103 EGL_WIDTH, getSurfaceWidth(),
104 EGL_HEIGHT, getSurfaceHeight(),
105 EGL_NONE };
106
Jamie Gennis1876d132011-03-17 16:32:52 -0700107 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800108 pbufferAttribs);
109 }
110 ASSERT_EQ(EGL_SUCCESS, eglGetError());
111 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
112
Jamie Gennis1876d132011-03-17 16:32:52 -0700113 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800114 getContextAttribs());
115 ASSERT_EQ(EGL_SUCCESS, eglGetError());
116 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
117
118 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
119 mEglContext));
120 ASSERT_EQ(EGL_SUCCESS, eglGetError());
121
122 EGLint w, h;
123 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
124 ASSERT_EQ(EGL_SUCCESS, eglGetError());
125 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
126 ASSERT_EQ(EGL_SUCCESS, eglGetError());
127 RecordProperty("EglSurfaceWidth", w);
128 RecordProperty("EglSurfaceHeight", h);
129
130 glViewport(0, 0, w, h);
131 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
132 }
133
134 virtual void TearDown() {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700135 // Display the result
Jamie Gennisd99c0882011-03-10 16:24:46 -0800136 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
137 eglSwapBuffers(mEglDisplay, mEglSurface);
138 sleep(mDisplaySecs);
139 }
140
141 if (mComposerClient != NULL) {
142 mComposerClient->dispose();
143 }
144 if (mEglContext != EGL_NO_CONTEXT) {
145 eglDestroyContext(mEglDisplay, mEglContext);
146 }
147 if (mEglSurface != EGL_NO_SURFACE) {
148 eglDestroySurface(mEglDisplay, mEglSurface);
149 }
150 if (mEglDisplay != EGL_NO_DISPLAY) {
151 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
152 EGL_NO_CONTEXT);
153 eglTerminate(mEglDisplay);
154 }
155 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700156
157 const ::testing::TestInfo* const testInfo =
158 ::testing::UnitTest::GetInstance()->current_test_info();
159 ALOGV("End test: %s.%s", testInfo->test_case_name(),
160 testInfo->name());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800161 }
162
163 virtual EGLint const* getConfigAttribs() {
164 static EGLint sDefaultConfigAttribs[] = {
165 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
166 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
167 EGL_RED_SIZE, 8,
168 EGL_GREEN_SIZE, 8,
169 EGL_BLUE_SIZE, 8,
170 EGL_ALPHA_SIZE, 8,
171 EGL_DEPTH_SIZE, 16,
172 EGL_STENCIL_SIZE, 8,
173 EGL_NONE };
174
175 return sDefaultConfigAttribs;
176 }
177
178 virtual EGLint const* getContextAttribs() {
179 static EGLint sDefaultContextAttribs[] = {
180 EGL_CONTEXT_CLIENT_VERSION, 2,
181 EGL_NONE };
182
183 return sDefaultContextAttribs;
184 }
185
186 virtual EGLint getSurfaceWidth() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700187 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800188 }
189
190 virtual EGLint getSurfaceHeight() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700191 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800192 }
193
Jamie Gennisd99c0882011-03-10 16:24:46 -0800194 ::testing::AssertionResult checkPixel(int x, int y, int r,
Jamie Gennis824efa72011-06-13 13:41:01 -0700195 int g, int b, int a, int tolerance=2) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800196 GLubyte pixel[4];
197 String8 msg;
198 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
199 GLenum err = glGetError();
200 if (err != GL_NO_ERROR) {
201 msg += String8::format("error reading pixel: %#x", err);
202 while ((err = glGetError()) != GL_NO_ERROR) {
203 msg += String8::format(", %#x", err);
204 }
205 fprintf(stderr, "pixel check failure: %s\n", msg.string());
206 return ::testing::AssertionFailure(
207 ::testing::Message(msg.string()));
208 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700209 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800210 msg += String8::format("r(%d isn't %d)", pixel[0], r);
211 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700212 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800213 if (!msg.isEmpty()) {
214 msg += " ";
215 }
216 msg += String8::format("g(%d isn't %d)", pixel[1], g);
217 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700218 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800219 if (!msg.isEmpty()) {
220 msg += " ";
221 }
222 msg += String8::format("b(%d isn't %d)", pixel[2], b);
223 }
Jamie Gennis824efa72011-06-13 13:41:01 -0700224 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800225 if (!msg.isEmpty()) {
226 msg += " ";
227 }
228 msg += String8::format("a(%d isn't %d)", pixel[3], a);
229 }
230 if (!msg.isEmpty()) {
231 fprintf(stderr, "pixel check failure: %s\n", msg.string());
232 return ::testing::AssertionFailure(
233 ::testing::Message(msg.string()));
234 } else {
235 return ::testing::AssertionSuccess();
236 }
237 }
238
Daniel Lam016c8cb2012-04-03 15:54:58 -0700239 ::testing::AssertionResult assertRectEq(const Rect &r1,
240 const Rect &r2, int tolerance=1) {
241
242 String8 msg;
243
244 if (abs(r1.left - r2.left) > tolerance) {
245 msg += String8::format("left(%d isn't %d)", r1.left, r2.left);
246 }
247 if (abs(r1.top - r2.top) > tolerance) {
248 if (!msg.isEmpty()) {
249 msg += " ";
250 }
251 msg += String8::format("top(%d isn't %d)", r1.top, r2.top);
252 }
253 if (abs(r1.right - r2.right) > tolerance) {
254 if (!msg.isEmpty()) {
255 msg += " ";
256 }
257 msg += String8::format("right(%d isn't %d)", r1.right, r2.right);
258 }
259 if (abs(r1.bottom - r2.bottom) > tolerance) {
260 if (!msg.isEmpty()) {
261 msg += " ";
262 }
263 msg += String8::format("bottom(%d isn't %d)", r1.bottom, r2.bottom);
264 }
265 if (!msg.isEmpty()) {
266 msg += String8::format(" R1: [%d %d %d %d] R2: [%d %d %d %d]",
267 r1.left, r1.top, r1.right, r1.bottom,
268 r2.left, r2.top, r2.right, r2.bottom);
269 fprintf(stderr, "assertRectEq: %s\n", msg.string());
270 return ::testing::AssertionFailure(
271 ::testing::Message(msg.string()));
272 } else {
273 return ::testing::AssertionSuccess();
274 }
275 }
276
Jamie Gennisd99c0882011-03-10 16:24:46 -0800277 int mDisplaySecs;
278 sp<SurfaceComposerClient> mComposerClient;
279 sp<SurfaceControl> mSurfaceControl;
280
281 EGLDisplay mEglDisplay;
282 EGLSurface mEglSurface;
283 EGLContext mEglContext;
Jamie Gennis1876d132011-03-17 16:32:52 -0700284 EGLConfig mGlConfig;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800285};
286
Jamie Gennis74bed552012-03-28 19:05:54 -0700287static void loadShader(GLenum shaderType, const char* pSource,
288 GLuint* outShader) {
289 GLuint shader = glCreateShader(shaderType);
290 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
291 if (shader) {
292 glShaderSource(shader, 1, &pSource, NULL);
293 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
294 glCompileShader(shader);
295 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
296 GLint compiled = 0;
297 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
298 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
299 if (!compiled) {
300 GLint infoLen = 0;
301 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
302 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
303 if (infoLen) {
304 char* buf = (char*) malloc(infoLen);
305 if (buf) {
306 glGetShaderInfoLog(shader, infoLen, NULL, buf);
307 printf("Shader compile log:\n%s\n", buf);
308 free(buf);
309 FAIL();
310 }
311 } else {
312 char* buf = (char*) malloc(0x1000);
313 if (buf) {
314 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
315 printf("Shader compile log:\n%s\n", buf);
316 free(buf);
317 FAIL();
318 }
319 }
320 glDeleteShader(shader);
321 shader = 0;
322 }
323 }
324 ASSERT_TRUE(shader != 0);
325 *outShader = shader;
326}
327
328static void createProgram(const char* pVertexSource,
329 const char* pFragmentSource, GLuint* outPgm) {
330 GLuint vertexShader, fragmentShader;
331 {
332 SCOPED_TRACE("compiling vertex shader");
333 ASSERT_NO_FATAL_FAILURE(loadShader(GL_VERTEX_SHADER, pVertexSource,
334 &vertexShader));
335 }
336 {
337 SCOPED_TRACE("compiling fragment shader");
338 ASSERT_NO_FATAL_FAILURE(loadShader(GL_FRAGMENT_SHADER, pFragmentSource,
339 &fragmentShader));
340 }
341
342 GLuint program = glCreateProgram();
343 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
344 if (program) {
345 glAttachShader(program, vertexShader);
346 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
347 glAttachShader(program, fragmentShader);
348 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
349 glLinkProgram(program);
350 GLint linkStatus = GL_FALSE;
351 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
352 if (linkStatus != GL_TRUE) {
353 GLint bufLength = 0;
354 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
355 if (bufLength) {
356 char* buf = (char*) malloc(bufLength);
357 if (buf) {
358 glGetProgramInfoLog(program, bufLength, NULL, buf);
359 printf("Program link log:\n%s\n", buf);
360 free(buf);
361 FAIL();
362 }
363 }
364 glDeleteProgram(program);
365 program = 0;
366 }
367 }
368 glDeleteShader(vertexShader);
369 glDeleteShader(fragmentShader);
370 ASSERT_TRUE(program != 0);
371 *outPgm = program;
372}
373
374static int abs(int value) {
375 return value > 0 ? value : -value;
376}
377
378
Jamie Gennisd99c0882011-03-10 16:24:46 -0800379// XXX: Code above this point should live elsewhere
380
381class SurfaceTextureGLTest : public GLTest {
382protected:
Jamie Gennis79e31252011-10-19 15:19:19 -0700383 enum { TEX_ID = 123 };
Jamie Gennisd99c0882011-03-10 16:24:46 -0800384
385 virtual void SetUp() {
386 GLTest::SetUp();
387 mST = new SurfaceTexture(TEX_ID);
388 mSTC = new SurfaceTextureClient(mST);
389 mANW = mSTC;
Jamie Gennis74bed552012-03-28 19:05:54 -0700390 mTextureRenderer = new TextureRenderer(TEX_ID, mST);
391 ASSERT_NO_FATAL_FAILURE(mTextureRenderer->SetUp());
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700392 mFW = new FrameWaiter;
393 mST->setFrameAvailableListener(mFW);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800394 }
395
Jamie Gennis2640bfd2011-07-14 17:11:47 -0700396 virtual void TearDown() {
397 mANW.clear();
398 mSTC.clear();
399 mST.clear();
400 GLTest::TearDown();
401 }
402
Jamie Gennisd99c0882011-03-10 16:24:46 -0800403 void drawTexture() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700404 mTextureRenderer->drawTexture();
Jamie Gennisd99c0882011-03-10 16:24:46 -0800405 }
406
Jamie Gennis74bed552012-03-28 19:05:54 -0700407 class TextureRenderer: public RefBase {
408 public:
409 TextureRenderer(GLuint texName, const sp<SurfaceTexture>& st):
410 mTexName(texName),
411 mST(st) {
412 }
413
414 void SetUp() {
415 const char vsrc[] =
416 "attribute vec4 vPosition;\n"
417 "varying vec2 texCoords;\n"
418 "uniform mat4 texMatrix;\n"
419 "void main() {\n"
420 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
421 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
422 " gl_Position = vPosition;\n"
423 "}\n";
424
425 const char fsrc[] =
426 "#extension GL_OES_EGL_image_external : require\n"
427 "precision mediump float;\n"
428 "uniform samplerExternalOES texSampler;\n"
429 "varying vec2 texCoords;\n"
430 "void main() {\n"
431 " gl_FragColor = texture2D(texSampler, texCoords);\n"
432 "}\n";
433
434 {
435 SCOPED_TRACE("creating shader program");
436 ASSERT_NO_FATAL_FAILURE(createProgram(vsrc, fsrc, &mPgm));
437 }
438
439 mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
440 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
441 ASSERT_NE(-1, mPositionHandle);
442 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
443 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
444 ASSERT_NE(-1, mTexSamplerHandle);
445 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
446 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
447 ASSERT_NE(-1, mTexMatrixHandle);
448 }
449
450 // drawTexture draws the SurfaceTexture over the entire GL viewport.
451 void drawTexture() {
Jamie Gennisa96b6bd2012-04-11 17:27:12 -0700452 static const GLfloat triangleVertices[] = {
Jamie Gennis74bed552012-03-28 19:05:54 -0700453 -1.0f, 1.0f,
454 -1.0f, -1.0f,
455 1.0f, -1.0f,
456 1.0f, 1.0f,
457 };
458
459 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
460 triangleVertices);
461 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
462 glEnableVertexAttribArray(mPositionHandle);
463 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
464
465 glUseProgram(mPgm);
466 glUniform1i(mTexSamplerHandle, 0);
467 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
468 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
469 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
470
471 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
472 // they're setting the defautls for that target, but when hacking
473 // things to use GL_TEXTURE_2D they are needed to achieve the same
474 // behavior.
475 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
476 GL_LINEAR);
477 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
478 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
479 GL_LINEAR);
480 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
481 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
482 GL_CLAMP_TO_EDGE);
483 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
484 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
485 GL_CLAMP_TO_EDGE);
486 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
487
488 GLfloat texMatrix[16];
489 mST->getTransformMatrix(texMatrix);
490 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
491
492 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
493 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
494 }
495
496 GLuint mTexName;
497 sp<SurfaceTexture> mST;
498 GLuint mPgm;
499 GLint mPositionHandle;
500 GLint mTexSamplerHandle;
501 GLint mTexMatrixHandle;
502 };
503
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700504 class FrameWaiter : public SurfaceTexture::FrameAvailableListener {
505 public:
506 FrameWaiter():
507 mPendingFrames(0) {
508 }
509
510 void waitForFrame() {
511 Mutex::Autolock lock(mMutex);
512 while (mPendingFrames == 0) {
513 mCondition.wait(mMutex);
514 }
515 mPendingFrames--;
516 }
517
518 virtual void onFrameAvailable() {
519 Mutex::Autolock lock(mMutex);
520 mPendingFrames++;
521 mCondition.signal();
522 }
523
524 int mPendingFrames;
525 Mutex mMutex;
526 Condition mCondition;
527 };
528
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700529 // Note that SurfaceTexture will lose the notifications
530 // onBuffersReleased and onFrameAvailable as there is currently
531 // no way to forward the events. This DisconnectWaiter will not let the
532 // disconnect finish until finishDisconnect() is called. It will
533 // also block until a disconnect is called
534 class DisconnectWaiter : public BufferQueue::ConsumerListener {
535 public:
536 DisconnectWaiter () :
537 mWaitForDisconnect(false),
538 mPendingFrames(0) {
539 }
540
541 void waitForFrame() {
542 Mutex::Autolock lock(mMutex);
543 while (mPendingFrames == 0) {
544 mFrameCondition.wait(mMutex);
545 }
546 mPendingFrames--;
547 }
548
549 virtual void onFrameAvailable() {
550 Mutex::Autolock lock(mMutex);
551 mPendingFrames++;
552 mFrameCondition.signal();
553 }
554
555 virtual void onBuffersReleased() {
556 Mutex::Autolock lock(mMutex);
557 while (!mWaitForDisconnect) {
558 mDisconnectCondition.wait(mMutex);
559 }
560 }
561
562 void finishDisconnect() {
563 Mutex::Autolock lock(mMutex);
564 mWaitForDisconnect = true;
565 mDisconnectCondition.signal();
566 }
567
568 private:
569 Mutex mMutex;
570
571 bool mWaitForDisconnect;
572 Condition mDisconnectCondition;
573
574 int mPendingFrames;
575 Condition mFrameCondition;
576 };
577
Jamie Gennisd99c0882011-03-10 16:24:46 -0800578 sp<SurfaceTexture> mST;
579 sp<SurfaceTextureClient> mSTC;
580 sp<ANativeWindow> mANW;
Jamie Gennis74bed552012-03-28 19:05:54 -0700581 sp<TextureRenderer> mTextureRenderer;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700582 sp<FrameWaiter> mFW;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800583};
584
585// Fill a YV12 buffer with a multi-colored checkerboard pattern
586void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
587 const int blockWidth = w > 16 ? w / 16 : 1;
588 const int blockHeight = h > 16 ? h / 16 : 1;
589 const int yuvTexOffsetY = 0;
590 int yuvTexStrideY = stride;
591 int yuvTexOffsetV = yuvTexStrideY * h;
592 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
593 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
594 int yuvTexStrideU = yuvTexStrideV;
595 for (int x = 0; x < w; x++) {
596 for (int y = 0; y < h; y++) {
597 int parityX = (x / blockWidth) & 1;
598 int parityY = (y / blockHeight) & 1;
599 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
600 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
601 if (x < w / 2 && y < h / 2) {
602 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
603 if (x * 2 < w / 2 && y * 2 < h / 2) {
604 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
605 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
606 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
607 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
608 intensity;
609 }
610 }
611 }
612 }
613}
614
615// Fill a YV12 buffer with red outside a given rectangle and green inside it.
616void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
617 const android_native_rect_t& rect) {
618 const int yuvTexOffsetY = 0;
619 int yuvTexStrideY = stride;
620 int yuvTexOffsetV = yuvTexStrideY * h;
621 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
622 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
623 int yuvTexStrideU = yuvTexStrideV;
624 for (int x = 0; x < w; x++) {
625 for (int y = 0; y < h; y++) {
626 bool inside = rect.left <= x && x < rect.right &&
627 rect.top <= y && y < rect.bottom;
628 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
629 if (x < w / 2 && y < h / 2) {
630 bool inside = rect.left <= 2*x && 2*x < rect.right &&
631 rect.top <= 2*y && 2*y < rect.bottom;
632 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
633 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
634 inside ? 16 : 255;
635 }
636 }
637 }
638}
639
Jamie Gennis1876d132011-03-17 16:32:52 -0700640void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
641 const size_t PIXEL_SIZE = 4;
642 for (int x = 0; x < w; x++) {
643 for (int y = 0; y < h; y++) {
644 off_t offset = (y * stride + x) * PIXEL_SIZE;
645 for (int c = 0; c < 4; c++) {
646 int parityX = (x / (1 << (c+2))) & 1;
647 int parityY = (y / (1 << (c+2))) & 1;
648 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
649 }
650 }
651 }
652}
653
Jamie Gennisfe27e2f2011-11-11 18:05:11 -0800654void fillRGBA8BufferSolid(uint8_t* buf, int w, int h, int stride, uint8_t r,
655 uint8_t g, uint8_t b, uint8_t a) {
656 const size_t PIXEL_SIZE = 4;
657 for (int y = 0; y < h; y++) {
658 for (int x = 0; x < h; x++) {
659 off_t offset = (y * stride + x) * PIXEL_SIZE;
660 buf[offset + 0] = r;
661 buf[offset + 1] = g;
662 buf[offset + 2] = b;
663 buf[offset + 3] = a;
664 }
665 }
666}
667
Jamie Gennisce561372012-03-19 18:33:05 -0700668// Produce a single RGBA8 frame by filling a buffer with a checkerboard pattern
669// using the CPU. This assumes that the ANativeWindow is already configured to
670// allow this to be done (e.g. the format is set to RGBA8).
671//
672// Calls to this function should be wrapped in an ASSERT_NO_FATAL_FAILURE().
673void produceOneRGBA8Frame(const sp<ANativeWindow>& anw) {
674 android_native_buffer_t* anb;
675 ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &anb));
676 ASSERT_TRUE(anb != NULL);
677
678 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
679 ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf->getNativeBuffer()));
680
681 uint8_t* img = NULL;
682 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
683 (void**)(&img)));
684 fillRGBA8Buffer(img, buf->getWidth(), buf->getHeight(), buf->getStride());
685 ASSERT_EQ(NO_ERROR, buf->unlock());
686 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer()));
687}
688
Jamie Gennisd99c0882011-03-10 16:24:46 -0800689TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700690 const int texWidth = 64;
691 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800692
693 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700694 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800695 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
696 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
697
Iliyan Malchev697526b2011-05-01 11:33:26 -0700698 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800699 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
700 ASSERT_TRUE(anb != NULL);
701
702 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
703 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
704
705 // Fill the buffer with the a checkerboard pattern
706 uint8_t* img = NULL;
707 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700708 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800709 buf->unlock();
710 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
711
712 mST->updateTexImage();
713
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 Gennisd99c0882011-03-10 16:24:46 -0800744 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
745 ASSERT_TRUE(anb != NULL);
746
747 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
748 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
749
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();
755 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
756
757 mST->updateTexImage();
758
759 glClearColor(0.2, 0.2, 0.2, 0.2);
760 glClear(GL_COLOR_BUFFER_BIT);
761
Jamie Gennisc8c51522011-06-15 14:24:38 -0700762 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800763 drawTexture();
764
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700765 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
766 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800767 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
768 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
769
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700770 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
771 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
772 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
773 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
774 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
775 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
776 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800777}
778
779TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700780 const int texWidth = 64;
781 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800782
783 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700784 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800785 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
786 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
787
788 android_native_rect_t crops[] = {
789 {4, 6, 22, 36},
790 {0, 6, 22, 36},
791 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700792 {4, 6, texWidth, 36},
793 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800794 };
795
796 for (int i = 0; i < 5; i++) {
797 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800798 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
799 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800800
801 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
802
Iliyan Malchev697526b2011-05-01 11:33:26 -0700803 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800804 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
805 ASSERT_TRUE(anb != NULL);
806
807 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800808 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(),
809 buf->getNativeBuffer()));
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(),
816 buf->getNativeBuffer()));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800817
818 mST->updateTexImage();
819
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));
849 ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2));
850 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;
880 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
881 return false;
882 }
883 if (anb == NULL) {
884 return false;
885 }
886
887 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
888 if (mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())
889 != NO_ERROR) {
890 return false;
891 }
892
893 const int yuvTexOffsetY = 0;
894 int stride = buf->getStride();
895 int yuvTexStrideY = stride;
896 int yuvTexOffsetV = yuvTexStrideY * texHeight;
897 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
898 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
899 int yuvTexStrideU = yuvTexStrideV;
900
901 uint8_t* img = NULL;
902 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
903
904 // Gray out all the test pixels first, so we're more likely to
905 // see a failure if GL is still texturing from the buffer we
906 // just dequeued.
907 for (int j = 0; j < numTestPixels; j++) {
908 int x = mTestPixels[j].x;
909 int y = mTestPixels[j].y;
910 uint8_t value = 128;
911 img[y*stride + x] = value;
912 }
913
914 // Fill the buffer with gray.
915 for (int y = 0; y < texHeight; y++) {
916 for (int x = 0; x < texWidth; x++) {
917 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
918 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
919 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
920 }
921 }
922
923 // Set the test pixels to either white or black.
924 for (int j = 0; j < numTestPixels; j++) {
925 int x = mTestPixels[j].x;
926 int y = mTestPixels[j].y;
927 uint8_t value = 0;
928 if (j == (i % numTestPixels)) {
929 value = 255;
930 }
931 img[y*stride + x] = value;
932 }
933
934 buf->unlock();
935 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())
936 != NO_ERROR) {
937 return false;
938 }
939 }
940 return false;
941 }
942
943 sp<ANativeWindow> mANW;
944 const TestPixel* mTestPixels;
945 };
946
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700947 sp<Thread> pt(new ProducerThread(mANW, testPixels));
948 pt->run();
949
950 glViewport(0, 0, texWidth, texHeight);
951
952 glClearColor(0.2, 0.2, 0.2, 0.2);
953 glClear(GL_COLOR_BUFFER_BIT);
954
955 // We wait for the first two frames up front so that the producer will be
956 // likely to dequeue the buffer that's currently being textured from.
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700957 mFW->waitForFrame();
958 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700959
960 for (int i = 0; i < numFrames; i++) {
961 SCOPED_TRACE(String8::format("frame %d", i).string());
962
963 // We must wait for each frame to come in because if we ever do an
964 // updateTexImage call that doesn't consume a newly available buffer
965 // then the producer and consumer will get out of sync, which will cause
966 // a deadlock.
967 if (i > 1) {
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700968 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700969 }
970 mST->updateTexImage();
971 drawTexture();
972
973 for (int j = 0; j < numTestPixels; j++) {
974 int x = testPixels[j].x;
975 int y = testPixels[j].y;
976 uint8_t value = 0;
977 if (j == (i % numTestPixels)) {
978 // We must y-invert the texture coords
979 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
980 } else {
981 // We must y-invert the texture coords
982 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
983 }
984 }
985 }
986
987 pt->requestExitAndWait();
988}
989
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700990TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700991 const int texWidth = 64;
992 const int texHeight = 66;
993
994 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
995 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
996 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
997 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
998
Jamie Gennisce561372012-03-19 18:33:05 -0700999 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -07001000
1001 mST->updateTexImage();
1002
1003 glClearColor(0.2, 0.2, 0.2, 0.2);
1004 glClear(GL_COLOR_BUFFER_BIT);
1005
Jamie Gennisc8c51522011-06-15 14:24:38 -07001006 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001007 drawTexture();
1008
1009 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
1010 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001011 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
1012 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001013
1014 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001015 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001016 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001017 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
1018 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001019 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001020 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001021 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
1022 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
1023 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001024 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001025 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
1026 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001027 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001028 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001029 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
1030}
1031
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001032TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -07001033 const int texWidth = 64;
1034 const int texHeight = 64;
1035
1036 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
1037 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
1038 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
1039 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
1040
Jamie Gennisce561372012-03-19 18:33:05 -07001041 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -07001042
1043 mST->updateTexImage();
1044
1045 glClearColor(0.2, 0.2, 0.2, 0.2);
1046 glClear(GL_COLOR_BUFFER_BIT);
1047
Jamie Gennisc8c51522011-06-15 14:24:38 -07001048 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001049 drawTexture();
1050
1051 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
1052 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
1053 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
1054 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
1055
1056 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
1057 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
1058 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
1059 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
1060 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
1061 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
1062 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
1063 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
1064 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
1065 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
1066 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
1067 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
1068 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
1069 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
1070 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
1071 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
1072}
1073
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001074// Tests if SurfaceTexture and BufferQueue are robust enough
1075// to handle a special case where updateTexImage is called
1076// in the middle of disconnect. This ordering is enforced
1077// by blocking in the disconnect callback.
1078TEST_F(SurfaceTextureGLTest, DisconnectStressTest) {
1079
1080 class ProducerThread : public Thread {
1081 public:
1082 ProducerThread(const sp<ANativeWindow>& anw):
1083 mANW(anw) {
1084 }
1085
1086 virtual ~ProducerThread() {
1087 }
1088
1089 virtual bool threadLoop() {
1090 ANativeWindowBuffer* anb;
1091
1092 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL);
1093
1094 for (int numFrames =0 ; numFrames < 2; numFrames ++) {
1095
1096 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1097 return false;
1098 }
1099 if (anb == NULL) {
1100 return false;
1101 }
1102 if (mANW->queueBuffer(mANW.get(), anb)
1103 != NO_ERROR) {
1104 return false;
1105 }
1106 }
1107
1108 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_EGL);
1109
1110 return false;
1111 }
1112
1113 private:
1114 sp<ANativeWindow> mANW;
1115 };
1116
1117 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1118
1119 sp<DisconnectWaiter> dw(new DisconnectWaiter());
1120 mST->getBufferQueue()->consumerConnect(dw);
1121
1122
1123 sp<Thread> pt(new ProducerThread(mANW));
1124 pt->run();
1125
1126 // eat a frame so SurfaceTexture will own an at least one slot
1127 dw->waitForFrame();
1128 EXPECT_EQ(OK,mST->updateTexImage());
1129
1130 dw->waitForFrame();
1131 // Could fail here as SurfaceTexture thinks it still owns the slot
1132 // but bufferQueue has released all slots
1133 EXPECT_EQ(OK,mST->updateTexImage());
1134
1135 dw->finishDisconnect();
1136}
1137
1138
1139// This test ensures that the SurfaceTexture clears the mCurrentTexture
1140// when it is disconnected and reconnected. Otherwise it will
1141// attempt to release a buffer that it does not owned
1142TEST_F(SurfaceTextureGLTest, DisconnectClearsCurrentTexture) {
1143 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1144
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001145 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1146 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001147
1148 ANativeWindowBuffer *anb;
1149
1150 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1151 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1152
1153 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1154 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1155
1156 EXPECT_EQ(OK,mST->updateTexImage());
1157 EXPECT_EQ(OK,mST->updateTexImage());
1158
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001159 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1160 NATIVE_WINDOW_API_EGL));
1161 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1162 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001163
1164 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1165
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001166 EXPECT_EQ(OK, mANW->dequeueBuffer(mANW.get(), &anb));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001167 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
1168
1169 // Will fail here if mCurrentTexture is not cleared properly
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001170 mFW->waitForFrame();
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001171 EXPECT_EQ(OK,mST->updateTexImage());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001172
1173 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1174 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001175}
1176
Daniel Lam016c8cb2012-04-03 15:54:58 -07001177TEST_F(SurfaceTextureGLTest, ScaleToWindowMode) {
1178 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1179
1180 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1181 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW));
1182
1183 // The producer image size
1184 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1185
1186 // The consumer image size (16 x 9) ratio
1187 mST->setDefaultBufferSize(1280, 720);
1188
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001189 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
1190 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001191
1192 ANativeWindowBuffer *anb;
1193
1194 android_native_rect_t odd = {23, 78, 123, 477};
1195 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &odd));
1196 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1197 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001198 mFW->waitForFrame();
Daniel Lam016c8cb2012-04-03 15:54:58 -07001199 EXPECT_EQ(OK,mST->updateTexImage());
1200 Rect r = mST->getCurrentCrop();
1201 assertRectEq(Rect(23, 78, 123, 477), r);
1202
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001203 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
1204 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -07001205}
1206
1207// This test ensures the scaling mode does the right thing
1208// ie NATIVE_WINDOW_SCALING_MODE_CROP should crop
1209// the image such that it has the same aspect ratio as the
1210// default buffer size
1211TEST_F(SurfaceTextureGLTest, CroppedScalingMode) {
1212 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1213
1214 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(),
1215 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP));
1216
1217 // The producer image size
1218 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512));
1219
1220 // The consumer image size (16 x 9) ratio
1221 mST->setDefaultBufferSize(1280, 720);
1222
1223 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU);
1224
1225 ANativeWindowBuffer *anb;
1226
1227 // The crop is in the shape of (320, 180) === 16 x 9
1228 android_native_rect_t standard = {10, 20, 330, 200};
1229 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &standard));
1230 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1231 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001232 mFW->waitForFrame();
Daniel Lam016c8cb2012-04-03 15:54:58 -07001233 EXPECT_EQ(OK,mST->updateTexImage());
1234 Rect r = mST->getCurrentCrop();
1235 // crop should be the same as crop (same aspect ratio)
1236 assertRectEq(Rect(10, 20, 330, 200), r);
1237
1238 // make this wider then desired aspect 239 x 100 (2.39:1)
1239 android_native_rect_t wide = {20, 30, 259, 130};
1240 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &wide));
1241 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1242 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001243 mFW->waitForFrame();
Daniel Lam016c8cb2012-04-03 15:54:58 -07001244 EXPECT_EQ(OK,mST->updateTexImage());
1245 r = mST->getCurrentCrop();
1246 // crop should be the same height, but have cropped left and right borders
1247 // offset is 30.6 px L+, R-
1248 assertRectEq(Rect(51, 30, 228, 130), r);
1249
1250 // This image is taller then desired aspect 400 x 300 (4:3)
1251 android_native_rect_t narrow = {0, 0, 400, 300};
1252 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &narrow));
1253 EXPECT_EQ (OK, mANW->dequeueBuffer(mANW.get(), &anb));
1254 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001255 mFW->waitForFrame();
Daniel Lam016c8cb2012-04-03 15:54:58 -07001256 EXPECT_EQ(OK,mST->updateTexImage());
1257 r = mST->getCurrentCrop();
1258 // crop should be the same width, but have cropped top and bottom borders
1259 // offset is 37.5 px
1260 assertRectEq(Rect(0, 37, 400, 262), r);
1261
1262 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
1263}
1264
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001265TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
1266 class ProducerThread : public Thread {
1267 public:
1268 ProducerThread(const sp<ANativeWindow>& anw):
1269 mANW(anw),
1270 mDequeueError(NO_ERROR) {
1271 }
1272
1273 virtual ~ProducerThread() {
1274 }
1275
1276 virtual bool threadLoop() {
1277 Mutex::Autolock lock(mMutex);
1278 ANativeWindowBuffer* anb;
1279
1280 // Frame 1
1281 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1282 return false;
1283 }
1284 if (anb == NULL) {
1285 return false;
1286 }
1287 if (mANW->queueBuffer(mANW.get(), anb)
1288 != NO_ERROR) {
1289 return false;
1290 }
1291
1292 // Frame 2
1293 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1294 return false;
1295 }
1296 if (anb == NULL) {
1297 return false;
1298 }
1299 if (mANW->queueBuffer(mANW.get(), anb)
1300 != NO_ERROR) {
1301 return false;
1302 }
1303
1304 // Frame 3 - error expected
1305 mDequeueError = mANW->dequeueBuffer(mANW.get(), &anb);
1306 return false;
1307 }
1308
1309 status_t getDequeueError() {
1310 Mutex::Autolock lock(mMutex);
1311 return mDequeueError;
1312 }
1313
1314 private:
1315 sp<ANativeWindow> mANW;
1316 status_t mDequeueError;
1317 Mutex mMutex;
1318 };
1319
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001320 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1321 ASSERT_EQ(OK, mST->setBufferCountServer(2));
1322
1323 sp<Thread> pt(new ProducerThread(mANW));
1324 pt->run();
1325
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001326 mFW->waitForFrame();
1327 mFW->waitForFrame();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001328
1329 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1330 // block waiting for a buffer to become available.
1331 usleep(100000);
1332
1333 mST->abandon();
1334
1335 pt->requestExitAndWait();
1336 ASSERT_EQ(NO_INIT,
1337 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1338}
1339
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001340TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1341 int texHeight = 16;
1342 ANativeWindowBuffer* anb;
1343
1344 GLint maxTextureSize;
1345 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1346
1347 // make sure it works with small textures
1348 mST->setDefaultBufferSize(16, texHeight);
1349 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1350 EXPECT_EQ(16, anb->width);
1351 EXPECT_EQ(texHeight, anb->height);
1352 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1353 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1354
1355 // make sure it works with GL_MAX_TEXTURE_SIZE
1356 mST->setDefaultBufferSize(maxTextureSize, texHeight);
1357 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1358 EXPECT_EQ(maxTextureSize, anb->width);
1359 EXPECT_EQ(texHeight, anb->height);
1360 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1361 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1362
1363 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1364 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
1365 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1366 EXPECT_EQ(maxTextureSize+1, anb->width);
1367 EXPECT_EQ(texHeight, anb->height);
1368 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1369 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1370}
1371
Jamie Gennis5451d152011-06-08 09:40:45 -07001372/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001373 * This test fixture is for testing GL -> GL texture streaming. It creates an
1374 * EGLSurface and an EGLContext for the image producer to use.
1375 */
1376class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1377protected:
1378 SurfaceTextureGLToGLTest():
1379 mProducerEglSurface(EGL_NO_SURFACE),
1380 mProducerEglContext(EGL_NO_CONTEXT) {
1381 }
1382
1383 virtual void SetUp() {
1384 SurfaceTextureGLTest::SetUp();
1385
Jamie Gennisce561372012-03-19 18:33:05 -07001386 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001387 mANW.get(), NULL);
1388 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1389 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1390
Jamie Gennisce561372012-03-19 18:33:05 -07001391 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001392 EGL_NO_CONTEXT, getContextAttribs());
1393 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1394 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1395 }
1396
1397 virtual void TearDown() {
1398 if (mProducerEglContext != EGL_NO_CONTEXT) {
1399 eglDestroyContext(mEglDisplay, mProducerEglContext);
1400 }
1401 if (mProducerEglSurface != EGL_NO_SURFACE) {
1402 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1403 }
1404 SurfaceTextureGLTest::TearDown();
1405 }
1406
1407 EGLSurface mProducerEglSurface;
1408 EGLContext mProducerEglContext;
1409};
1410
1411TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1412 const int texWidth = 64;
1413 const int texHeight = 64;
1414
1415 mST->setDefaultBufferSize(texWidth, texHeight);
1416
1417 // Do the producer side of things
1418 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1419 mProducerEglSurface, mProducerEglContext));
1420 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1421
1422 // This is needed to ensure we pick up a buffer of the correct size.
1423 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1424
1425 glClearColor(0.6, 0.6, 0.6, 0.6);
1426 glClear(GL_COLOR_BUFFER_BIT);
1427
1428 glEnable(GL_SCISSOR_TEST);
1429 glScissor(4, 4, 4, 4);
1430 glClearColor(1.0, 0.0, 0.0, 1.0);
1431 glClear(GL_COLOR_BUFFER_BIT);
1432
1433 glScissor(24, 48, 4, 4);
1434 glClearColor(0.0, 1.0, 0.0, 1.0);
1435 glClear(GL_COLOR_BUFFER_BIT);
1436
1437 glScissor(37, 17, 4, 4);
1438 glClearColor(0.0, 0.0, 1.0, 1.0);
1439 glClear(GL_COLOR_BUFFER_BIT);
1440
1441 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1442
1443 // Do the consumer side of things
1444 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1445 mEglContext));
1446 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1447
1448 glDisable(GL_SCISSOR_TEST);
1449
1450 mST->updateTexImage(); // Skip the first frame, which was empty
1451 mST->updateTexImage();
1452
1453 glClearColor(0.2, 0.2, 0.2, 0.2);
1454 glClear(GL_COLOR_BUFFER_BIT);
1455
1456 glViewport(0, 0, texWidth, texHeight);
1457 drawTexture();
1458
1459 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1460 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1461 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1462 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1463
1464 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1465 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1466 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1467 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1468 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1469 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1470 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1471 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1472 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1473 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1474 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1475 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1476 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1477 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1478 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1479 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1480}
1481
1482TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001483 sp<GraphicBuffer> buffers[2];
1484
Jamie Gennise3603d72011-11-19 21:20:17 -08001485 // This test requires async mode to run on a single thread.
1486 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1487 mProducerEglSurface, mProducerEglContext));
1488 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1489 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1490 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1491
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001492 for (int i = 0; i < 2; i++) {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001493 // Produce a frame
1494 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1495 mProducerEglSurface, mProducerEglContext));
1496 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1497 glClear(GL_COLOR_BUFFER_BIT);
1498 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1499
1500 // Consume a frame
1501 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1502 mEglContext));
1503 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001504 mFW->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001505 mST->updateTexImage();
1506 buffers[i] = mST->getCurrentBuffer();
1507 }
1508
1509 // Destroy the GL texture object to release its ref on buffers[2].
1510 GLuint texID = TEX_ID;
1511 glDeleteTextures(1, &texID);
1512
1513 // Destroy the EGLSurface
1514 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1515 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001516 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001517
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001518 // This test should have the only reference to buffer 0.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001519 EXPECT_EQ(1, buffers[0]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001520
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001521 // The SurfaceTexture should hold a single reference to buffer 1 in its
1522 // mCurrentBuffer member. All of the references in the slots should have
1523 // been released.
1524 EXPECT_EQ(2, buffers[1]->getStrongCount());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001525}
1526
1527TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1528 sp<GraphicBuffer> buffers[3];
1529
Jamie Gennise3603d72011-11-19 21:20:17 -08001530 // This test requires async mode to run on a single thread.
1531 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1532 mProducerEglSurface, mProducerEglContext));
1533 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1534 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1535 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1536
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001537 for (int i = 0; i < 3; i++) {
1538 // Produce a frame
1539 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1540 mProducerEglSurface, mProducerEglContext));
1541 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1542 glClear(GL_COLOR_BUFFER_BIT);
1543 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1544 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1545
1546 // Consume a frame
1547 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1548 mEglContext));
1549 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001550 mFW->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001551 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1552 buffers[i] = mST->getCurrentBuffer();
1553 }
1554
1555 // Abandon the SurfaceTexture, releasing the ref that the SurfaceTexture has
1556 // on buffers[2].
1557 mST->abandon();
1558
1559 // Destroy the GL texture object to release its ref on buffers[2].
1560 GLuint texID = TEX_ID;
1561 glDeleteTextures(1, &texID);
1562
1563 // Destroy the EGLSurface.
1564 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1565 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001566 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001567
1568 EXPECT_EQ(1, buffers[0]->getStrongCount());
1569 EXPECT_EQ(1, buffers[1]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001570
1571 // Depending on how lazily the GL driver dequeues buffers, we may end up
1572 // with either two or three total buffers. If there are three, make sure
1573 // the last one was properly down-ref'd.
1574 if (buffers[2] != buffers[0]) {
1575 EXPECT_EQ(1, buffers[2]->getStrongCount());
1576 }
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001577}
1578
Jamie Gennis59769462011-11-19 18:04:43 -08001579TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) {
1580 // This test requires 3 buffers to run on a single thread.
1581 mST->setBufferCountServer(3);
1582
1583 ASSERT_TRUE(mST->isSynchronousMode());
1584
1585 for (int i = 0; i < 10; i++) {
1586 // Produce a frame
1587 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1588 mProducerEglSurface, mProducerEglContext));
1589 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1590 glClear(GL_COLOR_BUFFER_BIT);
1591 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1592 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1593
1594 // Consume a frame
1595 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1596 mEglContext));
1597 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1598 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1599 }
1600
1601 ASSERT_TRUE(mST->isSynchronousMode());
1602}
1603
Jamie Gennisc2c38022012-04-11 17:20:03 -07001604TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
1605 enum { texWidth = 64 };
1606 enum { texHeight = 64 };
1607
1608 // Set the user buffer size.
1609 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1610
1611 // Do the producer side of things
1612 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1613 mProducerEglSurface, mProducerEglContext));
1614 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1615
1616 // This is needed to ensure we pick up a buffer of the correct size.
1617 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1618
1619 glClearColor(0.6, 0.6, 0.6, 0.6);
1620 glClear(GL_COLOR_BUFFER_BIT);
1621
1622 glEnable(GL_SCISSOR_TEST);
1623 glScissor(4, 4, 1, 1);
1624 glClearColor(1.0, 0.0, 0.0, 1.0);
1625 glClear(GL_COLOR_BUFFER_BIT);
1626
1627 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1628
1629 // Do the consumer side of things
1630 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1631 mEglContext));
1632 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1633
1634 glDisable(GL_SCISSOR_TEST);
1635
1636 mST->updateTexImage(); // Skip the first frame, which was empty
1637 mST->updateTexImage();
1638
1639 glClearColor(0.2, 0.2, 0.2, 0.2);
1640 glClear(GL_COLOR_BUFFER_BIT);
1641
1642 glViewport(0, 0, texWidth, texHeight);
1643 drawTexture();
1644
1645 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1646 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1647 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1648 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1649
1650 EXPECT_TRUE(checkPixel( 4, 4, 255, 0, 0, 255));
1651 EXPECT_TRUE(checkPixel( 5, 5, 153, 153, 153, 153));
1652 EXPECT_TRUE(checkPixel( 3, 3, 153, 153, 153, 153));
1653 EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153));
1654 EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153));
1655}
1656
1657TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
1658 enum { texWidth = 64 };
1659 enum { texHeight = 16 };
1660
1661 // Set the transform hint.
1662 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1663
1664 // Set the user buffer size.
1665 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1666
1667 // Do the producer side of things
1668 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1669 mProducerEglSurface, mProducerEglContext));
1670 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1671
1672 // This is needed to ensure we pick up a buffer of the correct size and the
1673 // new rotation hint.
1674 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1675
1676 glClearColor(0.6, 0.6, 0.6, 0.6);
1677 glClear(GL_COLOR_BUFFER_BIT);
1678
1679 glEnable(GL_SCISSOR_TEST);
1680 glScissor(24, 4, 1, 1);
1681 glClearColor(1.0, 0.0, 0.0, 1.0);
1682 glClear(GL_COLOR_BUFFER_BIT);
1683
1684 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1685
1686 // Do the consumer side of things
1687 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1688 mEglContext));
1689 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1690
1691 glDisable(GL_SCISSOR_TEST);
1692
1693 mST->updateTexImage(); // Skip the first frame, which was empty
1694 mST->updateTexImage();
1695
1696 glClearColor(0.2, 0.2, 0.2, 0.2);
1697 glClear(GL_COLOR_BUFFER_BIT);
1698
1699 glViewport(0, 0, texWidth, texHeight);
1700 drawTexture();
1701
1702 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1703 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1704 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1705 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1706
1707 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1708 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1709 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1710 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1711 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1712}
1713
1714TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
1715 enum { texWidth = 64 };
1716 enum { texHeight = 16 };
1717
1718 // Set the transform hint.
1719 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1720
1721 // Set the default buffer size.
1722 mST->setDefaultBufferSize(texWidth, texHeight);
1723
1724 // Do the producer side of things
1725 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1726 mProducerEglSurface, mProducerEglContext));
1727 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1728
1729 // This is needed to ensure we pick up a buffer of the correct size and the
1730 // new rotation hint.
1731 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1732
1733 glClearColor(0.6, 0.6, 0.6, 0.6);
1734 glClear(GL_COLOR_BUFFER_BIT);
1735
1736 glEnable(GL_SCISSOR_TEST);
1737 glScissor(24, 4, 1, 1);
1738 glClearColor(1.0, 0.0, 0.0, 1.0);
1739 glClear(GL_COLOR_BUFFER_BIT);
1740
1741 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1742
1743 // Do the consumer side of things
1744 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1745 mEglContext));
1746 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1747
1748 glDisable(GL_SCISSOR_TEST);
1749
1750 mST->updateTexImage(); // Skip the first frame, which was empty
1751 mST->updateTexImage();
1752
1753 glClearColor(0.2, 0.2, 0.2, 0.2);
1754 glClear(GL_COLOR_BUFFER_BIT);
1755
1756 glViewport(0, 0, texWidth, texHeight);
1757 drawTexture();
1758
1759 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1760 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1761 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1762 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1763
1764 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1765 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1766 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1767 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1768 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1769}
1770
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001771/*
1772 * This test fixture is for testing GL -> GL texture streaming from one thread
1773 * to another. It contains functionality to create a producer thread that will
1774 * perform GL rendering to an ANativeWindow that feeds frames to a
1775 * SurfaceTexture. Additionally it supports interlocking the producer and
1776 * consumer threads so that a specific sequence of calls can be
1777 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07001778 *
1779 * The intended usage is as follows:
1780 *
1781 * TEST_F(...) {
1782 * class PT : public ProducerThread {
1783 * virtual void render() {
1784 * ...
1785 * swapBuffers();
1786 * }
1787 * };
1788 *
1789 * runProducerThread(new PT());
1790 *
1791 * // The order of these calls will vary from test to test and may include
1792 * // multiple frames and additional operations (e.g. GL rendering from the
1793 * // texture).
1794 * fc->waitForFrame();
1795 * mST->updateTexImage();
1796 * fc->finishFrame();
1797 * }
1798 *
1799 */
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001800class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07001801protected:
1802
1803 // ProducerThread is an abstract base class to simplify the creation of
1804 // OpenGL ES frame producer threads.
1805 class ProducerThread : public Thread {
1806 public:
1807 virtual ~ProducerThread() {
1808 }
1809
1810 void setEglObjects(EGLDisplay producerEglDisplay,
1811 EGLSurface producerEglSurface,
1812 EGLContext producerEglContext) {
1813 mProducerEglDisplay = producerEglDisplay;
1814 mProducerEglSurface = producerEglSurface;
1815 mProducerEglContext = producerEglContext;
1816 }
1817
1818 virtual bool threadLoop() {
1819 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
1820 mProducerEglSurface, mProducerEglContext);
1821 render();
1822 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1823 EGL_NO_CONTEXT);
1824 return false;
1825 }
1826
1827 protected:
1828 virtual void render() = 0;
1829
1830 void swapBuffers() {
1831 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
1832 }
1833
1834 EGLDisplay mProducerEglDisplay;
1835 EGLSurface mProducerEglSurface;
1836 EGLContext mProducerEglContext;
1837 };
1838
1839 // FrameCondition is a utility class for interlocking between the producer
1840 // and consumer threads. The FrameCondition object should be created and
1841 // destroyed in the consumer thread only. The consumer thread should set
1842 // the FrameCondition as the FrameAvailableListener of the SurfaceTexture,
1843 // and should call both waitForFrame and finishFrame once for each expected
1844 // frame.
1845 //
1846 // This interlocking relies on the fact that onFrameAvailable gets called
1847 // synchronously from SurfaceTexture::queueBuffer.
1848 class FrameCondition : public SurfaceTexture::FrameAvailableListener {
1849 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001850 FrameCondition():
1851 mFrameAvailable(false),
1852 mFrameFinished(false) {
1853 }
1854
Jamie Gennis5451d152011-06-08 09:40:45 -07001855 // waitForFrame waits for the next frame to arrive. This should be
1856 // called from the consumer thread once for every frame expected by the
1857 // test.
1858 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001859 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001860 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001861 while (!mFrameAvailable) {
1862 mFrameAvailableCondition.wait(mMutex);
1863 }
1864 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01001865 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001866 }
1867
1868 // Allow the producer to return from its swapBuffers call and continue
1869 // on to produce the next frame. This should be called by the consumer
1870 // thread once for every frame expected by the test.
1871 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001872 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001873 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001874 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001875 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01001876 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001877 }
1878
1879 // This should be called by SurfaceTexture on the producer thread.
1880 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001881 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001882 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001883 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001884 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001885 while (!mFrameFinished) {
1886 mFrameFinishCondition.wait(mMutex);
1887 }
1888 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01001889 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07001890 }
1891
1892 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001893 bool mFrameAvailable;
1894 bool mFrameFinished;
1895
Jamie Gennis5451d152011-06-08 09:40:45 -07001896 Mutex mMutex;
1897 Condition mFrameAvailableCondition;
1898 Condition mFrameFinishCondition;
1899 };
1900
Jamie Gennis5451d152011-06-08 09:40:45 -07001901 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001902 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07001903 mFC = new FrameCondition();
1904 mST->setFrameAvailableListener(mFC);
1905 }
1906
1907 virtual void TearDown() {
1908 if (mProducerThread != NULL) {
1909 mProducerThread->requestExitAndWait();
1910 }
Jamie Gennis5451d152011-06-08 09:40:45 -07001911 mProducerThread.clear();
1912 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001913 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07001914 }
1915
1916 void runProducerThread(const sp<ProducerThread> producerThread) {
1917 ASSERT_TRUE(mProducerThread == NULL);
1918 mProducerThread = producerThread;
1919 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
1920 mProducerEglContext);
1921 producerThread->run();
1922 }
1923
Jamie Gennis5451d152011-06-08 09:40:45 -07001924 sp<ProducerThread> mProducerThread;
1925 sp<FrameCondition> mFC;
1926};
1927
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001928TEST_F(SurfaceTextureGLThreadToGLTest,
1929 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001930 class PT : public ProducerThread {
1931 virtual void render() {
1932 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1933 glClear(GL_COLOR_BUFFER_BIT);
1934 swapBuffers();
1935 }
1936 };
1937
1938 runProducerThread(new PT());
1939
1940 mFC->waitForFrame();
1941 mST->updateTexImage();
1942 mFC->finishFrame();
1943
1944 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08001945}
Jamie Gennis5451d152011-06-08 09:40:45 -07001946
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001947TEST_F(SurfaceTextureGLThreadToGLTest,
1948 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001949 class PT : public ProducerThread {
1950 virtual void render() {
1951 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1952 glClear(GL_COLOR_BUFFER_BIT);
1953 swapBuffers();
1954 }
1955 };
1956
1957 runProducerThread(new PT());
1958
1959 mFC->waitForFrame();
1960 mFC->finishFrame();
1961 mST->updateTexImage();
1962
1963 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1964}
1965
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001966TEST_F(SurfaceTextureGLThreadToGLTest,
1967 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001968 enum { NUM_ITERATIONS = 1024 };
1969
1970 class PT : public ProducerThread {
1971 virtual void render() {
1972 for (int i = 0; i < NUM_ITERATIONS; i++) {
1973 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1974 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001975 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001976 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001977 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001978 }
1979 }
1980 };
1981
1982 runProducerThread(new PT());
1983
1984 for (int i = 0; i < NUM_ITERATIONS; i++) {
1985 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01001986 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001987 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01001988 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001989 mFC->finishFrame();
1990
1991 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1992 }
1993}
1994
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001995TEST_F(SurfaceTextureGLThreadToGLTest,
1996 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001997 enum { NUM_ITERATIONS = 1024 };
1998
1999 class PT : public ProducerThread {
2000 virtual void render() {
2001 for (int i = 0; i < NUM_ITERATIONS; i++) {
2002 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2003 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002004 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002005 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002006 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002007 }
2008 }
2009 };
2010
2011 runProducerThread(new PT());
2012
2013 for (int i = 0; i < NUM_ITERATIONS; i++) {
2014 mFC->waitForFrame();
2015 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002016 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002017 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01002018 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002019
2020 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2021 }
2022}
2023
Jamie Gennis6e502192011-07-21 14:31:31 -07002024// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002025TEST_F(SurfaceTextureGLThreadToGLTest,
2026 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07002027 enum { NUM_ITERATIONS = 64 };
2028
2029 class PT : public ProducerThread {
2030 virtual void render() {
2031 for (int i = 0; i < NUM_ITERATIONS; i++) {
2032 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2033 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002034 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002035 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002036 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002037 }
2038 }
2039 };
2040
2041 ASSERT_EQ(OK, mST->setSynchronousMode(true));
2042 ASSERT_EQ(OK, mST->setBufferCountServer(2));
2043
2044 runProducerThread(new PT());
2045
2046 // Allow three frames to be rendered and queued before starting the
2047 // rendering in this thread. For the latter two frames we don't call
2048 // updateTexImage so the next dequeue from the producer thread will block
2049 // waiting for a frame to become available.
2050 mFC->waitForFrame();
2051 mFC->finishFrame();
2052
2053 // We must call updateTexImage to consume the first frame so that the
2054 // SurfaceTexture is able to reduce the buffer count to 2. This is because
2055 // the GL driver may dequeue a buffer when the EGLSurface is created, and
2056 // that happens before we call setBufferCountServer. It's possible that the
2057 // driver does not dequeue a buffer at EGLSurface creation time, so we
2058 // cannot rely on this to cause the second dequeueBuffer call to block.
2059 mST->updateTexImage();
2060
2061 mFC->waitForFrame();
2062 mFC->finishFrame();
2063 mFC->waitForFrame();
2064 mFC->finishFrame();
2065
2066 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
2067 // block waiting for a buffer to become available.
2068 usleep(100000);
2069
2070 // Render and present a number of images. This thread should not be blocked
2071 // by the fact that the producer thread is blocking in dequeue.
2072 for (int i = 0; i < NUM_ITERATIONS; i++) {
2073 glClear(GL_COLOR_BUFFER_BIT);
2074 eglSwapBuffers(mEglDisplay, mEglSurface);
2075 }
2076
2077 // Consume the two pending buffers to unblock the producer thread.
2078 mST->updateTexImage();
2079 mST->updateTexImage();
2080
2081 // Consume the remaining buffers from the producer thread.
2082 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
2083 mFC->waitForFrame();
2084 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002085 ALOGV("+updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002086 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01002087 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002088 }
2089}
2090
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002091class SurfaceTextureFBOTest : public SurfaceTextureGLTest {
2092protected:
2093
2094 virtual void SetUp() {
2095 SurfaceTextureGLTest::SetUp();
2096
2097 glGenFramebuffers(1, &mFbo);
2098 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2099
2100 glGenTextures(1, &mFboTex);
2101 glBindTexture(GL_TEXTURE_2D, mFboTex);
2102 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getSurfaceWidth(),
2103 getSurfaceHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2104 glBindTexture(GL_TEXTURE_2D, 0);
2105 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2106
2107 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2108 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2109 GL_TEXTURE_2D, mFboTex, 0);
2110 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2111 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2112 }
2113
2114 virtual void TearDown() {
2115 SurfaceTextureGLTest::TearDown();
2116
2117 glDeleteTextures(1, &mFboTex);
2118 glDeleteFramebuffers(1, &mFbo);
2119 }
2120
2121 GLuint mFbo;
2122 GLuint mFboTex;
2123};
2124
2125// This test is intended to verify that proper synchronization is done when
2126// rendering into an FBO.
2127TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) {
2128 const int texWidth = 64;
2129 const int texHeight = 64;
2130
2131 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
2132 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
2133 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
2134 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
2135
2136 android_native_buffer_t* anb;
2137 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
2138 ASSERT_TRUE(anb != NULL);
2139
2140 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
2141 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
2142
2143 // Fill the buffer with green
2144 uint8_t* img = NULL;
2145 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
2146 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255,
2147 0, 255);
2148 buf->unlock();
2149 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
2150
2151 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2152
2153 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2154 drawTexture();
2155 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2156
2157 for (int i = 0; i < 4; i++) {
2158 SCOPED_TRACE(String8::format("frame %d", i).string());
2159
2160 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
2161 ASSERT_TRUE(anb != NULL);
2162
2163 buf = new GraphicBuffer(anb, false);
2164 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(),
2165 buf->getNativeBuffer()));
2166
2167 // Fill the buffer with red
2168 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
2169 (void**)(&img)));
2170 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0,
2171 0, 255);
2172 ASSERT_EQ(NO_ERROR, buf->unlock());
2173 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
2174 buf->getNativeBuffer()));
2175
2176 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2177
2178 drawTexture();
2179
2180 EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255));
2181 }
2182
2183 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2184
2185 EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255));
2186}
2187
Jamie Gennisce561372012-03-19 18:33:05 -07002188class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest {
2189protected:
Jamie Gennis74bed552012-03-28 19:05:54 -07002190 enum { SECOND_TEX_ID = 123 };
2191 enum { THIRD_TEX_ID = 456 };
2192
Jamie Gennisce561372012-03-19 18:33:05 -07002193 SurfaceTextureMultiContextGLTest():
2194 mSecondEglContext(EGL_NO_CONTEXT) {
2195 }
2196
2197 virtual void SetUp() {
2198 SurfaceTextureGLTest::SetUp();
2199
Jamie Gennis74bed552012-03-28 19:05:54 -07002200 // Set up the secondary context and texture renderer.
Jamie Gennisce561372012-03-19 18:33:05 -07002201 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2202 EGL_NO_CONTEXT, getContextAttribs());
2203 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2204 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext);
Jamie Gennis74bed552012-03-28 19:05:54 -07002205
2206 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2207 mSecondEglContext));
2208 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2209 mSecondTextureRenderer = new TextureRenderer(SECOND_TEX_ID, mST);
2210 ASSERT_NO_FATAL_FAILURE(mSecondTextureRenderer->SetUp());
2211
2212 // Set up the tertiary context and texture renderer.
2213 mThirdEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2214 EGL_NO_CONTEXT, getContextAttribs());
2215 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2216 ASSERT_NE(EGL_NO_CONTEXT, mThirdEglContext);
2217
2218 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2219 mThirdEglContext));
2220 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2221 mThirdTextureRenderer = new TextureRenderer(THIRD_TEX_ID, mST);
2222 ASSERT_NO_FATAL_FAILURE(mThirdTextureRenderer->SetUp());
2223
2224 // Switch back to the primary context to start the tests.
2225 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2226 mEglContext));
Jamie Gennisce561372012-03-19 18:33:05 -07002227 }
2228
2229 virtual void TearDown() {
Jamie Gennis74bed552012-03-28 19:05:54 -07002230 if (mThirdEglContext != EGL_NO_CONTEXT) {
2231 eglDestroyContext(mEglDisplay, mThirdEglContext);
2232 }
Jamie Gennisce561372012-03-19 18:33:05 -07002233 if (mSecondEglContext != EGL_NO_CONTEXT) {
2234 eglDestroyContext(mEglDisplay, mSecondEglContext);
2235 }
2236 SurfaceTextureGLTest::TearDown();
2237 }
2238
2239 EGLContext mSecondEglContext;
Jamie Gennis74bed552012-03-28 19:05:54 -07002240 sp<TextureRenderer> mSecondTextureRenderer;
2241
2242 EGLContext mThirdEglContext;
2243 sp<TextureRenderer> mThirdTextureRenderer;
Jamie Gennisce561372012-03-19 18:33:05 -07002244};
2245
2246TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) {
Jamie Gennisce561372012-03-19 18:33:05 -07002247 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2248
2249 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002250 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002251 ASSERT_EQ(OK, mST->updateTexImage());
Jamie Gennisce561372012-03-19 18:33:05 -07002252
2253 // Attempt to latch the texture on the secondary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07002254 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
Jamie Gennisce561372012-03-19 18:33:05 -07002255 mSecondEglContext));
2256 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -07002257 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2258}
2259
2260TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002261 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2262
2263 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002264 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002265 ASSERT_EQ(OK, mST->updateTexImage());
2266
2267 // Detach from the primary context.
2268 ASSERT_EQ(OK, mST->detachFromContext());
2269
2270 // Check that the GL texture was deleted.
2271 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2272}
2273
2274TEST_F(SurfaceTextureMultiContextGLTest,
2275 DetachFromContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002276 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2277
2278 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002279 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002280 ASSERT_EQ(OK, mST->updateTexImage());
2281
2282 // Detach from the primary context.
2283 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2284 ASSERT_EQ(OK, mST->detachFromContext());
2285
2286 // Check that the GL texture was deleted.
2287 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2288}
2289
2290TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002291 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2292
2293 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002294 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002295 ASSERT_EQ(OK, mST->updateTexImage());
2296
2297 // Attempt to detach from the primary context.
2298 mST->abandon();
2299 ASSERT_EQ(NO_INIT, mST->detachFromContext());
2300}
2301
2302TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002303 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2304
2305 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002306 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002307 ASSERT_EQ(OK, mST->updateTexImage());
2308
2309 // Detach from the primary context.
2310 ASSERT_EQ(OK, mST->detachFromContext());
2311
2312 // Attempt to detach from the primary context again.
2313 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2314}
2315
2316TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002317 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2318
2319 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002320 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002321 ASSERT_EQ(OK, mST->updateTexImage());
2322
2323 // Make there be no current display.
2324 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2325 EGL_NO_CONTEXT));
2326 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2327
2328 // Attempt to detach from the primary context.
2329 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2330}
2331
2332TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002333 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2334
2335 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002336 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002337 ASSERT_EQ(OK, mST->updateTexImage());
2338
2339 // Make current context be incorrect.
2340 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2341 mSecondEglContext));
2342 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2343
2344 // Attempt to detach from the primary context.
2345 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2346}
2347
2348TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002349 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2350
2351 // Detach from the primary context.
2352 ASSERT_EQ(OK, mST->detachFromContext());
2353
2354 // Attempt to latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002355 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002356 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2357}
2358
2359TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002360 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2361
2362 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002363 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002364 ASSERT_EQ(OK, mST->updateTexImage());
2365
2366 // Detach from the primary context.
2367 ASSERT_EQ(OK, mST->detachFromContext());
2368
2369 // Attach to the secondary context.
2370 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2371 mSecondEglContext));
2372 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2373
2374 // Verify that the texture object was created and bound.
2375 GLint texBinding = -1;
2376 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2377 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2378
2379 // Try to use the texture from the secondary context.
2380 glClearColor(0.2, 0.2, 0.2, 0.2);
2381 glClear(GL_COLOR_BUFFER_BIT);
2382 glViewport(0, 0, 1, 1);
2383 mSecondTextureRenderer->drawTexture();
2384 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2385 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2386}
2387
2388TEST_F(SurfaceTextureMultiContextGLTest,
2389 AttachToContextSucceedsAfterProducerDisconnect) {
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 // Detach from the primary context.
2397 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2398 ASSERT_EQ(OK, mST->detachFromContext());
2399
2400 // Attach to the secondary context.
2401 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2402 mSecondEglContext));
2403 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2404
2405 // Verify that the texture object was created and bound.
2406 GLint texBinding = -1;
2407 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2408 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2409
2410 // Try to use the texture from the secondary context.
2411 glClearColor(0.2, 0.2, 0.2, 0.2);
2412 glClear(GL_COLOR_BUFFER_BIT);
2413 glViewport(0, 0, 1, 1);
2414 mSecondTextureRenderer->drawTexture();
2415 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2416 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2417}
2418
2419TEST_F(SurfaceTextureMultiContextGLTest,
2420 AttachToContextSucceedsBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002421 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2422
2423 // Detach from the primary context.
2424 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2425 ASSERT_EQ(OK, mST->detachFromContext());
2426
2427 // Attach to the secondary context.
2428 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2429 mSecondEglContext));
2430 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2431
2432 // Verify that the texture object was created and bound.
2433 GLint texBinding = -1;
2434 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2435 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2436
2437 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002438 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002439 ASSERT_EQ(OK, mST->updateTexImage());
2440
2441 // Try to use the texture from the secondary context.
2442 glClearColor(0.2, 0.2, 0.2, 0.2);
2443 glClear(GL_COLOR_BUFFER_BIT);
2444 glViewport(0, 0, 1, 1);
2445 mSecondTextureRenderer->drawTexture();
2446 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2447 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2448}
2449
2450TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002451 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2452
2453 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002454 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002455 ASSERT_EQ(OK, mST->updateTexImage());
2456
2457 // Detach from the primary context.
2458 ASSERT_EQ(OK, mST->detachFromContext());
2459
2460 // Attempt to attach to the secondary context.
2461 mST->abandon();
2462
2463 // Attempt to attach to the primary context.
2464 ASSERT_EQ(NO_INIT, mST->attachToContext(SECOND_TEX_ID));
2465}
2466
2467TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002468 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2469
2470 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002471 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002472 ASSERT_EQ(OK, mST->updateTexImage());
2473
2474 // Attempt to attach to the primary context.
2475 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2476}
2477
2478TEST_F(SurfaceTextureMultiContextGLTest,
2479 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002480 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2481
2482 // Attempt to attach to the primary context.
2483 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2484}
2485
2486TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002487 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2488
2489 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002490 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002491 ASSERT_EQ(OK, mST->updateTexImage());
2492
2493 // Detach from the primary context.
2494 ASSERT_EQ(OK, mST->detachFromContext());
2495
2496 // Make there be no current display.
2497 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2498 EGL_NO_CONTEXT));
2499 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2500
2501 // Attempt to attach with no context current.
2502 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2503}
2504
2505TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002506 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2507
2508 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002509 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002510 ASSERT_EQ(OK, mST->updateTexImage());
2511
2512 // Detach from the primary context.
2513 ASSERT_EQ(OK, mST->detachFromContext());
2514
2515 // Attach to the secondary context.
2516 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2517 mSecondEglContext));
2518 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2519
2520 // Detach from the secondary context.
2521 ASSERT_EQ(OK, mST->detachFromContext());
2522
2523 // Attach to the tertiary context.
2524 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2525 mThirdEglContext));
2526 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2527
2528 // Verify that the texture object was created and bound.
2529 GLint texBinding = -1;
2530 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2531 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2532
2533 // Try to use the texture from the tertiary context.
2534 glClearColor(0.2, 0.2, 0.2, 0.2);
2535 glClear(GL_COLOR_BUFFER_BIT);
2536 glViewport(0, 0, 1, 1);
2537 mThirdTextureRenderer->drawTexture();
2538 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2539 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2540}
2541
2542TEST_F(SurfaceTextureMultiContextGLTest,
2543 AttachToContextSucceedsTwiceBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002544 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2545
2546 // Detach from the primary context.
2547 ASSERT_EQ(OK, mST->detachFromContext());
2548
2549 // Attach to the secondary context.
2550 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2551 mSecondEglContext));
2552 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2553
2554 // Detach from the secondary context.
2555 ASSERT_EQ(OK, mST->detachFromContext());
2556
2557 // Attach to the tertiary context.
2558 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2559 mThirdEglContext));
2560 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2561
2562 // Verify that the texture object was created and bound.
2563 GLint texBinding = -1;
2564 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2565 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2566
2567 // Latch the texture contents on the tertiary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002568 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002569 ASSERT_EQ(OK, mST->updateTexImage());
2570
2571 // Try to use the texture from the tertiary context.
2572 glClearColor(0.2, 0.2, 0.2, 0.2);
2573 glClear(GL_COLOR_BUFFER_BIT);
2574 glViewport(0, 0, 1, 1);
2575 mThirdTextureRenderer->drawTexture();
2576 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2577 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
Jamie Gennisce561372012-03-19 18:33:05 -07002578}
2579
Jesse Hall90ed8502012-05-16 23:44:34 -07002580TEST_F(SurfaceTextureMultiContextGLTest,
2581 UpdateTexImageSucceedsForBufferConsumedBeforeDetach) {
2582 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
2583 ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2));
2584
2585 // produce two frames and consume them both on the primary context
2586 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2587 mFW->waitForFrame();
2588 ASSERT_EQ(OK, mST->updateTexImage());
2589
2590 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2591 mFW->waitForFrame();
2592 ASSERT_EQ(OK, mST->updateTexImage());
2593
2594 // produce one more frame
2595 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2596
2597 // Detach from the primary context and attach to the secondary context
2598 ASSERT_EQ(OK, mST->detachFromContext());
2599 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2600 mSecondEglContext));
2601 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2602
2603 // Consume final frame on secondary context
2604 mFW->waitForFrame();
2605 ASSERT_EQ(OK, mST->updateTexImage());
2606}
2607
Jamie Gennis5451d152011-06-08 09:40:45 -07002608} // namespace android