blob: 04f4b55050578865249f2246b7cb0568a19bb2f5 [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(
Jeff Brown9d4e3d22012-08-24 20:00:51 -070086 String8("Test Surface"),
Jamie Gennisd99c0882011-03-10 16:24:46 -080087 getSurfaceWidth(), getSurfaceHeight(),
88 PIXEL_FORMAT_RGB_888, 0);
89
90 ASSERT_TRUE(mSurfaceControl != NULL);
91 ASSERT_TRUE(mSurfaceControl->isValid());
92
Mathias Agopian698c0872011-06-28 19:09:31 -070093 SurfaceComposerClient::openGlobalTransaction();
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070094 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080095 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070096 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennisd99c0882011-03-10 16:24:46 -080097
98 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -070099 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800100 window.get(), NULL);
101 } else {
102 EGLint pbufferAttribs[] = {
103 EGL_WIDTH, getSurfaceWidth(),
104 EGL_HEIGHT, getSurfaceHeight(),
105 EGL_NONE };
106
Jamie Gennis1876d132011-03-17 16:32:52 -0700107 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800108 pbufferAttribs);
109 }
110 ASSERT_EQ(EGL_SUCCESS, eglGetError());
111 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
112
Jamie Gennis1876d132011-03-17 16:32:52 -0700113 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800114 getContextAttribs());
115 ASSERT_EQ(EGL_SUCCESS, eglGetError());
116 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
117
118 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
119 mEglContext));
120 ASSERT_EQ(EGL_SUCCESS, eglGetError());
121
122 EGLint w, h;
123 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
124 ASSERT_EQ(EGL_SUCCESS, eglGetError());
125 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
126 ASSERT_EQ(EGL_SUCCESS, eglGetError());
127 RecordProperty("EglSurfaceWidth", w);
128 RecordProperty("EglSurfaceHeight", h);
129
130 glViewport(0, 0, w, h);
131 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
132 }
133
134 virtual void TearDown() {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700135 // Display the result
Jamie Gennisd99c0882011-03-10 16:24:46 -0800136 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
137 eglSwapBuffers(mEglDisplay, mEglSurface);
138 sleep(mDisplaySecs);
139 }
140
141 if (mComposerClient != NULL) {
142 mComposerClient->dispose();
143 }
144 if (mEglContext != EGL_NO_CONTEXT) {
145 eglDestroyContext(mEglDisplay, mEglContext);
146 }
147 if (mEglSurface != EGL_NO_SURFACE) {
148 eglDestroySurface(mEglDisplay, mEglSurface);
149 }
150 if (mEglDisplay != EGL_NO_DISPLAY) {
151 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
152 EGL_NO_CONTEXT);
153 eglTerminate(mEglDisplay);
154 }
155 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -0700156
157 const ::testing::TestInfo* const testInfo =
158 ::testing::UnitTest::GetInstance()->current_test_info();
159 ALOGV("End test: %s.%s", testInfo->test_case_name(),
160 testInfo->name());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800161 }
162
163 virtual EGLint const* getConfigAttribs() {
164 static EGLint sDefaultConfigAttribs[] = {
165 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
166 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
167 EGL_RED_SIZE, 8,
168 EGL_GREEN_SIZE, 8,
169 EGL_BLUE_SIZE, 8,
170 EGL_ALPHA_SIZE, 8,
171 EGL_DEPTH_SIZE, 16,
172 EGL_STENCIL_SIZE, 8,
173 EGL_NONE };
174
175 return sDefaultConfigAttribs;
176 }
177
178 virtual EGLint const* getContextAttribs() {
179 static EGLint sDefaultContextAttribs[] = {
180 EGL_CONTEXT_CLIENT_VERSION, 2,
181 EGL_NONE };
182
183 return sDefaultContextAttribs;
184 }
185
186 virtual EGLint getSurfaceWidth() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700187 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800188 }
189
190 virtual EGLint getSurfaceHeight() {
Jamie Gennisc8c51522011-06-15 14:24:38 -0700191 return 512;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800192 }
193
Jamie Gennisd99c0882011-03-10 16:24:46 -0800194 ::testing::AssertionResult checkPixel(int x, int y, int r,
Jamie Gennis824efa72011-06-13 13:41:01 -0700195 int g, int b, int a, int tolerance=2) {
Jamie Gennisd99c0882011-03-10 16:24:46 -0800196 GLubyte pixel[4];
197 String8 msg;
198 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
199 GLenum err = glGetError();
200 if (err != GL_NO_ERROR) {
201 msg += String8::format("error reading pixel: %#x", err);
202 while ((err = glGetError()) != GL_NO_ERROR) {
203 msg += String8::format(", %#x", err);
204 }
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;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700675 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
676 &anb));
Jamie Gennisce561372012-03-19 18:33:05 -0700677 ASSERT_TRUE(anb != NULL);
678
679 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisce561372012-03-19 18:33:05 -0700680
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());
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700686 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer(),
687 -1));
Jamie Gennisce561372012-03-19 18:33:05 -0700688}
689
Jamie Gennisd99c0882011-03-10 16:24:46 -0800690TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700691 const int texWidth = 64;
692 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800693
694 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700695 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800696 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
697 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
698
Iliyan Malchev697526b2011-05-01 11:33:26 -0700699 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700700 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
701 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800702 ASSERT_TRUE(anb != NULL);
703
704 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800705
706 // Fill the buffer with the a checkerboard pattern
707 uint8_t* img = NULL;
708 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700709 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800710 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700711 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
712 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800713
714 mST->updateTexImage();
715
716 glClearColor(0.2, 0.2, 0.2, 0.2);
717 glClear(GL_COLOR_BUFFER_BIT);
718
Jamie Gennisc8c51522011-06-15 14:24:38 -0700719 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800720 drawTexture();
721
722 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255));
723 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700724 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255));
725 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800726
Jamie Gennisc8c51522011-06-15 14:24:38 -0700727 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255));
728 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255));
729 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800730 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700731 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800732 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255));
733 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255));
734}
735
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700736TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700737 const int texWidth = 64;
738 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800739
740 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700741 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800742 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
743 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
744
Iliyan Malchev697526b2011-05-01 11:33:26 -0700745 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700746 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
747 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800748 ASSERT_TRUE(anb != NULL);
749
750 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800751
752 // Fill the buffer with the a checkerboard pattern
753 uint8_t* img = NULL;
754 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700755 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800756 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700757 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
758 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800759
760 mST->updateTexImage();
761
762 glClearColor(0.2, 0.2, 0.2, 0.2);
763 glClear(GL_COLOR_BUFFER_BIT);
764
Jamie Gennisc8c51522011-06-15 14:24:38 -0700765 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800766 drawTexture();
767
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700768 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
769 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800770 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
771 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
772
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700773 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
774 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
775 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
776 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
777 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
778 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
779 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800780}
781
782TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700783 const int texWidth = 64;
784 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800785
786 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700787 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800788 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
789 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
790
791 android_native_rect_t crops[] = {
792 {4, 6, 22, 36},
793 {0, 6, 22, 36},
794 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700795 {4, 6, texWidth, 36},
796 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800797 };
798
799 for (int i = 0; i < 5; i++) {
800 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800801 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
802 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800803
804 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
805
Iliyan Malchev697526b2011-05-01 11:33:26 -0700806 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700807 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
808 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800809 ASSERT_TRUE(anb != NULL);
810
811 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800812
813 uint8_t* img = NULL;
814 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700815 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800816 buf->unlock();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800817 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700818 buf->getNativeBuffer(), -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800819
820 mST->updateTexImage();
821
822 glClearColor(0.2, 0.2, 0.2, 0.2);
823 glClear(GL_COLOR_BUFFER_BIT);
824
Jamie Gennisc8c51522011-06-15 14:24:38 -0700825 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800826 drawTexture();
827
828 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
829 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
830 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
831 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
832
833 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
834 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
835 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
836 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
837 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
838 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
839 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
840 }
841}
842
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700843// This test is intended to catch synchronization bugs between the CPU-written
844// and GPU-read buffers.
845TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
846 enum { texWidth = 16 };
847 enum { texHeight = 16 };
848 enum { numFrames = 1024 };
849
850 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
851 ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2));
852 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
853 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
854 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
855 GRALLOC_USAGE_SW_WRITE_OFTEN));
856
857 struct TestPixel {
858 int x;
859 int y;
860 };
861 const TestPixel testPixels[] = {
862 { 4, 11 },
863 { 12, 14 },
864 { 7, 2 },
865 };
866 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
867
868 class ProducerThread : public Thread {
869 public:
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800870 ProducerThread(const sp<ANativeWindow>& anw,
871 const TestPixel* testPixels):
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700872 mANW(anw),
873 mTestPixels(testPixels) {
874 }
875
876 virtual ~ProducerThread() {
877 }
878
879 virtual bool threadLoop() {
880 for (int i = 0; i < numFrames; i++) {
881 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700882 if (native_window_dequeue_buffer_and_wait(mANW.get(),
883 &anb) != NO_ERROR) {
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700884 return false;
885 }
886 if (anb == NULL) {
887 return false;
888 }
889
890 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700891
892 const int yuvTexOffsetY = 0;
893 int stride = buf->getStride();
894 int yuvTexStrideY = stride;
895 int yuvTexOffsetV = yuvTexStrideY * texHeight;
896 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
897 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
898 int yuvTexStrideU = yuvTexStrideV;
899
900 uint8_t* img = NULL;
901 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
902
903 // Gray out all the test pixels first, so we're more likely to
904 // see a failure if GL is still texturing from the buffer we
905 // just dequeued.
906 for (int j = 0; j < numTestPixels; j++) {
907 int x = mTestPixels[j].x;
908 int y = mTestPixels[j].y;
909 uint8_t value = 128;
910 img[y*stride + x] = value;
911 }
912
913 // Fill the buffer with gray.
914 for (int y = 0; y < texHeight; y++) {
915 for (int x = 0; x < texWidth; x++) {
916 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
917 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
918 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
919 }
920 }
921
922 // Set the test pixels to either white or black.
923 for (int j = 0; j < numTestPixels; j++) {
924 int x = mTestPixels[j].x;
925 int y = mTestPixels[j].y;
926 uint8_t value = 0;
927 if (j == (i % numTestPixels)) {
928 value = 255;
929 }
930 img[y*stride + x] = value;
931 }
932
933 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700934 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), -1)
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700935 != NO_ERROR) {
936 return false;
937 }
938 }
939 return false;
940 }
941
942 sp<ANativeWindow> mANW;
943 const TestPixel* mTestPixels;
944 };
945
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700946 sp<Thread> pt(new ProducerThread(mANW, testPixels));
947 pt->run();
948
949 glViewport(0, 0, texWidth, texHeight);
950
951 glClearColor(0.2, 0.2, 0.2, 0.2);
952 glClear(GL_COLOR_BUFFER_BIT);
953
954 // We wait for the first two frames up front so that the producer will be
955 // likely to dequeue the buffer that's currently being textured from.
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700956 mFW->waitForFrame();
957 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700958
959 for (int i = 0; i < numFrames; i++) {
960 SCOPED_TRACE(String8::format("frame %d", i).string());
961
962 // We must wait for each frame to come in because if we ever do an
963 // updateTexImage call that doesn't consume a newly available buffer
964 // then the producer and consumer will get out of sync, which will cause
965 // a deadlock.
966 if (i > 1) {
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700967 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700968 }
969 mST->updateTexImage();
970 drawTexture();
971
972 for (int j = 0; j < numTestPixels; j++) {
973 int x = testPixels[j].x;
974 int y = testPixels[j].y;
975 uint8_t value = 0;
976 if (j == (i % numTestPixels)) {
977 // We must y-invert the texture coords
978 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
979 } else {
980 // We must y-invert the texture coords
981 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
982 }
983 }
984 }
985
986 pt->requestExitAndWait();
987}
988
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700989TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700990 const int texWidth = 64;
991 const int texHeight = 66;
992
993 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
994 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
995 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
996 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
997
Jamie Gennisce561372012-03-19 18:33:05 -0700998 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -0700999
1000 mST->updateTexImage();
1001
1002 glClearColor(0.2, 0.2, 0.2, 0.2);
1003 glClear(GL_COLOR_BUFFER_BIT);
1004
Jamie Gennisc8c51522011-06-15 14:24:38 -07001005 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001006 drawTexture();
1007
1008 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
1009 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001010 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
1011 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001012
1013 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001014 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -07001015 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001016 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
1017 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001018 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001019 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001020 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
1021 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
1022 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001023 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001024 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
1025 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -07001026 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001027 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -07001028 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
1029}
1030
Jamie Gennis1f8e09f2011-07-19 17:58:43 -07001031TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -07001032 const int texWidth = 64;
1033 const int texHeight = 64;
1034
1035 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
1036 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
1037 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
1038 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
1039
Jamie Gennisce561372012-03-19 18:33:05 -07001040 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -07001041
1042 mST->updateTexImage();
1043
1044 glClearColor(0.2, 0.2, 0.2, 0.2);
1045 glClear(GL_COLOR_BUFFER_BIT);
1046
Jamie Gennisc8c51522011-06-15 14:24:38 -07001047 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -07001048 drawTexture();
1049
1050 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
1051 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
1052 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
1053 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
1054
1055 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
1056 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
1057 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
1058 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
1059 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
1060 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
1061 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
1062 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
1063 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
1064 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
1065 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
1066 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
1067 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
1068 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
1069 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
1070 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
1071}
1072
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001073// Tests if SurfaceTexture and BufferQueue are robust enough
1074// to handle a special case where updateTexImage is called
1075// in the middle of disconnect. This ordering is enforced
1076// by blocking in the disconnect callback.
1077TEST_F(SurfaceTextureGLTest, DisconnectStressTest) {
1078
1079 class ProducerThread : public Thread {
1080 public:
1081 ProducerThread(const sp<ANativeWindow>& anw):
1082 mANW(anw) {
1083 }
1084
1085 virtual ~ProducerThread() {
1086 }
1087
1088 virtual bool threadLoop() {
1089 ANativeWindowBuffer* anb;
1090
1091 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL);
1092
1093 for (int numFrames =0 ; numFrames < 2; numFrames ++) {
1094
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001095 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1096 &anb) != NO_ERROR) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001097 return false;
1098 }
1099 if (anb == NULL) {
1100 return false;
1101 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001102 if (mANW->queueBuffer(mANW.get(), anb, -1)
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001103 != 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
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001150 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1151 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001152
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001153 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1154 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001155
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 Gennisd8e812c2012-06-13 16:32:25 -07001166 EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1167 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -07001168
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));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001196 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1197 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
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));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001230 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1231 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
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));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001241 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1242 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
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));
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001253 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1254 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
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
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001281 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1282 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001283 return false;
1284 }
1285 if (anb == NULL) {
1286 return false;
1287 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001288 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001289 != NO_ERROR) {
1290 return false;
1291 }
1292
1293 // Frame 2
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001294 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1295 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001296 return false;
1297 }
1298 if (anb == NULL) {
1299 return false;
1300 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001301 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001302 != NO_ERROR) {
1303 return false;
1304 }
1305
1306 // Frame 3 - error expected
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001307 mDequeueError = native_window_dequeue_buffer_and_wait(mANW.get(),
1308 &anb);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001309 return false;
1310 }
1311
1312 status_t getDequeueError() {
1313 Mutex::Autolock lock(mMutex);
1314 return mDequeueError;
1315 }
1316
1317 private:
1318 sp<ANativeWindow> mANW;
1319 status_t mDequeueError;
1320 Mutex mMutex;
1321 };
1322
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001323 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1324 ASSERT_EQ(OK, mST->setBufferCountServer(2));
1325
1326 sp<Thread> pt(new ProducerThread(mANW));
1327 pt->run();
1328
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001329 mFW->waitForFrame();
1330 mFW->waitForFrame();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001331
1332 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1333 // block waiting for a buffer to become available.
1334 usleep(100000);
1335
1336 mST->abandon();
1337
1338 pt->requestExitAndWait();
1339 ASSERT_EQ(NO_INIT,
1340 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1341}
1342
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001343TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1344 int texHeight = 16;
1345 ANativeWindowBuffer* anb;
1346
1347 GLint maxTextureSize;
1348 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1349
1350 // make sure it works with small textures
1351 mST->setDefaultBufferSize(16, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001352 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1353 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001354 EXPECT_EQ(16, anb->width);
1355 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001356 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001357 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1358
1359 // make sure it works with GL_MAX_TEXTURE_SIZE
1360 mST->setDefaultBufferSize(maxTextureSize, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001361 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1362 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001363 EXPECT_EQ(maxTextureSize, anb->width);
1364 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001365 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001366 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1367
1368 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1369 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001370 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1371 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001372 EXPECT_EQ(maxTextureSize+1, anb->width);
1373 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001374 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001375 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1376}
1377
Jamie Gennis5451d152011-06-08 09:40:45 -07001378/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001379 * This test fixture is for testing GL -> GL texture streaming. It creates an
1380 * EGLSurface and an EGLContext for the image producer to use.
1381 */
1382class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1383protected:
1384 SurfaceTextureGLToGLTest():
1385 mProducerEglSurface(EGL_NO_SURFACE),
1386 mProducerEglContext(EGL_NO_CONTEXT) {
1387 }
1388
1389 virtual void SetUp() {
1390 SurfaceTextureGLTest::SetUp();
1391
Jamie Gennisce561372012-03-19 18:33:05 -07001392 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001393 mANW.get(), NULL);
1394 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1395 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1396
Jamie Gennisce561372012-03-19 18:33:05 -07001397 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001398 EGL_NO_CONTEXT, getContextAttribs());
1399 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1400 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1401 }
1402
1403 virtual void TearDown() {
1404 if (mProducerEglContext != EGL_NO_CONTEXT) {
1405 eglDestroyContext(mEglDisplay, mProducerEglContext);
1406 }
1407 if (mProducerEglSurface != EGL_NO_SURFACE) {
1408 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1409 }
1410 SurfaceTextureGLTest::TearDown();
1411 }
1412
1413 EGLSurface mProducerEglSurface;
1414 EGLContext mProducerEglContext;
1415};
1416
1417TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1418 const int texWidth = 64;
1419 const int texHeight = 64;
1420
1421 mST->setDefaultBufferSize(texWidth, texHeight);
1422
1423 // Do the producer side of things
1424 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1425 mProducerEglSurface, mProducerEglContext));
1426 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1427
1428 // This is needed to ensure we pick up a buffer of the correct size.
1429 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1430
1431 glClearColor(0.6, 0.6, 0.6, 0.6);
1432 glClear(GL_COLOR_BUFFER_BIT);
1433
1434 glEnable(GL_SCISSOR_TEST);
1435 glScissor(4, 4, 4, 4);
1436 glClearColor(1.0, 0.0, 0.0, 1.0);
1437 glClear(GL_COLOR_BUFFER_BIT);
1438
1439 glScissor(24, 48, 4, 4);
1440 glClearColor(0.0, 1.0, 0.0, 1.0);
1441 glClear(GL_COLOR_BUFFER_BIT);
1442
1443 glScissor(37, 17, 4, 4);
1444 glClearColor(0.0, 0.0, 1.0, 1.0);
1445 glClear(GL_COLOR_BUFFER_BIT);
1446
1447 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1448
1449 // Do the consumer side of things
1450 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1451 mEglContext));
1452 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1453
1454 glDisable(GL_SCISSOR_TEST);
1455
1456 mST->updateTexImage(); // Skip the first frame, which was empty
1457 mST->updateTexImage();
1458
1459 glClearColor(0.2, 0.2, 0.2, 0.2);
1460 glClear(GL_COLOR_BUFFER_BIT);
1461
1462 glViewport(0, 0, texWidth, texHeight);
1463 drawTexture();
1464
1465 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1466 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1467 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1468 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1469
1470 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1471 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1472 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1473 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1474 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1475 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1476 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1477 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1478 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1479 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1480 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1481 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1482 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1483 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1484 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1485 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1486}
1487
1488TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001489 sp<GraphicBuffer> buffers[2];
1490
Jamie Gennise3603d72011-11-19 21:20:17 -08001491 // This test requires async mode to run on a single thread.
1492 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1493 mProducerEglSurface, mProducerEglContext));
1494 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1495 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1496 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1497
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001498 for (int i = 0; i < 2; i++) {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001499 // Produce a frame
1500 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1501 mProducerEglSurface, mProducerEglContext));
1502 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1503 glClear(GL_COLOR_BUFFER_BIT);
1504 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1505
1506 // Consume a frame
1507 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1508 mEglContext));
1509 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001510 mFW->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001511 mST->updateTexImage();
1512 buffers[i] = mST->getCurrentBuffer();
1513 }
1514
1515 // Destroy the GL texture object to release its ref on buffers[2].
1516 GLuint texID = TEX_ID;
1517 glDeleteTextures(1, &texID);
1518
1519 // Destroy the EGLSurface
1520 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1521 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001522 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001523
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001524 // This test should have the only reference to buffer 0.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001525 EXPECT_EQ(1, buffers[0]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001526
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001527 // The SurfaceTexture should hold a single reference to buffer 1 in its
1528 // mCurrentBuffer member. All of the references in the slots should have
1529 // been released.
1530 EXPECT_EQ(2, buffers[1]->getStrongCount());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001531}
1532
1533TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1534 sp<GraphicBuffer> buffers[3];
1535
Jamie Gennise3603d72011-11-19 21:20:17 -08001536 // This test requires async mode to run on a single thread.
1537 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1538 mProducerEglSurface, mProducerEglContext));
1539 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1540 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1541 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1542
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001543 for (int i = 0; i < 3; i++) {
1544 // Produce a frame
1545 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1546 mProducerEglSurface, mProducerEglContext));
1547 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1548 glClear(GL_COLOR_BUFFER_BIT);
1549 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1550 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1551
1552 // Consume a frame
1553 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1554 mEglContext));
1555 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001556 mFW->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001557 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1558 buffers[i] = mST->getCurrentBuffer();
1559 }
1560
1561 // Abandon the SurfaceTexture, releasing the ref that the SurfaceTexture has
1562 // on buffers[2].
1563 mST->abandon();
1564
1565 // Destroy the GL texture object to release its ref on buffers[2].
1566 GLuint texID = TEX_ID;
1567 glDeleteTextures(1, &texID);
1568
1569 // Destroy the EGLSurface.
1570 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1571 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001572 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001573
1574 EXPECT_EQ(1, buffers[0]->getStrongCount());
1575 EXPECT_EQ(1, buffers[1]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001576
1577 // Depending on how lazily the GL driver dequeues buffers, we may end up
1578 // with either two or three total buffers. If there are three, make sure
1579 // the last one was properly down-ref'd.
1580 if (buffers[2] != buffers[0]) {
1581 EXPECT_EQ(1, buffers[2]->getStrongCount());
1582 }
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001583}
1584
Jamie Gennis59769462011-11-19 18:04:43 -08001585TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) {
1586 // This test requires 3 buffers to run on a single thread.
1587 mST->setBufferCountServer(3);
1588
1589 ASSERT_TRUE(mST->isSynchronousMode());
1590
1591 for (int i = 0; i < 10; i++) {
1592 // Produce a frame
1593 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1594 mProducerEglSurface, mProducerEglContext));
1595 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1596 glClear(GL_COLOR_BUFFER_BIT);
1597 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1598 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1599
1600 // Consume a frame
1601 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1602 mEglContext));
1603 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1604 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1605 }
1606
1607 ASSERT_TRUE(mST->isSynchronousMode());
1608}
1609
Jamie Gennisc2c38022012-04-11 17:20:03 -07001610TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
1611 enum { texWidth = 64 };
1612 enum { texHeight = 64 };
1613
1614 // Set the user buffer size.
1615 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1616
1617 // Do the producer side of things
1618 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1619 mProducerEglSurface, mProducerEglContext));
1620 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1621
1622 // This is needed to ensure we pick up a buffer of the correct size.
1623 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1624
1625 glClearColor(0.6, 0.6, 0.6, 0.6);
1626 glClear(GL_COLOR_BUFFER_BIT);
1627
1628 glEnable(GL_SCISSOR_TEST);
1629 glScissor(4, 4, 1, 1);
1630 glClearColor(1.0, 0.0, 0.0, 1.0);
1631 glClear(GL_COLOR_BUFFER_BIT);
1632
1633 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1634
1635 // Do the consumer side of things
1636 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1637 mEglContext));
1638 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1639
1640 glDisable(GL_SCISSOR_TEST);
1641
1642 mST->updateTexImage(); // Skip the first frame, which was empty
1643 mST->updateTexImage();
1644
1645 glClearColor(0.2, 0.2, 0.2, 0.2);
1646 glClear(GL_COLOR_BUFFER_BIT);
1647
1648 glViewport(0, 0, texWidth, texHeight);
1649 drawTexture();
1650
1651 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1652 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1653 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1654 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1655
1656 EXPECT_TRUE(checkPixel( 4, 4, 255, 0, 0, 255));
1657 EXPECT_TRUE(checkPixel( 5, 5, 153, 153, 153, 153));
1658 EXPECT_TRUE(checkPixel( 3, 3, 153, 153, 153, 153));
1659 EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153));
1660 EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153));
1661}
1662
1663TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
1664 enum { texWidth = 64 };
1665 enum { texHeight = 16 };
1666
1667 // Set the transform hint.
1668 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1669
1670 // Set the user buffer size.
1671 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight);
1672
1673 // Do the producer side of things
1674 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1675 mProducerEglSurface, mProducerEglContext));
1676 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1677
1678 // This is needed to ensure we pick up a buffer of the correct size and the
1679 // new rotation hint.
1680 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1681
1682 glClearColor(0.6, 0.6, 0.6, 0.6);
1683 glClear(GL_COLOR_BUFFER_BIT);
1684
1685 glEnable(GL_SCISSOR_TEST);
1686 glScissor(24, 4, 1, 1);
1687 glClearColor(1.0, 0.0, 0.0, 1.0);
1688 glClear(GL_COLOR_BUFFER_BIT);
1689
1690 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1691
1692 // Do the consumer side of things
1693 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1694 mEglContext));
1695 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1696
1697 glDisable(GL_SCISSOR_TEST);
1698
1699 mST->updateTexImage(); // Skip the first frame, which was empty
1700 mST->updateTexImage();
1701
1702 glClearColor(0.2, 0.2, 0.2, 0.2);
1703 glClear(GL_COLOR_BUFFER_BIT);
1704
1705 glViewport(0, 0, texWidth, texHeight);
1706 drawTexture();
1707
1708 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1709 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1710 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1711 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1712
1713 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1714 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1715 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1716 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1717 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1718}
1719
1720TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
1721 enum { texWidth = 64 };
1722 enum { texHeight = 16 };
1723
1724 // Set the transform hint.
1725 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90);
1726
1727 // Set the default buffer size.
1728 mST->setDefaultBufferSize(texWidth, texHeight);
1729
1730 // Do the producer side of things
1731 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1732 mProducerEglSurface, mProducerEglContext));
1733 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1734
1735 // This is needed to ensure we pick up a buffer of the correct size and the
1736 // new rotation hint.
1737 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1738
1739 glClearColor(0.6, 0.6, 0.6, 0.6);
1740 glClear(GL_COLOR_BUFFER_BIT);
1741
1742 glEnable(GL_SCISSOR_TEST);
1743 glScissor(24, 4, 1, 1);
1744 glClearColor(1.0, 0.0, 0.0, 1.0);
1745 glClear(GL_COLOR_BUFFER_BIT);
1746
1747 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1748
1749 // Do the consumer side of things
1750 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1751 mEglContext));
1752 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1753
1754 glDisable(GL_SCISSOR_TEST);
1755
1756 mST->updateTexImage(); // Skip the first frame, which was empty
1757 mST->updateTexImage();
1758
1759 glClearColor(0.2, 0.2, 0.2, 0.2);
1760 glClear(GL_COLOR_BUFFER_BIT);
1761
1762 glViewport(0, 0, texWidth, texHeight);
1763 drawTexture();
1764
1765 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1766 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1767 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153));
1768 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153));
1769
1770 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255));
1771 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153));
1772 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153));
1773 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153));
1774 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153));
1775}
1776
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001777/*
1778 * This test fixture is for testing GL -> GL texture streaming from one thread
1779 * to another. It contains functionality to create a producer thread that will
1780 * perform GL rendering to an ANativeWindow that feeds frames to a
1781 * SurfaceTexture. Additionally it supports interlocking the producer and
1782 * consumer threads so that a specific sequence of calls can be
1783 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07001784 *
1785 * The intended usage is as follows:
1786 *
1787 * TEST_F(...) {
1788 * class PT : public ProducerThread {
1789 * virtual void render() {
1790 * ...
1791 * swapBuffers();
1792 * }
1793 * };
1794 *
1795 * runProducerThread(new PT());
1796 *
1797 * // The order of these calls will vary from test to test and may include
1798 * // multiple frames and additional operations (e.g. GL rendering from the
1799 * // texture).
1800 * fc->waitForFrame();
1801 * mST->updateTexImage();
1802 * fc->finishFrame();
1803 * }
1804 *
1805 */
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001806class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07001807protected:
1808
1809 // ProducerThread is an abstract base class to simplify the creation of
1810 // OpenGL ES frame producer threads.
1811 class ProducerThread : public Thread {
1812 public:
1813 virtual ~ProducerThread() {
1814 }
1815
1816 void setEglObjects(EGLDisplay producerEglDisplay,
1817 EGLSurface producerEglSurface,
1818 EGLContext producerEglContext) {
1819 mProducerEglDisplay = producerEglDisplay;
1820 mProducerEglSurface = producerEglSurface;
1821 mProducerEglContext = producerEglContext;
1822 }
1823
1824 virtual bool threadLoop() {
1825 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
1826 mProducerEglSurface, mProducerEglContext);
1827 render();
1828 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1829 EGL_NO_CONTEXT);
1830 return false;
1831 }
1832
1833 protected:
1834 virtual void render() = 0;
1835
1836 void swapBuffers() {
1837 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
1838 }
1839
1840 EGLDisplay mProducerEglDisplay;
1841 EGLSurface mProducerEglSurface;
1842 EGLContext mProducerEglContext;
1843 };
1844
1845 // FrameCondition is a utility class for interlocking between the producer
1846 // and consumer threads. The FrameCondition object should be created and
1847 // destroyed in the consumer thread only. The consumer thread should set
1848 // the FrameCondition as the FrameAvailableListener of the SurfaceTexture,
1849 // and should call both waitForFrame and finishFrame once for each expected
1850 // frame.
1851 //
1852 // This interlocking relies on the fact that onFrameAvailable gets called
1853 // synchronously from SurfaceTexture::queueBuffer.
1854 class FrameCondition : public SurfaceTexture::FrameAvailableListener {
1855 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001856 FrameCondition():
1857 mFrameAvailable(false),
1858 mFrameFinished(false) {
1859 }
1860
Jamie Gennis5451d152011-06-08 09:40:45 -07001861 // waitForFrame waits for the next frame to arrive. This should be
1862 // called from the consumer thread once for every frame expected by the
1863 // test.
1864 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001865 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001866 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001867 while (!mFrameAvailable) {
1868 mFrameAvailableCondition.wait(mMutex);
1869 }
1870 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01001871 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001872 }
1873
1874 // Allow the producer to return from its swapBuffers call and continue
1875 // on to produce the next frame. This should be called by the consumer
1876 // thread once for every frame expected by the test.
1877 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001878 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001879 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001880 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001881 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01001882 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001883 }
1884
1885 // This should be called by SurfaceTexture on the producer thread.
1886 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001887 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001888 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001889 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001890 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001891 while (!mFrameFinished) {
1892 mFrameFinishCondition.wait(mMutex);
1893 }
1894 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01001895 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07001896 }
1897
1898 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001899 bool mFrameAvailable;
1900 bool mFrameFinished;
1901
Jamie Gennis5451d152011-06-08 09:40:45 -07001902 Mutex mMutex;
1903 Condition mFrameAvailableCondition;
1904 Condition mFrameFinishCondition;
1905 };
1906
Jamie Gennis5451d152011-06-08 09:40:45 -07001907 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001908 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07001909 mFC = new FrameCondition();
1910 mST->setFrameAvailableListener(mFC);
1911 }
1912
1913 virtual void TearDown() {
1914 if (mProducerThread != NULL) {
1915 mProducerThread->requestExitAndWait();
1916 }
Jamie Gennis5451d152011-06-08 09:40:45 -07001917 mProducerThread.clear();
1918 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001919 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07001920 }
1921
1922 void runProducerThread(const sp<ProducerThread> producerThread) {
1923 ASSERT_TRUE(mProducerThread == NULL);
1924 mProducerThread = producerThread;
1925 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
1926 mProducerEglContext);
1927 producerThread->run();
1928 }
1929
Jamie Gennis5451d152011-06-08 09:40:45 -07001930 sp<ProducerThread> mProducerThread;
1931 sp<FrameCondition> mFC;
1932};
1933
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001934TEST_F(SurfaceTextureGLThreadToGLTest,
1935 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001936 class PT : public ProducerThread {
1937 virtual void render() {
1938 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1939 glClear(GL_COLOR_BUFFER_BIT);
1940 swapBuffers();
1941 }
1942 };
1943
1944 runProducerThread(new PT());
1945
1946 mFC->waitForFrame();
1947 mST->updateTexImage();
1948 mFC->finishFrame();
1949
1950 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08001951}
Jamie Gennis5451d152011-06-08 09:40:45 -07001952
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001953TEST_F(SurfaceTextureGLThreadToGLTest,
1954 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001955 class PT : public ProducerThread {
1956 virtual void render() {
1957 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1958 glClear(GL_COLOR_BUFFER_BIT);
1959 swapBuffers();
1960 }
1961 };
1962
1963 runProducerThread(new PT());
1964
1965 mFC->waitForFrame();
1966 mFC->finishFrame();
1967 mST->updateTexImage();
1968
1969 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1970}
1971
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001972TEST_F(SurfaceTextureGLThreadToGLTest,
1973 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001974 enum { NUM_ITERATIONS = 1024 };
1975
1976 class PT : public ProducerThread {
1977 virtual void render() {
1978 for (int i = 0; i < NUM_ITERATIONS; i++) {
1979 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1980 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001981 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001982 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001983 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001984 }
1985 }
1986 };
1987
1988 runProducerThread(new PT());
1989
1990 for (int i = 0; i < NUM_ITERATIONS; i++) {
1991 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01001992 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001993 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01001994 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001995 mFC->finishFrame();
1996
1997 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1998 }
1999}
2000
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002001TEST_F(SurfaceTextureGLThreadToGLTest,
2002 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07002003 enum { NUM_ITERATIONS = 1024 };
2004
2005 class PT : public ProducerThread {
2006 virtual void render() {
2007 for (int i = 0; i < NUM_ITERATIONS; i++) {
2008 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2009 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002010 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002011 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002012 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07002013 }
2014 }
2015 };
2016
2017 runProducerThread(new PT());
2018
2019 for (int i = 0; i < NUM_ITERATIONS; i++) {
2020 mFC->waitForFrame();
2021 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002022 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002023 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01002024 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07002025
2026 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
2027 }
2028}
2029
Jamie Gennis6e502192011-07-21 14:31:31 -07002030// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08002031TEST_F(SurfaceTextureGLThreadToGLTest,
2032 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07002033 enum { NUM_ITERATIONS = 64 };
2034
2035 class PT : public ProducerThread {
2036 virtual void render() {
2037 for (int i = 0; i < NUM_ITERATIONS; i++) {
2038 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2039 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01002040 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002041 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01002042 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07002043 }
2044 }
2045 };
2046
2047 ASSERT_EQ(OK, mST->setSynchronousMode(true));
2048 ASSERT_EQ(OK, mST->setBufferCountServer(2));
2049
2050 runProducerThread(new PT());
2051
2052 // Allow three frames to be rendered and queued before starting the
2053 // rendering in this thread. For the latter two frames we don't call
2054 // updateTexImage so the next dequeue from the producer thread will block
2055 // waiting for a frame to become available.
2056 mFC->waitForFrame();
2057 mFC->finishFrame();
2058
2059 // We must call updateTexImage to consume the first frame so that the
2060 // SurfaceTexture is able to reduce the buffer count to 2. This is because
2061 // the GL driver may dequeue a buffer when the EGLSurface is created, and
2062 // that happens before we call setBufferCountServer. It's possible that the
2063 // driver does not dequeue a buffer at EGLSurface creation time, so we
2064 // cannot rely on this to cause the second dequeueBuffer call to block.
2065 mST->updateTexImage();
2066
2067 mFC->waitForFrame();
2068 mFC->finishFrame();
2069 mFC->waitForFrame();
2070 mFC->finishFrame();
2071
2072 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
2073 // block waiting for a buffer to become available.
2074 usleep(100000);
2075
2076 // Render and present a number of images. This thread should not be blocked
2077 // by the fact that the producer thread is blocking in dequeue.
2078 for (int i = 0; i < NUM_ITERATIONS; i++) {
2079 glClear(GL_COLOR_BUFFER_BIT);
2080 eglSwapBuffers(mEglDisplay, mEglSurface);
2081 }
2082
2083 // Consume the two pending buffers to unblock the producer thread.
2084 mST->updateTexImage();
2085 mST->updateTexImage();
2086
2087 // Consume the remaining buffers from the producer thread.
2088 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
2089 mFC->waitForFrame();
2090 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01002091 ALOGV("+updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002092 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01002093 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07002094 }
2095}
2096
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002097class SurfaceTextureFBOTest : public SurfaceTextureGLTest {
2098protected:
2099
2100 virtual void SetUp() {
2101 SurfaceTextureGLTest::SetUp();
2102
2103 glGenFramebuffers(1, &mFbo);
2104 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2105
2106 glGenTextures(1, &mFboTex);
2107 glBindTexture(GL_TEXTURE_2D, mFboTex);
2108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getSurfaceWidth(),
2109 getSurfaceHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2110 glBindTexture(GL_TEXTURE_2D, 0);
2111 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2112
2113 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2114 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
2115 GL_TEXTURE_2D, mFboTex, 0);
2116 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2117 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2118 }
2119
2120 virtual void TearDown() {
2121 SurfaceTextureGLTest::TearDown();
2122
2123 glDeleteTextures(1, &mFboTex);
2124 glDeleteFramebuffers(1, &mFbo);
2125 }
2126
2127 GLuint mFbo;
2128 GLuint mFboTex;
2129};
2130
2131// This test is intended to verify that proper synchronization is done when
2132// rendering into an FBO.
2133TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) {
2134 const int texWidth = 64;
2135 const int texHeight = 64;
2136
2137 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
2138 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
2139 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
2140 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
2141
2142 android_native_buffer_t* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002143 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2144 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002145 ASSERT_TRUE(anb != NULL);
2146
2147 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002148
2149 // Fill the buffer with green
2150 uint8_t* img = NULL;
2151 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
2152 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255,
2153 0, 255);
2154 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002155 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
2156 -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002157
2158 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2159
2160 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2161 drawTexture();
2162 glBindFramebuffer(GL_FRAMEBUFFER, 0);
2163
2164 for (int i = 0; i < 4; i++) {
2165 SCOPED_TRACE(String8::format("frame %d", i).string());
2166
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002167 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2168 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002169 ASSERT_TRUE(anb != NULL);
2170
2171 buf = new GraphicBuffer(anb, false);
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002172
2173 // Fill the buffer with red
2174 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
2175 (void**)(&img)));
2176 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0,
2177 0, 255);
2178 ASSERT_EQ(NO_ERROR, buf->unlock());
2179 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -07002180 buf->getNativeBuffer(), -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002181
2182 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
2183
2184 drawTexture();
2185
2186 EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255));
2187 }
2188
2189 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
2190
2191 EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255));
2192}
2193
Jamie Gennisce561372012-03-19 18:33:05 -07002194class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest {
2195protected:
Jamie Gennis74bed552012-03-28 19:05:54 -07002196 enum { SECOND_TEX_ID = 123 };
2197 enum { THIRD_TEX_ID = 456 };
2198
Jamie Gennisce561372012-03-19 18:33:05 -07002199 SurfaceTextureMultiContextGLTest():
2200 mSecondEglContext(EGL_NO_CONTEXT) {
2201 }
2202
2203 virtual void SetUp() {
2204 SurfaceTextureGLTest::SetUp();
2205
Jamie Gennis74bed552012-03-28 19:05:54 -07002206 // Set up the secondary context and texture renderer.
Jamie Gennisce561372012-03-19 18:33:05 -07002207 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2208 EGL_NO_CONTEXT, getContextAttribs());
2209 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2210 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext);
Jamie Gennis74bed552012-03-28 19:05:54 -07002211
2212 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2213 mSecondEglContext));
2214 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2215 mSecondTextureRenderer = new TextureRenderer(SECOND_TEX_ID, mST);
2216 ASSERT_NO_FATAL_FAILURE(mSecondTextureRenderer->SetUp());
2217
2218 // Set up the tertiary context and texture renderer.
2219 mThirdEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2220 EGL_NO_CONTEXT, getContextAttribs());
2221 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2222 ASSERT_NE(EGL_NO_CONTEXT, mThirdEglContext);
2223
2224 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2225 mThirdEglContext));
2226 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2227 mThirdTextureRenderer = new TextureRenderer(THIRD_TEX_ID, mST);
2228 ASSERT_NO_FATAL_FAILURE(mThirdTextureRenderer->SetUp());
2229
2230 // Switch back to the primary context to start the tests.
2231 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2232 mEglContext));
Jamie Gennisce561372012-03-19 18:33:05 -07002233 }
2234
2235 virtual void TearDown() {
Jamie Gennis74bed552012-03-28 19:05:54 -07002236 if (mThirdEglContext != EGL_NO_CONTEXT) {
2237 eglDestroyContext(mEglDisplay, mThirdEglContext);
2238 }
Jamie Gennisce561372012-03-19 18:33:05 -07002239 if (mSecondEglContext != EGL_NO_CONTEXT) {
2240 eglDestroyContext(mEglDisplay, mSecondEglContext);
2241 }
2242 SurfaceTextureGLTest::TearDown();
2243 }
2244
2245 EGLContext mSecondEglContext;
Jamie Gennis74bed552012-03-28 19:05:54 -07002246 sp<TextureRenderer> mSecondTextureRenderer;
2247
2248 EGLContext mThirdEglContext;
2249 sp<TextureRenderer> mThirdTextureRenderer;
Jamie Gennisce561372012-03-19 18:33:05 -07002250};
2251
2252TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) {
Jamie Gennisce561372012-03-19 18:33:05 -07002253 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2254
2255 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002256 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002257 ASSERT_EQ(OK, mST->updateTexImage());
Jamie Gennisce561372012-03-19 18:33:05 -07002258
2259 // Attempt to latch the texture on the secondary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07002260 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
Jamie Gennisce561372012-03-19 18:33:05 -07002261 mSecondEglContext));
2262 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -07002263 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2264}
2265
2266TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002267 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2268
2269 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002270 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002271 ASSERT_EQ(OK, mST->updateTexImage());
2272
2273 // Detach from the primary context.
2274 ASSERT_EQ(OK, mST->detachFromContext());
2275
2276 // Check that the GL texture was deleted.
2277 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2278}
2279
2280TEST_F(SurfaceTextureMultiContextGLTest,
2281 DetachFromContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002282 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2283
2284 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002285 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002286 ASSERT_EQ(OK, mST->updateTexImage());
2287
2288 // Detach from the primary context.
2289 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2290 ASSERT_EQ(OK, mST->detachFromContext());
2291
2292 // Check that the GL texture was deleted.
2293 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
2294}
2295
2296TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002297 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2298
2299 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002300 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002301 ASSERT_EQ(OK, mST->updateTexImage());
2302
2303 // Attempt to detach from the primary context.
2304 mST->abandon();
2305 ASSERT_EQ(NO_INIT, mST->detachFromContext());
2306}
2307
2308TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002309 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2310
2311 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002312 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002313 ASSERT_EQ(OK, mST->updateTexImage());
2314
2315 // Detach from the primary context.
2316 ASSERT_EQ(OK, mST->detachFromContext());
2317
2318 // Attempt to detach from the primary context again.
2319 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2320}
2321
2322TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002323 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2324
2325 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002326 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002327 ASSERT_EQ(OK, mST->updateTexImage());
2328
2329 // Make there be no current display.
2330 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2331 EGL_NO_CONTEXT));
2332 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2333
2334 // Attempt to detach from the primary context.
2335 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2336}
2337
2338TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002339 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2340
2341 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002342 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002343 ASSERT_EQ(OK, mST->updateTexImage());
2344
2345 // Make current context be incorrect.
2346 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2347 mSecondEglContext));
2348 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2349
2350 // Attempt to detach from the primary context.
2351 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
2352}
2353
2354TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002355 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2356
2357 // Detach from the primary context.
2358 ASSERT_EQ(OK, mST->detachFromContext());
2359
2360 // Attempt to latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002361 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002362 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2363}
2364
2365TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002366 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2367
2368 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002369 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002370 ASSERT_EQ(OK, mST->updateTexImage());
2371
2372 // Detach from the primary context.
2373 ASSERT_EQ(OK, mST->detachFromContext());
2374
2375 // Attach to the secondary context.
2376 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2377 mSecondEglContext));
2378 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2379
2380 // Verify that the texture object was created and bound.
2381 GLint texBinding = -1;
2382 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2383 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2384
2385 // Try to use the texture from the secondary context.
2386 glClearColor(0.2, 0.2, 0.2, 0.2);
2387 glClear(GL_COLOR_BUFFER_BIT);
2388 glViewport(0, 0, 1, 1);
2389 mSecondTextureRenderer->drawTexture();
2390 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2391 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2392}
2393
2394TEST_F(SurfaceTextureMultiContextGLTest,
2395 AttachToContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002396 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2397
2398 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002399 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002400 ASSERT_EQ(OK, mST->updateTexImage());
2401
2402 // Detach from the primary context.
2403 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2404 ASSERT_EQ(OK, mST->detachFromContext());
2405
2406 // Attach to the secondary context.
2407 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2408 mSecondEglContext));
2409 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2410
2411 // Verify that the texture object was created and bound.
2412 GLint texBinding = -1;
2413 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2414 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2415
2416 // Try to use the texture from the secondary context.
2417 glClearColor(0.2, 0.2, 0.2, 0.2);
2418 glClear(GL_COLOR_BUFFER_BIT);
2419 glViewport(0, 0, 1, 1);
2420 mSecondTextureRenderer->drawTexture();
2421 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2422 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2423}
2424
2425TEST_F(SurfaceTextureMultiContextGLTest,
2426 AttachToContextSucceedsBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002427 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2428
2429 // Detach from the primary context.
2430 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2431 ASSERT_EQ(OK, mST->detachFromContext());
2432
2433 // Attach to the secondary context.
2434 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2435 mSecondEglContext));
2436 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2437
2438 // Verify that the texture object was created and bound.
2439 GLint texBinding = -1;
2440 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2441 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2442
2443 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002444 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002445 ASSERT_EQ(OK, mST->updateTexImage());
2446
2447 // Try to use the texture from the secondary context.
2448 glClearColor(0.2, 0.2, 0.2, 0.2);
2449 glClear(GL_COLOR_BUFFER_BIT);
2450 glViewport(0, 0, 1, 1);
2451 mSecondTextureRenderer->drawTexture();
2452 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2453 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2454}
2455
2456TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002457 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2458
2459 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002460 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002461 ASSERT_EQ(OK, mST->updateTexImage());
2462
2463 // Detach from the primary context.
2464 ASSERT_EQ(OK, mST->detachFromContext());
2465
2466 // Attempt to attach to the secondary context.
2467 mST->abandon();
2468
2469 // Attempt to attach to the primary context.
2470 ASSERT_EQ(NO_INIT, mST->attachToContext(SECOND_TEX_ID));
2471}
2472
2473TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002474 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2475
2476 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002477 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002478 ASSERT_EQ(OK, mST->updateTexImage());
2479
2480 // Attempt to attach to the primary context.
2481 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2482}
2483
2484TEST_F(SurfaceTextureMultiContextGLTest,
2485 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002486 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2487
2488 // Attempt to attach to the primary context.
2489 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2490}
2491
2492TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002493 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2494
2495 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002496 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002497 ASSERT_EQ(OK, mST->updateTexImage());
2498
2499 // Detach from the primary context.
2500 ASSERT_EQ(OK, mST->detachFromContext());
2501
2502 // Make there be no current display.
2503 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2504 EGL_NO_CONTEXT));
2505 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2506
2507 // Attempt to attach with no context current.
2508 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2509}
2510
2511TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002512 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2513
2514 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002515 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002516 ASSERT_EQ(OK, mST->updateTexImage());
2517
2518 // Detach from the primary context.
2519 ASSERT_EQ(OK, mST->detachFromContext());
2520
2521 // Attach to the secondary context.
2522 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2523 mSecondEglContext));
2524 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2525
2526 // Detach from the secondary context.
2527 ASSERT_EQ(OK, mST->detachFromContext());
2528
2529 // Attach to the tertiary context.
2530 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2531 mThirdEglContext));
2532 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2533
2534 // Verify that the texture object was created and bound.
2535 GLint texBinding = -1;
2536 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2537 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2538
2539 // Try to use the texture from the tertiary context.
2540 glClearColor(0.2, 0.2, 0.2, 0.2);
2541 glClear(GL_COLOR_BUFFER_BIT);
2542 glViewport(0, 0, 1, 1);
2543 mThirdTextureRenderer->drawTexture();
2544 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2545 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2546}
2547
2548TEST_F(SurfaceTextureMultiContextGLTest,
2549 AttachToContextSucceedsTwiceBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002550 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2551
2552 // Detach from the primary context.
2553 ASSERT_EQ(OK, mST->detachFromContext());
2554
2555 // Attach to the secondary context.
2556 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2557 mSecondEglContext));
2558 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2559
2560 // Detach from the secondary context.
2561 ASSERT_EQ(OK, mST->detachFromContext());
2562
2563 // Attach to the tertiary context.
2564 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2565 mThirdEglContext));
2566 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2567
2568 // Verify that the texture object was created and bound.
2569 GLint texBinding = -1;
2570 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2571 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2572
2573 // Latch the texture contents on the tertiary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002574 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002575 ASSERT_EQ(OK, mST->updateTexImage());
2576
2577 // Try to use the texture from the tertiary context.
2578 glClearColor(0.2, 0.2, 0.2, 0.2);
2579 glClear(GL_COLOR_BUFFER_BIT);
2580 glViewport(0, 0, 1, 1);
2581 mThirdTextureRenderer->drawTexture();
2582 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2583 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
Jamie Gennisce561372012-03-19 18:33:05 -07002584}
2585
Jesse Hall90ed8502012-05-16 23:44:34 -07002586TEST_F(SurfaceTextureMultiContextGLTest,
2587 UpdateTexImageSucceedsForBufferConsumedBeforeDetach) {
2588 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
2589 ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2));
2590
2591 // produce two frames and consume them both on the primary context
2592 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2593 mFW->waitForFrame();
2594 ASSERT_EQ(OK, mST->updateTexImage());
2595
2596 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2597 mFW->waitForFrame();
2598 ASSERT_EQ(OK, mST->updateTexImage());
2599
2600 // produce one more frame
2601 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2602
2603 // Detach from the primary context and attach to the secondary context
2604 ASSERT_EQ(OK, mST->detachFromContext());
2605 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2606 mSecondEglContext));
2607 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2608
2609 // Consume final frame on secondary context
2610 mFW->waitForFrame();
2611 ASSERT_EQ(OK, mST->updateTexImage());
2612}
2613
Jamie Gennis5451d152011-06-08 09:40:45 -07002614} // namespace android