CanvasContext.cpp revision 998a6d81896df8b662cc10ddeb35087b78b38d72
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 "CanvasContext.h" 18 19#include <algorithm> 20#include <private/hwui/DrawGlInfo.h> 21#include <strings.h> 22 23#include "EglManager.h" 24#include "RenderThread.h" 25#include "../AnimationContext.h" 26#include "../Caches.h" 27#include "../DeferredLayerUpdater.h" 28#include "../RenderState.h" 29#include "../LayerRenderer.h" 30#include "../OpenGLRenderer.h" 31#include "../Stencil.h" 32 33#define TRIM_MEMORY_COMPLETE 80 34#define TRIM_MEMORY_UI_HIDDEN 20 35 36namespace android { 37namespace uirenderer { 38namespace renderthread { 39 40CanvasContext::CanvasContext(RenderThread& thread, bool translucent, 41 RenderNode* rootRenderNode, IContextFactory* contextFactory) 42 : mRenderThread(thread) 43 , mEglManager(thread.eglManager()) 44 , mEglSurface(EGL_NO_SURFACE) 45 , mDirtyRegionsEnabled(false) 46 , mOpaque(!translucent) 47 , mCanvas(NULL) 48 , mHaveNewSurface(false) 49 , mRootRenderNode(rootRenderNode) { 50 mAnimationContext = contextFactory->createAnimationContext(mRenderThread.timeLord()); 51} 52 53CanvasContext::~CanvasContext() { 54 destroyCanvasAndSurface(); 55 mRenderThread.removeFrameCallback(this); 56 delete mAnimationContext; 57 freePrefetechedLayers(); 58} 59 60void CanvasContext::destroyCanvasAndSurface() { 61 if (mCanvas) { 62 delete mCanvas; 63 mCanvas = 0; 64 } 65 setSurface(NULL); 66} 67 68void CanvasContext::setSurface(ANativeWindow* window) { 69 mNativeWindow = window; 70 71 if (mEglSurface != EGL_NO_SURFACE) { 72 mEglManager.destroySurface(mEglSurface); 73 mEglSurface = EGL_NO_SURFACE; 74 } 75 76 if (window) { 77 mEglSurface = mEglManager.createSurface(window); 78 } 79 80 if (mEglSurface != EGL_NO_SURFACE) { 81 mDirtyRegionsEnabled = mEglManager.enableDirtyRegions(mEglSurface); 82 mHaveNewSurface = true; 83 makeCurrent(); 84 } else { 85 mRenderThread.removeFrameCallback(this); 86 } 87} 88 89void CanvasContext::swapBuffers() { 90 mEglManager.swapBuffers(mEglSurface); 91 mHaveNewSurface = false; 92} 93 94void CanvasContext::requireSurface() { 95 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE, 96 "requireSurface() called but no surface set!"); 97 makeCurrent(); 98} 99 100bool CanvasContext::initialize(ANativeWindow* window) { 101 if (mCanvas) return false; 102 setSurface(window); 103 mCanvas = new OpenGLRenderer(mRenderThread.renderState()); 104 mCanvas->initProperties(); 105 return true; 106} 107 108void CanvasContext::updateSurface(ANativeWindow* window) { 109 setSurface(window); 110} 111 112void CanvasContext::pauseSurface(ANativeWindow* window) { 113 // TODO: For now we just need a fence, in the future suspend any animations 114 // and such to prevent from trying to render into this surface 115} 116 117void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius, 118 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) { 119 if (!mCanvas) return; 120 mCanvas->setViewport(width, height); 121 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha); 122} 123 124void CanvasContext::setOpaque(bool opaque) { 125 mOpaque = opaque; 126} 127 128void CanvasContext::makeCurrent() { 129 // TODO: Figure out why this workaround is needed, see b/13913604 130 // In the meantime this matches the behavior of GLRenderer, so it is not a regression 131 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface); 132} 133 134void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) { 135 bool success = layerUpdater->apply(); 136 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!"); 137 if (layerUpdater->backingLayer()->deferredUpdateScheduled) { 138 mCanvas->pushLayerUpdate(layerUpdater->backingLayer()); 139 } 140} 141 142void CanvasContext::prepareTree(TreeInfo& info) { 143 mRenderThread.removeFrameCallback(this); 144 145 info.damageAccumulator = &mDamageAccumulator; 146 info.renderer = mCanvas; 147 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) { 148 info.canvasContext = this; 149 } 150 mAnimationContext->startFrame(); 151 mRootRenderNode->prepareTree(info); 152 mAnimationContext->runRemainingAnimations(info); 153 154 if (info.canvasContext) { 155 freePrefetechedLayers(); 156 } 157 158 int runningBehind = 0; 159 // TODO: This query is moderately expensive, investigate adding some sort 160 // of fast-path based off when we last called eglSwapBuffers() as well as 161 // last vsync time. Or something. 162 mNativeWindow->query(mNativeWindow.get(), 163 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind); 164 info.out.canDrawThisFrame = !runningBehind; 165 166 if (info.out.hasAnimations || !info.out.canDrawThisFrame) { 167 if (!info.out.requiresUiRedraw) { 168 // If animationsNeedsRedraw is set don't bother posting for an RT anim 169 // as we will just end up fighting the UI thread. 170 mRenderThread.postFrameCallback(this); 171 } 172 } 173} 174 175void CanvasContext::stopDrawing() { 176 mRenderThread.removeFrameCallback(this); 177} 178 179void CanvasContext::notifyFramePending() { 180 ATRACE_CALL(); 181 mRenderThread.pushBackFrameCallback(this); 182} 183 184void CanvasContext::draw() { 185 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE, 186 "drawRenderNode called on a context with no canvas or surface!"); 187 188 profiler().markPlaybackStart(); 189 190 SkRect dirty; 191 mDamageAccumulator.finish(&dirty); 192 193 EGLint width, height; 194 mEglManager.beginFrame(mEglSurface, &width, &height); 195 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) { 196 mCanvas->setViewport(width, height); 197 dirty.setEmpty(); 198 } else if (!mDirtyRegionsEnabled || mHaveNewSurface) { 199 dirty.setEmpty(); 200 } else { 201 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) { 202 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?", 203 SK_RECT_ARGS(dirty), width, height); 204 dirty.setEmpty(); 205 } 206 profiler().unionDirty(&dirty); 207 } 208 209 status_t status; 210 if (!dirty.isEmpty()) { 211 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop, 212 dirty.fRight, dirty.fBottom, mOpaque); 213 } else { 214 status = mCanvas->prepare(mOpaque); 215 } 216 217 Rect outBounds; 218 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds); 219 220 profiler().draw(mCanvas); 221 222 mCanvas->finish(); 223 224 profiler().markPlaybackEnd(); 225 226 if (status & DrawGlInfo::kStatusDrew) { 227 swapBuffers(); 228 } 229 230 profiler().finishFrame(); 231} 232 233// Called by choreographer to do an RT-driven animation 234void CanvasContext::doFrame() { 235 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) { 236 return; 237 } 238 239 ATRACE_CALL(); 240 241 profiler().startFrame(); 242 243 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState()); 244 prepareTree(info); 245 if (info.out.canDrawThisFrame) { 246 draw(); 247 } 248} 249 250void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) { 251 ATRACE_CALL(); 252 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext; 253 if (thread.eglManager().hasEglContext()) { 254 thread.eglManager().requireGlContext(); 255 mode = DrawGlInfo::kModeProcess; 256 } 257 258 thread.renderState().invokeFunctor(functor, mode, NULL); 259} 260 261void CanvasContext::markLayerInUse(RenderNode* node) { 262 if (mPrefetechedLayers.erase(node)) { 263 node->decStrong(0); 264 } 265} 266 267static void destroyPrefetechedNode(RenderNode* node) { 268 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName()); 269 node->destroyHardwareResources(); 270 node->decStrong(0); 271} 272 273void CanvasContext::freePrefetechedLayers() { 274 if (mPrefetechedLayers.size()) { 275 requireGlContext(); 276 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode); 277 mPrefetechedLayers.clear(); 278 } 279} 280 281void CanvasContext::buildLayer(RenderNode* node) { 282 ATRACE_CALL(); 283 if (!mEglManager.hasEglContext() || !mCanvas) { 284 return; 285 } 286 requireGlContext(); 287 // buildLayer() will leave the tree in an unknown state, so we must stop drawing 288 stopDrawing(); 289 290 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState()); 291 info.damageAccumulator = &mDamageAccumulator; 292 info.renderer = mCanvas; 293 info.runAnimations = false; 294 node->prepareTree(info); 295 SkRect ignore; 296 mDamageAccumulator.finish(&ignore); 297 // Tickle the GENERIC property on node to mark it as dirty for damaging 298 // purposes when the frame is actually drawn 299 node->setPropertyFieldsDirty(RenderNode::GENERIC); 300 301 mCanvas->flushLayerUpdates(); 302 303 node->incStrong(0); 304 mPrefetechedLayers.insert(node); 305} 306 307bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { 308 requireGlContext(); 309 layer->apply(); 310 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap); 311} 312 313void CanvasContext::destroyHardwareResources() { 314 stopDrawing(); 315 if (mEglManager.hasEglContext()) { 316 requireGlContext(); 317 mRootRenderNode->destroyHardwareResources(); 318 Caches::getInstance().flush(Caches::kFlushMode_Layers); 319 } 320} 321 322void CanvasContext::trimMemory(RenderThread& thread, int level) { 323 // No context means nothing to free 324 if (!thread.eglManager().hasEglContext()) return; 325 326 thread.eglManager().requireGlContext(); 327 if (level >= TRIM_MEMORY_COMPLETE) { 328 Caches::getInstance().flush(Caches::kFlushMode_Full); 329 thread.eglManager().destroy(); 330 } else if (level >= TRIM_MEMORY_UI_HIDDEN) { 331 Caches::getInstance().flush(Caches::kFlushMode_Moderate); 332 } 333} 334 335void CanvasContext::runWithGlContext(RenderTask* task) { 336 requireGlContext(); 337 task->run(); 338} 339 340Layer* CanvasContext::createRenderLayer(int width, int height) { 341 requireSurface(); 342 return LayerRenderer::createRenderLayer(mRenderThread.renderState(), width, height); 343} 344 345Layer* CanvasContext::createTextureLayer() { 346 requireSurface(); 347 return LayerRenderer::createTextureLayer(mRenderThread.renderState()); 348} 349 350void CanvasContext::requireGlContext() { 351 mEglManager.requireGlContext(); 352} 353 354void CanvasContext::setTextureAtlas(RenderThread& thread, 355 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) { 356 thread.eglManager().setTextureAtlas(buffer, map, mapSize); 357} 358 359} /* namespace renderthread */ 360} /* namespace uirenderer */ 361} /* namespace android */ 362