12f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian/*
22f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian * Copyright (C) 2011 The Android Open Source Project
32f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian *
42f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
52f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian * you may not use this file except in compliance with the License.
62f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian * You may obtain a copy of the License at
72f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian *
82f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
92f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian *
102f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian * Unless required by applicable law or agreed to in writing, software
112f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
122f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian * See the License for the specific language governing permissions and
142f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian * limitations under the License.
152f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian */
162f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
172f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian#include <gtest/gtest.h>
182f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
192f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian#include <utils/String8.h>
202f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
212f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian#include <EGL/egl.h>
22e3c697fb929c856b59fa56a8e05a2a7eba187c3dMathias Agopian#include <gui/Surface.h>
231cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam
242f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
252f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopiannamespace android {
262f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
272f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopianclass EGLTest : public ::testing::Test {
282f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopianprotected:
292f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLDisplay mEglDisplay;
302f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
312f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopianprotected:
322f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLTest() :
332f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            mEglDisplay(EGL_NO_DISPLAY) {
342f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    }
352f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
362f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    virtual void SetUp() {
372f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
382f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
392f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        ASSERT_EQ(EGL_SUCCESS, eglGetError());
402f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
412f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        EGLint majorVersion;
422f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        EGLint minorVersion;
432f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
442f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        ASSERT_EQ(EGL_SUCCESS, eglGetError());
452f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        RecordProperty("EglVersionMajor", majorVersion);
462f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        RecordProperty("EglVersionMajor", minorVersion);
472f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    }
482f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
492f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    virtual void TearDown() {
502f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        EGLBoolean success = eglTerminate(mEglDisplay);
512f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        ASSERT_EQ(EGL_TRUE, success);
522f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian        ASSERT_EQ(EGL_SUCCESS, eglGetError());
532f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    }
542f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian};
552f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
562f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias AgopianTEST_F(EGLTest, DISABLED_EGLConfigEightBitFirst) {
572f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
582f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLint numConfigs;
592f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLConfig config;
602f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLBoolean success;
612f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLint attrs[] = {
622f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
632f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES2_BIT,
642f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_NONE
652f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    };
662f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
672f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    success = eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs);
682f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_TRUE, success);
692f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_SUCCESS, eglGetError());
702f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_GE(numConfigs, 1);
712f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
722f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLint components[3];
732f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
742f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    success = eglGetConfigAttrib(mEglDisplay, config, EGL_RED_SIZE, &components[0]);
752f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_TRUE, success);
762f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_SUCCESS, eglGetError());
772f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    success = eglGetConfigAttrib(mEglDisplay, config, EGL_GREEN_SIZE, &components[1]);
782f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_TRUE, success);
792f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_SUCCESS, eglGetError());
802f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    success = eglGetConfigAttrib(mEglDisplay, config, EGL_BLUE_SIZE, &components[2]);
812f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_TRUE, success);
822f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_SUCCESS, eglGetError());
832f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
842f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EXPECT_GE(components[0], 8);
852f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EXPECT_GE(components[1], 8);
862f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EXPECT_GE(components[2], 8);
872f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian}
882f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
891cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel LamTEST_F(EGLTest, EGLTerminateSucceedsWithRemainingObjects) {
901cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    EGLint numConfigs;
911cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    EGLConfig config;
921cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    EGLint attrs[] = {
931cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam        EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
941cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam        EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES2_BIT,
951cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam        EGL_RED_SIZE,           8,
961cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam        EGL_GREEN_SIZE,         8,
971cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam        EGL_BLUE_SIZE,          8,
981cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam        EGL_ALPHA_SIZE,         8,
991cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam        EGL_NONE
1001cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    };
1011cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    EXPECT_TRUE(eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs));
1021cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam
103a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    struct DummyConsumer : public BnConsumerListener {
1048dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza        virtual void onFrameAvailable(const BufferItem& /* item */) {}
105595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian        virtual void onBuffersReleased() {}
10685c0fee024384a7756a5eeb31592b6a0d7bacd91Jesse Hall        virtual void onSidebandStreamChanged() {}
107595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    };
108595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian
1091cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    // Create a EGLSurface
1105603a2fbbd1aae74c4635e2f600819fb05d112e0Dan Stoza    sp<IGraphicBufferProducer> producer;
1115603a2fbbd1aae74c4635e2f600819fb05d112e0Dan Stoza    sp<IGraphicBufferConsumer> consumer;
1125603a2fbbd1aae74c4635e2f600819fb05d112e0Dan Stoza    BufferQueue::createBufferQueue(&producer, &consumer);
1135603a2fbbd1aae74c4635e2f600819fb05d112e0Dan Stoza    consumer->consumerConnect(new DummyConsumer, false);
1145603a2fbbd1aae74c4635e2f600819fb05d112e0Dan Stoza    sp<Surface> mSTC = new Surface(producer);
1151cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    sp<ANativeWindow> mANW = mSTC;
1161cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam
1171cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    EGLSurface eglSurface = eglCreateWindowSurface(mEglDisplay, config,
1181cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam                                mANW.get(), NULL);
1191cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1201cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    ASSERT_NE(EGL_NO_SURFACE, eglSurface) ;
1211cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam
1221cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    // do not destroy eglSurface
1231cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam    // eglTerminate is called in the tear down and should destroy it for us
1241cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam}
1251cbcb98a2528ea0d8ce34dc35513859c7ef957f3Daniel Lam
1262f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias AgopianTEST_F(EGLTest, EGLConfigRGBA8888First) {
1272f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
1282f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLint numConfigs;
1292f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLConfig config;
1302f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLBoolean success;
1312f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLint attrs[] = {
1322f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
1332f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES2_BIT,
1342f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_RED_SIZE,           8,
1352f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_GREEN_SIZE,         8,
1362f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_BLUE_SIZE,          8,
1372f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_ALPHA_SIZE,         8,
1382f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian            EGL_NONE
1392f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    };
1402f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
1412f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    success = eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs);
1422f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_TRUE, success);
1432f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1442f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_GE(numConfigs, 1);
1452f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
1462f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EGLint components[4];
1472f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
1482f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    success = eglGetConfigAttrib(mEglDisplay, config, EGL_RED_SIZE, &components[0]);
1492f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_TRUE, success);
1502f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1512f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    success = eglGetConfigAttrib(mEglDisplay, config, EGL_GREEN_SIZE, &components[1]);
1522f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_TRUE, success);
1532f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1542f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    success = eglGetConfigAttrib(mEglDisplay, config, EGL_BLUE_SIZE, &components[2]);
1552f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_TRUE, success);
1562f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1572f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    success = eglGetConfigAttrib(mEglDisplay, config, EGL_ALPHA_SIZE, &components[3]);
1582f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_TRUE, success);
1592f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    ASSERT_EQ(EGL_SUCCESS, eglGetError());
1602f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
1612f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EXPECT_GE(components[0], 8);
1622f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EXPECT_GE(components[1], 8);
1632f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EXPECT_GE(components[2], 8);
1642f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian    EXPECT_GE(components[3], 8);
1652f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian}
1662f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
1672f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian
1682f739f8f0442f9c1bb42c47ab43d365fd9020008Mathias Agopian}
169