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