Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | 2640bfd | 2011-07-14 17:11:47 -0700 | [diff] [blame] | 17 | #define LOG_TAG "SurfaceTexture_test" |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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> |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 25 | #include <utils/threads.h> |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
| 38 | namespace android { |
| 39 | |
| 40 | class GLTest : public ::testing::Test { |
| 41 | protected: |
| 42 | |
| 43 | GLTest(): |
| 44 | mEglDisplay(EGL_NO_DISPLAY), |
| 45 | mEglSurface(EGL_NO_SURFACE), |
| 46 | mEglContext(EGL_NO_CONTEXT) { |
| 47 | } |
| 48 | |
| 49 | virtual void SetUp() { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 61 | EGLint numConfigs = 0; |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 62 | EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | fc85012 | 2011-04-25 16:40:05 -0700 | [diff] [blame] | 80 | mSurfaceControl = mComposerClient->createSurface( |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 88 | SurfaceComposerClient::openGlobalTransaction(); |
Jamie Gennis | 5dd0c4f | 2011-06-13 19:06:52 -0700 | [diff] [blame] | 89 | ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF)); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 90 | ASSERT_EQ(NO_ERROR, mSurfaceControl->show()); |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 91 | SurfaceComposerClient::closeGlobalTransaction(); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 92 | |
| 93 | sp<ANativeWindow> window = mSurfaceControl->getSurface(); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 94 | mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 95 | window.get(), NULL); |
| 96 | } else { |
| 97 | EGLint pbufferAttribs[] = { |
| 98 | EGL_WIDTH, getSurfaceWidth(), |
| 99 | EGL_HEIGHT, getSurfaceHeight(), |
| 100 | EGL_NONE }; |
| 101 | |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 102 | mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 103 | pbufferAttribs); |
| 104 | } |
| 105 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); |
| 106 | ASSERT_NE(EGL_NO_SURFACE, mEglSurface); |
| 107 | |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 108 | mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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() { |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 177 | return 512; |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | virtual EGLint getSurfaceHeight() { |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 181 | return 512; |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | 824efa7 | 2011-06-13 13:41:01 -0700 | [diff] [blame] | 274 | static int abs(int value) { |
| 275 | return value > 0 ? value : -value; |
| 276 | } |
| 277 | |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 278 | ::testing::AssertionResult checkPixel(int x, int y, int r, |
Jamie Gennis | 824efa7 | 2011-06-13 13:41:01 -0700 | [diff] [blame] | 279 | int g, int b, int a, int tolerance=2) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | } |
Jamie Gennis | 824efa7 | 2011-06-13 13:41:01 -0700 | [diff] [blame] | 293 | if (r >= 0 && abs(r - int(pixel[0])) > tolerance) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 294 | msg += String8::format("r(%d isn't %d)", pixel[0], r); |
| 295 | } |
Jamie Gennis | 824efa7 | 2011-06-13 13:41:01 -0700 | [diff] [blame] | 296 | if (g >= 0 && abs(g - int(pixel[1])) > tolerance) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 297 | if (!msg.isEmpty()) { |
| 298 | msg += " "; |
| 299 | } |
| 300 | msg += String8::format("g(%d isn't %d)", pixel[1], g); |
| 301 | } |
Jamie Gennis | 824efa7 | 2011-06-13 13:41:01 -0700 | [diff] [blame] | 302 | if (b >= 0 && abs(b - int(pixel[2])) > tolerance) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 303 | if (!msg.isEmpty()) { |
| 304 | msg += " "; |
| 305 | } |
| 306 | msg += String8::format("b(%d isn't %d)", pixel[2], b); |
| 307 | } |
Jamie Gennis | 824efa7 | 2011-06-13 13:41:01 -0700 | [diff] [blame] | 308 | if (a >= 0 && abs(a - int(pixel[3])) > tolerance) { |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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; |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 330 | EGLConfig mGlConfig; |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | // XXX: Code above this point should live elsewhere |
| 334 | |
| 335 | class SurfaceTextureGLTest : public GLTest { |
| 336 | protected: |
| 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 | |
Jamie Gennis | 2640bfd | 2011-07-14 17:11:47 -0700 | [diff] [blame] | 383 | virtual void TearDown() { |
| 384 | mANW.clear(); |
| 385 | mSTC.clear(); |
| 386 | mST.clear(); |
| 387 | GLTest::TearDown(); |
| 388 | } |
| 389 | |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 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 | |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | dfcff4b | 2011-06-17 11:39:18 -0700 | [diff] [blame] | 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 | |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 |
| 466 | void 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. |
| 496 | void 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 | |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 520 | void 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 | |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 534 | TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) { |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 535 | const int texWidth = 64; |
| 536 | const int texHeight = 66; |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 537 | |
| 538 | ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 539 | texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 540 | ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), |
| 541 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 542 | |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 543 | ANativeWindowBuffer* anb; |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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)); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 553 | fillYV12Buffer(img, texWidth, texHeight, buf->getStride()); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 562 | glViewport(0, 0, texWidth, texHeight); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 563 | drawTexture(); |
| 564 | |
| 565 | EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255)); |
| 566 | EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255)); |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 567 | EXPECT_TRUE(checkPixel(63, 65, 0, 133, 0, 255)); |
| 568 | EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255)); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 569 | |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 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)); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 573 | EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255)); |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 574 | EXPECT_TRUE(checkPixel(31, 9, 107, 24, 87, 255)); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 575 | EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255)); |
| 576 | EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255)); |
| 577 | } |
| 578 | |
Jamie Gennis | d05bb2e | 2011-06-14 15:41:45 -0700 | [diff] [blame] | 579 | TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) { |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 580 | const int texWidth = 64; |
| 581 | const int texHeight = 64; |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 582 | |
| 583 | ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 584 | texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 585 | ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), |
| 586 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 587 | |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 588 | ANativeWindowBuffer* anb; |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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)); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 598 | fillYV12Buffer(img, texWidth, texHeight, buf->getStride()); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 607 | glViewport(0, 0, texWidth, texHeight); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 608 | drawTexture(); |
| 609 | |
Jamie Gennis | d05bb2e | 2011-06-14 15:41:45 -0700 | [diff] [blame] | 610 | EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255)); |
| 611 | EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255)); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 612 | EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255)); |
| 613 | EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255)); |
| 614 | |
Jamie Gennis | d05bb2e | 2011-06-14 15:41:45 -0700 | [diff] [blame] | 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)); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) { |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 625 | const int texWidth = 64; |
| 626 | const int texHeight = 66; |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 627 | |
| 628 | ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 629 | texWidth, texHeight, HAL_PIXEL_FORMAT_YV12)); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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}, |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 637 | {4, 6, texWidth, 36}, |
| 638 | {4, 6, 22, texHeight}, |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 648 | ANativeWindowBuffer* anb; |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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)); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 657 | fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 666 | glViewport(0, 0, 64, 64); |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 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 | |
Jamie Gennis | dfcff4b | 2011-06-17 11:39:18 -0700 | [diff] [blame] | 684 | // This test is intended to catch synchronization bugs between the CPU-written |
| 685 | // and GPU-read buffers. |
| 686 | TEST_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 | |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 835 | // XXX: This test is disabled because there are currently no drivers that can |
| 836 | // handle RGBA textures with the GL_TEXTURE_EXTERNAL_OES target. |
| 837 | TEST_F(SurfaceTextureGLTest, DISABLED_TexturingFromCpuFilledRGBABufferNpot) { |
| 838 | const int texWidth = 64; |
| 839 | const int texHeight = 66; |
| 840 | |
| 841 | ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), |
| 842 | texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888)); |
| 843 | ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), |
| 844 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 845 | |
| 846 | android_native_buffer_t* anb; |
| 847 | ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb)); |
| 848 | ASSERT_TRUE(anb != NULL); |
| 849 | |
| 850 | sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); |
| 851 | ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())); |
| 852 | |
| 853 | // Fill the buffer with the a checkerboard pattern |
| 854 | uint8_t* img = NULL; |
| 855 | buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); |
| 856 | fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride()); |
| 857 | buf->unlock(); |
| 858 | ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())); |
| 859 | |
| 860 | mST->updateTexImage(); |
| 861 | |
| 862 | glClearColor(0.2, 0.2, 0.2, 0.2); |
| 863 | glClear(GL_COLOR_BUFFER_BIT); |
| 864 | |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 865 | glViewport(0, 0, texWidth, texHeight); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 866 | drawTexture(); |
| 867 | |
| 868 | EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35)); |
| 869 | EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231)); |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 870 | EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231)); |
| 871 | EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35)); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 872 | |
| 873 | EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231)); |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 874 | EXPECT_TRUE(checkPixel(24, 63, 38, 228, 231, 35)); |
| 875 | EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35)); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 876 | EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35)); |
| 877 | EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231)); |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 878 | EXPECT_TRUE(checkPixel(37, 33, 228, 38, 38, 38)); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 879 | EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231)); |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 880 | EXPECT_TRUE(checkPixel(36, 47, 228, 35, 231, 231)); |
| 881 | EXPECT_TRUE(checkPixel(24, 63, 38, 228, 231, 35)); |
| 882 | EXPECT_TRUE(checkPixel(48, 3, 228, 228, 38, 35)); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 883 | EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231)); |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 884 | EXPECT_TRUE(checkPixel(24, 25, 41, 41, 231, 231)); |
| 885 | EXPECT_TRUE(checkPixel(10, 9, 38, 38, 231, 231)); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 886 | EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231)); |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 887 | EXPECT_TRUE(checkPixel(56, 31, 38, 228, 231, 35)); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 888 | EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231)); |
| 889 | } |
| 890 | |
| 891 | // XXX: This test is disabled because there are currently no drivers that can |
| 892 | // handle RGBA textures with the GL_TEXTURE_EXTERNAL_OES target. |
| 893 | TEST_F(SurfaceTextureGLTest, DISABLED_TexturingFromCpuFilledRGBABufferPow2) { |
| 894 | const int texWidth = 64; |
| 895 | const int texHeight = 64; |
| 896 | |
| 897 | ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(), |
| 898 | texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888)); |
| 899 | ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(), |
| 900 | GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN)); |
| 901 | |
| 902 | android_native_buffer_t* anb; |
| 903 | ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb)); |
| 904 | ASSERT_TRUE(anb != NULL); |
| 905 | |
| 906 | sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); |
| 907 | ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())); |
| 908 | |
| 909 | // Fill the buffer with the a checkerboard pattern |
| 910 | uint8_t* img = NULL; |
| 911 | buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); |
| 912 | fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride()); |
| 913 | buf->unlock(); |
| 914 | ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())); |
| 915 | |
| 916 | mST->updateTexImage(); |
| 917 | |
| 918 | glClearColor(0.2, 0.2, 0.2, 0.2); |
| 919 | glClear(GL_COLOR_BUFFER_BIT); |
| 920 | |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 921 | glViewport(0, 0, texWidth, texHeight); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 922 | drawTexture(); |
| 923 | |
| 924 | EXPECT_TRUE(checkPixel( 0, 0, 231, 231, 231, 231)); |
| 925 | EXPECT_TRUE(checkPixel(63, 0, 35, 35, 35, 35)); |
| 926 | EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231)); |
| 927 | EXPECT_TRUE(checkPixel( 0, 63, 35, 35, 35, 35)); |
| 928 | |
| 929 | EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231, 35)); |
| 930 | EXPECT_TRUE(checkPixel(16, 1, 231, 231, 35, 231)); |
| 931 | EXPECT_TRUE(checkPixel(21, 12, 231, 35, 35, 231)); |
| 932 | EXPECT_TRUE(checkPixel(26, 51, 231, 35, 231, 35)); |
| 933 | EXPECT_TRUE(checkPixel( 5, 32, 35, 231, 231, 35)); |
| 934 | EXPECT_TRUE(checkPixel(13, 8, 35, 231, 231, 231)); |
| 935 | EXPECT_TRUE(checkPixel(46, 3, 35, 35, 231, 35)); |
| 936 | EXPECT_TRUE(checkPixel(30, 33, 35, 35, 35, 35)); |
| 937 | EXPECT_TRUE(checkPixel( 6, 52, 231, 231, 35, 35)); |
| 938 | EXPECT_TRUE(checkPixel(55, 33, 35, 231, 35, 231)); |
| 939 | EXPECT_TRUE(checkPixel(16, 29, 35, 35, 231, 231)); |
| 940 | EXPECT_TRUE(checkPixel( 1, 30, 35, 35, 35, 231)); |
| 941 | EXPECT_TRUE(checkPixel(41, 37, 35, 35, 231, 231)); |
| 942 | EXPECT_TRUE(checkPixel(46, 29, 231, 231, 35, 35)); |
| 943 | EXPECT_TRUE(checkPixel(15, 25, 35, 231, 35, 231)); |
| 944 | EXPECT_TRUE(checkPixel( 3, 52, 35, 231, 35, 35)); |
| 945 | } |
| 946 | |
| 947 | // XXX: This test is disabled because there are currently no drivers that can |
| 948 | // handle RGBA textures with the GL_TEXTURE_EXTERNAL_OES target. |
| 949 | TEST_F(SurfaceTextureGLTest, DISABLED_TexturingFromGLFilledRGBABufferPow2) { |
| 950 | const int texWidth = 64; |
| 951 | const int texHeight = 64; |
| 952 | |
| 953 | mST->setDefaultBufferSize(texWidth, texHeight); |
| 954 | |
| 955 | // Do the producer side of things |
| 956 | EGLSurface stcEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, |
| 957 | mANW.get(), NULL); |
| 958 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); |
| 959 | ASSERT_NE(EGL_NO_SURFACE, mEglSurface); |
| 960 | |
| 961 | EXPECT_TRUE(eglMakeCurrent(mEglDisplay, stcEglSurface, stcEglSurface, |
| 962 | mEglContext)); |
| 963 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); |
| 964 | |
| 965 | glClearColor(0.6, 0.6, 0.6, 0.6); |
| 966 | glClear(GL_COLOR_BUFFER_BIT); |
| 967 | |
| 968 | glEnable(GL_SCISSOR_TEST); |
| 969 | glScissor(4, 4, 4, 4); |
| 970 | glClearColor(1.0, 0.0, 0.0, 1.0); |
| 971 | glClear(GL_COLOR_BUFFER_BIT); |
| 972 | |
| 973 | glScissor(24, 48, 4, 4); |
| 974 | glClearColor(0.0, 1.0, 0.0, 1.0); |
| 975 | glClear(GL_COLOR_BUFFER_BIT); |
| 976 | |
| 977 | glScissor(37, 17, 4, 4); |
| 978 | glClearColor(0.0, 0.0, 1.0, 1.0); |
| 979 | glClear(GL_COLOR_BUFFER_BIT); |
| 980 | |
| 981 | eglSwapBuffers(mEglDisplay, stcEglSurface); |
| 982 | |
| 983 | // Do the consumer side of things |
| 984 | EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, |
| 985 | mEglContext)); |
| 986 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); |
| 987 | |
| 988 | glDisable(GL_SCISSOR_TEST); |
| 989 | |
| 990 | mST->updateTexImage(); |
| 991 | |
| 992 | glClearColor(0.2, 0.2, 0.2, 0.2); |
| 993 | glClear(GL_COLOR_BUFFER_BIT); |
| 994 | |
Jamie Gennis | c8c5152 | 2011-06-15 14:24:38 -0700 | [diff] [blame] | 995 | glViewport(0, 0, texWidth, texHeight); |
Jamie Gennis | 1876d13 | 2011-03-17 16:32:52 -0700 | [diff] [blame] | 996 | drawTexture(); |
| 997 | |
| 998 | EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153)); |
| 999 | EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153)); |
| 1000 | EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153)); |
| 1001 | EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153)); |
| 1002 | |
| 1003 | EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255)); |
| 1004 | EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255)); |
| 1005 | EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255)); |
| 1006 | EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153)); |
| 1007 | EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153)); |
| 1008 | EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153)); |
| 1009 | EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153)); |
| 1010 | EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153)); |
| 1011 | EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153)); |
| 1012 | EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153)); |
| 1013 | EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153)); |
| 1014 | EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153)); |
| 1015 | EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153)); |
| 1016 | EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153)); |
| 1017 | EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153)); |
| 1018 | EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153)); |
| 1019 | } |
| 1020 | |
Jamie Gennis | 7b305ff | 2011-07-19 12:08:33 -0700 | [diff] [blame^] | 1021 | TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) { |
| 1022 | class ProducerThread : public Thread { |
| 1023 | public: |
| 1024 | ProducerThread(const sp<ANativeWindow>& anw): |
| 1025 | mANW(anw), |
| 1026 | mDequeueError(NO_ERROR) { |
| 1027 | } |
| 1028 | |
| 1029 | virtual ~ProducerThread() { |
| 1030 | } |
| 1031 | |
| 1032 | virtual bool threadLoop() { |
| 1033 | Mutex::Autolock lock(mMutex); |
| 1034 | ANativeWindowBuffer* anb; |
| 1035 | |
| 1036 | // Frame 1 |
| 1037 | if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) { |
| 1038 | return false; |
| 1039 | } |
| 1040 | if (anb == NULL) { |
| 1041 | return false; |
| 1042 | } |
| 1043 | if (mANW->queueBuffer(mANW.get(), anb) |
| 1044 | != NO_ERROR) { |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
| 1048 | // Frame 2 |
| 1049 | if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) { |
| 1050 | return false; |
| 1051 | } |
| 1052 | if (anb == NULL) { |
| 1053 | return false; |
| 1054 | } |
| 1055 | if (mANW->queueBuffer(mANW.get(), anb) |
| 1056 | != NO_ERROR) { |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | // Frame 3 - error expected |
| 1061 | mDequeueError = mANW->dequeueBuffer(mANW.get(), &anb); |
| 1062 | return false; |
| 1063 | } |
| 1064 | |
| 1065 | status_t getDequeueError() { |
| 1066 | Mutex::Autolock lock(mMutex); |
| 1067 | return mDequeueError; |
| 1068 | } |
| 1069 | |
| 1070 | private: |
| 1071 | sp<ANativeWindow> mANW; |
| 1072 | status_t mDequeueError; |
| 1073 | Mutex mMutex; |
| 1074 | }; |
| 1075 | |
| 1076 | sp<FrameWaiter> fw(new FrameWaiter); |
| 1077 | mST->setFrameAvailableListener(fw); |
| 1078 | ASSERT_EQ(OK, mST->setSynchronousMode(true)); |
| 1079 | ASSERT_EQ(OK, mST->setBufferCountServer(2)); |
| 1080 | |
| 1081 | sp<Thread> pt(new ProducerThread(mANW)); |
| 1082 | pt->run(); |
| 1083 | |
| 1084 | fw->waitForFrame(); |
| 1085 | fw->waitForFrame(); |
| 1086 | |
| 1087 | // Sleep for 100ms to allow the producer thread's dequeueBuffer call to |
| 1088 | // block waiting for a buffer to become available. |
| 1089 | usleep(100000); |
| 1090 | |
| 1091 | mST->abandon(); |
| 1092 | |
| 1093 | pt->requestExitAndWait(); |
| 1094 | ASSERT_EQ(NO_INIT, |
| 1095 | reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError()); |
| 1096 | } |
| 1097 | |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1098 | /* |
| 1099 | * This test is for testing GL -> GL texture streaming via SurfaceTexture. It |
| 1100 | * contains functionality to create a producer thread that will perform GL |
| 1101 | * rendering to an ANativeWindow that feeds frames to a SurfaceTexture. |
| 1102 | * Additionally it supports interlocking the producer and consumer threads so |
| 1103 | * that a specific sequence of calls can be deterministically created by the |
| 1104 | * test. |
| 1105 | * |
| 1106 | * The intended usage is as follows: |
| 1107 | * |
| 1108 | * TEST_F(...) { |
| 1109 | * class PT : public ProducerThread { |
| 1110 | * virtual void render() { |
| 1111 | * ... |
| 1112 | * swapBuffers(); |
| 1113 | * } |
| 1114 | * }; |
| 1115 | * |
| 1116 | * runProducerThread(new PT()); |
| 1117 | * |
| 1118 | * // The order of these calls will vary from test to test and may include |
| 1119 | * // multiple frames and additional operations (e.g. GL rendering from the |
| 1120 | * // texture). |
| 1121 | * fc->waitForFrame(); |
| 1122 | * mST->updateTexImage(); |
| 1123 | * fc->finishFrame(); |
| 1124 | * } |
| 1125 | * |
| 1126 | */ |
| 1127 | class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest { |
| 1128 | protected: |
| 1129 | |
| 1130 | // ProducerThread is an abstract base class to simplify the creation of |
| 1131 | // OpenGL ES frame producer threads. |
| 1132 | class ProducerThread : public Thread { |
| 1133 | public: |
| 1134 | virtual ~ProducerThread() { |
| 1135 | } |
| 1136 | |
| 1137 | void setEglObjects(EGLDisplay producerEglDisplay, |
| 1138 | EGLSurface producerEglSurface, |
| 1139 | EGLContext producerEglContext) { |
| 1140 | mProducerEglDisplay = producerEglDisplay; |
| 1141 | mProducerEglSurface = producerEglSurface; |
| 1142 | mProducerEglContext = producerEglContext; |
| 1143 | } |
| 1144 | |
| 1145 | virtual bool threadLoop() { |
| 1146 | eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface, |
| 1147 | mProducerEglSurface, mProducerEglContext); |
| 1148 | render(); |
| 1149 | eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, |
| 1150 | EGL_NO_CONTEXT); |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | protected: |
| 1155 | virtual void render() = 0; |
| 1156 | |
| 1157 | void swapBuffers() { |
| 1158 | eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface); |
| 1159 | } |
| 1160 | |
| 1161 | EGLDisplay mProducerEglDisplay; |
| 1162 | EGLSurface mProducerEglSurface; |
| 1163 | EGLContext mProducerEglContext; |
| 1164 | }; |
| 1165 | |
| 1166 | // FrameCondition is a utility class for interlocking between the producer |
| 1167 | // and consumer threads. The FrameCondition object should be created and |
| 1168 | // destroyed in the consumer thread only. The consumer thread should set |
| 1169 | // the FrameCondition as the FrameAvailableListener of the SurfaceTexture, |
| 1170 | // and should call both waitForFrame and finishFrame once for each expected |
| 1171 | // frame. |
| 1172 | // |
| 1173 | // This interlocking relies on the fact that onFrameAvailable gets called |
| 1174 | // synchronously from SurfaceTexture::queueBuffer. |
| 1175 | class FrameCondition : public SurfaceTexture::FrameAvailableListener { |
| 1176 | public: |
Jamie Gennis | 2640bfd | 2011-07-14 17:11:47 -0700 | [diff] [blame] | 1177 | FrameCondition(): |
| 1178 | mFrameAvailable(false), |
| 1179 | mFrameFinished(false) { |
| 1180 | } |
| 1181 | |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1182 | // waitForFrame waits for the next frame to arrive. This should be |
| 1183 | // called from the consumer thread once for every frame expected by the |
| 1184 | // test. |
| 1185 | void waitForFrame() { |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1186 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 2640bfd | 2011-07-14 17:11:47 -0700 | [diff] [blame] | 1187 | LOGV("+waitForFrame"); |
| 1188 | while (!mFrameAvailable) { |
| 1189 | mFrameAvailableCondition.wait(mMutex); |
| 1190 | } |
| 1191 | mFrameAvailable = false; |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1192 | LOGV("-waitForFrame"); |
| 1193 | } |
| 1194 | |
| 1195 | // Allow the producer to return from its swapBuffers call and continue |
| 1196 | // on to produce the next frame. This should be called by the consumer |
| 1197 | // thread once for every frame expected by the test. |
| 1198 | void finishFrame() { |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1199 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 2640bfd | 2011-07-14 17:11:47 -0700 | [diff] [blame] | 1200 | LOGV("+finishFrame"); |
| 1201 | mFrameFinished = true; |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1202 | mFrameFinishCondition.signal(); |
| 1203 | LOGV("-finishFrame"); |
| 1204 | } |
| 1205 | |
| 1206 | // This should be called by SurfaceTexture on the producer thread. |
| 1207 | virtual void onFrameAvailable() { |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1208 | Mutex::Autolock lock(mMutex); |
Jamie Gennis | 2640bfd | 2011-07-14 17:11:47 -0700 | [diff] [blame] | 1209 | LOGV("+onFrameAvailable"); |
| 1210 | mFrameAvailable = true; |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1211 | mFrameAvailableCondition.signal(); |
Jamie Gennis | 2640bfd | 2011-07-14 17:11:47 -0700 | [diff] [blame] | 1212 | while (!mFrameFinished) { |
| 1213 | mFrameFinishCondition.wait(mMutex); |
| 1214 | } |
| 1215 | mFrameFinished = false; |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1216 | LOGV("-onFrameAvailable"); |
| 1217 | } |
| 1218 | |
| 1219 | protected: |
Jamie Gennis | 2640bfd | 2011-07-14 17:11:47 -0700 | [diff] [blame] | 1220 | bool mFrameAvailable; |
| 1221 | bool mFrameFinished; |
| 1222 | |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1223 | Mutex mMutex; |
| 1224 | Condition mFrameAvailableCondition; |
| 1225 | Condition mFrameFinishCondition; |
| 1226 | }; |
| 1227 | |
| 1228 | SurfaceTextureGLToGLTest(): |
| 1229 | mProducerEglSurface(EGL_NO_SURFACE), |
| 1230 | mProducerEglContext(EGL_NO_CONTEXT) { |
| 1231 | } |
| 1232 | |
| 1233 | virtual void SetUp() { |
| 1234 | SurfaceTextureGLTest::SetUp(); |
| 1235 | |
| 1236 | EGLConfig myConfig = {0}; |
| 1237 | EGLint numConfigs = 0; |
| 1238 | EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &myConfig, |
| 1239 | 1, &numConfigs)); |
| 1240 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); |
| 1241 | |
| 1242 | mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, myConfig, |
| 1243 | mANW.get(), NULL); |
| 1244 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); |
| 1245 | ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface); |
| 1246 | |
| 1247 | mProducerEglContext = eglCreateContext(mEglDisplay, myConfig, |
| 1248 | EGL_NO_CONTEXT, getContextAttribs()); |
| 1249 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); |
| 1250 | ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext); |
| 1251 | |
| 1252 | mFC = new FrameCondition(); |
| 1253 | mST->setFrameAvailableListener(mFC); |
| 1254 | } |
| 1255 | |
| 1256 | virtual void TearDown() { |
| 1257 | if (mProducerThread != NULL) { |
| 1258 | mProducerThread->requestExitAndWait(); |
| 1259 | } |
| 1260 | if (mProducerEglContext != EGL_NO_CONTEXT) { |
| 1261 | eglDestroyContext(mEglDisplay, mProducerEglContext); |
| 1262 | } |
| 1263 | if (mProducerEglSurface != EGL_NO_SURFACE) { |
| 1264 | eglDestroySurface(mEglDisplay, mProducerEglSurface); |
| 1265 | } |
| 1266 | mProducerThread.clear(); |
| 1267 | mFC.clear(); |
Jamie Gennis | 2640bfd | 2011-07-14 17:11:47 -0700 | [diff] [blame] | 1268 | SurfaceTextureGLTest::TearDown(); |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | void runProducerThread(const sp<ProducerThread> producerThread) { |
| 1272 | ASSERT_TRUE(mProducerThread == NULL); |
| 1273 | mProducerThread = producerThread; |
| 1274 | producerThread->setEglObjects(mEglDisplay, mProducerEglSurface, |
| 1275 | mProducerEglContext); |
| 1276 | producerThread->run(); |
| 1277 | } |
| 1278 | |
| 1279 | EGLSurface mProducerEglSurface; |
| 1280 | EGLContext mProducerEglContext; |
| 1281 | sp<ProducerThread> mProducerThread; |
| 1282 | sp<FrameCondition> mFC; |
| 1283 | }; |
| 1284 | |
Jamie Gennis | 6e50219 | 2011-07-21 14:31:31 -0700 | [diff] [blame] | 1285 | TEST_F(SurfaceTextureGLToGLTest, UpdateTexImageBeforeFrameFinishedCompletes) { |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1286 | class PT : public ProducerThread { |
| 1287 | virtual void render() { |
| 1288 | glClearColor(0.0f, 1.0f, 0.0f, 1.0f); |
| 1289 | glClear(GL_COLOR_BUFFER_BIT); |
| 1290 | swapBuffers(); |
| 1291 | } |
| 1292 | }; |
| 1293 | |
| 1294 | runProducerThread(new PT()); |
| 1295 | |
| 1296 | mFC->waitForFrame(); |
| 1297 | mST->updateTexImage(); |
| 1298 | mFC->finishFrame(); |
| 1299 | |
| 1300 | // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! |
Jamie Gennis | d99c088 | 2011-03-10 16:24:46 -0800 | [diff] [blame] | 1301 | } |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1302 | |
Jamie Gennis | 6e50219 | 2011-07-21 14:31:31 -0700 | [diff] [blame] | 1303 | TEST_F(SurfaceTextureGLToGLTest, UpdateTexImageAfterFrameFinishedCompletes) { |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1304 | class PT : public ProducerThread { |
| 1305 | virtual void render() { |
| 1306 | glClearColor(0.0f, 1.0f, 0.0f, 1.0f); |
| 1307 | glClear(GL_COLOR_BUFFER_BIT); |
| 1308 | swapBuffers(); |
| 1309 | } |
| 1310 | }; |
| 1311 | |
| 1312 | runProducerThread(new PT()); |
| 1313 | |
| 1314 | mFC->waitForFrame(); |
| 1315 | mFC->finishFrame(); |
| 1316 | mST->updateTexImage(); |
| 1317 | |
| 1318 | // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! |
| 1319 | } |
| 1320 | |
Jamie Gennis | 6e50219 | 2011-07-21 14:31:31 -0700 | [diff] [blame] | 1321 | TEST_F(SurfaceTextureGLToGLTest, RepeatedUpdateTexImageBeforeFrameFinishedCompletes) { |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1322 | enum { NUM_ITERATIONS = 1024 }; |
| 1323 | |
| 1324 | class PT : public ProducerThread { |
| 1325 | virtual void render() { |
| 1326 | for (int i = 0; i < NUM_ITERATIONS; i++) { |
| 1327 | glClearColor(0.0f, 1.0f, 0.0f, 1.0f); |
| 1328 | glClear(GL_COLOR_BUFFER_BIT); |
| 1329 | LOGV("+swapBuffers"); |
| 1330 | swapBuffers(); |
| 1331 | LOGV("-swapBuffers"); |
| 1332 | } |
| 1333 | } |
| 1334 | }; |
| 1335 | |
| 1336 | runProducerThread(new PT()); |
| 1337 | |
| 1338 | for (int i = 0; i < NUM_ITERATIONS; i++) { |
| 1339 | mFC->waitForFrame(); |
| 1340 | LOGV("+updateTexImage"); |
| 1341 | mST->updateTexImage(); |
| 1342 | LOGV("-updateTexImage"); |
| 1343 | mFC->finishFrame(); |
| 1344 | |
| 1345 | // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! |
| 1346 | } |
| 1347 | } |
| 1348 | |
Jamie Gennis | 6e50219 | 2011-07-21 14:31:31 -0700 | [diff] [blame] | 1349 | TEST_F(SurfaceTextureGLToGLTest, RepeatedUpdateTexImageAfterFrameFinishedCompletes) { |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1350 | enum { NUM_ITERATIONS = 1024 }; |
| 1351 | |
| 1352 | class PT : public ProducerThread { |
| 1353 | virtual void render() { |
| 1354 | for (int i = 0; i < NUM_ITERATIONS; i++) { |
| 1355 | glClearColor(0.0f, 1.0f, 0.0f, 1.0f); |
| 1356 | glClear(GL_COLOR_BUFFER_BIT); |
| 1357 | LOGV("+swapBuffers"); |
| 1358 | swapBuffers(); |
| 1359 | LOGV("-swapBuffers"); |
| 1360 | } |
| 1361 | } |
| 1362 | }; |
| 1363 | |
| 1364 | runProducerThread(new PT()); |
| 1365 | |
| 1366 | for (int i = 0; i < NUM_ITERATIONS; i++) { |
| 1367 | mFC->waitForFrame(); |
| 1368 | mFC->finishFrame(); |
| 1369 | LOGV("+updateTexImage"); |
| 1370 | mST->updateTexImage(); |
| 1371 | LOGV("-updateTexImage"); |
| 1372 | |
| 1373 | // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported! |
| 1374 | } |
| 1375 | } |
| 1376 | |
Jamie Gennis | 6e50219 | 2011-07-21 14:31:31 -0700 | [diff] [blame] | 1377 | // XXX: This test is disabled because it is currently hanging on some devices. |
| 1378 | TEST_F(SurfaceTextureGLToGLTest, DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) { |
| 1379 | enum { NUM_ITERATIONS = 64 }; |
| 1380 | |
| 1381 | class PT : public ProducerThread { |
| 1382 | virtual void render() { |
| 1383 | for (int i = 0; i < NUM_ITERATIONS; i++) { |
| 1384 | glClearColor(0.0f, 1.0f, 0.0f, 1.0f); |
| 1385 | glClear(GL_COLOR_BUFFER_BIT); |
| 1386 | LOGV("+swapBuffers"); |
| 1387 | swapBuffers(); |
| 1388 | LOGV("-swapBuffers"); |
| 1389 | } |
| 1390 | } |
| 1391 | }; |
| 1392 | |
| 1393 | ASSERT_EQ(OK, mST->setSynchronousMode(true)); |
| 1394 | ASSERT_EQ(OK, mST->setBufferCountServer(2)); |
| 1395 | |
| 1396 | runProducerThread(new PT()); |
| 1397 | |
| 1398 | // Allow three frames to be rendered and queued before starting the |
| 1399 | // rendering in this thread. For the latter two frames we don't call |
| 1400 | // updateTexImage so the next dequeue from the producer thread will block |
| 1401 | // waiting for a frame to become available. |
| 1402 | mFC->waitForFrame(); |
| 1403 | mFC->finishFrame(); |
| 1404 | |
| 1405 | // We must call updateTexImage to consume the first frame so that the |
| 1406 | // SurfaceTexture is able to reduce the buffer count to 2. This is because |
| 1407 | // the GL driver may dequeue a buffer when the EGLSurface is created, and |
| 1408 | // that happens before we call setBufferCountServer. It's possible that the |
| 1409 | // driver does not dequeue a buffer at EGLSurface creation time, so we |
| 1410 | // cannot rely on this to cause the second dequeueBuffer call to block. |
| 1411 | mST->updateTexImage(); |
| 1412 | |
| 1413 | mFC->waitForFrame(); |
| 1414 | mFC->finishFrame(); |
| 1415 | mFC->waitForFrame(); |
| 1416 | mFC->finishFrame(); |
| 1417 | |
| 1418 | // Sleep for 100ms to allow the producer thread's dequeueBuffer call to |
| 1419 | // block waiting for a buffer to become available. |
| 1420 | usleep(100000); |
| 1421 | |
| 1422 | // Render and present a number of images. This thread should not be blocked |
| 1423 | // by the fact that the producer thread is blocking in dequeue. |
| 1424 | for (int i = 0; i < NUM_ITERATIONS; i++) { |
| 1425 | glClear(GL_COLOR_BUFFER_BIT); |
| 1426 | eglSwapBuffers(mEglDisplay, mEglSurface); |
| 1427 | } |
| 1428 | |
| 1429 | // Consume the two pending buffers to unblock the producer thread. |
| 1430 | mST->updateTexImage(); |
| 1431 | mST->updateTexImage(); |
| 1432 | |
| 1433 | // Consume the remaining buffers from the producer thread. |
| 1434 | for (int i = 0; i < NUM_ITERATIONS-3; i++) { |
| 1435 | mFC->waitForFrame(); |
| 1436 | mFC->finishFrame(); |
| 1437 | LOGV("+updateTexImage"); |
| 1438 | mST->updateTexImage(); |
| 1439 | LOGV("-updateTexImage"); |
| 1440 | } |
| 1441 | } |
| 1442 | |
Jamie Gennis | 5451d15 | 2011-06-08 09:40:45 -0700 | [diff] [blame] | 1443 | } // namespace android |