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 | |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 17 | #include <log/log.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 18 | #include <ui/Rect.h> |
| 19 | #include <ui/Region.h> |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 20 | |
| 21 | #include "RenderEngine.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 22 | #include "GLES20RenderEngine.h" |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 23 | #include "GLExtensions.h" |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 24 | #include "Mesh.h" |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 25 | |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | #include <SurfaceFlinger.h> |
| 28 | |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 29 | EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); |
| 30 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 31 | // --------------------------------------------------------------------------- |
| 32 | namespace android { |
| 33 | // --------------------------------------------------------------------------- |
| 34 | |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 35 | static bool findExtension(const char* exts, const char* name) { |
| 36 | if (!exts) |
| 37 | return false; |
| 38 | size_t len = strlen(name); |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 39 | |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 40 | const char* pos = exts; |
| 41 | while ((pos = strstr(pos, name)) != NULL) { |
| 42 | if (pos[len] == '\0' || pos[len] == ' ') |
| 43 | return true; |
| 44 | pos += len; |
Mathias Agopian | 2185f8b | 2013-09-18 16:20:26 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | |
| 50 | RenderEngine* RenderEngine::create(EGLDisplay display, int hwcFormat) { |
| 51 | // EGL_ANDROIDX_no_config_context is an experimental extension with no |
| 52 | // written specification. It will be replaced by something more formal. |
| 53 | // SurfaceFlinger is using it to allow a single EGLContext to render to |
| 54 | // both a 16-bit primary display framebuffer and a 32-bit virtual display |
| 55 | // framebuffer. |
| 56 | // |
Courtney Goeltzenleuchter | 0ebaac3 | 2017-04-13 12:17:03 -0600 | [diff] [blame] | 57 | // EGL_KHR_no_config_context is official extension to allow creating a |
| 58 | // context that works with any surface of a display. |
| 59 | // |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 60 | // The code assumes that ES2 or later is available if this extension is |
| 61 | // supported. |
| 62 | EGLConfig config = EGL_NO_CONFIG; |
Courtney Goeltzenleuchter | 0ebaac3 | 2017-04-13 12:17:03 -0600 | [diff] [blame] | 63 | if (!findExtension(eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS), |
| 64 | "EGL_ANDROIDX_no_config_context") && |
| 65 | !findExtension(eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS), |
| 66 | "EGL_KHR_no_config_context")) { |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 67 | config = chooseEglConfig(display, hwcFormat); |
| 68 | } |
| 69 | |
| 70 | EGLint renderableType = 0; |
| 71 | if (config == EGL_NO_CONFIG) { |
| 72 | renderableType = EGL_OPENGL_ES2_BIT; |
| 73 | } else if (!eglGetConfigAttrib(display, config, |
| 74 | EGL_RENDERABLE_TYPE, &renderableType)) { |
| 75 | LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE"); |
| 76 | } |
| 77 | EGLint contextClientVersion = 0; |
Mathias Agopian | 2185f8b | 2013-09-18 16:20:26 -0700 | [diff] [blame] | 78 | if (renderableType & EGL_OPENGL_ES2_BIT) { |
| 79 | contextClientVersion = 2; |
| 80 | } else if (renderableType & EGL_OPENGL_ES_BIT) { |
| 81 | contextClientVersion = 1; |
| 82 | } else { |
| 83 | LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs"); |
| 84 | } |
| 85 | |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 86 | std::vector<EGLint> contextAttributes; |
| 87 | contextAttributes.reserve(6); |
| 88 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| 89 | contextAttributes.push_back(contextClientVersion); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 90 | #ifdef EGL_IMG_context_priority |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 91 | if (SurfaceFlinger::useContextPriority) { |
| 92 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
| 93 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG); |
| 94 | } |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 95 | #endif |
Fabien Sanglard | c93afd5 | 2017-03-13 13:02:42 -0700 | [diff] [blame] | 96 | contextAttributes.push_back(EGL_NONE); |
| 97 | contextAttributes.push_back(EGL_NONE); |
| 98 | |
| 99 | EGLContext ctxt = eglCreateContext(display, config, NULL, |
| 100 | contextAttributes.data()); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 101 | |
| 102 | // if can't create a GL context, we can only abort. |
| 103 | LOG_ALWAYS_FATAL_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed"); |
| 104 | |
| 105 | |
| 106 | // now figure out what version of GL did we actually get |
| 107 | // NOTE: a dummy surface is not needed if KHR_create_context is supported |
| 108 | |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 109 | EGLConfig dummyConfig = config; |
| 110 | if (dummyConfig == EGL_NO_CONFIG) { |
| 111 | dummyConfig = chooseEglConfig(display, hwcFormat); |
| 112 | } |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 113 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE }; |
Jesse Hall | 19e8729 | 2013-12-23 21:02:15 -0800 | [diff] [blame] | 114 | EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 115 | LOG_ALWAYS_FATAL_IF(dummy==EGL_NO_SURFACE, "can't create dummy pbuffer"); |
| 116 | EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt); |
| 117 | LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current"); |
| 118 | |
| 119 | GLExtensions& extensions(GLExtensions::getInstance()); |
| 120 | extensions.initWithGLStrings( |
| 121 | glGetString(GL_VENDOR), |
| 122 | glGetString(GL_RENDERER), |
| 123 | glGetString(GL_VERSION), |
| 124 | glGetString(GL_EXTENSIONS)); |
| 125 | |
| 126 | GlesVersion version = parseGlesVersion( extensions.getVersion() ); |
| 127 | |
| 128 | // initialize the renderer while GL is current |
| 129 | |
| 130 | RenderEngine* engine = NULL; |
| 131 | switch (version) { |
| 132 | case GLES_VERSION_1_0: |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 133 | case GLES_VERSION_1_1: |
Fabien Sanglard | efb9345 | 2016-10-04 14:02:11 -0700 | [diff] [blame] | 134 | LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run."); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 135 | break; |
| 136 | case GLES_VERSION_2_0: |
| 137 | case GLES_VERSION_3_0: |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 138 | engine = new GLES20RenderEngine(); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 139 | break; |
| 140 | } |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 141 | engine->setEGLHandles(config, ctxt); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 142 | |
| 143 | ALOGI("OpenGL ES informations:"); |
| 144 | ALOGI("vendor : %s", extensions.getVendor()); |
| 145 | ALOGI("renderer : %s", extensions.getRenderer()); |
| 146 | ALOGI("version : %s", extensions.getVersion()); |
| 147 | ALOGI("extensions: %s", extensions.getExtension()); |
Michael Lentine | 9ae79d8 | 2014-07-30 16:42:12 -0700 | [diff] [blame] | 148 | ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize()); |
| 149 | ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims()); |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 150 | |
| 151 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 152 | eglDestroySurface(display, dummy); |
| 153 | |
| 154 | return engine; |
| 155 | } |
| 156 | |
Pablo Ceballos | 53390e1 | 2015-08-04 11:25:59 -0700 | [diff] [blame] | 157 | RenderEngine::RenderEngine() : mEGLConfig(NULL), mEGLContext(EGL_NO_CONTEXT) { |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | RenderEngine::~RenderEngine() { |
| 161 | } |
| 162 | |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 163 | void RenderEngine::setEGLHandles(EGLConfig config, EGLContext ctxt) { |
| 164 | mEGLConfig = config; |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 165 | mEGLContext = ctxt; |
| 166 | } |
| 167 | |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 168 | EGLContext RenderEngine::getEGLConfig() const { |
| 169 | return mEGLConfig; |
| 170 | } |
| 171 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 172 | EGLContext RenderEngine::getEGLContext() const { |
| 173 | return mEGLContext; |
| 174 | } |
| 175 | |
| 176 | void RenderEngine::checkErrors() const { |
| 177 | do { |
| 178 | // there could be more than one error flag |
| 179 | GLenum error = glGetError(); |
| 180 | if (error == GL_NO_ERROR) |
| 181 | break; |
| 182 | ALOGE("GL error 0x%04x", int(error)); |
| 183 | } while (true); |
| 184 | } |
| 185 | |
| 186 | RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) { |
| 187 | int major, minor; |
| 188 | if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) { |
| 189 | if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) { |
| 190 | ALOGW("Unable to parse GL_VERSION string: \"%s\"", str); |
| 191 | return GLES_VERSION_1_0; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (major == 1 && minor == 0) return GLES_VERSION_1_0; |
| 196 | if (major == 1 && minor >= 1) return GLES_VERSION_1_1; |
| 197 | if (major == 2 && minor >= 0) return GLES_VERSION_2_0; |
| 198 | if (major == 3 && minor >= 0) return GLES_VERSION_3_0; |
| 199 | |
| 200 | ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor); |
| 201 | return GLES_VERSION_1_0; |
| 202 | } |
| 203 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 204 | void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, |
| 205 | float red, float green, float blue, float alpha) { |
| 206 | size_t c; |
| 207 | Rect const* r = region.getArray(&c); |
| 208 | Mesh mesh(Mesh::TRIANGLES, c*6, 2); |
Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 209 | Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 210 | for (size_t i=0 ; i<c ; i++, r++) { |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 211 | position[i*6 + 0].x = r->left; |
| 212 | position[i*6 + 0].y = height - r->top; |
| 213 | position[i*6 + 1].x = r->left; |
| 214 | position[i*6 + 1].y = height - r->bottom; |
| 215 | position[i*6 + 2].x = r->right; |
| 216 | position[i*6 + 2].y = height - r->bottom; |
| 217 | position[i*6 + 3].x = r->left; |
| 218 | position[i*6 + 3].y = height - r->top; |
| 219 | position[i*6 + 4].x = r->right; |
| 220 | position[i*6 + 4].y = height - r->bottom; |
| 221 | position[i*6 + 5].x = r->right; |
| 222 | position[i*6 + 5].y = height - r->top; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 223 | } |
Mathias Agopian | 19733a3 | 2013-08-28 18:13:56 -0700 | [diff] [blame] | 224 | setupFillWithColor(red, green, blue, alpha); |
| 225 | drawMesh(mesh); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Riley Andrews | 9707f4d | 2014-10-23 16:17:04 -0700 | [diff] [blame] | 228 | void RenderEngine::flush() { |
| 229 | glFlush(); |
| 230 | } |
| 231 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 232 | void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) { |
| 233 | glClearColor(red, green, blue, alpha); |
| 234 | glClear(GL_COLOR_BUFFER_BIT); |
| 235 | } |
| 236 | |
| 237 | void RenderEngine::setScissor( |
| 238 | uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) { |
| 239 | glScissor(left, bottom, right, top); |
| 240 | glEnable(GL_SCISSOR_TEST); |
| 241 | } |
| 242 | |
| 243 | void RenderEngine::disableScissor() { |
| 244 | glDisable(GL_SCISSOR_TEST); |
| 245 | } |
| 246 | |
| 247 | void RenderEngine::genTextures(size_t count, uint32_t* names) { |
| 248 | glGenTextures(count, names); |
| 249 | } |
| 250 | |
| 251 | void RenderEngine::deleteTextures(size_t count, uint32_t const* names) { |
| 252 | glDeleteTextures(count, names); |
| 253 | } |
| 254 | |
Mathias Agopian | d555684 | 2013-09-19 17:08:37 -0700 | [diff] [blame] | 255 | void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) { |
| 256 | glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 257 | } |
| 258 | |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 259 | void RenderEngine::dump(String8& result) { |
| 260 | const GLExtensions& extensions(GLExtensions::getInstance()); |
| 261 | result.appendFormat("GLES: %s, %s, %s\n", |
| 262 | extensions.getVendor(), |
| 263 | extensions.getRenderer(), |
| 264 | extensions.getVersion()); |
| 265 | result.appendFormat("%s\n", extensions.getExtension()); |
| 266 | } |
| 267 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 268 | // --------------------------------------------------------------------------- |
| 269 | |
| 270 | RenderEngine::BindImageAsFramebuffer::BindImageAsFramebuffer( |
| 271 | RenderEngine& engine, EGLImageKHR image) : mEngine(engine) |
| 272 | { |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 273 | mEngine.bindImageAsFramebuffer(image, &mTexName, &mFbName, &mStatus); |
| 274 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 275 | ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES, |
| 276 | "glCheckFramebufferStatusOES error %d", mStatus); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | RenderEngine::BindImageAsFramebuffer::~BindImageAsFramebuffer() { |
| 280 | // back to main framebuffer |
Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 281 | mEngine.unbindFramebuffer(mTexName, mFbName); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | status_t RenderEngine::BindImageAsFramebuffer::getStatus() const { |
| 285 | return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE; |
| 286 | } |
| 287 | |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 288 | // --------------------------------------------------------------------------- |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 289 | |
| 290 | static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, |
| 291 | EGLint attribute, EGLint wanted, EGLConfig* outConfig) { |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 292 | EGLint numConfigs = -1, n = 0; |
| 293 | eglGetConfigs(dpy, NULL, 0, &numConfigs); |
| 294 | EGLConfig* const configs = new EGLConfig[numConfigs]; |
| 295 | eglChooseConfig(dpy, attrs, configs, numConfigs, &n); |
| 296 | |
| 297 | if (n) { |
| 298 | if (attribute != EGL_NONE) { |
| 299 | for (int i=0 ; i<n ; i++) { |
| 300 | EGLint value = 0; |
| 301 | eglGetConfigAttrib(dpy, configs[i], attribute, &value); |
| 302 | if (wanted == value) { |
| 303 | *outConfig = configs[i]; |
| 304 | delete [] configs; |
| 305 | return NO_ERROR; |
| 306 | } |
| 307 | } |
| 308 | } else { |
| 309 | // just pick the first one |
| 310 | *outConfig = configs[0]; |
| 311 | delete [] configs; |
| 312 | return NO_ERROR; |
| 313 | } |
| 314 | } |
| 315 | delete [] configs; |
| 316 | return NAME_NOT_FOUND; |
| 317 | } |
| 318 | |
| 319 | class EGLAttributeVector { |
| 320 | struct Attribute; |
| 321 | class Adder; |
| 322 | friend class Adder; |
| 323 | KeyedVector<Attribute, EGLint> mList; |
| 324 | struct Attribute { |
Pablo Ceballos | 53390e1 | 2015-08-04 11:25:59 -0700 | [diff] [blame] | 325 | Attribute() : v(0) {}; |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 326 | explicit Attribute(EGLint v) : v(v) { } |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 327 | EGLint v; |
| 328 | bool operator < (const Attribute& other) const { |
| 329 | // this places EGL_NONE at the end |
| 330 | EGLint lhs(v); |
| 331 | EGLint rhs(other.v); |
| 332 | if (lhs == EGL_NONE) lhs = 0x7FFFFFFF; |
| 333 | if (rhs == EGL_NONE) rhs = 0x7FFFFFFF; |
| 334 | return lhs < rhs; |
| 335 | } |
| 336 | }; |
| 337 | class Adder { |
| 338 | friend class EGLAttributeVector; |
| 339 | EGLAttributeVector& v; |
| 340 | EGLint attribute; |
| 341 | Adder(EGLAttributeVector& v, EGLint attribute) |
| 342 | : v(v), attribute(attribute) { |
| 343 | } |
| 344 | public: |
| 345 | void operator = (EGLint value) { |
| 346 | if (attribute != EGL_NONE) { |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 347 | v.mList.add(Attribute(attribute), value); |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | operator EGLint () const { return v.mList[attribute]; } |
| 351 | }; |
| 352 | public: |
| 353 | EGLAttributeVector() { |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 354 | mList.add(Attribute(EGL_NONE), EGL_NONE); |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 355 | } |
| 356 | void remove(EGLint attribute) { |
| 357 | if (attribute != EGL_NONE) { |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 358 | mList.removeItem(Attribute(attribute)); |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | Adder operator [] (EGLint attribute) { |
| 362 | return Adder(*this, attribute); |
| 363 | } |
| 364 | EGLint operator [] (EGLint attribute) const { |
| 365 | return mList[attribute]; |
| 366 | } |
| 367 | // cast-operator to (EGLint const*) |
| 368 | operator EGLint const* () const { return &mList.keyAt(0).v; } |
| 369 | }; |
| 370 | |
| 371 | |
| 372 | static status_t selectEGLConfig(EGLDisplay display, EGLint format, |
| 373 | EGLint renderableType, EGLConfig* config) { |
| 374 | // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if |
| 375 | // it is to be used with WIFI displays |
| 376 | status_t err; |
| 377 | EGLint wantedAttribute; |
| 378 | EGLint wantedAttributeValue; |
| 379 | |
| 380 | EGLAttributeVector attribs; |
| 381 | if (renderableType) { |
| 382 | attribs[EGL_RENDERABLE_TYPE] = renderableType; |
| 383 | attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE; |
| 384 | attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT; |
| 385 | attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE; |
| 386 | attribs[EGL_RED_SIZE] = 8; |
| 387 | attribs[EGL_GREEN_SIZE] = 8; |
| 388 | attribs[EGL_BLUE_SIZE] = 8; |
neo.he | e7f3972 | 2017-03-21 11:48:36 +0800 | [diff] [blame] | 389 | attribs[EGL_ALPHA_SIZE] = 8; |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 390 | wantedAttribute = EGL_NONE; |
| 391 | wantedAttributeValue = EGL_NONE; |
| 392 | } else { |
| 393 | // if no renderable type specified, fallback to a simplified query |
| 394 | wantedAttribute = EGL_NATIVE_VISUAL_ID; |
| 395 | wantedAttributeValue = format; |
| 396 | } |
| 397 | |
| 398 | err = selectConfigForAttribute(display, attribs, |
| 399 | wantedAttribute, wantedAttributeValue, config); |
| 400 | if (err == NO_ERROR) { |
| 401 | EGLint caveat; |
| 402 | if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat)) |
| 403 | ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!"); |
| 404 | } |
| 405 | |
| 406 | return err; |
| 407 | } |
| 408 | |
| 409 | EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format) { |
| 410 | status_t err; |
| 411 | EGLConfig config; |
| 412 | |
| 413 | // First try to get an ES2 config |
| 414 | err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config); |
| 415 | if (err != NO_ERROR) { |
| 416 | // If ES2 fails, try ES1 |
| 417 | err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config); |
| 418 | if (err != NO_ERROR) { |
| 419 | // still didn't work, probably because we're on the emulator... |
| 420 | // try a simplified query |
| 421 | ALOGW("no suitable EGLConfig found, trying a simpler query"); |
| 422 | err = selectEGLConfig(display, format, 0, &config); |
| 423 | if (err != NO_ERROR) { |
| 424 | // this EGL is too lame for android |
| 425 | LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // print some debugging info |
| 431 | EGLint r,g,b,a; |
| 432 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); |
| 433 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); |
| 434 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); |
| 435 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); |
| 436 | ALOGI("EGL information:"); |
| 437 | ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR)); |
| 438 | ALOGI("version : %s", eglQueryString(display, EGL_VERSION)); |
| 439 | ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS)); |
| 440 | ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported"); |
| 441 | ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config); |
| 442 | |
| 443 | return config; |
| 444 | } |
| 445 | |
Dan Stoza | 4e63777 | 2016-07-28 13:31:51 -0700 | [diff] [blame] | 446 | |
| 447 | void RenderEngine::primeCache() const { |
| 448 | // Getting the ProgramCache instance causes it to prime its shader cache, |
| 449 | // which is performed in its constructor |
| 450 | ProgramCache::getInstance(); |
| 451 | } |
| 452 | |
Jesse Hall | 05f8c70 | 2013-12-23 20:44:38 -0800 | [diff] [blame] | 453 | // --------------------------------------------------------------------------- |
Mathias Agopian | 875d8e1 | 2013-06-07 15:35:48 -0700 | [diff] [blame] | 454 | }; // namespace android |
| 455 | // --------------------------------------------------------------------------- |