EglManager.cpp revision 3b20251a355c88193c439f928a84ae69483fb488
1/* 2 * Copyright (C) 2014 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#define LOG_TAG "EglContext" 18 19#include "EglManager.h" 20 21#include <cutils/log.h> 22#include <cutils/properties.h> 23 24#include "../RenderState.h" 25#include "RenderThread.h" 26 27#define PROPERTY_RENDER_DIRTY_REGIONS "debug.hwui.render_dirty_regions" 28#define GLES_VERSION 2 29 30// Android-specific addition that is used to show when frames began in systrace 31EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); 32 33namespace android { 34namespace uirenderer { 35namespace renderthread { 36 37#define ERROR_CASE(x) case x: return #x; 38static const char* egl_error_str(EGLint error) { 39 switch (error) { 40 ERROR_CASE(EGL_SUCCESS) 41 ERROR_CASE(EGL_NOT_INITIALIZED) 42 ERROR_CASE(EGL_BAD_ACCESS) 43 ERROR_CASE(EGL_BAD_ALLOC) 44 ERROR_CASE(EGL_BAD_ATTRIBUTE) 45 ERROR_CASE(EGL_BAD_CONFIG) 46 ERROR_CASE(EGL_BAD_CONTEXT) 47 ERROR_CASE(EGL_BAD_CURRENT_SURFACE) 48 ERROR_CASE(EGL_BAD_DISPLAY) 49 ERROR_CASE(EGL_BAD_MATCH) 50 ERROR_CASE(EGL_BAD_NATIVE_PIXMAP) 51 ERROR_CASE(EGL_BAD_NATIVE_WINDOW) 52 ERROR_CASE(EGL_BAD_PARAMETER) 53 ERROR_CASE(EGL_BAD_SURFACE) 54 ERROR_CASE(EGL_CONTEXT_LOST) 55 default: 56 return "Unknown error"; 57 } 58} 59static const char* egl_error_str() { 60 return egl_error_str(eglGetError()); 61} 62 63static bool load_dirty_regions_property() { 64 char buf[PROPERTY_VALUE_MAX]; 65 int len = property_get(PROPERTY_RENDER_DIRTY_REGIONS, buf, "true"); 66 return !strncasecmp("true", buf, len); 67} 68 69EglManager::EglManager(RenderThread& thread) 70 : mRenderThread(thread) 71 , mEglDisplay(EGL_NO_DISPLAY) 72 , mEglConfig(0) 73 , mEglContext(EGL_NO_CONTEXT) 74 , mPBufferSurface(EGL_NO_SURFACE) 75 , mRequestDirtyRegions(load_dirty_regions_property()) 76 , mCurrentSurface(EGL_NO_SURFACE) 77 , mAtlasMap(NULL) 78 , mAtlasMapSize(0) { 79 mCanSetDirtyRegions = mRequestDirtyRegions; 80 ALOGD("Render dirty regions requested: %s", mRequestDirtyRegions ? "true" : "false"); 81} 82 83void EglManager::initialize() { 84 if (hasEglContext()) return; 85 86 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); 87 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, 88 "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); 89 90 EGLint major, minor; 91 LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, 92 "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); 93 94 ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); 95 96 loadConfig(); 97 createContext(); 98 usePBufferSurface(); 99 mRenderThread.renderState().onGLContextCreated(); 100 initAtlas(); 101} 102 103bool EglManager::hasEglContext() { 104 return mEglDisplay != EGL_NO_DISPLAY; 105} 106 107void EglManager::requireGlContext() { 108 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, "No EGL context"); 109 110 // We don't care *WHAT* surface is active, just that one is active to give 111 // us access to the GL context 112 if (mCurrentSurface == EGL_NO_SURFACE) { 113 usePBufferSurface(); 114 } 115} 116 117void EglManager::loadConfig() { 118 EGLint swapBehavior = mCanSetDirtyRegions ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; 119 EGLint attribs[] = { 120 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 121 EGL_RED_SIZE, 8, 122 EGL_GREEN_SIZE, 8, 123 EGL_BLUE_SIZE, 8, 124 EGL_ALPHA_SIZE, 8, 125 EGL_DEPTH_SIZE, 0, 126 EGL_CONFIG_CAVEAT, EGL_NONE, 127 EGL_STENCIL_SIZE, Stencil::getStencilSize(), 128 EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, 129 EGL_NONE 130 }; 131 132 EGLint num_configs = 1; 133 if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) 134 || num_configs != 1) { 135 // Failed to get a valid config 136 if (mCanSetDirtyRegions) { 137 ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); 138 // Try again without dirty regions enabled 139 mCanSetDirtyRegions = false; 140 loadConfig(); 141 } else { 142 LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); 143 } 144 } 145} 146 147void EglManager::createContext() { 148 EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE }; 149 mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); 150 LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, 151 "Failed to create context, error = %s", egl_error_str()); 152} 153 154void EglManager::setTextureAtlas(const sp<GraphicBuffer>& buffer, 155 int64_t* map, size_t mapSize) { 156 157 // Already initialized 158 if (mAtlasBuffer.get()) { 159 ALOGW("Multiple calls to setTextureAtlas!"); 160 delete map; 161 return; 162 } 163 164 mAtlasBuffer = buffer; 165 mAtlasMap = map; 166 mAtlasMapSize = mapSize; 167 168 if (hasEglContext()) { 169 usePBufferSurface(); 170 initAtlas(); 171 } 172} 173 174void EglManager::initAtlas() { 175 if (mAtlasBuffer.get()) { 176 Caches::getInstance().assetAtlas.init(mAtlasBuffer, mAtlasMap, mAtlasMapSize); 177 } 178} 179 180void EglManager::usePBufferSurface() { 181 LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, 182 "usePBufferSurface() called on uninitialized GlobalContext!"); 183 184 if (mPBufferSurface == EGL_NO_SURFACE) { 185 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; 186 mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); 187 } 188 makeCurrent(mPBufferSurface); 189} 190 191EGLSurface EglManager::createSurface(EGLNativeWindowType window) { 192 initialize(); 193 EGLSurface surface = eglCreateWindowSurface(mEglDisplay, mEglConfig, window, NULL); 194 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, 195 "Failed to create EGLSurface for window %p, eglErr = %s", 196 (void*) window, egl_error_str()); 197 return surface; 198} 199 200void EglManager::destroySurface(EGLSurface surface) { 201 if (isCurrent(surface)) { 202 makeCurrent(EGL_NO_SURFACE); 203 } 204 if (!eglDestroySurface(mEglDisplay, surface)) { 205 ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); 206 } 207} 208 209void EglManager::destroy() { 210 if (mEglDisplay == EGL_NO_DISPLAY) return; 211 212 usePBufferSurface(); 213 if (Caches::hasInstance()) { 214 Caches::getInstance().terminate(); 215 } 216 217 eglDestroyContext(mEglDisplay, mEglContext); 218 eglDestroySurface(mEglDisplay, mPBufferSurface); 219 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 220 eglTerminate(mEglDisplay); 221 eglReleaseThread(); 222 223 mEglDisplay = EGL_NO_DISPLAY; 224 mEglContext = EGL_NO_CONTEXT; 225 mPBufferSurface = EGL_NO_SURFACE; 226 mCurrentSurface = EGL_NO_SURFACE; 227} 228 229bool EglManager::makeCurrent(EGLSurface surface) { 230 if (isCurrent(surface)) return false; 231 232 if (surface == EGL_NO_SURFACE) { 233 // If we are setting EGL_NO_SURFACE we don't care about any of the potential 234 // return errors, which would only happen if mEglDisplay had already been 235 // destroyed in which case the current context is already NO_CONTEXT 236 eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 237 } else if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { 238 LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", 239 (void*)surface, egl_error_str()); 240 } 241 mCurrentSurface = surface; 242 return true; 243} 244 245void EglManager::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) { 246 LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, 247 "Tried to beginFrame on EGL_NO_SURFACE!"); 248 makeCurrent(surface); 249 if (width) { 250 eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width); 251 } 252 if (height) { 253 eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height); 254 } 255 eglBeginFrame(mEglDisplay, surface); 256} 257 258void EglManager::swapBuffers(EGLSurface surface) { 259 eglSwapBuffers(mEglDisplay, surface); 260 EGLint err = eglGetError(); 261 LOG_ALWAYS_FATAL_IF(err != EGL_SUCCESS, 262 "Encountered EGL error %d %s during rendering", err, egl_error_str(err)); 263} 264 265bool EglManager::enableDirtyRegions(EGLSurface surface) { 266 if (!mRequestDirtyRegions) return false; 267 268 if (mCanSetDirtyRegions) { 269 if (!eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED)) { 270 ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", 271 (void*) surface, egl_error_str()); 272 return false; 273 } 274 return true; 275 } 276 // Perhaps it is already enabled? 277 EGLint value; 278 if (!eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &value)) { 279 ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", 280 (void*) surface, egl_error_str()); 281 return false; 282 } 283 return value == EGL_BUFFER_PRESERVED; 284} 285 286} /* namespace renderthread */ 287} /* namespace uirenderer */ 288} /* namespace android */ 289