| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 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 |  | 
| Courtney Goeltzenleuchter | f29f287 | 2017-03-28 17:29:52 -0600 | [diff] [blame] | 19 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> | 
|  | 20 |  | 
|  | 21 | #include <configstore/Utils.h> | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 22 | #include <utils/String8.h> | 
|  | 23 |  | 
|  | 24 | #include <EGL/egl.h> | 
| Mathias Agopian | e3c697f | 2013-02-14 17:11:02 -0800 | [diff] [blame] | 25 | #include <gui/Surface.h> | 
| Mathias Agopian | 2b5dd40 | 2017-02-07 17:36:19 -0800 | [diff] [blame] | 26 | #include <gui/IConsumerListener.h> | 
|  | 27 | #include <gui/IProducerListener.h> | 
|  | 28 | #include <gui/IGraphicBufferConsumer.h> | 
|  | 29 | #include <gui/BufferQueue.h> | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 30 |  | 
| Courtney Goeltzenleuchter | f29f287 | 2017-03-28 17:29:52 -0600 | [diff] [blame] | 31 | #define PIXEL_FORMAT_FLOAT "EGL_EXT_pixel_format_float" | 
|  | 32 |  | 
|  | 33 | bool 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 Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 46 | namespace android { | 
|  | 47 |  | 
| Kalle Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 48 | #define EGL_UNSIGNED_TRUE static_cast<EGLBoolean>(EGL_TRUE) | 
|  | 49 |  | 
| Courtney Goeltzenleuchter | f29f287 | 2017-03-28 17:29:52 -0600 | [diff] [blame] | 50 | // retrieve wide-color setting from configstore | 
|  | 51 | using namespace android::hardware::configstore; | 
|  | 52 |  | 
|  | 53 | static bool hasWideColorDisplay = | 
|  | 54 | getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasWideColorDisplay>(false); | 
|  | 55 |  | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 56 | class EGLTest : public ::testing::Test { | 
|  | 57 | protected: | 
|  | 58 | EGLDisplay mEglDisplay; | 
|  | 59 |  | 
|  | 60 | protected: | 
|  | 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 Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 80 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 81 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); | 
|  | 82 | } | 
|  | 83 | }; | 
|  | 84 |  | 
|  | 85 | TEST_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 Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 97 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 98 | 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 Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 104 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 105 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); | 
|  | 106 | success = eglGetConfigAttrib(mEglDisplay, config, EGL_GREEN_SIZE, &components[1]); | 
| Kalle Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 107 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 108 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); | 
|  | 109 | success = eglGetConfigAttrib(mEglDisplay, config, EGL_BLUE_SIZE, &components[2]); | 
| Kalle Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 110 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 111 | 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 Lam | 1cbcb98 | 2012-04-16 22:21:02 -0700 | [diff] [blame] | 118 | TEST_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 Agopian | a4e1952 | 2013-07-31 20:09:53 -0700 | [diff] [blame] | 132 | struct DummyConsumer : public BnConsumerListener { | 
| Brian Anderson | 5ea5e59 | 2016-12-01 16:54:33 -0800 | [diff] [blame] | 133 | void onFrameAvailable(const BufferItem& /* item */) override {} | 
|  | 134 | void onBuffersReleased() override {} | 
|  | 135 | void onSidebandStreamChanged() override {} | 
| Mathias Agopian | 595264f | 2013-07-16 22:56:09 -0700 | [diff] [blame] | 136 | }; | 
|  | 137 |  | 
| Daniel Lam | 1cbcb98 | 2012-04-16 22:21:02 -0700 | [diff] [blame] | 138 | // Create a EGLSurface | 
| Dan Stoza | 5603a2f | 2014-04-07 13:41:37 -0700 | [diff] [blame] | 139 | 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 Lam | 1cbcb98 | 2012-04-16 22:21:02 -0700 | [diff] [blame] | 144 | 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 Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 155 | TEST_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 Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 171 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 172 | 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 Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 178 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 179 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); | 
|  | 180 | success = eglGetConfigAttrib(mEglDisplay, config, EGL_GREEN_SIZE, &components[1]); | 
| Kalle Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 181 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 182 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); | 
|  | 183 | success = eglGetConfigAttrib(mEglDisplay, config, EGL_BLUE_SIZE, &components[2]); | 
| Kalle Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 184 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 185 | ASSERT_EQ(EGL_SUCCESS, eglGetError()); | 
|  | 186 | success = eglGetConfigAttrib(mEglDisplay, config, EGL_ALPHA_SIZE, &components[3]); | 
| Kalle Raita | 4cf3637 | 2017-01-13 10:18:36 -0800 | [diff] [blame] | 187 | ASSERT_EQ(EGL_UNSIGNED_TRUE, success); | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 188 | 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 Goeltzenleuchter | f29f287 | 2017-03-28 17:29:52 -0600 | [diff] [blame] | 196 | TEST_F(EGLTest, EGLConfigFP16) { | 
|  | 197 | EGLint numConfigs; | 
|  | 198 | EGLConfig config; | 
|  | 199 | EGLBoolean success; | 
| Mathias Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 200 |  | 
| Courtney Goeltzenleuchter | f29f287 | 2017-03-28 17:29:52 -0600 | [diff] [blame] | 201 | 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 Agopian | 2f739f8 | 2011-07-07 14:54:30 -0700 | [diff] [blame] | 267 | } |