blob: 75bd0676d70a4267f693fb952cd3cf87e24d41bc [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();
Sangkyu Leea12b2402014-11-21 18:05:41 +090063 setSurface(NULL);
John Reck17035b02014-09-03 07:39:53 -070064 freePrefetechedLayers();
65 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070066 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080067 if (mCanvas) {
68 delete mCanvas;
69 mCanvas = 0;
70 }
John Reck23b797a2014-01-03 18:08:34 -080071}
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
John Reck01a5ea32014-12-03 13:01:07 -0800126bool CanvasContext::pauseSurface(ANativeWindow* window) {
127 return mRenderThread.removeFrameCallback(this);
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
Chris Craik058fc642014-07-23 18:19:28 -0700131void 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
227 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700228 if (!dirty.isEmpty()) {
229 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
230 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800231 } else {
232 status = mCanvas->prepare(mOpaque);
233 }
234
235 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700236 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800237
John Reckfe5e7b72014-05-23 17:42:28 -0700238 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800239
240 mCanvas->finish();
241
John Reckfe5e7b72014-05-23 17:42:28 -0700242 profiler().markPlaybackEnd();
243
John Reck4f02bf42014-01-03 18:09:17 -0800244 if (status & DrawGlInfo::kStatusDrew) {
245 swapBuffers();
John Reck0e89e2b2014-10-31 14:49:06 -0700246 } else {
247 mEglManager.cancelFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800248 }
John Reckfe5e7b72014-05-23 17:42:28 -0700249
250 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800251}
252
John Recke45b1fd2014-04-15 09:50:16 -0700253// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700254void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700255 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
256 return;
257 }
258
John Recke45b1fd2014-04-15 09:50:16 -0700259 ATRACE_CALL();
260
John Reckfe5e7b72014-05-23 17:42:28 -0700261 profiler().startFrame();
262
John Reck3b202512014-06-23 13:13:08 -0700263 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700264 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700265 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700266 draw();
John Recka5dda642014-05-22 15:43:54 -0700267 }
John Recke45b1fd2014-04-15 09:50:16 -0700268}
269
John Reck3b202512014-06-23 13:13:08 -0700270void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700271 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700272 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700273 if (thread.eglManager().hasEglContext()) {
274 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700275 mode = DrawGlInfo::kModeProcess;
276 }
John Reck6f07a0d2014-04-16 21:31:25 -0700277
John Reck3b202512014-06-23 13:13:08 -0700278 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800279}
280
John Reck998a6d82014-08-28 15:35:53 -0700281void CanvasContext::markLayerInUse(RenderNode* node) {
282 if (mPrefetechedLayers.erase(node)) {
283 node->decStrong(0);
284 }
285}
286
287static void destroyPrefetechedNode(RenderNode* node) {
288 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
289 node->destroyHardwareResources();
290 node->decStrong(0);
291}
292
293void CanvasContext::freePrefetechedLayers() {
294 if (mPrefetechedLayers.size()) {
295 requireGlContext();
296 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
297 mPrefetechedLayers.clear();
298 }
299}
300
John Reck3e824952014-08-20 10:08:39 -0700301void CanvasContext::buildLayer(RenderNode* node) {
302 ATRACE_CALL();
303 if (!mEglManager.hasEglContext() || !mCanvas) {
304 return;
305 }
306 requireGlContext();
307 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
308 stopDrawing();
309
310 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700311 info.damageAccumulator = &mDamageAccumulator;
312 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700313 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700314 node->prepareTree(info);
315 SkRect ignore;
316 mDamageAccumulator.finish(&ignore);
317 // Tickle the GENERIC property on node to mark it as dirty for damaging
318 // purposes when the frame is actually drawn
319 node->setPropertyFieldsDirty(RenderNode::GENERIC);
320
John Reck443a7142014-09-04 17:40:05 -0700321 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700322 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700323
324 node->incStrong(0);
325 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700326}
327
John Reck19b6bcf2014-02-14 20:03:38 -0800328bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
329 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700330 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700331 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800332}
333
John Reckf47a5942014-06-30 16:20:04 -0700334void CanvasContext::destroyHardwareResources() {
335 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700336 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700337 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700338 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700339 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700340 Caches::getInstance().flush(Caches::kFlushMode_Layers);
341 }
342}
343
344void CanvasContext::trimMemory(RenderThread& thread, int level) {
345 // No context means nothing to free
346 if (!thread.eglManager().hasEglContext()) return;
347
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200348 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700349 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700350 if (level >= TRIM_MEMORY_COMPLETE) {
351 Caches::getInstance().flush(Caches::kFlushMode_Full);
352 thread.eglManager().destroy();
353 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
354 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700355 }
356}
357
John Reckfc53ef272014-02-11 10:40:25 -0800358void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800359 requireGlContext();
360 task->run();
361}
362
John Reck1949e792014-04-08 15:18:56 -0700363Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700364 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700365 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700366}
367
John Reck19b6bcf2014-02-14 20:03:38 -0800368void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700369 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800370}
371
John Reck3b202512014-06-23 13:13:08 -0700372void CanvasContext::setTextureAtlas(RenderThread& thread,
373 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
374 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700375}
376
John Reck23b797a2014-01-03 18:08:34 -0800377} /* namespace renderthread */
378} /* namespace uirenderer */
379} /* namespace android */