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