blob: 16280d2e01c1f4067313b72ab67ddb00cc17cf4a [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 Gennis5451d152011-06-08 09:40:45 -070017//#define LOG_NDEBUG 0
18
Jamie Gennisd99c0882011-03-10 16:24:46 -080019#include <gtest/gtest.h>
20#include <gui/SurfaceTexture.h>
21#include <gui/SurfaceTextureClient.h>
22#include <ui/GraphicBuffer.h>
23#include <utils/String8.h>
Jamie Gennis5451d152011-06-08 09:40:45 -070024#include <utils/threads.h>
Jamie Gennisd99c0882011-03-10 16:24:46 -080025
26#include <surfaceflinger/ISurfaceComposer.h>
27#include <surfaceflinger/Surface.h>
28#include <surfaceflinger/SurfaceComposerClient.h>
29
30#include <EGL/egl.h>
31#include <EGL/eglext.h>
32#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
34
35#include <ui/FramebufferNativeWindow.h>
36
37namespace android {
38
39class GLTest : public ::testing::Test {
40protected:
41
42 GLTest():
43 mEglDisplay(EGL_NO_DISPLAY),
44 mEglSurface(EGL_NO_SURFACE),
45 mEglContext(EGL_NO_CONTEXT) {
46 }
47
48 virtual void SetUp() {
49 EGLBoolean returnValue;
50
51 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
52 ASSERT_EQ(EGL_SUCCESS, eglGetError());
53 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
54
55 EGLint majorVersion;
56 EGLint minorVersion;
57 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
58 ASSERT_EQ(EGL_SUCCESS, eglGetError());
59 RecordProperty("EglVersionMajor", majorVersion);
60 RecordProperty("EglVersionMajor", minorVersion);
61
62 EGLConfig myConfig = {0};
63 EGLint numConfigs = 0;
64 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &myConfig,
65 1, &numConfigs));
66 ASSERT_EQ(EGL_SUCCESS, eglGetError());
67
68 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
69 if (displaySecsEnv != NULL) {
70 mDisplaySecs = atoi(displaySecsEnv);
71 if (mDisplaySecs < 0) {
72 mDisplaySecs = 0;
73 }
74 } else {
75 mDisplaySecs = 0;
76 }
77
78 if (mDisplaySecs > 0) {
79 mComposerClient = new SurfaceComposerClient;
80 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
81
Jamie Gennisfc850122011-04-25 16:40:05 -070082 mSurfaceControl = mComposerClient->createSurface(
Jamie Gennisd99c0882011-03-10 16:24:46 -080083 String8("Test Surface"), 0,
84 getSurfaceWidth(), getSurfaceHeight(),
85 PIXEL_FORMAT_RGB_888, 0);
86
87 ASSERT_TRUE(mSurfaceControl != NULL);
88 ASSERT_TRUE(mSurfaceControl->isValid());
89
90 ASSERT_EQ(NO_ERROR, mComposerClient->openTransaction());
91 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(30000));
92 ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
93 ASSERT_EQ(NO_ERROR, mComposerClient->closeTransaction());
94
95 sp<ANativeWindow> window = mSurfaceControl->getSurface();
96 mEglSurface = eglCreateWindowSurface(mEglDisplay, myConfig,
97 window.get(), NULL);
98 } else {
99 EGLint pbufferAttribs[] = {
100 EGL_WIDTH, getSurfaceWidth(),
101 EGL_HEIGHT, getSurfaceHeight(),
102 EGL_NONE };
103
104 mEglSurface = eglCreatePbufferSurface(mEglDisplay, myConfig,
105 pbufferAttribs);
106 }
107 ASSERT_EQ(EGL_SUCCESS, eglGetError());
108 ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
109
110 mEglContext = eglCreateContext(mEglDisplay, myConfig, EGL_NO_CONTEXT,
111 getContextAttribs());
112 ASSERT_EQ(EGL_SUCCESS, eglGetError());
113 ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
114
115 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
116 mEglContext));
117 ASSERT_EQ(EGL_SUCCESS, eglGetError());
118
119 EGLint w, h;
120 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
121 ASSERT_EQ(EGL_SUCCESS, eglGetError());
122 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
123 ASSERT_EQ(EGL_SUCCESS, eglGetError());
124 RecordProperty("EglSurfaceWidth", w);
125 RecordProperty("EglSurfaceHeight", h);
126
127 glViewport(0, 0, w, h);
128 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
129 }
130
131 virtual void TearDown() {
132 // Display the result
133 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
134 eglSwapBuffers(mEglDisplay, mEglSurface);
135 sleep(mDisplaySecs);
136 }
137
138 if (mComposerClient != NULL) {
139 mComposerClient->dispose();
140 }
141 if (mEglContext != EGL_NO_CONTEXT) {
142 eglDestroyContext(mEglDisplay, mEglContext);
143 }
144 if (mEglSurface != EGL_NO_SURFACE) {
145 eglDestroySurface(mEglDisplay, mEglSurface);
146 }
147 if (mEglDisplay != EGL_NO_DISPLAY) {
148 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
149 EGL_NO_CONTEXT);
150 eglTerminate(mEglDisplay);
151 }
152 ASSERT_EQ(EGL_SUCCESS, eglGetError());
153 }
154
155 virtual EGLint const* getConfigAttribs() {
156 static EGLint sDefaultConfigAttribs[] = {
157 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
158 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
159 EGL_RED_SIZE, 8,
160 EGL_GREEN_SIZE, 8,
161 EGL_BLUE_SIZE, 8,
162 EGL_ALPHA_SIZE, 8,
163 EGL_DEPTH_SIZE, 16,
164 EGL_STENCIL_SIZE, 8,
165 EGL_NONE };
166
167 return sDefaultConfigAttribs;
168 }
169
170 virtual EGLint const* getContextAttribs() {
171 static EGLint sDefaultContextAttribs[] = {
172 EGL_CONTEXT_CLIENT_VERSION, 2,
173 EGL_NONE };
174
175 return sDefaultContextAttribs;
176 }
177
178 virtual EGLint getSurfaceWidth() {
179 return 64;
180 }
181
182 virtual EGLint getSurfaceHeight() {
183 return 64;
184 }
185
186 void loadShader(GLenum shaderType, const char* pSource, GLuint* outShader) {
187 GLuint shader = glCreateShader(shaderType);
188 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
189 if (shader) {
190 glShaderSource(shader, 1, &pSource, NULL);
191 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
192 glCompileShader(shader);
193 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
194 GLint compiled = 0;
195 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
196 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
197 if (!compiled) {
198 GLint infoLen = 0;
199 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
200 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
201 if (infoLen) {
202 char* buf = (char*) malloc(infoLen);
203 if (buf) {
204 glGetShaderInfoLog(shader, infoLen, NULL, buf);
205 printf("Shader compile log:\n%s\n", buf);
206 free(buf);
207 FAIL();
208 }
209 } else {
210 char* buf = (char*) malloc(0x1000);
211 if (buf) {
212 glGetShaderInfoLog(shader, 0x1000, NULL, buf);
213 printf("Shader compile log:\n%s\n", buf);
214 free(buf);
215 FAIL();
216 }
217 }
218 glDeleteShader(shader);
219 shader = 0;
220 }
221 }
222 ASSERT_TRUE(shader != 0);
223 *outShader = shader;
224 }
225
226 void createProgram(const char* pVertexSource, const char* pFragmentSource,
227 GLuint* outPgm) {
228 GLuint vertexShader, fragmentShader;
229 {
230 SCOPED_TRACE("compiling vertex shader");
231 loadShader(GL_VERTEX_SHADER, pVertexSource, &vertexShader);
232 if (HasFatalFailure()) {
233 return;
234 }
235 }
236 {
237 SCOPED_TRACE("compiling fragment shader");
238 loadShader(GL_FRAGMENT_SHADER, pFragmentSource, &fragmentShader);
239 if (HasFatalFailure()) {
240 return;
241 }
242 }
243
244 GLuint program = glCreateProgram();
245 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
246 if (program) {
247 glAttachShader(program, vertexShader);
248 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
249 glAttachShader(program, fragmentShader);
250 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
251 glLinkProgram(program);
252 GLint linkStatus = GL_FALSE;
253 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
254 if (linkStatus != GL_TRUE) {
255 GLint bufLength = 0;
256 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
257 if (bufLength) {
258 char* buf = (char*) malloc(bufLength);
259 if (buf) {
260 glGetProgramInfoLog(program, bufLength, NULL, buf);
261 printf("Program link log:\n%s\n", buf);
262 free(buf);
263 FAIL();
264 }
265 }
266 glDeleteProgram(program);
267 program = 0;
268 }
269 }
270 glDeleteShader(vertexShader);
271 glDeleteShader(fragmentShader);
272 ASSERT_TRUE(program != 0);
273 *outPgm = program;
274 }
275
276 ::testing::AssertionResult checkPixel(int x, int y, int r,
277 int g, int b, int a) {
278 GLubyte pixel[4];
279 String8 msg;
280 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
281 GLenum err = glGetError();
282 if (err != GL_NO_ERROR) {
283 msg += String8::format("error reading pixel: %#x", err);
284 while ((err = glGetError()) != GL_NO_ERROR) {
285 msg += String8::format(", %#x", err);
286 }
287 fprintf(stderr, "pixel check failure: %s\n", msg.string());
288 return ::testing::AssertionFailure(
289 ::testing::Message(msg.string()));
290 }
291 if (r >= 0 && GLubyte(r) != pixel[0]) {
292 msg += String8::format("r(%d isn't %d)", pixel[0], r);
293 }
294 if (g >= 0 && GLubyte(g) != pixel[1]) {
295 if (!msg.isEmpty()) {
296 msg += " ";
297 }
298 msg += String8::format("g(%d isn't %d)", pixel[1], g);
299 }
300 if (b >= 0 && GLubyte(b) != pixel[2]) {
301 if (!msg.isEmpty()) {
302 msg += " ";
303 }
304 msg += String8::format("b(%d isn't %d)", pixel[2], b);
305 }
306 if (a >= 0 && GLubyte(a) != pixel[3]) {
307 if (!msg.isEmpty()) {
308 msg += " ";
309 }
310 msg += String8::format("a(%d isn't %d)", pixel[3], a);
311 }
312 if (!msg.isEmpty()) {
313 fprintf(stderr, "pixel check failure: %s\n", msg.string());
314 return ::testing::AssertionFailure(
315 ::testing::Message(msg.string()));
316 } else {
317 return ::testing::AssertionSuccess();
318 }
319 }
320
321 int mDisplaySecs;
322 sp<SurfaceComposerClient> mComposerClient;
323 sp<SurfaceControl> mSurfaceControl;
324
325 EGLDisplay mEglDisplay;
326 EGLSurface mEglSurface;
327 EGLContext mEglContext;
328};
329
330// XXX: Code above this point should live elsewhere
331
332class SurfaceTextureGLTest : public GLTest {
333protected:
334 static const GLint TEX_ID = 123;
335
336 virtual void SetUp() {
337 GLTest::SetUp();
338 mST = new SurfaceTexture(TEX_ID);
339 mSTC = new SurfaceTextureClient(mST);
340 mANW = mSTC;
341
342 const char vsrc[] =
343 "attribute vec4 vPosition;\n"
344 "varying vec2 texCoords;\n"
345 "uniform mat4 texMatrix;\n"
346 "void main() {\n"
347 " vec2 vTexCoords = 0.5 * (vPosition.xy + vec2(1.0, 1.0));\n"
348 " texCoords = (texMatrix * vec4(vTexCoords, 0.0, 1.0)).xy;\n"
349 " gl_Position = vPosition;\n"
350 "}\n";
351
352 const char fsrc[] =
353 "#extension GL_OES_EGL_image_external : require\n"
354 "precision mediump float;\n"
355 "uniform samplerExternalOES texSampler;\n"
356 "varying vec2 texCoords;\n"
357 "void main() {\n"
358 " gl_FragColor = texture2D(texSampler, texCoords);\n"
359 "}\n";
360
361 {
362 SCOPED_TRACE("creating shader program");
363 createProgram(vsrc, fsrc, &mPgm);
364 if (HasFatalFailure()) {
365 return;
366 }
367 }
368
369 mPositionHandle = glGetAttribLocation(mPgm, "vPosition");
370 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
371 ASSERT_NE(-1, mPositionHandle);
372 mTexSamplerHandle = glGetUniformLocation(mPgm, "texSampler");
373 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
374 ASSERT_NE(-1, mTexSamplerHandle);
375 mTexMatrixHandle = glGetUniformLocation(mPgm, "texMatrix");
376 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
377 ASSERT_NE(-1, mTexMatrixHandle);
378 }
379
380 // drawTexture draws the SurfaceTexture over the entire GL viewport.
381 void drawTexture() {
382 const GLfloat triangleVertices[] = {
383 -1.0f, 1.0f,
384 -1.0f, -1.0f,
385 1.0f, -1.0f,
386 1.0f, 1.0f,
387 };
388
389 glVertexAttribPointer(mPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, triangleVertices);
390 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
391 glEnableVertexAttribArray(mPositionHandle);
392 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
393
394 glUseProgram(mPgm);
395 glUniform1i(mTexSamplerHandle, 0);
396 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
397 glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
398 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
399
400 GLfloat texMatrix[16];
401 mST->getTransformMatrix(texMatrix);
402 glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
403
404 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
405 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
406 }
407
408 sp<SurfaceTexture> mST;
409 sp<SurfaceTextureClient> mSTC;
410 sp<ANativeWindow> mANW;
411
412 GLuint mPgm;
413 GLint mPositionHandle;
414 GLint mTexSamplerHandle;
415 GLint mTexMatrixHandle;
416};
417
418// Fill a YV12 buffer with a multi-colored checkerboard pattern
419void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
420 const int blockWidth = w > 16 ? w / 16 : 1;
421 const int blockHeight = h > 16 ? h / 16 : 1;
422 const int yuvTexOffsetY = 0;
423 int yuvTexStrideY = stride;
424 int yuvTexOffsetV = yuvTexStrideY * h;
425 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
426 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
427 int yuvTexStrideU = yuvTexStrideV;
428 for (int x = 0; x < w; x++) {
429 for (int y = 0; y < h; y++) {
430 int parityX = (x / blockWidth) & 1;
431 int parityY = (y / blockHeight) & 1;
432 unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
433 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
434 if (x < w / 2 && y < h / 2) {
435 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
436 if (x * 2 < w / 2 && y * 2 < h / 2) {
437 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
438 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
439 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
440 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
441 intensity;
442 }
443 }
444 }
445 }
446}
447
448// Fill a YV12 buffer with red outside a given rectangle and green inside it.
449void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
450 const android_native_rect_t& rect) {
451 const int yuvTexOffsetY = 0;
452 int yuvTexStrideY = stride;
453 int yuvTexOffsetV = yuvTexStrideY * h;
454 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
455 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
456 int yuvTexStrideU = yuvTexStrideV;
457 for (int x = 0; x < w; x++) {
458 for (int y = 0; y < h; y++) {
459 bool inside = rect.left <= x && x < rect.right &&
460 rect.top <= y && y < rect.bottom;
461 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
462 if (x < w / 2 && y < h / 2) {
463 bool inside = rect.left <= 2*x && 2*x < rect.right &&
464 rect.top <= 2*y && 2*y < rect.bottom;
465 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
466 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
467 inside ? 16 : 255;
468 }
469 }
470 }
471}
472
473TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
474 const int yuvTexWidth = 64;
475 const int yuvTexHeight = 66;
476
477 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
478 yuvTexWidth, yuvTexHeight, HAL_PIXEL_FORMAT_YV12));
479 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
480 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
481
Iliyan Malchev697526b2011-05-01 11:33:26 -0700482 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800483 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
484 ASSERT_TRUE(anb != NULL);
485
486 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
487 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
488
489 // Fill the buffer with the a checkerboard pattern
490 uint8_t* img = NULL;
491 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
492 fillYV12Buffer(img, yuvTexWidth, yuvTexHeight, buf->getStride());
493 buf->unlock();
494 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
495
496 mST->updateTexImage();
497
498 glClearColor(0.2, 0.2, 0.2, 0.2);
499 glClear(GL_COLOR_BUFFER_BIT);
500
501 drawTexture();
502
503 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255));
504 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255));
505 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
506 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
507
508 EXPECT_TRUE(checkPixel(22, 44, 247, 70, 255, 255));
509 EXPECT_TRUE(checkPixel(45, 52, 209, 32, 235, 255));
510 EXPECT_TRUE(checkPixel(52, 51, 100, 255, 73, 255));
511 EXPECT_TRUE(checkPixel( 7, 31, 155, 0, 118, 255));
512 EXPECT_TRUE(checkPixel(31, 9, 148, 71, 110, 255));
513 EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255));
514 EXPECT_TRUE(checkPixel(36, 22, 155, 29, 0, 255));
515}
516
517// XXX: This test is disabled because it it currently broken on all devices to
518// which I have access. Some of the checkPixel calls are not correct because
519// I just copied them from the npot test above and haven't bothered to figure
520// out the correct values.
521TEST_F(SurfaceTextureGLTest, DISABLED_TexturingFromCpuFilledYV12BufferPow2) {
522 const int yuvTexWidth = 64;
523 const int yuvTexHeight = 64;
524
525 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
526 yuvTexWidth, yuvTexHeight, HAL_PIXEL_FORMAT_YV12));
527 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
528 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
529
Iliyan Malchev697526b2011-05-01 11:33:26 -0700530 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800531 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
532 ASSERT_TRUE(anb != NULL);
533
534 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
535 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
536
537 // Fill the buffer with the a checkerboard pattern
538 uint8_t* img = NULL;
539 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
540 fillYV12Buffer(img, yuvTexWidth, yuvTexHeight, buf->getStride());
541 buf->unlock();
542 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
543
544 mST->updateTexImage();
545
546 glClearColor(0.2, 0.2, 0.2, 0.2);
547 glClear(GL_COLOR_BUFFER_BIT);
548
549 drawTexture();
550
551 EXPECT_TRUE(checkPixel( 0, 0, 255, 127, 255, 255));
552 EXPECT_TRUE(checkPixel(63, 0, 0, 133, 0, 255));
553 EXPECT_TRUE(checkPixel(63, 63, 0, 133, 0, 255));
554 EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
555
556 EXPECT_TRUE(checkPixel(22, 19, 247, 70, 255, 255));
557 EXPECT_TRUE(checkPixel(45, 11, 209, 32, 235, 255));
558 EXPECT_TRUE(checkPixel(52, 12, 100, 255, 73, 255));
559 EXPECT_TRUE(checkPixel( 7, 32, 155, 0, 118, 255));
560 EXPECT_TRUE(checkPixel(31, 54, 148, 71, 110, 255));
561 EXPECT_TRUE(checkPixel(29, 28, 255, 127, 255, 255));
562 EXPECT_TRUE(checkPixel(36, 41, 155, 29, 0, 255));
563}
564
565TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
566 const int yuvTexWidth = 64;
567 const int yuvTexHeight = 66;
568
569 ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
570 yuvTexWidth, yuvTexHeight, HAL_PIXEL_FORMAT_YV12));
571 ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
572 GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
573
574 android_native_rect_t crops[] = {
575 {4, 6, 22, 36},
576 {0, 6, 22, 36},
577 {4, 0, 22, 36},
578 {4, 6, yuvTexWidth, 36},
579 {4, 6, 22, yuvTexHeight},
580 };
581
582 for (int i = 0; i < 5; i++) {
583 const android_native_rect_t& crop(crops[i]);
584 SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }", crop.left,
585 crop.top, crop.right, crop.bottom).string());
586
587 ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
588
Iliyan Malchev697526b2011-05-01 11:33:26 -0700589 ANativeWindowBuffer* anb;
Jamie Gennisd99c0882011-03-10 16:24:46 -0800590 ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
591 ASSERT_TRUE(anb != NULL);
592
593 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
594 ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
595
596 uint8_t* img = NULL;
597 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
598 fillYV12BufferRect(img, yuvTexWidth, yuvTexHeight, buf->getStride(), crop);
599 buf->unlock();
600 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
601
602 mST->updateTexImage();
603
604 glClearColor(0.2, 0.2, 0.2, 0.2);
605 glClear(GL_COLOR_BUFFER_BIT);
606
607 drawTexture();
608
609 EXPECT_TRUE(checkPixel( 0, 0, 82, 255, 35, 255));
610 EXPECT_TRUE(checkPixel(63, 0, 82, 255, 35, 255));
611 EXPECT_TRUE(checkPixel(63, 63, 82, 255, 35, 255));
612 EXPECT_TRUE(checkPixel( 0, 63, 82, 255, 35, 255));
613
614 EXPECT_TRUE(checkPixel(25, 14, 82, 255, 35, 255));
615 EXPECT_TRUE(checkPixel(35, 31, 82, 255, 35, 255));
616 EXPECT_TRUE(checkPixel(57, 6, 82, 255, 35, 255));
617 EXPECT_TRUE(checkPixel( 5, 42, 82, 255, 35, 255));
618 EXPECT_TRUE(checkPixel(32, 33, 82, 255, 35, 255));
619 EXPECT_TRUE(checkPixel(16, 26, 82, 255, 35, 255));
620 EXPECT_TRUE(checkPixel(46, 51, 82, 255, 35, 255));
621 }
622}
623
Jamie Gennis5451d152011-06-08 09:40:45 -0700624/*
625 * This test is for testing GL -> GL texture streaming via SurfaceTexture. It
626 * contains functionality to create a producer thread that will perform GL
627 * rendering to an ANativeWindow that feeds frames to a SurfaceTexture.
628 * Additionally it supports interlocking the producer and consumer threads so
629 * that a specific sequence of calls can be deterministically created by the
630 * test.
631 *
632 * The intended usage is as follows:
633 *
634 * TEST_F(...) {
635 * class PT : public ProducerThread {
636 * virtual void render() {
637 * ...
638 * swapBuffers();
639 * }
640 * };
641 *
642 * runProducerThread(new PT());
643 *
644 * // The order of these calls will vary from test to test and may include
645 * // multiple frames and additional operations (e.g. GL rendering from the
646 * // texture).
647 * fc->waitForFrame();
648 * mST->updateTexImage();
649 * fc->finishFrame();
650 * }
651 *
652 */
653class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
654protected:
655
656 // ProducerThread is an abstract base class to simplify the creation of
657 // OpenGL ES frame producer threads.
658 class ProducerThread : public Thread {
659 public:
660 virtual ~ProducerThread() {
661 }
662
663 void setEglObjects(EGLDisplay producerEglDisplay,
664 EGLSurface producerEglSurface,
665 EGLContext producerEglContext) {
666 mProducerEglDisplay = producerEglDisplay;
667 mProducerEglSurface = producerEglSurface;
668 mProducerEglContext = producerEglContext;
669 }
670
671 virtual bool threadLoop() {
672 eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
673 mProducerEglSurface, mProducerEglContext);
674 render();
675 eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
676 EGL_NO_CONTEXT);
677 return false;
678 }
679
680 protected:
681 virtual void render() = 0;
682
683 void swapBuffers() {
684 eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
685 }
686
687 EGLDisplay mProducerEglDisplay;
688 EGLSurface mProducerEglSurface;
689 EGLContext mProducerEglContext;
690 };
691
692 // FrameCondition is a utility class for interlocking between the producer
693 // and consumer threads. The FrameCondition object should be created and
694 // destroyed in the consumer thread only. The consumer thread should set
695 // the FrameCondition as the FrameAvailableListener of the SurfaceTexture,
696 // and should call both waitForFrame and finishFrame once for each expected
697 // frame.
698 //
699 // This interlocking relies on the fact that onFrameAvailable gets called
700 // synchronously from SurfaceTexture::queueBuffer.
701 class FrameCondition : public SurfaceTexture::FrameAvailableListener {
702 public:
703 // waitForFrame waits for the next frame to arrive. This should be
704 // called from the consumer thread once for every frame expected by the
705 // test.
706 void waitForFrame() {
707 LOGV("+waitForFrame");
708 Mutex::Autolock lock(mMutex);
709 status_t result = mFrameAvailableCondition.wait(mMutex);
710 LOGV("-waitForFrame");
711 }
712
713 // Allow the producer to return from its swapBuffers call and continue
714 // on to produce the next frame. This should be called by the consumer
715 // thread once for every frame expected by the test.
716 void finishFrame() {
717 LOGV("+finishFrame");
718 Mutex::Autolock lock(mMutex);
719 mFrameFinishCondition.signal();
720 LOGV("-finishFrame");
721 }
722
723 // This should be called by SurfaceTexture on the producer thread.
724 virtual void onFrameAvailable() {
725 LOGV("+onFrameAvailable");
726 Mutex::Autolock lock(mMutex);
727 mFrameAvailableCondition.signal();
728 mFrameFinishCondition.wait(mMutex);
729 LOGV("-onFrameAvailable");
730 }
731
732 protected:
733 Mutex mMutex;
734 Condition mFrameAvailableCondition;
735 Condition mFrameFinishCondition;
736 };
737
738 SurfaceTextureGLToGLTest():
739 mProducerEglSurface(EGL_NO_SURFACE),
740 mProducerEglContext(EGL_NO_CONTEXT) {
741 }
742
743 virtual void SetUp() {
744 SurfaceTextureGLTest::SetUp();
745
746 EGLConfig myConfig = {0};
747 EGLint numConfigs = 0;
748 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &myConfig,
749 1, &numConfigs));
750 ASSERT_EQ(EGL_SUCCESS, eglGetError());
751
752 mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, myConfig,
753 mANW.get(), NULL);
754 ASSERT_EQ(EGL_SUCCESS, eglGetError());
755 ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
756
757 mProducerEglContext = eglCreateContext(mEglDisplay, myConfig,
758 EGL_NO_CONTEXT, getContextAttribs());
759 ASSERT_EQ(EGL_SUCCESS, eglGetError());
760 ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
761
762 mFC = new FrameCondition();
763 mST->setFrameAvailableListener(mFC);
764 }
765
766 virtual void TearDown() {
767 if (mProducerThread != NULL) {
768 mProducerThread->requestExitAndWait();
769 }
770 if (mProducerEglContext != EGL_NO_CONTEXT) {
771 eglDestroyContext(mEglDisplay, mProducerEglContext);
772 }
773 if (mProducerEglSurface != EGL_NO_SURFACE) {
774 eglDestroySurface(mEglDisplay, mProducerEglSurface);
775 }
776 mProducerThread.clear();
777 mFC.clear();
778 }
779
780 void runProducerThread(const sp<ProducerThread> producerThread) {
781 ASSERT_TRUE(mProducerThread == NULL);
782 mProducerThread = producerThread;
783 producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
784 mProducerEglContext);
785 producerThread->run();
786 }
787
788 EGLSurface mProducerEglSurface;
789 EGLContext mProducerEglContext;
790 sp<ProducerThread> mProducerThread;
791 sp<FrameCondition> mFC;
792};
793
794// XXX: This test is disabled because it causes hangs on some devices.
795TEST_F(SurfaceTextureGLToGLTest, DISABLED_UpdateTexImageBeforeFrameFinishedWorks) {
796 class PT : public ProducerThread {
797 virtual void render() {
798 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
799 glClear(GL_COLOR_BUFFER_BIT);
800 swapBuffers();
801 }
802 };
803
804 runProducerThread(new PT());
805
806 mFC->waitForFrame();
807 mST->updateTexImage();
808 mFC->finishFrame();
809
810 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
Jamie Gennisd99c0882011-03-10 16:24:46 -0800811}
Jamie Gennis5451d152011-06-08 09:40:45 -0700812
813TEST_F(SurfaceTextureGLToGLTest, UpdateTexImageAfterFrameFinishedWorks) {
814 class PT : public ProducerThread {
815 virtual void render() {
816 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
817 glClear(GL_COLOR_BUFFER_BIT);
818 swapBuffers();
819 }
820 };
821
822 runProducerThread(new PT());
823
824 mFC->waitForFrame();
825 mFC->finishFrame();
826 mST->updateTexImage();
827
828 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
829}
830
831// XXX: This test is disabled because it causes hangs on some devices.
832TEST_F(SurfaceTextureGLToGLTest, DISABLED_RepeatedUpdateTexImageBeforeFrameFinishedWorks) {
833 enum { NUM_ITERATIONS = 1024 };
834
835 class PT : public ProducerThread {
836 virtual void render() {
837 for (int i = 0; i < NUM_ITERATIONS; i++) {
838 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
839 glClear(GL_COLOR_BUFFER_BIT);
840 LOGV("+swapBuffers");
841 swapBuffers();
842 LOGV("-swapBuffers");
843 }
844 }
845 };
846
847 runProducerThread(new PT());
848
849 for (int i = 0; i < NUM_ITERATIONS; i++) {
850 mFC->waitForFrame();
851 LOGV("+updateTexImage");
852 mST->updateTexImage();
853 LOGV("-updateTexImage");
854 mFC->finishFrame();
855
856 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
857 }
858}
859
860// XXX: This test is disabled because it causes hangs on some devices.
861TEST_F(SurfaceTextureGLToGLTest, DISABLED_RepeatedUpdateTexImageAfterFrameFinishedWorks) {
862 enum { NUM_ITERATIONS = 1024 };
863
864 class PT : public ProducerThread {
865 virtual void render() {
866 for (int i = 0; i < NUM_ITERATIONS; i++) {
867 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
868 glClear(GL_COLOR_BUFFER_BIT);
869 LOGV("+swapBuffers");
870 swapBuffers();
871 LOGV("-swapBuffers");
872 }
873 }
874 };
875
876 runProducerThread(new PT());
877
878 for (int i = 0; i < NUM_ITERATIONS; i++) {
879 mFC->waitForFrame();
880 mFC->finishFrame();
881 LOGV("+updateTexImage");
882 mST->updateTexImage();
883 LOGV("-updateTexImage");
884
885 // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
886 }
887}
888
889} // namespace android