blob: 428e42605107c675a4e713d2b51c571f3d8cb64c [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 Reck23b797a2014-01-03 18:08:34 -080051}
52
53CanvasContext::~CanvasContext() {
John Reck17035b02014-09-03 07:39:53 -070054 destroy();
John Reck119907c2014-08-14 09:02:01 -070055 delete mAnimationContext;
John Reck4f02bf42014-01-03 18:09:17 -080056}
57
John Reck17035b02014-09-03 07:39:53 -070058void CanvasContext::destroy() {
59 stopDrawing();
60 freePrefetechedLayers();
61 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070062 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080063 if (mCanvas) {
64 delete mCanvas;
65 mCanvas = 0;
66 }
John Reck23b797a2014-01-03 18:08:34 -080067 setSurface(NULL);
68}
69
John Recka5dda642014-05-22 15:43:54 -070070void CanvasContext::setSurface(ANativeWindow* window) {
71 mNativeWindow = window;
72
John Reck23b797a2014-01-03 18:08:34 -080073 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070074 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080075 mEglSurface = EGL_NO_SURFACE;
76 }
77
78 if (window) {
John Reck3b202512014-06-23 13:13:08 -070079 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080080 }
81
82 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070083 mDirtyRegionsEnabled = mEglManager.enableDirtyRegions(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080084 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070085 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070086 } else {
87 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080088 }
John Reck23b797a2014-01-03 18:08:34 -080089}
90
John Reck4f02bf42014-01-03 18:09:17 -080091void CanvasContext::swapBuffers() {
John Reck3b202512014-06-23 13:13:08 -070092 mEglManager.swapBuffers(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080093 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -080094}
95
John Reckf7d9c1d2014-04-09 10:01:03 -070096void CanvasContext::requireSurface() {
97 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
98 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -070099 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800100}
101
John Recka5dda642014-05-22 15:43:54 -0700102bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800103 if (mCanvas) return false;
104 setSurface(window);
John Reck3b202512014-06-23 13:13:08 -0700105 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800106 mCanvas->initProperties();
107 return true;
108}
109
John Recka5dda642014-05-22 15:43:54 -0700110void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800111 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700112}
113
John Recka5dda642014-05-22 15:43:54 -0700114void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck16617152014-09-02 15:44:14 -0700115 stopDrawing();
John Reck4f02bf42014-01-03 18:09:17 -0800116}
117
Chris Craik058fc642014-07-23 18:19:28 -0700118void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
119 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800120 if (!mCanvas) return;
121 mCanvas->setViewport(width, height);
Chris Craik058fc642014-07-23 18:19:28 -0700122 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800123}
124
John Reck63a06672014-05-07 13:45:54 -0700125void CanvasContext::setOpaque(bool opaque) {
126 mOpaque = opaque;
127}
128
John Reck860d1552014-04-11 19:15:05 -0700129void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700130 // TODO: Figure out why this workaround is needed, see b/13913604
131 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700132 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700133}
134
John Reck68bfe0a2014-06-24 15:34:58 -0700135void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
136 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700137 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
138 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
139 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800140 }
141}
142
John Recke45b1fd2014-04-15 09:50:16 -0700143void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700144 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700145
John Recke4267ea2014-06-03 15:53:15 -0700146 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700147 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700148 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
149 info.canvasContext = this;
150 }
John Reck119907c2014-08-14 09:02:01 -0700151 mAnimationContext->startFrame();
John Recke45b1fd2014-04-15 09:50:16 -0700152 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700153 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700154
John Reck998a6d82014-08-28 15:35:53 -0700155 if (info.canvasContext) {
156 freePrefetechedLayers();
157 }
158
John Recka5dda642014-05-22 15:43:54 -0700159 int runningBehind = 0;
160 // TODO: This query is moderately expensive, investigate adding some sort
161 // of fast-path based off when we last called eglSwapBuffers() as well as
162 // last vsync time. Or something.
163 mNativeWindow->query(mNativeWindow.get(),
164 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
165 info.out.canDrawThisFrame = !runningBehind;
166
167 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700168 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700169 // If animationsNeedsRedraw is set don't bother posting for an RT anim
170 // as we will just end up fighting the UI thread.
171 mRenderThread.postFrameCallback(this);
172 }
John Recke45b1fd2014-04-15 09:50:16 -0700173 }
174}
175
John Reckf47a5942014-06-30 16:20:04 -0700176void CanvasContext::stopDrawing() {
177 mRenderThread.removeFrameCallback(this);
178}
179
John Recka5dda642014-05-22 15:43:54 -0700180void CanvasContext::notifyFramePending() {
181 ATRACE_CALL();
182 mRenderThread.pushBackFrameCallback(this);
183}
184
John Recke4267ea2014-06-03 15:53:15 -0700185void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800186 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700187 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800188
John Reckfe5e7b72014-05-23 17:42:28 -0700189 profiler().markPlaybackStart();
190
John Recke4267ea2014-06-03 15:53:15 -0700191 SkRect dirty;
192 mDamageAccumulator.finish(&dirty);
193
John Reck4f02bf42014-01-03 18:09:17 -0800194 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700195 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800196 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
197 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700198 dirty.setEmpty();
John Reck4f02bf42014-01-03 18:09:17 -0800199 } else if (!mDirtyRegionsEnabled || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700200 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700201 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700202 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700203 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
204 SK_RECT_ARGS(dirty), width, height);
205 dirty.setEmpty();
206 }
John Recke4267ea2014-06-03 15:53:15 -0700207 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800208 }
209
210 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700211 if (!dirty.isEmpty()) {
212 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
213 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800214 } else {
215 status = mCanvas->prepare(mOpaque);
216 }
217
218 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700219 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800220
John Reckfe5e7b72014-05-23 17:42:28 -0700221 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800222
223 mCanvas->finish();
224
John Reckfe5e7b72014-05-23 17:42:28 -0700225 profiler().markPlaybackEnd();
226
John Reck4f02bf42014-01-03 18:09:17 -0800227 if (status & DrawGlInfo::kStatusDrew) {
228 swapBuffers();
229 }
John Reckfe5e7b72014-05-23 17:42:28 -0700230
231 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800232}
233
John Recke45b1fd2014-04-15 09:50:16 -0700234// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700235void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700236 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
237 return;
238 }
239
John Recke45b1fd2014-04-15 09:50:16 -0700240 ATRACE_CALL();
241
John Reckfe5e7b72014-05-23 17:42:28 -0700242 profiler().startFrame();
243
John Reck3b202512014-06-23 13:13:08 -0700244 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700245 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700246 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700247 draw();
John Recka5dda642014-05-22 15:43:54 -0700248 }
John Recke45b1fd2014-04-15 09:50:16 -0700249}
250
John Reck3b202512014-06-23 13:13:08 -0700251void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700252 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700253 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700254 if (thread.eglManager().hasEglContext()) {
255 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700256 mode = DrawGlInfo::kModeProcess;
257 }
John Reck6f07a0d2014-04-16 21:31:25 -0700258
John Reck3b202512014-06-23 13:13:08 -0700259 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800260}
261
John Reck998a6d82014-08-28 15:35:53 -0700262void CanvasContext::markLayerInUse(RenderNode* node) {
263 if (mPrefetechedLayers.erase(node)) {
264 node->decStrong(0);
265 }
266}
267
268static void destroyPrefetechedNode(RenderNode* node) {
269 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
270 node->destroyHardwareResources();
271 node->decStrong(0);
272}
273
274void CanvasContext::freePrefetechedLayers() {
275 if (mPrefetechedLayers.size()) {
276 requireGlContext();
277 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
278 mPrefetechedLayers.clear();
279 }
280}
281
John Reck3e824952014-08-20 10:08:39 -0700282void CanvasContext::buildLayer(RenderNode* node) {
283 ATRACE_CALL();
284 if (!mEglManager.hasEglContext() || !mCanvas) {
285 return;
286 }
287 requireGlContext();
288 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
289 stopDrawing();
290
291 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700292 info.damageAccumulator = &mDamageAccumulator;
293 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700294 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700295 node->prepareTree(info);
296 SkRect ignore;
297 mDamageAccumulator.finish(&ignore);
298 // Tickle the GENERIC property on node to mark it as dirty for damaging
299 // purposes when the frame is actually drawn
300 node->setPropertyFieldsDirty(RenderNode::GENERIC);
301
302 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700303
304 node->incStrong(0);
305 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700306}
307
John Reck19b6bcf2014-02-14 20:03:38 -0800308bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
309 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700310 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700311 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800312}
313
John Reckf47a5942014-06-30 16:20:04 -0700314void CanvasContext::destroyHardwareResources() {
315 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700316 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700317 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700318 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700319 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700320 Caches::getInstance().flush(Caches::kFlushMode_Layers);
321 }
322}
323
324void CanvasContext::trimMemory(RenderThread& thread, int level) {
325 // No context means nothing to free
326 if (!thread.eglManager().hasEglContext()) return;
327
John Reck3c2b7fa2014-07-07 09:16:54 -0700328 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700329 if (level >= TRIM_MEMORY_COMPLETE) {
330 Caches::getInstance().flush(Caches::kFlushMode_Full);
331 thread.eglManager().destroy();
332 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
333 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700334 }
335}
336
John Reckfc53ef22014-02-11 10:40:25 -0800337void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800338 requireGlContext();
339 task->run();
340}
341
John Reck1949e792014-04-08 15:18:56 -0700342Layer* CanvasContext::createRenderLayer(int width, int height) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700343 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700344 return LayerRenderer::createRenderLayer(mRenderThread.renderState(), width, height);
John Reck1949e792014-04-08 15:18:56 -0700345}
346
347Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700348 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700349 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700350}
351
John Reck19b6bcf2014-02-14 20:03:38 -0800352void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700353 mEglManager.requireGlContext();
John Reckfc53ef22014-02-11 10:40:25 -0800354}
355
John Reck3b202512014-06-23 13:13:08 -0700356void CanvasContext::setTextureAtlas(RenderThread& thread,
357 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
358 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700359}
360
John Reck23b797a2014-01-03 18:08:34 -0800361} /* namespace renderthread */
362} /* namespace uirenderer */
363} /* namespace android */