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