SurfaceTexture_test.cpp revision fa1e002cec5dc33ebe52685dc81d644f783811a8
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/SurfaceTexture.h> 22#include <gui/SurfaceTextureClient.h> 23#include <ui/GraphicBuffer.h> 24#include <utils/String8.h> 25#include <utils/threads.h> 26 27#include <surfaceflinger/ISurfaceComposer.h> 28#include <surfaceflinger/Surface.h> 29#include <surfaceflinger/SurfaceComposerClient.h> 30 31#include <EGL/egl.h> 32#include <EGL/eglext.h> 33#include <GLES2/gl2.h> 34#include <GLES2/gl2ext.h> 35 36#include <ui/FramebufferNativeWindow.h> 37 38namespace android { 39 40class GLTest : public ::testing::Test { 41protected: 42 43 GLTest(): 44 mEglDisplay(EGL_NO_DISPLAY), 45 mEglSurface(EGL_NO_SURFACE), 46 mEglContext(EGL_NO_CONTEXT) { 47 } 48 49 virtual void SetUp() { 50 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); 51 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 52 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay); 53 54 EGLint majorVersion; 55 EGLint minorVersion; 56 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion)); 57 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 58 RecordProperty("EglVersionMajor", majorVersion); 59 RecordProperty("EglVersionMajor", minorVersion); 60 61 EGLint numConfigs = 0; 62 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig, 63 1, &numConfigs)); 64 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 65 66 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS"); 67 if (displaySecsEnv != NULL) { 68 mDisplaySecs = atoi(displaySecsEnv); 69 if (mDisplaySecs < 0) { 70 mDisplaySecs = 0; 71 } 72 } else { 73 mDisplaySecs = 0; 74 } 75 76 if (mDisplaySecs > 0) { 77 mComposerClient = new SurfaceComposerClient; 78 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); 79 80 mSurfaceControl = mComposerClient->createSurface( 81 String8("Test Surface"), 0, 82 getSurfaceWidth(), getSurfaceHeight(), 83 PIXEL_FORMAT_RGB_888, 0); 84 85 ASSERT_TRUE(mSurfaceControl != NULL); 86 ASSERT_TRUE(mSurfaceControl->isValid()); 87 88 SurfaceComposerClient::openGlobalTransaction(); 89 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF)); 90 ASSERT_EQ(NO_ERROR, mSurfaceControl->show()); 91 SurfaceComposerClient::closeGlobalTransaction(); 92 93 sp<ANativeWindow> window = mSurfaceControl->getSurface(); 94 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, 95 window.get(), NULL); 96 } else { 97 EGLint pbufferAttribs[] = { 98 EGL_WIDTH, getSurfaceWidth(), 99 EGL_HEIGHT, getSurfaceHeight(), 100 EGL_NONE }; 101 102 mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig, 103 pbufferAttribs); 104 } 105 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 106 ASSERT_NE(EGL_NO_SURFACE, mEglSurface); 107 108 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT, 109 getContextAttribs()); 110 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 111 ASSERT_NE(EGL_NO_CONTEXT, mEglContext); 112 113 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 114 mEglContext)); 115 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 116 117 EGLint w, h; 118 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w)); 119 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 120 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h)); 121 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 122 RecordProperty("EglSurfaceWidth", w); 123 RecordProperty("EglSurfaceHeight", h); 124 125 glViewport(0, 0, w, h); 126 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 127 } 128 129 virtual void TearDown() { 130 // Display the result 131 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) { 132 eglSwapBuffers(mEglDisplay, mEglSurface); 133 sleep(mDisplaySecs); 134 } 135 136 if (mComposerClient != NULL) { 137 mComposerClient->dispose(); 138 } 139 if (mEglContext != EGL_NO_CONTEXT) { 140 eglDestroyContext(mEglDisplay, mEglContext); 141 } 142 if (mEglSurface != EGL_NO_SURFACE) { 143 eglDestroySurface(mEglDisplay, mEglSurface); 144 } 145 if (mEglDisplay != EGL_NO_DISPLAY) { 146 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 147 EGL_NO_CONTEXT); 148 eglTerminate(mEglDisplay); 149 } 150 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 151 } 152 153 virtual EGLint const* getConfigAttribs() { 154 static EGLint sDefaultConfigAttribs[] = { 155 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, 156 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 157 EGL_RED_SIZE, 8, 158 EGL_GREEN_SIZE, 8, 159 EGL_BLUE_SIZE, 8, 160 EGL_ALPHA_SIZE, 8, 161 EGL_DEPTH_SIZE, 16, 162 EGL_STENCIL_SIZE, 8, 163 EGL_NONE }; 164 165 return sDefaultConfigAttribs; 166 } 167 168 virtual EGLint const* getContextAttribs() { 169 static EGLint sDefaultContextAttribs[] = { 170 EGL_CONTEXT_CLIENT_VERSION, 2, 171 EGL_NONE }; 172 173 return sDefaultContextAttribs; 174 } 175 176 virtual EGLint getSurfaceWidth() { 177 return 512; 178 } 179 180 virtual EGLint getSurfaceHeight() { 181 return 512; 182 } 183 184 void loadShader(GLenum shaderType, const char* pSource, GLuint* outShader) { 185 GLuint shader = glCreateShader(shaderType); 186 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 187 if (shader) { 188 glShaderSource(shader, 1, &pSource, NULL); 189 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 190 glCompileShader(shader); 191 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 192 GLint compiled = 0; 193 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); 194 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 195 if (!compiled) { 196 GLint infoLen = 0; 197 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); 198 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 199 if (infoLen) { 200 char* buf = (char*) malloc(infoLen); 201 if (buf) { 202 glGetShaderInfoLog(shader, infoLen, NULL, buf); 203 printf("Shader compile log:\n%s\n", buf); 204 free(buf); 205 FAIL(); 206 } 207 } else { 208 char* buf = (char*) malloc(0x1000); 209 if (buf) { 210 glGetShaderInfoLog(shader, 0x1000, NULL, buf); 211 printf("Shader compile log:\n%s\n", buf); 212 free(buf); 213 FAIL(); 214 } 215 } 216 glDeleteShader(shader); 217 shader = 0; 218 } 219 } 220 ASSERT_TRUE(shader != 0); 221 *outShader = shader; 222 } 223 224 void createProgram(const char* pVertexSource, const char* pFragmentSource, 225 GLuint* outPgm) { 226 GLuint vertexShader, fragmentShader; 227 { 228 SCOPED_TRACE("compiling vertex shader"); 229 loadShader(GL_VERTEX_SHADER, pVertexSource, &vertexShader); 230 if (HasFatalFailure()) { 231 return; 232 } 233 } 234 { 235 SCOPED_TRACE("compiling fragment shader"); 236 loadShader(GL_FRAGMENT_SHADER, pFragmentSource, &fragmentShader); 237 if (HasFatalFailure()) { 238 return; 239 } 240 } 241 242 GLuint program = glCreateProgram(); 243 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 244 if (program) { 245 glAttachShader(program, vertexShader); 246 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 247 glAttachShader(program, fragmentShader); 248 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 249 glLinkProgram(program); 250 GLint linkStatus = GL_FALSE; 251 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); 252 if (linkStatus != GL_TRUE) { 253 GLint bufLength = 0; 254 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); 255 if (bufLength) { 256 char* buf = (char*) malloc(bufLength); 257 if (buf) { 258 glGetProgramInfoLog(program, bufLength, NULL, buf); 259 printf("Program link log:\n%s\n", buf); 260 free(buf); 261 FAIL(); 262 } 263 } 264 glDeleteProgram(program); 265 program = 0; 266 } 267 } 268 glDeleteShader(vertexShader); 269 glDeleteShader(fragmentShader); 270 ASSERT_TRUE(program != 0); 271 *outPgm = program; 272 } 273 274 static int abs(int value) { 275 return value > 0 ? value : -value; 276 } 277 278 ::testing::AssertionResult checkPixel(int x, int y, int r, 279 int g, int b, int a, int tolerance=2) { 280 GLubyte pixel[4]; 281 String8 msg; 282 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel); 283 GLenum err = glGetError(); 284 if (err != GL_NO_ERROR) { 285 msg += String8::format("error reading pixel: %#x", err); 286 while ((err = glGetError()) != GL_NO_ERROR) { 287 msg += String8::format(", %#x", err); 288 } 289 fprintf(stderr, "pixel check failure: %s\n", msg.string()); 290 return ::testing::AssertionFailure( 291 ::testing::Message(msg.string())); 292 } 293 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) { 294 msg += String8::format("r(%d isn't %d)", pixel[0], r); 295 } 296 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) { 297 if (!msg.isEmpty()) { 298 msg += " "; 299 } 300 msg += String8::format("g(%d isn't %d)", pixel[1], g); 301 } 302 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) { 303 if (!msg.isEmpty()) { 304 msg += " "; 305 } 306 msg += String8::format("b(%d isn't %d)", pixel[2], b); 307 } 308 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) { 309 if (!msg.isEmpty()) { 310 msg += " "; 311 } 312 msg += String8::format("a(%d isn't %d)", pixel[3], a); 313 } 314 if (!msg.isEmpty()) { 315 fprintf(stderr, "pixel check failure: %s\n", msg.string()); 316 return ::testing::AssertionFailure( 317 ::testing::Message(msg.string())); 318 } else { 319 return ::testing::AssertionSuccess(); 320 } 321 } 322 323 int mDisplaySecs; 324 sp<SurfaceComposerClient> mComposerClient; 325 sp<SurfaceControl> mSurfaceControl; 326 327 EGLDisplay mEglDisplay; 328 EGLSurface mEglSurface; 329 EGLContext mEglContext; 330 EGLConfig mGlConfig; 331}; 332 333// XXX: Code above this point should live elsewhere 334 335class SurfaceTextureGLTest : public GLTest { 336protected: 337 static const GLint TEX_ID = 123; 338 339 virtual void SetUp() { 340 GLTest::SetUp(); 341 mST = new SurfaceTexture(TEX_ID); 342 mSTC = new SurfaceTextureClient(mST); 343 mANW = mSTC; 344 345 const char vsrc[] = 346 "attribute vec4 vPosition;\n" 347 "varying vec2 texCoords;\n" 348 "uniform mat4 texMatrix;\n" 349 "void main() {\n" 350 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n" 351 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n" 352 " gl_Position = vPosition;\n" 353 "}\n"; 354 355 const char fsrc[] = 356 "#extension GL_OES_EGL_image_external : require\n" 357 "precision mediump float;\n" 358 "uniform samplerExternalOES texSampler;\n" 359 "varying vec2 texCoords;\n" 360 "void main() {\n" 361 " gl_FragColor = texture2D(texSampler, texCoords);\n" 362 "}\n"; 363 364 { 365 SCOPED_TRACE("creating shader program"); 366 createProgram(vsrc, fsrc, &mPgm); 367 if (HasFatalFailure()) { 368 return; 369 } 370 } 371 372 mPositionHandle = glGetAttribLocation(mPgm, "vPosition"); 373 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 374 ASSERT_NE(-1, mPositionHandle); 375 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler"); 376 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 377 ASSERT_NE(-1, mTexSamplerHandle); 378 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix"); 379 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 380 ASSERT_NE(-1, mTexMatrixHandle); 381 } 382 383 virtual void TearDown() { 384 mANW.clear(); 385 mSTC.clear(); 386 mST.clear(); 387 GLTest::TearDown(); 388 } 389 390 // drawTexture draws the SurfaceTexture over the entire GL viewport. 391 void drawTexture() { 392 const GLfloat triangleVertices[] = { 393 -1.0f, 1.0f, 394 -1.0f, -1.0f, 395 1.0f, -1.0f, 396 1.0f, 1.0f, 397 }; 398 399 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, triangleVertices); 400 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 401 glEnableVertexAttribArray(mPositionHandle); 402 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 403 404 glUseProgram(mPgm); 405 glUniform1i(mTexSamplerHandle, 0); 406 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 407 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID); 408 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 409 410 // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as 411 // they're setting the defautls for that target, but when hacking things 412 // to use GL_TEXTURE_2D they are needed to achieve the same behavior. 413 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 414 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 415 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 416 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 417 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 418 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 419 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 420 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 421 422 GLfloat texMatrix[16]; 423 mST->getTransformMatrix(texMatrix); 424 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix); 425 426 glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 427 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 428 } 429 430 class FrameWaiter : public SurfaceTexture::FrameAvailableListener { 431 public: 432 FrameWaiter(): 433 mPendingFrames(0) { 434 } 435 436 void waitForFrame() { 437 Mutex::Autolock lock(mMutex); 438 while (mPendingFrames == 0) { 439 mCondition.wait(mMutex); 440 } 441 mPendingFrames--; 442 } 443 444 virtual void onFrameAvailable() { 445 Mutex::Autolock lock(mMutex); 446 mPendingFrames++; 447 mCondition.signal(); 448 } 449 450 int mPendingFrames; 451 Mutex mMutex; 452 Condition mCondition; 453 }; 454 455 sp<SurfaceTexture> mST; 456 sp<SurfaceTextureClient> mSTC; 457 sp<ANativeWindow> mANW; 458 459 GLuint mPgm; 460 GLint mPositionHandle; 461 GLint mTexSamplerHandle; 462 GLint mTexMatrixHandle; 463}; 464 465// Fill a YV12 buffer with a multi-colored checkerboard pattern 466void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) { 467 const int blockWidth = w > 16 ? w / 16 : 1; 468 const int blockHeight = h > 16 ? h / 16 : 1; 469 const int yuvTexOffsetY = 0; 470 int yuvTexStrideY = stride; 471 int yuvTexOffsetV = yuvTexStrideY * h; 472 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 473 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2; 474 int yuvTexStrideU = yuvTexStrideV; 475 for (int x = 0; x < w; x++) { 476 for (int y = 0; y < h; y++) { 477 int parityX = (x / blockWidth) & 1; 478 int parityY = (y / blockHeight) & 1; 479 unsigned char intensity = (parityX ^ parityY) ? 63 : 191; 480 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity; 481 if (x < w / 2 && y < h / 2) { 482 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity; 483 if (x * 2 < w / 2 && y * 2 < h / 2) { 484 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] = 485 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] = 486 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] = 487 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] = 488 intensity; 489 } 490 } 491 } 492 } 493} 494 495// Fill a YV12 buffer with red outside a given rectangle and green inside it. 496void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride, 497 const android_native_rect_t& rect) { 498 const int yuvTexOffsetY = 0; 499 int yuvTexStrideY = stride; 500 int yuvTexOffsetV = yuvTexStrideY * h; 501 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 502 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2; 503 int yuvTexStrideU = yuvTexStrideV; 504 for (int x = 0; x < w; x++) { 505 for (int y = 0; y < h; y++) { 506 bool inside = rect.left <= x && x < rect.right && 507 rect.top <= y && y < rect.bottom; 508 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64; 509 if (x < w / 2 && y < h / 2) { 510 bool inside = rect.left <= 2*x && 2*x < rect.right && 511 rect.top <= 2*y && 2*y < rect.bottom; 512 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16; 513 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] = 514 inside ? 16 : 255; 515 } 516 } 517 } 518} 519 520void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) { 521 const size_t PIXEL_SIZE = 4; 522 for (int x = 0; x < w; x++) { 523 for (int y = 0; y < h; y++) { 524 off_t offset = (y * stride + x) * PIXEL_SIZE; 525 for (int c = 0; c < 4; c++) { 526 int parityX = (x / (1 << (c+2))) & 1; 527 int parityY = (y / (1 << (c+2))) & 1; 528 buf[offset + c] = (parityX ^ parityY) ? 231 : 35; 529 } 530 } 531 } 532} 533 534TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) { 535 const int texWidth = 64; 536 const int texHeight = 66; 537 538 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 539 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); 540 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 541 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 542 543 ANativeWindowBuffer* anb; 544 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb)); 545 ASSERT_TRUE(anb != NULL); 546 547 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 548 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())); 549 550 // Fill the buffer with the a checkerboard pattern 551 uint8_t* img = NULL; 552 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 553 fillYV12Buffer(img, texWidth, texHeight, buf->getStride()); 554 buf->unlock(); 555 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())); 556 557 mST->updateTexImage(); 558 559 glClearColor(0.2, 0.2, 0.2, 0.2); 560 glClear(GL_COLOR_BUFFER_BIT); 561 562 glViewport(0, 0, texWidth, texHeight); 563 drawTexture(); 564 565 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255)); 566 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255)); 567 EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255)); 568 EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255)); 569 570 EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255)); 571 EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255)); 572 EXPECT_TRUE(checkPixel(52, 51, 98, 255, 73, 255)); 573 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255)); 574 EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255)); 575 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255)); 576 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255)); 577} 578 579TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) { 580 const int texWidth = 64; 581 const int texHeight = 64; 582 583 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 584 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); 585 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 586 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 587 588 ANativeWindowBuffer* anb; 589 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb)); 590 ASSERT_TRUE(anb != NULL); 591 592 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 593 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())); 594 595 // Fill the buffer with the a checkerboard pattern 596 uint8_t* img = NULL; 597 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 598 fillYV12Buffer(img, texWidth, texHeight, buf->getStride()); 599 buf->unlock(); 600 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())); 601 602 mST->updateTexImage(); 603 604 glClearColor(0.2, 0.2, 0.2, 0.2); 605 glClear(GL_COLOR_BUFFER_BIT); 606 607 glViewport(0, 0, texWidth, texHeight); 608 drawTexture(); 609 610 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255)); 611 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255)); 612 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255)); 613 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255)); 614 615 EXPECT_TRUE(checkPixel(22, 19, 100, 255, 74, 255)); 616 EXPECT_TRUE(checkPixel(45, 11, 100, 255, 74, 255)); 617 EXPECT_TRUE(checkPixel(52, 12, 155, 0, 181, 255)); 618 EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255)); 619 EXPECT_TRUE(checkPixel(31, 54, 0, 71, 117, 255)); 620 EXPECT_TRUE(checkPixel(29, 28, 0, 133, 0, 255)); 621 EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255)); 622} 623 624TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) { 625 const int texWidth = 64; 626 const int texHeight = 66; 627 628 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 629 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); 630 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 631 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 632 633 android_native_rect_t crops[] = { 634 {4, 6, 22, 36}, 635 {0, 6, 22, 36}, 636 {4, 0, 22, 36}, 637 {4, 6, texWidth, 36}, 638 {4, 6, 22, texHeight}, 639 }; 640 641 for (int i = 0; i < 5; i++) { 642 const android_native_rect_t& crop(crops[i]); 643 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }", crop.left, 644 crop.top, crop.right, crop.bottom).string()); 645 646 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop)); 647 648 ANativeWindowBuffer* anb; 649 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb)); 650 ASSERT_TRUE(anb != NULL); 651 652 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 653 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())); 654 655 uint8_t* img = NULL; 656 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 657 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop); 658 buf->unlock(); 659 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())); 660 661 mST->updateTexImage(); 662 663 glClearColor(0.2, 0.2, 0.2, 0.2); 664 glClear(GL_COLOR_BUFFER_BIT); 665 666 glViewport(0, 0, 64, 64); 667 drawTexture(); 668 669 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255)); 670 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255)); 671 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255)); 672 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255)); 673 674 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255)); 675 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255)); 676 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255)); 677 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255)); 678 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255)); 679 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255)); 680 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255)); 681 } 682} 683 684// This test is intended to catch synchronization bugs between the CPU-written 685// and GPU-read buffers. 686TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) { 687 enum { texWidth = 16 }; 688 enum { texHeight = 16 }; 689 enum { numFrames = 1024 }; 690 691 ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true)); 692 ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2)); 693 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 694 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); 695 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 696 GRALLOC_USAGE_SW_WRITE_OFTEN)); 697 698 struct TestPixel { 699 int x; 700 int y; 701 }; 702 const TestPixel testPixels[] = { 703 { 4, 11 }, 704 { 12, 14 }, 705 { 7, 2 }, 706 }; 707 enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])}; 708 709 class ProducerThread : public Thread { 710 public: 711 ProducerThread(const sp<ANativeWindow>& anw, const TestPixel* testPixels): 712 mANW(anw), 713 mTestPixels(testPixels) { 714 } 715 716 virtual ~ProducerThread() { 717 } 718 719 virtual bool threadLoop() { 720 for (int i = 0; i < numFrames; i++) { 721 ANativeWindowBuffer* anb; 722 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) { 723 return false; 724 } 725 if (anb == NULL) { 726 return false; 727 } 728 729 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 730 if (mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()) 731 != NO_ERROR) { 732 return false; 733 } 734 735 const int yuvTexOffsetY = 0; 736 int stride = buf->getStride(); 737 int yuvTexStrideY = stride; 738 int yuvTexOffsetV = yuvTexStrideY * texHeight; 739 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 740 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2; 741 int yuvTexStrideU = yuvTexStrideV; 742 743 uint8_t* img = NULL; 744 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 745 746 // Gray out all the test pixels first, so we're more likely to 747 // see a failure if GL is still texturing from the buffer we 748 // just dequeued. 749 for (int j = 0; j < numTestPixels; j++) { 750 int x = mTestPixels[j].x; 751 int y = mTestPixels[j].y; 752 uint8_t value = 128; 753 img[y*stride + x] = value; 754 } 755 756 // Fill the buffer with gray. 757 for (int y = 0; y < texHeight; y++) { 758 for (int x = 0; x < texWidth; x++) { 759 img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128; 760 img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128; 761 img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128; 762 } 763 } 764 765 // Set the test pixels to either white or black. 766 for (int j = 0; j < numTestPixels; j++) { 767 int x = mTestPixels[j].x; 768 int y = mTestPixels[j].y; 769 uint8_t value = 0; 770 if (j == (i % numTestPixels)) { 771 value = 255; 772 } 773 img[y*stride + x] = value; 774 } 775 776 buf->unlock(); 777 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()) 778 != NO_ERROR) { 779 return false; 780 } 781 } 782 return false; 783 } 784 785 sp<ANativeWindow> mANW; 786 const TestPixel* mTestPixels; 787 }; 788 789 sp<FrameWaiter> fw(new FrameWaiter); 790 mST->setFrameAvailableListener(fw); 791 792 sp<Thread> pt(new ProducerThread(mANW, testPixels)); 793 pt->run(); 794 795 glViewport(0, 0, texWidth, texHeight); 796 797 glClearColor(0.2, 0.2, 0.2, 0.2); 798 glClear(GL_COLOR_BUFFER_BIT); 799 800 // We wait for the first two frames up front so that the producer will be 801 // likely to dequeue the buffer that's currently being textured from. 802 fw->waitForFrame(); 803 fw->waitForFrame(); 804 805 for (int i = 0; i < numFrames; i++) { 806 SCOPED_TRACE(String8::format("frame %d", i).string()); 807 808 // We must wait for each frame to come in because if we ever do an 809 // updateTexImage call that doesn't consume a newly available buffer 810 // then the producer and consumer will get out of sync, which will cause 811 // a deadlock. 812 if (i > 1) { 813 fw->waitForFrame(); 814 } 815 mST->updateTexImage(); 816 drawTexture(); 817 818 for (int j = 0; j < numTestPixels; j++) { 819 int x = testPixels[j].x; 820 int y = testPixels[j].y; 821 uint8_t value = 0; 822 if (j == (i % numTestPixels)) { 823 // We must y-invert the texture coords 824 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255)); 825 } else { 826 // We must y-invert the texture coords 827 EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255)); 828 } 829 } 830 } 831 832 pt->requestExitAndWait(); 833} 834 835TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) { 836 const int texWidth = 64; 837 const int texHeight = 66; 838 839 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 840 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888)); 841 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 842 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 843 844 android_native_buffer_t* anb; 845 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb)); 846 ASSERT_TRUE(anb != NULL); 847 848 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 849 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())); 850 851 // Fill the buffer with the a checkerboard pattern 852 uint8_t* img = NULL; 853 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 854 fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride()); 855 buf->unlock(); 856 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())); 857 858 mST->updateTexImage(); 859 860 glClearColor(0.2, 0.2, 0.2, 0.2); 861 glClear(GL_COLOR_BUFFER_BIT); 862 863 glViewport(0, 0, texWidth, texHeight); 864 drawTexture(); 865 866 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35)); 867 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231)); 868 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231)); 869 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35)); 870 871 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231)); 872 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35)); 873 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35)); 874 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35)); 875 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231)); 876 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231)); 877 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231)); 878 EXPECT_TRUE(checkPixel(37, 47, 231, 35, 231, 231)); 879 EXPECT_TRUE(checkPixel(25, 38, 35, 35, 35, 35)); 880 EXPECT_TRUE(checkPixel(49, 6, 35, 231, 35, 35)); 881 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231)); 882 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231)); 883 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231)); 884 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231)); 885 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35)); 886 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231)); 887} 888 889TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) { 890 const int texWidth = 64; 891 const int texHeight = 64; 892 893 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), 894 texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888)); 895 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), 896 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); 897 898 android_native_buffer_t* anb; 899 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb)); 900 ASSERT_TRUE(anb != NULL); 901 902 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 903 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())); 904 905 // Fill the buffer with the a checkerboard pattern 906 uint8_t* img = NULL; 907 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 908 fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride()); 909 buf->unlock(); 910 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())); 911 912 mST->updateTexImage(); 913 914 glClearColor(0.2, 0.2, 0.2, 0.2); 915 glClear(GL_COLOR_BUFFER_BIT); 916 917 glViewport(0, 0, texWidth, texHeight); 918 drawTexture(); 919 920 EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231)); 921 EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35)); 922 EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231)); 923 EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35)); 924 925 EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35)); 926 EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231)); 927 EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231)); 928 EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35)); 929 EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35)); 930 EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231)); 931 EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35)); 932 EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35)); 933 EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35)); 934 EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231)); 935 EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231)); 936 EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231)); 937 EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231)); 938 EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35)); 939 EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231)); 940 EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35)); 941} 942 943TEST_F(SurfaceTextureGLTest, TexturingFromGLFilledRGBABufferPow2) { 944 const int texWidth = 64; 945 const int texHeight = 64; 946 947 mST->setDefaultBufferSize(texWidth, texHeight); 948 949 // Do the producer side of things 950 EGLSurface stcEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, 951 mANW.get(), NULL); 952 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 953 ASSERT_NE(EGL_NO_SURFACE, stcEglSurface); 954 955 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, stcEglSurface, stcEglSurface, 956 mEglContext)); 957 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 958 959 glClearColor(0.6, 0.6, 0.6, 0.6); 960 glClear(GL_COLOR_BUFFER_BIT); 961 962 glEnable(GL_SCISSOR_TEST); 963 glScissor(4, 4, 4, 4); 964 glClearColor(1.0, 0.0, 0.0, 1.0); 965 glClear(GL_COLOR_BUFFER_BIT); 966 967 glScissor(24, 48, 4, 4); 968 glClearColor(0.0, 1.0, 0.0, 1.0); 969 glClear(GL_COLOR_BUFFER_BIT); 970 971 glScissor(37, 17, 4, 4); 972 glClearColor(0.0, 0.0, 1.0, 1.0); 973 glClear(GL_COLOR_BUFFER_BIT); 974 975 eglSwapBuffers(mEglDisplay, stcEglSurface); 976 977 eglDestroySurface(mEglDisplay, stcEglSurface); 978 979 // Do the consumer side of things 980 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 981 mEglContext)); 982 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 983 984 glDisable(GL_SCISSOR_TEST); 985 986 mST->updateTexImage(); 987 988 glClearColor(0.2, 0.2, 0.2, 0.2); 989 glClear(GL_COLOR_BUFFER_BIT); 990 991 glViewport(0, 0, texWidth, texHeight); 992 drawTexture(); 993 994 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153)); 995 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153)); 996 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153)); 997 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153)); 998 999 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255)); 1000 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255)); 1001 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255)); 1002 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153)); 1003 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153)); 1004 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153)); 1005 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153)); 1006 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153)); 1007 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153)); 1008 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153)); 1009 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153)); 1010 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153)); 1011 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153)); 1012 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153)); 1013 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153)); 1014 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153)); 1015} 1016 1017TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) { 1018 class ProducerThread : public Thread { 1019 public: 1020 ProducerThread(const sp<ANativeWindow>& anw): 1021 mANW(anw), 1022 mDequeueError(NO_ERROR) { 1023 } 1024 1025 virtual ~ProducerThread() { 1026 } 1027 1028 virtual bool threadLoop() { 1029 Mutex::Autolock lock(mMutex); 1030 ANativeWindowBuffer* anb; 1031 1032 // Frame 1 1033 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) { 1034 return false; 1035 } 1036 if (anb == NULL) { 1037 return false; 1038 } 1039 if (mANW->queueBuffer(mANW.get(), anb) 1040 != NO_ERROR) { 1041 return false; 1042 } 1043 1044 // Frame 2 1045 if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) { 1046 return false; 1047 } 1048 if (anb == NULL) { 1049 return false; 1050 } 1051 if (mANW->queueBuffer(mANW.get(), anb) 1052 != NO_ERROR) { 1053 return false; 1054 } 1055 1056 // Frame 3 - error expected 1057 mDequeueError = mANW->dequeueBuffer(mANW.get(), &anb); 1058 return false; 1059 } 1060 1061 status_t getDequeueError() { 1062 Mutex::Autolock lock(mMutex); 1063 return mDequeueError; 1064 } 1065 1066 private: 1067 sp<ANativeWindow> mANW; 1068 status_t mDequeueError; 1069 Mutex mMutex; 1070 }; 1071 1072 sp<FrameWaiter> fw(new FrameWaiter); 1073 mST->setFrameAvailableListener(fw); 1074 ASSERT_EQ(OK, mST->setSynchronousMode(true)); 1075 ASSERT_EQ(OK, mST->setBufferCountServer(2)); 1076 1077 sp<Thread> pt(new ProducerThread(mANW)); 1078 pt->run(); 1079 1080 fw->waitForFrame(); 1081 fw->waitForFrame(); 1082 1083 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to 1084 // block waiting for a buffer to become available. 1085 usleep(100000); 1086 1087 mST->abandon(); 1088 1089 pt->requestExitAndWait(); 1090 ASSERT_EQ(NO_INIT, 1091 reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError()); 1092} 1093 1094/* 1095 * This test is for testing GL -> GL texture streaming via SurfaceTexture. It 1096 * contains functionality to create a producer thread that will perform GL 1097 * rendering to an ANativeWindow that feeds frames to a SurfaceTexture. 1098 * Additionally it supports interlocking the producer and consumer threads so 1099 * that a specific sequence of calls can be deterministically created by the 1100 * test. 1101 * 1102 * The intended usage is as follows: 1103 * 1104 * TEST_F(...) { 1105 * class PT : public ProducerThread { 1106 * virtual void render() { 1107 * ... 1108 * swapBuffers(); 1109 * } 1110 * }; 1111 * 1112 * runProducerThread(new PT()); 1113 * 1114 * // The order of these calls will vary from test to test and may include 1115 * // multiple frames and additional operations (e.g. GL rendering from the 1116 * // texture). 1117 * fc->waitForFrame(); 1118 * mST->updateTexImage(); 1119 * fc->finishFrame(); 1120 * } 1121 * 1122 */ 1123class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest { 1124protected: 1125 1126 // ProducerThread is an abstract base class to simplify the creation of 1127 // OpenGL ES frame producer threads. 1128 class ProducerThread : public Thread { 1129 public: 1130 virtual ~ProducerThread() { 1131 } 1132 1133 void setEglObjects(EGLDisplay producerEglDisplay, 1134 EGLSurface producerEglSurface, 1135 EGLContext producerEglContext) { 1136 mProducerEglDisplay = producerEglDisplay; 1137 mProducerEglSurface = producerEglSurface; 1138 mProducerEglContext = producerEglContext; 1139 } 1140 1141 virtual bool threadLoop() { 1142 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface, 1143 mProducerEglSurface, mProducerEglContext); 1144 render(); 1145 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 1146 EGL_NO_CONTEXT); 1147 return false; 1148 } 1149 1150 protected: 1151 virtual void render() = 0; 1152 1153 void swapBuffers() { 1154 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface); 1155 } 1156 1157 EGLDisplay mProducerEglDisplay; 1158 EGLSurface mProducerEglSurface; 1159 EGLContext mProducerEglContext; 1160 }; 1161 1162 // FrameCondition is a utility class for interlocking between the producer 1163 // and consumer threads. The FrameCondition object should be created and 1164 // destroyed in the consumer thread only. The consumer thread should set 1165 // the FrameCondition as the FrameAvailableListener of the SurfaceTexture, 1166 // and should call both waitForFrame and finishFrame once for each expected 1167 // frame. 1168 // 1169 // This interlocking relies on the fact that onFrameAvailable gets called 1170 // synchronously from SurfaceTexture::queueBuffer. 1171 class FrameCondition : public SurfaceTexture::FrameAvailableListener { 1172 public: 1173 FrameCondition(): 1174 mFrameAvailable(false), 1175 mFrameFinished(false) { 1176 } 1177 1178 // waitForFrame waits for the next frame to arrive. This should be 1179 // called from the consumer thread once for every frame expected by the 1180 // test. 1181 void waitForFrame() { 1182 Mutex::Autolock lock(mMutex); 1183 LOGV("+waitForFrame"); 1184 while (!mFrameAvailable) { 1185 mFrameAvailableCondition.wait(mMutex); 1186 } 1187 mFrameAvailable = false; 1188 LOGV("-waitForFrame"); 1189 } 1190 1191 // Allow the producer to return from its swapBuffers call and continue 1192 // on to produce the next frame. This should be called by the consumer 1193 // thread once for every frame expected by the test. 1194 void finishFrame() { 1195 Mutex::Autolock lock(mMutex); 1196 LOGV("+finishFrame"); 1197 mFrameFinished = true; 1198 mFrameFinishCondition.signal(); 1199 LOGV("-finishFrame"); 1200 } 1201 1202 // This should be called by SurfaceTexture on the producer thread. 1203 virtual void onFrameAvailable() { 1204 Mutex::Autolock lock(mMutex); 1205 LOGV("+onFrameAvailable"); 1206 mFrameAvailable = true; 1207 mFrameAvailableCondition.signal(); 1208 while (!mFrameFinished) { 1209 mFrameFinishCondition.wait(mMutex); 1210 } 1211 mFrameFinished = false; 1212 LOGV("-onFrameAvailable"); 1213 } 1214 1215 protected: 1216 bool mFrameAvailable; 1217 bool mFrameFinished; 1218 1219 Mutex mMutex; 1220 Condition mFrameAvailableCondition; 1221 Condition mFrameFinishCondition; 1222 }; 1223 1224 SurfaceTextureGLToGLTest(): 1225 mProducerEglSurface(EGL_NO_SURFACE), 1226 mProducerEglContext(EGL_NO_CONTEXT) { 1227 } 1228 1229 virtual void SetUp() { 1230 SurfaceTextureGLTest::SetUp(); 1231 1232 EGLConfig myConfig = {0}; 1233 EGLint numConfigs = 0; 1234 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &myConfig, 1235 1, &numConfigs)); 1236 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1237 1238 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, myConfig, 1239 mANW.get(), NULL); 1240 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1241 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface); 1242 1243 mProducerEglContext = eglCreateContext(mEglDisplay, myConfig, 1244 EGL_NO_CONTEXT, getContextAttribs()); 1245 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 1246 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext); 1247 1248 mFC = new FrameCondition(); 1249 mST->setFrameAvailableListener(mFC); 1250 } 1251 1252 virtual void TearDown() { 1253 if (mProducerThread != NULL) { 1254 mProducerThread->requestExitAndWait(); 1255 } 1256 if (mProducerEglContext != EGL_NO_CONTEXT) { 1257 eglDestroyContext(mEglDisplay, mProducerEglContext); 1258 } 1259 if (mProducerEglSurface != EGL_NO_SURFACE) { 1260 eglDestroySurface(mEglDisplay, mProducerEglSurface); 1261 } 1262 mProducerThread.clear(); 1263 mFC.clear(); 1264 SurfaceTextureGLTest::TearDown(); 1265 } 1266 1267 void runProducerThread(const sp<ProducerThread> producerThread) { 1268 ASSERT_TRUE(mProducerThread == NULL); 1269 mProducerThread = producerThread; 1270 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface, 1271 mProducerEglContext); 1272 producerThread->run(); 1273 } 1274 1275 EGLSurface mProducerEglSurface; 1276 EGLContext mProducerEglContext; 1277 sp<ProducerThread> mProducerThread; 1278 sp<FrameCondition> mFC; 1279}; 1280 1281TEST_F(SurfaceTextureGLToGLTest, UpdateTexImageBeforeFrameFinishedCompletes) { 1282 class PT : public ProducerThread { 1283 virtual void render() { 1284 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 1285 glClear(GL_COLOR_BUFFER_BIT); 1286 swapBuffers(); 1287 } 1288 }; 1289 1290 runProducerThread(new PT()); 1291 1292 mFC->waitForFrame(); 1293 mST->updateTexImage(); 1294 mFC->finishFrame(); 1295 1296 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! 1297} 1298 1299TEST_F(SurfaceTextureGLToGLTest, UpdateTexImageAfterFrameFinishedCompletes) { 1300 class PT : public ProducerThread { 1301 virtual void render() { 1302 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 1303 glClear(GL_COLOR_BUFFER_BIT); 1304 swapBuffers(); 1305 } 1306 }; 1307 1308 runProducerThread(new PT()); 1309 1310 mFC->waitForFrame(); 1311 mFC->finishFrame(); 1312 mST->updateTexImage(); 1313 1314 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! 1315} 1316 1317TEST_F(SurfaceTextureGLToGLTest, RepeatedUpdateTexImageBeforeFrameFinishedCompletes) { 1318 enum { NUM_ITERATIONS = 1024 }; 1319 1320 class PT : public ProducerThread { 1321 virtual void render() { 1322 for (int i = 0; i < NUM_ITERATIONS; i++) { 1323 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 1324 glClear(GL_COLOR_BUFFER_BIT); 1325 LOGV("+swapBuffers"); 1326 swapBuffers(); 1327 LOGV("-swapBuffers"); 1328 } 1329 } 1330 }; 1331 1332 runProducerThread(new PT()); 1333 1334 for (int i = 0; i < NUM_ITERATIONS; i++) { 1335 mFC->waitForFrame(); 1336 LOGV("+updateTexImage"); 1337 mST->updateTexImage(); 1338 LOGV("-updateTexImage"); 1339 mFC->finishFrame(); 1340 1341 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! 1342 } 1343} 1344 1345TEST_F(SurfaceTextureGLToGLTest, RepeatedUpdateTexImageAfterFrameFinishedCompletes) { 1346 enum { NUM_ITERATIONS = 1024 }; 1347 1348 class PT : public ProducerThread { 1349 virtual void render() { 1350 for (int i = 0; i < NUM_ITERATIONS; i++) { 1351 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 1352 glClear(GL_COLOR_BUFFER_BIT); 1353 LOGV("+swapBuffers"); 1354 swapBuffers(); 1355 LOGV("-swapBuffers"); 1356 } 1357 } 1358 }; 1359 1360 runProducerThread(new PT()); 1361 1362 for (int i = 0; i < NUM_ITERATIONS; i++) { 1363 mFC->waitForFrame(); 1364 mFC->finishFrame(); 1365 LOGV("+updateTexImage"); 1366 mST->updateTexImage(); 1367 LOGV("-updateTexImage"); 1368 1369 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! 1370 } 1371} 1372 1373// XXX: This test is disabled because it is currently hanging on some devices. 1374TEST_F(SurfaceTextureGLToGLTest, DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) { 1375 enum { NUM_ITERATIONS = 64 }; 1376 1377 class PT : public ProducerThread { 1378 virtual void render() { 1379 for (int i = 0; i < NUM_ITERATIONS; i++) { 1380 glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 1381 glClear(GL_COLOR_BUFFER_BIT); 1382 LOGV("+swapBuffers"); 1383 swapBuffers(); 1384 LOGV("-swapBuffers"); 1385 } 1386 } 1387 }; 1388 1389 ASSERT_EQ(OK, mST->setSynchronousMode(true)); 1390 ASSERT_EQ(OK, mST->setBufferCountServer(2)); 1391 1392 runProducerThread(new PT()); 1393 1394 // Allow three frames to be rendered and queued before starting the 1395 // rendering in this thread. For the latter two frames we don't call 1396 // updateTexImage so the next dequeue from the producer thread will block 1397 // waiting for a frame to become available. 1398 mFC->waitForFrame(); 1399 mFC->finishFrame(); 1400 1401 // We must call updateTexImage to consume the first frame so that the 1402 // SurfaceTexture is able to reduce the buffer count to 2. This is because 1403 // the GL driver may dequeue a buffer when the EGLSurface is created, and 1404 // that happens before we call setBufferCountServer. It's possible that the 1405 // driver does not dequeue a buffer at EGLSurface creation time, so we 1406 // cannot rely on this to cause the second dequeueBuffer call to block. 1407 mST->updateTexImage(); 1408 1409 mFC->waitForFrame(); 1410 mFC->finishFrame(); 1411 mFC->waitForFrame(); 1412 mFC->finishFrame(); 1413 1414 // Sleep for 100ms to allow the producer thread's dequeueBuffer call to 1415 // block waiting for a buffer to become available. 1416 usleep(100000); 1417 1418 // Render and present a number of images. This thread should not be blocked 1419 // by the fact that the producer thread is blocking in dequeue. 1420 for (int i = 0; i < NUM_ITERATIONS; i++) { 1421 glClear(GL_COLOR_BUFFER_BIT); 1422 eglSwapBuffers(mEglDisplay, mEglSurface); 1423 } 1424 1425 // Consume the two pending buffers to unblock the producer thread. 1426 mST->updateTexImage(); 1427 mST->updateTexImage(); 1428 1429 // Consume the remaining buffers from the producer thread. 1430 for (int i = 0; i < NUM_ITERATIONS-3; i++) { 1431 mFC->waitForFrame(); 1432 mFC->finishFrame(); 1433 LOGV("+updateTexImage"); 1434 mST->updateTexImage(); 1435 LOGV("-updateTexImage"); 1436 } 1437} 1438 1439} // namespace android 1440