blob: 0e541690a5ebf46315677ebf217ce0aa57856bf1 [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)
Chris Craik51d6a3d2014-12-22 17:16:56 -080050 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
John Recke45b1fd2014-04-15 09:50:16 -070051 , mRootRenderNode(rootRenderNode) {
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 Reck443a7142014-09-04 17:40:05 -070057 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -080058}
59
John Reck17035b02014-09-03 07:39:53 -070060void CanvasContext::destroy() {
61 stopDrawing();
Sangkyu Leef76d36f2014-11-21 18:05:41 +090062 setSurface(NULL);
John Reck17035b02014-09-03 07:39:53 -070063 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}
71
John Recka5dda642014-05-22 15:43:54 -070072void CanvasContext::setSurface(ANativeWindow* window) {
John Reckfbc8df02014-11-14 16:18:41 -080073 ATRACE_CALL();
74
John Recka5dda642014-05-22 15:43:54 -070075 mNativeWindow = window;
76
John Reck23b797a2014-01-03 18:08:34 -080077 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070078 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080079 mEglSurface = EGL_NO_SURFACE;
80 }
81
82 if (window) {
John Reck3b202512014-06-23 13:13:08 -070083 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080084 }
85
86 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -070087 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
88 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -080089 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070090 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070091 } else {
92 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080093 }
John Reck23b797a2014-01-03 18:08:34 -080094}
95
John Reck4f02bf42014-01-03 18:09:17 -080096void CanvasContext::swapBuffers() {
John Reck2cdbc7d2014-09-17 16:06:36 -070097 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
98 setSurface(NULL);
99 }
John Reck4f02bf42014-01-03 18:09:17 -0800100 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -0800101}
102
John Reckf7d9c1d2014-04-09 10:01:03 -0700103void CanvasContext::requireSurface() {
104 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
105 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700106 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800107}
108
John Reck1125d1f2014-10-23 11:02:19 -0700109void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
110 mSwapBehavior = swapBehavior;
111}
112
John Recka5dda642014-05-22 15:43:54 -0700113bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800114 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700115 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700116 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800117 mCanvas->initProperties();
118 return true;
119}
120
John Recka5dda642014-05-22 15:43:54 -0700121void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800122 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700123}
124
John Reck9fb42f02014-12-04 13:51:41 -0800125bool CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck01a5ea32014-12-03 13:01:07 -0800126 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800127}
128
Chris Craik284b2432014-09-18 16:05:35 -0700129// TODO: don't pass viewport size, it's automatic via EGL
Andreas Gampe64bb4132014-11-22 00:35:09 +0000130void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
131 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800132 if (!mCanvas) return;
Chris Craik058fc642014-07-23 18:19:28 -0700133 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800134}
135
John Reck63a06672014-05-07 13:45:54 -0700136void CanvasContext::setOpaque(bool opaque) {
137 mOpaque = opaque;
138}
139
John Reck860d1552014-04-11 19:15:05 -0700140void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700141 // TODO: Figure out why this workaround is needed, see b/13913604
142 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700143 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700144}
145
John Reck68bfe0a2014-06-24 15:34:58 -0700146void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
147 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700148 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
149 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
150 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800151 }
152}
153
John Recke45b1fd2014-04-15 09:50:16 -0700154void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700155 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700156
John Recke4267ea2014-06-03 15:53:15 -0700157 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700158 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700159 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
160 info.canvasContext = this;
161 }
John Reckec845a22014-09-05 15:23:38 -0700162 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700163 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700164 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700165
John Reck998a6d82014-08-28 15:35:53 -0700166 if (info.canvasContext) {
167 freePrefetechedLayers();
168 }
169
John Reckaa95a882014-11-07 11:02:07 -0800170 if (CC_UNLIKELY(!mNativeWindow.get())) {
171 info.out.canDrawThisFrame = false;
172 return;
173 }
174
John Recka5dda642014-05-22 15:43:54 -0700175 int runningBehind = 0;
176 // TODO: This query is moderately expensive, investigate adding some sort
177 // of fast-path based off when we last called eglSwapBuffers() as well as
178 // last vsync time. Or something.
179 mNativeWindow->query(mNativeWindow.get(),
180 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
181 info.out.canDrawThisFrame = !runningBehind;
182
183 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700184 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700185 // If animationsNeedsRedraw is set don't bother posting for an RT anim
186 // as we will just end up fighting the UI thread.
187 mRenderThread.postFrameCallback(this);
188 }
John Recke45b1fd2014-04-15 09:50:16 -0700189 }
190}
191
John Reckf47a5942014-06-30 16:20:04 -0700192void CanvasContext::stopDrawing() {
193 mRenderThread.removeFrameCallback(this);
194}
195
John Recka5dda642014-05-22 15:43:54 -0700196void CanvasContext::notifyFramePending() {
197 ATRACE_CALL();
198 mRenderThread.pushBackFrameCallback(this);
199}
200
John Recke4267ea2014-06-03 15:53:15 -0700201void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800202 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700203 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800204
John Reckfe5e7b72014-05-23 17:42:28 -0700205 profiler().markPlaybackStart();
206
John Recke4267ea2014-06-03 15:53:15 -0700207 SkRect dirty;
208 mDamageAccumulator.finish(&dirty);
209
John Reck4f02bf42014-01-03 18:09:17 -0800210 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700211 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800212 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
213 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700214 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700215 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700216 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700217 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700218 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700219 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
220 SK_RECT_ARGS(dirty), width, height);
221 dirty.setEmpty();
222 }
John Recke4267ea2014-06-03 15:53:15 -0700223 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800224 }
225
John Recke4267ea2014-06-03 15:53:15 -0700226 if (!dirty.isEmpty()) {
Tom Hudson107843d2014-09-08 11:26:26 -0400227 mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
John Recke4267ea2014-06-03 15:53:15 -0700228 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800229 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400230 mCanvas->prepare(mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800231 }
232
233 Rect outBounds;
Tom Hudson107843d2014-09-08 11:26:26 -0400234 mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800235
John Reckfe5e7b72014-05-23 17:42:28 -0700236 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800237
Tom Hudson107843d2014-09-08 11:26:26 -0400238 bool drew = mCanvas->finish();
John Reck4f02bf42014-01-03 18:09:17 -0800239
John Reckfe5e7b72014-05-23 17:42:28 -0700240 profiler().markPlaybackEnd();
241
Tom Hudson107843d2014-09-08 11:26:26 -0400242 if (drew) {
John Reck4f02bf42014-01-03 18:09:17 -0800243 swapBuffers();
John Reck0e89e2b2014-10-31 14:49:06 -0700244 } else {
245 mEglManager.cancelFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800246 }
John Reckfe5e7b72014-05-23 17:42:28 -0700247
248 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800249}
250
John Recke45b1fd2014-04-15 09:50:16 -0700251// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700252void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700253 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
254 return;
255 }
256
John Recke45b1fd2014-04-15 09:50:16 -0700257 ATRACE_CALL();
258
John Reckfe5e7b72014-05-23 17:42:28 -0700259 profiler().startFrame();
260
John Reck3b202512014-06-23 13:13:08 -0700261 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700262 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700263 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700264 draw();
John Recka5dda642014-05-22 15:43:54 -0700265 }
John Recke45b1fd2014-04-15 09:50:16 -0700266}
267
John Reck3b202512014-06-23 13:13:08 -0700268void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700269 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700270 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700271 if (thread.eglManager().hasEglContext()) {
272 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700273 mode = DrawGlInfo::kModeProcess;
274 }
John Reck6f07a0d2014-04-16 21:31:25 -0700275
John Reck3b202512014-06-23 13:13:08 -0700276 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800277}
278
John Reck998a6d82014-08-28 15:35:53 -0700279void CanvasContext::markLayerInUse(RenderNode* node) {
280 if (mPrefetechedLayers.erase(node)) {
281 node->decStrong(0);
282 }
283}
284
285static void destroyPrefetechedNode(RenderNode* node) {
286 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
287 node->destroyHardwareResources();
288 node->decStrong(0);
289}
290
291void CanvasContext::freePrefetechedLayers() {
292 if (mPrefetechedLayers.size()) {
293 requireGlContext();
294 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
295 mPrefetechedLayers.clear();
296 }
297}
298
John Reck3e824952014-08-20 10:08:39 -0700299void CanvasContext::buildLayer(RenderNode* node) {
300 ATRACE_CALL();
301 if (!mEglManager.hasEglContext() || !mCanvas) {
302 return;
303 }
304 requireGlContext();
305 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
306 stopDrawing();
307
308 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700309 info.damageAccumulator = &mDamageAccumulator;
310 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700311 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700312 node->prepareTree(info);
313 SkRect ignore;
314 mDamageAccumulator.finish(&ignore);
315 // Tickle the GENERIC property on node to mark it as dirty for damaging
316 // purposes when the frame is actually drawn
317 node->setPropertyFieldsDirty(RenderNode::GENERIC);
318
John Reck443a7142014-09-04 17:40:05 -0700319 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700320 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700321
322 node->incStrong(0);
323 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700324}
325
John Reck19b6bcf2014-02-14 20:03:38 -0800326bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
327 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700328 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700329 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800330}
331
John Reckf47a5942014-06-30 16:20:04 -0700332void CanvasContext::destroyHardwareResources() {
333 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700334 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700335 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700336 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700337 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700338 Caches::getInstance().flush(Caches::kFlushMode_Layers);
339 }
340}
341
342void CanvasContext::trimMemory(RenderThread& thread, int level) {
343 // No context means nothing to free
344 if (!thread.eglManager().hasEglContext()) return;
345
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200346 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700347 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700348 if (level >= TRIM_MEMORY_COMPLETE) {
349 Caches::getInstance().flush(Caches::kFlushMode_Full);
350 thread.eglManager().destroy();
351 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
352 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700353 }
354}
355
John Reckfc53ef272014-02-11 10:40:25 -0800356void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800357 requireGlContext();
358 task->run();
359}
360
John Reck1949e792014-04-08 15:18:56 -0700361Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700362 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700363 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700364}
365
John Reck19b6bcf2014-02-14 20:03:38 -0800366void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700367 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800368}
369
John Reck3b202512014-06-23 13:13:08 -0700370void CanvasContext::setTextureAtlas(RenderThread& thread,
371 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
372 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700373}
374
John Reck23b797a2014-01-03 18:08:34 -0800375} /* namespace renderthread */
376} /* namespace uirenderer */
377} /* namespace android */