blob: 94de5af155669434b532e528352e20c5ec20d499 [file] [log] [blame]
Mathias Agopian2f739f82011-07-07 14:54:30 -07001/*
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
Courtney Goeltzenleuchterf29f2872017-03-28 17:29:52 -060019#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
20
21#include <configstore/Utils.h>
Mathias Agopian2f739f82011-07-07 14:54:30 -070022#include <utils/String8.h>
23
24#include <EGL/egl.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080025#include <gui/Surface.h>
Mathias Agopian2b5dd402017-02-07 17:36:19 -080026#include <gui/IConsumerListener.h>
27#include <gui/IProducerListener.h>
28#include <gui/IGraphicBufferConsumer.h>
29#include <gui/BufferQueue.h>
Mathias Agopian2f739f82011-07-07 14:54:30 -070030
Courtney Goeltzenleuchterf29f2872017-03-28 17:29:52 -060031#define PIXEL_FORMAT_FLOAT "EGL_EXT_pixel_format_float"
32
33bool hasEglPixelFormatFloat() {
34 EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
35 const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
36 size_t cropExtLen = strlen(PIXEL_FORMAT_FLOAT);
37 size_t extsLen = strlen(exts);
38 bool equal = !strcmp(PIXEL_FORMAT_FLOAT, exts);
39 bool atStart = !strncmp(PIXEL_FORMAT_FLOAT " ", exts, cropExtLen + 1);
40 bool atEnd = (cropExtLen + 1) < extsLen &&
41 !strcmp(" " PIXEL_FORMAT_FLOAT, exts + extsLen - (cropExtLen + 1));
42 bool inMiddle = strstr(exts, " " PIXEL_FORMAT_FLOAT " ");
43 return equal || atStart || atEnd || inMiddle;
44}
45
Mathias Agopian2f739f82011-07-07 14:54:30 -070046namespace android {
47
Kalle Raita4cf36372017-01-13 10:18:36 -080048#define EGL_UNSIGNED_TRUE static_cast<EGLBoolean>(EGL_TRUE)
49
Courtney Goeltzenleuchterf29f2872017-03-28 17:29:52 -060050// retrieve wide-color setting from configstore
51using namespace android::hardware::configstore;
52
53static bool hasWideColorDisplay =
54 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false);
55
Mathias Agopian2f739f82011-07-07 14:54:30 -070056class EGLTest : public ::testing::Test {
57protected:
58 EGLDisplay mEglDisplay;
59
60protected:
61 EGLTest() :
62 mEglDisplay(EGL_NO_DISPLAY) {
63 }
64
65 virtual void SetUp() {
66 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
67 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay);
68 ASSERT_EQ(EGL_SUCCESS, eglGetError());
69
70 EGLint majorVersion;
71 EGLint minorVersion;
72 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion));
73 ASSERT_EQ(EGL_SUCCESS, eglGetError());
74 RecordProperty("EglVersionMajor", majorVersion);
75 RecordProperty("EglVersionMajor", minorVersion);
76 }
77
78 virtual void TearDown() {
79 EGLBoolean success = eglTerminate(mEglDisplay);
Kalle Raita4cf36372017-01-13 10:18:36 -080080 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -070081 ASSERT_EQ(EGL_SUCCESS, eglGetError());
82 }
83};
84
85TEST_F(EGLTest, DISABLED_EGLConfigEightBitFirst) {
86
87 EGLint numConfigs;
88 EGLConfig config;
89 EGLBoolean success;
90 EGLint attrs[] = {
91 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
92 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
93 EGL_NONE
94 };
95
96 success = eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs);
Kalle Raita4cf36372017-01-13 10:18:36 -080097 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -070098 ASSERT_EQ(EGL_SUCCESS, eglGetError());
99 ASSERT_GE(numConfigs, 1);
100
101 EGLint components[3];
102
103 success = eglGetConfigAttrib(mEglDisplay, config, EGL_RED_SIZE, &components[0]);
Kalle Raita4cf36372017-01-13 10:18:36 -0800104 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -0700105 ASSERT_EQ(EGL_SUCCESS, eglGetError());
106 success = eglGetConfigAttrib(mEglDisplay, config, EGL_GREEN_SIZE, &components[1]);
Kalle Raita4cf36372017-01-13 10:18:36 -0800107 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -0700108 ASSERT_EQ(EGL_SUCCESS, eglGetError());
109 success = eglGetConfigAttrib(mEglDisplay, config, EGL_BLUE_SIZE, &components[2]);
Kalle Raita4cf36372017-01-13 10:18:36 -0800110 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -0700111 ASSERT_EQ(EGL_SUCCESS, eglGetError());
112
113 EXPECT_GE(components[0], 8);
114 EXPECT_GE(components[1], 8);
115 EXPECT_GE(components[2], 8);
116}
117
Daniel Lam1cbcb982012-04-16 22:21:02 -0700118TEST_F(EGLTest, EGLTerminateSucceedsWithRemainingObjects) {
119 EGLint numConfigs;
120 EGLConfig config;
121 EGLint attrs[] = {
122 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
123 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
124 EGL_RED_SIZE, 8,
125 EGL_GREEN_SIZE, 8,
126 EGL_BLUE_SIZE, 8,
127 EGL_ALPHA_SIZE, 8,
128 EGL_NONE
129 };
130 EXPECT_TRUE(eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs));
131
Mathias Agopiana4e19522013-07-31 20:09:53 -0700132 struct DummyConsumer : public BnConsumerListener {
Brian Anderson5ea5e592016-12-01 16:54:33 -0800133 void onFrameAvailable(const BufferItem& /* item */) override {}
134 void onBuffersReleased() override {}
135 void onSidebandStreamChanged() override {}
Mathias Agopian595264f2013-07-16 22:56:09 -0700136 };
137
Daniel Lam1cbcb982012-04-16 22:21:02 -0700138 // Create a EGLSurface
Dan Stoza5603a2f2014-04-07 13:41:37 -0700139 sp<IGraphicBufferProducer> producer;
140 sp<IGraphicBufferConsumer> consumer;
141 BufferQueue::createBufferQueue(&producer, &consumer);
142 consumer->consumerConnect(new DummyConsumer, false);
143 sp<Surface> mSTC = new Surface(producer);
Daniel Lam1cbcb982012-04-16 22:21:02 -0700144 sp<ANativeWindow> mANW = mSTC;
145
146 EGLSurface eglSurface = eglCreateWindowSurface(mEglDisplay, config,
147 mANW.get(), NULL);
148 ASSERT_EQ(EGL_SUCCESS, eglGetError());
149 ASSERT_NE(EGL_NO_SURFACE, eglSurface) ;
150
151 // do not destroy eglSurface
152 // eglTerminate is called in the tear down and should destroy it for us
153}
154
Mathias Agopian2f739f82011-07-07 14:54:30 -0700155TEST_F(EGLTest, EGLConfigRGBA8888First) {
156
157 EGLint numConfigs;
158 EGLConfig config;
159 EGLBoolean success;
160 EGLint attrs[] = {
161 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
162 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
163 EGL_RED_SIZE, 8,
164 EGL_GREEN_SIZE, 8,
165 EGL_BLUE_SIZE, 8,
166 EGL_ALPHA_SIZE, 8,
167 EGL_NONE
168 };
169
170 success = eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs);
Kalle Raita4cf36372017-01-13 10:18:36 -0800171 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -0700172 ASSERT_EQ(EGL_SUCCESS, eglGetError());
173 ASSERT_GE(numConfigs, 1);
174
175 EGLint components[4];
176
177 success = eglGetConfigAttrib(mEglDisplay, config, EGL_RED_SIZE, &components[0]);
Kalle Raita4cf36372017-01-13 10:18:36 -0800178 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -0700179 ASSERT_EQ(EGL_SUCCESS, eglGetError());
180 success = eglGetConfigAttrib(mEglDisplay, config, EGL_GREEN_SIZE, &components[1]);
Kalle Raita4cf36372017-01-13 10:18:36 -0800181 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -0700182 ASSERT_EQ(EGL_SUCCESS, eglGetError());
183 success = eglGetConfigAttrib(mEglDisplay, config, EGL_BLUE_SIZE, &components[2]);
Kalle Raita4cf36372017-01-13 10:18:36 -0800184 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -0700185 ASSERT_EQ(EGL_SUCCESS, eglGetError());
186 success = eglGetConfigAttrib(mEglDisplay, config, EGL_ALPHA_SIZE, &components[3]);
Kalle Raita4cf36372017-01-13 10:18:36 -0800187 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
Mathias Agopian2f739f82011-07-07 14:54:30 -0700188 ASSERT_EQ(EGL_SUCCESS, eglGetError());
189
190 EXPECT_GE(components[0], 8);
191 EXPECT_GE(components[1], 8);
192 EXPECT_GE(components[2], 8);
193 EXPECT_GE(components[3], 8);
194}
195
Courtney Goeltzenleuchterf29f2872017-03-28 17:29:52 -0600196TEST_F(EGLTest, EGLConfigFP16) {
197 EGLint numConfigs;
198 EGLConfig config;
199 EGLBoolean success;
Mathias Agopian2f739f82011-07-07 14:54:30 -0700200
Courtney Goeltzenleuchterf29f2872017-03-28 17:29:52 -0600201 if (!hasWideColorDisplay) {
202 // skip this test if device does not have wide-color display
203 return;
204 }
205
206 ASSERT_TRUE(hasEglPixelFormatFloat());
207
208 EGLint attrs[] = {EGL_SURFACE_TYPE,
209 EGL_WINDOW_BIT,
210 EGL_RENDERABLE_TYPE,
211 EGL_OPENGL_ES2_BIT,
212 EGL_RED_SIZE,
213 16,
214 EGL_GREEN_SIZE,
215 16,
216 EGL_BLUE_SIZE,
217 16,
218 EGL_ALPHA_SIZE,
219 16,
220 EGL_COLOR_COMPONENT_TYPE_EXT,
221 EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT,
222 EGL_NONE};
223 success = eglChooseConfig(mEglDisplay, attrs, &config, 1, &numConfigs);
224 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
225 ASSERT_EQ(1, numConfigs);
226
227 EGLint components[4];
228
229 success = eglGetConfigAttrib(mEglDisplay, config, EGL_RED_SIZE, &components[0]);
230 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
231 ASSERT_EQ(EGL_SUCCESS, eglGetError());
232 success = eglGetConfigAttrib(mEglDisplay, config, EGL_GREEN_SIZE, &components[1]);
233 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
234 ASSERT_EQ(EGL_SUCCESS, eglGetError());
235 success = eglGetConfigAttrib(mEglDisplay, config, EGL_BLUE_SIZE, &components[2]);
236 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
237 ASSERT_EQ(EGL_SUCCESS, eglGetError());
238 success = eglGetConfigAttrib(mEglDisplay, config, EGL_ALPHA_SIZE, &components[3]);
239 ASSERT_EQ(EGL_UNSIGNED_TRUE, success);
240 ASSERT_EQ(EGL_SUCCESS, eglGetError());
241
242 EXPECT_GE(components[0], 16);
243 EXPECT_GE(components[1], 16);
244 EXPECT_GE(components[2], 16);
245 EXPECT_GE(components[3], 16);
246
247 struct DummyConsumer : public BnConsumerListener {
248 void onFrameAvailable(const BufferItem& /* item */) override {}
249 void onBuffersReleased() override {}
250 void onSidebandStreamChanged() override {}
251 };
252
253 // Create a EGLSurface
254 sp<IGraphicBufferProducer> producer;
255 sp<IGraphicBufferConsumer> consumer;
256 BufferQueue::createBufferQueue(&producer, &consumer);
257 consumer->consumerConnect(new DummyConsumer, false);
258 sp<Surface> mSTC = new Surface(producer);
259 sp<ANativeWindow> mANW = mSTC;
260
261 EGLSurface eglSurface = eglCreateWindowSurface(mEglDisplay, config, mANW.get(), NULL);
262 ASSERT_EQ(EGL_SUCCESS, eglGetError());
263 ASSERT_NE(EGL_NO_SURFACE, eglSurface);
264
265 EXPECT_TRUE(eglDestroySurface(mEglDisplay, eglSurface));
266}
Mathias Agopian2f739f82011-07-07 14:54:30 -0700267}