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