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