SurfaceTexture_test.cpp revision 8f938a53385a3c6d1c6aa24b3f38437bb2cc47ae
1/* 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 17#define LOG_TAG "SurfaceTexture_test" 18//#define LOG_NDEBUG 0 19 20#include <gtest/gtest.h> 21#include <gui/GLConsumer.h> 22#include <ui/GraphicBuffer.h> 23#include <utils/String8.h> 24#include <utils/threads.h> 25 26#include <gui/ISurfaceComposer.h> 27#include <gui/Surface.h> 28#include <gui/SurfaceComposerClient.h> 29 30#include <EGL/egl.h> 31#include <EGL/eglext.h> 32#include <GLES/gl.h> 33#include <GLES/glext.h> 34#include <GLES2/gl2.h> 35#include <GLES2/gl2ext.h> 36 37#include <ui/FramebufferNativeWindow.h> 38#include <utils/UniquePtr.h> 39#include <android/native_window.h> 40 41namespace android { 42 43class GLTest : public ::testing::Test { 44protected: 45 46 GLTest(): 47 mEglDisplay(EGL_NO_DISPLAY), 48 mEglSurface(EGL_NO_SURFACE), 49 mEglContext(EGL_NO_CONTEXT) { 50 } 51 52 virtual void SetUp() { 53 const ::testing::TestInfo* const testInfo = 54 ::testing::UnitTest::GetInstance()->current_test_info(); 55 ALOGV("Begin test: %s.%s", testInfo->test_case_name(), 56 testInfo->name()); 57 58 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); 59 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 60 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay); 61 62 EGLint majorVersion; 63 EGLint minorVersion; 64 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion)); 65 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 66 RecordProperty("EglVersionMajor", majorVersion); 67 RecordProperty("EglVersionMajor", minorVersion); 68 69 EGLint numConfigs = 0; 70 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig, 71 1, &numConfigs)); 72 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 73 74 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS"); 75 if (displaySecsEnv != NULL) { 76 mDisplaySecs = atoi(displaySecsEnv); 77 if (mDisplaySecs < 0) { 78 mDisplaySecs = 0; 79 } 80 } else { 81 mDisplaySecs = 0; 82 } 83 84 if (mDisplaySecs > 0) { 85 mComposerClient = new SurfaceComposerClient; 86 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); 87 88 mSurfaceControl = mComposerClient->createSurface( 89 String8("Test Surface"), 90 getSurfaceWidth(), getSurfaceHeight(), 91 PIXEL_FORMAT_RGB_888, 0); 92 93 ASSERT_TRUE(mSurfaceControl != NULL); 94 ASSERT_TRUE(mSurfaceControl->isValid()); 95 96 SurfaceComposerClient::openGlobalTransaction(); 97 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF)); 98 ASSERT_EQ(NO_ERROR, mSurfaceControl->show()); 99 SurfaceComposerClient::closeGlobalTransaction(); 100 101 sp<ANativeWindow> window = mSurfaceControl->getSurface(); 102 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, 103 window.get(), NULL); 104 } else { 105 EGLint pbufferAttribs[] = { 106 EGL_WIDTH, getSurfaceWidth(), 107 EGL_HEIGHT, getSurfaceHeight(), 108 EGL_NONE }; 109 110 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig, 111 pbufferAttribs); 112 } 113 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 114 ASSERT_NE(EGL_NO_SURFACE, mEglSurface); 115 116 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT, 117 getContextAttribs()); 118 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 119 ASSERT_NE(EGL_NO_CONTEXT, mEglContext); 120 121 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 122 mEglContext)); 123 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 124 125 EGLint w, h; 126 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w)); 127 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 128 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h)); 129 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 130 RecordProperty("EglSurfaceWidth", w); 131 RecordProperty("EglSurfaceHeight", h); 132 133 glViewport(0, 0, w, h); 134 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 135 } 136 137 virtual void TearDown() { 138 // Display the result 139 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) { 140 eglSwapBuffers(mEglDisplay, mEglSurface); 141 sleep(mDisplaySecs); 142 } 143 144 if (mComposerClient != NULL) { 145 mComposerClient->dispose(); 146 } 147 if (mEglContext != EGL_NO_CONTEXT) { 148 eglDestroyContext(mEglDisplay, mEglContext); 149 } 150 if (mEglSurface != EGL_NO_SURFACE) { 151 eglDestroySurface(mEglDisplay, mEglSurface); 152 } 153 if (mEglDisplay != EGL_NO_DISPLAY) { 154 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 155 EGL_NO_CONTEXT); 156 eglTerminate(mEglDisplay); 157 } 158 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 159 160 const ::testing::TestInfo* const testInfo = 161 ::testing::UnitTest::GetInstance()->current_test_info(); 162 ALOGV("End test: %s.%s", testInfo->test_case_name(), 163 testInfo->name()); 164 } 165 166 virtual EGLint const* getConfigAttribs() { 167 static EGLint sDefaultConfigAttribs[] = { 168 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, 169 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 170 EGL_RED_SIZE, 8, 171 EGL_GREEN_SIZE, 8, 172 EGL_BLUE_SIZE, 8, 173 EGL_ALPHA_SIZE, 8, 174 EGL_DEPTH_SIZE, 16, 175 EGL_STENCIL_SIZE, 8, 176 EGL_NONE }; 177 178 return sDefaultConfigAttribs; 179 } 180 181 virtual EGLint const* getContextAttribs() { 182 static EGLint sDefaultContextAttribs[] = { 183 EGL_CONTEXT_CLIENT_VERSION, 2, 184 EGL_NONE }; 185 186 return sDefaultContextAttribs; 187 } 188 189 virtual EGLint getSurfaceWidth() { 190 return 512; 191 } 192 193 virtual EGLint getSurfaceHeight() { 194 return 512; 195 } 196 197 ::testing::AssertionResult checkPixel(int x, int y, int r, 198 int g, int b, int a, int tolerance=2) { 199 GLubyte pixel[4]; 200 String8 msg; 201 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel); 202 GLenum err = glGetError(); 203 if (err != GL_NO_ERROR) { 204 msg += String8::format("error reading pixel: %#x", err); 205 while ((err = glGetError()) != GL_NO_ERROR) { 206 msg += String8::format(", %#x", err); 207 } 208 return ::testing::AssertionFailure( 209 ::testing::Message(msg.string())); 210 } 211 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) { 212 msg += String8::format("r(%d isn't %d)", pixel[0], r); 213 } 214 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) { 215 if (!msg.isEmpty()) { 216 msg += " "; 217 } 218 msg += String8::format("g(%d isn't %d)", pixel[1], g); 219 } 220 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) { 221 if (!msg.isEmpty()) { 222 msg += " "; 223 } 224 msg += String8::format("b(%d isn't %d)", pixel[2], b); 225 } 226 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) { 227 if (!msg.isEmpty()) { 228 msg += " "; 229 } 230 msg += String8::format("a(%d isn't %d)", pixel[3], a); 231 } 232 if (!msg.isEmpty()) { 233 return ::testing::AssertionFailure( 234 ::testing::Message(msg.string())); 235 } else { 236 return ::testing::AssertionSuccess(); 237 } 238 } 239 240 ::testing::AssertionResult assertRectEq(const Rect &r1, 241 const Rect &r2, int tolerance=1) { 242 243 String8 msg; 244 245 if (abs(r1.left - r2.left) > tolerance) { 246 msg += String8::format("left(%d isn't %d)", r1.left, r2.left); 247 } 248 if (abs(r1.top - r2.top) > tolerance) { 249 if (!msg.isEmpty()) { 250 msg += " "; 251 } 252 msg += String8::format("top(%d isn't %d)", r1.top, r2.top); 253 } 254 if (abs(r1.right - r2.right) > tolerance) { 255 if (!msg.isEmpty()) { 256 msg += " "; 257 } 258 msg += String8::format("right(%d isn't %d)", r1.right, r2.right); 259 } 260 if (abs(r1.bottom - r2.bottom) > tolerance) { 261 if (!msg.isEmpty()) { 262 msg += " "; 263 } 264 msg += String8::format("bottom(%d isn't %d)", r1.bottom, r2.bottom); 265 } 266 if (!msg.isEmpty()) { 267 msg += String8::format(" R1: [%d %d %d %d] R2: [%d %d %d %d]", 268 r1.left, r1.top, r1.right, r1.bottom, 269 r2.left, r2.top, r2.right, r2.bottom); 270 fprintf(stderr, "assertRectEq: %s\n", msg.string()); 271 return ::testing::AssertionFailure( 272 ::testing::Message(msg.string())); 273 } else { 274 return ::testing::AssertionSuccess(); 275 } 276 } 277 278 int mDisplaySecs; 279 sp<SurfaceComposerClient> mComposerClient; 280 sp<SurfaceControl> mSurfaceControl; 281 282 EGLDisplay mEglDisplay; 283 EGLSurface mEglSurface; 284 EGLContext mEglContext; 285 EGLConfig mGlConfig; 286}; 287 288static void loadShader(GLenum shaderType, const char* pSource, 289 GLuint* outShader) { 290 GLuint shader = glCreateShader(shaderType); 291 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 292 if (shader) { 293 glShaderSource(shader, 1, &pSource, NULL); 294 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 295 glCompileShader(shader); 296 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 297 GLint compiled = 0; 298 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); 299 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 300 if (!compiled) { 301 GLint infoLen = 0; 302 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); 303 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 304 if (infoLen) { 305 char* buf = (char*) malloc(infoLen); 306 if (buf) { 307 glGetShaderInfoLog(shader, infoLen, NULL, buf); 308 printf("Shader compile log:\n%s\n", buf); 309 free(buf); 310 FAIL(); 311 } 312 } else { 313 char* buf = (char*) malloc(0x1000); 314 if (buf) { 315 glGetShaderInfoLog(shader, 0x1000, NULL, buf); 316 printf("Shader compile log:\n%s\n", buf); 317 free(buf); 318 FAIL(); 319 } 320 } 321 glDeleteShader(shader); 322 shader = 0; 323 } 324 } 325 ASSERT_TRUE(shader != 0); 326 *outShader = shader; 327} 328 329static void createProgram(const char* pVertexSource, 330 const char* pFragmentSource, GLuint* outPgm) { 331 GLuint vertexShader, fragmentShader; 332 { 333 SCOPED_TRACE("compiling vertex shader"); 334 ASSERT_NO_FATAL_FAILURE(loadShader(GL_VERTEX_SHADER, pVertexSource, 335 &vertexShader)); 336 } 337 { 338 SCOPED_TRACE("compiling fragment shader"); 339 ASSERT_NO_FATAL_FAILURE(loadShader(GL_FRAGMENT_SHADER, pFragmentSource, 340 &fragmentShader)); 341 } 342 343 GLuint program = glCreateProgram(); 344 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 345 if (program) { 346 glAttachShader(program, vertexShader); 347 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 348 glAttachShader(program, fragmentShader); 349 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 350 glLinkProgram(program); 351 GLint linkStatus = GL_FALSE; 352 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); 353 if (linkStatus != GL_TRUE) { 354 GLint bufLength = 0; 355 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); 356 if (bufLength) { 357 char* buf = (char*) malloc(bufLength); 358 if (buf) { 359 glGetProgramInfoLog(program, bufLength, NULL, buf); 360 printf("Program link log:\n%s\n", buf); 361 free(buf); 362 FAIL(); 363 } 364 } 365 glDeleteProgram(program); 366 program = 0; 367 } 368 } 369 glDeleteShader(vertexShader); 370 glDeleteShader(fragmentShader); 371 ASSERT_TRUE(program != 0); 372 *outPgm = program; 373} 374 375static int abs(int value) { 376 return value > 0 ? value : -value; 377} 378 379 380// XXX: Code above this point should live elsewhere 381 382class MultiTextureConsumerTest : public GLTest { 383protected: 384 enum { TEX_ID = 123 }; 385 386 virtual void SetUp() { 387 GLTest::SetUp(); 388 sp<BufferQueue> bq = new BufferQueue(); 389 mGlConsumer = new GLConsumer(bq, TEX_ID); 390 mSurface = new Surface(mGlConsumer->getBufferQueue()); 391 mANW = mSurface.get(); 392 393 } 394 virtual void TearDown() { 395 GLTest::TearDown(); 396 } 397 virtual EGLint const* getContextAttribs() { 398 return NULL; 399 } 400 virtual EGLint const* getConfigAttribs() { 401 static EGLint sDefaultConfigAttribs[] = { 402 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, 403 EGL_RED_SIZE, 8, 404 EGL_GREEN_SIZE, 8, 405 EGL_BLUE_SIZE, 8, 406 EGL_ALPHA_SIZE, 8, 407 EGL_NONE }; 408 409 return sDefaultConfigAttribs; 410 } 411 sp<GLConsumer> mGlConsumer; 412 sp<Surface> mSurface; 413 ANativeWindow* mANW; 414}; 415 416 417TEST_F(MultiTextureConsumerTest, EGLImageTargetWorks) { 418 ANativeWindow_Buffer buffer; 419 420 ASSERT_EQ(native_window_set_usage(mANW, GRALLOC_USAGE_SW_WRITE_OFTEN), NO_ERROR); 421 ASSERT_EQ(native_window_set_buffers_format(mANW, HAL_PIXEL_FORMAT_RGBA_8888), NO_ERROR); 422 423 glShadeModel(GL_FLAT); 424 glDisable(GL_DITHER); 425 glDisable(GL_CULL_FACE); 426 glViewport(0, 0, getSurfaceWidth(), getSurfaceHeight()); 427 glOrthof(0, getSurfaceWidth(), 0, getSurfaceHeight(), 0, 1); 428 glEnableClientState(GL_VERTEX_ARRAY); 429 glColor4f(1, 1, 1, 1); 430 431 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID); 432 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 433 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 434 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 435 glTexParameterx(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 436 437 uint32_t texel = 0x80808080; 438 glBindTexture(GL_TEXTURE_2D, TEX_ID+1); 439 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &texel); 440 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 441 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 442 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 443 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 444 445 glActiveTexture(GL_TEXTURE1); 446 glBindTexture(GL_TEXTURE_2D, TEX_ID+1); 447 glEnable(GL_TEXTURE_2D); 448 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 449 450 glActiveTexture(GL_TEXTURE0); 451 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID); 452 glEnable(GL_TEXTURE_EXTERNAL_OES); 453 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 454 455 glClear(GL_COLOR_BUFFER_BIT); 456 for (int i=0 ; i<8 ; i++) { 457 mSurface->lock(&buffer, NULL); 458 memset(buffer.bits, (i&7) * 0x20, buffer.stride * buffer.height * 4); 459 mSurface->unlockAndPost(); 460 461 mGlConsumer->updateTexImage(); 462 463 GLfloat vertices[][2] = { {i*16.0f, 0}, {(i+1)*16.0f, 0}, {(i+1)*16.0f, 16.0f}, {i*16.0f, 16.0f} }; 464 glVertexPointer(2, GL_FLOAT, 0, vertices); 465 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 466 467 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 468 } 469 470 for (int i=0 ; i<8 ; i++) { 471 EXPECT_TRUE(checkPixel(i*16 + 8, 8, i*16, i*16, i*16, i*16, 0)); 472 } 473} 474 475 476 477class SurfaceTextureGLTest : public GLTest { 478protected: 479 enum { TEX_ID = 123 }; 480 481 virtual void SetUp() { 482 GLTest::SetUp(); 483 sp<BufferQueue> bq = new BufferQueue(); 484 mST = new GLConsumer(bq, TEX_ID); 485 mSTC = new Surface(mST->getBufferQueue()); 486 mANW = mSTC; 487 mTextureRenderer = new TextureRenderer(TEX_ID, mST); 488 ASSERT_NO_FATAL_FAILURE(mTextureRenderer->SetUp()); 489 mFW = new FrameWaiter; 490 mST->setFrameAvailableListener(mFW); 491 } 492 493 virtual void TearDown() { 494 mANW.clear(); 495 mSTC.clear(); 496 mST.clear(); 497 GLTest::TearDown(); 498 } 499 500 void drawTexture() { 501 mTextureRenderer->drawTexture(); 502 } 503 504 class TextureRenderer: public RefBase { 505 public: 506 TextureRenderer(GLuint texName, const sp<GLConsumer>& st): 507 mTexName(texName), 508 mST(st) { 509 } 510 511 void SetUp() { 512 const char vsrc[] = 513 "attribute vec4 vPosition;\n" 514 "varying vec2 texCoords;\n" 515 "uniform mat4 texMatrix;\n" 516 "void main() {\n" 517 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n" 518 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n" 519 " gl_Position = vPosition;\n" 520 "}\n"; 521 522 const char fsrc[] = 523 "#extension GL_OES_EGL_image_external : require\n" 524 "precision mediump float;\n" 525 "uniform samplerExternalOES texSampler;\n" 526 "varying vec2 texCoords;\n" 527 "void main() {\n" 528 " gl_FragColor = texture2D(texSampler, texCoords);\n" 529 "}\n"; 530 531 { 532 SCOPED_TRACE("creating shader program"); 533 ASSERT_NO_FATAL_FAILURE(createProgram(vsrc, fsrc, &mPgm)); 534 } 535 536 mPositionHandle = glGetAttribLocation(mPgm, "vPosition"); 537 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 538 ASSERT_NE(-1, mPositionHandle); 539 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler"); 540 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 541 ASSERT_NE(-1, mTexSamplerHandle); 542 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix"); 543 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 544 ASSERT_NE(-1, mTexMatrixHandle); 545 } 546 547 // drawTexture draws the GLConsumer over the entire GL viewport. 548 void drawTexture() { 549 static const GLfloat triangleVertices[] = { 550 -1.0f, 1.0f, 551 -1.0f, -1.0f, 552 1.0f, -1.0f, 553 1.0f, 1.0f, 554 }; 555 556 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, 557 triangleVertices); 558 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 559 glEnableVertexAttribArray(mPositionHandle); 560 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 561 562 glUseProgram(mPgm); 563 glUniform1i(mTexSamplerHandle, 0); 564 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 565 glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTexName); 566 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 567 568 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as 569 // they're setting the defautls for that target, but when hacking 570 // things to use GL_TEXTURE_2D they are needed to achieve the same 571 // behavior. 572 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, 573 GL_LINEAR); 574 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 575 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, 576 GL_LINEAR); 577 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 578 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, 579 GL_CLAMP_TO_EDGE); 580 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 581 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, 582 GL_CLAMP_TO_EDGE); 583 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 584 585 GLfloat texMatrix[16]; 586 mST->getTransformMatrix(texMatrix); 587 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix); 588 589 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 590 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 591 } 592 593 GLuint mTexName; 594 sp<GLConsumer> mST; 595 GLuint mPgm; 596 GLint mPositionHandle; 597 GLint mTexSamplerHandle; 598 GLint mTexMatrixHandle; 599 }; 600 601 class FrameWaiter : public GLConsumer::FrameAvailableListener { 602 public: 603 FrameWaiter(): 604 mPendingFrames(0) { 605 } 606 607 void waitForFrame() { 608 Mutex::Autolock lock(mMutex); 609 while (mPendingFrames == 0) { 610 mCondition.wait(mMutex); 611 } 612 mPendingFrames--; 613 } 614 615 virtual void onFrameAvailable() { 616 Mutex::Autolock lock(mMutex); 617 mPendingFrames++; 618 mCondition.signal(); 619 } 620 621 int mPendingFrames; 622 Mutex mMutex; 623 Condition mCondition; 624 }; 625 626 // Note that GLConsumer will lose the notifications 627 // onBuffersReleased and onFrameAvailable as there is currently 628 // no way to forward the events. This DisconnectWaiter will not let the 629 // disconnect finish until finishDisconnect() is called. It will 630 // also block until a disconnect is called 631 class DisconnectWaiter : public BufferQueue::ConsumerListener { 632 public: 633 DisconnectWaiter () : 634 mWaitForDisconnect(false), 635 mPendingFrames(0) { 636 } 637 638 void waitForFrame() { 639 Mutex::Autolock lock(mMutex); 640 while (mPendingFrames == 0) { 641 mFrameCondition.wait(mMutex); 642 } 643 mPendingFrames--; 644 } 645 646 virtual void onFrameAvailable() { 647 Mutex::Autolock lock(mMutex); 648 mPendingFrames++; 649 mFrameCondition.signal(); 650 } 651 652 virtual void onBuffersReleased() { 653 Mutex::Autolock lock(mMutex); 654 while (!mWaitForDisconnect) { 655 mDisconnectCondition.wait(mMutex); 656 } 657 } 658 659 void finishDisconnect() { 660 Mutex::Autolock lock(mMutex); 661 mWaitForDisconnect = true; 662 mDisconnectCondition.signal(); 663 } 664 665 private: 666 Mutex mMutex; 667 668 bool mWaitForDisconnect; 669 Condition mDisconnectCondition; 670 671 int mPendingFrames; 672 Condition mFrameCondition; 673 }; 674 675 sp<GLConsumer> mST; 676 sp<Surface> mSTC; 677 sp<ANativeWindow> mANW; 678 sp<TextureRenderer> mTextureRenderer; 679 sp<FrameWaiter> mFW; 680}; 681 682// Fill a YV12 buffer with a multi-colored checkerboard pattern 683void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) { 684 const int blockWidth = w > 16 ? w / 16 : 1; 685 const int blockHeight = h > 16 ? h / 16 : 1; 686 const int yuvTexOffsetY = 0; 687 int yuvTexStrideY = stride; 688 int yuvTexOffsetV = yuvTexStrideY * h; 689 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 690 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2; 691 int yuvTexStrideU = yuvTexStrideV; 692 for (int x = 0; x < w; x++) { 693 for (int y = 0; y < h; y++) { 694 int parityX = (x / blockWidth) & 1; 695 int parityY = (y / blockHeight) & 1; 696 unsigned char intensity = (parityX ^ parityY) ? 63 : 191; 697 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity; 698 if (x < w / 2 && y < h / 2) { 699 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity; 700 if (x * 2 < w / 2 && y * 2 < h / 2) { 701 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] = 702 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] = 703 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] = 704 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] = 705 intensity; 706 } 707 } 708 } 709 } 710} 711 712// Fill a YV12 buffer with red outside a given rectangle and green inside it. 713void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride, 714 const android_native_rect_t& rect) { 715 const int yuvTexOffsetY = 0; 716 int yuvTexStrideY = stride; 717 int yuvTexOffsetV = yuvTexStrideY * h; 718 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 719 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2; 720 int yuvTexStrideU = yuvTexStrideV; 721 for (int x = 0; x < w; x++) { 722 for (int y = 0; y < h; y++) { 723 bool inside = rect.left <= x && x < rect.right && 724 rect.top <= y && y < rect.bottom; 725 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64; 726 if (x < w / 2 && y < h / 2) { 727 bool inside = rect.left <= 2*x && 2*x < rect.right && 728 rect.top <= 2*y && 2*y < rect.bottom; 729 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16; 730 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] = 731 inside ? 16 : 255; 732 } 733 } 734 } 735} 736 737void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) { 738 const size_t PIXEL_SIZE = 4; 739 for (int x = 0; x < w; x++) { 740 for (int y = 0; y < h; y++) { 741 off_t offset = (y * stride + x) * PIXEL_SIZE; 742 for (int c = 0; c < 4; c++) { 743 int parityX = (x / (1 << (c+2))) & 1; 744 int parityY = (y / (1 << (c+2))) & 1; 745 buf[offset + c] = (parityX ^ parityY) ? 231 : 35; 746 } 747 } 748 } 749} 750 751void fillRGBA8BufferSolid(uint8_t* buf, int w, int h, int stride, uint8_t r, 752 uint8_t g, uint8_t b, uint8_t a) { 753 const size_t PIXEL_SIZE = 4; 754 for (int y = 0; y < h; y++) { 755 for (int x = 0; x < h; x++) { 756 off_t offset = (y * stride + x) * PIXEL_SIZE; 757 buf[offset + 0] = r; 758 buf[offset + 1] = g; 759 buf[offset + 2] = b; 760 buf[offset + 3] = a; 761 } 762 } 763} 764 765// Produce a single RGBA8 frame by filling a buffer with a checkerboard pattern 766// using the CPU. This assumes that the ANativeWindow is already configured to 767// allow this to be done (e.g. the format is set to RGBA8). 768// 769// Calls to this function should be wrapped in an ASSERT_NO_FATAL_FAILURE(). 770void produceOneRGBA8Frame(const sp<ANativeWindow>& anw) { 771 android_native_buffer_t* anb; 772 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(), 773 &anb)); 774 ASSERT_TRUE(anb != NULL); 775 776 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 777 778 uint8_t* img = NULL; 779 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, 780 (void**)(&img))); 781 fillRGBA8Buffer(img, buf->getWidth(), buf->getHeight(), buf->getStride()); 782 ASSERT_EQ(NO_ERROR, buf->unlock()); 783 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer(), 784 -1)); 785} 786 787TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) { 788 const int texWidth = 64; 789 const int texHeight = 66; 790 791 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 792 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); 793 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 794 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 795 796 ANativeWindowBuffer* anb; 797 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), 798 &anb)); 799 ASSERT_TRUE(anb != NULL); 800 801 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 802 803 // Fill the buffer with the a checkerboard pattern 804 uint8_t* img = NULL; 805 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 806 fillYV12Buffer(img, texWidth, texHeight, buf->getStride()); 807 buf->unlock(); 808 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), 809 -1)); 810 811 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 812 813 glClearColor(0.2, 0.2, 0.2, 0.2); 814 glClear(GL_COLOR_BUFFER_BIT); 815 816 glViewport(0, 0, texWidth, texHeight); 817 drawTexture(); 818 819 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255, 3)); 820 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255, 3)); 821 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255, 3)); 822 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255, 3)); 823 824 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255, 3)); 825 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255, 3)); 826 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255, 3)); 827 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255, 3)); 828 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255, 3)); 829 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255, 3)); 830 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255, 3)); 831} 832 833TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) { 834 const int texWidth = 64; 835 const int texHeight = 64; 836 837 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 838 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); 839 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 840 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 841 842 ANativeWindowBuffer* anb; 843 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), 844 &anb)); 845 ASSERT_TRUE(anb != NULL); 846 847 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 848 849 // Fill the buffer with the a checkerboard pattern 850 uint8_t* img = NULL; 851 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 852 fillYV12Buffer(img, texWidth, texHeight, buf->getStride()); 853 buf->unlock(); 854 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), 855 -1)); 856 857 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 858 859 glClearColor(0.2, 0.2, 0.2, 0.2); 860 glClear(GL_COLOR_BUFFER_BIT); 861 862 glViewport(0, 0, texWidth, texHeight); 863 drawTexture(); 864 865 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255)); 866 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255)); 867 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255)); 868 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255)); 869 870 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255)); 871 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255)); 872 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255)); 873 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255)); 874 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255)); 875 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255)); 876 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255)); 877} 878 879TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) { 880 const int texWidth = 64; 881 const int texHeight = 66; 882 883 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 884 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); 885 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 886 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 887 888 android_native_rect_t crops[] = { 889 {4, 6, 22, 36}, 890 {0, 6, 22, 36}, 891 {4, 0, 22, 36}, 892 {4, 6, texWidth, 36}, 893 {4, 6, 22, texHeight}, 894 }; 895 896 for (int i = 0; i < 5; i++) { 897 const android_native_rect_t& crop(crops[i]); 898 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }", 899 crop.left, crop.top, crop.right, crop.bottom).string()); 900 901 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop)); 902 903 ANativeWindowBuffer* anb; 904 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), 905 &anb)); 906 ASSERT_TRUE(anb != NULL); 907 908 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 909 910 uint8_t* img = NULL; 911 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 912 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop); 913 buf->unlock(); 914 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), 915 buf->getNativeBuffer(), -1)); 916 917 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 918 919 glClearColor(0.2, 0.2, 0.2, 0.2); 920 glClear(GL_COLOR_BUFFER_BIT); 921 922 glViewport(0, 0, 64, 64); 923 drawTexture(); 924 925 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255)); 926 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255)); 927 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255)); 928 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255)); 929 930 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255)); 931 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255)); 932 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255)); 933 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255)); 934 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255)); 935 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255)); 936 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255)); 937 } 938} 939 940// This test is intended to catch synchronization bugs between the CPU-written 941// and GPU-read buffers. 942TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) { 943 enum { texWidth = 16 }; 944 enum { texHeight = 16 }; 945 enum { numFrames = 1024 }; 946 947 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true)); 948 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2)); 949 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 950 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); 951 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 952 GRALLOC_USAGE_SW_WRITE_OFTEN)); 953 954 struct TestPixel { 955 int x; 956 int y; 957 }; 958 const TestPixel testPixels[] = { 959 { 4, 11 }, 960 { 12, 14 }, 961 { 7, 2 }, 962 }; 963 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])}; 964 965 class ProducerThread : public Thread { 966 public: 967 ProducerThread(const sp<ANativeWindow>& anw, 968 const TestPixel* testPixels): 969 mANW(anw), 970 mTestPixels(testPixels) { 971 } 972 973 virtual ~ProducerThread() { 974 } 975 976 virtual bool threadLoop() { 977 for (int i = 0; i < numFrames; i++) { 978 ANativeWindowBuffer* anb; 979 if (native_window_dequeue_buffer_and_wait(mANW.get(), 980 &anb) != NO_ERROR) { 981 return false; 982 } 983 if (anb == NULL) { 984 return false; 985 } 986 987 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 988 989 const int yuvTexOffsetY = 0; 990 int stride = buf->getStride(); 991 int yuvTexStrideY = stride; 992 int yuvTexOffsetV = yuvTexStrideY * texHeight; 993 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 994 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2; 995 int yuvTexStrideU = yuvTexStrideV; 996 997 uint8_t* img = NULL; 998 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 999 1000 // Gray out all the test pixels first, so we're more likely to 1001 // see a failure if GL is still texturing from the buffer we 1002 // just dequeued. 1003 for (int j = 0; j < numTestPixels; j++) { 1004 int x = mTestPixels[j].x; 1005 int y = mTestPixels[j].y; 1006 uint8_t value = 128; 1007 img[y*stride + x] = value; 1008 } 1009 1010 // Fill the buffer with gray. 1011 for (int y = 0; y < texHeight; y++) { 1012 for (int x = 0; x < texWidth; x++) { 1013 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128; 1014 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128; 1015 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128; 1016 } 1017 } 1018 1019 // Set the test pixels to either white or black. 1020 for (int j = 0; j < numTestPixels; j++) { 1021 int x = mTestPixels[j].x; 1022 int y = mTestPixels[j].y; 1023 uint8_t value = 0; 1024 if (j == (i % numTestPixels)) { 1025 value = 255; 1026 } 1027 img[y*stride + x] = value; 1028 } 1029 1030 buf->unlock(); 1031 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), -1) 1032 != NO_ERROR) { 1033 return false; 1034 } 1035 } 1036 return false; 1037 } 1038 1039 sp<ANativeWindow> mANW; 1040 const TestPixel* mTestPixels; 1041 }; 1042 1043 sp<Thread> pt(new ProducerThread(mANW, testPixels)); 1044 pt->run(); 1045 1046 glViewport(0, 0, texWidth, texHeight); 1047 1048 glClearColor(0.2, 0.2, 0.2, 0.2); 1049 glClear(GL_COLOR_BUFFER_BIT); 1050 1051 // We wait for the first two frames up front so that the producer will be 1052 // likely to dequeue the buffer that's currently being textured from. 1053 mFW->waitForFrame(); 1054 mFW->waitForFrame(); 1055 1056 for (int i = 0; i < numFrames; i++) { 1057 SCOPED_TRACE(String8::format("frame %d", i).string()); 1058 1059 // We must wait for each frame to come in because if we ever do an 1060 // updateTexImage call that doesn't consume a newly available buffer 1061 // then the producer and consumer will get out of sync, which will cause 1062 // a deadlock. 1063 if (i > 1) { 1064 mFW->waitForFrame(); 1065 } 1066 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1067 drawTexture(); 1068 1069 for (int j = 0; j < numTestPixels; j++) { 1070 int x = testPixels[j].x; 1071 int y = testPixels[j].y; 1072 uint8_t value = 0; 1073 if (j == (i % numTestPixels)) { 1074 // We must y-invert the texture coords 1075 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255)); 1076 } else { 1077 // We must y-invert the texture coords 1078 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255)); 1079 } 1080 } 1081 } 1082 1083 pt->requestExitAndWait(); 1084} 1085 1086TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) { 1087 const int texWidth = 64; 1088 const int texHeight = 66; 1089 1090 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 1091 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888)); 1092 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 1093 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 1094 1095 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 1096 1097 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1098 1099 glClearColor(0.2, 0.2, 0.2, 0.2); 1100 glClear(GL_COLOR_BUFFER_BIT); 1101 1102 glViewport(0, 0, texWidth, texHeight); 1103 drawTexture(); 1104 1105 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35)); 1106 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231)); 1107 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231)); 1108 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35)); 1109 1110 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231)); 1111 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35)); 1112 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35)); 1113 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35)); 1114 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231)); 1115 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231)); 1116 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231)); 1117 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231)); 1118 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35)); 1119 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35)); 1120 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231)); 1121 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231)); 1122 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231)); 1123 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231)); 1124 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35)); 1125 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231)); 1126} 1127 1128TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) { 1129 const int texWidth = 64; 1130 const int texHeight = 64; 1131 1132 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 1133 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888)); 1134 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 1135 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 1136 1137 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 1138 1139 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1140 1141 glClearColor(0.2, 0.2, 0.2, 0.2); 1142 glClear(GL_COLOR_BUFFER_BIT); 1143 1144 glViewport(0, 0, texWidth, texHeight); 1145 drawTexture(); 1146 1147 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231)); 1148 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35)); 1149 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231)); 1150 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35)); 1151 1152 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35)); 1153 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231)); 1154 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231)); 1155 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35)); 1156 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35)); 1157 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231)); 1158 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35)); 1159 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35)); 1160 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35)); 1161 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231)); 1162 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231)); 1163 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231)); 1164 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231)); 1165 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35)); 1166 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231)); 1167 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35)); 1168} 1169 1170// Tests if GLConsumer and BufferQueue are robust enough 1171// to handle a special case where updateTexImage is called 1172// in the middle of disconnect. This ordering is enforced 1173// by blocking in the disconnect callback. 1174TEST_F(SurfaceTextureGLTest, DisconnectStressTest) { 1175 1176 class ProducerThread : public Thread { 1177 public: 1178 ProducerThread(const sp<ANativeWindow>& anw): 1179 mANW(anw) { 1180 } 1181 1182 virtual ~ProducerThread() { 1183 } 1184 1185 virtual bool threadLoop() { 1186 ANativeWindowBuffer* anb; 1187 1188 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_EGL); 1189 1190 for (int numFrames =0 ; numFrames < 2; numFrames ++) { 1191 1192 if (native_window_dequeue_buffer_and_wait(mANW.get(), 1193 &anb) != NO_ERROR) { 1194 return false; 1195 } 1196 if (anb == NULL) { 1197 return false; 1198 } 1199 if (mANW->queueBuffer(mANW.get(), anb, -1) 1200 != NO_ERROR) { 1201 return false; 1202 } 1203 } 1204 1205 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_EGL); 1206 1207 return false; 1208 } 1209 1210 private: 1211 sp<ANativeWindow> mANW; 1212 }; 1213 1214 ASSERT_EQ(OK, mST->setSynchronousMode(true)); 1215 1216 sp<DisconnectWaiter> dw(new DisconnectWaiter()); 1217 mST->getBufferQueue()->consumerConnect(dw); 1218 1219 1220 sp<Thread> pt(new ProducerThread(mANW)); 1221 pt->run(); 1222 1223 // eat a frame so GLConsumer will own an at least one slot 1224 dw->waitForFrame(); 1225 EXPECT_EQ(OK,mST->updateTexImage()); 1226 1227 dw->waitForFrame(); 1228 // Could fail here as GLConsumer thinks it still owns the slot 1229 // but bufferQueue has released all slots 1230 EXPECT_EQ(OK,mST->updateTexImage()); 1231 1232 dw->finishDisconnect(); 1233} 1234 1235 1236// This test ensures that the GLConsumer clears the mCurrentTexture 1237// when it is disconnected and reconnected. Otherwise it will 1238// attempt to release a buffer that it does not owned 1239TEST_F(SurfaceTextureGLTest, DisconnectClearsCurrentTexture) { 1240 ASSERT_EQ(OK, mST->setSynchronousMode(true)); 1241 1242 ASSERT_EQ(OK, native_window_api_connect(mANW.get(), 1243 NATIVE_WINDOW_API_EGL)); 1244 1245 ANativeWindowBuffer *anb; 1246 1247 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 1248 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1)); 1249 1250 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 1251 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1)); 1252 1253 EXPECT_EQ(OK,mST->updateTexImage()); 1254 EXPECT_EQ(OK,mST->updateTexImage()); 1255 1256 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(), 1257 NATIVE_WINDOW_API_EGL)); 1258 ASSERT_EQ(OK, native_window_api_connect(mANW.get(), 1259 NATIVE_WINDOW_API_EGL)); 1260 1261 ASSERT_EQ(OK, mST->setSynchronousMode(true)); 1262 1263 EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 1264 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1)); 1265 1266 // Will fail here if mCurrentTexture is not cleared properly 1267 mFW->waitForFrame(); 1268 EXPECT_EQ(OK,mST->updateTexImage()); 1269 1270 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(), 1271 NATIVE_WINDOW_API_EGL)); 1272} 1273 1274TEST_F(SurfaceTextureGLTest, ScaleToWindowMode) { 1275 ASSERT_EQ(OK, mST->setSynchronousMode(true)); 1276 1277 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(), 1278 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW)); 1279 1280 // The producer image size 1281 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512)); 1282 1283 // The consumer image size (16 x 9) ratio 1284 mST->setDefaultBufferSize(1280, 720); 1285 1286 ASSERT_EQ(OK, native_window_api_connect(mANW.get(), 1287 NATIVE_WINDOW_API_CPU)); 1288 1289 ANativeWindowBuffer *anb; 1290 1291 android_native_rect_t odd = {23, 78, 123, 477}; 1292 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &odd)); 1293 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 1294 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1)); 1295 mFW->waitForFrame(); 1296 EXPECT_EQ(OK, mST->updateTexImage()); 1297 Rect r = mST->getCurrentCrop(); 1298 assertRectEq(Rect(23, 78, 123, 477), r); 1299 1300 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(), 1301 NATIVE_WINDOW_API_CPU)); 1302} 1303 1304// This test ensures the scaling mode does the right thing 1305// ie NATIVE_WINDOW_SCALING_MODE_CROP should crop 1306// the image such that it has the same aspect ratio as the 1307// default buffer size 1308TEST_F(SurfaceTextureGLTest, CroppedScalingMode) { 1309 ASSERT_EQ(OK, mST->setSynchronousMode(true)); 1310 1311 ASSERT_EQ(OK, native_window_set_scaling_mode(mANW.get(), 1312 NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)); 1313 1314 // The producer image size 1315 ASSERT_EQ(OK, native_window_set_buffers_dimensions(mANW.get(), 512, 512)); 1316 1317 // The consumer image size (16 x 9) ratio 1318 mST->setDefaultBufferSize(1280, 720); 1319 1320 native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU); 1321 1322 ANativeWindowBuffer *anb; 1323 1324 // The crop is in the shape of (320, 180) === 16 x 9 1325 android_native_rect_t standard = {10, 20, 330, 200}; 1326 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &standard)); 1327 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 1328 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1)); 1329 mFW->waitForFrame(); 1330 EXPECT_EQ(OK, mST->updateTexImage()); 1331 Rect r = mST->getCurrentCrop(); 1332 // crop should be the same as crop (same aspect ratio) 1333 assertRectEq(Rect(10, 20, 330, 200), r); 1334 1335 // make this wider then desired aspect 239 x 100 (2.39:1) 1336 android_native_rect_t wide = {20, 30, 259, 130}; 1337 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &wide)); 1338 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 1339 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1)); 1340 mFW->waitForFrame(); 1341 EXPECT_EQ(OK, mST->updateTexImage()); 1342 r = mST->getCurrentCrop(); 1343 // crop should be the same height, but have cropped left and right borders 1344 // offset is 30.6 px L+, R- 1345 assertRectEq(Rect(51, 30, 228, 130), r); 1346 1347 // This image is taller then desired aspect 400 x 300 (4:3) 1348 android_native_rect_t narrow = {0, 0, 400, 300}; 1349 ASSERT_EQ(OK, native_window_set_crop(mANW.get(), &narrow)); 1350 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 1351 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1)); 1352 mFW->waitForFrame(); 1353 EXPECT_EQ(OK, mST->updateTexImage()); 1354 r = mST->getCurrentCrop(); 1355 // crop should be the same width, but have cropped top and bottom borders 1356 // offset is 37.5 px 1357 assertRectEq(Rect(0, 37, 400, 262), r); 1358 1359 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU); 1360} 1361 1362TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) { 1363 class ProducerThread : public Thread { 1364 public: 1365 ProducerThread(const sp<ANativeWindow>& anw): 1366 mANW(anw), 1367 mDequeueError(NO_ERROR) { 1368 } 1369 1370 virtual ~ProducerThread() { 1371 } 1372 1373 virtual bool threadLoop() { 1374 Mutex::Autolock lock(mMutex); 1375 ANativeWindowBuffer* anb; 1376 1377 // Frame 1 1378 if (native_window_dequeue_buffer_and_wait(mANW.get(), 1379 &anb) != NO_ERROR) { 1380 return false; 1381 } 1382 if (anb == NULL) { 1383 return false; 1384 } 1385 if (mANW->queueBuffer(mANW.get(), anb, -1) 1386 != NO_ERROR) { 1387 return false; 1388 } 1389 1390 // Frame 2 1391 if (native_window_dequeue_buffer_and_wait(mANW.get(), 1392 &anb) != NO_ERROR) { 1393 return false; 1394 } 1395 if (anb == NULL) { 1396 return false; 1397 } 1398 if (mANW->queueBuffer(mANW.get(), anb, -1) 1399 != NO_ERROR) { 1400 return false; 1401 } 1402 1403 // Frame 3 - error expected 1404 mDequeueError = native_window_dequeue_buffer_and_wait(mANW.get(), 1405 &anb); 1406 return false; 1407 } 1408 1409 status_t getDequeueError() { 1410 Mutex::Autolock lock(mMutex); 1411 return mDequeueError; 1412 } 1413 1414 private: 1415 sp<ANativeWindow> mANW; 1416 status_t mDequeueError; 1417 Mutex mMutex; 1418 }; 1419 1420 ASSERT_EQ(OK, mST->setSynchronousMode(true)); 1421 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2)); 1422 1423 sp<Thread> pt(new ProducerThread(mANW)); 1424 pt->run(); 1425 1426 mFW->waitForFrame(); 1427 mFW->waitForFrame(); 1428 1429 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to 1430 // block waiting for a buffer to become available. 1431 usleep(100000); 1432 1433 mST->abandon(); 1434 1435 pt->requestExitAndWait(); 1436 ASSERT_EQ(NO_INIT, 1437 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError()); 1438} 1439 1440TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) { 1441 int texHeight = 16; 1442 ANativeWindowBuffer* anb; 1443 1444 GLint maxTextureSize; 1445 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize); 1446 1447 // make sure it works with small textures 1448 mST->setDefaultBufferSize(16, texHeight); 1449 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), 1450 &anb)); 1451 EXPECT_EQ(16, anb->width); 1452 EXPECT_EQ(texHeight, anb->height); 1453 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1)); 1454 EXPECT_EQ(NO_ERROR, mST->updateTexImage()); 1455 1456 // make sure it works with GL_MAX_TEXTURE_SIZE 1457 mST->setDefaultBufferSize(maxTextureSize, texHeight); 1458 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), 1459 &anb)); 1460 EXPECT_EQ(maxTextureSize, anb->width); 1461 EXPECT_EQ(texHeight, anb->height); 1462 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1)); 1463 EXPECT_EQ(NO_ERROR, mST->updateTexImage()); 1464 1465 // make sure it fails with GL_MAX_TEXTURE_SIZE+1 1466 mST->setDefaultBufferSize(maxTextureSize+1, texHeight); 1467 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), 1468 &anb)); 1469 EXPECT_EQ(maxTextureSize+1, anb->width); 1470 EXPECT_EQ(texHeight, anb->height); 1471 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1)); 1472 ASSERT_NE(NO_ERROR, mST->updateTexImage()); 1473} 1474 1475/* 1476 * This test fixture is for testing GL -> GL texture streaming. It creates an 1477 * EGLSurface and an EGLContext for the image producer to use. 1478 */ 1479class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest { 1480protected: 1481 SurfaceTextureGLToGLTest(): 1482 mProducerEglSurface(EGL_NO_SURFACE), 1483 mProducerEglContext(EGL_NO_CONTEXT) { 1484 } 1485 1486 virtual void SetUp() { 1487 SurfaceTextureGLTest::SetUp(); 1488 1489 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, 1490 mANW.get(), NULL); 1491 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1492 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface); 1493 1494 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig, 1495 EGL_NO_CONTEXT, getContextAttribs()); 1496 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1497 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext); 1498 } 1499 1500 virtual void TearDown() { 1501 if (mProducerEglContext != EGL_NO_CONTEXT) { 1502 eglDestroyContext(mEglDisplay, mProducerEglContext); 1503 } 1504 if (mProducerEglSurface != EGL_NO_SURFACE) { 1505 eglDestroySurface(mEglDisplay, mProducerEglSurface); 1506 } 1507 SurfaceTextureGLTest::TearDown(); 1508 } 1509 1510 EGLSurface mProducerEglSurface; 1511 EGLContext mProducerEglContext; 1512}; 1513 1514TEST_F(SurfaceTextureGLToGLTest, TransformHintGetsRespected) { 1515 const uint32_t texWidth = 32; 1516 const uint32_t texHeight = 64; 1517 1518 mST->setDefaultBufferSize(texWidth, texHeight); 1519 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90); 1520 1521 // This test requires 3 buffers to avoid deadlock because we're 1522 // both producer and consumer, and only using one thread. 1523 mST->setDefaultMaxBufferCount(3); 1524 1525 // Do the producer side of things 1526 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1527 mProducerEglSurface, mProducerEglContext)); 1528 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1529 1530 // Start a buffer with our chosen size and transform hint moving 1531 // through the system. 1532 glClear(GL_COLOR_BUFFER_BIT); // give the driver something to do 1533 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1534 mST->updateTexImage(); // consume it 1535 // Swap again. 1536 glClear(GL_COLOR_BUFFER_BIT); 1537 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1538 mST->updateTexImage(); 1539 1540 // The current buffer should either show the effects of the transform 1541 // hint (in the form of an inverse transform), or show that the 1542 // transform hint has been ignored. 1543 sp<GraphicBuffer> buf = mST->getCurrentBuffer(); 1544 if (mST->getCurrentTransform() == NATIVE_WINDOW_TRANSFORM_ROT_270) { 1545 ASSERT_EQ(texWidth, buf->getHeight()); 1546 ASSERT_EQ(texHeight, buf->getWidth()); 1547 } else { 1548 ASSERT_EQ(texWidth, buf->getWidth()); 1549 ASSERT_EQ(texHeight, buf->getHeight()); 1550 } 1551 1552 // Reset the transform hint and confirm that it takes. 1553 mST->setTransformHint(0); 1554 glClear(GL_COLOR_BUFFER_BIT); 1555 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1556 mST->updateTexImage(); 1557 glClear(GL_COLOR_BUFFER_BIT); 1558 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1559 mST->updateTexImage(); 1560 1561 buf = mST->getCurrentBuffer(); 1562 ASSERT_EQ((uint32_t) 0, mST->getCurrentTransform()); 1563 ASSERT_EQ(texWidth, buf->getWidth()); 1564 ASSERT_EQ(texHeight, buf->getHeight()); 1565} 1566 1567TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) { 1568 const int texWidth = 64; 1569 const int texHeight = 64; 1570 1571 mST->setDefaultBufferSize(texWidth, texHeight); 1572 1573 // This test requires 3 buffers to complete run on a single thread. 1574 mST->setDefaultMaxBufferCount(3); 1575 1576 // Do the producer side of things 1577 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1578 mProducerEglSurface, mProducerEglContext)); 1579 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1580 1581 // This is needed to ensure we pick up a buffer of the correct size. 1582 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1583 1584 glClearColor(0.6, 0.6, 0.6, 0.6); 1585 glClear(GL_COLOR_BUFFER_BIT); 1586 1587 glEnable(GL_SCISSOR_TEST); 1588 glScissor(4, 4, 4, 4); 1589 glClearColor(1.0, 0.0, 0.0, 1.0); 1590 glClear(GL_COLOR_BUFFER_BIT); 1591 1592 glScissor(24, 48, 4, 4); 1593 glClearColor(0.0, 1.0, 0.0, 1.0); 1594 glClear(GL_COLOR_BUFFER_BIT); 1595 1596 glScissor(37, 17, 4, 4); 1597 glClearColor(0.0, 0.0, 1.0, 1.0); 1598 glClear(GL_COLOR_BUFFER_BIT); 1599 1600 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1601 1602 // Do the consumer side of things 1603 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 1604 mEglContext)); 1605 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1606 1607 glDisable(GL_SCISSOR_TEST); 1608 1609 // Skip the first frame, which was empty 1610 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1611 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1612 1613 glClearColor(0.2, 0.2, 0.2, 0.2); 1614 glClear(GL_COLOR_BUFFER_BIT); 1615 1616 glViewport(0, 0, texWidth, texHeight); 1617 drawTexture(); 1618 1619 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153)); 1620 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153)); 1621 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153)); 1622 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153)); 1623 1624 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255)); 1625 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255)); 1626 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255)); 1627 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153)); 1628 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153)); 1629 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153)); 1630 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153)); 1631 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153)); 1632 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153)); 1633 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153)); 1634 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153)); 1635 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153)); 1636 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153)); 1637 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153)); 1638 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153)); 1639 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153)); 1640} 1641 1642TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) { 1643 sp<GraphicBuffer> buffers[2]; 1644 1645 // This test requires async mode to run on a single thread. 1646 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1647 mProducerEglSurface, mProducerEglContext)); 1648 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1649 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0)); 1650 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1651 1652 for (int i = 0; i < 2; i++) { 1653 // Produce a frame 1654 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1655 mProducerEglSurface, mProducerEglContext)); 1656 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1657 glClear(GL_COLOR_BUFFER_BIT); 1658 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1659 1660 // Consume a frame 1661 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 1662 mEglContext)); 1663 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1664 mFW->waitForFrame(); 1665 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1666 buffers[i] = mST->getCurrentBuffer(); 1667 } 1668 1669 // Destroy the GL texture object to release its ref on buffers[2]. 1670 GLuint texID = TEX_ID; 1671 glDeleteTextures(1, &texID); 1672 1673 // Destroy the EGLSurface 1674 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface)); 1675 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1676 mProducerEglSurface = EGL_NO_SURFACE; 1677 1678 // This test should have the only reference to buffer 0. 1679 EXPECT_EQ(1, buffers[0]->getStrongCount()); 1680 1681 // The GLConsumer should hold a single reference to buffer 1 in its 1682 // mCurrentBuffer member. All of the references in the slots should have 1683 // been released. 1684 EXPECT_EQ(2, buffers[1]->getStrongCount()); 1685} 1686 1687TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) { 1688 sp<GraphicBuffer> buffers[3]; 1689 1690 // This test requires async mode to run on a single thread. 1691 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1692 mProducerEglSurface, mProducerEglContext)); 1693 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1694 EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0)); 1695 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1696 1697 for (int i = 0; i < 3; i++) { 1698 // Produce a frame 1699 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1700 mProducerEglSurface, mProducerEglContext)); 1701 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1702 glClear(GL_COLOR_BUFFER_BIT); 1703 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface)); 1704 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1705 1706 // Consume a frame 1707 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 1708 mEglContext)); 1709 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1710 mFW->waitForFrame(); 1711 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1712 buffers[i] = mST->getCurrentBuffer(); 1713 } 1714 1715 // Abandon the GLConsumer, releasing the ref that the GLConsumer has 1716 // on buffers[2]. 1717 mST->abandon(); 1718 1719 // Destroy the GL texture object to release its ref on buffers[2]. 1720 GLuint texID = TEX_ID; 1721 glDeleteTextures(1, &texID); 1722 1723 // Destroy the EGLSurface. 1724 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface)); 1725 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1726 mProducerEglSurface = EGL_NO_SURFACE; 1727 1728 EXPECT_EQ(1, buffers[0]->getStrongCount()); 1729 EXPECT_EQ(1, buffers[1]->getStrongCount()); 1730 1731 // Depending on how lazily the GL driver dequeues buffers, we may end up 1732 // with either two or three total buffers. If there are three, make sure 1733 // the last one was properly down-ref'd. 1734 if (buffers[2] != buffers[0]) { 1735 EXPECT_EQ(1, buffers[2]->getStrongCount()); 1736 } 1737} 1738 1739TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentBeforeConsumerDeathUnrefsBuffers) { 1740 sp<GraphicBuffer> buffer; 1741 1742 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1743 mProducerEglSurface, mProducerEglContext)); 1744 1745 // Produce a frame 1746 glClear(GL_COLOR_BUFFER_BIT); 1747 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface)); 1748 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1749 1750 // Destroy the EGLSurface. 1751 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface)); 1752 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1753 mProducerEglSurface = EGL_NO_SURFACE; 1754 mSTC.clear(); 1755 mANW.clear(); 1756 mTextureRenderer.clear(); 1757 1758 // Consume a frame 1759 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1760 buffer = mST->getCurrentBuffer(); 1761 1762 // Destroy the GL texture object to release its ref 1763 GLuint texID = TEX_ID; 1764 glDeleteTextures(1, &texID); 1765 1766 // make un-current, all references to buffer should be gone 1767 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, 1768 EGL_NO_SURFACE, EGL_NO_CONTEXT)); 1769 1770 // Destroy consumer 1771 mST.clear(); 1772 1773 EXPECT_EQ(1, buffer->getStrongCount()); 1774} 1775 1776TEST_F(SurfaceTextureGLToGLTest, EglMakeCurrentAfterConsumerDeathUnrefsBuffers) { 1777 sp<GraphicBuffer> buffer; 1778 1779 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1780 mProducerEglSurface, mProducerEglContext)); 1781 1782 // Produce a frame 1783 glClear(GL_COLOR_BUFFER_BIT); 1784 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface)); 1785 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1786 1787 // Destroy the EGLSurface. 1788 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface)); 1789 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1790 mProducerEglSurface = EGL_NO_SURFACE; 1791 mSTC.clear(); 1792 mANW.clear(); 1793 mTextureRenderer.clear(); 1794 1795 // Consume a frame 1796 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1797 buffer = mST->getCurrentBuffer(); 1798 1799 // Destroy the GL texture object to release its ref 1800 GLuint texID = TEX_ID; 1801 glDeleteTextures(1, &texID); 1802 1803 // Destroy consumer 1804 mST.clear(); 1805 1806 // make un-current, all references to buffer should be gone 1807 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, 1808 EGL_NO_SURFACE, EGL_NO_CONTEXT)); 1809 1810 EXPECT_EQ(1, buffer->getStrongCount()); 1811} 1812 1813 1814TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) { 1815 // This test requires 3 buffers to run on a single thread. 1816 mST->setDefaultMaxBufferCount(3); 1817 1818 ASSERT_TRUE(mST->isSynchronousMode()); 1819 1820 for (int i = 0; i < 10; i++) { 1821 // Produce a frame 1822 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1823 mProducerEglSurface, mProducerEglContext)); 1824 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1825 glClear(GL_COLOR_BUFFER_BIT); 1826 EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface)); 1827 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1828 1829 // Consume a frame 1830 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 1831 mEglContext)); 1832 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1833 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1834 } 1835 1836 ASSERT_TRUE(mST->isSynchronousMode()); 1837} 1838 1839TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) { 1840 enum { texWidth = 64 }; 1841 enum { texHeight = 64 }; 1842 1843 // This test requires 3 buffers to complete run on a single thread. 1844 mST->setDefaultMaxBufferCount(3); 1845 1846 // Set the user buffer size. 1847 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight); 1848 1849 // Do the producer side of things 1850 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1851 mProducerEglSurface, mProducerEglContext)); 1852 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1853 1854 // This is needed to ensure we pick up a buffer of the correct size. 1855 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1856 1857 glClearColor(0.6, 0.6, 0.6, 0.6); 1858 glClear(GL_COLOR_BUFFER_BIT); 1859 1860 glEnable(GL_SCISSOR_TEST); 1861 glScissor(4, 4, 1, 1); 1862 glClearColor(1.0, 0.0, 0.0, 1.0); 1863 glClear(GL_COLOR_BUFFER_BIT); 1864 1865 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1866 1867 // Do the consumer side of things 1868 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 1869 mEglContext)); 1870 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1871 1872 glDisable(GL_SCISSOR_TEST); 1873 1874 // Skip the first frame, which was empty 1875 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1876 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1877 1878 glClearColor(0.2, 0.2, 0.2, 0.2); 1879 glClear(GL_COLOR_BUFFER_BIT); 1880 1881 glViewport(0, 0, texWidth, texHeight); 1882 drawTexture(); 1883 1884 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153)); 1885 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153)); 1886 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153)); 1887 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153)); 1888 1889 EXPECT_TRUE(checkPixel( 4, 4, 255, 0, 0, 255)); 1890 EXPECT_TRUE(checkPixel( 5, 5, 153, 153, 153, 153)); 1891 EXPECT_TRUE(checkPixel( 3, 3, 153, 153, 153, 153)); 1892 EXPECT_TRUE(checkPixel(45, 52, 153, 153, 153, 153)); 1893 EXPECT_TRUE(checkPixel(12, 36, 153, 153, 153, 153)); 1894} 1895 1896TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) { 1897 enum { texWidth = 64 }; 1898 enum { texHeight = 16 }; 1899 1900 // This test requires 3 buffers to complete run on a single thread. 1901 mST->setDefaultMaxBufferCount(3); 1902 1903 // Set the transform hint. 1904 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90); 1905 1906 // Set the user buffer size. 1907 native_window_set_buffers_user_dimensions(mANW.get(), texWidth, texHeight); 1908 1909 // Do the producer side of things 1910 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1911 mProducerEglSurface, mProducerEglContext)); 1912 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1913 1914 // This is needed to ensure we pick up a buffer of the correct size and the 1915 // new rotation hint. 1916 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1917 1918 glClearColor(0.6, 0.6, 0.6, 0.6); 1919 glClear(GL_COLOR_BUFFER_BIT); 1920 1921 glEnable(GL_SCISSOR_TEST); 1922 glScissor(24, 4, 1, 1); 1923 glClearColor(1.0, 0.0, 0.0, 1.0); 1924 glClear(GL_COLOR_BUFFER_BIT); 1925 1926 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1927 1928 // Do the consumer side of things 1929 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 1930 mEglContext)); 1931 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1932 1933 glDisable(GL_SCISSOR_TEST); 1934 1935 // Skip the first frame, which was empty 1936 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1937 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1938 1939 glClearColor(0.2, 0.2, 0.2, 0.2); 1940 glClear(GL_COLOR_BUFFER_BIT); 1941 1942 glViewport(0, 0, texWidth, texHeight); 1943 drawTexture(); 1944 1945 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153)); 1946 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153)); 1947 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153)); 1948 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153)); 1949 1950 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255)); 1951 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153)); 1952 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153)); 1953 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153)); 1954 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153)); 1955} 1956 1957TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) { 1958 enum { texWidth = 64 }; 1959 enum { texHeight = 16 }; 1960 1961 // This test requires 3 buffers to complete run on a single thread. 1962 mST->setDefaultMaxBufferCount(3); 1963 1964 // Set the transform hint. 1965 mST->setTransformHint(NATIVE_WINDOW_TRANSFORM_ROT_90); 1966 1967 // Set the default buffer size. 1968 mST->setDefaultBufferSize(texWidth, texHeight); 1969 1970 // Do the producer side of things 1971 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface, 1972 mProducerEglSurface, mProducerEglContext)); 1973 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1974 1975 // This is needed to ensure we pick up a buffer of the correct size and the 1976 // new rotation hint. 1977 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1978 1979 glClearColor(0.6, 0.6, 0.6, 0.6); 1980 glClear(GL_COLOR_BUFFER_BIT); 1981 1982 glEnable(GL_SCISSOR_TEST); 1983 glScissor(24, 4, 1, 1); 1984 glClearColor(1.0, 0.0, 0.0, 1.0); 1985 glClear(GL_COLOR_BUFFER_BIT); 1986 1987 eglSwapBuffers(mEglDisplay, mProducerEglSurface); 1988 1989 // Do the consumer side of things 1990 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 1991 mEglContext)); 1992 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1993 1994 glDisable(GL_SCISSOR_TEST); 1995 1996 // Skip the first frame, which was empty 1997 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1998 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 1999 2000 glClearColor(0.2, 0.2, 0.2, 0.2); 2001 glClear(GL_COLOR_BUFFER_BIT); 2002 2003 glViewport(0, 0, texWidth, texHeight); 2004 drawTexture(); 2005 2006 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153)); 2007 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153)); 2008 EXPECT_TRUE(checkPixel(63, 15, 153, 153, 153, 153)); 2009 EXPECT_TRUE(checkPixel( 0, 15, 153, 153, 153, 153)); 2010 2011 EXPECT_TRUE(checkPixel(24, 4, 255, 0, 0, 255)); 2012 EXPECT_TRUE(checkPixel(25, 5, 153, 153, 153, 153)); 2013 EXPECT_TRUE(checkPixel(23, 3, 153, 153, 153, 153)); 2014 EXPECT_TRUE(checkPixel(45, 13, 153, 153, 153, 153)); 2015 EXPECT_TRUE(checkPixel(12, 8, 153, 153, 153, 153)); 2016} 2017 2018/* 2019 * This test fixture is for testing GL -> GL texture streaming from one thread 2020 * to another. It contains functionality to create a producer thread that will 2021 * perform GL rendering to an ANativeWindow that feeds frames to a 2022 * GLConsumer. Additionally it supports interlocking the producer and 2023 * consumer threads so that a specific sequence of calls can be 2024 * deterministically created by the test. 2025 * 2026 * The intended usage is as follows: 2027 * 2028 * TEST_F(...) { 2029 * class PT : public ProducerThread { 2030 * virtual void render() { 2031 * ... 2032 * swapBuffers(); 2033 * } 2034 * }; 2035 * 2036 * runProducerThread(new PT()); 2037 * 2038 * // The order of these calls will vary from test to test and may include 2039 * // multiple frames and additional operations (e.g. GL rendering from the 2040 * // texture). 2041 * fc->waitForFrame(); 2042 * mST->updateTexImage(); 2043 * fc->finishFrame(); 2044 * } 2045 * 2046 */ 2047class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest { 2048protected: 2049 2050 // ProducerThread is an abstract base class to simplify the creation of 2051 // OpenGL ES frame producer threads. 2052 class ProducerThread : public Thread { 2053 public: 2054 virtual ~ProducerThread() { 2055 } 2056 2057 void setEglObjects(EGLDisplay producerEglDisplay, 2058 EGLSurface producerEglSurface, 2059 EGLContext producerEglContext) { 2060 mProducerEglDisplay = producerEglDisplay; 2061 mProducerEglSurface = producerEglSurface; 2062 mProducerEglContext = producerEglContext; 2063 } 2064 2065 virtual bool threadLoop() { 2066 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface, 2067 mProducerEglSurface, mProducerEglContext); 2068 render(); 2069 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 2070 EGL_NO_CONTEXT); 2071 return false; 2072 } 2073 2074 protected: 2075 virtual void render() = 0; 2076 2077 void swapBuffers() { 2078 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface); 2079 } 2080 2081 EGLDisplay mProducerEglDisplay; 2082 EGLSurface mProducerEglSurface; 2083 EGLContext mProducerEglContext; 2084 }; 2085 2086 // FrameCondition is a utility class for interlocking between the producer 2087 // and consumer threads. The FrameCondition object should be created and 2088 // destroyed in the consumer thread only. The consumer thread should set 2089 // the FrameCondition as the FrameAvailableListener of the GLConsumer, 2090 // and should call both waitForFrame and finishFrame once for each expected 2091 // frame. 2092 // 2093 // This interlocking relies on the fact that onFrameAvailable gets called 2094 // synchronously from GLConsumer::queueBuffer. 2095 class FrameCondition : public GLConsumer::FrameAvailableListener { 2096 public: 2097 FrameCondition(): 2098 mFrameAvailable(false), 2099 mFrameFinished(false) { 2100 } 2101 2102 // waitForFrame waits for the next frame to arrive. This should be 2103 // called from the consumer thread once for every frame expected by the 2104 // test. 2105 void waitForFrame() { 2106 Mutex::Autolock lock(mMutex); 2107 ALOGV("+waitForFrame"); 2108 while (!mFrameAvailable) { 2109 mFrameAvailableCondition.wait(mMutex); 2110 } 2111 mFrameAvailable = false; 2112 ALOGV("-waitForFrame"); 2113 } 2114 2115 // Allow the producer to return from its swapBuffers call and continue 2116 // on to produce the next frame. This should be called by the consumer 2117 // thread once for every frame expected by the test. 2118 void finishFrame() { 2119 Mutex::Autolock lock(mMutex); 2120 ALOGV("+finishFrame"); 2121 mFrameFinished = true; 2122 mFrameFinishCondition.signal(); 2123 ALOGV("-finishFrame"); 2124 } 2125 2126 // This should be called by GLConsumer on the producer thread. 2127 virtual void onFrameAvailable() { 2128 Mutex::Autolock lock(mMutex); 2129 ALOGV("+onFrameAvailable"); 2130 mFrameAvailable = true; 2131 mFrameAvailableCondition.signal(); 2132 while (!mFrameFinished) { 2133 mFrameFinishCondition.wait(mMutex); 2134 } 2135 mFrameFinished = false; 2136 ALOGV("-onFrameAvailable"); 2137 } 2138 2139 protected: 2140 bool mFrameAvailable; 2141 bool mFrameFinished; 2142 2143 Mutex mMutex; 2144 Condition mFrameAvailableCondition; 2145 Condition mFrameFinishCondition; 2146 }; 2147 2148 virtual void SetUp() { 2149 SurfaceTextureGLToGLTest::SetUp(); 2150 mFC = new FrameCondition(); 2151 mST->setFrameAvailableListener(mFC); 2152 } 2153 2154 virtual void TearDown() { 2155 if (mProducerThread != NULL) { 2156 mProducerThread->requestExitAndWait(); 2157 } 2158 mProducerThread.clear(); 2159 mFC.clear(); 2160 SurfaceTextureGLToGLTest::TearDown(); 2161 } 2162 2163 void runProducerThread(const sp<ProducerThread> producerThread) { 2164 ASSERT_TRUE(mProducerThread == NULL); 2165 mProducerThread = producerThread; 2166 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface, 2167 mProducerEglContext); 2168 producerThread->run(); 2169 } 2170 2171 sp<ProducerThread> mProducerThread; 2172 sp<FrameCondition> mFC; 2173}; 2174 2175TEST_F(SurfaceTextureGLThreadToGLTest, 2176 UpdateTexImageBeforeFrameFinishedCompletes) { 2177 class PT : public ProducerThread { 2178 virtual void render() { 2179 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 2180 glClear(GL_COLOR_BUFFER_BIT); 2181 swapBuffers(); 2182 } 2183 }; 2184 2185 runProducerThread(new PT()); 2186 2187 mFC->waitForFrame(); 2188 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2189 mFC->finishFrame(); 2190 2191 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! 2192} 2193 2194TEST_F(SurfaceTextureGLThreadToGLTest, 2195 UpdateTexImageAfterFrameFinishedCompletes) { 2196 class PT : public ProducerThread { 2197 virtual void render() { 2198 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 2199 glClear(GL_COLOR_BUFFER_BIT); 2200 swapBuffers(); 2201 } 2202 }; 2203 2204 runProducerThread(new PT()); 2205 2206 mFC->waitForFrame(); 2207 mFC->finishFrame(); 2208 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2209 2210 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! 2211} 2212 2213TEST_F(SurfaceTextureGLThreadToGLTest, 2214 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) { 2215 enum { NUM_ITERATIONS = 1024 }; 2216 2217 class PT : public ProducerThread { 2218 virtual void render() { 2219 for (int i = 0; i < NUM_ITERATIONS; i++) { 2220 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 2221 glClear(GL_COLOR_BUFFER_BIT); 2222 ALOGV("+swapBuffers"); 2223 swapBuffers(); 2224 ALOGV("-swapBuffers"); 2225 } 2226 } 2227 }; 2228 2229 runProducerThread(new PT()); 2230 2231 for (int i = 0; i < NUM_ITERATIONS; i++) { 2232 mFC->waitForFrame(); 2233 ALOGV("+updateTexImage"); 2234 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2235 ALOGV("-updateTexImage"); 2236 mFC->finishFrame(); 2237 2238 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! 2239 } 2240} 2241 2242TEST_F(SurfaceTextureGLThreadToGLTest, 2243 RepeatedUpdateTexImageAfterFrameFinishedCompletes) { 2244 enum { NUM_ITERATIONS = 1024 }; 2245 2246 class PT : public ProducerThread { 2247 virtual void render() { 2248 for (int i = 0; i < NUM_ITERATIONS; i++) { 2249 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 2250 glClear(GL_COLOR_BUFFER_BIT); 2251 ALOGV("+swapBuffers"); 2252 swapBuffers(); 2253 ALOGV("-swapBuffers"); 2254 } 2255 } 2256 }; 2257 2258 runProducerThread(new PT()); 2259 2260 for (int i = 0; i < NUM_ITERATIONS; i++) { 2261 mFC->waitForFrame(); 2262 mFC->finishFrame(); 2263 ALOGV("+updateTexImage"); 2264 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2265 ALOGV("-updateTexImage"); 2266 2267 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! 2268 } 2269} 2270 2271// XXX: This test is disabled because it is currently hanging on some devices. 2272TEST_F(SurfaceTextureGLThreadToGLTest, 2273 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) { 2274 enum { NUM_ITERATIONS = 64 }; 2275 2276 class PT : public ProducerThread { 2277 virtual void render() { 2278 for (int i = 0; i < NUM_ITERATIONS; i++) { 2279 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 2280 glClear(GL_COLOR_BUFFER_BIT); 2281 ALOGV("+swapBuffers"); 2282 swapBuffers(); 2283 ALOGV("-swapBuffers"); 2284 } 2285 } 2286 }; 2287 2288 ASSERT_EQ(OK, mST->setSynchronousMode(true)); 2289 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2)); 2290 2291 runProducerThread(new PT()); 2292 2293 // Allow three frames to be rendered and queued before starting the 2294 // rendering in this thread. For the latter two frames we don't call 2295 // updateTexImage so the next dequeue from the producer thread will block 2296 // waiting for a frame to become available. 2297 mFC->waitForFrame(); 2298 mFC->finishFrame(); 2299 2300 // We must call updateTexImage to consume the first frame so that the 2301 // SurfaceTexture is able to reduce the buffer count to 2. This is because 2302 // the GL driver may dequeue a buffer when the EGLSurface is created, and 2303 // that happens before we call setDefaultMaxBufferCount. It's possible that the 2304 // driver does not dequeue a buffer at EGLSurface creation time, so we 2305 // cannot rely on this to cause the second dequeueBuffer call to block. 2306 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2307 2308 mFC->waitForFrame(); 2309 mFC->finishFrame(); 2310 mFC->waitForFrame(); 2311 mFC->finishFrame(); 2312 2313 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to 2314 // block waiting for a buffer to become available. 2315 usleep(100000); 2316 2317 // Render and present a number of images. This thread should not be blocked 2318 // by the fact that the producer thread is blocking in dequeue. 2319 for (int i = 0; i < NUM_ITERATIONS; i++) { 2320 glClear(GL_COLOR_BUFFER_BIT); 2321 eglSwapBuffers(mEglDisplay, mEglSurface); 2322 } 2323 2324 // Consume the two pending buffers to unblock the producer thread. 2325 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2326 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2327 2328 // Consume the remaining buffers from the producer thread. 2329 for (int i = 0; i < NUM_ITERATIONS-3; i++) { 2330 mFC->waitForFrame(); 2331 mFC->finishFrame(); 2332 ALOGV("+updateTexImage"); 2333 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2334 ALOGV("-updateTexImage"); 2335 } 2336} 2337 2338class SurfaceTextureFBOTest : public SurfaceTextureGLTest { 2339protected: 2340 2341 virtual void SetUp() { 2342 SurfaceTextureGLTest::SetUp(); 2343 2344 glGenFramebuffers(1, &mFbo); 2345 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 2346 2347 glGenTextures(1, &mFboTex); 2348 glBindTexture(GL_TEXTURE_2D, mFboTex); 2349 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getSurfaceWidth(), 2350 getSurfaceHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 2351 glBindTexture(GL_TEXTURE_2D, 0); 2352 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 2353 2354 glBindFramebuffer(GL_FRAMEBUFFER, mFbo); 2355 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, 2356 GL_TEXTURE_2D, mFboTex, 0); 2357 glBindFramebuffer(GL_FRAMEBUFFER, 0); 2358 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 2359 } 2360 2361 virtual void TearDown() { 2362 SurfaceTextureGLTest::TearDown(); 2363 2364 glDeleteTextures(1, &mFboTex); 2365 glDeleteFramebuffers(1, &mFbo); 2366 } 2367 2368 GLuint mFbo; 2369 GLuint mFboTex; 2370}; 2371 2372// This test is intended to verify that proper synchronization is done when 2373// rendering into an FBO. 2374TEST_F(SurfaceTextureFBOTest, BlitFromCpuFilledBufferToFbo) { 2375 const int texWidth = 64; 2376 const int texHeight = 64; 2377 2378 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 2379 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888)); 2380 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 2381 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 2382 2383 android_native_buffer_t* anb; 2384 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), 2385 &anb)); 2386 ASSERT_TRUE(anb != NULL); 2387 2388 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 2389 2390 // Fill the buffer with green 2391 uint8_t* img = NULL; 2392 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 2393 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 0, 255, 2394 0, 255); 2395 buf->unlock(); 2396 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), 2397 -1)); 2398 2399 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2400 2401 glBindFramebuffer(GL_FRAMEBUFFER, mFbo); 2402 drawTexture(); 2403 glBindFramebuffer(GL_FRAMEBUFFER, 0); 2404 2405 for (int i = 0; i < 4; i++) { 2406 SCOPED_TRACE(String8::format("frame %d", i).string()); 2407 2408 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), 2409 &anb)); 2410 ASSERT_TRUE(anb != NULL); 2411 2412 buf = new GraphicBuffer(anb, false); 2413 2414 // Fill the buffer with red 2415 ASSERT_EQ(NO_ERROR, buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, 2416 (void**)(&img))); 2417 fillRGBA8BufferSolid(img, texWidth, texHeight, buf->getStride(), 255, 0, 2418 0, 255); 2419 ASSERT_EQ(NO_ERROR, buf->unlock()); 2420 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), 2421 buf->getNativeBuffer(), -1)); 2422 2423 ASSERT_EQ(NO_ERROR, mST->updateTexImage()); 2424 2425 drawTexture(); 2426 2427 EXPECT_TRUE(checkPixel( 24, 39, 255, 0, 0, 255)); 2428 } 2429 2430 glBindFramebuffer(GL_FRAMEBUFFER, mFbo); 2431 2432 EXPECT_TRUE(checkPixel( 24, 39, 0, 255, 0, 255)); 2433} 2434 2435class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest { 2436protected: 2437 enum { SECOND_TEX_ID = 123 }; 2438 enum { THIRD_TEX_ID = 456 }; 2439 2440 SurfaceTextureMultiContextGLTest(): 2441 mSecondEglContext(EGL_NO_CONTEXT) { 2442 } 2443 2444 virtual void SetUp() { 2445 SurfaceTextureGLTest::SetUp(); 2446 2447 // Set up the secondary context and texture renderer. 2448 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig, 2449 EGL_NO_CONTEXT, getContextAttribs()); 2450 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 2451 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext); 2452 2453 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2454 mSecondEglContext)); 2455 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 2456 mSecondTextureRenderer = new TextureRenderer(SECOND_TEX_ID, mST); 2457 ASSERT_NO_FATAL_FAILURE(mSecondTextureRenderer->SetUp()); 2458 2459 // Set up the tertiary context and texture renderer. 2460 mThirdEglContext = eglCreateContext(mEglDisplay, mGlConfig, 2461 EGL_NO_CONTEXT, getContextAttribs()); 2462 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 2463 ASSERT_NE(EGL_NO_CONTEXT, mThirdEglContext); 2464 2465 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2466 mThirdEglContext)); 2467 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 2468 mThirdTextureRenderer = new TextureRenderer(THIRD_TEX_ID, mST); 2469 ASSERT_NO_FATAL_FAILURE(mThirdTextureRenderer->SetUp()); 2470 2471 // Switch back to the primary context to start the tests. 2472 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2473 mEglContext)); 2474 } 2475 2476 virtual void TearDown() { 2477 if (mThirdEglContext != EGL_NO_CONTEXT) { 2478 eglDestroyContext(mEglDisplay, mThirdEglContext); 2479 } 2480 if (mSecondEglContext != EGL_NO_CONTEXT) { 2481 eglDestroyContext(mEglDisplay, mSecondEglContext); 2482 } 2483 SurfaceTextureGLTest::TearDown(); 2484 } 2485 2486 EGLContext mSecondEglContext; 2487 sp<TextureRenderer> mSecondTextureRenderer; 2488 2489 EGLContext mThirdEglContext; 2490 sp<TextureRenderer> mThirdTextureRenderer; 2491}; 2492 2493TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) { 2494 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2495 2496 // Latch the texture contents on the primary context. 2497 mFW->waitForFrame(); 2498 ASSERT_EQ(OK, mST->updateTexImage()); 2499 2500 // Attempt to latch the texture on the secondary context. 2501 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2502 mSecondEglContext)); 2503 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 2504 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage()); 2505} 2506 2507TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) { 2508 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2509 2510 // Latch the texture contents on the primary context. 2511 mFW->waitForFrame(); 2512 ASSERT_EQ(OK, mST->updateTexImage()); 2513 2514 // Detach from the primary context. 2515 ASSERT_EQ(OK, mST->detachFromContext()); 2516 2517 // Check that the GL texture was deleted. 2518 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID)); 2519} 2520 2521TEST_F(SurfaceTextureMultiContextGLTest, 2522 DetachFromContextSucceedsAfterProducerDisconnect) { 2523 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2524 2525 // Latch the texture contents on the primary context. 2526 mFW->waitForFrame(); 2527 ASSERT_EQ(OK, mST->updateTexImage()); 2528 2529 // Detach from the primary context. 2530 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU); 2531 ASSERT_EQ(OK, mST->detachFromContext()); 2532 2533 // Check that the GL texture was deleted. 2534 EXPECT_EQ(GL_FALSE, glIsTexture(TEX_ID)); 2535} 2536 2537TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) { 2538 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2539 2540 // Latch the texture contents on the primary context. 2541 mFW->waitForFrame(); 2542 ASSERT_EQ(OK, mST->updateTexImage()); 2543 2544 // Attempt to detach from the primary context. 2545 mST->abandon(); 2546 ASSERT_EQ(NO_INIT, mST->detachFromContext()); 2547} 2548 2549TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) { 2550 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2551 2552 // Latch the texture contents on the primary context. 2553 mFW->waitForFrame(); 2554 ASSERT_EQ(OK, mST->updateTexImage()); 2555 2556 // Detach from the primary context. 2557 ASSERT_EQ(OK, mST->detachFromContext()); 2558 2559 // Attempt to detach from the primary context again. 2560 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext()); 2561} 2562 2563TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) { 2564 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2565 2566 // Latch the texture contents on the primary context. 2567 mFW->waitForFrame(); 2568 ASSERT_EQ(OK, mST->updateTexImage()); 2569 2570 // Make there be no current display. 2571 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 2572 EGL_NO_CONTEXT)); 2573 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 2574 2575 // Attempt to detach from the primary context. 2576 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext()); 2577} 2578 2579TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) { 2580 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2581 2582 // Latch the texture contents on the primary context. 2583 mFW->waitForFrame(); 2584 ASSERT_EQ(OK, mST->updateTexImage()); 2585 2586 // Make current context be incorrect. 2587 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2588 mSecondEglContext)); 2589 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 2590 2591 // Attempt to detach from the primary context. 2592 ASSERT_EQ(INVALID_OPERATION, mST->detachFromContext()); 2593} 2594 2595TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) { 2596 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2597 2598 // Detach from the primary context. 2599 ASSERT_EQ(OK, mST->detachFromContext()); 2600 2601 // Attempt to latch the texture contents on the primary context. 2602 mFW->waitForFrame(); 2603 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage()); 2604} 2605 2606TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) { 2607 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2608 2609 // Latch the texture contents on the primary context. 2610 mFW->waitForFrame(); 2611 ASSERT_EQ(OK, mST->updateTexImage()); 2612 2613 // Detach from the primary context. 2614 ASSERT_EQ(OK, mST->detachFromContext()); 2615 2616 // Attach to the secondary context. 2617 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2618 mSecondEglContext)); 2619 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID)); 2620 2621 // Verify that the texture object was created and bound. 2622 GLint texBinding = -1; 2623 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding); 2624 EXPECT_EQ(SECOND_TEX_ID, texBinding); 2625 2626 // Try to use the texture from the secondary context. 2627 glClearColor(0.2, 0.2, 0.2, 0.2); 2628 glClear(GL_COLOR_BUFFER_BIT); 2629 glViewport(0, 0, 1, 1); 2630 mSecondTextureRenderer->drawTexture(); 2631 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35)); 2632 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 2633} 2634 2635TEST_F(SurfaceTextureMultiContextGLTest, 2636 AttachToContextSucceedsAfterProducerDisconnect) { 2637 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2638 2639 // Latch the texture contents on the primary context. 2640 mFW->waitForFrame(); 2641 ASSERT_EQ(OK, mST->updateTexImage()); 2642 2643 // Detach from the primary context. 2644 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU); 2645 ASSERT_EQ(OK, mST->detachFromContext()); 2646 2647 // Attach to the secondary context. 2648 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2649 mSecondEglContext)); 2650 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID)); 2651 2652 // Verify that the texture object was created and bound. 2653 GLint texBinding = -1; 2654 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding); 2655 EXPECT_EQ(SECOND_TEX_ID, texBinding); 2656 2657 // Try to use the texture from the secondary context. 2658 glClearColor(0.2, 0.2, 0.2, 0.2); 2659 glClear(GL_COLOR_BUFFER_BIT); 2660 glViewport(0, 0, 1, 1); 2661 mSecondTextureRenderer->drawTexture(); 2662 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 2663 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35)); 2664} 2665 2666TEST_F(SurfaceTextureMultiContextGLTest, 2667 AttachToContextSucceedsBeforeUpdateTexImage) { 2668 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2669 2670 // Detach from the primary context. 2671 native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU); 2672 ASSERT_EQ(OK, mST->detachFromContext()); 2673 2674 // Attach to the secondary context. 2675 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2676 mSecondEglContext)); 2677 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID)); 2678 2679 // Verify that the texture object was created and bound. 2680 GLint texBinding = -1; 2681 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding); 2682 EXPECT_EQ(SECOND_TEX_ID, texBinding); 2683 2684 // Latch the texture contents on the primary context. 2685 mFW->waitForFrame(); 2686 ASSERT_EQ(OK, mST->updateTexImage()); 2687 2688 // Try to use the texture from the secondary context. 2689 glClearColor(0.2, 0.2, 0.2, 0.2); 2690 glClear(GL_COLOR_BUFFER_BIT); 2691 glViewport(0, 0, 1, 1); 2692 mSecondTextureRenderer->drawTexture(); 2693 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 2694 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35)); 2695} 2696 2697TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) { 2698 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2699 2700 // Latch the texture contents on the primary context. 2701 mFW->waitForFrame(); 2702 ASSERT_EQ(OK, mST->updateTexImage()); 2703 2704 // Detach from the primary context. 2705 ASSERT_EQ(OK, mST->detachFromContext()); 2706 2707 // Attempt to attach to the secondary context. 2708 mST->abandon(); 2709 2710 // Attempt to attach to the primary context. 2711 ASSERT_EQ(NO_INIT, mST->attachToContext(SECOND_TEX_ID)); 2712} 2713 2714TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) { 2715 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2716 2717 // Latch the texture contents on the primary context. 2718 mFW->waitForFrame(); 2719 ASSERT_EQ(OK, mST->updateTexImage()); 2720 2721 // Attempt to attach to the primary context. 2722 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID)); 2723} 2724 2725TEST_F(SurfaceTextureMultiContextGLTest, 2726 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) { 2727 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2728 2729 // Attempt to attach to the primary context. 2730 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID)); 2731} 2732 2733TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) { 2734 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2735 2736 // Latch the texture contents on the primary context. 2737 mFW->waitForFrame(); 2738 ASSERT_EQ(OK, mST->updateTexImage()); 2739 2740 // Detach from the primary context. 2741 ASSERT_EQ(OK, mST->detachFromContext()); 2742 2743 // Make there be no current display. 2744 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 2745 EGL_NO_CONTEXT)); 2746 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 2747 2748 // Attempt to attach with no context current. 2749 ASSERT_EQ(INVALID_OPERATION, mST->attachToContext(SECOND_TEX_ID)); 2750} 2751 2752TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) { 2753 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2754 2755 // Latch the texture contents on the primary context. 2756 mFW->waitForFrame(); 2757 ASSERT_EQ(OK, mST->updateTexImage()); 2758 2759 // Detach from the primary context. 2760 ASSERT_EQ(OK, mST->detachFromContext()); 2761 2762 // Attach to the secondary context. 2763 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2764 mSecondEglContext)); 2765 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID)); 2766 2767 // Detach from the secondary context. 2768 ASSERT_EQ(OK, mST->detachFromContext()); 2769 2770 // Attach to the tertiary context. 2771 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2772 mThirdEglContext)); 2773 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID)); 2774 2775 // Verify that the texture object was created and bound. 2776 GLint texBinding = -1; 2777 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding); 2778 EXPECT_EQ(THIRD_TEX_ID, texBinding); 2779 2780 // Try to use the texture from the tertiary context. 2781 glClearColor(0.2, 0.2, 0.2, 0.2); 2782 glClear(GL_COLOR_BUFFER_BIT); 2783 glViewport(0, 0, 1, 1); 2784 mThirdTextureRenderer->drawTexture(); 2785 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 2786 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35)); 2787} 2788 2789TEST_F(SurfaceTextureMultiContextGLTest, 2790 AttachToContextSucceedsTwiceBeforeUpdateTexImage) { 2791 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2792 2793 // Detach from the primary context. 2794 ASSERT_EQ(OK, mST->detachFromContext()); 2795 2796 // Attach to the secondary context. 2797 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2798 mSecondEglContext)); 2799 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID)); 2800 2801 // Detach from the secondary context. 2802 ASSERT_EQ(OK, mST->detachFromContext()); 2803 2804 // Attach to the tertiary context. 2805 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2806 mThirdEglContext)); 2807 ASSERT_EQ(OK, mST->attachToContext(THIRD_TEX_ID)); 2808 2809 // Verify that the texture object was created and bound. 2810 GLint texBinding = -1; 2811 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, &texBinding); 2812 EXPECT_EQ(THIRD_TEX_ID, texBinding); 2813 2814 // Latch the texture contents on the tertiary context. 2815 mFW->waitForFrame(); 2816 ASSERT_EQ(OK, mST->updateTexImage()); 2817 2818 // Try to use the texture from the tertiary context. 2819 glClearColor(0.2, 0.2, 0.2, 0.2); 2820 glClear(GL_COLOR_BUFFER_BIT); 2821 glViewport(0, 0, 1, 1); 2822 mThirdTextureRenderer->drawTexture(); 2823 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 2824 ASSERT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35)); 2825} 2826 2827TEST_F(SurfaceTextureMultiContextGLTest, 2828 UpdateTexImageSucceedsForBufferConsumedBeforeDetach) { 2829 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true)); 2830 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2)); 2831 2832 // produce two frames and consume them both on the primary context 2833 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2834 mFW->waitForFrame(); 2835 ASSERT_EQ(OK, mST->updateTexImage()); 2836 2837 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2838 mFW->waitForFrame(); 2839 ASSERT_EQ(OK, mST->updateTexImage()); 2840 2841 // produce one more frame 2842 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW)); 2843 2844 // Detach from the primary context and attach to the secondary context 2845 ASSERT_EQ(OK, mST->detachFromContext()); 2846 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 2847 mSecondEglContext)); 2848 ASSERT_EQ(OK, mST->attachToContext(SECOND_TEX_ID)); 2849 2850 // Consume final frame on secondary context 2851 mFW->waitForFrame(); 2852 ASSERT_EQ(OK, mST->updateTexImage()); 2853} 2854 2855} // namespace android 2856