John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 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 "CanvasContext" |
| 18 | |
| 19 | #include "CanvasContext.h" |
| 20 | |
| 21 | #include <cutils/properties.h> |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 22 | #include <private/hwui/DrawGlInfo.h> |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 23 | #include <strings.h> |
| 24 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 25 | #include "RenderThread.h" |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 26 | #include "../Caches.h" |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 27 | #include "../DeferredLayerUpdater.h" |
| 28 | #include "../LayerRenderer.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 29 | #include "../OpenGLRenderer.h" |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 30 | #include "../Stencil.h" |
| 31 | |
| 32 | #define PROPERTY_RENDER_DIRTY_REGIONS "debug.hwui.render_dirty_regions" |
| 33 | #define GLES_VERSION 2 |
John Reck | cdfeef6 | 2014-05-14 16:35:46 -0700 | [diff] [blame] | 34 | #define USE_TEXTURE_ATLAS false |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 35 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 36 | // Android-specific addition that is used to show when frames began in systrace |
| 37 | EGLAPI void EGLAPIENTRY eglBeginFrame(EGLDisplay dpy, EGLSurface surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 38 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace uirenderer { |
| 41 | namespace renderthread { |
| 42 | |
| 43 | #define ERROR_CASE(x) case x: return #x; |
| 44 | static const char* egl_error_str(EGLint error) { |
| 45 | switch (error) { |
| 46 | ERROR_CASE(EGL_SUCCESS) |
| 47 | ERROR_CASE(EGL_NOT_INITIALIZED) |
| 48 | ERROR_CASE(EGL_BAD_ACCESS) |
| 49 | ERROR_CASE(EGL_BAD_ALLOC) |
| 50 | ERROR_CASE(EGL_BAD_ATTRIBUTE) |
| 51 | ERROR_CASE(EGL_BAD_CONFIG) |
| 52 | ERROR_CASE(EGL_BAD_CONTEXT) |
| 53 | ERROR_CASE(EGL_BAD_CURRENT_SURFACE) |
| 54 | ERROR_CASE(EGL_BAD_DISPLAY) |
| 55 | ERROR_CASE(EGL_BAD_MATCH) |
| 56 | ERROR_CASE(EGL_BAD_NATIVE_PIXMAP) |
| 57 | ERROR_CASE(EGL_BAD_NATIVE_WINDOW) |
| 58 | ERROR_CASE(EGL_BAD_PARAMETER) |
| 59 | ERROR_CASE(EGL_BAD_SURFACE) |
| 60 | ERROR_CASE(EGL_CONTEXT_LOST) |
| 61 | default: |
| 62 | return "Unknown error"; |
| 63 | } |
| 64 | } |
| 65 | static const char* egl_error_str() { |
| 66 | return egl_error_str(eglGetError()); |
| 67 | } |
| 68 | |
| 69 | static bool load_dirty_regions_property() { |
| 70 | char buf[PROPERTY_VALUE_MAX]; |
| 71 | int len = property_get(PROPERTY_RENDER_DIRTY_REGIONS, buf, "true"); |
| 72 | return !strncasecmp("true", buf, len); |
| 73 | } |
| 74 | |
| 75 | // This class contains the shared global EGL objects, such as EGLDisplay |
| 76 | // and EGLConfig, which are re-used by CanvasContext |
| 77 | class GlobalContext { |
| 78 | public: |
| 79 | static GlobalContext* get(); |
| 80 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 81 | // Returns true on success, false on failure |
| 82 | void initialize(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 83 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 84 | bool hasContext(); |
| 85 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 86 | void usePBufferSurface(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 87 | EGLSurface createSurface(EGLNativeWindowType window); |
| 88 | void destroySurface(EGLSurface surface); |
| 89 | |
| 90 | void destroy(); |
| 91 | |
| 92 | bool isCurrent(EGLSurface surface) { return mCurrentSurface == surface; } |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 93 | // Returns true if the current surface changed, false if it was already current |
| 94 | bool makeCurrent(EGLSurface surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 95 | void beginFrame(EGLSurface surface, EGLint* width, EGLint* height); |
| 96 | void swapBuffers(EGLSurface surface); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 97 | |
| 98 | bool enableDirtyRegions(EGLSurface surface); |
| 99 | |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 100 | void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize); |
| 101 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 102 | private: |
| 103 | GlobalContext(); |
| 104 | // GlobalContext is never destroyed, method is purposely not implemented |
| 105 | ~GlobalContext(); |
| 106 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 107 | void loadConfig(); |
| 108 | void createContext(); |
| 109 | void initAtlas(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 110 | |
| 111 | static GlobalContext* sContext; |
| 112 | |
| 113 | EGLDisplay mEglDisplay; |
| 114 | EGLConfig mEglConfig; |
| 115 | EGLContext mEglContext; |
| 116 | EGLSurface mPBufferSurface; |
| 117 | |
| 118 | const bool mRequestDirtyRegions; |
| 119 | bool mCanSetDirtyRegions; |
| 120 | |
| 121 | EGLSurface mCurrentSurface; |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 122 | |
| 123 | sp<GraphicBuffer> mAtlasBuffer; |
| 124 | int64_t* mAtlasMap; |
| 125 | size_t mAtlasMapSize; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | GlobalContext* GlobalContext::sContext = 0; |
| 129 | |
| 130 | GlobalContext* GlobalContext::get() { |
| 131 | if (!sContext) { |
| 132 | sContext = new GlobalContext(); |
| 133 | } |
| 134 | return sContext; |
| 135 | } |
| 136 | |
| 137 | GlobalContext::GlobalContext() |
| 138 | : mEglDisplay(EGL_NO_DISPLAY) |
| 139 | , mEglConfig(0) |
| 140 | , mEglContext(EGL_NO_CONTEXT) |
| 141 | , mPBufferSurface(EGL_NO_SURFACE) |
| 142 | , mRequestDirtyRegions(load_dirty_regions_property()) |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 143 | , mCurrentSurface(EGL_NO_SURFACE) |
| 144 | , mAtlasMap(NULL) |
| 145 | , mAtlasMapSize(0) { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 146 | mCanSetDirtyRegions = mRequestDirtyRegions; |
| 147 | ALOGD("Render dirty regions requested: %s", mRequestDirtyRegions ? "true" : "false"); |
| 148 | } |
| 149 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 150 | void GlobalContext::initialize() { |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 151 | if (hasContext()) return; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 152 | |
| 153 | mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 154 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 155 | "Failed to get EGL_DEFAULT_DISPLAY! err=%s", egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 156 | |
| 157 | EGLint major, minor; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 158 | LOG_ALWAYS_FATAL_IF(eglInitialize(mEglDisplay, &major, &minor) == EGL_FALSE, |
| 159 | "Failed to initialize display %p! err=%s", mEglDisplay, egl_error_str()); |
| 160 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 161 | ALOGI("Initialized EGL, version %d.%d", (int)major, (int)minor); |
| 162 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 163 | loadConfig(); |
| 164 | createContext(); |
| 165 | usePBufferSurface(); |
| 166 | Caches::getInstance().init(); |
| 167 | initAtlas(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 168 | } |
| 169 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 170 | bool GlobalContext::hasContext() { |
| 171 | return mEglDisplay != EGL_NO_DISPLAY; |
| 172 | } |
| 173 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 174 | void GlobalContext::loadConfig() { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 175 | EGLint swapBehavior = mCanSetDirtyRegions ? EGL_SWAP_BEHAVIOR_PRESERVED_BIT : 0; |
| 176 | EGLint attribs[] = { |
| 177 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 178 | EGL_RED_SIZE, 8, |
| 179 | EGL_GREEN_SIZE, 8, |
| 180 | EGL_BLUE_SIZE, 8, |
| 181 | EGL_ALPHA_SIZE, 8, |
| 182 | EGL_DEPTH_SIZE, 0, |
| 183 | EGL_CONFIG_CAVEAT, EGL_NONE, |
| 184 | EGL_STENCIL_SIZE, Stencil::getStencilSize(), |
| 185 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT | swapBehavior, |
| 186 | EGL_NONE |
| 187 | }; |
| 188 | |
| 189 | EGLint num_configs = 1; |
| 190 | if (!eglChooseConfig(mEglDisplay, attribs, &mEglConfig, num_configs, &num_configs) |
| 191 | || num_configs != 1) { |
| 192 | // Failed to get a valid config |
| 193 | if (mCanSetDirtyRegions) { |
| 194 | ALOGW("Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without..."); |
| 195 | // Try again without dirty regions enabled |
| 196 | mCanSetDirtyRegions = false; |
| 197 | loadConfig(); |
| 198 | } else { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 199 | LOG_ALWAYS_FATAL("Failed to choose config, error = %s", egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 202 | } |
| 203 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 204 | void GlobalContext::createContext() { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 205 | EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, GLES_VERSION, EGL_NONE }; |
| 206 | mEglContext = eglCreateContext(mEglDisplay, mEglConfig, EGL_NO_CONTEXT, attribs); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 207 | LOG_ALWAYS_FATAL_IF(mEglContext == EGL_NO_CONTEXT, |
| 208 | "Failed to create context, error = %s", egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 209 | } |
| 210 | |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 211 | void GlobalContext::setTextureAtlas(const sp<GraphicBuffer>& buffer, |
| 212 | int64_t* map, size_t mapSize) { |
| 213 | |
| 214 | // Already initialized |
| 215 | if (mAtlasBuffer.get()) { |
| 216 | ALOGW("Multiple calls to setTextureAtlas!"); |
| 217 | delete map; |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | mAtlasBuffer = buffer; |
| 222 | mAtlasMap = map; |
| 223 | mAtlasMapSize = mapSize; |
| 224 | |
| 225 | if (hasContext()) { |
| 226 | usePBufferSurface(); |
| 227 | initAtlas(); |
| 228 | } |
| 229 | } |
| 230 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 231 | void GlobalContext::initAtlas() { |
John Reck | cdfeef6 | 2014-05-14 16:35:46 -0700 | [diff] [blame] | 232 | if (USE_TEXTURE_ATLAS) { |
| 233 | Caches::getInstance().assetAtlas.init(mAtlasBuffer, mAtlasMap, mAtlasMapSize); |
| 234 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void GlobalContext::usePBufferSurface() { |
| 238 | LOG_ALWAYS_FATAL_IF(mEglDisplay == EGL_NO_DISPLAY, |
| 239 | "usePBufferSurface() called on uninitialized GlobalContext!"); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 240 | |
| 241 | if (mPBufferSurface == EGL_NO_SURFACE) { |
| 242 | EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE }; |
| 243 | mPBufferSurface = eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 244 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 245 | makeCurrent(mPBufferSurface); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | EGLSurface GlobalContext::createSurface(EGLNativeWindowType window) { |
| 249 | initialize(); |
| 250 | return eglCreateWindowSurface(mEglDisplay, mEglConfig, window, NULL); |
| 251 | } |
| 252 | |
| 253 | void GlobalContext::destroySurface(EGLSurface surface) { |
| 254 | if (isCurrent(surface)) { |
| 255 | makeCurrent(EGL_NO_SURFACE); |
| 256 | } |
| 257 | if (!eglDestroySurface(mEglDisplay, surface)) { |
| 258 | ALOGW("Failed to destroy surface %p, error=%s", (void*)surface, egl_error_str()); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void GlobalContext::destroy() { |
| 263 | if (mEglDisplay == EGL_NO_DISPLAY) return; |
| 264 | |
| 265 | usePBufferSurface(); |
| 266 | if (Caches::hasInstance()) { |
| 267 | Caches::getInstance().terminate(); |
| 268 | } |
| 269 | |
| 270 | eglDestroyContext(mEglDisplay, mEglContext); |
| 271 | eglDestroySurface(mEglDisplay, mPBufferSurface); |
| 272 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 273 | eglTerminate(mEglDisplay); |
| 274 | eglReleaseThread(); |
| 275 | |
| 276 | mEglDisplay = EGL_NO_DISPLAY; |
| 277 | mEglContext = EGL_NO_CONTEXT; |
| 278 | mPBufferSurface = EGL_NO_SURFACE; |
| 279 | mCurrentSurface = EGL_NO_SURFACE; |
| 280 | } |
| 281 | |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 282 | bool GlobalContext::makeCurrent(EGLSurface surface) { |
| 283 | if (isCurrent(surface)) return false; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 284 | |
| 285 | if (surface == EGL_NO_SURFACE) { |
| 286 | // If we are setting EGL_NO_SURFACE we don't care about any of the potential |
| 287 | // return errors, which would only happen if mEglDisplay had already been |
| 288 | // destroyed in which case the current context is already NO_CONTEXT |
| 289 | eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 290 | } else if (!eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 291 | LOG_ALWAYS_FATAL("Failed to make current on surface %p, error=%s", |
| 292 | (void*)surface, egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 293 | } |
| 294 | mCurrentSurface = surface; |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 295 | return true; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 296 | } |
| 297 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 298 | void GlobalContext::beginFrame(EGLSurface surface, EGLint* width, EGLint* height) { |
| 299 | LOG_ALWAYS_FATAL_IF(surface == EGL_NO_SURFACE, |
| 300 | "Tried to beginFrame on EGL_NO_SURFACE!"); |
| 301 | makeCurrent(surface); |
| 302 | if (width) { |
| 303 | eglQuerySurface(mEglDisplay, surface, EGL_WIDTH, width); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 304 | } |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 305 | if (height) { |
| 306 | eglQuerySurface(mEglDisplay, surface, EGL_HEIGHT, height); |
| 307 | } |
| 308 | eglBeginFrame(mEglDisplay, surface); |
| 309 | } |
| 310 | |
| 311 | void GlobalContext::swapBuffers(EGLSurface surface) { |
| 312 | eglSwapBuffers(mEglDisplay, surface); |
| 313 | EGLint err = eglGetError(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 314 | LOG_ALWAYS_FATAL_IF(err != EGL_SUCCESS, |
| 315 | "Encountered EGL error %d %s during rendering", err, egl_error_str(err)); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | bool GlobalContext::enableDirtyRegions(EGLSurface surface) { |
| 319 | if (!mRequestDirtyRegions) return false; |
| 320 | |
| 321 | if (mCanSetDirtyRegions) { |
| 322 | if (!eglSurfaceAttrib(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED)) { |
| 323 | ALOGW("Failed to set EGL_SWAP_BEHAVIOR on surface %p, error=%s", |
| 324 | (void*) surface, egl_error_str()); |
| 325 | return false; |
| 326 | } |
| 327 | return true; |
| 328 | } |
| 329 | // Perhaps it is already enabled? |
| 330 | EGLint value; |
| 331 | if (!eglQuerySurface(mEglDisplay, surface, EGL_SWAP_BEHAVIOR, &value)) { |
| 332 | ALOGW("Failed to query EGL_SWAP_BEHAVIOR on surface %p, error=%p", |
| 333 | (void*) surface, egl_error_str()); |
| 334 | return false; |
| 335 | } |
| 336 | return value == EGL_BUFFER_PRESERVED; |
| 337 | } |
| 338 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 339 | CanvasContext::CanvasContext(bool translucent, RenderNode* rootRenderNode) |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 340 | : mRenderThread(RenderThread::getInstance()) |
| 341 | , mEglSurface(EGL_NO_SURFACE) |
| 342 | , mDirtyRegionsEnabled(false) |
| 343 | , mOpaque(!translucent) |
| 344 | , mCanvas(0) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 345 | , mHaveNewSurface(false) |
| 346 | , mRootRenderNode(rootRenderNode) { |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 347 | mGlobalContext = GlobalContext::get(); |
| 348 | } |
| 349 | |
| 350 | CanvasContext::~CanvasContext() { |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 351 | destroyCanvasAndSurface(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 352 | mRenderThread.removeFrameCallback(this); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 353 | } |
| 354 | |
John Reck | fae904d | 2014-04-14 11:01:57 -0700 | [diff] [blame] | 355 | void CanvasContext::destroyCanvasAndSurface() { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 356 | if (mCanvas) { |
| 357 | delete mCanvas; |
| 358 | mCanvas = 0; |
| 359 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 360 | setSurface(NULL); |
| 361 | } |
| 362 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 363 | void CanvasContext::setSurface(ANativeWindow* window) { |
| 364 | mNativeWindow = window; |
| 365 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 366 | if (mEglSurface != EGL_NO_SURFACE) { |
| 367 | mGlobalContext->destroySurface(mEglSurface); |
| 368 | mEglSurface = EGL_NO_SURFACE; |
| 369 | } |
| 370 | |
| 371 | if (window) { |
| 372 | mEglSurface = mGlobalContext->createSurface(window); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 373 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 374 | "Failed to create EGLSurface for window %p, eglErr = %s", |
| 375 | (void*) window, egl_error_str()); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | if (mEglSurface != EGL_NO_SURFACE) { |
| 379 | mDirtyRegionsEnabled = mGlobalContext->enableDirtyRegions(mEglSurface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 380 | mHaveNewSurface = true; |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 381 | makeCurrent(); |
John Reck | 368cdd8 | 2014-05-07 13:11:00 -0700 | [diff] [blame] | 382 | } else { |
| 383 | mRenderThread.removeFrameCallback(this); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 384 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 385 | } |
| 386 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 387 | void CanvasContext::swapBuffers() { |
| 388 | mGlobalContext->swapBuffers(mEglSurface); |
| 389 | mHaveNewSurface = false; |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 390 | } |
| 391 | |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 392 | void CanvasContext::requireSurface() { |
| 393 | LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, |
| 394 | "requireSurface() called but no surface set!"); |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 395 | makeCurrent(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 396 | } |
| 397 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 398 | bool CanvasContext::initialize(ANativeWindow* window) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 399 | if (mCanvas) return false; |
| 400 | setSurface(window); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 401 | mCanvas = new OpenGLRenderer(); |
| 402 | mCanvas->initProperties(); |
| 403 | return true; |
| 404 | } |
| 405 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 406 | void CanvasContext::updateSurface(ANativeWindow* window) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 407 | setSurface(window); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 408 | } |
| 409 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 410 | void CanvasContext::pauseSurface(ANativeWindow* window) { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 411 | // TODO: For now we just need a fence, in the future suspend any animations |
| 412 | // and such to prevent from trying to render into this surface |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 415 | void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 416 | if (!mCanvas) return; |
| 417 | mCanvas->setViewport(width, height); |
Chris Craik | 797b95b | 2014-05-20 18:10:25 -0700 | [diff] [blame] | 418 | mCanvas->initializeLight(lightCenter, lightRadius); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 419 | } |
| 420 | |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 421 | void CanvasContext::setOpaque(bool opaque) { |
| 422 | mOpaque = opaque; |
| 423 | } |
| 424 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 425 | void CanvasContext::makeCurrent() { |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 426 | // TODO: Figure out why this workaround is needed, see b/13913604 |
| 427 | // In the meantime this matches the behavior of GLRenderer, so it is not a regression |
| 428 | mHaveNewSurface |= mGlobalContext->makeCurrent(mEglSurface); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 429 | } |
| 430 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 431 | void CanvasContext::prepareDraw(const Vector<DeferredLayerUpdater*>* layerUpdaters, |
| 432 | TreeInfo& info) { |
| 433 | LOG_ALWAYS_FATAL_IF(!mCanvas, "Cannot prepareDraw without a canvas!"); |
| 434 | makeCurrent(); |
| 435 | |
| 436 | processLayerUpdates(layerUpdaters, info); |
| 437 | if (info.out.hasAnimations) { |
| 438 | // TODO: Uh... crap? |
| 439 | } |
| 440 | prepareTree(info); |
| 441 | } |
| 442 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 443 | void CanvasContext::processLayerUpdates(const Vector<DeferredLayerUpdater*>* layerUpdaters, |
| 444 | TreeInfo& info) { |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 445 | for (size_t i = 0; i < layerUpdaters->size(); i++) { |
| 446 | DeferredLayerUpdater* update = layerUpdaters->itemAt(i); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 447 | bool success = update->apply(info); |
| 448 | LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!"); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 449 | if (update->backingLayer()->deferredUpdateScheduled) { |
| 450 | mCanvas->pushLayerUpdate(update->backingLayer()); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 455 | void CanvasContext::prepareTree(TreeInfo& info) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 456 | mRenderThread.removeFrameCallback(this); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 457 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 458 | info.frameTimeMs = mRenderThread.timeLord().frameTimeMs(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 459 | mRootRenderNode->prepareTree(info); |
| 460 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 461 | int runningBehind = 0; |
| 462 | // TODO: This query is moderately expensive, investigate adding some sort |
| 463 | // of fast-path based off when we last called eglSwapBuffers() as well as |
| 464 | // last vsync time. Or something. |
| 465 | mNativeWindow->query(mNativeWindow.get(), |
| 466 | NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind); |
| 467 | info.out.canDrawThisFrame = !runningBehind; |
| 468 | |
| 469 | if (info.out.hasAnimations || !info.out.canDrawThisFrame) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 470 | if (info.out.hasFunctors) { |
| 471 | info.out.requiresUiRedraw = true; |
| 472 | } else if (!info.out.requiresUiRedraw) { |
| 473 | // If animationsNeedsRedraw is set don't bother posting for an RT anim |
| 474 | // as we will just end up fighting the UI thread. |
| 475 | mRenderThread.postFrameCallback(this); |
| 476 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 480 | void CanvasContext::notifyFramePending() { |
| 481 | ATRACE_CALL(); |
| 482 | mRenderThread.pushBackFrameCallback(this); |
| 483 | } |
| 484 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 485 | void CanvasContext::draw(Rect* dirty) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 486 | LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE, |
| 487 | "drawDisplayList called on a context with no canvas or surface!"); |
| 488 | |
| 489 | EGLint width, height; |
| 490 | mGlobalContext->beginFrame(mEglSurface, &width, &height); |
| 491 | if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) { |
| 492 | mCanvas->setViewport(width, height); |
| 493 | dirty = NULL; |
| 494 | } else if (!mDirtyRegionsEnabled || mHaveNewSurface) { |
| 495 | dirty = NULL; |
| 496 | } |
| 497 | |
| 498 | status_t status; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 499 | if (dirty && !dirty->isEmpty()) { |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 500 | status = mCanvas->prepareDirty(dirty->left, dirty->top, |
| 501 | dirty->right, dirty->bottom, mOpaque); |
| 502 | } else { |
| 503 | status = mCanvas->prepare(mOpaque); |
| 504 | } |
| 505 | |
| 506 | Rect outBounds; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 507 | status |= mCanvas->drawDisplayList(mRootRenderNode.get(), outBounds); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 508 | |
| 509 | // TODO: Draw debug info |
| 510 | // TODO: Performance tracking |
| 511 | |
| 512 | mCanvas->finish(); |
| 513 | |
| 514 | if (status & DrawGlInfo::kStatusDrew) { |
| 515 | swapBuffers(); |
| 516 | } |
| 517 | } |
| 518 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 519 | // Called by choreographer to do an RT-driven animation |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 520 | void CanvasContext::doFrame() { |
John Reck | 368cdd8 | 2014-05-07 13:11:00 -0700 | [diff] [blame] | 521 | if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) { |
| 522 | return; |
| 523 | } |
| 524 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 525 | ATRACE_CALL(); |
| 526 | |
| 527 | TreeInfo info; |
| 528 | info.evaluateAnimations = true; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 529 | info.performStagingPush = false; |
| 530 | info.prepareTextures = false; |
| 531 | |
| 532 | prepareTree(info); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 533 | if (info.out.canDrawThisFrame) { |
| 534 | draw(NULL); |
| 535 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 536 | } |
| 537 | |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 538 | void CanvasContext::invokeFunctor(Functor* functor) { |
John Reck | d3d8daf | 2014-04-10 15:00:13 -0700 | [diff] [blame] | 539 | ATRACE_CALL(); |
John Reck | 0d1f634 | 2014-03-28 20:30:27 -0700 | [diff] [blame] | 540 | DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; |
| 541 | if (mGlobalContext->hasContext()) { |
| 542 | requireGlContext(); |
| 543 | mode = DrawGlInfo::kModeProcess; |
| 544 | } |
John Reck | 832b1514 | 2014-05-07 14:39:44 -0700 | [diff] [blame] | 545 | (*functor)(mode, NULL); |
John Reck | 6f07a0d | 2014-04-16 21:31:25 -0700 | [diff] [blame] | 546 | |
| 547 | if (mCanvas) { |
| 548 | mCanvas->resume(); |
| 549 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 550 | } |
| 551 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 552 | bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
| 553 | requireGlContext(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 554 | TreeInfo info; |
| 555 | layer->apply(info); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 556 | return LayerRenderer::copyLayer(layer->backingLayer(), bitmap); |
| 557 | } |
| 558 | |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame^] | 559 | void CanvasContext::flushCaches(Caches::FlushMode flushMode) { |
| 560 | if (mGlobalContext->hasContext()) { |
| 561 | requireGlContext(); |
| 562 | Caches::getInstance().flush(flushMode); |
| 563 | } |
| 564 | } |
| 565 | |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 566 | void CanvasContext::runWithGlContext(RenderTask* task) { |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 567 | requireGlContext(); |
| 568 | task->run(); |
| 569 | } |
| 570 | |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 571 | Layer* CanvasContext::createRenderLayer(int width, int height) { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 572 | requireSurface(); |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 573 | return LayerRenderer::createRenderLayer(width, height); |
| 574 | } |
| 575 | |
| 576 | Layer* CanvasContext::createTextureLayer() { |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 577 | requireSurface(); |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 578 | return LayerRenderer::createTextureLayer(); |
| 579 | } |
| 580 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 581 | void CanvasContext::requireGlContext() { |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 582 | if (mEglSurface != EGL_NO_SURFACE) { |
John Reck | dbc9a86 | 2014-04-17 20:25:13 -0700 | [diff] [blame] | 583 | makeCurrent(); |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 584 | } else { |
| 585 | mGlobalContext->usePBufferSurface(); |
| 586 | } |
John Reck | fc53ef27 | 2014-02-11 10:40:25 -0800 | [diff] [blame] | 587 | } |
| 588 | |
John Reck | 66f0be6 | 2014-05-13 13:39:31 -0700 | [diff] [blame] | 589 | void CanvasContext::setTextureAtlas(const sp<GraphicBuffer>& buffer, |
| 590 | int64_t* map, size_t mapSize) { |
| 591 | GlobalContext::get()->setTextureAtlas(buffer, map, mapSize); |
| 592 | } |
| 593 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 594 | } /* namespace renderthread */ |
| 595 | } /* namespace uirenderer */ |
| 596 | } /* namespace android */ |