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