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