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