SurfaceMediaSource_test.cpp revision abf0610a8cea021548f2909e1d47d656206f641c
13399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi/*
23399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * Copyright (C) 2011 The Android Open Source Project
33399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi *
43399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * Licensed under the Apache License, Version 2.0 (the "License");
53399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * you may not use this file except in compliance with the License.
63399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * You may obtain a copy of the License at
73399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi *
83399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi *      http://www.apache.org/licenses/LICENSE-2.0
93399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi *
103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * Unless required by applicable law or agreed to in writing, software
113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * distributed under the License is distributed on an "AS IS" BASIS,
123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * See the License for the specific language governing permissions and
143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi * limitations under the License.
153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi */
163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// #define LOG_NDEBUG 0
180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi#define LOG_TAG "SurfaceMediaSource_test"
193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <gtest/gtest.h>
213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <utils/String8.h>
223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <utils/Errors.h>
230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi#include <fcntl.h>
240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi#include <unistd.h>
253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
261a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi#include <media/stagefright/SurfaceMediaSource.h>
270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi#include <media/mediarecorder.h>
283399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
293399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <ui/GraphicBuffer.h>
30df712ea86e6350f7005a02ab0e1c60c28a343ed0Mathias Agopian#include <gui/SurfaceTextureClient.h>
31df712ea86e6350f7005a02ab0e1c60c28a343ed0Mathias Agopian#include <gui/ISurfaceComposer.h>
32df712ea86e6350f7005a02ab0e1c60c28a343ed0Mathias Agopian#include <gui/Surface.h>
33df712ea86e6350f7005a02ab0e1c60c28a343ed0Mathias Agopian#include <gui/SurfaceComposerClient.h>
343399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
353399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <binder/ProcessState.h>
363399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <ui/FramebufferNativeWindow.h>
373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
38f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong#include <media/stagefright/foundation/ADebug.h>
390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi#include <media/stagefright/MediaBufferGroup.h>
403399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/MediaDefs.h>
413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/MetaData.h>
423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/OMXClient.h>
433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/OMXCodec.h>
443399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <OMX_Component.h>
453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include "DummyRecorder.h"
473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketinamespace android {
503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketiclass GLTest : public ::testing::Test {
520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketiprotected:
530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    GLTest():
550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglDisplay(EGL_NO_DISPLAY),
560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglSurface(EGL_NO_SURFACE),
570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglContext(EGL_NO_CONTEXT) {
580c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual void SetUp() {
613856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("GLTest::SetUp()");
620c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
650c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGLint majorVersion;
670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGLint minorVersion;
680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        RecordProperty("EglVersionMajor", majorVersion);
710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        RecordProperty("EglVersionMajor", minorVersion);
720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGLint numConfigs = 0;
740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                1, &numConfigs));
760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (displaySecsEnv != NULL) {
800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mDisplaySecs = atoi(displaySecsEnv);
810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (mDisplaySecs < 0) {
820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                mDisplaySecs = 0;
830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        } else {
850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mDisplaySecs = 0;
860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mDisplaySecs > 0) {
890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mComposerClient = new SurfaceComposerClient;
900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mSurfaceControl = mComposerClient->createSurface(
930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    String8("Test Surface"), 0,
940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    getSurfaceWidth(), getSurfaceHeight(),
950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    PIXEL_FORMAT_RGB_888, 0);
960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_TRUE(mSurfaceControl != NULL);
980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_TRUE(mSurfaceControl->isValid());
990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            SurfaceComposerClient::openGlobalTransaction();
1010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
1020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
1030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            SurfaceComposerClient::closeGlobalTransaction();
1040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            sp<ANativeWindow> window = mSurfaceControl->getSurface();
1060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
1070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    window.get(), NULL);
1080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        } else {
1093856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("No actual display. Choosing EGLSurface based on SurfaceMediaSource");
110abf0610a8cea021548f2909e1d47d656206f641cDaniel Lam            sp<ISurfaceTexture> sms = new SurfaceMediaSource(
11121fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi                    getSurfaceWidth(), getSurfaceHeight());
11221fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi            sp<SurfaceTextureClient> stc = new SurfaceTextureClient(sms);
11321fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi            sp<ANativeWindow> window = stc;
1140c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
11521fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi            mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
11621fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi                    window.get(), NULL);
1170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
1200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
1220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                getContextAttribs());
1230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
1250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                mEglContext));
1280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGLint w, h;
1310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
1320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
1340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        RecordProperty("EglSurfaceWidth", w);
1360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        RecordProperty("EglSurfaceHeight", h);
1370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        glViewport(0, 0, w, h);
1390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
1400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual void TearDown() {
1430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        // Display the result
1440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
1450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            eglSwapBuffers(mEglDisplay, mEglSurface);
1460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            sleep(mDisplaySecs);
1470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mComposerClient != NULL) {
1500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mComposerClient->dispose();
1510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mEglContext != EGL_NO_CONTEXT) {
1530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            eglDestroyContext(mEglDisplay, mEglContext);
1540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mEglSurface != EGL_NO_SURFACE) {
1560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            eglDestroySurface(mEglDisplay, mEglSurface);
1570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1580c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mEglDisplay != EGL_NO_DISPLAY) {
1590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            eglTerminate(mEglDisplay);
1600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1610c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1620c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint const* getConfigAttribs() {
1653856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("GLTest getConfigAttribs");
1660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        static EGLint sDefaultConfigAttribs[] = {
1670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
1680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_RED_SIZE, 8,
1700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_GREEN_SIZE, 8,
1710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_BLUE_SIZE, 8,
1720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_ALPHA_SIZE, 8,
1730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_DEPTH_SIZE, 16,
1740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_STENCIL_SIZE, 8,
1750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_NONE };
1760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return sDefaultConfigAttribs;
1780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint const* getContextAttribs() {
1810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        static EGLint sDefaultContextAttribs[] = {
1820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_CONTEXT_CLIENT_VERSION, 2,
1830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_NONE };
1840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return sDefaultContextAttribs;
1860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint getSurfaceWidth() {
1890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return 512;
1900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint getSurfaceHeight() {
1930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return 512;
1940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void loadShader(GLenum shaderType, const char* pSource, GLuint* outShader) {
1970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLuint shader = glCreateShader(shaderType);
1980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
1990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (shader) {
2000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glShaderSource(shader, 1, &pSource, NULL);
2010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glCompileShader(shader);
2030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            GLint compiled = 0;
2050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
2060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (!compiled) {
2080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                GLint infoLen = 0;
2090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
2100c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2110c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                if (infoLen) {
2120c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    char* buf = (char*) malloc(infoLen);
2130c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    if (buf) {
2140c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        glGetShaderInfoLog(shader, infoLen, NULL, buf);
2150c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        printf("Shader compile log:\n%s\n", buf);
2160c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        free(buf);
2170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        FAIL();
2180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    }
2190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                } else {
2200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    char* buf = (char*) malloc(0x1000);
2210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    if (buf) {
2220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        glGetShaderInfoLog(shader, 0x1000, NULL, buf);
2230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        printf("Shader compile log:\n%s\n", buf);
2240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        free(buf);
2250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        FAIL();
2260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    }
2270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                }
2280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                glDeleteShader(shader);
2290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                shader = 0;
2300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
2310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
2320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_TRUE(shader != 0);
2330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        *outShader = shader;
2340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
2350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
2360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void createProgram(const char* pVertexSource, const char* pFragmentSource,
2370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            GLuint* outPgm) {
2380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLuint vertexShader, fragmentShader;
2390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        {
2400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            SCOPED_TRACE("compiling vertex shader");
2410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            loadShader(GL_VERTEX_SHADER, pVertexSource, &vertexShader);
2420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (HasFatalFailure()) {
2430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                return;
2440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
2450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
2460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        {
2470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            SCOPED_TRACE("compiling fragment shader");
2480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            loadShader(GL_FRAGMENT_SHADER, pFragmentSource, &fragmentShader);
2490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (HasFatalFailure()) {
2500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                return;
2510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
2520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
2530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
2540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLuint program = glCreateProgram();
2550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (program) {
2570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glAttachShader(program, vertexShader);
2580c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glAttachShader(program, fragmentShader);
2600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2610c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glLinkProgram(program);
2620c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            GLint linkStatus = GL_FALSE;
2630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
2640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (linkStatus != GL_TRUE) {
2650c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                GLint bufLength = 0;
2660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
2670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                if (bufLength) {
2680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    char* buf = (char*) malloc(bufLength);
2690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    if (buf) {
2700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        glGetProgramInfoLog(program, bufLength, NULL, buf);
2710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        printf("Program link log:\n%s\n", buf);
2720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        free(buf);
2730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        FAIL();
2740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    }
2750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                }
2760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                glDeleteProgram(program);
2770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                program = 0;
2780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
2790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
2800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        glDeleteShader(vertexShader);
2810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        glDeleteShader(fragmentShader);
2820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_TRUE(program != 0);
2830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        *outPgm = program;
2840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
2853399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    static int abs(int value) {
2870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return value > 0 ? value : -value;
2880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
2890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
2900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ::testing::AssertionResult checkPixel(int x, int y, int r,
2910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            int g, int b, int a, int tolerance=2) {
2920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLubyte pixel[4];
2930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        String8 msg;
2940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
2950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLenum err = glGetError();
2960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (err != GL_NO_ERROR) {
2970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("error reading pixel: %#x", err);
2980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            while ((err = glGetError()) != GL_NO_ERROR) {
2990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                msg += String8::format(", %#x", err);
3000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
3010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            fprintf(stderr, "pixel check failure: %s\n", msg.string());
3020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            return ::testing::AssertionFailure(
3030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    ::testing::Message(msg.string()));
3040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
3060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("r(%d isn't %d)", pixel[0], r);
3070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
3090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (!msg.isEmpty()) {
3100c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                msg += " ";
3110c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
3120c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("g(%d isn't %d)", pixel[1], g);
3130c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3140c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
3150c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (!msg.isEmpty()) {
3160c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                msg += " ";
3170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
3180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("b(%d isn't %d)", pixel[2], b);
3190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
3210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (!msg.isEmpty()) {
3220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                msg += " ";
3230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
3240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("a(%d isn't %d)", pixel[3], a);
3250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (!msg.isEmpty()) {
3270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            fprintf(stderr, "pixel check failure: %s\n", msg.string());
3280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            return ::testing::AssertionFailure(
3290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    ::testing::Message(msg.string()));
3300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        } else {
3310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            return ::testing::AssertionSuccess();
3320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
3340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int mDisplaySecs;
3360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<SurfaceComposerClient> mComposerClient;
3370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<SurfaceControl> mSurfaceControl;
3380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EGLDisplay mEglDisplay;
3400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EGLSurface mEglSurface;
3410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EGLContext mEglContext;
3420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EGLConfig  mGlConfig;
3430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi};
3440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi///////////////////////////////////////////////////////////////////////
3460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi//    Class for  the NON-GL tests
3470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi///////////////////////////////////////////////////////////////////////
3481a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketiclass SurfaceMediaSourceTest : public ::testing::Test {
3493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketipublic:
3503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    SurfaceMediaSourceTest( ): mYuvTexWidth(176), mYuvTexHeight(144) { }
3523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    void oneBufferPass(int width, int height );
3530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void oneBufferPassNoFill(int width, int height );
3543399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    static void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) ;
3553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    static void fillYV12BufferRect(uint8_t* buf, int w, int h,
3563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                        int stride, const android_native_rect_t& rect) ;
3573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketiprotected:
3583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual void SetUp() {
360b6f2fced47866a5d095b2b18d2e28d7c4321e3a6James Dong        android::ProcessState::self()->startThreadPool();
3611a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi        mSMS = new SurfaceMediaSource(mYuvTexWidth, mYuvTexHeight);
3621a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi        mSMS->setSynchronousMode(true);
363abf0610a8cea021548f2909e1d47d656206f641cDaniel Lam        // Manual cast is required to avoid constructor ambiguity
364abf0610a8cea021548f2909e1d47d656206f641cDaniel Lam        mSTC = new SurfaceTextureClient(static_cast<sp<ISurfaceTexture> >( mSMS));
3653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        mANW = mSTC;
3660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
3673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual void TearDown() {
3690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mSMS.clear();
3700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mSTC.clear();
3710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mANW.clear();
3723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
3733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const int mYuvTexWidth;
3750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const int mYuvTexHeight;
3760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<SurfaceMediaSource> mSMS;
3780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<SurfaceTextureClient> mSTC;
3790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<ANativeWindow> mANW;
3800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi};
3810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi///////////////////////////////////////////////////////////////////////
3830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi//    Class for  the GL tests
3840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi///////////////////////////////////////////////////////////////////////
3850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketiclass SurfaceMediaSourceGLTest : public GLTest {
3860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketipublic:
3870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    SurfaceMediaSourceGLTest( ): mYuvTexWidth(176), mYuvTexHeight(144) { }
3890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint const* getConfigAttribs();
3900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void oneBufferPassGL(int num = 0);
3910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    static sp<MediaRecorder> setUpMediaRecorder(int fileDescriptor, int videoSource,
3920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        int outputFormat, int videoEncoder, int width, int height, int fps);
3930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketiprotected:
3940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual void SetUp() {
3963856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("SMS-GLTest::SetUp()");
3970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        android::ProcessState::self()->startThreadPool();
3980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mSMS = new SurfaceMediaSource(mYuvTexWidth, mYuvTexHeight);
399abf0610a8cea021548f2909e1d47d656206f641cDaniel Lam        mSTC = new SurfaceTextureClient(static_cast<sp<ISurfaceTexture> >( mSMS));
4000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mANW = mSTC;
4010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        // Doing the setup related to the GL Side
4030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLTest::SetUp();
4040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
4053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
4063399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual void TearDown() {
4071a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi        mSMS.clear();
4083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        mSTC.clear();
4093399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        mANW.clear();
4100c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLTest::TearDown();
4113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
4123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
4130c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void setUpEGLSurfaceFromMediaRecorder(sp<MediaRecorder>& mr);
4140c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
415a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi    const int mYuvTexWidth;
416a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi    const int mYuvTexHeight;
4173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
4181a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    sp<SurfaceMediaSource> mSMS;
4193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    sp<SurfaceTextureClient> mSTC;
4203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    sp<ANativeWindow> mANW;
4213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi};
4223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
4230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
4240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Methods in SurfaceMediaSourceGLTest
4250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
4260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiEGLint const* SurfaceMediaSourceGLTest::getConfigAttribs() {
4273856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("SurfaceMediaSourceGLTest getConfigAttribs");
4280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    static EGLint sDefaultConfigAttribs[] = {
4290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
4300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
4310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_RED_SIZE, 8,
4320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_GREEN_SIZE, 8,
4330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_BLUE_SIZE, 8,
4340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_RECORDABLE_ANDROID, EGL_TRUE,
4350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_NONE };
4360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    return sDefaultConfigAttribs;
4380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
4390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// One pass of dequeuing and queuing a GLBuffer
4410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketivoid SurfaceMediaSourceGLTest::oneBufferPassGL(int num) {
4420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int d = num % 50;
4430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    float f = 0.2f; // 0.1f * d;
4440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(0, 0.3, 0, 0.6);
4460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
4470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glEnable(GL_SCISSOR_TEST);
4490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(4 + d, 4 + d, 4, 4);
4500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(1.0 - f, f, f, 1.0);
4510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
4520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(24 + d, 48 + d, 4, 4);
4540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(f, 1.0 - f, f, 1.0);
4550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
4560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(37 + d, 17 + d, 4, 4);
4580c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(f, f, 1.0 - f, 1.0);
4590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
4600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4610c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // The following call dequeues and queues the buffer
46221fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    eglSwapBuffers(mEglDisplay, mEglSurface);
46331292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
4640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glDisable(GL_SCISSOR_TEST);
4650c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
4660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Set up the MediaRecorder which runs in the same process as mediaserver
4680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketisp<MediaRecorder> SurfaceMediaSourceGLTest::setUpMediaRecorder(int fd, int videoSource,
4690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        int outputFormat, int videoEncoder, int width, int height, int fps) {
4700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<MediaRecorder> mr = new MediaRecorder();
4710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setVideoSource(videoSource);
4720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setOutputFormat(outputFormat);
4730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setVideoEncoder(videoEncoder);
4740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setOutputFile(fd, 0, 0);
4750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setVideoSize(width, height);
4760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setVideoFrameRate(fps);
4770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->prepare();
4783856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Starting MediaRecorder...");
479f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong    CHECK_EQ((status_t)OK, mr->start());
4800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    return mr;
4810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
4820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// query the mediarecorder for a surfacemeidasource and create an egl surface with that
4840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketivoid SurfaceMediaSourceGLTest::setUpEGLSurfaceFromMediaRecorder(sp<MediaRecorder>& mr) {
4850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<ISurfaceTexture> iST = mr->querySurfaceMediaSourceFromMediaServer();
4860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mSTC = new SurfaceTextureClient(iST);
4870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mANW = mSTC;
4880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
48921fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
4900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                                mANW.get(), NULL);
4910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
49221fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    ASSERT_NE(EGL_NO_SURFACE, mEglSurface) ;
4930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
49421fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
4950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglContext));
4960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
4970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
4980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
5000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
5010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Methods in SurfaceMediaSourceTest
5020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
5030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
5040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// One pass of dequeuing and queuing the buffer. Fill it in with
5050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// cpu YV12 buffer
5061a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketivoid SurfaceMediaSourceTest::oneBufferPass(int width, int height ) {
5073399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ANativeWindowBuffer* anb;
5083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
5093399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ASSERT_TRUE(anb != NULL);
5103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
5123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
5133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Fill the buffer with the a checkerboard pattern
5153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    uint8_t* img = NULL;
5163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
5171a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    SurfaceMediaSourceTest::fillYV12Buffer(img, width, height, buf->getStride());
5183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    buf->unlock();
5193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
5213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
5223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dequeuing and queuing the buffer without really filling it in.
5240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketivoid SurfaceMediaSourceTest::oneBufferPassNoFill(int width, int height ) {
5250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ANativeWindowBuffer* anb;
5260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
5270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_TRUE(anb != NULL);
5283399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
5300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // ASSERT_EQ(NO_ERROR, mANW->lockBuffer(mANW.get(), buf->getNativeBuffer()));
5310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // We do not fill the buffer in. Just queue it back.
5320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer()));
5333399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
5343399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5353399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// Fill a YV12 buffer with a multi-colored checkerboard pattern
5361a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketivoid SurfaceMediaSourceTest::fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
5373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    const int blockWidth = w > 16 ? w / 16 : 1;
5383399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    const int blockHeight = h > 16 ? h / 16 : 1;
5393399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    const int yuvTexOffsetY = 0;
5403399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideY = stride;
5413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexOffsetV = yuvTexStrideY * h;
5423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
5433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
5443399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideU = yuvTexStrideV;
5453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    for (int x = 0; x < w; x++) {
5463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        for (int y = 0; y < h; y++) {
5473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            int parityX = (x / blockWidth) & 1;
5483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            int parityY = (y / blockHeight) & 1;
5493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
5503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
5513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            if (x < w / 2 && y < h / 2) {
5523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
5533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                if (x * 2 < w / 2 && y * 2 < h / 2) {
5543399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
5553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
5563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
5573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
5583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                        intensity;
5593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                }
5603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            }
5613399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        }
5623399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
5633399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
5643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// Fill a YV12 buffer with red outside a given rectangle and green inside it.
5661a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketivoid SurfaceMediaSourceTest::fillYV12BufferRect(uint8_t* buf, int w,
5673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                  int h, int stride, const android_native_rect_t& rect) {
5683399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    const int yuvTexOffsetY = 0;
5693399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideY = stride;
5703399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexOffsetV = yuvTexStrideY * h;
5713399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
5723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
5733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideU = yuvTexStrideV;
5743399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    for (int x = 0; x < w; x++) {
5753399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        for (int y = 0; y < h; y++) {
5763399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            bool inside = rect.left <= x && x < rect.right &&
5773399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    rect.top <= y && y < rect.bottom;
5783399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
5793399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            if (x < w / 2 && y < h / 2) {
5803399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                bool inside = rect.left <= 2*x && 2*x < rect.right &&
5813399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                        rect.top <= 2*y && 2*y < rect.bottom;
5823399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
5833399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
5843399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                                                inside ? 16 : 255;
5853399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            }
5863399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        }
5873399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
5881a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi}  ///////// End of class SurfaceMediaSourceTest
5893399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5903399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi///////////////////////////////////////////////////////////////////
5913399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// Class to imitate the recording     /////////////////////////////
5923399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// ////////////////////////////////////////////////////////////////
5933399b7267185646c69b04352211fca4fad9d7547Pannag Sanketistruct SimpleDummyRecorder {
5943399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        sp<MediaSource> mSource;
5953399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5963399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        SimpleDummyRecorder
5973399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                (const sp<MediaSource> &source): mSource(source) {}
5983399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5993399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        status_t start() { return mSource->start();}
6003399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        status_t stop()  { return mSource->stop();}
6013399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6023399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // fakes reading from a media source
6033399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        status_t readFromSource() {
6043399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            MediaBuffer *buffer;
6053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            status_t err = mSource->read(&buffer);
6063399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            if (err != OK) {
6073399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                return err;
6083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            }
6093399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            buffer->release();
6103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            buffer = NULL;
6113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            return OK;
6123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        }
6133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi};
6143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi///////////////////////////////////////////////////////////////////
6153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi//           TESTS
6160c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// SurfaceMediaSourceTest class contains tests that fill the buffers
6170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// using the cpu calls
6180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// SurfaceMediaSourceGLTest class contains tests that fill the buffers
6190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// using the GL calls.
6200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// TODO: None of the tests actually verify the encoded images.. so at this point,
6210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// these are mostly functionality tests + visual inspection
6220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi//////////////////////////////////////////////////////////////////////
6230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
6241a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi// Just pass one buffer from the native_window to the SurfaceMediaSource
6250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dummy Encoder
6260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketistatic int testId = 1;
6270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest, DISABLED_DummyEncodingFromCpuFilledYV12BufferNpotOneBufferPass) {
6283856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
6293856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing OneBufferPass ******************************");
6303399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
6320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
6333399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    oneBufferPass(mYuvTexWidth, mYuvTexHeight);
6343399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
6353399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6363399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// Pass the buffer with the wrong height and weight and should not be accepted
6370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dummy Encoder
6380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest, DISABLED_DummyEncodingFromCpuFilledYV12BufferNpotWrongSizeBufferPass) {
6393856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
6403856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing Wrong size BufferPass ******************************");
6413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // setting the client side buffer size different than the server size
6430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_dimensions(mANW.get(),
6440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi             10, 10));
6450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
6460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
6473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ANativeWindowBuffer* anb;
6493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // Note: make sure we get an ERROR back when dequeuing!
6513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ASSERT_NE(NO_ERROR, mANW->dequeueBuffer(mANW.get(), &anb));
6523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
6533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6541a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi// pass multiple buffers from the native_window the SurfaceMediaSource
6550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dummy Encoder
6560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest,  DISABLED_DummyEncodingFromCpuFilledYV12BufferNpotMultiBufferPass) {
6573856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
6583856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing MultiBufferPass, Dummy Recorder *********************");
6590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
6600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
661a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi
6621a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    SimpleDummyRecorder writer(mSMS);
6633399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.start();
6643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int32_t nFramesCount = 0;
6663399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    while (nFramesCount < 300) {
6673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        oneBufferPass(mYuvTexWidth, mYuvTexHeight);
6683399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6693399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        ASSERT_EQ(NO_ERROR, writer.readFromSource());
6703399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6713399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        nFramesCount++;
6723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
6733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.stop();
6743399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
6753399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6761a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi// Delayed pass of multiple buffers from the native_window the SurfaceMediaSource
6770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dummy Encoder
6780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest,  DISABLED_DummyLagEncodingFromCpuFilledYV12BufferNpotMultiBufferPass) {
6793856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
6803856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing MultiBufferPass, Dummy Recorder Lagging **************");
681a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi
6820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
6830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
684a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi
6851a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    SimpleDummyRecorder writer(mSMS);
6863399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.start();
6873399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6883399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int32_t nFramesCount = 1;
6891a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    const int FRAMES_LAG = mSMS->getBufferCount() - 1;
6903399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    while (nFramesCount <= 300) {
6913399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        oneBufferPass(mYuvTexWidth, mYuvTexHeight);
6923399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // Forcing the writer to lag behind a few frames
6933399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        if (nFramesCount > FRAMES_LAG) {
6943399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            ASSERT_EQ(NO_ERROR, writer.readFromSource());
6953399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        }
6963399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        nFramesCount++;
6973399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
6983399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.stop();
6993399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
7003399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7011a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi// pass multiple buffers from the native_window the SurfaceMediaSource
7023399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// A dummy writer (MULTITHREADED) is used to simulate actual MPEG4Writer
7030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest, DISABLED_DummyThreadedEncodingFromCpuFilledYV12BufferNpotMultiBufferPass) {
7043856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
7053856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing MultiBufferPass, Dummy Recorder Multi-Threaded **********");
7060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
7070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
7083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7091a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    DummyRecorder writer(mSMS);
7103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.start();
7113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int32_t nFramesCount = 0;
7133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    while (nFramesCount <= 300) {
7143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        oneBufferPass(mYuvTexWidth, mYuvTexHeight);
7153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        nFramesCount++;
7173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
7183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.stop();
7193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
7203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine actual encoding using mediarecorder
7220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// We use the mediaserver to create a mediarecorder and send
7230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// it back to us. So SurfaceMediaSource lives in the same process
7240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// as the mediaserver.
7250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Very close to the actual camera, except that the
7260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// buffers are filled and queueud by the CPU instead of GL.
7270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest, DISABLED_EncodingFromCpuYV12BufferNpotWriteMediaServer) {
7283856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
7293856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** Testing the whole pipeline with actual MediaRecorder ***********");
7303856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** SurfaceMediaSource is same process as mediaserver    ***********");
7310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const char *fileName = "/sdcard/outputSurfEncMSource.mp4";
7330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int fd = open(fileName, O_RDWR | O_CREAT, 0744);
7340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    if (fd < 0) {
73529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("ERROR: Could not open the the file %s, fd = %d !!", fileName, fd);
7360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
7370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    CHECK(fd >= 0);
7380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<MediaRecorder> mr = SurfaceMediaSourceGLTest::setUpMediaRecorder(fd,
7400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            VIDEO_SOURCE_GRALLOC_BUFFER,
7410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            OUTPUT_FORMAT_MPEG_4, VIDEO_ENCODER_H264, mYuvTexWidth,
7420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mYuvTexHeight, 30);
7430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // get the reference to the surfacemediasource living in
7440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // mediaserver that is created by stagefrightrecorder
7450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<ISurfaceTexture> iST = mr->querySurfaceMediaSourceFromMediaServer();
7460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mSTC = new SurfaceTextureClient(iST);
7470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mANW = mSTC;
7480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
7490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
7500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                                                HAL_PIXEL_FORMAT_YV12));
7513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int32_t nFramesCount = 0;
7533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    while (nFramesCount <= 300) {
7540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        oneBufferPassNoFill(mYuvTexWidth, mYuvTexHeight);
7553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        nFramesCount++;
7563856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("framesCount = %d", nFramesCount);
7573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
7583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU));
7603856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Stopping MediaRecorder...");
761f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong    CHECK_EQ((status_t)OK, mr->stop());
7620c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr.clear();
7630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    close(fd);
7643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
7653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi//////////////////////////////////////////////////////////////////////
7670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// GL tests
7680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
7690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine whether we can choose the Recordable Android GLConfig
7710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// DummyRecorder used- no real encoding here
77221fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag SanketiTEST_F(SurfaceMediaSourceGLTest, ChooseAndroidRecordableEGLConfigDummyWriter) {
7733856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
7743856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Verify creating a surface w/ right config + dummy writer*********");
7750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mSMS = new SurfaceMediaSource(mYuvTexWidth, mYuvTexHeight);
777abf0610a8cea021548f2909e1d47d656206f641cDaniel Lam    mSTC = new SurfaceTextureClient(static_cast<sp<ISurfaceTexture> >( mSMS));
7780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mANW = mSTC;
7790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    DummyRecorder writer(mSMS);
7810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    writer.start();
7820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
78321fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
7840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                                mANW.get(), NULL);
7850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
78621fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    ASSERT_NE(EGL_NO_SURFACE, mEglSurface) ;
7870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
78821fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
7890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglContext));
7900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
7913399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int32_t nFramesCount = 0;
7930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    while (nFramesCount <= 300) {
7940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        oneBufferPassGL();
7950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        nFramesCount++;
7963856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("framesCount = %d", nFramesCount);
7970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
7980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
79931292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
80031292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi            EGL_NO_CONTEXT));
80131292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
80231292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    eglDestroySurface(mEglDisplay, mEglSurface);
80331292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    mEglSurface = EGL_NO_SURFACE;
80431292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi
8050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    writer.stop();
8060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
8070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine whether we can render GL buffers in to the surface
8080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// created with the native window handle
8090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceGLTest, RenderingToRecordableEGLSurfaceWorks) {
8103856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
8113856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("RenderingToRecordableEGLSurfaceWorks *********************");
8120c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // Do the producer side of things
8130c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(0.6, 0.6, 0.6, 0.6);
8140c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
8150c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8160c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glEnable(GL_SCISSOR_TEST);
8170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(4, 4, 4, 4);
8180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(1.0, 0.0, 0.0, 1.0);
8190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
8200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(24, 48, 4, 4);
8220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(0.0, 1.0, 0.0, 1.0);
8230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
8240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(37, 17, 4, 4);
8260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(0.0, 0.0, 1.0, 1.0);
8270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
8280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
8300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
8310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
8320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
8330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 4,  7, 255,   0,   0, 255));
8350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(25, 51,   0, 255,   0, 255));
8360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(40, 19,   0,   0, 255, 255));
8370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
8380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
8390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(13,  8, 153, 153, 153, 153));
8400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(46,  3, 153, 153, 153, 153));
8410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
8420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
8430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
8440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
8450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
8460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
8470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
8480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
8490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
8500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
8510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine the actual encoding with GL buffers
8530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Actual encoder, Actual GL Buffers Filled SurfaceMediaSource
8540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// The same pattern is rendered every frame
8550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceGLTest, EncodingFromGLRgbaSameImageEachBufNpotWrite) {
8563856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
8573856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** Testing the whole pipeline with actual Recorder ***********");
8583856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** GL Filling the buffers ***********");
8590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // Note: No need to set the colorformat for the buffers. The colorformat is
8600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // in the GRAlloc buffers itself.
8610c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8620c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const char *fileName = "/sdcard/outputSurfEncMSourceGL.mp4";
8630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int fd = open(fileName, O_RDWR | O_CREAT, 0744);
8640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    if (fd < 0) {
86529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("ERROR: Could not open the the file %s, fd = %d !!", fileName, fd);
8660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
8670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    CHECK(fd >= 0);
8680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<MediaRecorder> mr = setUpMediaRecorder(fd, VIDEO_SOURCE_GRALLOC_BUFFER,
8700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            OUTPUT_FORMAT_MPEG_4, VIDEO_ENCODER_H264, mYuvTexWidth, mYuvTexHeight, 30);
8710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // get the reference to the surfacemediasource living in
8730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // mediaserver that is created by stagefrightrecorder
8740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    setUpEGLSurfaceFromMediaRecorder(mr);
8750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int32_t nFramesCount = 0;
8770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    while (nFramesCount <= 300) {
8780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        oneBufferPassGL();
8790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        nFramesCount++;
8803856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("framesCount = %d", nFramesCount);
8810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
8820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
88331292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
88431292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi            EGL_NO_CONTEXT));
88531292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
88631292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    eglDestroySurface(mEglDisplay, mEglSurface);
88731292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    mEglSurface = EGL_NO_SURFACE;
88831292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi
8893856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Stopping MediaRecorder...");
890f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong    CHECK_EQ((status_t)OK, mr->stop());
8910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr.clear();
8920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    close(fd);
8930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
8940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine the actual encoding from the GL Buffers
8960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Actual encoder, Actual GL Buffers Filled SurfaceMediaSource
8970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// A different pattern is rendered every frame
8980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceGLTest, EncodingFromGLRgbaDiffImageEachBufNpotWrite) {
8993856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
9003856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** Testing the whole pipeline with actual Recorder ***********");
9013856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** Diff GL Filling the buffers ***********");
9020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // Note: No need to set the colorformat for the buffers. The colorformat is
9030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // in the GRAlloc buffers itself.
9040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
9050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const char *fileName = "/sdcard/outputSurfEncMSourceGLDiff.mp4";
9060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int fd = open(fileName, O_RDWR | O_CREAT, 0744);
9070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    if (fd < 0) {
90829357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("ERROR: Could not open the the file %s, fd = %d !!", fileName, fd);
9090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
9100c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    CHECK(fd >= 0);
9110c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
9120c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<MediaRecorder> mr = setUpMediaRecorder(fd, VIDEO_SOURCE_GRALLOC_BUFFER,
9130c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            OUTPUT_FORMAT_MPEG_4, VIDEO_ENCODER_H264, mYuvTexWidth, mYuvTexHeight, 30);
9140c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
9150c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // get the reference to the surfacemediasource living in
9160c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // mediaserver that is created by stagefrightrecorder
9170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    setUpEGLSurfaceFromMediaRecorder(mr);
9180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
9190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int32_t nFramesCount = 0;
9200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    while (nFramesCount <= 300) {
9210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        oneBufferPassGL(nFramesCount);
9220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        nFramesCount++;
9233856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("framesCount = %d", nFramesCount);
9240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
9250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
92631292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
92731292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi            EGL_NO_CONTEXT));
92831292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
92931292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    eglDestroySurface(mEglDisplay, mEglSurface);
93031292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    mEglSurface = EGL_NO_SURFACE;
93131292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi
9323856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Stopping MediaRecorder...");
933f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong    CHECK_EQ((status_t)OK, mr->stop());
9340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr.clear();
9350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    close(fd);
9360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
9373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi} // namespace android
938