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