Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 17 | #include <renderengine/RenderEngine.h> |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 18 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Jorim Jaggi | 5b15ef6 | 2018-01-17 18:31:39 +0100 | [diff] [blame] | 21 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> |
| 22 | #include <configstore/Utils.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 23 | #include <log/log.h> |
Lloyd Pique | 63f9dbf | 2018-06-21 13:13:07 -0700 | [diff] [blame] | 24 | #include <private/gui/SyncFeatures.h> |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 25 | #include <renderengine/Image.h> |
| 26 | #include <renderengine/Mesh.h> |
| 27 | #include <renderengine/Surface.h> |
| 28 | #include <ui/Rect.h> |
| 29 | #include <ui/Region.h> |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 30 | #include <utils/KeyedVector.h> |
| 31 | #include "gl/GLES20RenderEngine.h" |
| 32 | #include "gl/GLExtensions.h" |
| 33 | #include "gl/ProgramCache.h" |
Jorim Jaggi | 5b15ef6 | 2018-01-17 18:31:39 +0100 | [diff] [blame] | 34 | |
| 35 | using namespace android::hardware::configstore; |
| 36 | using namespace android::hardware::configstore::V1_0; |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 37 | using namespace android::renderengine::gl; |
Jorim Jaggi | 5b15ef6 | 2018-01-17 18:31:39 +0100 | [diff] [blame] | 38 | |
Jiyong Park | 00b15b8 | 2017-08-10 20:30:56 +0900 | [diff] [blame] | 39 | extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 40 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 41 | namespace android { |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 42 | namespace renderengine { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 43 | |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 44 | RenderEngine::~RenderEngine() = default; |
| 45 | |
| 46 | namespace impl { |
| 47 | |
Chia-I Wu | d4d9c6f | 2017-11-09 16:55:31 -0800 | [diff] [blame] | 48 | std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) { |
| 49 | // initialize EGL for the default display |
| 50 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 51 | if (!eglInitialize(display, nullptr, nullptr)) { |
Chia-I Wu | d4d9c6f | 2017-11-09 16:55:31 -0800 | [diff] [blame] | 52 | LOG_ALWAYS_FATAL("failed to initialize EGL"); |
| 53 | } |
| 54 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 55 | GLExtensions& extensions = GLExtensions::getInstance(); |
Chia-I Wu | 767d7c9 | 2017-11-30 13:22:48 -0800 | [diff] [blame] | 56 | extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION), |
| 57 | eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS)); |
| 58 | |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 59 | // The code assumes that ES2 or later is available if this extension is |
| 60 | // supported. |
| 61 | EGLConfig config = EGL_NO_CONFIG; |
Chia-I Wu | 767d7c9 | 2017-11-30 13:22:48 -0800 | [diff] [blame] | 62 | if (!extensions.hasNoConfigContext()) { |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 63 | config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | EGLint renderableType = 0; |
| 67 | if (config == EGL_NO_CONFIG) { |
| 68 | renderableType = EGL_OPENGL_ES2_BIT; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 69 | } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) { |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 70 | LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE"); |
| 71 | } |
| 72 | EGLint contextClientVersion = 0; |
Mathias Agopian | 2185f8b | 2013-09-18 16:20:26 -0700 | [diff] [blame] | 73 | if (renderableType & EGL_OPENGL_ES2_BIT) { |
| 74 | contextClientVersion = 2; |
| 75 | } else if (renderableType & EGL_OPENGL_ES_BIT) { |
| 76 | contextClientVersion = 1; |
| 77 | } else { |
| 78 | LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs"); |
| 79 | } |
| 80 | |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 81 | std::vector<EGLint> contextAttributes; |
| 82 | contextAttributes.reserve(6); |
| 83 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| 84 | contextAttributes.push_back(contextClientVersion); |
Jorim Jaggi | 5b15ef6 | 2018-01-17 18:31:39 +0100 | [diff] [blame] | 85 | bool useContextPriority = overrideUseContextPriorityFromConfig(extensions.hasContextPriority()); |
| 86 | if (useContextPriority) { |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 87 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
| 88 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG); |
| 89 | } |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 90 | contextAttributes.push_back(EGL_NONE); |
| 91 | |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 92 | EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data()); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 93 | |
| 94 | // if can't create a GL context, we can only abort. |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 95 | LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed"); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 96 | |
| 97 | // now figure out what version of GL did we actually get |
| 98 | // NOTE: a dummy surface is not needed if KHR_create_context is supported |
| 99 | |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 100 | EGLConfig dummyConfig = config; |
| 101 | if (dummyConfig == EGL_NO_CONFIG) { |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 102 | dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 103 | } |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 104 | EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE}; |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 105 | EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 106 | LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer"); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 107 | EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt); |
| 108 | LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current"); |
| 109 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 110 | extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER), |
| 111 | glGetString(GL_VERSION), glGetString(GL_EXTENSIONS)); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 112 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 113 | GlesVersion version = parseGlesVersion(extensions.getVersion()); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 114 | |
| 115 | // initialize the renderer while GL is current |
| 116 | |
Chia-I Wu | b2c7624 | 2017-11-09 17:17:07 -0800 | [diff] [blame] | 117 | std::unique_ptr<RenderEngine> engine; |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 118 | switch (version) { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 119 | case GLES_VERSION_1_0: |
| 120 | case GLES_VERSION_1_1: |
| 121 | LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run."); |
| 122 | break; |
| 123 | case GLES_VERSION_2_0: |
| 124 | case GLES_VERSION_3_0: |
| 125 | engine = std::make_unique<GLES20RenderEngine>(featureFlags); |
| 126 | break; |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 127 | } |
Chia-I Wu | 2b6386e | 2017-11-09 13:03:17 -0800 | [diff] [blame] | 128 | engine->setEGLHandles(display, config, ctxt); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 129 | |
| 130 | ALOGI("OpenGL ES informations:"); |
| 131 | ALOGI("vendor : %s", extensions.getVendor()); |
| 132 | ALOGI("renderer : %s", extensions.getRenderer()); |
| 133 | ALOGI("version : %s", extensions.getVersion()); |
Chia-I Wu | 767d7c9 | 2017-11-30 13:22:48 -0800 | [diff] [blame] | 134 | ALOGI("extensions: %s", extensions.getExtensions()); |
Michael Lentine | 9ae79d8 | 2014-07-30 16:42:12 -0700 | [diff] [blame] | 135 | ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize()); |
| 136 | ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims()); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 137 | |
| 138 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 139 | eglDestroySurface(display, dummy); |
| 140 | |
| 141 | return engine; |
| 142 | } |
| 143 | |
Jorim Jaggi | 5b15ef6 | 2018-01-17 18:31:39 +0100 | [diff] [blame] | 144 | bool RenderEngine::overrideUseContextPriorityFromConfig(bool useContextPriority) { |
| 145 | OptionalBool ret; |
Lloyd Pique | 144e116 | 2017-12-20 16:44:52 -0800 | [diff] [blame] | 146 | ISurfaceFlingerConfigs::getService()->useContextPriority([&ret](OptionalBool b) { ret = b; }); |
Jorim Jaggi | 5b15ef6 | 2018-01-17 18:31:39 +0100 | [diff] [blame] | 147 | if (ret.specified) { |
| 148 | return ret.value; |
| 149 | } else { |
| 150 | return useContextPriority; |
| 151 | } |
| 152 | } |
| 153 | |
Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 154 | RenderEngine::RenderEngine(uint32_t featureFlags) |
| 155 | : mEGLDisplay(EGL_NO_DISPLAY), |
| 156 | mEGLConfig(nullptr), |
| 157 | mEGLContext(EGL_NO_CONTEXT), |
| 158 | mFeatureFlags(featureFlags) {} |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 159 | |
| 160 | RenderEngine::~RenderEngine() { |
Chia-I Wu | b01450b | 2017-11-09 16:55:31 -0800 | [diff] [blame] | 161 | eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 162 | eglTerminate(mEGLDisplay); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Chia-I Wu | 2b6386e | 2017-11-09 13:03:17 -0800 | [diff] [blame] | 165 | void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) { |
| 166 | mEGLDisplay = display; |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 167 | mEGLConfig = config; |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 168 | mEGLContext = ctxt; |
| 169 | } |
| 170 | |
Chia-I Wu | 2b6386e | 2017-11-09 13:03:17 -0800 | [diff] [blame] | 171 | EGLDisplay RenderEngine::getEGLDisplay() const { |
| 172 | return mEGLDisplay; |
| 173 | } |
| 174 | |
| 175 | EGLConfig RenderEngine::getEGLConfig() const { |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 176 | return mEGLConfig; |
| 177 | } |
| 178 | |
Lloyd Pique | 63f9dbf | 2018-06-21 13:13:07 -0700 | [diff] [blame] | 179 | bool RenderEngine::useNativeFenceSync() const { |
| 180 | return SyncFeatures::getInstance().useNativeFenceSync(); |
| 181 | } |
| 182 | |
| 183 | bool RenderEngine::useWaitSync() const { |
| 184 | return SyncFeatures::getInstance().useWaitSync(); |
| 185 | } |
| 186 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 187 | RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) { |
| 188 | int major, minor; |
| 189 | if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) { |
| 190 | if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) { |
| 191 | ALOGW("Unable to parse GL_VERSION string: \"%s\"", str); |
| 192 | return GLES_VERSION_1_0; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (major == 1 && minor == 0) return GLES_VERSION_1_0; |
| 197 | if (major == 1 && minor >= 1) return GLES_VERSION_1_1; |
| 198 | if (major == 2 && minor >= 0) return GLES_VERSION_2_0; |
| 199 | if (major == 3 && minor >= 0) return GLES_VERSION_3_0; |
| 200 | |
| 201 | ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor); |
| 202 | return GLES_VERSION_1_0; |
| 203 | } |
| 204 | |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 205 | void RenderEngine::dump(String8& result) { |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 206 | const GLExtensions& extensions = GLExtensions::getInstance(); |
Chia-I Wu | 767d7c9 | 2017-11-30 13:22:48 -0800 | [diff] [blame] | 207 | |
| 208 | result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion()); |
| 209 | result.appendFormat("%s\n", extensions.getEGLExtensions()); |
| 210 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 211 | result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(), |
| 212 | extensions.getVersion()); |
Chia-I Wu | 767d7c9 | 2017-11-30 13:22:48 -0800 | [diff] [blame] | 213 | result.appendFormat("%s\n", extensions.getExtensions()); |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 216 | static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute, |
| 217 | EGLint wanted, EGLConfig* outConfig) { |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 218 | EGLint numConfigs = -1, n = 0; |
Peiyong Lin | 566a3b4 | 2018-01-09 18:22:43 -0800 | [diff] [blame] | 219 | eglGetConfigs(dpy, nullptr, 0, &numConfigs); |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 220 | EGLConfig* const configs = new EGLConfig[numConfigs]; |
| 221 | eglChooseConfig(dpy, attrs, configs, numConfigs, &n); |
| 222 | |
| 223 | if (n) { |
| 224 | if (attribute != EGL_NONE) { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 225 | for (int i = 0; i < n; i++) { |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 226 | EGLint value = 0; |
| 227 | eglGetConfigAttrib(dpy, configs[i], attribute, &value); |
| 228 | if (wanted == value) { |
| 229 | *outConfig = configs[i]; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 230 | delete[] configs; |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 231 | return NO_ERROR; |
| 232 | } |
| 233 | } |
| 234 | } else { |
| 235 | // just pick the first one |
| 236 | *outConfig = configs[0]; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 237 | delete[] configs; |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 238 | return NO_ERROR; |
| 239 | } |
| 240 | } |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 241 | delete[] configs; |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 242 | return NAME_NOT_FOUND; |
| 243 | } |
| 244 | |
| 245 | class EGLAttributeVector { |
| 246 | struct Attribute; |
| 247 | class Adder; |
| 248 | friend class Adder; |
| 249 | KeyedVector<Attribute, EGLint> mList; |
| 250 | struct Attribute { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 251 | Attribute() : v(0){}; |
| 252 | explicit Attribute(EGLint v) : v(v) {} |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 253 | EGLint v; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 254 | bool operator<(const Attribute& other) const { |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 255 | // this places EGL_NONE at the end |
| 256 | EGLint lhs(v); |
| 257 | EGLint rhs(other.v); |
| 258 | if (lhs == EGL_NONE) lhs = 0x7FFFFFFF; |
| 259 | if (rhs == EGL_NONE) rhs = 0x7FFFFFFF; |
| 260 | return lhs < rhs; |
| 261 | } |
| 262 | }; |
| 263 | class Adder { |
| 264 | friend class EGLAttributeVector; |
| 265 | EGLAttributeVector& v; |
| 266 | EGLint attribute; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 267 | Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {} |
| 268 | |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 269 | public: |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 270 | void operator=(EGLint value) { |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 271 | if (attribute != EGL_NONE) { |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 272 | v.mList.add(Attribute(attribute), value); |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 273 | } |
| 274 | } |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 275 | operator EGLint() const { return v.mList[attribute]; } |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 276 | }; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 277 | |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 278 | public: |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 279 | EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); } |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 280 | void remove(EGLint attribute) { |
| 281 | if (attribute != EGL_NONE) { |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 282 | mList.removeItem(Attribute(attribute)); |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 285 | Adder operator[](EGLint attribute) { return Adder(*this, attribute); } |
| 286 | EGLint operator[](EGLint attribute) const { return mList[attribute]; } |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 287 | // cast-operator to (EGLint const*) |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 288 | operator EGLint const*() const { return &mList.keyAt(0).v; } |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 289 | }; |
| 290 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 291 | static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType, |
| 292 | EGLConfig* config) { |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 293 | // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if |
| 294 | // it is to be used with WIFI displays |
| 295 | status_t err; |
| 296 | EGLint wantedAttribute; |
| 297 | EGLint wantedAttributeValue; |
| 298 | |
| 299 | EGLAttributeVector attribs; |
| 300 | if (renderableType) { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 301 | attribs[EGL_RENDERABLE_TYPE] = renderableType; |
| 302 | attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE; |
| 303 | attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT; |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 304 | attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE; |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 305 | attribs[EGL_RED_SIZE] = 8; |
| 306 | attribs[EGL_GREEN_SIZE] = 8; |
| 307 | attribs[EGL_BLUE_SIZE] = 8; |
| 308 | attribs[EGL_ALPHA_SIZE] = 8; |
| 309 | wantedAttribute = EGL_NONE; |
| 310 | wantedAttributeValue = EGL_NONE; |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 311 | } else { |
| 312 | // if no renderable type specified, fallback to a simplified query |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 313 | wantedAttribute = EGL_NATIVE_VISUAL_ID; |
| 314 | wantedAttributeValue = format; |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 317 | err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config); |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 318 | if (err == NO_ERROR) { |
| 319 | EGLint caveat; |
| 320 | if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat)) |
| 321 | ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!"); |
| 322 | } |
| 323 | |
| 324 | return err; |
| 325 | } |
| 326 | |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 327 | EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) { |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 328 | status_t err; |
| 329 | EGLConfig config; |
| 330 | |
| 331 | // First try to get an ES2 config |
| 332 | err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config); |
| 333 | if (err != NO_ERROR) { |
| 334 | // If ES2 fails, try ES1 |
| 335 | err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config); |
| 336 | if (err != NO_ERROR) { |
| 337 | // still didn't work, probably because we're on the emulator... |
| 338 | // try a simplified query |
| 339 | ALOGW("no suitable EGLConfig found, trying a simpler query"); |
| 340 | err = selectEGLConfig(display, format, 0, &config); |
| 341 | if (err != NO_ERROR) { |
| 342 | // this EGL is too lame for android |
| 343 | LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 348 | if (logConfig) { |
| 349 | // print some debugging info |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 350 | EGLint r, g, b, a; |
| 351 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 352 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 353 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 354 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); |
| 355 | ALOGI("EGL information:"); |
| 356 | ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR)); |
| 357 | ALOGI("version : %s", eglQueryString(display, EGL_VERSION)); |
| 358 | ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS)); |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 359 | ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported"); |
Steven Thomas | d7f49c5 | 2017-07-26 18:48:28 -0700 | [diff] [blame] | 360 | ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config); |
| 361 | } |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 362 | |
| 363 | return config; |
| 364 | } |
| 365 | |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 366 | } // namespace impl |
| 367 | } // namespace renderengine |
| 368 | } // namespace android |