blob: 9d2ae8b6d8adcc330b1971d5fa6b55c11c22b31c [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) {
74 mNativeWindow = window;
75
John Reck23b797a2014-01-03 18:08:34 -080076 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070077 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080078 mEglSurface = EGL_NO_SURFACE;
79 }
80
81 if (window) {
John Reck3b202512014-06-23 13:13:08 -070082 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080083 }
84
85 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -070086 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
87 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -080088 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070089 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070090 } else {
91 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080092 }
John Reck23b797a2014-01-03 18:08:34 -080093}
94
John Reck4f02bf42014-01-03 18:09:17 -080095void CanvasContext::swapBuffers() {
John Reck2cdbc7d2014-09-17 16:06:36 -070096 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
97 setSurface(NULL);
98 }
John Reck4f02bf42014-01-03 18:09:17 -080099 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -0800100}
101
John Reckf7d9c1d2014-04-09 10:01:03 -0700102void CanvasContext::requireSurface() {
103 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
104 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700105 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800106}
107
John Reck1125d1f2014-10-23 11:02:19 -0700108void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
109 mSwapBehavior = swapBehavior;
110}
111
John Recka5dda642014-05-22 15:43:54 -0700112bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800113 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700114 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700115 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800116 mCanvas->initProperties();
117 return true;
118}
119
John Recka5dda642014-05-22 15:43:54 -0700120void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800121 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700122}
123
John Recka5dda642014-05-22 15:43:54 -0700124void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck16617152014-09-02 15:44:14 -0700125 stopDrawing();
John Reck4f02bf42014-01-03 18:09:17 -0800126}
127
Chris Craik284b2432014-09-18 16:05:35 -0700128// TODO: don't pass viewport size, it's automatic via EGL
Chris Craik058fc642014-07-23 18:19:28 -0700129void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
130 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800131 if (!mCanvas) return;
Chris Craik058fc642014-07-23 18:19:28 -0700132 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800133}
134
John Reck63a06672014-05-07 13:45:54 -0700135void CanvasContext::setOpaque(bool opaque) {
136 mOpaque = opaque;
137}
138
John Reck860d1552014-04-11 19:15:05 -0700139void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700140 // TODO: Figure out why this workaround is needed, see b/13913604
141 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700142 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700143}
144
John Reck68bfe0a2014-06-24 15:34:58 -0700145void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
146 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700147 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
148 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
149 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800150 }
151}
152
John Recke45b1fd2014-04-15 09:50:16 -0700153void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700154 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700155
John Recke4267ea2014-06-03 15:53:15 -0700156 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700157 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700158 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
159 info.canvasContext = this;
160 }
John Reckec845a22014-09-05 15:23:38 -0700161 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700162 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700163 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700164
John Reck998a6d82014-08-28 15:35:53 -0700165 if (info.canvasContext) {
166 freePrefetechedLayers();
167 }
168
John Recka5dda642014-05-22 15:43:54 -0700169 int runningBehind = 0;
170 // TODO: This query is moderately expensive, investigate adding some sort
171 // of fast-path based off when we last called eglSwapBuffers() as well as
172 // last vsync time. Or something.
173 mNativeWindow->query(mNativeWindow.get(),
174 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
175 info.out.canDrawThisFrame = !runningBehind;
176
177 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700178 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700179 // If animationsNeedsRedraw is set don't bother posting for an RT anim
180 // as we will just end up fighting the UI thread.
181 mRenderThread.postFrameCallback(this);
182 }
John Recke45b1fd2014-04-15 09:50:16 -0700183 }
184}
185
John Reckf47a5942014-06-30 16:20:04 -0700186void CanvasContext::stopDrawing() {
187 mRenderThread.removeFrameCallback(this);
188}
189
John Recka5dda642014-05-22 15:43:54 -0700190void CanvasContext::notifyFramePending() {
191 ATRACE_CALL();
192 mRenderThread.pushBackFrameCallback(this);
193}
194
John Recke4267ea2014-06-03 15:53:15 -0700195void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800196 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700197 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800198
John Reckfe5e7b72014-05-23 17:42:28 -0700199 profiler().markPlaybackStart();
200
John Recke4267ea2014-06-03 15:53:15 -0700201 SkRect dirty;
202 mDamageAccumulator.finish(&dirty);
203
John Reck4f02bf42014-01-03 18:09:17 -0800204 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700205 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800206 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
207 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700208 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700209 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700210 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700211 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700212 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700213 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
214 SK_RECT_ARGS(dirty), width, height);
215 dirty.setEmpty();
216 }
John Recke4267ea2014-06-03 15:53:15 -0700217 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800218 }
219
220 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700221 if (!dirty.isEmpty()) {
222 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
223 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800224 } else {
225 status = mCanvas->prepare(mOpaque);
226 }
227
228 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700229 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800230
John Reckfe5e7b72014-05-23 17:42:28 -0700231 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800232
233 mCanvas->finish();
234
John Reckfe5e7b72014-05-23 17:42:28 -0700235 profiler().markPlaybackEnd();
236
John Reck4f02bf42014-01-03 18:09:17 -0800237 if (status & DrawGlInfo::kStatusDrew) {
238 swapBuffers();
239 }
John Reckfe5e7b72014-05-23 17:42:28 -0700240
241 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800242}
243
John Recke45b1fd2014-04-15 09:50:16 -0700244// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700245void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700246 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
247 return;
248 }
249
John Recke45b1fd2014-04-15 09:50:16 -0700250 ATRACE_CALL();
251
John Reckfe5e7b72014-05-23 17:42:28 -0700252 profiler().startFrame();
253
John Reck3b202512014-06-23 13:13:08 -0700254 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700255 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700256 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700257 draw();
John Recka5dda642014-05-22 15:43:54 -0700258 }
John Recke45b1fd2014-04-15 09:50:16 -0700259}
260
John Reck3b202512014-06-23 13:13:08 -0700261void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700262 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700263 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700264 if (thread.eglManager().hasEglContext()) {
265 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700266 mode = DrawGlInfo::kModeProcess;
267 }
John Reck6f07a0d2014-04-16 21:31:25 -0700268
John Reck3b202512014-06-23 13:13:08 -0700269 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800270}
271
John Reck998a6d82014-08-28 15:35:53 -0700272void CanvasContext::markLayerInUse(RenderNode* node) {
273 if (mPrefetechedLayers.erase(node)) {
274 node->decStrong(0);
275 }
276}
277
278static void destroyPrefetechedNode(RenderNode* node) {
279 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
280 node->destroyHardwareResources();
281 node->decStrong(0);
282}
283
284void CanvasContext::freePrefetechedLayers() {
285 if (mPrefetechedLayers.size()) {
286 requireGlContext();
287 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
288 mPrefetechedLayers.clear();
289 }
290}
291
John Reck3e824952014-08-20 10:08:39 -0700292void CanvasContext::buildLayer(RenderNode* node) {
293 ATRACE_CALL();
294 if (!mEglManager.hasEglContext() || !mCanvas) {
295 return;
296 }
297 requireGlContext();
298 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
299 stopDrawing();
300
301 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700302 info.damageAccumulator = &mDamageAccumulator;
303 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700304 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700305 node->prepareTree(info);
306 SkRect ignore;
307 mDamageAccumulator.finish(&ignore);
308 // Tickle the GENERIC property on node to mark it as dirty for damaging
309 // purposes when the frame is actually drawn
310 node->setPropertyFieldsDirty(RenderNode::GENERIC);
311
John Reck443a7142014-09-04 17:40:05 -0700312 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700313 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700314
315 node->incStrong(0);
316 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700317}
318
John Reck19b6bcf2014-02-14 20:03:38 -0800319bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
320 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700321 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700322 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800323}
324
John Reckf47a5942014-06-30 16:20:04 -0700325void CanvasContext::destroyHardwareResources() {
326 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700327 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700328 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700329 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700330 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700331 Caches::getInstance().flush(Caches::kFlushMode_Layers);
332 }
333}
334
335void CanvasContext::trimMemory(RenderThread& thread, int level) {
336 // No context means nothing to free
337 if (!thread.eglManager().hasEglContext()) return;
338
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200339 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700340 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700341 if (level >= TRIM_MEMORY_COMPLETE) {
342 Caches::getInstance().flush(Caches::kFlushMode_Full);
343 thread.eglManager().destroy();
344 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
345 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700346 }
347}
348
John Reckfc53ef272014-02-11 10:40:25 -0800349void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800350 requireGlContext();
351 task->run();
352}
353
John Reck1949e792014-04-08 15:18:56 -0700354Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700355 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700356 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700357}
358
John Reck19b6bcf2014-02-14 20:03:38 -0800359void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700360 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800361}
362
John Reck3b202512014-06-23 13:13:08 -0700363void CanvasContext::setTextureAtlas(RenderThread& thread,
364 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
365 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700366}
367
John Reck23b797a2014-01-03 18:08:34 -0800368} /* namespace renderthread */
369} /* namespace uirenderer */
370} /* namespace android */