blob: d9fa0bc2aa5462b3d4199bf989a3c095c0892d7c [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 Reck4f02bf42014-01-03 18:09:17 -080019#include <private/hwui/DrawGlInfo.h>
John Reck23b797a2014-01-03 18:08:34 -080020#include <strings.h>
21
John Reck3b202512014-06-23 13:13:08 -070022#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080023#include "RenderThread.h"
John Reck119907c2014-08-14 09:02:01 -070024#include "../AnimationContext.h"
John Reck23b797a2014-01-03 18:08:34 -080025#include "../Caches.h"
John Reck19b6bcf2014-02-14 20:03:38 -080026#include "../DeferredLayerUpdater.h"
John Reck3b202512014-06-23 13:13:08 -070027#include "../RenderState.h"
John Reck19b6bcf2014-02-14 20:03:38 -080028#include "../LayerRenderer.h"
John Reck4f02bf42014-01-03 18:09:17 -080029#include "../OpenGLRenderer.h"
John Reck23b797a2014-01-03 18:08:34 -080030#include "../Stencil.h"
31
John Reckf47a5942014-06-30 16:20:04 -070032#define TRIM_MEMORY_COMPLETE 80
33#define TRIM_MEMORY_UI_HIDDEN 20
34
John Reck23b797a2014-01-03 18:08:34 -080035namespace android {
36namespace uirenderer {
37namespace renderthread {
38
John Reck119907c2014-08-14 09:02:01 -070039CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
40 RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck3b202512014-06-23 13:13:08 -070041 : mRenderThread(thread)
42 , mEglManager(thread.eglManager())
John Reck4f02bf42014-01-03 18:09:17 -080043 , mEglSurface(EGL_NO_SURFACE)
44 , mDirtyRegionsEnabled(false)
45 , mOpaque(!translucent)
John Reck3b202512014-06-23 13:13:08 -070046 , mCanvas(NULL)
John Recke45b1fd2014-04-15 09:50:16 -070047 , mHaveNewSurface(false)
48 , mRootRenderNode(rootRenderNode) {
John Reck119907c2014-08-14 09:02:01 -070049 mAnimationContext = contextFactory->createAnimationContext(mRenderThread.timeLord());
John Reck23b797a2014-01-03 18:08:34 -080050}
51
52CanvasContext::~CanvasContext() {
John Reckfae904d2014-04-14 11:01:57 -070053 destroyCanvasAndSurface();
John Recke45b1fd2014-04-15 09:50:16 -070054 mRenderThread.removeFrameCallback(this);
John Reck119907c2014-08-14 09:02:01 -070055 delete mAnimationContext;
John Reck4f02bf42014-01-03 18:09:17 -080056}
57
John Reckfae904d2014-04-14 11:01:57 -070058void CanvasContext::destroyCanvasAndSurface() {
John Reck4f02bf42014-01-03 18:09:17 -080059 if (mCanvas) {
60 delete mCanvas;
61 mCanvas = 0;
62 }
John Reck23b797a2014-01-03 18:08:34 -080063 setSurface(NULL);
64}
65
John Recka5dda642014-05-22 15:43:54 -070066void CanvasContext::setSurface(ANativeWindow* window) {
67 mNativeWindow = window;
68
John Reck23b797a2014-01-03 18:08:34 -080069 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070070 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080071 mEglSurface = EGL_NO_SURFACE;
72 }
73
74 if (window) {
John Reck3b202512014-06-23 13:13:08 -070075 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080076 }
77
78 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070079 mDirtyRegionsEnabled = mEglManager.enableDirtyRegions(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080080 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070081 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070082 } else {
83 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080084 }
John Reck23b797a2014-01-03 18:08:34 -080085}
86
John Reck4f02bf42014-01-03 18:09:17 -080087void CanvasContext::swapBuffers() {
John Reck3b202512014-06-23 13:13:08 -070088 mEglManager.swapBuffers(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080089 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -080090}
91
John Reckf7d9c1d2014-04-09 10:01:03 -070092void CanvasContext::requireSurface() {
93 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
94 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -070095 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -080096}
97
John Recka5dda642014-05-22 15:43:54 -070098bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -080099 if (mCanvas) return false;
100 setSurface(window);
John Reck3b202512014-06-23 13:13:08 -0700101 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800102 mCanvas->initProperties();
103 return true;
104}
105
John Recka5dda642014-05-22 15:43:54 -0700106void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800107 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700108}
109
John Recka5dda642014-05-22 15:43:54 -0700110void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700111 // TODO: For now we just need a fence, in the future suspend any animations
112 // and such to prevent from trying to render into this surface
John Reck4f02bf42014-01-03 18:09:17 -0800113}
114
Chris Craik058fc642014-07-23 18:19:28 -0700115void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
116 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800117 if (!mCanvas) return;
118 mCanvas->setViewport(width, height);
Chris Craik058fc642014-07-23 18:19:28 -0700119 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800120}
121
John Reck63a06672014-05-07 13:45:54 -0700122void CanvasContext::setOpaque(bool opaque) {
123 mOpaque = opaque;
124}
125
John Reck860d1552014-04-11 19:15:05 -0700126void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700127 // TODO: Figure out why this workaround is needed, see b/13913604
128 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700129 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700130}
131
John Reck68bfe0a2014-06-24 15:34:58 -0700132void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
133 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700134 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
135 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
136 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800137 }
138}
139
John Recke45b1fd2014-04-15 09:50:16 -0700140void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700141 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700142
John Recke4267ea2014-06-03 15:53:15 -0700143 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700144 info.renderer = mCanvas;
John Reck119907c2014-08-14 09:02:01 -0700145 mAnimationContext->startFrame();
John Recke45b1fd2014-04-15 09:50:16 -0700146 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700147 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700148
John Recka5dda642014-05-22 15:43:54 -0700149 int runningBehind = 0;
150 // TODO: This query is moderately expensive, investigate adding some sort
151 // of fast-path based off when we last called eglSwapBuffers() as well as
152 // last vsync time. Or something.
153 mNativeWindow->query(mNativeWindow.get(),
154 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
155 info.out.canDrawThisFrame = !runningBehind;
156
157 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700158 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700159 // If animationsNeedsRedraw is set don't bother posting for an RT anim
160 // as we will just end up fighting the UI thread.
161 mRenderThread.postFrameCallback(this);
162 }
John Recke45b1fd2014-04-15 09:50:16 -0700163 }
164}
165
John Reckf47a5942014-06-30 16:20:04 -0700166void CanvasContext::stopDrawing() {
167 mRenderThread.removeFrameCallback(this);
168}
169
John Recka5dda642014-05-22 15:43:54 -0700170void CanvasContext::notifyFramePending() {
171 ATRACE_CALL();
172 mRenderThread.pushBackFrameCallback(this);
173}
174
John Recke4267ea2014-06-03 15:53:15 -0700175void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800176 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700177 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800178
John Reckfe5e7b72014-05-23 17:42:28 -0700179 profiler().markPlaybackStart();
180
John Recke4267ea2014-06-03 15:53:15 -0700181 SkRect dirty;
182 mDamageAccumulator.finish(&dirty);
183
John Reck4f02bf42014-01-03 18:09:17 -0800184 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700185 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800186 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
187 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700188 dirty.setEmpty();
John Reck4f02bf42014-01-03 18:09:17 -0800189 } else if (!mDirtyRegionsEnabled || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700190 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700191 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700192 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700193 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
194 SK_RECT_ARGS(dirty), width, height);
195 dirty.setEmpty();
196 }
John Recke4267ea2014-06-03 15:53:15 -0700197 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800198 }
199
200 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700201 if (!dirty.isEmpty()) {
202 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
203 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800204 } else {
205 status = mCanvas->prepare(mOpaque);
206 }
207
208 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700209 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800210
John Reckfe5e7b72014-05-23 17:42:28 -0700211 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800212
213 mCanvas->finish();
214
John Reckfe5e7b72014-05-23 17:42:28 -0700215 profiler().markPlaybackEnd();
216
John Reck4f02bf42014-01-03 18:09:17 -0800217 if (status & DrawGlInfo::kStatusDrew) {
218 swapBuffers();
219 }
John Reckfe5e7b72014-05-23 17:42:28 -0700220
221 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800222}
223
John Recke45b1fd2014-04-15 09:50:16 -0700224// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700225void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700226 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
227 return;
228 }
229
John Recke45b1fd2014-04-15 09:50:16 -0700230 ATRACE_CALL();
231
John Reckfe5e7b72014-05-23 17:42:28 -0700232 profiler().startFrame();
233
John Reck3b202512014-06-23 13:13:08 -0700234 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700235 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700236 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700237 draw();
John Recka5dda642014-05-22 15:43:54 -0700238 }
John Recke45b1fd2014-04-15 09:50:16 -0700239}
240
John Reck3b202512014-06-23 13:13:08 -0700241void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700242 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700243 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700244 if (thread.eglManager().hasEglContext()) {
245 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700246 mode = DrawGlInfo::kModeProcess;
247 }
John Reck6f07a0d2014-04-16 21:31:25 -0700248
John Reck3b202512014-06-23 13:13:08 -0700249 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800250}
251
John Reck3e824952014-08-20 10:08:39 -0700252void CanvasContext::buildLayer(RenderNode* node) {
253 ATRACE_CALL();
254 if (!mEglManager.hasEglContext() || !mCanvas) {
255 return;
256 }
257 requireGlContext();
258 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
259 stopDrawing();
260
261 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700262 info.damageAccumulator = &mDamageAccumulator;
263 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700264 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700265 node->prepareTree(info);
266 SkRect ignore;
267 mDamageAccumulator.finish(&ignore);
268 // Tickle the GENERIC property on node to mark it as dirty for damaging
269 // purposes when the frame is actually drawn
270 node->setPropertyFieldsDirty(RenderNode::GENERIC);
271
272 mCanvas->flushLayerUpdates();
273}
274
John Reck19b6bcf2014-02-14 20:03:38 -0800275bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
276 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700277 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700278 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800279}
280
John Reckf47a5942014-06-30 16:20:04 -0700281void CanvasContext::destroyHardwareResources() {
282 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700283 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700284 requireGlContext();
John Reckdcba6722014-07-08 13:59:49 -0700285 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700286 Caches::getInstance().flush(Caches::kFlushMode_Layers);
287 }
288}
289
290void CanvasContext::trimMemory(RenderThread& thread, int level) {
291 // No context means nothing to free
292 if (!thread.eglManager().hasEglContext()) return;
293
John Reck3c2b7fa2014-07-07 09:16:54 -0700294 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700295 if (level >= TRIM_MEMORY_COMPLETE) {
296 Caches::getInstance().flush(Caches::kFlushMode_Full);
297 thread.eglManager().destroy();
298 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
299 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700300 }
301}
302
John Reckfc53ef272014-02-11 10:40:25 -0800303void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800304 requireGlContext();
305 task->run();
306}
307
John Reck1949e792014-04-08 15:18:56 -0700308Layer* CanvasContext::createRenderLayer(int width, int height) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700309 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700310 return LayerRenderer::createRenderLayer(mRenderThread.renderState(), width, height);
John Reck1949e792014-04-08 15:18:56 -0700311}
312
313Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700314 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700315 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700316}
317
John Reck19b6bcf2014-02-14 20:03:38 -0800318void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700319 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800320}
321
John Reck3b202512014-06-23 13:13:08 -0700322void CanvasContext::setTextureAtlas(RenderThread& thread,
323 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
324 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700325}
326
John Reck23b797a2014-01-03 18:08:34 -0800327} /* namespace renderthread */
328} /* namespace uirenderer */
329} /* namespace android */