blob: fd18ad23c64704f01f5b9625a71fe848e6e085a0 [file] [log] [blame]
Jamie Gennisd99c0882011-03-10 16:24:46 -08001/*
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 Gennis2640bfd2011-07-14 17:11:47 -070017#define LOG_TAG "SurfaceTexture_test"
Jamie Gennis5451d152011-06-08 09:40:45 -070018//#define LOG_NDEBUG 0
19
Dan Stozaf3730fb2013-11-26 15:10:10 -080020#include "GLTest.h"
21
Andy McFadden2adaf042012-12-18 09:49:45 -080022#include <gui/GLConsumer.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080023#include <ui/GraphicBuffer.h>
24#include <utils/String8.h>
Jamie Gennis5451d152011-06-08 09:40:45 -070025#include <utils/threads.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080026
Mathias Agopian90ac7992012-02-25 18:48:35 -080027#include <gui/ISurfaceComposer.h>
28#include <gui/Surface.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080029
Jamie Gennisd99c0882011-03-10 16:24:46 -080030#include <EGL/eglext.h>
Mathias Agopianf31510a2013-04-16 23:32:38 -070031#include <GLES/glext.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080032#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
34
35#include <ui/FramebufferNativeWindow.h>
Kenny Rootc6688712013-09-11 23:25:08 -070036#include <UniquePtr.h>
Mathias Agopianf31510a2013-04-16 23:32:38 -070037#include <android/native_window.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080038
39namespace android {
40
Mathias Agopianf31510a2013-04-16 23:32:38 -070041class MultiTextureConsumerTest : public GLTest {
42protected:
43 enum { TEX_ID = 123 };
44
45 virtual void SetUp() {
46 GLTest::SetUp();
Mathias Agopian8f938a52013-07-12 22:06:26 -070047 sp<BufferQueue> bq = new BufferQueue();
48 mGlConsumer = new GLConsumer(bq, TEX_ID);
Mathias Agopiandb89edc2013-08-02 01:40:18 -070049 mSurface = new Surface(bq);
Mathias Agopianf31510a2013-04-16 23:32:38 -070050 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
76TEST_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 Gennisea2d9422013-04-23 15:00:45 -0700126 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
Mathias Agopianf31510a2013-04-16 23:32:38 -0700127 }
Mathias Agopianf31510a2013-04-16 23:32:38 -0700128
129 for (int i=0 ; i<8 ; i++) {
Jamie Gennisea2d9422013-04-23 15:00:45 -0700130 EXPECT_TRUE(checkPixel(i*16 + 8, 8, i*16, i*16, i*16, i*16, 0));
Mathias Agopianf31510a2013-04-16 23:32:38 -0700131 }
Mathias Agopianf31510a2013-04-16 23:32:38 -0700132}
133
134
135
Jamie Gennisd99c0882011-03-10 16:24:46 -0800136class SurfaceTextureGLTest : public GLTest {
137protected:
Jamie Gennis79e31252011-10-19 15:19:19 -0700138 enum { TEX_ID = 123 };
Jamie Gennisd99c0882011-03-10 16:24:46 -0800139
140 virtual void SetUp() {
141 GLTest::SetUp();
Mathias Agopian8f938a52013-07-12 22:06:26 -0700142 sp<BufferQueue> bq = new BufferQueue();
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700143 mBQ = bq;
Mathias Agopian8f938a52013-07-12 22:06:26 -0700144 mST = new GLConsumer(bq, TEX_ID);
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700145 mSTC = new Surface(bq);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800146 mANW = mSTC;
Jamie Gennis74bed552012-03-28 19:05:54 -0700147 mTextureRenderer = new TextureRenderer(TEX_ID, mST);
148 ASSERT_NO_FATAL_FAILURE(mTextureRenderer->SetUp());
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700149 mFW = new FrameWaiter;
150 mST->setFrameAvailableListener(mFW);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800151 }
152
Jamie Gennis2640bfd2011-07-14 17:11:47 -0700153 virtual void TearDown() {
Dan Stozaf3730fb2013-11-26 15:10:10 -0800154 mTextureRenderer.clear();
Jamie Gennis2640bfd2011-07-14 17:11:47 -0700155 mANW.clear();
156 mSTC.clear();
157 mST.clear();
158 GLTest::TearDown();
159 }
160
Jamie Gennisd99c0882011-03-10 16:24:46 -0800161 void drawTexture() {
Jamie Gennis74bed552012-03-28 19:05:54 -0700162 mTextureRenderer->drawTexture();
Jamie Gennisd99c0882011-03-10 16:24:46 -0800163 }
164
Jamie Gennis74bed552012-03-28 19:05:54 -0700165 class TextureRenderer: public RefBase {
166 public:
Andy McFadden2adaf042012-12-18 09:49:45 -0800167 TextureRenderer(GLuint texName, const sp<GLConsumer>& st):
Jamie Gennis74bed552012-03-28 19:05:54 -0700168 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 McFadden2adaf042012-12-18 09:49:45 -0800208 // drawTexture draws the GLConsumer over the entire GL viewport.
Jamie Gennis74bed552012-03-28 19:05:54 -0700209 void drawTexture() {
Jamie Gennisa96b6bd2012-04-11 17:27:12 -0700210 static const GLfloat triangleVertices[] = {
Jamie Gennis74bed552012-03-28 19:05:54 -0700211 -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 McFadden2adaf042012-12-18 09:49:45 -0800255 sp<GLConsumer> mST;
Jamie Gennis74bed552012-03-28 19:05:54 -0700256 GLuint mPgm;
257 GLint mPositionHandle;
258 GLint mTexSamplerHandle;
259 GLint mTexMatrixHandle;
260 };
261
Andy McFadden2adaf042012-12-18 09:49:45 -0800262 class FrameWaiter : public GLConsumer::FrameAvailableListener {
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700263 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 McFadden2adaf042012-12-18 09:49:45 -0800287 // Note that GLConsumer will lose the notifications
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700288 // 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 Agopiana4e19522013-07-31 20:09:53 -0700292 class DisconnectWaiter : public BnConsumerListener {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700293 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 Agopiandb89edc2013-08-02 01:40:18 -0700336 sp<BufferQueue> mBQ;
Andy McFadden2adaf042012-12-18 09:49:45 -0800337 sp<GLConsumer> mST;
Mathias Agopiane3c697f2013-02-14 17:11:02 -0800338 sp<Surface> mSTC;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800339 sp<ANativeWindow> mANW;
Jamie Gennis74bed552012-03-28 19:05:54 -0700340 sp<TextureRenderer> mTextureRenderer;
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700341 sp<FrameWaiter> mFW;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800342};
343
344// Fill a YV12 buffer with a multi-colored checkerboard pattern
345void 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.
375void 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 Gennis1876d132011-03-17 16:32:52 -0700399void 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 Stozaf3730fb2013-11-26 15:10:10 -0800413void fillRGBA8BufferSolid(uint8_t* buf, int w, int h, int stride,
414 uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
Jamie Gennisfe27e2f2011-11-11 18:05:11 -0800415 const size_t PIXEL_SIZE = 4;
416 for (int y = 0; y < h; y++) {
Dan Stozaf3730fb2013-11-26 15:10:10 -0800417 for (int x = 0; x < w; x++) {
Jamie Gennisfe27e2f2011-11-11 18:05:11 -0800418 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 Gennisce561372012-03-19 18:33:05 -0700427// 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().
432void produceOneRGBA8Frame(const sp<ANativeWindow>& anw) {
433 android_native_buffer_t* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700434 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(anw.get(),
435 &anb));
Jamie Gennisce561372012-03-19 18:33:05 -0700436 ASSERT_TRUE(anb != NULL);
437
438 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisce561372012-03-19 18:33:05 -0700439
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 Gennisd8e812c2012-06-13 16:32:25 -0700445 ASSERT_EQ(NO_ERROR, anw->queueBuffer(anw.get(), buf->getNativeBuffer(),
446 -1));
Jamie Gennisce561372012-03-19 18:33:05 -0700447}
448
Jamie Gennisd99c0882011-03-10 16:24:46 -0800449TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700450 const int texWidth = 64;
451 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800452
453 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700454 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800455 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
456 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
457
Iliyan Malchev697526b2011-05-01 11:33:26 -0700458 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700459 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
460 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800461 ASSERT_TRUE(anb != NULL);
462
463 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800464
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 Gennis1876d132011-03-17 16:32:52 -0700468 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800469 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700470 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
471 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800472
Jamie Gennisd69097f2012-08-30 13:28:23 -0700473 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800474
475 glClearColor(0.2, 0.2, 0.2, 0.2);
476 glClear(GL_COLOR_BUFFER_BIT);
477
Jamie Gennisc8c51522011-06-15 14:24:38 -0700478 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800479 drawTexture();
480
Jamie Gennise6a0f502013-04-05 17:37:32 -0700481 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 Gennisd99c0882011-03-10 16:24:46 -0800485
Jamie Gennise6a0f502013-04-05 17:37:32 -0700486 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 Gennisd99c0882011-03-10 16:24:46 -0800493}
494
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700495TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700496 const int texWidth = 64;
497 const int texHeight = 64;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800498
499 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700500 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800501 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
502 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
503
Iliyan Malchev697526b2011-05-01 11:33:26 -0700504 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700505 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
506 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800507 ASSERT_TRUE(anb != NULL);
508
509 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800510
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 Gennis1876d132011-03-17 16:32:52 -0700514 fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800515 buf->unlock();
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700516 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
517 -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800518
Jamie Gennisd69097f2012-08-30 13:28:23 -0700519 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800520
521 glClearColor(0.2, 0.2, 0.2, 0.2);
522 glClear(GL_COLOR_BUFFER_BIT);
523
Jamie Gennisc8c51522011-06-15 14:24:38 -0700524 glViewport(0, 0, texWidth, texHeight);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800525 drawTexture();
526
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700527 EXPECT_TRUE(checkPixel( 0, 0, 0, 133, 0, 255));
528 EXPECT_TRUE(checkPixel(63, 0, 255, 127, 255, 255));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800529 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
530 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
531
Jamie Gennisd05bb2e2011-06-14 15:41:45 -0700532 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 Gennisd99c0882011-03-10 16:24:46 -0800539}
540
541TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700542 const int texWidth = 64;
543 const int texHeight = 66;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800544
545 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
Jamie Gennis1876d132011-03-17 16:32:52 -0700546 texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800547 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 Gennis1876d132011-03-17 16:32:52 -0700554 {4, 6, texWidth, 36},
555 {4, 6, 22, texHeight},
Jamie Gennisd99c0882011-03-10 16:24:46 -0800556 };
557
558 for (int i = 0; i < 5; i++) {
559 const android_native_rect_t& crop(crops[i]);
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800560 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
561 crop.left, crop.top, crop.right, crop.bottom).string());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800562
563 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
564
Iliyan Malchev697526b2011-05-01 11:33:26 -0700565 ANativeWindowBuffer* anb;
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700566 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
567 &anb));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800568 ASSERT_TRUE(anb != NULL);
569
570 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800571
572 uint8_t* img = NULL;
573 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
Jamie Gennis1876d132011-03-17 16:32:52 -0700574 fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800575 buf->unlock();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -0800576 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700577 buf->getNativeBuffer(), -1));
Jamie Gennisd99c0882011-03-10 16:24:46 -0800578
Jamie Gennisd69097f2012-08-30 13:28:23 -0700579 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisd99c0882011-03-10 16:24:46 -0800580
581 glClearColor(0.2, 0.2, 0.2, 0.2);
582 glClear(GL_COLOR_BUFFER_BIT);
583
Jamie Gennisc8c51522011-06-15 14:24:38 -0700584 glViewport(0, 0, 64, 64);
Jamie Gennisd99c0882011-03-10 16:24:46 -0800585 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 Gennisdfcff4b2011-06-17 11:39:18 -0700602// This test is intended to catch synchronization bugs between the CPU-written
603// and GPU-read buffers.
604TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
605 enum { texWidth = 16 };
606 enum { texHeight = 16 };
607 enum { numFrames = 1024 };
608
Jamie Gennis31a353d2012-08-24 17:25:13 -0700609 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2));
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700610 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 Gennis6f4cdfe2011-11-19 17:49:21 -0800628 ProducerThread(const sp<ANativeWindow>& anw,
629 const TestPixel* testPixels):
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700630 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 Gennisd8e812c2012-06-13 16:32:25 -0700640 if (native_window_dequeue_buffer_and_wait(mANW.get(),
641 &anb) != NO_ERROR) {
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700642 return false;
643 }
644 if (anb == NULL) {
645 return false;
646 }
647
648 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700649
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 Gennisd8e812c2012-06-13 16:32:25 -0700692 if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), -1)
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700693 != NO_ERROR) {
694 return false;
695 }
696 }
697 return false;
698 }
699
700 sp<ANativeWindow> mANW;
701 const TestPixel* mTestPixels;
702 };
703
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700704 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 Gennisefc7ab62012-04-17 19:36:18 -0700714 mFW->waitForFrame();
715 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700716
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 Gennisefc7ab62012-04-17 19:36:18 -0700725 mFW->waitForFrame();
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700726 }
Jamie Gennisd69097f2012-08-30 13:28:23 -0700727 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisdfcff4b2011-06-17 11:39:18 -0700728 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 Gennis1f8e09f2011-07-19 17:58:43 -0700747TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700748 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 Gennisce561372012-03-19 18:33:05 -0700756 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -0700757
Jamie Gennisd69097f2012-08-30 13:28:23 -0700758 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis1876d132011-03-17 16:32:52 -0700759
760 glClearColor(0.2, 0.2, 0.2, 0.2);
761 glClear(GL_COLOR_BUFFER_BIT);
762
Jamie Gennisc8c51522011-06-15 14:24:38 -0700763 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -0700764 drawTexture();
765
766 EXPECT_TRUE(checkPixel( 0, 0, 35, 35, 35, 35));
767 EXPECT_TRUE(checkPixel(63, 0, 231, 231, 231, 231));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700768 EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
769 EXPECT_TRUE(checkPixel( 0, 65, 35, 35, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700770
771 EXPECT_TRUE(checkPixel(15, 10, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700772 EXPECT_TRUE(checkPixel(23, 65, 231, 35, 231, 35));
Jamie Gennisc8c51522011-06-15 14:24:38 -0700773 EXPECT_TRUE(checkPixel(19, 40, 35, 231, 35, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700774 EXPECT_TRUE(checkPixel(38, 30, 231, 35, 35, 35));
775 EXPECT_TRUE(checkPixel(42, 54, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700776 EXPECT_TRUE(checkPixel(37, 34, 35, 231, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -0700777 EXPECT_TRUE(checkPixel(31, 8, 231, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700778 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 Gennis1876d132011-03-17 16:32:52 -0700781 EXPECT_TRUE(checkPixel(54, 50, 35, 231, 231, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700782 EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
783 EXPECT_TRUE(checkPixel(10, 6, 35, 35, 231, 231));
Jamie Gennis1876d132011-03-17 16:32:52 -0700784 EXPECT_TRUE(checkPixel(29, 4, 35, 35, 35, 231));
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700785 EXPECT_TRUE(checkPixel(55, 28, 35, 35, 231, 35));
Jamie Gennis1876d132011-03-17 16:32:52 -0700786 EXPECT_TRUE(checkPixel(58, 55, 35, 35, 231, 231));
787}
788
Jamie Gennis1f8e09f2011-07-19 17:58:43 -0700789TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
Jamie Gennis1876d132011-03-17 16:32:52 -0700790 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 Gennisce561372012-03-19 18:33:05 -0700798 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
Jamie Gennis1876d132011-03-17 16:32:52 -0700799
Jamie Gennisd69097f2012-08-30 13:28:23 -0700800 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis1876d132011-03-17 16:32:52 -0700801
802 glClearColor(0.2, 0.2, 0.2, 0.2);
803 glClear(GL_COLOR_BUFFER_BIT);
804
Jamie Gennisc8c51522011-06-15 14:24:38 -0700805 glViewport(0, 0, texWidth, texHeight);
Jamie Gennis1876d132011-03-17 16:32:52 -0700806 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 McFadden2adaf042012-12-18 09:49:45 -0800831// Tests if GLConsumer and BufferQueue are robust enough
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700832// 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.
835TEST_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 Gennisd8e812c2012-06-13 16:32:25 -0700853 if (native_window_dequeue_buffer_and_wait(mANW.get(),
854 &anb) != NO_ERROR) {
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700855 return false;
856 }
857 if (anb == NULL) {
858 return false;
859 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700860 if (mANW->queueBuffer(mANW.get(), anb, -1)
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700861 != 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 Lam9abe1eb2012-03-26 20:37:15 -0700875 sp<DisconnectWaiter> dw(new DisconnectWaiter());
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700876 mBQ->consumerConnect(dw, false);
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700877
878
879 sp<Thread> pt(new ProducerThread(mANW));
880 pt->run();
881
Andy McFadden2adaf042012-12-18 09:49:45 -0800882 // eat a frame so GLConsumer will own an at least one slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700883 dw->waitForFrame();
884 EXPECT_EQ(OK,mST->updateTexImage());
885
886 dw->waitForFrame();
Andy McFadden2adaf042012-12-18 09:49:45 -0800887 // Could fail here as GLConsumer thinks it still owns the slot
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700888 // but bufferQueue has released all slots
889 EXPECT_EQ(OK,mST->updateTexImage());
890
891 dw->finishDisconnect();
892}
893
894
Andy McFadden2adaf042012-12-18 09:49:45 -0800895// This test ensures that the GLConsumer clears the mCurrentTexture
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700896// when it is disconnected and reconnected. Otherwise it will
897// attempt to release a buffer that it does not owned
898TEST_F(SurfaceTextureGLTest, DisconnectClearsCurrentTexture) {
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700899 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
900 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700901
902 ANativeWindowBuffer *anb;
903
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700904 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
905 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700906
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700907 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
908 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700909
910 EXPECT_EQ(OK,mST->updateTexImage());
911 EXPECT_EQ(OK,mST->updateTexImage());
912
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700913 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 Lam9abe1eb2012-03-26 20:37:15 -0700917
Jamie Gennisd8e812c2012-06-13 16:32:25 -0700918 EXPECT_EQ(OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
919 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700920
921 // Will fail here if mCurrentTexture is not cleared properly
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700922 mFW->waitForFrame();
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700923 EXPECT_EQ(OK,mST->updateTexImage());
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700924
925 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
926 NATIVE_WINDOW_API_EGL));
Daniel Lam9abe1eb2012-03-26 20:37:15 -0700927}
928
Daniel Lam016c8cb2012-04-03 15:54:58 -0700929TEST_F(SurfaceTextureGLTest, ScaleToWindowMode) {
Daniel Lam016c8cb2012-04-03 15:54:58 -0700930 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 Gennisefc7ab62012-04-17 19:36:18 -0700939 ASSERT_EQ(OK, native_window_api_connect(mANW.get(),
940 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -0700941
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 Gennisd8e812c2012-06-13 16:32:25 -0700946 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
947 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700948 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -0700949 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700950 Rect r = mST->getCurrentCrop();
951 assertRectEq(Rect(23, 78, 123, 477), r);
952
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700953 ASSERT_EQ(OK, native_window_api_disconnect(mANW.get(),
954 NATIVE_WINDOW_API_CPU));
Daniel Lam016c8cb2012-04-03 15:54:58 -0700955}
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
961TEST_F(SurfaceTextureGLTest, CroppedScalingMode) {
Daniel Lam016c8cb2012-04-03 15:54:58 -0700962 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 Gennisd8e812c2012-06-13 16:32:25 -0700978 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
979 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700980 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -0700981 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700982 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 Gennisd8e812c2012-06-13 16:32:25 -0700989 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
990 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -0700991 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -0700992 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -0700993 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 Gennisd8e812c2012-06-13 16:32:25 -07001001 EXPECT_EQ (OK, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
1002 EXPECT_EQ(OK, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001003 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001004 EXPECT_EQ(OK, mST->updateTexImage());
Daniel Lam016c8cb2012-04-03 15:54:58 -07001005 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 Gennis7b305ff2011-07-19 12:08:33 -07001013TEST_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 Gennisd8e812c2012-06-13 16:32:25 -07001029 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1030 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001031 return false;
1032 }
1033 if (anb == NULL) {
1034 return false;
1035 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001036 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001037 != NO_ERROR) {
1038 return false;
1039 }
1040
1041 // Frame 2
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001042 if (native_window_dequeue_buffer_and_wait(mANW.get(),
1043 &anb) != NO_ERROR) {
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001044 return false;
1045 }
1046 if (anb == NULL) {
1047 return false;
1048 }
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001049 if (mANW->queueBuffer(mANW.get(), anb, -1)
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001050 != NO_ERROR) {
1051 return false;
1052 }
1053
1054 // Frame 3 - error expected
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001055 mDequeueError = native_window_dequeue_buffer_and_wait(mANW.get(),
1056 &anb);
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001057 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 Gennis31a353d2012-08-24 17:25:13 -07001071 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001072
1073 sp<Thread> pt(new ProducerThread(mANW));
1074 pt->run();
1075
Jamie Gennisefc7ab62012-04-17 19:36:18 -07001076 mFW->waitForFrame();
1077 mFW->waitForFrame();
Jamie Gennis7b305ff2011-07-19 12:08:33 -07001078
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 Gennis6f4cdfe2011-11-19 17:49:21 -08001090TEST_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 Gennisd8e812c2012-06-13 16:32:25 -07001099 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1100 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001101 EXPECT_EQ(16, anb->width);
1102 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001103 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001104 EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1105
1106 // make sure it works with GL_MAX_TEXTURE_SIZE
1107 mST->setDefaultBufferSize(maxTextureSize, texHeight);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001108 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1109 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001110 EXPECT_EQ(maxTextureSize, anb->width);
1111 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001112 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001113 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 Gennisd8e812c2012-06-13 16:32:25 -07001117 EXPECT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
1118 &anb));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001119 EXPECT_EQ(maxTextureSize+1, anb->width);
1120 EXPECT_EQ(texHeight, anb->height);
Jamie Gennisd8e812c2012-06-13 16:32:25 -07001121 EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb, -1));
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001122 ASSERT_NE(NO_ERROR, mST->updateTexImage());
1123}
1124
Jamie Gennis5451d152011-06-08 09:40:45 -07001125/*
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001126 * 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 */
1129class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1130protected:
1131 SurfaceTextureGLToGLTest():
1132 mProducerEglSurface(EGL_NO_SURFACE),
1133 mProducerEglContext(EGL_NO_CONTEXT) {
1134 }
1135
1136 virtual void SetUp() {
1137 SurfaceTextureGLTest::SetUp();
1138
Jamie Gennisce561372012-03-19 18:33:05 -07001139 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001140 mANW.get(), NULL);
1141 ASSERT_EQ(EGL_SUCCESS, eglGetError());
1142 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1143
Jamie Gennisce561372012-03-19 18:33:05 -07001144 mProducerEglContext = eglCreateContext(mEglDisplay, mGlConfig,
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001145 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 McFadden71f683a2012-09-14 17:21:46 -07001164TEST_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 Gennis6f4cdfe2011-11-19 17:49:21 -08001217TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1218 const int texWidth = 64;
1219 const int texHeight = 64;
1220
1221 mST->setDefaultBufferSize(texWidth, texHeight);
1222
Jamie Gennis46975282012-08-30 18:35:50 -07001223 // This test requires 3 buffers to complete run on a single thread.
1224 mST->setDefaultMaxBufferCount(3);
1225
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001226 // 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 Gennisd69097f2012-08-30 13:28:23 -07001259 // Skip the first frame, which was empty
1260 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1261 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001262
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
1292TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001293 sp<GraphicBuffer> buffers[2];
1294
Jamie Gennise3603d72011-11-19 21:20:17 -08001295 // 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 Gennisfa5b40e2012-03-15 14:01:24 -07001302 for (int i = 0; i < 2; i++) {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001303 // 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 Gennisefc7ab62012-04-17 19:36:18 -07001314 mFW->waitForFrame();
Jamie Gennisd69097f2012-08-30 13:28:23 -07001315 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001316 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 Gennisfa5b40e2012-03-15 14:01:24 -07001326 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001327
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001328 // This test should have the only reference to buffer 0.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001329 EXPECT_EQ(1, buffers[0]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001330
Andy McFadden2adaf042012-12-18 09:49:45 -08001331 // The GLConsumer should hold a single reference to buffer 1 in its
Jamie Gennisfa5b40e2012-03-15 14:01:24 -07001332 // mCurrentBuffer member. All of the references in the slots should have
1333 // been released.
1334 EXPECT_EQ(2, buffers[1]->getStrongCount());
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001335}
1336
1337TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1338 sp<GraphicBuffer> buffers[3];
1339
Jamie Gennise3603d72011-11-19 21:20:17 -08001340 // 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 Gennis6f4cdfe2011-11-19 17:49:21 -08001347 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 Gennisefc7ab62012-04-17 19:36:18 -07001360 mFW->waitForFrame();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001361 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1362 buffers[i] = mST->getCurrentBuffer();
1363 }
1364
Andy McFadden2adaf042012-12-18 09:49:45 -08001365 // Abandon the GLConsumer, releasing the ref that the GLConsumer has
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001366 // 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 Gennisfa5b40e2012-03-15 14:01:24 -07001376 mProducerEglSurface = EGL_NO_SURFACE;
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001377
1378 EXPECT_EQ(1, buffers[0]->getStrongCount());
1379 EXPECT_EQ(1, buffers[1]->getStrongCount());
Jamie Gennise3603d72011-11-19 21:20:17 -08001380
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 Gennis6f4cdfe2011-11-19 17:49:21 -08001387}
1388
Mathias Agopian7589b2a2013-03-08 13:18:52 -08001389TEST_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
1426TEST_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 Gennisc2c38022012-04-11 17:20:03 -07001463TEST_F(SurfaceTextureGLToGLTest, TexturingFromUserSizedGLFilledBuffer) {
1464 enum { texWidth = 64 };
1465 enum { texHeight = 64 };
1466
Jamie Gennis46975282012-08-30 18:35:50 -07001467 // This test requires 3 buffers to complete run on a single thread.
1468 mST->setDefaultMaxBufferCount(3);
1469
Jamie Gennisc2c38022012-04-11 17:20:03 -07001470 // 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 Gennisd69097f2012-08-30 13:28:23 -07001498 // Skip the first frame, which was empty
1499 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1500 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001501
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
1520TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedUserSizedGLFilledBuffer) {
1521 enum { texWidth = 64 };
1522 enum { texHeight = 16 };
1523
Jamie Gennis46975282012-08-30 18:35:50 -07001524 // This test requires 3 buffers to complete run on a single thread.
1525 mST->setDefaultMaxBufferCount(3);
1526
Jamie Gennisc2c38022012-04-11 17:20:03 -07001527 // 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 Gennisd69097f2012-08-30 13:28:23 -07001559 // Skip the first frame, which was empty
1560 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1561 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001562
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
1581TEST_F(SurfaceTextureGLToGLTest, TexturingFromPreRotatedGLFilledBuffer) {
1582 enum { texWidth = 64 };
1583 enum { texHeight = 16 };
1584
Jamie Gennis46975282012-08-30 18:35:50 -07001585 // This test requires 3 buffers to complete run on a single thread.
1586 mST->setDefaultMaxBufferCount(3);
1587
Jamie Gennisc2c38022012-04-11 17:20:03 -07001588 // 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 Gennisd69097f2012-08-30 13:28:23 -07001620 // Skip the first frame, which was empty
1621 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1622 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennisc2c38022012-04-11 17:20:03 -07001623
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 Gennis6f4cdfe2011-11-19 17:49:21 -08001642/*
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 McFadden2adaf042012-12-18 09:49:45 -08001646 * GLConsumer. Additionally it supports interlocking the producer and
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001647 * consumer threads so that a specific sequence of calls can be
1648 * deterministically created by the test.
Jamie Gennis5451d152011-06-08 09:40:45 -07001649 *
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 Gennis6f4cdfe2011-11-19 17:49:21 -08001671class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
Jamie Gennis5451d152011-06-08 09:40:45 -07001672protected:
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 McFadden2adaf042012-12-18 09:49:45 -08001713 // the FrameCondition as the FrameAvailableListener of the GLConsumer,
Jamie Gennis5451d152011-06-08 09:40:45 -07001714 // 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 McFadden2adaf042012-12-18 09:49:45 -08001718 // synchronously from GLConsumer::queueBuffer.
1719 class FrameCondition : public GLConsumer::FrameAvailableListener {
Jamie Gennis5451d152011-06-08 09:40:45 -07001720 public:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001721 FrameCondition():
1722 mFrameAvailable(false),
1723 mFrameFinished(false) {
1724 }
1725
Jamie Gennis5451d152011-06-08 09:40:45 -07001726 // 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 Gennis5451d152011-06-08 09:40:45 -07001730 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001731 ALOGV("+waitForFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001732 while (!mFrameAvailable) {
1733 mFrameAvailableCondition.wait(mMutex);
1734 }
1735 mFrameAvailable = false;
Steve Block6807e592011-10-20 11:56:00 +01001736 ALOGV("-waitForFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001737 }
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 Gennis5451d152011-06-08 09:40:45 -07001743 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001744 ALOGV("+finishFrame");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001745 mFrameFinished = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001746 mFrameFinishCondition.signal();
Steve Block6807e592011-10-20 11:56:00 +01001747 ALOGV("-finishFrame");
Jamie Gennis5451d152011-06-08 09:40:45 -07001748 }
1749
Andy McFadden2adaf042012-12-18 09:49:45 -08001750 // This should be called by GLConsumer on the producer thread.
Jamie Gennis5451d152011-06-08 09:40:45 -07001751 virtual void onFrameAvailable() {
Jamie Gennis5451d152011-06-08 09:40:45 -07001752 Mutex::Autolock lock(mMutex);
Steve Block6807e592011-10-20 11:56:00 +01001753 ALOGV("+onFrameAvailable");
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001754 mFrameAvailable = true;
Jamie Gennis5451d152011-06-08 09:40:45 -07001755 mFrameAvailableCondition.signal();
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001756 while (!mFrameFinished) {
1757 mFrameFinishCondition.wait(mMutex);
1758 }
1759 mFrameFinished = false;
Steve Block6807e592011-10-20 11:56:00 +01001760 ALOGV("-onFrameAvailable");
Jamie Gennis5451d152011-06-08 09:40:45 -07001761 }
1762
1763 protected:
Jamie Gennis2640bfd2011-07-14 17:11:47 -07001764 bool mFrameAvailable;
1765 bool mFrameFinished;
1766
Jamie Gennis5451d152011-06-08 09:40:45 -07001767 Mutex mMutex;
1768 Condition mFrameAvailableCondition;
1769 Condition mFrameFinishCondition;
1770 };
1771
Jamie Gennis5451d152011-06-08 09:40:45 -07001772 virtual void SetUp() {
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001773 SurfaceTextureGLToGLTest::SetUp();
Jamie Gennis5451d152011-06-08 09:40:45 -07001774 mFC = new FrameCondition();
1775 mST->setFrameAvailableListener(mFC);
1776 }
1777
1778 virtual void TearDown() {
1779 if (mProducerThread != NULL) {
1780 mProducerThread->requestExitAndWait();
1781 }
Jamie Gennis5451d152011-06-08 09:40:45 -07001782 mProducerThread.clear();
1783 mFC.clear();
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001784 SurfaceTextureGLToGLTest::TearDown();
Jamie Gennis5451d152011-06-08 09:40:45 -07001785 }
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 Gennis5451d152011-06-08 09:40:45 -07001795 sp<ProducerThread> mProducerThread;
1796 sp<FrameCondition> mFC;
1797};
1798
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001799TEST_F(SurfaceTextureGLThreadToGLTest,
1800 UpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001801 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 Gennisd69097f2012-08-30 13:28:23 -07001812 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07001813 mFC->finishFrame();
1814
1815 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -08001816}
Jamie Gennis5451d152011-06-08 09:40:45 -07001817
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001818TEST_F(SurfaceTextureGLThreadToGLTest,
1819 UpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001820 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 Gennisd69097f2012-08-30 13:28:23 -07001832 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis5451d152011-06-08 09:40:45 -07001833
1834 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1835}
1836
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001837TEST_F(SurfaceTextureGLThreadToGLTest,
1838 RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001839 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 Block6807e592011-10-20 11:56:00 +01001846 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001847 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001848 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001849 }
1850 }
1851 };
1852
1853 runProducerThread(new PT());
1854
1855 for (int i = 0; i < NUM_ITERATIONS; i++) {
1856 mFC->waitForFrame();
Steve Block6807e592011-10-20 11:56:00 +01001857 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07001858 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01001859 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001860 mFC->finishFrame();
1861
1862 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1863 }
1864}
1865
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001866TEST_F(SurfaceTextureGLThreadToGLTest,
1867 RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
Jamie Gennis5451d152011-06-08 09:40:45 -07001868 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 Block6807e592011-10-20 11:56:00 +01001875 ALOGV("+swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001876 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001877 ALOGV("-swapBuffers");
Jamie Gennis5451d152011-06-08 09:40:45 -07001878 }
1879 }
1880 };
1881
1882 runProducerThread(new PT());
1883
1884 for (int i = 0; i < NUM_ITERATIONS; i++) {
1885 mFC->waitForFrame();
1886 mFC->finishFrame();
Steve Block6807e592011-10-20 11:56:00 +01001887 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07001888 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01001889 ALOGV("-updateTexImage");
Jamie Gennis5451d152011-06-08 09:40:45 -07001890
1891 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1892 }
1893}
1894
Jamie Gennis6e502192011-07-21 14:31:31 -07001895// XXX: This test is disabled because it is currently hanging on some devices.
Jamie Gennis6f4cdfe2011-11-19 17:49:21 -08001896TEST_F(SurfaceTextureGLThreadToGLTest,
1897 DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
Jamie Gennis6e502192011-07-21 14:31:31 -07001898 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 Block6807e592011-10-20 11:56:00 +01001905 ALOGV("+swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07001906 swapBuffers();
Steve Block6807e592011-10-20 11:56:00 +01001907 ALOGV("-swapBuffers");
Jamie Gennis6e502192011-07-21 14:31:31 -07001908 }
1909 }
1910 };
1911
Jamie Gennis31a353d2012-08-24 17:25:13 -07001912 ASSERT_EQ(OK, mST->setDefaultMaxBufferCount(2));
Jamie Gennis6e502192011-07-21 14:31:31 -07001913
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 Gennis31a353d2012-08-24 17:25:13 -07001926 // that happens before we call setDefaultMaxBufferCount. It's possible that the
Jamie Gennis6e502192011-07-21 14:31:31 -07001927 // 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 Gennisd69097f2012-08-30 13:28:23 -07001929 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07001930
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 Gennisd69097f2012-08-30 13:28:23 -07001948 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1949 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Jamie Gennis6e502192011-07-21 14:31:31 -07001950
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 Block6807e592011-10-20 11:56:00 +01001955 ALOGV("+updateTexImage");
Jamie Gennisd69097f2012-08-30 13:28:23 -07001956 ASSERT_EQ(NO_ERROR, mST->updateTexImage());
Steve Block6807e592011-10-20 11:56:00 +01001957 ALOGV("-updateTexImage");
Jamie Gennis6e502192011-07-21 14:31:31 -07001958 }
1959}
1960
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08001961class SurfaceTextureFBOTest : public SurfaceTextureGLTest {
1962protected:
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.
1997TEST_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 Gennisd8e812c2012-06-13 16:32:25 -07002007 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2008 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002009 ASSERT_TRUE(anb != NULL);
2010
2011 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002012
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 Gennisd8e812c2012-06-13 16:32:25 -07002019 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
2020 -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002021
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 Gennisd8e812c2012-06-13 16:32:25 -07002031 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(),
2032 &anb));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002033 ASSERT_TRUE(anb != NULL);
2034
2035 buf = new GraphicBuffer(anb, false);
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002036
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 Gennisd8e812c2012-06-13 16:32:25 -07002044 buf->getNativeBuffer(), -1));
Jamie Gennisfe27e2f2011-11-11 18:05:11 -08002045
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 Gennisce561372012-03-19 18:33:05 -07002058class SurfaceTextureMultiContextGLTest : public SurfaceTextureGLTest {
2059protected:
Jamie Gennis74bed552012-03-28 19:05:54 -07002060 enum { SECOND_TEX_ID = 123 };
2061 enum { THIRD_TEX_ID = 456 };
2062
Jamie Gennisce561372012-03-19 18:33:05 -07002063 SurfaceTextureMultiContextGLTest():
2064 mSecondEglContext(EGL_NO_CONTEXT) {
2065 }
2066
2067 virtual void SetUp() {
2068 SurfaceTextureGLTest::SetUp();
2069
Jamie Gennis74bed552012-03-28 19:05:54 -07002070 // Set up the secondary context and texture renderer.
Jamie Gennisce561372012-03-19 18:33:05 -07002071 mSecondEglContext = eglCreateContext(mEglDisplay, mGlConfig,
2072 EGL_NO_CONTEXT, getContextAttribs());
2073 ASSERT_EQ(EGL_SUCCESS, eglGetError());
2074 ASSERT_NE(EGL_NO_CONTEXT, mSecondEglContext);
Jamie Gennis74bed552012-03-28 19:05:54 -07002075
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 Gennisce561372012-03-19 18:33:05 -07002097 }
2098
2099 virtual void TearDown() {
Jamie Gennis74bed552012-03-28 19:05:54 -07002100 if (mThirdEglContext != EGL_NO_CONTEXT) {
2101 eglDestroyContext(mEglDisplay, mThirdEglContext);
2102 }
Jamie Gennisce561372012-03-19 18:33:05 -07002103 if (mSecondEglContext != EGL_NO_CONTEXT) {
2104 eglDestroyContext(mEglDisplay, mSecondEglContext);
2105 }
2106 SurfaceTextureGLTest::TearDown();
2107 }
2108
2109 EGLContext mSecondEglContext;
Jamie Gennis74bed552012-03-28 19:05:54 -07002110 sp<TextureRenderer> mSecondTextureRenderer;
2111
2112 EGLContext mThirdEglContext;
2113 sp<TextureRenderer> mThirdTextureRenderer;
Jamie Gennisce561372012-03-19 18:33:05 -07002114};
2115
2116TEST_F(SurfaceTextureMultiContextGLTest, UpdateFromMultipleContextsFails) {
Jamie Gennisce561372012-03-19 18:33:05 -07002117 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2118
2119 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002120 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002121 ASSERT_EQ(OK, mST->updateTexImage());
Jamie Gennisce561372012-03-19 18:33:05 -07002122
2123 // Attempt to latch the texture on the secondary context.
Jamie Gennis74bed552012-03-28 19:05:54 -07002124 ASSERT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
Jamie Gennisce561372012-03-19 18:33:05 -07002125 mSecondEglContext));
2126 ASSERT_EQ(EGL_SUCCESS, eglGetError());
Jamie Gennis74bed552012-03-28 19:05:54 -07002127 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2128}
2129
2130TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002131 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2132
2133 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002134 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002135 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
2144TEST_F(SurfaceTextureMultiContextGLTest,
2145 DetachFromContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002146 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2147
2148 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002149 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002150 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
2160TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002161 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2162
2163 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002164 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002165 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
2172TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002173 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2174
2175 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002176 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002177 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
2186TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002187 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2188
2189 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002190 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002191 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
2202TEST_F(SurfaceTextureMultiContextGLTest, DetachFromContextFailsWithNoContext) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002203 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2204
2205 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002206 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002207 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
2218TEST_F(SurfaceTextureMultiContextGLTest, UpdateTexImageFailsWhenDetached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002219 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 Gennisefc7ab62012-04-17 19:36:18 -07002225 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002226 ASSERT_EQ(INVALID_OPERATION, mST->updateTexImage());
2227}
2228
2229TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceeds) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002230 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2231
2232 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002233 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002234 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
2258TEST_F(SurfaceTextureMultiContextGLTest,
2259 AttachToContextSucceedsAfterProducerDisconnect) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002260 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2261
2262 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002263 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002264 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
2289TEST_F(SurfaceTextureMultiContextGLTest,
2290 AttachToContextSucceedsBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002291 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 Gennisefc7ab62012-04-17 19:36:18 -07002308 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002309 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
2320TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAbandoned) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002321 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2322
2323 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002324 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002325 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
2337TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWhenAttached) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002338 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2339
2340 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002341 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002342 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
2348TEST_F(SurfaceTextureMultiContextGLTest,
2349 AttachToContextFailsWhenAttachedBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002350 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
2356TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextFailsWithNoDisplay) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002357 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2358
2359 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002360 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002361 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
2375TEST_F(SurfaceTextureMultiContextGLTest, AttachToContextSucceedsTwice) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002376 ASSERT_NO_FATAL_FAILURE(produceOneRGBA8Frame(mANW));
2377
2378 // Latch the texture contents on the primary context.
Jamie Gennisefc7ab62012-04-17 19:36:18 -07002379 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002380 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
2412TEST_F(SurfaceTextureMultiContextGLTest,
2413 AttachToContextSucceedsTwiceBeforeUpdateTexImage) {
Jamie Gennis74bed552012-03-28 19:05:54 -07002414 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 Gennisefc7ab62012-04-17 19:36:18 -07002438 mFW->waitForFrame();
Jamie Gennis74bed552012-03-28 19:05:54 -07002439 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 Gennisce561372012-03-19 18:33:05 -07002448}
2449
Jesse Hall90ed8502012-05-16 23:44:34 -07002450TEST_F(SurfaceTextureMultiContextGLTest,
2451 UpdateTexImageSucceedsForBufferConsumedBeforeDetach) {
Jamie Gennis31a353d2012-08-24 17:25:13 -07002452 ASSERT_EQ(NO_ERROR, mST->setDefaultMaxBufferCount(2));
Jesse Hall90ed8502012-05-16 23:44:34 -07002453
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 Gennis5451d152011-06-08 09:40:45 -07002477} // namespace android