1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <utils/String8.h>
20
21#include <EGL/egl.h>
22#include <gui/Surface.h>
23
24
25namespace android {
26
27class EGLTest : public ::testing::Test {
28protected:
29    EGLDisplay mEglDisplay;
30
31protected:
32    EGLTest() :
33            mEglDisplay(EGL_NO_DISPLAY) {
34    }
35
36    virtual void SetUp() {
37        mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
38        ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
39        ASSERT_EQ(EGL_SUCCESS, eglGetError());
40
41        EGLint majorVersion;
42        EGLint minorVersion;
43        EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
44        ASSERT_EQ(EGL_SUCCESS, eglGetError());
45        RecordProperty("EglVersionMajor", majorVersion);
46        RecordProperty("EglVersionMajor", minorVersion);
47    }
48
49    virtual void TearDown() {
50        EGLBoolean success = eglTerminate(mEglDisplay);
51        ASSERT_EQ(EGL_TRUE, success);
52        ASSERT_EQ(EGL_SUCCESS, eglGetError());
53    }
54};
55
56TEST_F(EGLTest, DISABLED_EGLConfigEightBitFirst) {
57
58    EGLint numConfigs;
59    EGLConfig config;
60    EGLBoolean success;
61    EGLint attrs[] = {
62            EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
63            EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES2_BIT,
64            EGL_NONE
65    };
66
67    success = eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs);
68    ASSERT_EQ(EGL_TRUE, success);
69    ASSERT_EQ(EGL_SUCCESS, eglGetError());
70    ASSERT_GE(numConfigs, 1);
71
72    EGLint components[3];
73
74    success = eglGetConfigAttrib(mEglDisplay, config, EGL_RED_SIZE, &components[0]);
75    ASSERT_EQ(EGL_TRUE, success);
76    ASSERT_EQ(EGL_SUCCESS, eglGetError());
77    success = eglGetConfigAttrib(mEglDisplay, config, EGL_GREEN_SIZE, &components[1]);
78    ASSERT_EQ(EGL_TRUE, success);
79    ASSERT_EQ(EGL_SUCCESS, eglGetError());
80    success = eglGetConfigAttrib(mEglDisplay, config, EGL_BLUE_SIZE, &components[2]);
81    ASSERT_EQ(EGL_TRUE, success);
82    ASSERT_EQ(EGL_SUCCESS, eglGetError());
83
84    EXPECT_GE(components[0], 8);
85    EXPECT_GE(components[1], 8);
86    EXPECT_GE(components[2], 8);
87}
88
89TEST_F(EGLTest, EGLTerminateSucceedsWithRemainingObjects) {
90    EGLint numConfigs;
91    EGLConfig config;
92    EGLint attrs[] = {
93        EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
94        EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES2_BIT,
95        EGL_RED_SIZE,           8,
96        EGL_GREEN_SIZE,         8,
97        EGL_BLUE_SIZE,          8,
98        EGL_ALPHA_SIZE,         8,
99        EGL_NONE
100    };
101    EXPECT_TRUE(eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs));
102
103    struct DummyConsumer : public BnConsumerListener {
104        virtual void onFrameAvailable() {}
105        virtual void onBuffersReleased() {}
106    };
107
108    // Create a EGLSurface
109    sp<BufferQueue> bq = new BufferQueue();
110    bq->consumerConnect(new DummyConsumer, false);
111    sp<Surface> mSTC = new Surface(static_cast<sp<IGraphicBufferProducer> >( bq));
112    sp<ANativeWindow> mANW = mSTC;
113
114    EGLSurface eglSurface = eglCreateWindowSurface(mEglDisplay, config,
115                                mANW.get(), NULL);
116    ASSERT_EQ(EGL_SUCCESS, eglGetError());
117    ASSERT_NE(EGL_NO_SURFACE, eglSurface) ;
118
119    // do not destroy eglSurface
120    // eglTerminate is called in the tear down and should destroy it for us
121}
122
123TEST_F(EGLTest, EGLConfigRGBA8888First) {
124
125    EGLint numConfigs;
126    EGLConfig config;
127    EGLBoolean success;
128    EGLint attrs[] = {
129            EGL_SURFACE_TYPE,       EGL_WINDOW_BIT,
130            EGL_RENDERABLE_TYPE,    EGL_OPENGL_ES2_BIT,
131            EGL_RED_SIZE,           8,
132            EGL_GREEN_SIZE,         8,
133            EGL_BLUE_SIZE,          8,
134            EGL_ALPHA_SIZE,         8,
135            EGL_NONE
136    };
137
138    success = eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs);
139    ASSERT_EQ(EGL_TRUE, success);
140    ASSERT_EQ(EGL_SUCCESS, eglGetError());
141    ASSERT_GE(numConfigs, 1);
142
143    EGLint components[4];
144
145    success = eglGetConfigAttrib(mEglDisplay, config, EGL_RED_SIZE, &components[0]);
146    ASSERT_EQ(EGL_TRUE, success);
147    ASSERT_EQ(EGL_SUCCESS, eglGetError());
148    success = eglGetConfigAttrib(mEglDisplay, config, EGL_GREEN_SIZE, &components[1]);
149    ASSERT_EQ(EGL_TRUE, success);
150    ASSERT_EQ(EGL_SUCCESS, eglGetError());
151    success = eglGetConfigAttrib(mEglDisplay, config, EGL_BLUE_SIZE, &components[2]);
152    ASSERT_EQ(EGL_TRUE, success);
153    ASSERT_EQ(EGL_SUCCESS, eglGetError());
154    success = eglGetConfigAttrib(mEglDisplay, config, EGL_ALPHA_SIZE, &components[3]);
155    ASSERT_EQ(EGL_TRUE, success);
156    ASSERT_EQ(EGL_SUCCESS, eglGetError());
157
158    EXPECT_GE(components[0], 8);
159    EXPECT_GE(components[1], 8);
160    EXPECT_GE(components[2], 8);
161    EXPECT_GE(components[3], 8);
162}
163
164
165}
166