blob: 4bf5a8ac764d587a81a19cf54429f77a5cf7181e [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 Reck23b797a2014-01-03 18:08:34 -080024#include "../Caches.h"
John Reck19b6bcf2014-02-14 20:03:38 -080025#include "../DeferredLayerUpdater.h"
John Reck3b202512014-06-23 13:13:08 -070026#include "../RenderState.h"
John Reck19b6bcf2014-02-14 20:03:38 -080027#include "../LayerRenderer.h"
John Reck4f02bf42014-01-03 18:09:17 -080028#include "../OpenGLRenderer.h"
John Reck23b797a2014-01-03 18:08:34 -080029#include "../Stencil.h"
30
John Reckf47a5942014-06-30 16:20:04 -070031#define TRIM_MEMORY_COMPLETE 80
32#define TRIM_MEMORY_UI_HIDDEN 20
33
John Reck23b797a2014-01-03 18:08:34 -080034namespace android {
35namespace uirenderer {
36namespace renderthread {
37
John Reck3b202512014-06-23 13:13:08 -070038CanvasContext::CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode)
39 : mRenderThread(thread)
40 , mEglManager(thread.eglManager())
John Reck4f02bf42014-01-03 18:09:17 -080041 , mEglSurface(EGL_NO_SURFACE)
42 , mDirtyRegionsEnabled(false)
43 , mOpaque(!translucent)
John Reck3b202512014-06-23 13:13:08 -070044 , mCanvas(NULL)
John Recke45b1fd2014-04-15 09:50:16 -070045 , mHaveNewSurface(false)
46 , mRootRenderNode(rootRenderNode) {
John Reck23b797a2014-01-03 18:08:34 -080047}
48
49CanvasContext::~CanvasContext() {
John Reckfae904d2014-04-14 11:01:57 -070050 destroyCanvasAndSurface();
John Recke45b1fd2014-04-15 09:50:16 -070051 mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -080052}
53
John Reckfae904d2014-04-14 11:01:57 -070054void CanvasContext::destroyCanvasAndSurface() {
John Reck4f02bf42014-01-03 18:09:17 -080055 if (mCanvas) {
56 delete mCanvas;
57 mCanvas = 0;
58 }
John Reck23b797a2014-01-03 18:08:34 -080059 setSurface(NULL);
60}
61
John Recka5dda642014-05-22 15:43:54 -070062void CanvasContext::setSurface(ANativeWindow* window) {
63 mNativeWindow = window;
64
John Reck23b797a2014-01-03 18:08:34 -080065 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070066 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080067 mEglSurface = EGL_NO_SURFACE;
68 }
69
70 if (window) {
John Reck3b202512014-06-23 13:13:08 -070071 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080072 }
73
74 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070075 mDirtyRegionsEnabled = mEglManager.enableDirtyRegions(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080076 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070077 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070078 } else {
79 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080080 }
John Reck23b797a2014-01-03 18:08:34 -080081}
82
John Reck4f02bf42014-01-03 18:09:17 -080083void CanvasContext::swapBuffers() {
John Reck3b202512014-06-23 13:13:08 -070084 mEglManager.swapBuffers(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080085 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -080086}
87
John Reckf7d9c1d2014-04-09 10:01:03 -070088void CanvasContext::requireSurface() {
89 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
90 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -070091 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -080092}
93
John Recka5dda642014-05-22 15:43:54 -070094bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -080095 if (mCanvas) return false;
96 setSurface(window);
John Reck3b202512014-06-23 13:13:08 -070097 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -080098 mCanvas->initProperties();
99 return true;
100}
101
John Recka5dda642014-05-22 15:43:54 -0700102void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800103 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700104}
105
John Recka5dda642014-05-22 15:43:54 -0700106void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700107 // TODO: For now we just need a fence, in the future suspend any animations
108 // and such to prevent from trying to render into this surface
John Reck4f02bf42014-01-03 18:09:17 -0800109}
110
Chris Craik058fc642014-07-23 18:19:28 -0700111void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
112 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800113 if (!mCanvas) return;
114 mCanvas->setViewport(width, height);
Chris Craik058fc642014-07-23 18:19:28 -0700115 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800116}
117
John Reck63a06672014-05-07 13:45:54 -0700118void CanvasContext::setOpaque(bool opaque) {
119 mOpaque = opaque;
120}
121
John Reck860d1552014-04-11 19:15:05 -0700122void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700123 // TODO: Figure out why this workaround is needed, see b/13913604
124 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700125 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700126}
127
John Reck68bfe0a2014-06-24 15:34:58 -0700128void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
129 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700130 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
131 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
132 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800133 }
134}
135
John Recke45b1fd2014-04-15 09:50:16 -0700136void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700137 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700138
John Reckf9be7792014-05-02 18:21:16 -0700139 info.frameTimeMs = mRenderThread.timeLord().frameTimeMs();
John Recke4267ea2014-06-03 15:53:15 -0700140 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700141 info.renderer = mCanvas;
John Recke45b1fd2014-04-15 09:50:16 -0700142 mRootRenderNode->prepareTree(info);
143
John Recka5dda642014-05-22 15:43:54 -0700144 int runningBehind = 0;
145 // TODO: This query is moderately expensive, investigate adding some sort
146 // of fast-path based off when we last called eglSwapBuffers() as well as
147 // last vsync time. Or something.
148 mNativeWindow->query(mNativeWindow.get(),
149 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
150 info.out.canDrawThisFrame = !runningBehind;
151
152 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700153 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700154 // If animationsNeedsRedraw is set don't bother posting for an RT anim
155 // as we will just end up fighting the UI thread.
156 mRenderThread.postFrameCallback(this);
157 }
John Recke45b1fd2014-04-15 09:50:16 -0700158 }
159}
160
John Reckf47a5942014-06-30 16:20:04 -0700161void CanvasContext::stopDrawing() {
162 mRenderThread.removeFrameCallback(this);
163}
164
John Recka5dda642014-05-22 15:43:54 -0700165void CanvasContext::notifyFramePending() {
166 ATRACE_CALL();
167 mRenderThread.pushBackFrameCallback(this);
168}
169
John Recke4267ea2014-06-03 15:53:15 -0700170void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800171 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700172 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800173
John Reckfe5e7b72014-05-23 17:42:28 -0700174 profiler().markPlaybackStart();
175
John Recke4267ea2014-06-03 15:53:15 -0700176 SkRect dirty;
177 mDamageAccumulator.finish(&dirty);
178
John Reck4f02bf42014-01-03 18:09:17 -0800179 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700180 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800181 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
182 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700183 dirty.setEmpty();
John Reck4f02bf42014-01-03 18:09:17 -0800184 } else if (!mDirtyRegionsEnabled || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700185 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700186 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700187 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700188 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
189 SK_RECT_ARGS(dirty), width, height);
190 dirty.setEmpty();
191 }
John Recke4267ea2014-06-03 15:53:15 -0700192 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800193 }
194
195 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700196 if (!dirty.isEmpty()) {
197 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
198 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800199 } else {
200 status = mCanvas->prepare(mOpaque);
201 }
202
203 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700204 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800205
John Reckfe5e7b72014-05-23 17:42:28 -0700206 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800207
208 mCanvas->finish();
209
John Reckfe5e7b72014-05-23 17:42:28 -0700210 profiler().markPlaybackEnd();
211
John Reck4f02bf42014-01-03 18:09:17 -0800212 if (status & DrawGlInfo::kStatusDrew) {
213 swapBuffers();
214 }
John Reckfe5e7b72014-05-23 17:42:28 -0700215
216 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800217}
218
John Recke45b1fd2014-04-15 09:50:16 -0700219// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700220void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700221 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
222 return;
223 }
224
John Recke45b1fd2014-04-15 09:50:16 -0700225 ATRACE_CALL();
226
John Reckfe5e7b72014-05-23 17:42:28 -0700227 profiler().startFrame();
228
John Reck3b202512014-06-23 13:13:08 -0700229 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700230 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700231 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700232 draw();
John Recka5dda642014-05-22 15:43:54 -0700233 }
John Recke45b1fd2014-04-15 09:50:16 -0700234}
235
John Reck3b202512014-06-23 13:13:08 -0700236void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700237 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700238 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700239 if (thread.eglManager().hasEglContext()) {
240 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700241 mode = DrawGlInfo::kModeProcess;
242 }
John Reck6f07a0d2014-04-16 21:31:25 -0700243
John Reck3b202512014-06-23 13:13:08 -0700244 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800245}
246
John Reck3e824952014-08-20 10:08:39 -0700247void CanvasContext::buildLayer(RenderNode* node) {
248 ATRACE_CALL();
249 if (!mEglManager.hasEglContext() || !mCanvas) {
250 return;
251 }
252 requireGlContext();
253 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
254 stopDrawing();
255
256 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
257 info.frameTimeMs = mRenderThread.timeLord().frameTimeMs();
258 info.damageAccumulator = &mDamageAccumulator;
259 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700260 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700261 node->prepareTree(info);
262 SkRect ignore;
263 mDamageAccumulator.finish(&ignore);
264 // Tickle the GENERIC property on node to mark it as dirty for damaging
265 // purposes when the frame is actually drawn
266 node->setPropertyFieldsDirty(RenderNode::GENERIC);
267
268 mCanvas->flushLayerUpdates();
269}
270
John Reck19b6bcf2014-02-14 20:03:38 -0800271bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
272 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700273 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700274 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800275}
276
John Reckf47a5942014-06-30 16:20:04 -0700277void CanvasContext::destroyHardwareResources() {
278 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700279 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700280 requireGlContext();
John Reckdcba6722014-07-08 13:59:49 -0700281 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700282 Caches::getInstance().flush(Caches::kFlushMode_Layers);
283 }
284}
285
286void CanvasContext::trimMemory(RenderThread& thread, int level) {
287 // No context means nothing to free
288 if (!thread.eglManager().hasEglContext()) return;
289
John Reck3c2b7fa2014-07-07 09:16:54 -0700290 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700291 if (level >= TRIM_MEMORY_COMPLETE) {
292 Caches::getInstance().flush(Caches::kFlushMode_Full);
293 thread.eglManager().destroy();
294 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
295 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700296 }
297}
298
John Reckfc53ef272014-02-11 10:40:25 -0800299void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800300 requireGlContext();
301 task->run();
302}
303
John Reck1949e792014-04-08 15:18:56 -0700304Layer* CanvasContext::createRenderLayer(int width, int height) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700305 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700306 return LayerRenderer::createRenderLayer(mRenderThread.renderState(), width, height);
John Reck1949e792014-04-08 15:18:56 -0700307}
308
309Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700310 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700311 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700312}
313
John Reck19b6bcf2014-02-14 20:03:38 -0800314void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700315 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800316}
317
John Reck3b202512014-06-23 13:13:08 -0700318void CanvasContext::setTextureAtlas(RenderThread& thread,
319 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
320 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700321}
322
John Reck23b797a2014-01-03 18:08:34 -0800323} /* namespace renderthread */
324} /* namespace uirenderer */
325} /* namespace android */