blob: ee629c05b33471261cab58368f42cbe26dea74f8 [file] [log] [blame]
John Reck23b797a2014-01-03 18:08:34 -08001/*
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
John Reck23b797a2014-01-03 18:08:34 -080017#include "CanvasContext.h"
18
John Reck998a6d82014-08-28 15:35:53 -070019#include <algorithm>
John Reck4f02bf42014-01-03 18:09:17 -080020#include <private/hwui/DrawGlInfo.h>
John Reck23b797a2014-01-03 18:08:34 -080021#include <strings.h>
22
John Reck3b202512014-06-23 13:13:08 -070023#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080024#include "RenderThread.h"
John Reck119907c2014-08-14 09:02:01 -070025#include "../AnimationContext.h"
John Reck23b797a2014-01-03 18:08:34 -080026#include "../Caches.h"
John Reck19b6bcf2014-02-14 20:03:38 -080027#include "../DeferredLayerUpdater.h"
John Reck3b202512014-06-23 13:13:08 -070028#include "../RenderState.h"
John Reck19b6bcf2014-02-14 20:03:38 -080029#include "../LayerRenderer.h"
John Reck4f02bf42014-01-03 18:09:17 -080030#include "../OpenGLRenderer.h"
John Reck23b797a2014-01-03 18:08:34 -080031#include "../Stencil.h"
32
John Reckf47a5942014-06-30 16:20:04 -070033#define TRIM_MEMORY_COMPLETE 80
34#define TRIM_MEMORY_UI_HIDDEN 20
35
John Reck23b797a2014-01-03 18:08:34 -080036namespace android {
37namespace uirenderer {
38namespace renderthread {
39
John Reck119907c2014-08-14 09:02:01 -070040CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
41 RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck3b202512014-06-23 13:13:08 -070042 : mRenderThread(thread)
43 , mEglManager(thread.eglManager())
John Reck4f02bf42014-01-03 18:09:17 -080044 , mEglSurface(EGL_NO_SURFACE)
John Reck1125d1f2014-10-23 11:02:19 -070045 , mBufferPreserved(false)
46 , mSwapBehavior(kSwap_default)
John Reck4f02bf42014-01-03 18:09:17 -080047 , mOpaque(!translucent)
John Reck3b202512014-06-23 13:13:08 -070048 , mCanvas(NULL)
John Recke45b1fd2014-04-15 09:50:16 -070049 , mHaveNewSurface(false)
50 , mRootRenderNode(rootRenderNode) {
John Reck119907c2014-08-14 09:02:01 -070051 mAnimationContext = contextFactory->createAnimationContext(mRenderThread.timeLord());
John Reck443a7142014-09-04 17:40:05 -070052 mRenderThread.renderState().registerCanvasContext(this);
John Reck23b797a2014-01-03 18:08:34 -080053}
54
55CanvasContext::~CanvasContext() {
John Reck17035b02014-09-03 07:39:53 -070056 destroy();
John Reck119907c2014-08-14 09:02:01 -070057 delete mAnimationContext;
John Reck443a7142014-09-04 17:40:05 -070058 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -080059}
60
John Reck17035b02014-09-03 07:39:53 -070061void CanvasContext::destroy() {
62 stopDrawing();
63 freePrefetechedLayers();
64 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070065 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080066 if (mCanvas) {
67 delete mCanvas;
68 mCanvas = 0;
69 }
John Reck23b797a2014-01-03 18:08:34 -080070 setSurface(NULL);
71}
72
John Recka5dda642014-05-22 15:43:54 -070073void CanvasContext::setSurface(ANativeWindow* window) {
John Reckfbc8df02014-11-14 16:18:41 -080074 ATRACE_CALL();
75
John Recka5dda642014-05-22 15:43:54 -070076 mNativeWindow = window;
77
John Reck23b797a2014-01-03 18:08:34 -080078 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070079 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080080 mEglSurface = EGL_NO_SURFACE;
81 }
82
83 if (window) {
John Reck3b202512014-06-23 13:13:08 -070084 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080085 }
86
87 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -070088 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
89 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -080090 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070091 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070092 } else {
93 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080094 }
John Reck23b797a2014-01-03 18:08:34 -080095}
96
John Reck4f02bf42014-01-03 18:09:17 -080097void CanvasContext::swapBuffers() {
John Reck2cdbc7d2014-09-17 16:06:36 -070098 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
99 setSurface(NULL);
100 }
John Reck4f02bf42014-01-03 18:09:17 -0800101 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -0800102}
103
John Reckf7d9c1d2014-04-09 10:01:03 -0700104void CanvasContext::requireSurface() {
105 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
106 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700107 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800108}
109
John Reck1125d1f2014-10-23 11:02:19 -0700110void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
111 mSwapBehavior = swapBehavior;
112}
113
John Recka5dda642014-05-22 15:43:54 -0700114bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800115 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700116 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700117 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800118 mCanvas->initProperties();
119 return true;
120}
121
John Recka5dda642014-05-22 15:43:54 -0700122void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800123 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700124}
125
Andreas Gampe64bb4132014-11-22 00:35:09 +0000126void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck16617152014-09-02 15:44:14 -0700127 stopDrawing();
John Reck4f02bf42014-01-03 18:09:17 -0800128}
129
Chris Craik284b2432014-09-18 16:05:35 -0700130// TODO: don't pass viewport size, it's automatic via EGL
Andreas Gampe64bb4132014-11-22 00:35:09 +0000131void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
132 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800133 if (!mCanvas) return;
Chris Craik058fc642014-07-23 18:19:28 -0700134 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800135}
136
John Reck63a06672014-05-07 13:45:54 -0700137void CanvasContext::setOpaque(bool opaque) {
138 mOpaque = opaque;
139}
140
John Reck860d1552014-04-11 19:15:05 -0700141void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700142 // TODO: Figure out why this workaround is needed, see b/13913604
143 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700144 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700145}
146
John Reck68bfe0a2014-06-24 15:34:58 -0700147void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
148 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700149 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
150 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
151 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800152 }
153}
154
John Recke45b1fd2014-04-15 09:50:16 -0700155void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700156 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700157
John Recke4267ea2014-06-03 15:53:15 -0700158 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700159 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700160 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
161 info.canvasContext = this;
162 }
John Reckec845a22014-09-05 15:23:38 -0700163 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700164 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700165 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700166
John Reck998a6d82014-08-28 15:35:53 -0700167 if (info.canvasContext) {
168 freePrefetechedLayers();
169 }
170
John Reckaa95a882014-11-07 11:02:07 -0800171 if (CC_UNLIKELY(!mNativeWindow.get())) {
172 info.out.canDrawThisFrame = false;
173 return;
174 }
175
John Recka5dda642014-05-22 15:43:54 -0700176 int runningBehind = 0;
177 // TODO: This query is moderately expensive, investigate adding some sort
178 // of fast-path based off when we last called eglSwapBuffers() as well as
179 // last vsync time. Or something.
180 mNativeWindow->query(mNativeWindow.get(),
181 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
182 info.out.canDrawThisFrame = !runningBehind;
183
184 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700185 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700186 // If animationsNeedsRedraw is set don't bother posting for an RT anim
187 // as we will just end up fighting the UI thread.
188 mRenderThread.postFrameCallback(this);
189 }
John Recke45b1fd2014-04-15 09:50:16 -0700190 }
191}
192
John Reckf47a5942014-06-30 16:20:04 -0700193void CanvasContext::stopDrawing() {
194 mRenderThread.removeFrameCallback(this);
195}
196
John Recka5dda642014-05-22 15:43:54 -0700197void CanvasContext::notifyFramePending() {
198 ATRACE_CALL();
199 mRenderThread.pushBackFrameCallback(this);
200}
201
John Recke4267ea2014-06-03 15:53:15 -0700202void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800203 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700204 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800205
John Reckfe5e7b72014-05-23 17:42:28 -0700206 profiler().markPlaybackStart();
207
John Recke4267ea2014-06-03 15:53:15 -0700208 SkRect dirty;
209 mDamageAccumulator.finish(&dirty);
210
John Reck4f02bf42014-01-03 18:09:17 -0800211 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700212 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800213 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
214 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700215 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700216 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700217 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700218 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700219 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700220 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
221 SK_RECT_ARGS(dirty), width, height);
222 dirty.setEmpty();
223 }
John Recke4267ea2014-06-03 15:53:15 -0700224 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800225 }
226
John Recke4267ea2014-06-03 15:53:15 -0700227 if (!dirty.isEmpty()) {
Tom Hudson107843d2014-09-08 11:26:26 -0400228 mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
John Recke4267ea2014-06-03 15:53:15 -0700229 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800230 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400231 mCanvas->prepare(mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800232 }
233
234 Rect outBounds;
Tom Hudson107843d2014-09-08 11:26:26 -0400235 mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800236
John Reckfe5e7b72014-05-23 17:42:28 -0700237 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800238
Tom Hudson107843d2014-09-08 11:26:26 -0400239 bool drew = mCanvas->finish();
John Reck4f02bf42014-01-03 18:09:17 -0800240
John Reckfe5e7b72014-05-23 17:42:28 -0700241 profiler().markPlaybackEnd();
242
Tom Hudson107843d2014-09-08 11:26:26 -0400243 if (drew) {
John Reck4f02bf42014-01-03 18:09:17 -0800244 swapBuffers();
John Reck0e89e2b2014-10-31 14:49:06 -0700245 } else {
246 mEglManager.cancelFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800247 }
John Reckfe5e7b72014-05-23 17:42:28 -0700248
249 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800250}
251
John Recke45b1fd2014-04-15 09:50:16 -0700252// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700253void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700254 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
255 return;
256 }
257
John Recke45b1fd2014-04-15 09:50:16 -0700258 ATRACE_CALL();
259
John Reckfe5e7b72014-05-23 17:42:28 -0700260 profiler().startFrame();
261
John Reck3b202512014-06-23 13:13:08 -0700262 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700263 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700264 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700265 draw();
John Recka5dda642014-05-22 15:43:54 -0700266 }
John Recke45b1fd2014-04-15 09:50:16 -0700267}
268
John Reck3b202512014-06-23 13:13:08 -0700269void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700270 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700271 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700272 if (thread.eglManager().hasEglContext()) {
273 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700274 mode = DrawGlInfo::kModeProcess;
275 }
John Reck6f07a0d2014-04-16 21:31:25 -0700276
John Reck3b202512014-06-23 13:13:08 -0700277 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800278}
279
John Reck998a6d82014-08-28 15:35:53 -0700280void CanvasContext::markLayerInUse(RenderNode* node) {
281 if (mPrefetechedLayers.erase(node)) {
282 node->decStrong(0);
283 }
284}
285
286static void destroyPrefetechedNode(RenderNode* node) {
287 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
288 node->destroyHardwareResources();
289 node->decStrong(0);
290}
291
292void CanvasContext::freePrefetechedLayers() {
293 if (mPrefetechedLayers.size()) {
294 requireGlContext();
295 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
296 mPrefetechedLayers.clear();
297 }
298}
299
John Reck3e824952014-08-20 10:08:39 -0700300void CanvasContext::buildLayer(RenderNode* node) {
301 ATRACE_CALL();
302 if (!mEglManager.hasEglContext() || !mCanvas) {
303 return;
304 }
305 requireGlContext();
306 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
307 stopDrawing();
308
309 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700310 info.damageAccumulator = &mDamageAccumulator;
311 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700312 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700313 node->prepareTree(info);
314 SkRect ignore;
315 mDamageAccumulator.finish(&ignore);
316 // Tickle the GENERIC property on node to mark it as dirty for damaging
317 // purposes when the frame is actually drawn
318 node->setPropertyFieldsDirty(RenderNode::GENERIC);
319
John Reck443a7142014-09-04 17:40:05 -0700320 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700321 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700322
323 node->incStrong(0);
324 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700325}
326
John Reck19b6bcf2014-02-14 20:03:38 -0800327bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
328 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700329 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700330 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800331}
332
John Reckf47a5942014-06-30 16:20:04 -0700333void CanvasContext::destroyHardwareResources() {
334 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700335 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700336 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700337 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700338 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700339 Caches::getInstance().flush(Caches::kFlushMode_Layers);
340 }
341}
342
343void CanvasContext::trimMemory(RenderThread& thread, int level) {
344 // No context means nothing to free
345 if (!thread.eglManager().hasEglContext()) return;
346
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200347 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700348 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700349 if (level >= TRIM_MEMORY_COMPLETE) {
350 Caches::getInstance().flush(Caches::kFlushMode_Full);
351 thread.eglManager().destroy();
352 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
353 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700354 }
355}
356
John Reckfc53ef272014-02-11 10:40:25 -0800357void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800358 requireGlContext();
359 task->run();
360}
361
John Reck1949e792014-04-08 15:18:56 -0700362Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700363 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700364 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700365}
366
John Reck19b6bcf2014-02-14 20:03:38 -0800367void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700368 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800369}
370
John Reck3b202512014-06-23 13:13:08 -0700371void CanvasContext::setTextureAtlas(RenderThread& thread,
372 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
373 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700374}
375
John Reck23b797a2014-01-03 18:08:34 -0800376} /* namespace renderthread */
377} /* namespace uirenderer */
378} /* namespace android */