SurfaceMediaSource_test.cpp revision 1a2952aee048ca7b1765e2bc09ebe9aeddaeafa3
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
261a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi#include <media/stagefright/SurfaceMediaSource.h>
270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi#include <media/mediarecorder.h>
283399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
293399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi#include <ui/GraphicBuffer.h>
301a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian#include <gui/Surface.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(
93a6195decfe4f9021bbbd7deb050495c33371366bJeff Brown                    String8("Test Surface"),
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");
1108ba01021b573889802e67e029225a96f0dfa471aAndy McFadden            sp<IGraphicBufferProducer> sms = (new SurfaceMediaSource(
111bdddc659a941afdb7f4958f582c6901c07246097Daniel Lam                    getSurfaceWidth(), getSurfaceHeight()))->getBufferQueue();
1121a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian            sp<Surface> stc = new Surface(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);
362bdddc659a941afdb7f4958f582c6901c07246097Daniel Lam
363abf0610a8cea021548f2909e1d47d656206f641cDaniel Lam        // Manual cast is required to avoid constructor ambiguity
3641a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian        mSTC = new Surface(static_cast<sp<IGraphicBufferProducer> >( mSMS->getBufferQueue()));
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;
3781a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian    sp<Surface> 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);
3991a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian        mSTC = new Surface(static_cast<sp<IGraphicBufferProducer> >( mSMS->getBufferQueue()));
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;
4191a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian    sp<Surface> 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) {
4858ba01021b573889802e67e029225a96f0dfa471aAndy McFadden    sp<IGraphicBufferProducer> iST = mr->querySurfaceMediaSourceFromMediaServer();
4861a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian    mSTC = new Surface(iST);
4870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mANW = mSTC;
4880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
4899d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam    if (mEglSurface != EGL_NO_SURFACE) {
4909d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam        EXPECT_TRUE(eglDestroySurface(mEglDisplay, mEglSurface));
4919d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam        mEglSurface = EGL_NO_SURFACE;
4929d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam    }
49321fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
4940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                                mANW.get(), NULL);
4950c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
49621fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    ASSERT_NE(EGL_NO_SURFACE, mEglSurface) ;
4970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
49821fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
4990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglContext));
5000c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
5010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
5020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
5030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
5040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
5050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Methods in SurfaceMediaSourceTest
5060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
5070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
5080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// One pass of dequeuing and queuing the buffer. Fill it in with
5090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// cpu YV12 buffer
5101a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketivoid SurfaceMediaSourceTest::oneBufferPass(int width, int height ) {
5113399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ANativeWindowBuffer* anb;
5121e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), &anb));
5133399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    ASSERT_TRUE(anb != NULL);
5143399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5153399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5163399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    // Fill the buffer with the a checkerboard pattern
5173399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    uint8_t* img = NULL;
5181e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    sp<GraphicBuffer> buf(new GraphicBuffer(anb, false));
5193399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img));
5201a2fafbaa36390a06cc9a066fcbe147c8c47ea77Pannag Sanketi    SurfaceMediaSourceTest::fillYV12Buffer(img, width, height, buf->getStride());
5213399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    buf->unlock();
5223399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5231e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis    ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(),
5241e5b2b3361ddd07259bf4b29820ca4aa5f3a861bJamie Gennis            -1));
5253399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
5263399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
5270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Dequeuing and queuing the buffer without really filling it in.
5280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketivoid SurfaceMediaSourceTest::oneBufferPassNoFill(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) {
74229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("ERROR: Could not open the the file %s, fd = %d !!", fileName, fd);
7430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
7440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    CHECK(fd >= 0);
7450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<MediaRecorder> mr = SurfaceMediaSourceGLTest::setUpMediaRecorder(fd,
7470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            VIDEO_SOURCE_GRALLOC_BUFFER,
7480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            OUTPUT_FORMAT_MPEG_4, VIDEO_ENCODER_H264, mYuvTexWidth,
7490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mYuvTexHeight, 30);
7500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // get the reference to the surfacemediasource living in
7510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // mediaserver that is created by stagefrightrecorder
7528ba01021b573889802e67e029225a96f0dfa471aAndy McFadden    sp<IGraphicBufferProducer> iST = mr->querySurfaceMediaSourceFromMediaServer();
7531a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian    mSTC = new Surface(iST);
7540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mANW = mSTC;
7550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU));
7560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(),
7570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                                                HAL_PIXEL_FORMAT_YV12));
7583399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7593399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    int32_t nFramesCount = 0;
7603399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    while (nFramesCount <= 300) {
7610c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        oneBufferPassNoFill(mYuvTexWidth, mYuvTexHeight);
7623399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi        nFramesCount++;
7633856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("framesCount = %d", nFramesCount);
7643399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi    }
7653399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(NO_ERROR, native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU));
7673856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Stopping MediaRecorder...");
768f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong    CHECK_EQ((status_t)OK, mr->stop());
7690c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr.clear();
7700c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    close(fd);
7713399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi}
7723399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
7730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi//////////////////////////////////////////////////////////////////////
7740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// GL tests
7750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi/////////////////////////////////////////////////////////////////////
7760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7770c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine whether we can choose the Recordable Android GLConfig
7780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// DummyRecorder used- no real encoding here
77921fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag SanketiTEST_F(SurfaceMediaSourceGLTest, ChooseAndroidRecordableEGLConfigDummyWriter) {
7803856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
7813856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Verify creating a surface w/ right config + dummy writer*********");
7820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mSMS = new SurfaceMediaSource(mYuvTexWidth, mYuvTexHeight);
7841a2952aee048ca7b1765e2bc09ebe9aeddaeafa3Mathias Agopian    mSTC = new Surface(static_cast<sp<IGraphicBufferProducer> >( mSMS->getBufferQueue()));
7850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mANW = mSTC;
7860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    DummyRecorder writer(mSMS);
7880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    writer.start();
7890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
7909d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam    if (mEglSurface != EGL_NO_SURFACE) {
7919d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam        EXPECT_TRUE(eglDestroySurface(mEglDisplay, mEglSurface));
7929d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam        mEglSurface = EGL_NO_SURFACE;
7939d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam    }
7949d06df4ecfe39e7e46873f7cd75a71e32a01d4f7Daniel Lam
79521fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig,
7960c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi                                mANW.get(), NULL);
7970c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
79821fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    ASSERT_NE(EGL_NO_SURFACE, mEglSurface) ;
7990c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
80021fcf0830c8b4fb544a09467bc96a8d0463ad966Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
8010c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            mEglContext));
8020c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
8033399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi
8040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int32_t nFramesCount = 0;
8050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    while (nFramesCount <= 300) {
8060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        oneBufferPassGL();
8070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        nFramesCount++;
8083856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("framesCount = %d", nFramesCount);
8090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
8100c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
81131292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
81231292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi            EGL_NO_CONTEXT));
81331292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
81431292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    eglDestroySurface(mEglDisplay, mEglSurface);
81531292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    mEglSurface = EGL_NO_SURFACE;
81631292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi
8170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    writer.stop();
8180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
8190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine whether we can render GL buffers in to the surface
8200c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// created with the native window handle
8210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceGLTest, RenderingToRecordableEGLSurfaceWorks) {
8223856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
8233856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("RenderingToRecordableEGLSurfaceWorks *********************");
8240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // Do the producer side of things
8250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(0.6, 0.6, 0.6, 0.6);
8260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
8270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glEnable(GL_SCISSOR_TEST);
8290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(4, 4, 4, 4);
8300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(1.0, 0.0, 0.0, 1.0);
8310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
8320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(24, 48, 4, 4);
8340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(0.0, 1.0, 0.0, 1.0);
8350c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
8360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glScissor(37, 17, 4, 4);
8380c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClearColor(0.0, 0.0, 1.0, 1.0);
8390c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    glClear(GL_COLOR_BUFFER_BIT);
8400c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8410c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 0,  0, 153, 153, 153, 153));
8420c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(63,  0, 153, 153, 153, 153));
8430c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153));
8440c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153));
8450c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 4,  7, 255,   0,   0, 255));
8470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(25, 51,   0, 255,   0, 255));
8480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(40, 19,   0,   0, 255, 255));
8490c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153));
8500c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153));
8510c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(13,  8, 153, 153, 153, 153));
8520c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(46,  3, 153, 153, 153, 153));
8530c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153));
8540c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153));
8550c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153));
8560c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153));
8570c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153));
8580c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153));
8590c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153));
8600c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153));
8610c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153));
8620c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
8630c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8640c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine the actual encoding with GL buffers
8650c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Actual encoder, Actual GL Buffers Filled SurfaceMediaSource
8660c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// The same pattern is rendered every frame
8670c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceGLTest, EncodingFromGLRgbaSameImageEachBufNpotWrite) {
8683856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
8693856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** Testing the whole pipeline with actual Recorder ***********");
8703856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** GL Filling the buffers ***********");
8710c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // Note: No need to set the colorformat for the buffers. The colorformat is
8720c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // in the GRAlloc buffers itself.
8730c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8740c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const char *fileName = "/sdcard/outputSurfEncMSourceGL.mp4";
8750c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int fd = open(fileName, O_RDWR | O_CREAT, 0744);
8760c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    if (fd < 0) {
87729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("ERROR: Could not open the the file %s, fd = %d !!", fileName, fd);
8780c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
8790c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    CHECK(fd >= 0);
8800c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8810c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<MediaRecorder> mr = setUpMediaRecorder(fd, VIDEO_SOURCE_GRALLOC_BUFFER,
8820c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            OUTPUT_FORMAT_MPEG_4, VIDEO_ENCODER_H264, mYuvTexWidth, mYuvTexHeight, 30);
8830c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8840c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // get the reference to the surfacemediasource living in
8850c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // mediaserver that is created by stagefrightrecorder
8860c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    setUpEGLSurfaceFromMediaRecorder(mr);
8870c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
8880c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int32_t nFramesCount = 0;
8890c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    while (nFramesCount <= 300) {
8900c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        oneBufferPassGL();
8910c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        nFramesCount++;
8923856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("framesCount = %d", nFramesCount);
8930c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
8940c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
89531292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
89631292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi            EGL_NO_CONTEXT));
89731292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
89831292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    eglDestroySurface(mEglDisplay, mEglSurface);
89931292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    mEglSurface = EGL_NO_SURFACE;
90031292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi
9013856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Stopping MediaRecorder...");
902f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong    CHECK_EQ((status_t)OK, mr->stop());
9030c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr.clear();
9040c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    close(fd);
9050c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
9060c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
9070c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Test to examine the actual encoding from the GL Buffers
9080c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// Actual encoder, Actual GL Buffers Filled SurfaceMediaSource
9090c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi// A different pattern is rendered every frame
9100c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag SanketiTEST_F(SurfaceMediaSourceGLTest, EncodingFromGLRgbaDiffImageEachBufNpotWrite) {
9113856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Test # %d", testId++);
9123856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** Testing the whole pipeline with actual Recorder ***********");
9133856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("************** Diff GL Filling the buffers ***********");
9140c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // Note: No need to set the colorformat for the buffers. The colorformat is
9150c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // in the GRAlloc buffers itself.
9160c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
9170c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    const char *fileName = "/sdcard/outputSurfEncMSourceGLDiff.mp4";
9180c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int fd = open(fileName, O_RDWR | O_CREAT, 0744);
9190c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    if (fd < 0) {
92029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("ERROR: Could not open the the file %s, fd = %d !!", fileName, fd);
9210c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
9220c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    CHECK(fd >= 0);
9230c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
9240c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    sp<MediaRecorder> mr = setUpMediaRecorder(fd, VIDEO_SOURCE_GRALLOC_BUFFER,
9250c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi            OUTPUT_FORMAT_MPEG_4, VIDEO_ENCODER_H264, mYuvTexWidth, mYuvTexHeight, 30);
9260c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
9270c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // get the reference to the surfacemediasource living in
9280c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    // mediaserver that is created by stagefrightrecorder
9290c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    setUpEGLSurfaceFromMediaRecorder(mr);
9300c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
9310c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    int32_t nFramesCount = 0;
9320c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    while (nFramesCount <= 300) {
9330c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        oneBufferPassGL(nFramesCount);
9340c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi        nFramesCount++;
9353856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("framesCount = %d", nFramesCount);
9360c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    }
9370c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi
93831292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
93931292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi            EGL_NO_CONTEXT));
94031292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    ASSERT_EQ(EGL_SUCCESS, eglGetError());
94131292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    eglDestroySurface(mEglDisplay, mEglSurface);
94231292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi    mEglSurface = EGL_NO_SURFACE;
94331292d99f62a812af691e67e8e732c6ae582ee08Pannag Sanketi
9443856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("Stopping MediaRecorder...");
945f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong    CHECK_EQ((status_t)OK, mr->stop());
9460c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    mr.clear();
9470c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi    close(fd);
9480c5c7d2b119d2350c186ae9902919bcf28c3e277Pannag Sanketi}
9493399b7267185646c69b04352211fca4fad9d7547Pannag Sanketi} // namespace android
950