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