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
17bdddc659a941afdb7f4958f582c6901c07246097Daniel Lam//#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
2688c3c4acb13a93209b3572fecd585099defec184Mathias Agopian#include <GLES2/gl2.h>
2788c3c4acb13a93209b3572fecd585099defec184Mathias Agopian
281a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi#include <media/stagefright/SurfaceMediaSource.h>
290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi#include <media/mediarecorder.h>
303399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
313399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <ui/GraphicBuffer.h>
321a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian#include <gui/Surface.h>
33df712ea86e6350f7005a02ab0e1c60c28a343ed0Mathias Agopian#include <gui/ISurfaceComposer.h>
34df712ea86e6350f7005a02ab0e1c60c28a343ed0Mathias Agopian#include <gui/Surface.h>
35df712ea86e6350f7005a02ab0e1c60c28a343ed0Mathias Agopian#include <gui/SurfaceComposerClient.h>
363399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <binder/ProcessState.h>
383399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
39f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong#include <media/stagefright/foundation/ADebug.h>
400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi#include <media/stagefright/MediaBufferGroup.h>
413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/MediaDefs.h>
423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/MetaData.h>
433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/OMXClient.h>
443399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <media/stagefright/OMXCodec.h>
453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <OMX_Component.h>
463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include "DummyRecorder.h"
483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketinamespace android {
513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketiclass GLTest : public ::testing::Test {
530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketiprotected:
540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    GLTest():
560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglDisplay(EGL_NO_DISPLAY),
570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglSurface(EGL_NO_SURFACE),
580c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglContext(EGL_NO_CONTEXT) {
590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
610c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual void SetUp() {
623856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("GLTest::SetUp()");
630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
650c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGLint majorVersion;
680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGLint minorVersion;
690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        RecordProperty("EglVersionMajor", majorVersion);
720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        RecordProperty("EglVersionMajor", minorVersion);
730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGLint numConfigs = 0;
750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig,
760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                1, &numConfigs));
770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS");
800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (displaySecsEnv != NULL) {
810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mDisplaySecs = atoi(displaySecsEnv);
820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (mDisplaySecs < 0) {
830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                mDisplaySecs = 0;
840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        } else {
860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mDisplaySecs = 0;
870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mDisplaySecs > 0) {
900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mComposerClient = new SurfaceComposerClient;
910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(NO_ERROR, mComposerClient->initCheck());
920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mSurfaceControl = mComposerClient->createSurface(
94a6195decfe4f9021bbbd7deb050495c33371366bJeff Brown                    String8("Test Surface"),
950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    getSurfaceWidth(), getSurfaceHeight(),
960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    PIXEL_FORMAT_RGB_888, 0);
970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_TRUE(mSurfaceControl != NULL);
990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_TRUE(mSurfaceControl->isValid());
1000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            SurfaceComposerClient::openGlobalTransaction();
1020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF));
1030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(NO_ERROR, mSurfaceControl->show());
1040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            SurfaceComposerClient::closeGlobalTransaction();
1050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            sp<ANativeWindow> window = mSurfaceControl->getSurface();
1070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
1080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    window.get(), NULL);
1090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        } else {
1103856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("No actual display. Choosing EGLSurface based on SurfaceMediaSource");
1118ba01021b573889802e67e029225a96f0dfa471aAndy McFadden            sp<IGraphicBufferProducer> sms = (new SurfaceMediaSource(
1125205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza                    getSurfaceWidth(), getSurfaceHeight()))->getProducer();
1131a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian            sp<Surface> stc = new Surface(sms);
11421fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi            sp<ANativeWindow> window = stc;
1150c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
11621fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi            mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
11721fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi                    window.get(), NULL);
1180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_NE(EGL_NO_SURFACE, mEglSurface);
1210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT,
1230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                getContextAttribs());
1240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_NE(EGL_NO_CONTEXT, mEglContext);
1260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
1280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                mEglContext));
1290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGLint w, h;
1320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w));
1330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h));
1350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        RecordProperty("EglSurfaceWidth", w);
1370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        RecordProperty("EglSurfaceHeight", h);
1380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        glViewport(0, 0, w, h);
1400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
1410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual void TearDown() {
1440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        // Display the result
1450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) {
1460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            eglSwapBuffers(mEglDisplay, mEglSurface);
1470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            sleep(mDisplaySecs);
1480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mComposerClient != NULL) {
1510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mComposerClient->dispose();
1520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mEglContext != EGL_NO_CONTEXT) {
1540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            eglDestroyContext(mEglDisplay, mEglContext);
1550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mEglSurface != EGL_NO_SURFACE) {
1570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            eglDestroySurface(mEglDisplay, mEglSurface);
1580c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (mEglDisplay != EGL_NO_DISPLAY) {
1600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            eglTerminate(mEglDisplay);
1610c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
1620c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(EGL_SUCCESS, eglGetError());
1630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1650c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint const* getConfigAttribs() {
1663856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("GLTest getConfigAttribs");
1670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        static EGLint sDefaultConfigAttribs[] = {
1680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
1690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
1700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_RED_SIZE, 8,
1710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_GREEN_SIZE, 8,
1720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_BLUE_SIZE, 8,
1730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_ALPHA_SIZE, 8,
1740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_DEPTH_SIZE, 16,
1750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_STENCIL_SIZE, 8,
1760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_NONE };
1770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return sDefaultConfigAttribs;
1790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint const* getContextAttribs() {
1820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        static EGLint sDefaultContextAttribs[] = {
1830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_CONTEXT_CLIENT_VERSION, 2,
1840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            EGL_NONE };
1850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return sDefaultContextAttribs;
1870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint getSurfaceWidth() {
1900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return 512;
1910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint getSurfaceHeight() {
1940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return 512;
1950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
1960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
1970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void loadShader(GLenum shaderType, const char* pSource, GLuint* outShader) {
1980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLuint shader = glCreateShader(shaderType);
1990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (shader) {
2010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glShaderSource(shader, 1, &pSource, NULL);
2020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glCompileShader(shader);
2040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            GLint compiled = 0;
2060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
2070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (!compiled) {
2090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                GLint infoLen = 0;
2100c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
2110c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2120c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                if (infoLen) {
2130c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    char* buf = (char*) malloc(infoLen);
2140c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    if (buf) {
2150c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        glGetShaderInfoLog(shader, infoLen, NULL, buf);
2160c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        printf("Shader compile log:\n%s\n", buf);
2170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        free(buf);
2180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        FAIL();
2190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    }
2200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                } else {
2210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    char* buf = (char*) malloc(0x1000);
2220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    if (buf) {
2230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        glGetShaderInfoLog(shader, 0x1000, NULL, buf);
2240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        printf("Shader compile log:\n%s\n", buf);
2250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        free(buf);
2260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        FAIL();
2270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    }
2280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                }
2290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                glDeleteShader(shader);
2300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                shader = 0;
2310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
2320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
2330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_TRUE(shader != 0);
2340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        *outShader = shader;
2350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
2360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
2370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void createProgram(const char* pVertexSource, const char* pFragmentSource,
2380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            GLuint* outPgm) {
2390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLuint vertexShader, fragmentShader;
2400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        {
2410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            SCOPED_TRACE("compiling vertex shader");
2420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            loadShader(GL_VERTEX_SHADER, pVertexSource, &vertexShader);
2430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (HasFatalFailure()) {
2440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                return;
2450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
2460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
2470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        {
2480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            SCOPED_TRACE("compiling fragment shader");
2490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            loadShader(GL_FRAGMENT_SHADER, pFragmentSource, &fragmentShader);
2500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (HasFatalFailure()) {
2510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                return;
2520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
2530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
2540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
2550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLuint program = glCreateProgram();
2560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (program) {
2580c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glAttachShader(program, vertexShader);
2590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glAttachShader(program, fragmentShader);
2610c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError());
2620c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glLinkProgram(program);
2630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            GLint linkStatus = GL_FALSE;
2640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
2650c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (linkStatus != GL_TRUE) {
2660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                GLint bufLength = 0;
2670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
2680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                if (bufLength) {
2690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    char* buf = (char*) malloc(bufLength);
2700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    if (buf) {
2710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        glGetProgramInfoLog(program, bufLength, NULL, buf);
2720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        printf("Program link log:\n%s\n", buf);
2730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        free(buf);
2740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                        FAIL();
2750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    }
2760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                }
2770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                glDeleteProgram(program);
2780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                program = 0;
2790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
2800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
2810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        glDeleteShader(vertexShader);
2820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        glDeleteShader(fragmentShader);
2830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        ASSERT_TRUE(program != 0);
2840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        *outPgm = program;
2850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
2863399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
2870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    static int abs(int value) {
2880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        return value > 0 ? value : -value;
2890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
2900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
2910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ::testing::AssertionResult checkPixel(int x, int y, int r,
2920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            int g, int b, int a, int tolerance=2) {
2930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLubyte pixel[4];
2940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        String8 msg;
2950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel);
2960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLenum err = glGetError();
2970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (err != GL_NO_ERROR) {
2980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("error reading pixel: %#x", err);
2990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            while ((err = glGetError()) != GL_NO_ERROR) {
3000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                msg += String8::format(", %#x", err);
3010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
3020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            fprintf(stderr, "pixel check failure: %s\n", msg.string());
3030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            return ::testing::AssertionFailure(
3040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    ::testing::Message(msg.string()));
3050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (r >= 0 && abs(r - int(pixel[0])) > tolerance) {
3070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("r(%d isn't %d)", pixel[0], r);
3080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (g >= 0 && abs(g - int(pixel[1])) > tolerance) {
3100c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (!msg.isEmpty()) {
3110c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                msg += " ";
3120c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
3130c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("g(%d isn't %d)", pixel[1], g);
3140c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3150c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (b >= 0 && abs(b - int(pixel[2])) > tolerance) {
3160c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (!msg.isEmpty()) {
3170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                msg += " ";
3180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
3190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("b(%d isn't %d)", pixel[2], b);
3200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (a >= 0 && abs(a - int(pixel[3])) > tolerance) {
3220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            if (!msg.isEmpty()) {
3230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                msg += " ";
3240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            }
3250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            msg += String8::format("a(%d isn't %d)", pixel[3], a);
3260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        if (!msg.isEmpty()) {
3280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            fprintf(stderr, "pixel check failure: %s\n", msg.string());
3290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            return ::testing::AssertionFailure(
3300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                    ::testing::Message(msg.string()));
3310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        } else {
3320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            return ::testing::AssertionSuccess();
3330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        }
3340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
3350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int mDisplaySecs;
3370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<SurfaceComposerClient> mComposerClient;
3380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<SurfaceControl> mSurfaceControl;
3390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EGLDisplay mEglDisplay;
3410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EGLSurface mEglSurface;
3420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EGLContext mEglContext;
3430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EGLConfig  mGlConfig;
3440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi};
3450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi///////////////////////////////////////////////////////////////////////
3470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi//    Class for  the NON-GL tests
3480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi///////////////////////////////////////////////////////////////////////
3491a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketiclass SurfaceMediaSourceTest : public ::testing::Test {
3503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketipublic:
3513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    SurfaceMediaSourceTest( ): mYuvTexWidth(176), mYuvTexHeight(144) { }
3533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    void oneBufferPass(int width, int height );
3540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void oneBufferPassNoFill(int width, int height );
3553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    static void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) ;
3563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    static void fillYV12BufferRect(uint8_t* buf, int w, int h,
3573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                        int stride, const android_native_rect_t& rect) ;
3583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketiprotected:
3593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual void SetUp() {
361b6f2fced47866a5d095b2b18d2e28d7c4321e3a6James Dong        android::ProcessState::self()->startThreadPool();
3621a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi        mSMS = new SurfaceMediaSource(mYuvTexWidth, mYuvTexHeight);
3635205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza        mSTC = new Surface(mSMS->getProducer());
3643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        mANW = mSTC;
3650c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
3663399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual void TearDown() {
3680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mSMS.clear();
3690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mSTC.clear();
3700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mANW.clear();
3713399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
3723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
3730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const int mYuvTexWidth;
3740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const int mYuvTexHeight;
3750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<SurfaceMediaSource> mSMS;
3771a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian    sp<Surface> mSTC;
3780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<ANativeWindow> mANW;
3790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi};
3800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi///////////////////////////////////////////////////////////////////////
3820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi//    Class for  the GL tests
3830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi///////////////////////////////////////////////////////////////////////
3840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketiclass SurfaceMediaSourceGLTest : public GLTest {
3850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketipublic:
3860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    SurfaceMediaSourceGLTest( ): mYuvTexWidth(176), mYuvTexHeight(144) { }
3880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual EGLint const* getConfigAttribs();
3890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void oneBufferPassGL(int num = 0);
3900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    static sp<MediaRecorder> setUpMediaRecorder(int fileDescriptor, int videoSource,
3910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        int outputFormat, int videoEncoder, int width, int height, int fps);
3920c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketiprotected:
3930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
3940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    virtual void SetUp() {
3953856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("SMS-GLTest::SetUp()");
3960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        android::ProcessState::self()->startThreadPool();
3970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mSMS = new SurfaceMediaSource(mYuvTexWidth, mYuvTexHeight);
3985205977929c8a63d3bba026c6bd7b4cc1e236627Dan Stoza        mSTC = new Surface(mSMS->getProducer());
3990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        mANW = mSTC;
4000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        // Doing the setup related to the GL Side
4020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLTest::SetUp();
4030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
4043399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
4053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    virtual void TearDown() {
4061a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi        mSMS.clear();
4073399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        mSTC.clear();
4083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        mANW.clear();
4090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        GLTest::TearDown();
4103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
4113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
4120c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    void setUpEGLSurfaceFromMediaRecorder(sp<MediaRecorder>& mr);
4130c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
414a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi    const int mYuvTexWidth;
415a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi    const int mYuvTexHeight;
4163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
4171a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    sp<SurfaceMediaSource> mSMS;
4181a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian    sp<Surface> mSTC;
4193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    sp<ANativeWindow> mANW;
4203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi};
4213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
4220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
4230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Methods in SurfaceMediaSourceGLTest
4240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
4250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiEGLint const* SurfaceMediaSourceGLTest::getConfigAttribs() {
4263856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("SurfaceMediaSourceGLTest getConfigAttribs");
4270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    static EGLint sDefaultConfigAttribs[] = {
4280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
4290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
4300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_RED_SIZE, 8,
4310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_GREEN_SIZE, 8,
4320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_BLUE_SIZE, 8,
4330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_RECORDABLE_ANDROID, EGL_TRUE,
4340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        EGL_NONE };
4350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    return sDefaultConfigAttribs;
4370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
4380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// One pass of dequeuing and queuing a GLBuffer
4400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketivoid SurfaceMediaSourceGLTest::oneBufferPassGL(int num) {
4410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int d = num % 50;
4420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    float f = 0.2f; // 0.1f * d;
4430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(0, 0.3, 0, 0.6);
4450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
4460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glEnable(GL_SCISSOR_TEST);
4480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(4 + d, 4 + d, 4, 4);
4490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(1.0 - f, f, f, 1.0);
4500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
4510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(24 + d, 48 + d, 4, 4);
4530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(f, 1.0 - f, f, 1.0);
4540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
4550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(37 + d, 17 + d, 4, 4);
4570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(f, f, 1.0 - f, 1.0);
4580c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
4590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // The following call dequeues and queues the buffer
46121fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    eglSwapBuffers(mEglDisplay, mEglSurface);
46231292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
4630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glDisable(GL_SCISSOR_TEST);
4640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
4650c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Set up the MediaRecorder which runs in the same process as mediaserver
4670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketisp<MediaRecorder> SurfaceMediaSourceGLTest::setUpMediaRecorder(int fd, int videoSource,
4680c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        int outputFormat, int videoEncoder, int width, int height, int fps) {
4690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<MediaRecorder> mr = new MediaRecorder();
4700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setVideoSource(videoSource);
4710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setOutputFormat(outputFormat);
4720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setVideoEncoder(videoEncoder);
4730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setOutputFile(fd, 0, 0);
4740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setVideoSize(width, height);
4750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->setVideoFrameRate(fps);
4760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr->prepare();
4773856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Starting MediaRecorder...");
478f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong    CHECK_EQ((status_t)OK, mr->start());
4790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    return mr;
4800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
4810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// query the mediarecorder for a surfacemeidasource and create an egl surface with that
4830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketivoid SurfaceMediaSourceGLTest::setUpEGLSurfaceFromMediaRecorder(sp<MediaRecorder>& mr) {
4848ba01021b573889802e67e029225a96f0dfa471aAndy McFadden    sp<IGraphicBufferProducer> iST = mr->querySurfaceMediaSourceFromMediaServer();
4851a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian    mSTC = new Surface(iST);
4860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mANW = mSTC;
4870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4889d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam    if (mEglSurface != EGL_NO_SURFACE) {
4899d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam        EXPECT_TRUE(eglDestroySurface(mEglDisplay, mEglSurface));
4909d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam        mEglSurface = EGL_NO_SURFACE;
4919d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam    }
49221fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
4930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                                mANW.get(), NULL);
4940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
49521fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    ASSERT_NE(EGL_NO_SURFACE, mEglSurface) ;
4960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
49721fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
4980c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglContext));
4990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
5000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
5010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
5020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
5030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
5040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Methods in SurfaceMediaSourceTest
5050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
5060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
5070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// One pass of dequeuing and queuing the buffer. Fill it in with
5080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// cpu YV12 buffer
5091a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketivoid SurfaceMediaSourceTest::oneBufferPass(int width, int height ) {
5103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ANativeWindowBuffer* anb;
5111e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
5123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ASSERT_TRUE(anb != NULL);
5133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Fill the buffer with the a checkerboard pattern
5163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    uint8_t* img = NULL;
5171e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
5183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
5191a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    SurfaceMediaSourceTest::fillYV12Buffer(img, width, height, buf->getStride());
5203399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    buf->unlock();
5213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5221e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
5231e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis            -1));
5243399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
5253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dequeuing and queuing the buffer without really filling it in.
527ccb067b1d8424ba610cbd3de83368bd55b532b5bAndreas Hubervoid SurfaceMediaSourceTest::oneBufferPassNoFill(
528ccb067b1d8424ba610cbd3de83368bd55b532b5bAndreas Huber        int /* width */, int /* height  */) {
5290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ANativeWindowBuffer* anb;
5301e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
5310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_TRUE(anb != NULL);
5323399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // We do not fill the buffer in. Just queue it back.
5341e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
5351e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
5361e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis            -1));
5373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
5383399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5393399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// Fill a YV12 buffer with a multi-colored checkerboard pattern
5401a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketivoid SurfaceMediaSourceTest::fillYV12Buffer(uint8_t* buf, int w, int h, int stride) {
5413399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    const int blockWidth = w > 16 ? w / 16 : 1;
5423399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    const int blockHeight = h > 16 ? h / 16 : 1;
5433399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    const int yuvTexOffsetY = 0;
5443399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideY = stride;
5453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexOffsetV = yuvTexStrideY * h;
5463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
5473399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
5483399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideU = yuvTexStrideV;
5493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    for (int x = 0; x < w; x++) {
5503399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        for (int y = 0; y < h; y++) {
5513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            int parityX = (x / blockWidth) & 1;
5523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            int parityY = (y / blockHeight) & 1;
5533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            unsigned char intensity = (parityX ^ parityY) ? 63 : 191;
5543399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity;
5553399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            if (x < w / 2 && y < h / 2) {
5563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity;
5573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                if (x * 2 < w / 2 && y * 2 < h / 2) {
5583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] =
5593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] =
5603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] =
5613399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] =
5623399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                        intensity;
5633399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                }
5643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            }
5653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        }
5663399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
5673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
5683399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5693399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// Fill a YV12 buffer with red outside a given rectangle and green inside it.
5701a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketivoid SurfaceMediaSourceTest::fillYV12BufferRect(uint8_t* buf, int w,
5713399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                  int h, int stride, const android_native_rect_t& rect) {
5723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    const int yuvTexOffsetY = 0;
5733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideY = stride;
5743399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexOffsetV = yuvTexStrideY * h;
5753399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf;
5763399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2;
5773399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int yuvTexStrideU = yuvTexStrideV;
5783399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    for (int x = 0; x < w; x++) {
5793399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        for (int y = 0; y < h; y++) {
5803399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            bool inside = rect.left <= x && x < rect.right &&
5813399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                    rect.top <= y && y < rect.bottom;
5823399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64;
5833399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            if (x < w / 2 && y < h / 2) {
5843399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                bool inside = rect.left <= 2*x && 2*x < rect.right &&
5853399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                        rect.top <= 2*y && 2*y < rect.bottom;
5863399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16;
5873399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] =
5883399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                                                inside ? 16 : 255;
5893399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            }
5903399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        }
5913399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
5921a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi}  ///////// End of class SurfaceMediaSourceTest
5933399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5943399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi///////////////////////////////////////////////////////////////////
5953399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// Class to imitate the recording     /////////////////////////////
5963399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// ////////////////////////////////////////////////////////////////
5973399b7267185646c69b04352211fca4fad9d7547Pannag Sanketistruct SimpleDummyRecorder {
5983399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        sp<MediaSource> mSource;
5993399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6003399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        SimpleDummyRecorder
6013399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                (const sp<MediaSource> &source): mSource(source) {}
6023399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6033399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        status_t start() { return mSource->start();}
6043399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        status_t stop()  { return mSource->stop();}
6053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6063399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // fakes reading from a media source
6073399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        status_t readFromSource() {
6083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            MediaBuffer *buffer;
6093399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            status_t err = mSource->read(&buffer);
6103399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            if (err != OK) {
6113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi                return err;
6123399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            }
6133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            buffer->release();
6143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            buffer = NULL;
6153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            return OK;
6163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        }
6173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi};
6183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi///////////////////////////////////////////////////////////////////
6193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi//           TESTS
6200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// SurfaceMediaSourceTest class contains tests that fill the buffers
6210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// using the cpu calls
6220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// SurfaceMediaSourceGLTest class contains tests that fill the buffers
6230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// using the GL calls.
6240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// TODO: None of the tests actually verify the encoded images.. so at this point,
6250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// these are mostly functionality tests + visual inspection
6260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi//////////////////////////////////////////////////////////////////////
6270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
6281a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi// Just pass one buffer from the native_window to the SurfaceMediaSource
6290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dummy Encoder
6300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketistatic int testId = 1;
6310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest, DISABLED_DummyEncodingFromCpuFilledYV12BufferNpotOneBufferPass) {
6323856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
6333856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing OneBufferPass ******************************");
6343399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
6360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
6373399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    oneBufferPass(mYuvTexWidth, mYuvTexHeight);
6383399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
6393399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6403399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// Pass the buffer with the wrong height and weight and should not be accepted
6410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dummy Encoder
6420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest, DISABLED_DummyEncodingFromCpuFilledYV12BufferNpotWrongSizeBufferPass) {
6433856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
6443856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing Wrong size BufferPass ******************************");
6453399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6463399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // setting the client side buffer size different than the server size
6470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_dimensions(mANW.get(),
6480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi             10, 10));
6490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
6500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
6513399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6523399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ANativeWindowBuffer* anb;
6533399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // Note: make sure we get an ERROR back when dequeuing!
6551e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    ASSERT_NE(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
6563399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
6573399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6581a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi// pass multiple buffers from the native_window the SurfaceMediaSource
6590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dummy Encoder
6600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest,  DISABLED_DummyEncodingFromCpuFilledYV12BufferNpotMultiBufferPass) {
6613856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
6623856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing MultiBufferPass, Dummy Recorder *********************");
6630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
6640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
665a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi
6661a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    SimpleDummyRecorder writer(mSMS);
6673399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.start();
6683399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6693399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int32_t nFramesCount = 0;
6703399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    while (nFramesCount < 300) {
6713399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        oneBufferPass(mYuvTexWidth, mYuvTexHeight);
6723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6733399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        ASSERT_EQ(NO_ERROR, writer.readFromSource());
6743399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6753399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        nFramesCount++;
6763399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
6773399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.stop();
6783399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
6793399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6801a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi// Delayed pass of multiple buffers from the native_window the SurfaceMediaSource
6810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dummy Encoder
682bdddc659a941afdb7f4958f582c6901c07246097Daniel LamTEST_F(SurfaceMediaSourceTest,  DummyLagEncodingFromCpuFilledYV12BufferNpotMultiBufferPass) {
6833856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
6843856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing MultiBufferPass, Dummy Recorder Lagging **************");
685a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi
6860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
6870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
688a361483bb5dbd3bbf132c5b99b2df7d197c3fc50Pannag Sanketi
6891a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    SimpleDummyRecorder writer(mSMS);
6903399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.start();
6913399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
6923399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int32_t nFramesCount = 1;
693bdddc659a941afdb7f4958f582c6901c07246097Daniel Lam    const int FRAMES_LAG = SurfaceMediaSource::MIN_UNDEQUEUED_BUFFERS;
694bdddc659a941afdb7f4958f582c6901c07246097Daniel Lam
6953399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    while (nFramesCount <= 300) {
696bdddc659a941afdb7f4958f582c6901c07246097Daniel Lam        ALOGV("Frame: %d", nFramesCount);
6973399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        oneBufferPass(mYuvTexWidth, mYuvTexHeight);
6983399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        // Forcing the writer to lag behind a few frames
6993399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        if (nFramesCount > FRAMES_LAG) {
7003399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi            ASSERT_EQ(NO_ERROR, writer.readFromSource());
7013399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        }
7023399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        nFramesCount++;
7033399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
7043399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.stop();
7053399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
7063399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7071a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi// pass multiple buffers from the native_window the SurfaceMediaSource
7083399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi// A dummy writer (MULTITHREADED) is used to simulate actual MPEG4Writer
709bdddc659a941afdb7f4958f582c6901c07246097Daniel LamTEST_F(SurfaceMediaSourceTest, DummyThreadedEncodingFromCpuFilledYV12BufferNpotMultiBufferPass) {
7103856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
7113856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Testing MultiBufferPass, Dummy Recorder Multi-Threaded **********");
7120c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
7130c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            HAL_PIXEL_FORMAT_YV12));
7143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7151a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    DummyRecorder writer(mSMS);
7163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.start();
7173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7183399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int32_t nFramesCount = 0;
7193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    while (nFramesCount <= 300) {
720bdddc659a941afdb7f4958f582c6901c07246097Daniel Lam        ALOGV("Frame: %d", nFramesCount);
7213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        oneBufferPass(mYuvTexWidth, mYuvTexHeight);
7223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7233399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        nFramesCount++;
7243399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
7253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    writer.stop();
7263399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
7273399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine actual encoding using mediarecorder
7290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// We use the mediaserver to create a mediarecorder and send
7300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// it back to us. So SurfaceMediaSource lives in the same process
7310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// as the mediaserver.
7320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Very close to the actual camera, except that the
7330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// buffers are filled and queueud by the CPU instead of GL.
7340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceTest, DISABLED_EncodingFromCpuYV12BufferNpotWriteMediaServer) {
7353856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
7363856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** Testing the whole pipeline with actual MediaRecorder ***********");
7373856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** SurfaceMediaSource is same process as mediaserver    ***********");
7380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const char *fileName = "/sdcard/outputSurfEncMSource.mp4";
7400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int fd = open(fileName, O_RDWR | O_CREAT, 0744);
7410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    if (fd < 0) {
742