blob: bf347c6081cfb4d36d147bb1904ce3325b65ed42 [file] [log] [blame]
Jamie Gennisd99c0882011-03-10 16:24:46 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis2640bfd2011-07-14 17:11:47 -070017#define LOG_TAG "SurfaceTexture_test"
Jamie Gennis5451d152011-06-08 09:40:45 -070018//#define LOG_NDEBUG 0
19
Jamie Gennisd99c0882011-03-10 16:24:46 -080020#include <gtest/gtest.h>
21#include <gui/SurfaceTexture.h>
22#include <gui/SurfaceTextureClient.h>
23#include <ui/GraphicBuffer.h>
24#include <utils/String8.h>
Jamie Gennis5451d152011-06-08 09:40:45 -070025#include <utils/threads.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080026
Mathias Agopian90ac7992012-02-25 18:48:35 -080027#include <gui/ISurfaceComposer.h>
28#include <gui/Surface.h>
29#include <gui/SurfaceComposerClient.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080030
31#include <EGL/egl.h>
32#include <EGL/eglext.h>
33#include <GLES2/gl2.h>
34#include <GLES2/gl2ext.h>
35
36#include <ui/FramebufferNativeWindow.h>
37
38namespace android {
39
40class GLTest : public ::testing::Test {
41protected:
42
43 GLTest():
44 mEglDisplay(EGL_NO_DISPLAY),
45 mEglSurface(EGL_NO_SURFACE),
46 mEglContext(EGL_NO_CONTEXT) {
47 }
48
49 virtual void SetUp() {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -070050 const ::testing::TestInfo* const testInfo =
51 ::testing::UnitTest::GetInstance()->current_test_info();
52 ALOGV("Begin test: %s.%s", testInfo->test_case_name(),
53 testInfo->name());
54
Jamie Gennisd99c0882011-03-10 16:24:46 -080055 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
56 ASSERT_EQ(EGL_SUCCESS, eglGetError());
57 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
58
59 EGLint majorVersion;
60 EGLint minorVersion;
61 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
62 ASSERT_EQ(EGL_SUCCESS, eglGetError());
63 RecordProperty("EglVersionMajor", majorVersion);
64 RecordProperty("EglVersionMajor", minorVersion);
65
Jamie Gennisd99c0882011-03-10 16:24:46 -080066 EGLint numConfigs = 0;
Jamie Gennis1876d132011-03-17 16:32:52 -070067 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -080068 1, &numConfigs));
69 ASSERT_EQ(EGL_SUCCESS, eglGetError());
70
71 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
72 if (displaySecsEnv != NULL) {
73 mDisplaySecs = atoi(displaySecsEnv);
74 if (mDisplaySecs < 0) {
75 mDisplaySecs = 0;
76 }
77 } else {
78 mDisplaySecs = 0;
79 }
80
81 if (mDisplaySecs > 0) {
82 mComposerClient = new SurfaceComposerClient;
83 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
84
Jamie Gennisfc850122011-04-25 16:40:05 -070085 mSurfaceControl = mComposerClient->createSurface(
Jamie Gennisd99c0882011-03-10 16:24:46 -080086 String8("Test Surface"), 0,
87 getSurfaceWidth(), getSurfaceHeight(),
88 PIXEL_FORMAT_RGB_888, 0);
89
90 ASSERT_TRUE(mSurfaceControl != NULL);
91 ASSERT_TRUE(mSurfaceControl->isValid());
92
Mathias Agopian698c0872011-06-28 19:09:31 -070093 SurfaceComposerClient::openGlobalTransaction();
Jamie Gennis5dd0c4f2011-06-13 19:06:52 -070094 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
Jamie Gennisd99c0882011-03-10 16:24:46 -080095 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
Mathias Agopian698c0872011-06-28 19:09:31 -070096 SurfaceComposerClient::closeGlobalTransaction();
Jamie Gennisd99c0882011-03-10 16:24:46 -080097
98 sp<ANativeWindow> window = mSurfaceControl->getSurface();
Jamie Gennis1876d132011-03-17 16:32:52 -070099 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800100 window.get(), NULL);
101 } else {
102 EGLint pbufferAttribs[] = {
103 EGL_WIDTH, getSurfaceWidth(),
104 EGL_HEIGHT, getSurfaceHeight(),
105 EGL_NONE };
106
Jamie Gennis1876d132011-03-17 16:32:52 -0700107 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800108 pbufferAttribs);
109 }
110 ASSERT_EQ(EGL_SUCCESS, eglGetError());
111 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
112
Jamie Gennis1876d132011-03-17 16:32:52 -0700113 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
Jamie Gennisd99c0882011-03-10 16:24:46 -0800114 getContextAttribs());
115 ASSERT_EQ(EGL_SUCCESS, eglGetError());
116 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
117
118 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
119 mEglContext));
120 ASSERT_EQ(EGL_SUCCESS, eglGetError());
121
122 EGLint w, h;
123 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
124 ASSERT_EQ(EGL_SUCCESS, eglGetError());
125 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
126 ASSERT_EQ(EGL_SUCCESS, eglGetError());
127 RecordProperty("EglSurfaceWidth", w);
128 RecordProperty("EglSurfaceHeight", h);
129
130 glViewport(0, 0, w, h);
131 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
132 }
133
134 virtual void TearDown() {
135 // Display the result
136 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
239 int mDisplaySecs;
240 sp<SurfaceComposerClient> mComposerClient;
241 sp<SurfaceControl> mSurfaceControl;
242
243 EGLDisplay mEglDisplay;
244 EGLSurface mEglSurface;
245 EGLContext mEglContext;
Jamie Gennis1876d132011-03-17 16:32:52 -0700246 EGLConfig mGlConfig;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800247};
248
Jamie Gennis74bed552012-03-28 19:05:54 -0700249static void loadShader(GLenum shaderType, const char* pSource,
250 GLuint* outShader) {
251 GLuint shader = glCreateShader(shaderType);
252 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
253 if (shader) {
254 glShaderSource(shader, 1, &pSource, NULL);
255 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
256 glCompileShader(shader);
257 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
258 GLint compiled = 0;
259 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
260 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
261 if (!compiled) {
262 GLint infoLen = 0;
263 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
264 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
265 if (infoLen) {
266 char* buf = (char*) malloc(infoLen);
267 if (buf) {
268 glGetShaderInfoLog(shader, infoLen, NULL, buf);
269 printf("Shader compile log:\n%s\n", buf);
270 free(buf);
271 FAIL();
272 }
273 } else {
274 char* buf = (char*) malloc(0x1000);
275 if (buf) {
276 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
277 printf("Shader compile log:\n%s\n", buf);
278 free(buf);
279 FAIL();
280 }
281 }
282 glDeleteShader(shader);
283 shader = 0;
284 }
285 }
286 ASSERT_TRUE(shader != 0);
287 *outShader = shader;
288}
289
290static void createProgram(const char* pVertexSource,
291 const char* pFragmentSource, GLuint* outPgm) {
292 GLuint vertexShader, fragmentShader;
293 {
294 SCOPED_TRACE("compiling vertex shader");
295 ASSERT_NO_FATAL_FAILURE(loadShader(GL_VERTEX_SHADER, pVertexSource,
296 &vertexShader));
297 }
298 {
299 SCOPED_TRACE("compiling fragment shader");
300 ASSERT_NO_FATAL_FAILURE(loadShader(GL_FRAGMENT_SHADER, pFragmentSource,
301 &fragmentShader));
302 }
303
304 GLuint program = glCreateProgram();
305 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
306 if (program) {
307 glAttachShader(program, vertexShader);
308 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
309 glAttachShader(program, fragmentShader);
310 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
311 glLinkProgram(program);
312 GLint linkStatus = GL_FALSE;
313 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
314 if (linkStatus != GL_TRUE) {
315 GLint bufLength = 0;
316 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
317 if (bufLength) {
318 char* buf = (char*) malloc(bufLength);
319 if (buf) {
320 glGetProgramInfoLog(program, bufLength, NULL, buf);
321 printf("Program link log:\n%s\n", buf);
322 free(buf);
323 FAIL();
324 }
325 }
326 glDeleteProgram(program);
327 program = 0;
328 }
329 }
330 glDeleteShader(vertexShader);
331 glDeleteShader(fragmentShader);
332 ASSERT_TRUE(program != 0);
333 *outPgm = program;
334}
335
336static int abs(int value) {
337 return value > 0 ? value : -value;
338}
339
340
Jamie Gennisd99c0882011-03-10 16:24:46 -0800341// XXX: Code above this point should live elsewhere
342
343class SurfaceTextureGLTest : public GLTest {
344protected:
Jamie Gennis79e31252011-10-19 15:19:19 -0700345 enum { TEX_ID = 123 };
Jamie Gennisd99c0882011-03-10 16:24:46 -0800346
347 virtual void SetUp() {
348 GLTest::SetUp();
349 mST = new SurfaceTexture(TEX_ID);
350 mSTC = new SurfaceTextureClient(mST);
351 mANW = mSTC;
Jamie Gennis74bed552012-03-28 19:05:54 -0700352 mTextureRenderer = new TextureRenderer(TEX_ID, mST);
353 ASSERT_NO_FATAL_FAILURE(mTextureRenderer->SetUp());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800354 }
355
Jamie Gennis2640bfd2011-07-14 17:11:47 -0700356 virtual void TearDown() {
357 mANW.clear();
358 mSTC.clear();
359 mST.clear();
360 GLTest::TearDown();
361 }
362
Jamie Gennisd99c0882011-03-10 16:24:46 -0800363 void drawTexture() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700364 mTextureRenderer->drawTexture();
Jamie Gennisd99c0882011-03-10 16:24:46 -0800365 }
366
Jamie Gennis74bed552012-03-28 19:05:54 -0700367 class TextureRenderer: public RefBase {
368 public:
369 TextureRenderer(GLuint texName, const sp<SurfaceTexture>& st):
370 mTexName(texName),
371 mST(st) {
372 }
373
374 void SetUp() {
375 const char vsrc[] =
376 "attribute vec4 vPosition;\n"
377 "varying vec2 texCoords;\n"
378 "uniform mat4 texMatrix;\n"
379 "void main() {\n"
380 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
381 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
382 " gl_Position = vPosition;\n"
383 "}\n";
384
385 const char fsrc[] =
386 "#extension GL_OES_EGL_image_external : require\n"
387 "precision mediump float;\n"
388 "uniform samplerExternalOES texSampler;\n"
389 "varying vec2 texCoords;\n"
390 "void main() {\n"
391 " gl_FragColor = texture2D(texSampler, texCoords);\n"
392 "}\n";
393
394 {
395 SCOPED_TRACE("creating shader program");
396 ASSERT_NO_FATAL_FAILURE(createProgram(vsrc, fsrc, &mPgm));
397 }
398
399 mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
400 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
401 ASSERT_NE(-1, mPositionHandle);
402 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
403 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
404 ASSERT_NE(-1, mTexSamplerHandle);
405 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
406 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
407 ASSERT_NE(-1, mTexMatrixHandle);
408 }
409
410 // drawTexture draws the SurfaceTexture over the entire GL viewport.
411 void drawTexture() {
412 const GLfloat triangleVertices[] = {
413 -1.0f, 1.0f,
414 -1.0f, -1.0f,
415 1.0f, -1.0f,
416 1.0f, 1.0f,
417 };
418
419 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
420 triangleVertices);
421 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
422 glEnableVertexAttribArray(mPositionHandle);
423 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
424
425 glUseProgram(mPgm);
426 glUniform1i(mTexSamplerHandle, 0);
427 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
428 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName);
429 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
430
431 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
432 // they're setting the defautls for that target, but when hacking
433 // things to use GL_TEXTURE_2D they are needed to achieve the same
434 // behavior.
435 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
436 GL_LINEAR);
437 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
438 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
439 GL_LINEAR);
440 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
441 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
442 GL_CLAMP_TO_EDGE);
443 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
444 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
445 GL_CLAMP_TO_EDGE);
446 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
447
448 GLfloat texMatrix[16];
449 mST->getTransformMatrix(texMatrix);
450 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
451
452 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
453 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
454 }
455
456 GLuint mTexName;
457 sp<SurfaceTexture> mST;
458 GLuint mPgm;
459 GLint mPositionHandle;
460 GLint mTexSamplerHandle;
461 GLint mTexMatrixHandle;
462 };
463
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700464 class FrameWaiter : public SurfaceTexture::FrameAvailableListener {
465 public:
466 FrameWaiter():
467 mPendingFrames(0) {
468 }
469
470 void waitForFrame() {
471 Mutex::Autolock lock(mMutex);
472 while (mPendingFrames == 0) {
473 mCondition.wait(mMutex);
474 }
475 mPendingFrames--;
476 }
477
478 virtual void onFrameAvailable() {
479 Mutex::Autolock lock(mMutex);
480 mPendingFrames++;
481 mCondition.signal();
482 }
483
484 int mPendingFrames;
485 Mutex mMutex;
486 Condition mCondition;
487 };
488
Jamie Gennisd99c0882011-03-10 16:24:46 -0800489 sp<SurfaceTexture> mST;
490 sp<SurfaceTextureClient> mSTC;
491 sp<ANativeWindow> mANW;
Jamie Gennis74bed552012-03-28 19:05:54 -0700492 sp<TextureRenderer> mTextureRenderer;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800493};
494
495// Fill a YV12 buffer with a multi-colored checkerboard pattern
496void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
497 const int blockWidth = w > 16 ? w / 16 : 1;
498 const int blockHeight = h > 16 ? h / 16 : 1;
499 const int yuvTexOffsetY = 0;
500 int yuvTexStrideY = stride;
501 int yuvTexOffsetV = yuvTexStrideY * h;
502 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
503 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
504 int yuvTexStrideU = yuvTexStrideV;
505 for (int x = 0; x < w; x++) {
506 for (int y = 0; y < h; y++) {
507 int parityX = (x / blockWidth) & 1;
508 int parityY = (y / blockHeight) & 1;
509 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
510 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
511 if (x < w / 2 && y < h / 2) {
512 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
513 if (x * 2 < w / 2 && y * 2 < h / 2) {
514 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
515 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
516 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
517 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
518 intensity;
519 }
520 }
521 }
522 }
523}
524
525// Fill a YV12 buffer with red outside a given rectangle and green inside it.
526void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
527 const android_native_rect_t& rect) {
528 const int yuvTexOffsetY = 0;
529 int yuvTexStrideY = stride;
530 int yuvTexOffsetV = yuvTexStrideY * h;
531 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
532 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
533 int yuvTexStrideU = yuvTexStrideV;
534 for (int x = 0; x < w; x++) {
535 for (int y = 0; y < h; y++) {
536 bool inside = rect.left <= x && x < rect.right &&
537 rect.top <= y && y < rect.bottom;
538 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
539 if (x < w / 2 && y < h / 2) {
540 bool inside = rect.left <= 2*x && 2*x < rect.right &&
541 rect.top <= 2*y && 2*y < rect.bottom;
542 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
543 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
544 inside ? 16 : 255;
545 }
546 }
547 }
548}
549
Jamie Gennis1876d132011-03-17 16:32:52 -0700550void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
551 const size_t PIXEL_SIZE = 4;
552 for (int x = 0; x < w; x++) {
553 for (int y = 0; y < h; y++) {
554 off_t offset = (y * stride + x) * PIXEL_SIZE;
555 for (int c = 0; c < 4; c++) {
556 int parityX = (x / (1 << (c+2))) & 1;
557 int parityY = (y / (1 << (c+2))) & 1;
558 buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
559 }
560 }
561 }
562}
563
Jamie Gennisfe27e2f2011-11-11 18:05:11 -0800564void fillRGBA8BufferSolid(uint8_t* buf, int w, int h, int stride, uint8_t r,
565 uint8_t g, uint8_t b, uint8_t a) {
566 const size_t PIXEL_SIZE = 4;
567 for (int y = 0; y < h; y++) {
568 for (int x = 0; x < h; x++) {
569 off_t offset = (y * stride + x) * PIXEL_SIZE;
570 buf[offset + 0] = r;
571 buf[offset + 1] = g;
572 buf[offset + 2] = b;
573 buf[offset + 3] = a;
574 }
575 }
576}
577
Jamie Gennisce561372012-03-19 18:33:05 -0700578// Produce a single RGBA8 frame by filling a buffer with a checkerboard pattern
579// using the CPU. This assumes that the ANativeWindow is already configured to
580// allow this to be done (e.g. the format is set to RGBA8).
581//
582// Calls to this function should be wrapped in an ASSERT_NO_FATAL_FAILURE().
583void produceOneRGBA8Frame(const sp<ANativeWindow>& anw) {
584 android_native_buffer_t* anb;
585 ASSERT_EQ(NO_ERROR, anw->dequeueBuffer(anw.get(), &anb));
586 ASSERT_TRUE(anb != NULL);
587
588 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
589 ASSERT_EQ(NO_ERROR, anw->lockBuffer(anw.get(), buf->getNativeBuffer()));
590
591 uint8_t* img = NULL;
592 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
593 (void**)(&img)));
594 fillRGBA8Buffer(img, buf->getWidth(), buf->getHeight(), buf->getStride());
595 ASSERT_EQ(NO_ERROR, buf->unlock());
596 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer()));
597}
598
Jamie Gennisd99c0882011-03-10 16:24:46 -0800599TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700600 const int texWidth = 64;
601 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800602
603 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700604 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800605 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
606 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
607
Iliyan Malchev697526b2011-05-01 11:33:26 -0700608 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800609 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
610 ASSERT_TRUE(anb != NULL);
611
612 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
613 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
614
615 // Fill the buffer with the a checkerboard pattern
616 uint8_t* img = NULL;
617 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700618 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800619 buf->unlock();
620 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
621
622 mST->updateTexImage();
623
624 glClearColor(0.2, 0.2, 0.2, 0.2);
625 glClear(GL_COLOR_BUFFER_BIT);
626
Jamie Gennisc8c51522011-06-15 14:24:38 -0700627 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800628 drawTexture();
629
630 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255));
631 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700632 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255));
633 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800634
Jamie Gennisc8c51522011-06-15 14:24:38 -0700635 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255));
636 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255));
637 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800638 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700639 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800640 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255));
641 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255));
642}
643
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700644TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700645 const int texWidth = 64;
646 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800647
648 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700649 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800650 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
651 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
652
Iliyan Malchev697526b2011-05-01 11:33:26 -0700653 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800654 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
655 ASSERT_TRUE(anb != NULL);
656
657 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
658 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
659
660 // Fill the buffer with the a checkerboard pattern
661 uint8_t* img = NULL;
662 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700663 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800664 buf->unlock();
665 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
666
667 mST->updateTexImage();
668
669 glClearColor(0.2, 0.2, 0.2, 0.2);
670 glClear(GL_COLOR_BUFFER_BIT);
671
Jamie Gennisc8c51522011-06-15 14:24:38 -0700672 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800673 drawTexture();
674
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700675 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
676 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800677 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
678 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
679
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700680 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255));
681 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255));
682 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255));
683 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
684 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255));
685 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255));
686 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800687}
688
689TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700690 const int texWidth = 64;
691 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800692
693 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700694 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800695 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
696 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
697
698 android_native_rect_t crops[] = {
699 {4, 6, 22, 36},
700 {0, 6, 22, 36},
701 {4, 0, 22, 36},
Jamie Gennis1876d132011-03-17 16:32:52 -0700702 {4, 6, texWidth, 36},
703 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800704 };
705
706 for (int i = 0; i < 5; i++) {
707 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800708 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
709 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800710
711 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
712
Iliyan Malchev697526b2011-05-01 11:33:26 -0700713 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800714 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
715 ASSERT_TRUE(anb != NULL);
716
717 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800718 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(),
719 buf->getNativeBuffer()));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800720
721 uint8_t* img = NULL;
722 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700723 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800724 buf->unlock();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800725 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
726 buf->getNativeBuffer()));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800727
728 mST->updateTexImage();
729
730 glClearColor(0.2, 0.2, 0.2, 0.2);
731 glClear(GL_COLOR_BUFFER_BIT);
732
Jamie Gennisc8c51522011-06-15 14:24:38 -0700733 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800734 drawTexture();
735
736 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
737 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
738 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
739 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
740
741 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
742 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
743 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
744 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
745 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
746 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
747 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
748 }
749}
750
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700751// This test is intended to catch synchronization bugs between the CPU-written
752// and GPU-read buffers.
753TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
754 enum { texWidth = 16 };
755 enum { texHeight = 16 };
756 enum { numFrames = 1024 };
757
758 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
759 ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2));
760 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
761 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
762 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
763 GRALLOC_USAGE_SW_WRITE_OFTEN));
764
765 struct TestPixel {
766 int x;
767 int y;
768 };
769 const TestPixel testPixels[] = {
770 { 4, 11 },
771 { 12, 14 },
772 { 7, 2 },
773 };
774 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
775
776 class ProducerThread : public Thread {
777 public:
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800778 ProducerThread(const sp<ANativeWindow>& anw,
779 const TestPixel* testPixels):
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700780 mANW(anw),
781 mTestPixels(testPixels) {
782 }
783
784 virtual ~ProducerThread() {
785 }
786
787 virtual bool threadLoop() {
788 for (int i = 0; i < numFrames; i++) {
789 ANativeWindowBuffer* anb;
790 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
791 return false;
792 }
793 if (anb == NULL) {
794 return false;
795 }
796
797 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
798 if (mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())
799 != NO_ERROR) {
800 return false;
801 }
802
803 const int yuvTexOffsetY = 0;
804 int stride = buf->getStride();
805 int yuvTexStrideY = stride;
806 int yuvTexOffsetV = yuvTexStrideY * texHeight;
807 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
808 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
809 int yuvTexStrideU = yuvTexStrideV;
810
811 uint8_t* img = NULL;
812 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
813
814 // Gray out all the test pixels first, so we're more likely to
815 // see a failure if GL is still texturing from the buffer we
816 // just dequeued.
817 for (int j = 0; j < numTestPixels; j++) {
818 int x = mTestPixels[j].x;
819 int y = mTestPixels[j].y;
820 uint8_t value = 128;
821 img[y*stride + x] = value;
822 }
823
824 // Fill the buffer with gray.
825 for (int y = 0; y < texHeight; y++) {
826 for (int x = 0; x < texWidth; x++) {
827 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
828 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
829 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
830 }
831 }
832
833 // Set the test pixels to either white or black.
834 for (int j = 0; j < numTestPixels; j++) {
835 int x = mTestPixels[j].x;
836 int y = mTestPixels[j].y;
837 uint8_t value = 0;
838 if (j == (i % numTestPixels)) {
839 value = 255;
840 }
841 img[y*stride + x] = value;
842 }
843
844 buf->unlock();
845 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())
846 != NO_ERROR) {
847 return false;
848 }
849 }
850 return false;
851 }
852
853 sp<ANativeWindow> mANW;
854 const TestPixel* mTestPixels;
855 };
856
857 sp<FrameWaiter> fw(new FrameWaiter);
858 mST->setFrameAvailableListener(fw);
859
860 sp<Thread> pt(new ProducerThread(mANW, testPixels));
861 pt->run();
862
863 glViewport(0, 0, texWidth, texHeight);
864
865 glClearColor(0.2, 0.2, 0.2, 0.2);
866 glClear(GL_COLOR_BUFFER_BIT);
867
868 // We wait for the first two frames up front so that the producer will be
869 // likely to dequeue the buffer that's currently being textured from.
870 fw->waitForFrame();
871 fw->waitForFrame();
872
873 for (int i = 0; i < numFrames; i++) {
874 SCOPED_TRACE(String8::format("frame %d", i).string());
875
876 // We must wait for each frame to come in because if we ever do an
877 // updateTexImage call that doesn't consume a newly available buffer
878 // then the producer and consumer will get out of sync, which will cause
879 // a deadlock.
880 if (i > 1) {
881 fw->waitForFrame();
882 }
883 mST->updateTexImage();
884 drawTexture();
885
886 for (int j = 0; j < numTestPixels; j++) {
887 int x = testPixels[j].x;
888 int y = testPixels[j].y;
889 uint8_t value = 0;
890 if (j == (i % numTestPixels)) {
891 // We must y-invert the texture coords
892 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
893 } else {
894 // We must y-invert the texture coords
895 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
896 }
897 }
898 }
899
900 pt->requestExitAndWait();
901}
902
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700903TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700904 const int texWidth = 64;
905 const int texHeight = 66;
906
907 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
908 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
909 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
910 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
911
Jamie Gennisce561372012-03-19 18:33:05 -0700912 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -0700913
914 mST->updateTexImage();
915
916 glClearColor(0.2, 0.2, 0.2, 0.2);
917 glClear(GL_COLOR_BUFFER_BIT);
918
Jamie Gennisc8c51522011-06-15 14:24:38 -0700919 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -0700920 drawTexture();
921
922 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
923 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700924 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
925 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700926
927 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700928 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700929 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700930 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
931 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700932 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -0700933 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700934 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231));
935 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35));
936 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700937 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700938 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
939 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -0700940 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700941 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700942 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
943}
944
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700945TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700946 const int texWidth = 64;
947 const int texHeight = 64;
948
949 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
950 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
951 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
952 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
953
Jamie Gennisce561372012-03-19 18:33:05 -0700954 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -0700955
956 mST->updateTexImage();
957
958 glClearColor(0.2, 0.2, 0.2, 0.2);
959 glClear(GL_COLOR_BUFFER_BIT);
960
Jamie Gennisc8c51522011-06-15 14:24:38 -0700961 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -0700962 drawTexture();
963
964 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231));
965 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35));
966 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
967 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35));
968
969 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35));
970 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231));
971 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231));
972 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35));
973 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35));
974 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231));
975 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35));
976 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35));
977 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35));
978 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231));
979 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231));
980 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231));
981 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231));
982 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35));
983 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231));
984 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35));
985}
986
Jamie Gennis7b305ff2011-07-19 12:08:33 -0700987TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
988 class ProducerThread : public Thread {
989 public:
990 ProducerThread(const sp<ANativeWindow>& anw):
991 mANW(anw),
992 mDequeueError(NO_ERROR) {
993 }
994
995 virtual ~ProducerThread() {
996 }
997
998 virtual bool threadLoop() {
999 Mutex::Autolock lock(mMutex);
1000 ANativeWindowBuffer* anb;
1001
1002 // Frame 1
1003 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1004 return false;
1005 }
1006 if (anb == NULL) {
1007 return false;
1008 }
1009 if (mANW->queueBuffer(mANW.get(), anb)
1010 != NO_ERROR) {
1011 return false;
1012 }
1013
1014 // Frame 2
1015 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
1016 return false;
1017 }
1018 if (anb == NULL) {
1019 return false;
1020 }
1021 if (mANW->queueBuffer(mANW.get(), anb)
1022 != NO_ERROR) {
1023 return false;
1024 }
1025
1026 // Frame 3 - error expected
1027 mDequeueError = mANW->dequeueBuffer(mANW.get(), &anb);
1028 return false;
1029 }
1030
1031 status_t getDequeueError() {
1032 Mutex::Autolock lock(mMutex);
1033 return mDequeueError;
1034 }
1035
1036 private:
1037 sp<ANativeWindow> mANW;
1038 status_t mDequeueError;
1039 Mutex mMutex;
1040 };
1041
1042 sp<FrameWaiter> fw(new FrameWaiter);
1043 mST->setFrameAvailableListener(fw);
1044 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1045 ASSERT_EQ(OK, mST->setBufferCountServer(2));
1046
1047 sp<Thread> pt(new ProducerThread(mANW));
1048 pt->run();
1049
1050 fw->waitForFrame();
1051 fw->waitForFrame();
1052
1053 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1054 // block waiting for a buffer to become available.
1055 usleep(100000);
1056
1057 mST->abandon();
1058
1059 pt->requestExitAndWait();
1060 ASSERT_EQ(NO_INIT,
1061 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1062}
1063
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001064TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1065 int texHeight = 16;
1066 ANativeWindowBuffer* anb;
1067
1068 GLint maxTextureSize;
1069 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1070
1071 // make sure it works with small textures
1072 mST->setDefaultBufferSize(16, texHeight);
1073 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1074 EXPECT_EQ(16, anb->width);
1075 EXPECT_EQ(texHeight, anb->height);
1076 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1077 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1078
1079 // make sure it works with GL_MAX_TEXTURE_SIZE
1080 mST->setDefaultBufferSize(maxTextureSize, texHeight);
1081 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1082 EXPECT_EQ(maxTextureSize, anb->width);
1083 EXPECT_EQ(texHeight, anb->height);
1084 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1085 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1086
1087 // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1088 mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
1089 EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1090 EXPECT_EQ(maxTextureSize+1, anb->width);
1091 EXPECT_EQ(texHeight, anb->height);
1092 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1093 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1094}
1095
Jamie Gennis5451d152011-06-08 09:40:45 -07001096/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001097 * This test fixture is for testing GL -> GL texture streaming. It creates an
1098 * EGLSurface and an EGLContext for the image producer to use.
1099 */
1100class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1101protected:
1102 SurfaceTextureGLToGLTest():
1103 mProducerEglSurface(EGL_NO_SURFACE),
1104 mProducerEglContext(EGL_NO_CONTEXT) {
1105 }
1106
1107 virtual void SetUp() {
1108 SurfaceTextureGLTest::SetUp();
1109
Jamie Gennisce561372012-03-19 18:33:05 -07001110 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001111 mANW.get(), NULL);
1112 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1113 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1114
Jamie Gennisce561372012-03-19 18:33:05 -07001115 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001116 EGL_NO_CONTEXT, getContextAttribs());
1117 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1118 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1119 }
1120
1121 virtual void TearDown() {
1122 if (mProducerEglContext != EGL_NO_CONTEXT) {
1123 eglDestroyContext(mEglDisplay, mProducerEglContext);
1124 }
1125 if (mProducerEglSurface != EGL_NO_SURFACE) {
1126 eglDestroySurface(mEglDisplay, mProducerEglSurface);
1127 }
1128 SurfaceTextureGLTest::TearDown();
1129 }
1130
1131 EGLSurface mProducerEglSurface;
1132 EGLContext mProducerEglContext;
1133};
1134
1135TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1136 const int texWidth = 64;
1137 const int texHeight = 64;
1138
1139 mST->setDefaultBufferSize(texWidth, texHeight);
1140
1141 // Do the producer side of things
1142 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1143 mProducerEglSurface, mProducerEglContext));
1144 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1145
1146 // This is needed to ensure we pick up a buffer of the correct size.
1147 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1148
1149 glClearColor(0.6, 0.6, 0.6, 0.6);
1150 glClear(GL_COLOR_BUFFER_BIT);
1151
1152 glEnable(GL_SCISSOR_TEST);
1153 glScissor(4, 4, 4, 4);
1154 glClearColor(1.0, 0.0, 0.0, 1.0);
1155 glClear(GL_COLOR_BUFFER_BIT);
1156
1157 glScissor(24, 48, 4, 4);
1158 glClearColor(0.0, 1.0, 0.0, 1.0);
1159 glClear(GL_COLOR_BUFFER_BIT);
1160
1161 glScissor(37, 17, 4, 4);
1162 glClearColor(0.0, 0.0, 1.0, 1.0);
1163 glClear(GL_COLOR_BUFFER_BIT);
1164
1165 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1166
1167 // Do the consumer side of things
1168 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1169 mEglContext));
1170 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1171
1172 glDisable(GL_SCISSOR_TEST);
1173
1174 mST->updateTexImage(); // Skip the first frame, which was empty
1175 mST->updateTexImage();
1176
1177 glClearColor(0.2, 0.2, 0.2, 0.2);
1178 glClear(GL_COLOR_BUFFER_BIT);
1179
1180 glViewport(0, 0, texWidth, texHeight);
1181 drawTexture();
1182
1183 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153));
1184 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153));
1185 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1186 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1187
1188 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255));
1189 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255));
1190 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255));
1191 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1192 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1193 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153));
1194 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153));
1195 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1196 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1197 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1198 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1199 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1200 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1201 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1202 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1203 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1204}
1205
1206TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001207 sp<GraphicBuffer> buffers[2];
1208
1209 sp<FrameWaiter> fw(new FrameWaiter);
1210 mST->setFrameAvailableListener(fw);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001211
Jamie Gennise3603d72011-11-19 21:20:17 -08001212 // This test requires async mode to run on a single thread.
1213 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1214 mProducerEglSurface, mProducerEglContext));
1215 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1216 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1217 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1218
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001219 for (int i = 0; i < 2; i++) {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001220 // Produce a frame
1221 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1222 mProducerEglSurface, mProducerEglContext));
1223 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1224 glClear(GL_COLOR_BUFFER_BIT);
1225 eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1226
1227 // Consume a frame
1228 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1229 mEglContext));
1230 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001231 fw->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001232 mST->updateTexImage();
1233 buffers[i] = mST->getCurrentBuffer();
1234 }
1235
1236 // Destroy the GL texture object to release its ref on buffers[2].
1237 GLuint texID = TEX_ID;
1238 glDeleteTextures(1, &texID);
1239
1240 // Destroy the EGLSurface
1241 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1242 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001243 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001244
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001245 // This test should have the only reference to buffer 0.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001246 EXPECT_EQ(1, buffers[0]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001247
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001248 // The SurfaceTexture should hold a single reference to buffer 1 in its
1249 // mCurrentBuffer member. All of the references in the slots should have
1250 // been released.
1251 EXPECT_EQ(2, buffers[1]->getStrongCount());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001252}
1253
1254TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1255 sp<GraphicBuffer> buffers[3];
1256
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001257 sp<FrameWaiter> fw(new FrameWaiter);
1258 mST->setFrameAvailableListener(fw);
1259
Jamie Gennise3603d72011-11-19 21:20:17 -08001260 // This test requires async mode to run on a single thread.
1261 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1262 mProducerEglSurface, mProducerEglContext));
1263 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1264 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1265 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1266
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001267 for (int i = 0; i < 3; i++) {
1268 // Produce a frame
1269 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1270 mProducerEglSurface, mProducerEglContext));
1271 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1272 glClear(GL_COLOR_BUFFER_BIT);
1273 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1274 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1275
1276 // Consume a frame
1277 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1278 mEglContext));
1279 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001280 fw->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001281 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1282 buffers[i] = mST->getCurrentBuffer();
1283 }
1284
1285 // Abandon the SurfaceTexture, releasing the ref that the SurfaceTexture has
1286 // on buffers[2].
1287 mST->abandon();
1288
1289 // Destroy the GL texture object to release its ref on buffers[2].
1290 GLuint texID = TEX_ID;
1291 glDeleteTextures(1, &texID);
1292
1293 // Destroy the EGLSurface.
1294 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1295 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001296 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001297
1298 EXPECT_EQ(1, buffers[0]->getStrongCount());
1299 EXPECT_EQ(1, buffers[1]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001300
1301 // Depending on how lazily the GL driver dequeues buffers, we may end up
1302 // with either two or three total buffers. If there are three, make sure
1303 // the last one was properly down-ref'd.
1304 if (buffers[2] != buffers[0]) {
1305 EXPECT_EQ(1, buffers[2]->getStrongCount());
1306 }
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001307}
1308
Jamie Gennis59769462011-11-19 18:04:43 -08001309TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) {
1310 // This test requires 3 buffers to run on a single thread.
1311 mST->setBufferCountServer(3);
1312
1313 ASSERT_TRUE(mST->isSynchronousMode());
1314
1315 for (int i = 0; i < 10; i++) {
1316 // Produce a frame
1317 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1318 mProducerEglSurface, mProducerEglContext));
1319 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1320 glClear(GL_COLOR_BUFFER_BIT);
1321 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1322 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1323
1324 // Consume a frame
1325 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1326 mEglContext));
1327 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1328 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1329 }
1330
1331 ASSERT_TRUE(mST->isSynchronousMode());
1332}
1333
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001334/*
1335 * This test fixture is for testing GL -> GL texture streaming from one thread
1336 * to another. It contains functionality to create a producer thread that will
1337 * perform GL rendering to an ANativeWindow that feeds frames to a
1338 * SurfaceTexture. Additionally it supports interlocking the producer and
1339 * consumer threads so that a specific sequence of calls can be
1340 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07001341 *
1342 * The intended usage is as follows:
1343 *
1344 * TEST_F(...) {
1345 * class PT : public ProducerThread {
1346 * virtual void render() {
1347 * ...
1348 * swapBuffers();
1349 * }
1350 * };
1351 *
1352 * runProducerThread(new PT());
1353 *
1354 * // The order of these calls will vary from test to test and may include
1355 * // multiple frames and additional operations (e.g. GL rendering from the
1356 * // texture).
1357 * fc->waitForFrame();
1358 * mST->updateTexImage();
1359 * fc->finishFrame();
1360 * }
1361 *
1362 */
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001363class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07001364protected:
1365
1366 // ProducerThread is an abstract base class to simplify the creation of
1367 // OpenGL ES frame producer threads.
1368 class ProducerThread : public Thread {
1369 public:
1370 virtual ~ProducerThread() {
1371 }
1372
1373 void setEglObjects(EGLDisplay producerEglDisplay,
1374 EGLSurface producerEglSurface,
1375 EGLContext producerEglContext) {
1376 mProducerEglDisplay = producerEglDisplay;
1377 mProducerEglSurface = producerEglSurface;
1378 mProducerEglContext = producerEglContext;
1379 }
1380
1381 virtual bool threadLoop() {
1382 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
1383 mProducerEglSurface, mProducerEglContext);
1384 render();
1385 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1386 EGL_NO_CONTEXT);
1387 return false;
1388 }
1389
1390 protected:
1391 virtual void render() = 0;
1392
1393 void swapBuffers() {
1394 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
1395 }
1396
1397 EGLDisplay mProducerEglDisplay;
1398 EGLSurface mProducerEglSurface;
1399 EGLContext mProducerEglContext;
1400 };
1401
1402 // FrameCondition is a utility class for interlocking between the producer
1403 // and consumer threads. The FrameCondition object should be created and
1404 // destroyed in the consumer thread only. The consumer thread should set
1405 // the FrameCondition as the FrameAvailableListener of the SurfaceTexture,
1406 // and should call both waitForFrame and finishFrame once for each expected
1407 // frame.
1408 //
1409 // This interlocking relies on the fact that onFrameAvailable gets called
1410 // synchronously from SurfaceTexture::queueBuffer.
1411 class FrameCondition : public SurfaceTexture::FrameAvailableListener {
1412 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001413 FrameCondition():
1414 mFrameAvailable(false),
1415 mFrameFinished(false) {
1416 }
1417
Jamie Gennis5451d152011-06-08 09:40:45 -07001418 // waitForFrame waits for the next frame to arrive. This should be
1419 // called from the consumer thread once for every frame expected by the
1420 // test.
1421 void waitForFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001422 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001423 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001424 while (!mFrameAvailable) {
1425 mFrameAvailableCondition.wait(mMutex);
1426 }
1427 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01001428 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001429 }
1430
1431 // Allow the producer to return from its swapBuffers call and continue
1432 // on to produce the next frame. This should be called by the consumer
1433 // thread once for every frame expected by the test.
1434 void finishFrame() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001435 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001436 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001437 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001438 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01001439 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001440 }
1441
1442 // This should be called by SurfaceTexture on the producer thread.
1443 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001444 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001445 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001446 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001447 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001448 while (!mFrameFinished) {
1449 mFrameFinishCondition.wait(mMutex);
1450 }
1451 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01001452 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07001453 }
1454
1455 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001456 bool mFrameAvailable;
1457 bool mFrameFinished;
1458
Jamie Gennis5451d152011-06-08 09:40:45 -07001459 Mutex mMutex;
1460 Condition mFrameAvailableCondition;
1461 Condition mFrameFinishCondition;
1462 };
1463
Jamie Gennis5451d152011-06-08 09:40:45 -07001464 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001465 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07001466 mFC = new FrameCondition();
1467 mST->setFrameAvailableListener(mFC);
1468 }
1469
1470 virtual void TearDown() {
1471 if (mProducerThread != NULL) {
1472 mProducerThread->requestExitAndWait();
1473 }
Jamie Gennis5451d152011-06-08 09:40:45 -07001474 mProducerThread.clear();
1475 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001476 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07001477 }
1478
1479 void runProducerThread(const sp<ProducerThread> producerThread) {
1480 ASSERT_TRUE(mProducerThread == NULL);
1481 mProducerThread = producerThread;
1482 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
1483 mProducerEglContext);
1484 producerThread->run();
1485 }
1486
Jamie Gennis5451d152011-06-08 09:40:45 -07001487 sp<ProducerThread> mProducerThread;
1488 sp<FrameCondition> mFC;
1489};
1490
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001491TEST_F(SurfaceTextureGLThreadToGLTest,
1492 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001493 class PT : public ProducerThread {
1494 virtual void render() {
1495 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1496 glClear(GL_COLOR_BUFFER_BIT);
1497 swapBuffers();
1498 }
1499 };
1500
1501 runProducerThread(new PT());
1502
1503 mFC->waitForFrame();
1504 mST->updateTexImage();
1505 mFC->finishFrame();
1506
1507 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08001508}
Jamie Gennis5451d152011-06-08 09:40:45 -07001509
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001510TEST_F(SurfaceTextureGLThreadToGLTest,
1511 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001512 class PT : public ProducerThread {
1513 virtual void render() {
1514 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1515 glClear(GL_COLOR_BUFFER_BIT);
1516 swapBuffers();
1517 }
1518 };
1519
1520 runProducerThread(new PT());
1521
1522 mFC->waitForFrame();
1523 mFC->finishFrame();
1524 mST->updateTexImage();
1525
1526 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1527}
1528
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001529TEST_F(SurfaceTextureGLThreadToGLTest,
1530 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001531 enum { NUM_ITERATIONS = 1024 };
1532
1533 class PT : public ProducerThread {
1534 virtual void render() {
1535 for (int i = 0; i < NUM_ITERATIONS; i++) {
1536 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1537 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001538 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001539 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001540 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001541 }
1542 }
1543 };
1544
1545 runProducerThread(new PT());
1546
1547 for (int i = 0; i < NUM_ITERATIONS; i++) {
1548 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01001549 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001550 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01001551 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001552 mFC->finishFrame();
1553
1554 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1555 }
1556}
1557
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001558TEST_F(SurfaceTextureGLThreadToGLTest,
1559 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001560 enum { NUM_ITERATIONS = 1024 };
1561
1562 class PT : public ProducerThread {
1563 virtual void render() {
1564 for (int i = 0; i < NUM_ITERATIONS; i++) {
1565 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1566 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001567 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001568 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001569 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001570 }
1571 }
1572 };
1573
1574 runProducerThread(new PT());
1575
1576 for (int i = 0; i < NUM_ITERATIONS; i++) {
1577 mFC->waitForFrame();
1578 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01001579 ALOGV("+updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001580 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01001581 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001582
1583 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1584 }
1585}
1586
Jamie Gennis6e502192011-07-21 14:31:31 -07001587// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001588TEST_F(SurfaceTextureGLThreadToGLTest,
1589 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07001590 enum { NUM_ITERATIONS = 64 };
1591
1592 class PT : public ProducerThread {
1593 virtual void render() {
1594 for (int i = 0; i < NUM_ITERATIONS; i++) {
1595 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1596 glClear(GL_COLOR_BUFFER_BIT);
Steve Block6807e592011-10-20 11:56:00 +01001597 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07001598 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001599 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07001600 }
1601 }
1602 };
1603
1604 ASSERT_EQ(OK, mST->setSynchronousMode(true));
1605 ASSERT_EQ(OK, mST->setBufferCountServer(2));
1606
1607 runProducerThread(new PT());
1608
1609 // Allow three frames to be rendered and queued before starting the
1610 // rendering in this thread. For the latter two frames we don't call
1611 // updateTexImage so the next dequeue from the producer thread will block
1612 // waiting for a frame to become available.
1613 mFC->waitForFrame();
1614 mFC->finishFrame();
1615
1616 // We must call updateTexImage to consume the first frame so that the
1617 // SurfaceTexture is able to reduce the buffer count to 2. This is because
1618 // the GL driver may dequeue a buffer when the EGLSurface is created, and
1619 // that happens before we call setBufferCountServer. It's possible that the
1620 // driver does not dequeue a buffer at EGLSurface creation time, so we
1621 // cannot rely on this to cause the second dequeueBuffer call to block.
1622 mST->updateTexImage();
1623
1624 mFC->waitForFrame();
1625 mFC->finishFrame();
1626 mFC->waitForFrame();
1627 mFC->finishFrame();
1628
1629 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1630 // block waiting for a buffer to become available.
1631 usleep(100000);
1632
1633 // Render and present a number of images. This thread should not be blocked
1634 // by the fact that the producer thread is blocking in dequeue.
1635 for (int i = 0; i < NUM_ITERATIONS; i++) {
1636 glClear(GL_COLOR_BUFFER_BIT);
1637 eglSwapBuffers(mEglDisplay, mEglSurface);
1638 }
1639
1640 // Consume the two pending buffers to unblock the producer thread.
1641 mST->updateTexImage();
1642 mST->updateTexImage();
1643
1644 // Consume the remaining buffers from the producer thread.
1645 for (int i = 0; i < NUM_ITERATIONS-3; i++) {
1646 mFC->waitForFrame();
1647 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01001648 ALOGV("+updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07001649 mST->updateTexImage();
Steve Block6807e592011-10-20 11:56:00 +01001650 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07001651 }
1652}
1653
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08001654class SurfaceTextureFBOTest : public SurfaceTextureGLTest {
1655protected:
1656
1657 virtual void SetUp() {
1658 SurfaceTextureGLTest::SetUp();
1659
1660 glGenFramebuffers(1, &mFbo);
1661 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
1662
1663 glGenTextures(1, &mFboTex);
1664 glBindTexture(GL_TEXTURE_2D, mFboTex);
1665 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getSurfaceWidth(),
1666 getSurfaceHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
1667 glBindTexture(GL_TEXTURE_2D, 0);
1668 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
1669
1670 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
1671 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
1672 GL_TEXTURE_2D, mFboTex, 0);
1673 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1674 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
1675 }
1676
1677 virtual void TearDown() {
1678 SurfaceTextureGLTest::TearDown();
1679
1680 glDeleteTextures(1, &mFboTex);
1681 glDeleteFramebuffers(1, &mFbo);
1682 }
1683
1684 GLuint mFbo;
1685 GLuint mFboTex;
1686};
1687
1688// This test is intended to verify that proper synchronization is done when
1689// rendering into an FBO.
1690TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) {
1691 const int texWidth = 64;
1692 const int texHeight = 64;
1693
1694 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
1695 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
1696 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
1697 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
1698
1699 android_native_buffer_t* anb;
1700 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1701 ASSERT_TRUE(anb != NULL);
1702
1703 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
1704 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
1705
1706 // Fill the buffer with green
1707 uint8_t* img = NULL;
1708 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
1709 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255,
1710 0, 255);
1711 buf->unlock();
1712 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
1713
1714 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1715
1716 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
1717 drawTexture();
1718 glBindFramebuffer(GL_FRAMEBUFFER, 0);
1719
1720 for (int i = 0; i < 4; i++) {
1721 SCOPED_TRACE(String8::format("frame %d", i).string());
1722
1723 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1724 ASSERT_TRUE(anb != NULL);
1725
1726 buf = new GraphicBuffer(anb, false);
1727 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(),
1728 buf->getNativeBuffer()));
1729
1730 // Fill the buffer with red
1731 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN,
1732 (void**)(&img)));
1733 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0,
1734 0, 255);
1735 ASSERT_EQ(NO_ERROR, buf->unlock());
1736 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
1737 buf->getNativeBuffer()));
1738
1739 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1740
1741 drawTexture();
1742
1743 EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255));
1744 }
1745
1746 glBindFramebuffer(GL_FRAMEBUFFER, mFbo);
1747
1748 EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255));
1749}
1750
Jamie Gennisce561372012-03-19 18:33:05 -07001751class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest {
1752protected:
Jamie Gennis74bed552012-03-28 19:05:54 -07001753 enum { SECOND_TEX_ID = 123 };
1754 enum { THIRD_TEX_ID = 456 };
1755
Jamie Gennisce561372012-03-19 18:33:05 -07001756 SurfaceTextureMultiContextGLTest():
1757 mSecondEglContext(EGL_NO_CONTEXT) {
1758 }
1759
1760 virtual void SetUp() {
1761 SurfaceTextureGLTest::SetUp();
1762
Jamie Gennis74bed552012-03-28 19:05:54 -07001763 // Set up the secondary context and texture renderer.
Jamie Gennisce561372012-03-19 18:33:05 -07001764 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig,
1765 EGL_NO_CONTEXT, getContextAttribs());
1766 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1767 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext);
Jamie Gennis74bed552012-03-28 19:05:54 -07001768
1769 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1770 mSecondEglContext));
1771 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1772 mSecondTextureRenderer = new TextureRenderer(SECOND_TEX_ID, mST);
1773 ASSERT_NO_FATAL_FAILURE(mSecondTextureRenderer->SetUp());
1774
1775 // Set up the tertiary context and texture renderer.
1776 mThirdEglContext = eglCreateContext(mEglDisplay, mGlConfig,
1777 EGL_NO_CONTEXT, getContextAttribs());
1778 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1779 ASSERT_NE(EGL_NO_CONTEXT, mThirdEglContext);
1780
1781 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1782 mThirdEglContext));
1783 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1784 mThirdTextureRenderer = new TextureRenderer(THIRD_TEX_ID, mST);
1785 ASSERT_NO_FATAL_FAILURE(mThirdTextureRenderer->SetUp());
1786
1787 // Switch back to the primary context to start the tests.
1788 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1789 mEglContext));
Jamie Gennisce561372012-03-19 18:33:05 -07001790 }
1791
1792 virtual void TearDown() {
Jamie Gennis74bed552012-03-28 19:05:54 -07001793 if (mThirdEglContext != EGL_NO_CONTEXT) {
1794 eglDestroyContext(mEglDisplay, mThirdEglContext);
1795 }
Jamie Gennisce561372012-03-19 18:33:05 -07001796 if (mSecondEglContext != EGL_NO_CONTEXT) {
1797 eglDestroyContext(mEglDisplay, mSecondEglContext);
1798 }
1799 SurfaceTextureGLTest::TearDown();
1800 }
1801
1802 EGLContext mSecondEglContext;
Jamie Gennis74bed552012-03-28 19:05:54 -07001803 sp<TextureRenderer> mSecondTextureRenderer;
1804
1805 EGLContext mThirdEglContext;
1806 sp<TextureRenderer> mThirdTextureRenderer;
Jamie Gennisce561372012-03-19 18:33:05 -07001807};
1808
1809TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) {
1810 sp<FrameWaiter> fw(new FrameWaiter);
1811 mST->setFrameAvailableListener(fw);
1812
1813 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1814
1815 // Latch the texture contents on the primary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07001816 fw->waitForFrame();
1817 ASSERT_EQ(OK, mST->updateTexImage());
Jamie Gennisce561372012-03-19 18:33:05 -07001818
1819 // Attempt to latch the texture on the secondary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07001820 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
Jamie Gennisce561372012-03-19 18:33:05 -07001821 mSecondEglContext));
1822 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -07001823 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
1824}
1825
1826TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) {
1827 sp<FrameWaiter> fw(new FrameWaiter);
1828 mST->setFrameAvailableListener(fw);
1829
1830 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1831
1832 // Latch the texture contents on the primary context.
1833 fw->waitForFrame();
1834 ASSERT_EQ(OK, mST->updateTexImage());
1835
1836 // Detach from the primary context.
1837 ASSERT_EQ(OK, mST->detachFromContext());
1838
1839 // Check that the GL texture was deleted.
1840 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
1841}
1842
1843TEST_F(SurfaceTextureMultiContextGLTest,
1844 DetachFromContextSucceedsAfterProducerDisconnect) {
1845 sp<FrameWaiter> fw(new FrameWaiter);
1846 mST->setFrameAvailableListener(fw);
1847
1848 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1849
1850 // Latch the texture contents on the primary context.
1851 fw->waitForFrame();
1852 ASSERT_EQ(OK, mST->updateTexImage());
1853
1854 // Detach from the primary context.
1855 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
1856 ASSERT_EQ(OK, mST->detachFromContext());
1857
1858 // Check that the GL texture was deleted.
1859 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID));
1860}
1861
1862TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) {
1863 sp<FrameWaiter> fw(new FrameWaiter);
1864 mST->setFrameAvailableListener(fw);
1865
1866 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1867
1868 // Latch the texture contents on the primary context.
1869 fw->waitForFrame();
1870 ASSERT_EQ(OK, mST->updateTexImage());
1871
1872 // Attempt to detach from the primary context.
1873 mST->abandon();
1874 ASSERT_EQ(NO_INIT, mST->detachFromContext());
1875}
1876
1877TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) {
1878 sp<FrameWaiter> fw(new FrameWaiter);
1879 mST->setFrameAvailableListener(fw);
1880
1881 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1882
1883 // Latch the texture contents on the primary context.
1884 fw->waitForFrame();
1885 ASSERT_EQ(OK, mST->updateTexImage());
1886
1887 // Detach from the primary context.
1888 ASSERT_EQ(OK, mST->detachFromContext());
1889
1890 // Attempt to detach from the primary context again.
1891 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
1892}
1893
1894TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) {
1895 sp<FrameWaiter> fw(new FrameWaiter);
1896 mST->setFrameAvailableListener(fw);
1897
1898 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1899
1900 // Latch the texture contents on the primary context.
1901 fw->waitForFrame();
1902 ASSERT_EQ(OK, mST->updateTexImage());
1903
1904 // Make there be no current display.
1905 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1906 EGL_NO_CONTEXT));
1907 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1908
1909 // Attempt to detach from the primary context.
1910 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
1911}
1912
1913TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) {
1914 sp<FrameWaiter> fw(new FrameWaiter);
1915 mST->setFrameAvailableListener(fw);
1916
1917 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1918
1919 // Latch the texture contents on the primary context.
1920 fw->waitForFrame();
1921 ASSERT_EQ(OK, mST->updateTexImage());
1922
1923 // Make current context be incorrect.
1924 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1925 mSecondEglContext));
1926 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1927
1928 // Attempt to detach from the primary context.
1929 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext());
1930}
1931
1932TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) {
1933 sp<FrameWaiter> fw(new FrameWaiter);
1934 mST->setFrameAvailableListener(fw);
1935
1936 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1937
1938 // Detach from the primary context.
1939 ASSERT_EQ(OK, mST->detachFromContext());
1940
1941 // Attempt to latch the texture contents on the primary context.
1942 fw->waitForFrame();
1943 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
1944}
1945
1946TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) {
1947 sp<FrameWaiter> fw(new FrameWaiter);
1948 mST->setFrameAvailableListener(fw);
1949
1950 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1951
1952 // Latch the texture contents on the primary context.
1953 fw->waitForFrame();
1954 ASSERT_EQ(OK, mST->updateTexImage());
1955
1956 // Detach from the primary context.
1957 ASSERT_EQ(OK, mST->detachFromContext());
1958
1959 // Attach to the secondary context.
1960 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1961 mSecondEglContext));
1962 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
1963
1964 // Verify that the texture object was created and bound.
1965 GLint texBinding = -1;
1966 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
1967 EXPECT_EQ(SECOND_TEX_ID, texBinding);
1968
1969 // Try to use the texture from the secondary context.
1970 glClearColor(0.2, 0.2, 0.2, 0.2);
1971 glClear(GL_COLOR_BUFFER_BIT);
1972 glViewport(0, 0, 1, 1);
1973 mSecondTextureRenderer->drawTexture();
1974 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
1975 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
1976}
1977
1978TEST_F(SurfaceTextureMultiContextGLTest,
1979 AttachToContextSucceedsAfterProducerDisconnect) {
1980 sp<FrameWaiter> fw(new FrameWaiter);
1981 mST->setFrameAvailableListener(fw);
1982
1983 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
1984
1985 // Latch the texture contents on the primary context.
1986 fw->waitForFrame();
1987 ASSERT_EQ(OK, mST->updateTexImage());
1988
1989 // Detach from the primary context.
1990 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
1991 ASSERT_EQ(OK, mST->detachFromContext());
1992
1993 // Attach to the secondary context.
1994 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1995 mSecondEglContext));
1996 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
1997
1998 // Verify that the texture object was created and bound.
1999 GLint texBinding = -1;
2000 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2001 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2002
2003 // Try to use the texture from the secondary context.
2004 glClearColor(0.2, 0.2, 0.2, 0.2);
2005 glClear(GL_COLOR_BUFFER_BIT);
2006 glViewport(0, 0, 1, 1);
2007 mSecondTextureRenderer->drawTexture();
2008 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2009 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2010}
2011
2012TEST_F(SurfaceTextureMultiContextGLTest,
2013 AttachToContextSucceedsBeforeUpdateTexImage) {
2014 sp<FrameWaiter> fw(new FrameWaiter);
2015 mST->setFrameAvailableListener(fw);
2016
2017 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2018
2019 // Detach from the primary context.
2020 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU);
2021 ASSERT_EQ(OK, mST->detachFromContext());
2022
2023 // Attach to the secondary context.
2024 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2025 mSecondEglContext));
2026 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2027
2028 // Verify that the texture object was created and bound.
2029 GLint texBinding = -1;
2030 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2031 EXPECT_EQ(SECOND_TEX_ID, texBinding);
2032
2033 // Latch the texture contents on the primary context.
2034 fw->waitForFrame();
2035 ASSERT_EQ(OK, mST->updateTexImage());
2036
2037 // Try to use the texture from the secondary context.
2038 glClearColor(0.2, 0.2, 0.2, 0.2);
2039 glClear(GL_COLOR_BUFFER_BIT);
2040 glViewport(0, 0, 1, 1);
2041 mSecondTextureRenderer->drawTexture();
2042 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2043 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2044}
2045
2046TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) {
2047 sp<FrameWaiter> fw(new FrameWaiter);
2048 mST->setFrameAvailableListener(fw);
2049
2050 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2051
2052 // Latch the texture contents on the primary context.
2053 fw->waitForFrame();
2054 ASSERT_EQ(OK, mST->updateTexImage());
2055
2056 // Detach from the primary context.
2057 ASSERT_EQ(OK, mST->detachFromContext());
2058
2059 // Attempt to attach to the secondary context.
2060 mST->abandon();
2061
2062 // Attempt to attach to the primary context.
2063 ASSERT_EQ(NO_INIT, mST->attachToContext(SECOND_TEX_ID));
2064}
2065
2066TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) {
2067 sp<FrameWaiter> fw(new FrameWaiter);
2068 mST->setFrameAvailableListener(fw);
2069
2070 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2071
2072 // Latch the texture contents on the primary context.
2073 fw->waitForFrame();
2074 ASSERT_EQ(OK, mST->updateTexImage());
2075
2076 // Attempt to attach to the primary context.
2077 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2078}
2079
2080TEST_F(SurfaceTextureMultiContextGLTest,
2081 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) {
2082 sp<FrameWaiter> fw(new FrameWaiter);
2083 mST->setFrameAvailableListener(fw);
2084
2085 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2086
2087 // Attempt to attach to the primary context.
2088 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2089}
2090
2091TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) {
2092 sp<FrameWaiter> fw(new FrameWaiter);
2093 mST->setFrameAvailableListener(fw);
2094
2095 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2096
2097 // Latch the texture contents on the primary context.
2098 fw->waitForFrame();
2099 ASSERT_EQ(OK, mST->updateTexImage());
2100
2101 // Detach from the primary context.
2102 ASSERT_EQ(OK, mST->detachFromContext());
2103
2104 // Make there be no current display.
2105 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
2106 EGL_NO_CONTEXT));
2107 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2108
2109 // Attempt to attach with no context current.
2110 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID));
2111}
2112
2113TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) {
2114 sp<FrameWaiter> fw(new FrameWaiter);
2115 mST->setFrameAvailableListener(fw);
2116
2117 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2118
2119 // Latch the texture contents on the primary context.
2120 fw->waitForFrame();
2121 ASSERT_EQ(OK, mST->updateTexImage());
2122
2123 // Detach from the primary context.
2124 ASSERT_EQ(OK, mST->detachFromContext());
2125
2126 // Attach to the secondary context.
2127 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2128 mSecondEglContext));
2129 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2130
2131 // Detach from the secondary context.
2132 ASSERT_EQ(OK, mST->detachFromContext());
2133
2134 // Attach to the tertiary context.
2135 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2136 mThirdEglContext));
2137 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2138
2139 // Verify that the texture object was created and bound.
2140 GLint texBinding = -1;
2141 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2142 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2143
2144 // Try to use the texture from the tertiary context.
2145 glClearColor(0.2, 0.2, 0.2, 0.2);
2146 glClear(GL_COLOR_BUFFER_BIT);
2147 glViewport(0, 0, 1, 1);
2148 mThirdTextureRenderer->drawTexture();
2149 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2150 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
2151}
2152
2153TEST_F(SurfaceTextureMultiContextGLTest,
2154 AttachToContextSucceedsTwiceBeforeUpdateTexImage) {
2155 sp<FrameWaiter> fw(new FrameWaiter);
2156 mST->setFrameAvailableListener(fw);
2157
2158 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2159
2160 // Detach from the primary context.
2161 ASSERT_EQ(OK, mST->detachFromContext());
2162
2163 // Attach to the secondary context.
2164 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2165 mSecondEglContext));
2166 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID));
2167
2168 // Detach from the secondary context.
2169 ASSERT_EQ(OK, mST->detachFromContext());
2170
2171 // Attach to the tertiary context.
2172 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
2173 mThirdEglContext));
2174 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID));
2175
2176 // Verify that the texture object was created and bound.
2177 GLint texBinding = -1;
2178 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding);
2179 EXPECT_EQ(THIRD_TEX_ID, texBinding);
2180
2181 // Latch the texture contents on the tertiary context.
2182 fw->waitForFrame();
2183 ASSERT_EQ(OK, mST->updateTexImage());
2184
2185 // Try to use the texture from the tertiary context.
2186 glClearColor(0.2, 0.2, 0.2, 0.2);
2187 glClear(GL_COLOR_BUFFER_BIT);
2188 glViewport(0, 0, 1, 1);
2189 mThirdTextureRenderer->drawTexture();
2190 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2191 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
Jamie Gennisce561372012-03-19 18:33:05 -07002192}
2193
Jamie Gennis5451d152011-06-08 09:40:45 -07002194} // namespace android