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