blob: 4f39ac98c60199870c4585db09a83aabcb008976 [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();
John Reckb945f232014-11-25 09:54:13 -080063 setSurface(NULL);
64 mEglManager.usePBufferSurface();
John Reck17035b02014-09-03 07:39:53 -070065 freePrefetechedLayers();
66 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070067 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080068 if (mCanvas) {
69 delete mCanvas;
70 mCanvas = 0;
71 }
John Reck23b797a2014-01-03 18:08:34 -080072}
73
John Recka5dda642014-05-22 15:43:54 -070074void CanvasContext::setSurface(ANativeWindow* window) {
John Reckfbc8df02014-11-14 16:18:41 -080075 ATRACE_CALL();
76
John Recka5dda642014-05-22 15:43:54 -070077 mNativeWindow = window;
78
John Reck23b797a2014-01-03 18:08:34 -080079 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070080 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080081 mEglSurface = EGL_NO_SURFACE;
82 }
83
84 if (window) {
John Reck3b202512014-06-23 13:13:08 -070085 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080086 }
87
88 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -070089 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
90 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -080091 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070092 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070093 } else {
94 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080095 }
John Reck23b797a2014-01-03 18:08:34 -080096}
97
John Reck4f02bf42014-01-03 18:09:17 -080098void CanvasContext::swapBuffers() {
John Reck2cdbc7d2014-09-17 16:06:36 -070099 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
100 setSurface(NULL);
101 }
John Reck4f02bf42014-01-03 18:09:17 -0800102 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -0800103}
104
John Reckf7d9c1d2014-04-09 10:01:03 -0700105void CanvasContext::requireSurface() {
106 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
107 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700108 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800109}
110
John Reck1125d1f2014-10-23 11:02:19 -0700111void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
112 mSwapBehavior = swapBehavior;
113}
114
John Recka5dda642014-05-22 15:43:54 -0700115bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800116 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700117 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700118 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800119 mCanvas->initProperties();
120 return true;
121}
122
John Recka5dda642014-05-22 15:43:54 -0700123void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800124 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700125}
126
John Recka5dda642014-05-22 15:43:54 -0700127void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck16617152014-09-02 15:44:14 -0700128 stopDrawing();
John Reck4f02bf42014-01-03 18:09:17 -0800129}
130
Chris Craik284b2432014-09-18 16:05:35 -0700131// TODO: don't pass viewport size, it's automatic via EGL
Chris Craik058fc642014-07-23 18:19:28 -0700132void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
133 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800134 if (!mCanvas) return;
Chris Craik058fc642014-07-23 18:19:28 -0700135 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800136}
137
John Reck63a06672014-05-07 13:45:54 -0700138void CanvasContext::setOpaque(bool opaque) {
139 mOpaque = opaque;
140}
141
John Reck860d1552014-04-11 19:15:05 -0700142void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700143 // TODO: Figure out why this workaround is needed, see b/13913604
144 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700145 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700146}
147
John Reck68bfe0a2014-06-24 15:34:58 -0700148void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
149 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700150 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
151 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
152 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800153 }
154}
155
John Recke45b1fd2014-04-15 09:50:16 -0700156void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700157 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700158
John Recke4267ea2014-06-03 15:53:15 -0700159 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700160 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700161 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
162 info.canvasContext = this;
163 }
John Reckec845a22014-09-05 15:23:38 -0700164 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700165 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700166 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700167
John Reck998a6d82014-08-28 15:35:53 -0700168 if (info.canvasContext) {
169 freePrefetechedLayers();
170 }
171
John Reckaa95a882014-11-07 11:02:07 -0800172 if (CC_UNLIKELY(!mNativeWindow.get())) {
173 info.out.canDrawThisFrame = false;
174 return;
175 }
176
John Recka5dda642014-05-22 15:43:54 -0700177 int runningBehind = 0;
178 // TODO: This query is moderately expensive, investigate adding some sort
179 // of fast-path based off when we last called eglSwapBuffers() as well as
180 // last vsync time. Or something.
181 mNativeWindow->query(mNativeWindow.get(),
182 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
183 info.out.canDrawThisFrame = !runningBehind;
184
185 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700186 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700187 // If animationsNeedsRedraw is set don't bother posting for an RT anim
188 // as we will just end up fighting the UI thread.
189 mRenderThread.postFrameCallback(this);
190 }
John Recke45b1fd2014-04-15 09:50:16 -0700191 }
192}
193
John Reckf47a5942014-06-30 16:20:04 -0700194void CanvasContext::stopDrawing() {
195 mRenderThread.removeFrameCallback(this);
196}
197
John Recka5dda642014-05-22 15:43:54 -0700198void CanvasContext::notifyFramePending() {
199 ATRACE_CALL();
200 mRenderThread.pushBackFrameCallback(this);
201}
202
John Recke4267ea2014-06-03 15:53:15 -0700203void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800204 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700205 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800206
John Reckfe5e7b72014-05-23 17:42:28 -0700207 profiler().markPlaybackStart();
208
John Recke4267ea2014-06-03 15:53:15 -0700209 SkRect dirty;
210 mDamageAccumulator.finish(&dirty);
211
John Reck4f02bf42014-01-03 18:09:17 -0800212 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700213 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800214 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
215 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700216 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700217 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700218 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700219 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700220 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700221 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
222 SK_RECT_ARGS(dirty), width, height);
223 dirty.setEmpty();
224 }
John Recke4267ea2014-06-03 15:53:15 -0700225 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800226 }
227
228 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700229 if (!dirty.isEmpty()) {
230 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
231 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800232 } else {
233 status = mCanvas->prepare(mOpaque);
234 }
235
236 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700237 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800238
John Reckfe5e7b72014-05-23 17:42:28 -0700239 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800240
241 mCanvas->finish();
242
John Reckfe5e7b72014-05-23 17:42:28 -0700243 profiler().markPlaybackEnd();
244
John Reck4f02bf42014-01-03 18:09:17 -0800245 if (status & DrawGlInfo::kStatusDrew) {
246 swapBuffers();
John Reck0e89e2b2014-10-31 14:49:06 -0700247 } else {
248 mEglManager.cancelFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800249 }
John Reckfe5e7b72014-05-23 17:42:28 -0700250
251 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800252}
253
John Recke45b1fd2014-04-15 09:50:16 -0700254// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700255void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700256 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
257 return;
258 }
259
John Recke45b1fd2014-04-15 09:50:16 -0700260 ATRACE_CALL();
261
John Reckfe5e7b72014-05-23 17:42:28 -0700262 profiler().startFrame();
263
John Reck3b202512014-06-23 13:13:08 -0700264 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700265 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700266 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700267 draw();
John Recka5dda642014-05-22 15:43:54 -0700268 }
John Recke45b1fd2014-04-15 09:50:16 -0700269}
270
John Reck3b202512014-06-23 13:13:08 -0700271void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700272 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700273 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700274 if (thread.eglManager().hasEglContext()) {
275 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700276 mode = DrawGlInfo::kModeProcess;
277 }
John Reck6f07a0d2014-04-16 21:31:25 -0700278
John Reck3b202512014-06-23 13:13:08 -0700279 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800280}
281
John Reck998a6d82014-08-28 15:35:53 -0700282void CanvasContext::markLayerInUse(RenderNode* node) {
283 if (mPrefetechedLayers.erase(node)) {
284 node->decStrong(0);
285 }
286}
287
288static void destroyPrefetechedNode(RenderNode* node) {
289 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
290 node->destroyHardwareResources();
291 node->decStrong(0);
292}
293
294void CanvasContext::freePrefetechedLayers() {
295 if (mPrefetechedLayers.size()) {
296 requireGlContext();
297 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
298 mPrefetechedLayers.clear();
299 }
300}
301
John Reck3e824952014-08-20 10:08:39 -0700302void CanvasContext::buildLayer(RenderNode* node) {
303 ATRACE_CALL();
304 if (!mEglManager.hasEglContext() || !mCanvas) {
305 return;
306 }
307 requireGlContext();
308 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
309 stopDrawing();
310
311 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700312 info.damageAccumulator = &mDamageAccumulator;
313 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700314 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700315 node->prepareTree(info);
316 SkRect ignore;
317 mDamageAccumulator.finish(&ignore);
318 // Tickle the GENERIC property on node to mark it as dirty for damaging
319 // purposes when the frame is actually drawn
320 node->setPropertyFieldsDirty(RenderNode::GENERIC);
321
John Reck443a7142014-09-04 17:40:05 -0700322 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700323 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700324
325 node->incStrong(0);
326 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700327}
328
John Reck19b6bcf2014-02-14 20:03:38 -0800329bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
330 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700331 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700332 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800333}
334
John Reckf47a5942014-06-30 16:20:04 -0700335void CanvasContext::destroyHardwareResources() {
336 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700337 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700338 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700339 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700340 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700341 Caches::getInstance().flush(Caches::kFlushMode_Layers);
342 }
343}
344
345void CanvasContext::trimMemory(RenderThread& thread, int level) {
346 // No context means nothing to free
347 if (!thread.eglManager().hasEglContext()) return;
348
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200349 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700350 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700351 if (level >= TRIM_MEMORY_COMPLETE) {
352 Caches::getInstance().flush(Caches::kFlushMode_Full);
353 thread.eglManager().destroy();
354 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
355 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700356 }
357}
358
John Reckfc53ef272014-02-11 10:40:25 -0800359void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800360 requireGlContext();
361 task->run();
362}
363
John Reck1949e792014-04-08 15:18:56 -0700364Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700365 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700366 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700367}
368
John Reck19b6bcf2014-02-14 20:03:38 -0800369void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700370 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800371}
372
John Reck3b202512014-06-23 13:13:08 -0700373void CanvasContext::setTextureAtlas(RenderThread& thread,
374 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
375 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700376}
377
John Reck23b797a2014-01-03 18:08:34 -0800378} /* namespace renderthread */
379} /* namespace uirenderer */
380} /* namespace android */