blob: 3bb47786b2231443be099dc3611a84e72a42283c [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)
45 , mDirtyRegionsEnabled(false)
46 , mOpaque(!translucent)
John Reck3b202512014-06-23 13:13:08 -070047 , mCanvas(NULL)
John Recke45b1fd2014-04-15 09:50:16 -070048 , mHaveNewSurface(false)
49 , mRootRenderNode(rootRenderNode) {
John Reck119907c2014-08-14 09:02:01 -070050 mAnimationContext = contextFactory->createAnimationContext(mRenderThread.timeLord());
John Reck443a7142014-09-04 17:40:05 -070051 mRenderThread.renderState().registerCanvasContext(this);
John Reck23b797a2014-01-03 18:08:34 -080052}
53
54CanvasContext::~CanvasContext() {
John Reck17035b02014-09-03 07:39:53 -070055 destroy();
John Reck119907c2014-08-14 09:02:01 -070056 delete mAnimationContext;
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();
62 freePrefetechedLayers();
63 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070064 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080065 if (mCanvas) {
66 delete mCanvas;
67 mCanvas = 0;
68 }
John Reck23b797a2014-01-03 18:08:34 -080069 setSurface(NULL);
70}
71
John Recka5dda642014-05-22 15:43:54 -070072void CanvasContext::setSurface(ANativeWindow* window) {
73 mNativeWindow = window;
74
John Reck23b797a2014-01-03 18:08:34 -080075 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070076 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080077 mEglSurface = EGL_NO_SURFACE;
78 }
79
80 if (window) {
John Reck3b202512014-06-23 13:13:08 -070081 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080082 }
83
84 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070085 mDirtyRegionsEnabled = mEglManager.enableDirtyRegions(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080086 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070087 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070088 } else {
89 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080090 }
John Reck23b797a2014-01-03 18:08:34 -080091}
92
John Reck4f02bf42014-01-03 18:09:17 -080093void CanvasContext::swapBuffers() {
John Reck2cdbc7d2014-09-17 16:06:36 -070094 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
95 setSurface(NULL);
96 }
John Reck4f02bf42014-01-03 18:09:17 -080097 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -080098}
99
John Reckf7d9c1d2014-04-09 10:01:03 -0700100void CanvasContext::requireSurface() {
101 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
102 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700103 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800104}
105
John Recka5dda642014-05-22 15:43:54 -0700106bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800107 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700108 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700109 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800110 mCanvas->initProperties();
111 return true;
112}
113
John Recka5dda642014-05-22 15:43:54 -0700114void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800115 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700116}
117
John Recka5dda642014-05-22 15:43:54 -0700118void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck16617152014-09-02 15:44:14 -0700119 stopDrawing();
John Reck4f02bf42014-01-03 18:09:17 -0800120}
121
Chris Craik284b2432014-09-18 16:05:35 -0700122// TODO: don't pass viewport size, it's automatic via EGL
Chris Craik058fc642014-07-23 18:19:28 -0700123void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
124 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800125 if (!mCanvas) return;
Chris Craik058fc642014-07-23 18:19:28 -0700126 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800127}
128
John Reck63a06672014-05-07 13:45:54 -0700129void CanvasContext::setOpaque(bool opaque) {
130 mOpaque = opaque;
131}
132
John Reck860d1552014-04-11 19:15:05 -0700133void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700134 // TODO: Figure out why this workaround is needed, see b/13913604
135 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700136 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700137}
138
John Reck68bfe0a2014-06-24 15:34:58 -0700139void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
140 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700141 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
142 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
143 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800144 }
145}
146
John Recke45b1fd2014-04-15 09:50:16 -0700147void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700148 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700149
John Recke4267ea2014-06-03 15:53:15 -0700150 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700151 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700152 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
153 info.canvasContext = this;
154 }
John Reckec845a22014-09-05 15:23:38 -0700155 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700156 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700157 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700158
John Reck998a6d82014-08-28 15:35:53 -0700159 if (info.canvasContext) {
160 freePrefetechedLayers();
161 }
162
John Recka5dda642014-05-22 15:43:54 -0700163 int runningBehind = 0;
164 // TODO: This query is moderately expensive, investigate adding some sort
165 // of fast-path based off when we last called eglSwapBuffers() as well as
166 // last vsync time. Or something.
167 mNativeWindow->query(mNativeWindow.get(),
168 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
169 info.out.canDrawThisFrame = !runningBehind;
170
171 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700172 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700173 // If animationsNeedsRedraw is set don't bother posting for an RT anim
174 // as we will just end up fighting the UI thread.
175 mRenderThread.postFrameCallback(this);
176 }
John Recke45b1fd2014-04-15 09:50:16 -0700177 }
178}
179
John Reckf47a5942014-06-30 16:20:04 -0700180void CanvasContext::stopDrawing() {
181 mRenderThread.removeFrameCallback(this);
182}
183
John Recka5dda642014-05-22 15:43:54 -0700184void CanvasContext::notifyFramePending() {
185 ATRACE_CALL();
186 mRenderThread.pushBackFrameCallback(this);
187}
188
John Recke4267ea2014-06-03 15:53:15 -0700189void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800190 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700191 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800192
John Reckfe5e7b72014-05-23 17:42:28 -0700193 profiler().markPlaybackStart();
194
John Recke4267ea2014-06-03 15:53:15 -0700195 SkRect dirty;
196 mDamageAccumulator.finish(&dirty);
197
John Reck4f02bf42014-01-03 18:09:17 -0800198 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700199 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800200 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
201 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700202 dirty.setEmpty();
John Reck4f02bf42014-01-03 18:09:17 -0800203 } else if (!mDirtyRegionsEnabled || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700204 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700205 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700206 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700207 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
208 SK_RECT_ARGS(dirty), width, height);
209 dirty.setEmpty();
210 }
John Recke4267ea2014-06-03 15:53:15 -0700211 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800212 }
213
214 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700215 if (!dirty.isEmpty()) {
216 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
217 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800218 } else {
219 status = mCanvas->prepare(mOpaque);
220 }
221
222 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700223 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800224
John Reckfe5e7b72014-05-23 17:42:28 -0700225 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800226
227 mCanvas->finish();
228
John Reckfe5e7b72014-05-23 17:42:28 -0700229 profiler().markPlaybackEnd();
230
John Reck4f02bf42014-01-03 18:09:17 -0800231 if (status & DrawGlInfo::kStatusDrew) {
232 swapBuffers();
233 }
John Reckfe5e7b72014-05-23 17:42:28 -0700234
235 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800236}
237
John Recke45b1fd2014-04-15 09:50:16 -0700238// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700239void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700240 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
241 return;
242 }
243
John Recke45b1fd2014-04-15 09:50:16 -0700244 ATRACE_CALL();
245
John Reckfe5e7b72014-05-23 17:42:28 -0700246 profiler().startFrame();
247
John Reck3b202512014-06-23 13:13:08 -0700248 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700249 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700250 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700251 draw();
John Recka5dda642014-05-22 15:43:54 -0700252 }
John Recke45b1fd2014-04-15 09:50:16 -0700253}
254
John Reck3b202512014-06-23 13:13:08 -0700255void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700256 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700257 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700258 if (thread.eglManager().hasEglContext()) {
259 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700260 mode = DrawGlInfo::kModeProcess;
261 }
John Reck6f07a0d2014-04-16 21:31:25 -0700262
John Reck3b202512014-06-23 13:13:08 -0700263 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800264}
265
John Reck998a6d82014-08-28 15:35:53 -0700266void CanvasContext::markLayerInUse(RenderNode* node) {
267 if (mPrefetechedLayers.erase(node)) {
268 node->decStrong(0);
269 }
270}
271
272static void destroyPrefetechedNode(RenderNode* node) {
273 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
274 node->destroyHardwareResources();
275 node->decStrong(0);
276}
277
278void CanvasContext::freePrefetechedLayers() {
279 if (mPrefetechedLayers.size()) {
280 requireGlContext();
281 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
282 mPrefetechedLayers.clear();
283 }
284}
285
John Reck3e824952014-08-20 10:08:39 -0700286void CanvasContext::buildLayer(RenderNode* node) {
287 ATRACE_CALL();
288 if (!mEglManager.hasEglContext() || !mCanvas) {
289 return;
290 }
291 requireGlContext();
292 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
293 stopDrawing();
294
295 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700296 info.damageAccumulator = &mDamageAccumulator;
297 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700298 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700299 node->prepareTree(info);
300 SkRect ignore;
301 mDamageAccumulator.finish(&ignore);
302 // Tickle the GENERIC property on node to mark it as dirty for damaging
303 // purposes when the frame is actually drawn
304 node->setPropertyFieldsDirty(RenderNode::GENERIC);
305
John Reck443a7142014-09-04 17:40:05 -0700306 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700307 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700308
309 node->incStrong(0);
310 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700311}
312
John Reck19b6bcf2014-02-14 20:03:38 -0800313bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
314 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700315 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700316 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800317}
318
John Reckf47a5942014-06-30 16:20:04 -0700319void CanvasContext::destroyHardwareResources() {
320 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700321 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700322 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700323 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700324 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700325 Caches::getInstance().flush(Caches::kFlushMode_Layers);
326 }
327}
328
329void CanvasContext::trimMemory(RenderThread& thread, int level) {
330 // No context means nothing to free
331 if (!thread.eglManager().hasEglContext()) return;
332
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200333 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700334 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700335 if (level >= TRIM_MEMORY_COMPLETE) {
336 Caches::getInstance().flush(Caches::kFlushMode_Full);
337 thread.eglManager().destroy();
338 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
339 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700340 }
341}
342
John Reckfc53ef272014-02-11 10:40:25 -0800343void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800344 requireGlContext();
345 task->run();
346}
347
John Reck1949e792014-04-08 15:18:56 -0700348Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700349 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700350 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700351}
352
John Reck19b6bcf2014-02-14 20:03:38 -0800353void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700354 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800355}
356
John Reck3b202512014-06-23 13:13:08 -0700357void CanvasContext::setTextureAtlas(RenderThread& thread,
358 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
359 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700360}
361
John Reck23b797a2014-01-03 18:08:34 -0800362} /* namespace renderthread */
363} /* namespace uirenderer */
364} /* namespace android */