blob: 4129a89b6ef86ae1073080bd2f7c5da1a51147fe [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 Reck3b202512014-06-23 13:13:08 -070094 mEglManager.swapBuffers(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080095 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -080096}
97
John Reckf7d9c1d2014-04-09 10:01:03 -070098void CanvasContext::requireSurface() {
99 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
100 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700101 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800102}
103
John Recka5dda642014-05-22 15:43:54 -0700104bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800105 if (mCanvas) return false;
106 setSurface(window);
John Reck3b202512014-06-23 13:13:08 -0700107 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800108 mCanvas->initProperties();
109 return true;
110}
111
John Recka5dda642014-05-22 15:43:54 -0700112void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800113 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700114}
115
John Recka5dda642014-05-22 15:43:54 -0700116void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck16617152014-09-02 15:44:14 -0700117 stopDrawing();
John Reck4f02bf42014-01-03 18:09:17 -0800118}
119
Chris Craik058fc642014-07-23 18:19:28 -0700120void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
121 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800122 if (!mCanvas) return;
123 mCanvas->setViewport(width, height);
Chris Craik058fc642014-07-23 18:19:28 -0700124 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800125}
126
John Reck63a06672014-05-07 13:45:54 -0700127void CanvasContext::setOpaque(bool opaque) {
128 mOpaque = opaque;
129}
130
John Reck860d1552014-04-11 19:15:05 -0700131void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700132 // TODO: Figure out why this workaround is needed, see b/13913604
133 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700134 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700135}
136
John Reck68bfe0a2014-06-24 15:34:58 -0700137void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
138 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700139 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
140 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
141 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800142 }
143}
144
John Recke45b1fd2014-04-15 09:50:16 -0700145void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700146 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700147
John Recke4267ea2014-06-03 15:53:15 -0700148 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700149 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700150 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
151 info.canvasContext = this;
152 }
John Reck119907c2014-08-14 09:02:01 -0700153 mAnimationContext->startFrame();
John Recke45b1fd2014-04-15 09:50:16 -0700154 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700155 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700156
John Reck998a6d82014-08-28 15:35:53 -0700157 if (info.canvasContext) {
158 freePrefetechedLayers();
159 }
160
John Recka5dda642014-05-22 15:43:54 -0700161 int runningBehind = 0;
162 // TODO: This query is moderately expensive, investigate adding some sort
163 // of fast-path based off when we last called eglSwapBuffers() as well as
164 // last vsync time. Or something.
165 mNativeWindow->query(mNativeWindow.get(),
166 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
167 info.out.canDrawThisFrame = !runningBehind;
168
169 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700170 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700171 // If animationsNeedsRedraw is set don't bother posting for an RT anim
172 // as we will just end up fighting the UI thread.
173 mRenderThread.postFrameCallback(this);
174 }
John Recke45b1fd2014-04-15 09:50:16 -0700175 }
176}
177
John Reckf47a5942014-06-30 16:20:04 -0700178void CanvasContext::stopDrawing() {
179 mRenderThread.removeFrameCallback(this);
180}
181
John Recka5dda642014-05-22 15:43:54 -0700182void CanvasContext::notifyFramePending() {
183 ATRACE_CALL();
184 mRenderThread.pushBackFrameCallback(this);
185}
186
John Recke4267ea2014-06-03 15:53:15 -0700187void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800188 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700189 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800190
John Reckfe5e7b72014-05-23 17:42:28 -0700191 profiler().markPlaybackStart();
192
John Recke4267ea2014-06-03 15:53:15 -0700193 SkRect dirty;
194 mDamageAccumulator.finish(&dirty);
195
John Reck4f02bf42014-01-03 18:09:17 -0800196 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700197 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800198 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
199 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700200 dirty.setEmpty();
John Reck4f02bf42014-01-03 18:09:17 -0800201 } else if (!mDirtyRegionsEnabled || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700202 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700203 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700204 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700205 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
206 SK_RECT_ARGS(dirty), width, height);
207 dirty.setEmpty();
208 }
John Recke4267ea2014-06-03 15:53:15 -0700209 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800210 }
211
212 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700213 if (!dirty.isEmpty()) {
214 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
215 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800216 } else {
217 status = mCanvas->prepare(mOpaque);
218 }
219
220 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700221 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800222
John Reckfe5e7b72014-05-23 17:42:28 -0700223 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800224
225 mCanvas->finish();
226
John Reckfe5e7b72014-05-23 17:42:28 -0700227 profiler().markPlaybackEnd();
228
John Reck4f02bf42014-01-03 18:09:17 -0800229 if (status & DrawGlInfo::kStatusDrew) {
230 swapBuffers();
231 }
John Reckfe5e7b72014-05-23 17:42:28 -0700232
233 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800234}
235
John Recke45b1fd2014-04-15 09:50:16 -0700236// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700237void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700238 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
239 return;
240 }
241
John Recke45b1fd2014-04-15 09:50:16 -0700242 ATRACE_CALL();
243
John Reckfe5e7b72014-05-23 17:42:28 -0700244 profiler().startFrame();
245
John Reck3b202512014-06-23 13:13:08 -0700246 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700247 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700248 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700249 draw();
John Recka5dda642014-05-22 15:43:54 -0700250 }
John Recke45b1fd2014-04-15 09:50:16 -0700251}
252
John Reck3b202512014-06-23 13:13:08 -0700253void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700254 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700255 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700256 if (thread.eglManager().hasEglContext()) {
257 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700258 mode = DrawGlInfo::kModeProcess;
259 }
John Reck6f07a0d2014-04-16 21:31:25 -0700260
John Reck3b202512014-06-23 13:13:08 -0700261 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800262}
263
John Reck998a6d82014-08-28 15:35:53 -0700264void CanvasContext::markLayerInUse(RenderNode* node) {
265 if (mPrefetechedLayers.erase(node)) {
266 node->decStrong(0);
267 }
268}
269
270static void destroyPrefetechedNode(RenderNode* node) {
271 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
272 node->destroyHardwareResources();
273 node->decStrong(0);
274}
275
276void CanvasContext::freePrefetechedLayers() {
277 if (mPrefetechedLayers.size()) {
278 requireGlContext();
279 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
280 mPrefetechedLayers.clear();
281 }
282}
283
John Reck3e824952014-08-20 10:08:39 -0700284void CanvasContext::buildLayer(RenderNode* node) {
285 ATRACE_CALL();
286 if (!mEglManager.hasEglContext() || !mCanvas) {
287 return;
288 }
289 requireGlContext();
290 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
291 stopDrawing();
292
293 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700294 info.damageAccumulator = &mDamageAccumulator;
295 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700296 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700297 node->prepareTree(info);
298 SkRect ignore;
299 mDamageAccumulator.finish(&ignore);
300 // Tickle the GENERIC property on node to mark it as dirty for damaging
301 // purposes when the frame is actually drawn
302 node->setPropertyFieldsDirty(RenderNode::GENERIC);
303
John Reck443a7142014-09-04 17:40:05 -0700304 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700305 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700306
307 node->incStrong(0);
308 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700309}
310
John Reck19b6bcf2014-02-14 20:03:38 -0800311bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
312 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700313 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700314 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800315}
316
John Reckf47a5942014-06-30 16:20:04 -0700317void CanvasContext::destroyHardwareResources() {
318 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700319 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700320 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700321 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700322 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700323 Caches::getInstance().flush(Caches::kFlushMode_Layers);
324 }
325}
326
327void CanvasContext::trimMemory(RenderThread& thread, int level) {
328 // No context means nothing to free
329 if (!thread.eglManager().hasEglContext()) return;
330
John Reck3c2b7fa2014-07-07 09:16:54 -0700331 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700332 if (level >= TRIM_MEMORY_COMPLETE) {
333 Caches::getInstance().flush(Caches::kFlushMode_Full);
334 thread.eglManager().destroy();
335 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
336 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700337 }
338}
339
John Reckfc53ef272014-02-11 10:40:25 -0800340void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800341 requireGlContext();
342 task->run();
343}
344
John Reck1949e792014-04-08 15:18:56 -0700345Layer* CanvasContext::createRenderLayer(int width, int height) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700346 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700347 return LayerRenderer::createRenderLayer(mRenderThread.renderState(), width, height);
John Reck1949e792014-04-08 15:18:56 -0700348}
349
350Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700351 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700352 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700353}
354
John Reck19b6bcf2014-02-14 20:03:38 -0800355void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700356 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800357}
358
John Reck3b202512014-06-23 13:13:08 -0700359void CanvasContext::setTextureAtlas(RenderThread& thread,
360 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
361 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700362}
363
John Reck23b797a2014-01-03 18:08:34 -0800364} /* namespace renderthread */
365} /* namespace uirenderer */
366} /* namespace android */