SurfaceTexture_test.cpp revision 185a0a0420f54f9588ec1c7230313023a7a2259b
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SurfaceTexture_test"
18//#define LOG_NDEBUG 0
19
20#include <gtest/gtest.h>
21#include <gui/SurfaceTexture.h>
22#include <gui/SurfaceTextureClient.h>
23#include <ui/GraphicBuffer.h>
24#include <utils/String8.h>
25#include <utils/threads.h>
26
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() {
50        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
61        EGLint numConfigs = 0;
62        EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
63                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
80            mSurfaceControl = mComposerClient->createSurface(
81                    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
88            SurfaceComposerClient::openGlobalTransaction();
89            ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
90            ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
91            SurfaceComposerClient::closeGlobalTransaction();
92
93            sp<ANativeWindow> window = mSurfaceControl->getSurface();
94            mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
95                    window.get(), NULL);
96        } else {
97            EGLint pbufferAttribs[] = {
98                EGL_WIDTH, getSurfaceWidth(),
99                EGL_HEIGHT, getSurfaceHeight(),
100                EGL_NONE };
101
102            mEglSurface = eglCreatePbufferSurface(mEglDisplay, mGlConfig,
103                    pbufferAttribs);
104        }
105        ASSERT_EQ(EGL_SUCCESS, eglGetError());
106        ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
107
108        mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
109                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() {
177        return 512;
178    }
179
180    virtual EGLint getSurfaceHeight() {
181        return 512;
182    }
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
274    static int abs(int value) {
275        return value > 0 ? value : -value;
276    }
277
278    ::testing::AssertionResult checkPixel(int x, int y, int r,
279            int g, int b, int a, int tolerance=2) {
280        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        }
293        if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
294            msg += String8::format("r(%d isn't %d)", pixel[0], r);
295        }
296        if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
297            if (!msg.isEmpty()) {
298                msg += " ";
299            }
300            msg += String8::format("g(%d isn't %d)", pixel[1], g);
301        }
302        if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
303            if (!msg.isEmpty()) {
304                msg += " ";
305            }
306            msg += String8::format("b(%d isn't %d)", pixel[2], b);
307        }
308        if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
309            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;
330    EGLConfig  mGlConfig;
331};
332
333// XXX: Code above this point should live elsewhere
334
335class SurfaceTextureGLTest : public GLTest {
336protected:
337    enum { 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
383    virtual void TearDown() {
384        mANW.clear();
385        mSTC.clear();
386        mST.clear();
387        GLTest::TearDown();
388    }
389
390    // 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,
400                triangleVertices);
401        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
402        glEnableVertexAttribArray(mPositionHandle);
403        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
404
405        glUseProgram(mPgm);
406        glUniform1i(mTexSamplerHandle, 0);
407        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
408        glBindTexture(GL_TEXTURE_EXTERNAL_OES, TEX_ID);
409        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
410
411        // XXX: These calls are not needed for GL_TEXTURE_EXTERNAL_OES as
412        // they're setting the defautls for that target, but when hacking things
413        // to use GL_TEXTURE_2D they are needed to achieve the same behavior.
414        glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER,
415                GL_LINEAR);
416        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
417        glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER,
418                GL_LINEAR);
419        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
420        glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S,
421                GL_CLAMP_TO_EDGE);
422        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
423        glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T,
424                GL_CLAMP_TO_EDGE);
425        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
426
427        GLfloat texMatrix[16];
428        mST->getTransformMatrix(texMatrix);
429        glUniformMatrix4fv(mTexMatrixHandle, 1, GL_FALSE, texMatrix);
430
431        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
432        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
433    }
434
435    class FrameWaiter : public SurfaceTexture::FrameAvailableListener {
436    public:
437        FrameWaiter():
438                mPendingFrames(0) {
439        }
440
441        void waitForFrame() {
442            Mutex::Autolock lock(mMutex);
443            while (mPendingFrames == 0) {
444                mCondition.wait(mMutex);
445            }
446            mPendingFrames--;
447        }
448
449        virtual void onFrameAvailable() {
450            Mutex::Autolock lock(mMutex);
451            mPendingFrames++;
452            mCondition.signal();
453        }
454
455        int mPendingFrames;
456        Mutex mMutex;
457        Condition mCondition;
458    };
459
460    sp<SurfaceTexture> mST;
461    sp<SurfaceTextureClient> mSTC;
462    sp<ANativeWindow> mANW;
463
464    GLuint mPgm;
465    GLint mPositionHandle;
466    GLint mTexSamplerHandle;
467    GLint mTexMatrixHandle;
468};
469
470// Fill a YV12 buffer with a multi-colored checkerboard pattern
471void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
472    const int blockWidth = w > 16 ? w / 16 : 1;
473    const int blockHeight = h > 16 ? h / 16 : 1;
474    const int yuvTexOffsetY = 0;
475    int yuvTexStrideY = stride;
476    int yuvTexOffsetV = yuvTexStrideY * h;
477    int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
478    int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
479    int yuvTexStrideU = yuvTexStrideV;
480    for (int x = 0; x < w; x++) {
481        for (int y = 0; y < h; y++) {
482            int parityX = (x / blockWidth) & 1;
483            int parityY = (y / blockHeight) & 1;
484            unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
485            buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
486            if (x < w / 2 && y < h / 2) {
487                buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
488                if (x * 2 < w / 2 && y * 2 < h / 2) {
489                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
490                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
491                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
492                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
493                        intensity;
494                }
495            }
496        }
497    }
498}
499
500// Fill a YV12 buffer with red outside a given rectangle and green inside it.
501void fillYV12BufferRect(uint8_t* buf, int w, int h, int stride,
502        const android_native_rect_t& rect) {
503    const int yuvTexOffsetY = 0;
504    int yuvTexStrideY = stride;
505    int yuvTexOffsetV = yuvTexStrideY * h;
506    int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
507    int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
508    int yuvTexStrideU = yuvTexStrideV;
509    for (int x = 0; x < w; x++) {
510        for (int y = 0; y < h; y++) {
511            bool inside = rect.left <= x && x < rect.right &&
512                    rect.top <= y && y < rect.bottom;
513            buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
514            if (x < w / 2 && y < h / 2) {
515                bool inside = rect.left <= 2*x && 2*x < rect.right &&
516                        rect.top <= 2*y && 2*y < rect.bottom;
517                buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
518                buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
519                        inside ? 16 : 255;
520            }
521        }
522    }
523}
524
525void fillRGBA8Buffer(uint8_t* buf, int w, int h, int stride) {
526    const size_t PIXEL_SIZE = 4;
527    for (int x = 0; x < w; x++) {
528        for (int y = 0; y < h; y++) {
529            off_t offset = (y * stride + x) * PIXEL_SIZE;
530            for (int c = 0; c < 4; c++) {
531                int parityX = (x / (1 << (c+2))) & 1;
532                int parityY = (y / (1 << (c+2))) & 1;
533                buf[offset + c] = (parityX ^ parityY) ? 231 : 35;
534            }
535        }
536    }
537}
538
539TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferNpot) {
540    const int texWidth = 64;
541    const int texHeight = 66;
542
543    ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
544            texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
545    ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
546            GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
547
548    ANativeWindowBuffer* anb;
549    ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
550    ASSERT_TRUE(anb != NULL);
551
552    sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
553    ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
554
555    // Fill the buffer with the a checkerboard pattern
556    uint8_t* img = NULL;
557    buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
558    fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
559    buf->unlock();
560    ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
561
562    mST->updateTexImage();
563
564    glClearColor(0.2, 0.2, 0.2, 0.2);
565    glClear(GL_COLOR_BUFFER_BIT);
566
567    glViewport(0, 0, texWidth, texHeight);
568    drawTexture();
569
570    EXPECT_TRUE(checkPixel( 0,  0, 255, 127, 255, 255));
571    EXPECT_TRUE(checkPixel(63,  0,   0, 133,   0, 255));
572    EXPECT_TRUE(checkPixel(63, 65,   0, 133,   0, 255));
573    EXPECT_TRUE(checkPixel( 0, 65, 255, 127, 255, 255));
574
575    EXPECT_TRUE(checkPixel(22, 44, 255, 127, 255, 255));
576    EXPECT_TRUE(checkPixel(45, 52, 255, 127, 255, 255));
577    EXPECT_TRUE(checkPixel(52, 51,  98, 255,  73, 255));
578    EXPECT_TRUE(checkPixel( 7, 31, 155,   0, 118, 255));
579    EXPECT_TRUE(checkPixel(31,  9, 107,  24,  87, 255));
580    EXPECT_TRUE(checkPixel(29, 35, 255, 127, 255, 255));
581    EXPECT_TRUE(checkPixel(36, 22, 155,  29,   0, 255));
582}
583
584TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferPow2) {
585    const int texWidth = 64;
586    const int texHeight = 64;
587
588    ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
589            texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
590    ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
591            GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
592
593    ANativeWindowBuffer* anb;
594    ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
595    ASSERT_TRUE(anb != NULL);
596
597    sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
598    ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
599
600    // Fill the buffer with the a checkerboard pattern
601    uint8_t* img = NULL;
602    buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
603    fillYV12Buffer(img, texWidth, texHeight, buf->getStride());
604    buf->unlock();
605    ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
606
607    mST->updateTexImage();
608
609    glClearColor(0.2, 0.2, 0.2, 0.2);
610    glClear(GL_COLOR_BUFFER_BIT);
611
612    glViewport(0, 0, texWidth, texHeight);
613    drawTexture();
614
615    EXPECT_TRUE(checkPixel( 0,  0,   0, 133,   0, 255));
616    EXPECT_TRUE(checkPixel(63,  0, 255, 127, 255, 255));
617    EXPECT_TRUE(checkPixel(63, 63,   0, 133,   0, 255));
618    EXPECT_TRUE(checkPixel( 0, 63, 255, 127, 255, 255));
619
620    EXPECT_TRUE(checkPixel(22, 19, 100, 255,  74, 255));
621    EXPECT_TRUE(checkPixel(45, 11, 100, 255,  74, 255));
622    EXPECT_TRUE(checkPixel(52, 12, 155,   0, 181, 255));
623    EXPECT_TRUE(checkPixel( 7, 32, 150, 237, 170, 255));
624    EXPECT_TRUE(checkPixel(31, 54,   0,  71, 117, 255));
625    EXPECT_TRUE(checkPixel(29, 28,   0, 133,   0, 255));
626    EXPECT_TRUE(checkPixel(36, 41, 100, 232, 255, 255));
627}
628
629TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BufferWithCrop) {
630    const int texWidth = 64;
631    const int texHeight = 66;
632
633    ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
634            texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
635    ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
636            GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
637
638    android_native_rect_t crops[] = {
639        {4, 6, 22, 36},
640        {0, 6, 22, 36},
641        {4, 0, 22, 36},
642        {4, 6, texWidth, 36},
643        {4, 6, 22, texHeight},
644    };
645
646    for (int i = 0; i < 5; i++) {
647        const android_native_rect_t& crop(crops[i]);
648        SCOPED_TRACE(String8::format("rect{ l: %d t: %d r: %d b: %d }",
649                crop.left, crop.top, crop.right, crop.bottom).string());
650
651        ASSERT_EQ(NO_ERROR, native_window_set_crop(mANW.get(), &crop));
652
653        ANativeWindowBuffer* anb;
654        ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
655        ASSERT_TRUE(anb != NULL);
656
657        sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
658        ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(),
659                buf->getNativeBuffer()));
660
661        uint8_t* img = NULL;
662        buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
663        fillYV12BufferRect(img, texWidth, texHeight, buf->getStride(), crop);
664        buf->unlock();
665        ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(),
666                buf->getNativeBuffer()));
667
668        mST->updateTexImage();
669
670        glClearColor(0.2, 0.2, 0.2, 0.2);
671        glClear(GL_COLOR_BUFFER_BIT);
672
673        glViewport(0, 0, 64, 64);
674        drawTexture();
675
676        EXPECT_TRUE(checkPixel( 0,  0,  82, 255,  35, 255));
677        EXPECT_TRUE(checkPixel(63,  0,  82, 255,  35, 255));
678        EXPECT_TRUE(checkPixel(63, 63,  82, 255,  35, 255));
679        EXPECT_TRUE(checkPixel( 0, 63,  82, 255,  35, 255));
680
681        EXPECT_TRUE(checkPixel(25, 14,  82, 255,  35, 255));
682        EXPECT_TRUE(checkPixel(35, 31,  82, 255,  35, 255));
683        EXPECT_TRUE(checkPixel(57,  6,  82, 255,  35, 255));
684        EXPECT_TRUE(checkPixel( 5, 42,  82, 255,  35, 255));
685        EXPECT_TRUE(checkPixel(32, 33,  82, 255,  35, 255));
686        EXPECT_TRUE(checkPixel(16, 26,  82, 255,  35, 255));
687        EXPECT_TRUE(checkPixel(46, 51,  82, 255,  35, 255));
688    }
689}
690
691// This test is intended to catch synchronization bugs between the CPU-written
692// and GPU-read buffers.
693TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledYV12BuffersRepeatedly) {
694    enum { texWidth = 16 };
695    enum { texHeight = 16 };
696    enum { numFrames = 1024 };
697
698    ASSERT_EQ(NO_ERROR, mST->setSynchronousMode(true));
699    ASSERT_EQ(NO_ERROR, mST->setBufferCountServer(2));
700    ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
701            texWidth, texHeight, HAL_PIXEL_FORMAT_YV12));
702    ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
703            GRALLOC_USAGE_SW_WRITE_OFTEN));
704
705    struct TestPixel {
706        int x;
707        int y;
708    };
709    const TestPixel testPixels[] = {
710        {  4, 11 },
711        { 12, 14 },
712        {  7,  2 },
713    };
714    enum {numTestPixels = sizeof(testPixels) / sizeof(testPixels[0])};
715
716    class ProducerThread : public Thread {
717    public:
718        ProducerThread(const sp<ANativeWindow>& anw,
719                const TestPixel* testPixels):
720                mANW(anw),
721                mTestPixels(testPixels) {
722        }
723
724        virtual ~ProducerThread() {
725        }
726
727        virtual bool threadLoop() {
728            for (int i = 0; i < numFrames; i++) {
729                ANativeWindowBuffer* anb;
730                if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
731                    return false;
732                }
733                if (anb == NULL) {
734                    return false;
735                }
736
737                sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
738                if (mANW->lockBuffer(mANW.get(), buf->getNativeBuffer())
739                        != NO_ERROR) {
740                    return false;
741                }
742
743                const int yuvTexOffsetY = 0;
744                int stride = buf->getStride();
745                int yuvTexStrideY = stride;
746                int yuvTexOffsetV = yuvTexStrideY * texHeight;
747                int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
748                int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * texHeight/2;
749                int yuvTexStrideU = yuvTexStrideV;
750
751                uint8_t* img = NULL;
752                buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
753
754                // Gray out all the test pixels first, so we're more likely to
755                // see a failure if GL is still texturing from the buffer we
756                // just dequeued.
757                for (int j = 0; j < numTestPixels; j++) {
758                    int x = mTestPixels[j].x;
759                    int y = mTestPixels[j].y;
760                    uint8_t value = 128;
761                    img[y*stride + x] = value;
762                }
763
764                // Fill the buffer with gray.
765                for (int y = 0; y < texHeight; y++) {
766                    for (int x = 0; x < texWidth; x++) {
767                        img[yuvTexOffsetY + y*yuvTexStrideY + x] = 128;
768                        img[yuvTexOffsetU + (y/2)*yuvTexStrideU + x/2] = 128;
769                        img[yuvTexOffsetV + (y/2)*yuvTexStrideV + x/2] = 128;
770                    }
771                }
772
773                // Set the test pixels to either white or black.
774                for (int j = 0; j < numTestPixels; j++) {
775                    int x = mTestPixels[j].x;
776                    int y = mTestPixels[j].y;
777                    uint8_t value = 0;
778                    if (j == (i % numTestPixels)) {
779                        value = 255;
780                    }
781                    img[y*stride + x] = value;
782                }
783
784                buf->unlock();
785                if (mANW->queueBuffer(mANW.get(), buf->getNativeBuffer())
786                        != NO_ERROR) {
787                    return false;
788                }
789            }
790            return false;
791        }
792
793        sp<ANativeWindow> mANW;
794        const TestPixel* mTestPixels;
795    };
796
797    sp<FrameWaiter> fw(new FrameWaiter);
798    mST->setFrameAvailableListener(fw);
799
800    sp<Thread> pt(new ProducerThread(mANW, testPixels));
801    pt->run();
802
803    glViewport(0, 0, texWidth, texHeight);
804
805    glClearColor(0.2, 0.2, 0.2, 0.2);
806    glClear(GL_COLOR_BUFFER_BIT);
807
808    // We wait for the first two frames up front so that the producer will be
809    // likely to dequeue the buffer that's currently being textured from.
810    fw->waitForFrame();
811    fw->waitForFrame();
812
813    for (int i = 0; i < numFrames; i++) {
814        SCOPED_TRACE(String8::format("frame %d", i).string());
815
816        // We must wait for each frame to come in because if we ever do an
817        // updateTexImage call that doesn't consume a newly available buffer
818        // then the producer and consumer will get out of sync, which will cause
819        // a deadlock.
820        if (i > 1) {
821            fw->waitForFrame();
822        }
823        mST->updateTexImage();
824        drawTexture();
825
826        for (int j = 0; j < numTestPixels; j++) {
827            int x = testPixels[j].x;
828            int y = testPixels[j].y;
829            uint8_t value = 0;
830            if (j == (i % numTestPixels)) {
831                // We must y-invert the texture coords
832                EXPECT_TRUE(checkPixel(x, texHeight-y-1, 255, 255, 255, 255));
833            } else {
834                // We must y-invert the texture coords
835                EXPECT_TRUE(checkPixel(x, texHeight-y-1, 0, 0, 0, 255));
836            }
837        }
838    }
839
840    pt->requestExitAndWait();
841}
842
843TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferNpot) {
844    const int texWidth = 64;
845    const int texHeight = 66;
846
847    ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
848            texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
849    ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
850            GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
851
852    android_native_buffer_t* anb;
853    ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
854    ASSERT_TRUE(anb != NULL);
855
856    sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
857    ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
858
859    // Fill the buffer with the a checkerboard pattern
860    uint8_t* img = NULL;
861    buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
862    fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride());
863    buf->unlock();
864    ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
865
866    mST->updateTexImage();
867
868    glClearColor(0.2, 0.2, 0.2, 0.2);
869    glClear(GL_COLOR_BUFFER_BIT);
870
871    glViewport(0, 0, texWidth, texHeight);
872    drawTexture();
873
874    EXPECT_TRUE(checkPixel( 0,  0,  35,  35,  35,  35));
875    EXPECT_TRUE(checkPixel(63,  0, 231, 231, 231, 231));
876    EXPECT_TRUE(checkPixel(63, 65, 231, 231, 231, 231));
877    EXPECT_TRUE(checkPixel( 0, 65,  35,  35,  35,  35));
878
879    EXPECT_TRUE(checkPixel(15, 10,  35, 231, 231, 231));
880    EXPECT_TRUE(checkPixel(23, 65, 231,  35, 231,  35));
881    EXPECT_TRUE(checkPixel(19, 40,  35, 231,  35,  35));
882    EXPECT_TRUE(checkPixel(38, 30, 231,  35,  35,  35));
883    EXPECT_TRUE(checkPixel(42, 54,  35,  35,  35, 231));
884    EXPECT_TRUE(checkPixel(37, 34,  35, 231, 231, 231));
885    EXPECT_TRUE(checkPixel(31,  8, 231,  35,  35, 231));
886    EXPECT_TRUE(checkPixel(37, 47, 231,  35, 231, 231));
887    EXPECT_TRUE(checkPixel(25, 38,  35,  35,  35,  35));
888    EXPECT_TRUE(checkPixel(49,  6,  35, 231,  35,  35));
889    EXPECT_TRUE(checkPixel(54, 50,  35, 231, 231, 231));
890    EXPECT_TRUE(checkPixel(27, 26, 231, 231, 231, 231));
891    EXPECT_TRUE(checkPixel(10,  6,  35,  35, 231, 231));
892    EXPECT_TRUE(checkPixel(29,  4,  35,  35,  35, 231));
893    EXPECT_TRUE(checkPixel(55, 28,  35,  35, 231,  35));
894    EXPECT_TRUE(checkPixel(58, 55,  35,  35, 231, 231));
895}
896
897TEST_F(SurfaceTextureGLTest, TexturingFromCpuFilledRGBABufferPow2) {
898    const int texWidth = 64;
899    const int texHeight = 64;
900
901    ASSERT_EQ(NO_ERROR, native_window_set_buffers_geometry(mANW.get(),
902            texWidth, texHeight, HAL_PIXEL_FORMAT_RGBA_8888));
903    ASSERT_EQ(NO_ERROR, native_window_set_usage(mANW.get(),
904            GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN));
905
906    android_native_buffer_t* anb;
907    ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
908    ASSERT_TRUE(anb != NULL);
909
910    sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
911    ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
912
913    // Fill the buffer with the a checkerboard pattern
914    uint8_t* img = NULL;
915    buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
916    fillRGBA8Buffer(img, texWidth, texHeight, buf->getStride());
917    buf->unlock();
918    ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
919
920    mST->updateTexImage();
921
922    glClearColor(0.2, 0.2, 0.2, 0.2);
923    glClear(GL_COLOR_BUFFER_BIT);
924
925    glViewport(0, 0, texWidth, texHeight);
926    drawTexture();
927
928    EXPECT_TRUE(checkPixel( 0,  0, 231, 231, 231, 231));
929    EXPECT_TRUE(checkPixel(63,  0,  35,  35,  35,  35));
930    EXPECT_TRUE(checkPixel(63, 63, 231, 231, 231, 231));
931    EXPECT_TRUE(checkPixel( 0, 63,  35,  35,  35,  35));
932
933    EXPECT_TRUE(checkPixel(12, 46, 231, 231, 231,  35));
934    EXPECT_TRUE(checkPixel(16,  1, 231, 231,  35, 231));
935    EXPECT_TRUE(checkPixel(21, 12, 231,  35,  35, 231));
936    EXPECT_TRUE(checkPixel(26, 51, 231,  35, 231,  35));
937    EXPECT_TRUE(checkPixel( 5, 32,  35, 231, 231,  35));
938    EXPECT_TRUE(checkPixel(13,  8,  35, 231, 231, 231));
939    EXPECT_TRUE(checkPixel(46,  3,  35,  35, 231,  35));
940    EXPECT_TRUE(checkPixel(30, 33,  35,  35,  35,  35));
941    EXPECT_TRUE(checkPixel( 6, 52, 231, 231,  35,  35));
942    EXPECT_TRUE(checkPixel(55, 33,  35, 231,  35, 231));
943    EXPECT_TRUE(checkPixel(16, 29,  35,  35, 231, 231));
944    EXPECT_TRUE(checkPixel( 1, 30,  35,  35,  35, 231));
945    EXPECT_TRUE(checkPixel(41, 37,  35,  35, 231, 231));
946    EXPECT_TRUE(checkPixel(46, 29, 231, 231,  35,  35));
947    EXPECT_TRUE(checkPixel(15, 25,  35, 231,  35, 231));
948    EXPECT_TRUE(checkPixel( 3, 52,  35, 231,  35,  35));
949}
950
951TEST_F(SurfaceTextureGLTest, AbandonUnblocksDequeueBuffer) {
952    class ProducerThread : public Thread {
953    public:
954        ProducerThread(const sp<ANativeWindow>& anw):
955                mANW(anw),
956                mDequeueError(NO_ERROR) {
957        }
958
959        virtual ~ProducerThread() {
960        }
961
962        virtual bool threadLoop() {
963            Mutex::Autolock lock(mMutex);
964            ANativeWindowBuffer* anb;
965
966            // Frame 1
967            if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
968                return false;
969            }
970            if (anb == NULL) {
971                return false;
972            }
973            if (mANW->queueBuffer(mANW.get(), anb)
974                    != NO_ERROR) {
975                return false;
976            }
977
978            // Frame 2
979            if (mANW->dequeueBuffer(mANW.get(), &anb) != NO_ERROR) {
980                return false;
981            }
982            if (anb == NULL) {
983                return false;
984            }
985            if (mANW->queueBuffer(mANW.get(), anb)
986                    != NO_ERROR) {
987                return false;
988            }
989
990            // Frame 3 - error expected
991            mDequeueError = mANW->dequeueBuffer(mANW.get(), &anb);
992            return false;
993        }
994
995        status_t getDequeueError() {
996            Mutex::Autolock lock(mMutex);
997            return mDequeueError;
998        }
999
1000    private:
1001        sp<ANativeWindow> mANW;
1002        status_t mDequeueError;
1003        Mutex mMutex;
1004    };
1005
1006    sp<FrameWaiter> fw(new FrameWaiter);
1007    mST->setFrameAvailableListener(fw);
1008    ASSERT_EQ(OK, mST->setSynchronousMode(true));
1009    ASSERT_EQ(OK, mST->setBufferCountServer(2));
1010
1011    sp<Thread> pt(new ProducerThread(mANW));
1012    pt->run();
1013
1014    fw->waitForFrame();
1015    fw->waitForFrame();
1016
1017    // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1018    // block waiting for a buffer to become available.
1019    usleep(100000);
1020
1021    mST->abandon();
1022
1023    pt->requestExitAndWait();
1024    ASSERT_EQ(NO_INIT,
1025            reinterpret_cast<ProducerThread*>(pt.get())->getDequeueError());
1026}
1027
1028TEST_F(SurfaceTextureGLTest, InvalidWidthOrHeightFails) {
1029    int texHeight = 16;
1030    ANativeWindowBuffer* anb;
1031
1032    GLint maxTextureSize;
1033    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
1034
1035    // make sure it works with small textures
1036    mST->setDefaultBufferSize(16, texHeight);
1037    EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1038    EXPECT_EQ(16, anb->width);
1039    EXPECT_EQ(texHeight, anb->height);
1040    EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1041    EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1042
1043    // make sure it works with GL_MAX_TEXTURE_SIZE
1044    mST->setDefaultBufferSize(maxTextureSize, texHeight);
1045    EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1046    EXPECT_EQ(maxTextureSize, anb->width);
1047    EXPECT_EQ(texHeight, anb->height);
1048    EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1049    EXPECT_EQ(NO_ERROR, mST->updateTexImage());
1050
1051    // make sure it fails with GL_MAX_TEXTURE_SIZE+1
1052    mST->setDefaultBufferSize(maxTextureSize+1, texHeight);
1053    EXPECT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
1054    EXPECT_EQ(maxTextureSize+1, anb->width);
1055    EXPECT_EQ(texHeight, anb->height);
1056    EXPECT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), anb));
1057    ASSERT_NE(NO_ERROR, mST->updateTexImage());
1058}
1059
1060/*
1061 * This test fixture is for testing GL -> GL texture streaming.  It creates an
1062 * EGLSurface and an EGLContext for the image producer to use.
1063 */
1064class SurfaceTextureGLToGLTest : public SurfaceTextureGLTest {
1065protected:
1066    SurfaceTextureGLToGLTest():
1067            mProducerEglSurface(EGL_NO_SURFACE),
1068            mProducerEglContext(EGL_NO_CONTEXT) {
1069    }
1070
1071    virtual void SetUp() {
1072        SurfaceTextureGLTest::SetUp();
1073
1074        EGLConfig myConfig = {0};
1075        EGLint numConfigs = 0;
1076        EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &myConfig,
1077                1, &numConfigs));
1078        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1079
1080        mProducerEglSurface = eglCreateWindowSurface(mEglDisplay, myConfig,
1081                mANW.get(), NULL);
1082        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1083        ASSERT_NE(EGL_NO_SURFACE, mProducerEglSurface);
1084
1085        mProducerEglContext = eglCreateContext(mEglDisplay, myConfig,
1086                EGL_NO_CONTEXT, getContextAttribs());
1087        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1088        ASSERT_NE(EGL_NO_CONTEXT, mProducerEglContext);
1089    }
1090
1091    virtual void TearDown() {
1092        if (mProducerEglContext != EGL_NO_CONTEXT) {
1093            eglDestroyContext(mEglDisplay, mProducerEglContext);
1094        }
1095        if (mProducerEglSurface != EGL_NO_SURFACE) {
1096            eglDestroySurface(mEglDisplay, mProducerEglSurface);
1097        }
1098        SurfaceTextureGLTest::TearDown();
1099    }
1100
1101    EGLSurface mProducerEglSurface;
1102    EGLContext mProducerEglContext;
1103};
1104
1105TEST_F(SurfaceTextureGLToGLTest, TexturingFromGLFilledRGBABufferPow2) {
1106    const int texWidth = 64;
1107    const int texHeight = 64;
1108
1109    mST->setDefaultBufferSize(texWidth, texHeight);
1110
1111    // Do the producer side of things
1112    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1113            mProducerEglSurface, mProducerEglContext));
1114    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1115
1116    // This is needed to ensure we pick up a buffer of the correct size.
1117    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1118
1119    glClearColor(0.6, 0.6, 0.6, 0.6);
1120    glClear(GL_COLOR_BUFFER_BIT);
1121
1122    glEnable(GL_SCISSOR_TEST);
1123    glScissor(4, 4, 4, 4);
1124    glClearColor(1.0, 0.0, 0.0, 1.0);
1125    glClear(GL_COLOR_BUFFER_BIT);
1126
1127    glScissor(24, 48, 4, 4);
1128    glClearColor(0.0, 1.0, 0.0, 1.0);
1129    glClear(GL_COLOR_BUFFER_BIT);
1130
1131    glScissor(37, 17, 4, 4);
1132    glClearColor(0.0, 0.0, 1.0, 1.0);
1133    glClear(GL_COLOR_BUFFER_BIT);
1134
1135    eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1136
1137    // Do the consumer side of things
1138    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1139            mEglContext));
1140    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1141
1142    glDisable(GL_SCISSOR_TEST);
1143
1144    mST->updateTexImage(); // Skip the first frame, which was empty
1145    mST->updateTexImage();
1146
1147    glClearColor(0.2, 0.2, 0.2, 0.2);
1148    glClear(GL_COLOR_BUFFER_BIT);
1149
1150    glViewport(0, 0, texWidth, texHeight);
1151    drawTexture();
1152
1153    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
1154    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
1155    EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
1156    EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
1157
1158    EXPECT_TRUE(checkPixel( 4,  7, 255,   0,   0, 255));
1159    EXPECT_TRUE(checkPixel(25, 51,   0, 255,   0, 255));
1160    EXPECT_TRUE(checkPixel(40, 19,   0,   0, 255, 255));
1161    EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
1162    EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
1163    EXPECT_TRUE(checkPixel(13,  8, 153, 153, 153, 153));
1164    EXPECT_TRUE(checkPixel(46,  3, 153, 153, 153, 153));
1165    EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
1166    EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
1167    EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
1168    EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
1169    EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
1170    EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
1171    EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
1172    EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
1173    EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
1174}
1175
1176TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceUnrefsBuffers) {
1177    sp<GraphicBuffer> buffers[3];
1178
1179    // This test requires async mode to run on a single thread.
1180    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1181            mProducerEglSurface, mProducerEglContext));
1182    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1183    EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1184    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1185
1186    for (int i = 0; i < 3; i++) {
1187        // Produce a frame
1188        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1189                mProducerEglSurface, mProducerEglContext));
1190        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1191        glClear(GL_COLOR_BUFFER_BIT);
1192        eglSwapBuffers(mEglDisplay, mProducerEglSurface);
1193
1194        // Consume a frame
1195        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1196                mEglContext));
1197        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1198        mST->updateTexImage();
1199        buffers[i] = mST->getCurrentBuffer();
1200    }
1201
1202    // Destroy the GL texture object to release its ref on buffers[2].
1203    GLuint texID = TEX_ID;
1204    glDeleteTextures(1, &texID);
1205
1206    // Destroy the EGLSurface
1207    EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1208    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1209
1210    // Release the ref that the SurfaceTexture has on buffers[2].
1211    mST->abandon();
1212
1213    EXPECT_EQ(1, buffers[0]->getStrongCount());
1214    EXPECT_EQ(1, buffers[1]->getStrongCount());
1215
1216    // Depending on how lazily the GL driver dequeues buffers, we may end up
1217    // with either two or three total buffers.  If there are three, make sure
1218    // the last one was properly down-ref'd.
1219    if (buffers[2] != buffers[0]) {
1220        EXPECT_EQ(1, buffers[2]->getStrongCount());
1221    }
1222}
1223
1224TEST_F(SurfaceTextureGLToGLTest, EglDestroySurfaceAfterAbandonUnrefsBuffers) {
1225    sp<GraphicBuffer> buffers[3];
1226
1227    // This test requires async mode to run on a single thread.
1228    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1229            mProducerEglSurface, mProducerEglContext));
1230    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1231    EXPECT_TRUE(eglSwapInterval(mEglDisplay, 0));
1232    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1233
1234    for (int i = 0; i < 3; i++) {
1235        // Produce a frame
1236        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1237                mProducerEglSurface, mProducerEglContext));
1238        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1239        glClear(GL_COLOR_BUFFER_BIT);
1240        EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1241        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1242
1243        // Consume a frame
1244        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1245                mEglContext));
1246        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1247        ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1248        buffers[i] = mST->getCurrentBuffer();
1249    }
1250
1251    // Abandon the SurfaceTexture, releasing the ref that the SurfaceTexture has
1252    // on buffers[2].
1253    mST->abandon();
1254
1255    // Destroy the GL texture object to release its ref on buffers[2].
1256    GLuint texID = TEX_ID;
1257    glDeleteTextures(1, &texID);
1258
1259    // Destroy the EGLSurface.
1260    EXPECT_TRUE(eglDestroySurface(mEglDisplay, mProducerEglSurface));
1261    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1262
1263    EXPECT_EQ(1, buffers[0]->getStrongCount());
1264    EXPECT_EQ(1, buffers[1]->getStrongCount());
1265
1266    // Depending on how lazily the GL driver dequeues buffers, we may end up
1267    // with either two or three total buffers.  If there are three, make sure
1268    // the last one was properly down-ref'd.
1269    if (buffers[2] != buffers[0]) {
1270        EXPECT_EQ(1, buffers[2]->getStrongCount());
1271    }
1272}
1273
1274TEST_F(SurfaceTextureGLToGLTest, EglSurfaceDefaultsToSynchronousMode) {
1275    // This test requires 3 buffers to run on a single thread.
1276    mST->setBufferCountServer(3);
1277
1278    ASSERT_TRUE(mST->isSynchronousMode());
1279
1280    for (int i = 0; i < 10; i++) {
1281        // Produce a frame
1282        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mProducerEglSurface,
1283                mProducerEglSurface, mProducerEglContext));
1284        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1285        glClear(GL_COLOR_BUFFER_BIT);
1286        EXPECT_TRUE(eglSwapBuffers(mEglDisplay, mProducerEglSurface));
1287        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1288
1289        // Consume a frame
1290        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1291                mEglContext));
1292        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1293        ASSERT_EQ(NO_ERROR, mST->updateTexImage());
1294    }
1295
1296    ASSERT_TRUE(mST->isSynchronousMode());
1297}
1298
1299/*
1300 * This test fixture is for testing GL -> GL texture streaming from one thread
1301 * to another.  It contains functionality to create a producer thread that will
1302 * perform GL rendering to an ANativeWindow that feeds frames to a
1303 * SurfaceTexture.  Additionally it supports interlocking the producer and
1304 * consumer threads so that a specific sequence of calls can be
1305 * deterministically created by the test.
1306 *
1307 * The intended usage is as follows:
1308 *
1309 * TEST_F(...) {
1310 *     class PT : public ProducerThread {
1311 *         virtual void render() {
1312 *             ...
1313 *             swapBuffers();
1314 *         }
1315 *     };
1316 *
1317 *     runProducerThread(new PT());
1318 *
1319 *     // The order of these calls will vary from test to test and may include
1320 *     // multiple frames and additional operations (e.g. GL rendering from the
1321 *     // texture).
1322 *     fc->waitForFrame();
1323 *     mST->updateTexImage();
1324 *     fc->finishFrame();
1325 * }
1326 *
1327 */
1328class SurfaceTextureGLThreadToGLTest : public SurfaceTextureGLToGLTest {
1329protected:
1330
1331    // ProducerThread is an abstract base class to simplify the creation of
1332    // OpenGL ES frame producer threads.
1333    class ProducerThread : public Thread {
1334    public:
1335        virtual ~ProducerThread() {
1336        }
1337
1338        void setEglObjects(EGLDisplay producerEglDisplay,
1339                EGLSurface producerEglSurface,
1340                EGLContext producerEglContext) {
1341            mProducerEglDisplay = producerEglDisplay;
1342            mProducerEglSurface = producerEglSurface;
1343            mProducerEglContext = producerEglContext;
1344        }
1345
1346        virtual bool threadLoop() {
1347            eglMakeCurrent(mProducerEglDisplay, mProducerEglSurface,
1348                    mProducerEglSurface, mProducerEglContext);
1349            render();
1350            eglMakeCurrent(mProducerEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
1351                    EGL_NO_CONTEXT);
1352            return false;
1353        }
1354
1355    protected:
1356        virtual void render() = 0;
1357
1358        void swapBuffers() {
1359            eglSwapBuffers(mProducerEglDisplay, mProducerEglSurface);
1360        }
1361
1362        EGLDisplay mProducerEglDisplay;
1363        EGLSurface mProducerEglSurface;
1364        EGLContext mProducerEglContext;
1365    };
1366
1367    // FrameCondition is a utility class for interlocking between the producer
1368    // and consumer threads.  The FrameCondition object should be created and
1369    // destroyed in the consumer thread only.  The consumer thread should set
1370    // the FrameCondition as the FrameAvailableListener of the SurfaceTexture,
1371    // and should call both waitForFrame and finishFrame once for each expected
1372    // frame.
1373    //
1374    // This interlocking relies on the fact that onFrameAvailable gets called
1375    // synchronously from SurfaceTexture::queueBuffer.
1376    class FrameCondition : public SurfaceTexture::FrameAvailableListener {
1377    public:
1378        FrameCondition():
1379                mFrameAvailable(false),
1380                mFrameFinished(false) {
1381        }
1382
1383        // waitForFrame waits for the next frame to arrive.  This should be
1384        // called from the consumer thread once for every frame expected by the
1385        // test.
1386        void waitForFrame() {
1387            Mutex::Autolock lock(mMutex);
1388            ALOGV("+waitForFrame");
1389            while (!mFrameAvailable) {
1390                mFrameAvailableCondition.wait(mMutex);
1391            }
1392            mFrameAvailable = false;
1393            ALOGV("-waitForFrame");
1394        }
1395
1396        // Allow the producer to return from its swapBuffers call and continue
1397        // on to produce the next frame.  This should be called by the consumer
1398        // thread once for every frame expected by the test.
1399        void finishFrame() {
1400            Mutex::Autolock lock(mMutex);
1401            ALOGV("+finishFrame");
1402            mFrameFinished = true;
1403            mFrameFinishCondition.signal();
1404            ALOGV("-finishFrame");
1405        }
1406
1407        // This should be called by SurfaceTexture on the producer thread.
1408        virtual void onFrameAvailable() {
1409            Mutex::Autolock lock(mMutex);
1410            ALOGV("+onFrameAvailable");
1411            mFrameAvailable = true;
1412            mFrameAvailableCondition.signal();
1413            while (!mFrameFinished) {
1414                mFrameFinishCondition.wait(mMutex);
1415            }
1416            mFrameFinished = false;
1417            ALOGV("-onFrameAvailable");
1418        }
1419
1420    protected:
1421        bool mFrameAvailable;
1422        bool mFrameFinished;
1423
1424        Mutex mMutex;
1425        Condition mFrameAvailableCondition;
1426        Condition mFrameFinishCondition;
1427    };
1428
1429    virtual void SetUp() {
1430        SurfaceTextureGLToGLTest::SetUp();
1431        mFC = new FrameCondition();
1432        mST->setFrameAvailableListener(mFC);
1433    }
1434
1435    virtual void TearDown() {
1436        if (mProducerThread != NULL) {
1437            mProducerThread->requestExitAndWait();
1438        }
1439        mProducerThread.clear();
1440        mFC.clear();
1441        SurfaceTextureGLToGLTest::TearDown();
1442    }
1443
1444    void runProducerThread(const sp<ProducerThread> producerThread) {
1445        ASSERT_TRUE(mProducerThread == NULL);
1446        mProducerThread = producerThread;
1447        producerThread->setEglObjects(mEglDisplay, mProducerEglSurface,
1448                mProducerEglContext);
1449        producerThread->run();
1450    }
1451
1452    sp<ProducerThread> mProducerThread;
1453    sp<FrameCondition> mFC;
1454};
1455
1456TEST_F(SurfaceTextureGLThreadToGLTest,
1457        UpdateTexImageBeforeFrameFinishedCompletes) {
1458    class PT : public ProducerThread {
1459        virtual void render() {
1460            glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1461            glClear(GL_COLOR_BUFFER_BIT);
1462            swapBuffers();
1463        }
1464    };
1465
1466    runProducerThread(new PT());
1467
1468    mFC->waitForFrame();
1469    mST->updateTexImage();
1470    mFC->finishFrame();
1471
1472    // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1473}
1474
1475TEST_F(SurfaceTextureGLThreadToGLTest,
1476        UpdateTexImageAfterFrameFinishedCompletes) {
1477    class PT : public ProducerThread {
1478        virtual void render() {
1479            glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1480            glClear(GL_COLOR_BUFFER_BIT);
1481            swapBuffers();
1482        }
1483    };
1484
1485    runProducerThread(new PT());
1486
1487    mFC->waitForFrame();
1488    mFC->finishFrame();
1489    mST->updateTexImage();
1490
1491    // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1492}
1493
1494TEST_F(SurfaceTextureGLThreadToGLTest,
1495        RepeatedUpdateTexImageBeforeFrameFinishedCompletes) {
1496    enum { NUM_ITERATIONS = 1024 };
1497
1498    class PT : public ProducerThread {
1499        virtual void render() {
1500            for (int i = 0; i < NUM_ITERATIONS; i++) {
1501                glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1502                glClear(GL_COLOR_BUFFER_BIT);
1503                ALOGV("+swapBuffers");
1504                swapBuffers();
1505                ALOGV("-swapBuffers");
1506            }
1507        }
1508    };
1509
1510    runProducerThread(new PT());
1511
1512    for (int i = 0; i < NUM_ITERATIONS; i++) {
1513        mFC->waitForFrame();
1514        ALOGV("+updateTexImage");
1515        mST->updateTexImage();
1516        ALOGV("-updateTexImage");
1517        mFC->finishFrame();
1518
1519        // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1520    }
1521}
1522
1523TEST_F(SurfaceTextureGLThreadToGLTest,
1524        RepeatedUpdateTexImageAfterFrameFinishedCompletes) {
1525    enum { NUM_ITERATIONS = 1024 };
1526
1527    class PT : public ProducerThread {
1528        virtual void render() {
1529            for (int i = 0; i < NUM_ITERATIONS; i++) {
1530                glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1531                glClear(GL_COLOR_BUFFER_BIT);
1532                ALOGV("+swapBuffers");
1533                swapBuffers();
1534                ALOGV("-swapBuffers");
1535            }
1536        }
1537    };
1538
1539    runProducerThread(new PT());
1540
1541    for (int i = 0; i < NUM_ITERATIONS; i++) {
1542        mFC->waitForFrame();
1543        mFC->finishFrame();
1544        ALOGV("+updateTexImage");
1545        mST->updateTexImage();
1546        ALOGV("-updateTexImage");
1547
1548        // TODO: Add frame verification once RGB TEX_EXTERNAL_OES is supported!
1549    }
1550}
1551
1552// XXX: This test is disabled because it is currently hanging on some devices.
1553TEST_F(SurfaceTextureGLThreadToGLTest,
1554        DISABLED_RepeatedSwapBuffersWhileDequeueStalledCompletes) {
1555    enum { NUM_ITERATIONS = 64 };
1556
1557    class PT : public ProducerThread {
1558        virtual void render() {
1559            for (int i = 0; i < NUM_ITERATIONS; i++) {
1560                glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
1561                glClear(GL_COLOR_BUFFER_BIT);
1562                ALOGV("+swapBuffers");
1563                swapBuffers();
1564                ALOGV("-swapBuffers");
1565            }
1566        }
1567    };
1568
1569    ASSERT_EQ(OK, mST->setSynchronousMode(true));
1570    ASSERT_EQ(OK, mST->setBufferCountServer(2));
1571
1572    runProducerThread(new PT());
1573
1574    // Allow three frames to be rendered and queued before starting the
1575    // rendering in this thread.  For the latter two frames we don't call
1576    // updateTexImage so the next dequeue from the producer thread will block
1577    // waiting for a frame to become available.
1578    mFC->waitForFrame();
1579    mFC->finishFrame();
1580
1581    // We must call updateTexImage to consume the first frame so that the
1582    // SurfaceTexture is able to reduce the buffer count to 2.  This is because
1583    // the GL driver may dequeue a buffer when the EGLSurface is created, and
1584    // that happens before we call setBufferCountServer.  It's possible that the
1585    // driver does not dequeue a buffer at EGLSurface creation time, so we
1586    // cannot rely on this to cause the second dequeueBuffer call to block.
1587    mST->updateTexImage();
1588
1589    mFC->waitForFrame();
1590    mFC->finishFrame();
1591    mFC->waitForFrame();
1592    mFC->finishFrame();
1593
1594    // Sleep for 100ms to allow the producer thread's dequeueBuffer call to
1595    // block waiting for a buffer to become available.
1596    usleep(100000);
1597
1598    // Render and present a number of images.  This thread should not be blocked
1599    // by the fact that the producer thread is blocking in dequeue.
1600    for (int i = 0; i < NUM_ITERATIONS; i++) {
1601        glClear(GL_COLOR_BUFFER_BIT);
1602        eglSwapBuffers(mEglDisplay, mEglSurface);
1603    }
1604
1605    // Consume the two pending buffers to unblock the producer thread.
1606    mST->updateTexImage();
1607    mST->updateTexImage();
1608
1609    // Consume the remaining buffers from the producer thread.
1610    for (int i = 0; i < NUM_ITERATIONS-3; i++) {
1611        mFC->waitForFrame();
1612        mFC->finishFrame();
1613        ALOGV("+updateTexImage");
1614        mST->updateTexImage();
1615        ALOGV("-updateTexImage");
1616    }
1617}
1618
1619} // namespace android
1620