blob: 57279b778d92b67e249374c13ba7ba3aa526b7e5 [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
17#define LOG_TAG "CanvasContext"
18
19#include "CanvasContext.h"
20
John Reck4f02bf42014-01-03 18:09:17 -080021#include <private/hwui/DrawGlInfo.h>
John Reck23b797a2014-01-03 18:08:34 -080022#include <strings.h>
23
John Reck3b202512014-06-23 13:13:08 -070024#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080025#include "RenderThread.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 Reck3b202512014-06-23 13:13:08 -070040CanvasContext::CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode)
41 : 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 Reck23b797a2014-01-03 18:08:34 -080049}
50
51CanvasContext::~CanvasContext() {
John Reckfae904d2014-04-14 11:01:57 -070052 destroyCanvasAndSurface();
John Recke45b1fd2014-04-15 09:50:16 -070053 mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -080054}
55
John Reckfae904d2014-04-14 11:01:57 -070056void CanvasContext::destroyCanvasAndSurface() {
John Reck4f02bf42014-01-03 18:09:17 -080057 if (mCanvas) {
58 delete mCanvas;
59 mCanvas = 0;
60 }
John Reck23b797a2014-01-03 18:08:34 -080061 setSurface(NULL);
62}
63
John Recka5dda642014-05-22 15:43:54 -070064void CanvasContext::setSurface(ANativeWindow* window) {
65 mNativeWindow = window;
66
John Reck23b797a2014-01-03 18:08:34 -080067 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070068 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080069 mEglSurface = EGL_NO_SURFACE;
70 }
71
72 if (window) {
John Reck3b202512014-06-23 13:13:08 -070073 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080074 }
75
76 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070077 mDirtyRegionsEnabled = mEglManager.enableDirtyRegions(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080078 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070079 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070080 } else {
81 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080082 }
John Reck23b797a2014-01-03 18:08:34 -080083}
84
John Reck4f02bf42014-01-03 18:09:17 -080085void CanvasContext::swapBuffers() {
John Reck3b202512014-06-23 13:13:08 -070086 mEglManager.swapBuffers(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080087 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -080088}
89
John Reckf7d9c1d2014-04-09 10:01:03 -070090void CanvasContext::requireSurface() {
91 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
92 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -070093 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -080094}
95
John Recka5dda642014-05-22 15:43:54 -070096bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -080097 if (mCanvas) return false;
98 setSurface(window);
John Reck3b202512014-06-23 13:13:08 -070099 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800100 mCanvas->initProperties();
101 return true;
102}
103
John Recka5dda642014-05-22 15:43:54 -0700104void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800105 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700106}
107
John Recka5dda642014-05-22 15:43:54 -0700108void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700109 // TODO: For now we just need a fence, in the future suspend any animations
110 // and such to prevent from trying to render into this surface
John Reck4f02bf42014-01-03 18:09:17 -0800111}
112
Chris Craik797b95b2014-05-20 18:10:25 -0700113void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius) {
John Reck4f02bf42014-01-03 18:09:17 -0800114 if (!mCanvas) return;
115 mCanvas->setViewport(width, height);
Chris Craik797b95b2014-05-20 18:10:25 -0700116 mCanvas->initializeLight(lightCenter, lightRadius);
John Reck4f02bf42014-01-03 18:09:17 -0800117}
118
John Reck63a06672014-05-07 13:45:54 -0700119void CanvasContext::setOpaque(bool opaque) {
120 mOpaque = opaque;
121}
122
John Reck860d1552014-04-11 19:15:05 -0700123void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700124 // TODO: Figure out why this workaround is needed, see b/13913604
125 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700126 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700127}
128
John Reck68bfe0a2014-06-24 15:34:58 -0700129void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
130 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700131 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
132 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
133 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800134 }
135}
136
John Recke45b1fd2014-04-15 09:50:16 -0700137void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700138 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700139
John Reckf9be7792014-05-02 18:21:16 -0700140 info.frameTimeMs = mRenderThread.timeLord().frameTimeMs();
John Recke4267ea2014-06-03 15:53:15 -0700141 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700142 info.renderer = mCanvas;
John Recke45b1fd2014-04-15 09:50:16 -0700143 mRootRenderNode->prepareTree(info);
144
John Recka5dda642014-05-22 15:43:54 -0700145 int runningBehind = 0;
146 // TODO: This query is moderately expensive, investigate adding some sort
147 // of fast-path based off when we last called eglSwapBuffers() as well as
148 // last vsync time. Or something.
149 mNativeWindow->query(mNativeWindow.get(),
150 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
151 info.out.canDrawThisFrame = !runningBehind;
152
153 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700154 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700155 // If animationsNeedsRedraw is set don't bother posting for an RT anim
156 // as we will just end up fighting the UI thread.
157 mRenderThread.postFrameCallback(this);
158 }
John Recke45b1fd2014-04-15 09:50:16 -0700159 }
160}
161
John Reckf47a5942014-06-30 16:20:04 -0700162void CanvasContext::stopDrawing() {
163 mRenderThread.removeFrameCallback(this);
164}
165
John Recka5dda642014-05-22 15:43:54 -0700166void CanvasContext::notifyFramePending() {
167 ATRACE_CALL();
168 mRenderThread.pushBackFrameCallback(this);
169}
170
John Recke4267ea2014-06-03 15:53:15 -0700171void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800172 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700173 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800174
John Reckfe5e7b72014-05-23 17:42:28 -0700175 profiler().markPlaybackStart();
176
John Recke4267ea2014-06-03 15:53:15 -0700177 SkRect dirty;
178 mDamageAccumulator.finish(&dirty);
179
John Reck4f02bf42014-01-03 18:09:17 -0800180 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700181 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800182 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
183 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700184 dirty.setEmpty();
John Reck4f02bf42014-01-03 18:09:17 -0800185 } else if (!mDirtyRegionsEnabled || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700186 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700187 } else {
John Recke4267ea2014-06-03 15:53:15 -0700188 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800189 }
190
191 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700192 if (!dirty.isEmpty()) {
193 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
194 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800195 } else {
196 status = mCanvas->prepare(mOpaque);
197 }
198
199 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700200 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800201
John Reckfe5e7b72014-05-23 17:42:28 -0700202 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800203
204 mCanvas->finish();
205
John Reckfe5e7b72014-05-23 17:42:28 -0700206 profiler().markPlaybackEnd();
207
John Reck4f02bf42014-01-03 18:09:17 -0800208 if (status & DrawGlInfo::kStatusDrew) {
209 swapBuffers();
210 }
John Reckfe5e7b72014-05-23 17:42:28 -0700211
212 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800213}
214
John Recke45b1fd2014-04-15 09:50:16 -0700215// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700216void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700217 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
218 return;
219 }
220
John Recke45b1fd2014-04-15 09:50:16 -0700221 ATRACE_CALL();
222
John Reckfe5e7b72014-05-23 17:42:28 -0700223 profiler().startFrame();
224
John Reck3b202512014-06-23 13:13:08 -0700225 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700226 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700227 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700228 draw();
John Recka5dda642014-05-22 15:43:54 -0700229 }
John Recke45b1fd2014-04-15 09:50:16 -0700230}
231
John Reck3b202512014-06-23 13:13:08 -0700232void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700233 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700234 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700235 if (thread.eglManager().hasEglContext()) {
236 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700237 mode = DrawGlInfo::kModeProcess;
238 }
John Reck6f07a0d2014-04-16 21:31:25 -0700239
John Reck3b202512014-06-23 13:13:08 -0700240 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800241}
242
John Reck19b6bcf2014-02-14 20:03:38 -0800243bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
244 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700245 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700246 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800247}
248
John Reckf47a5942014-06-30 16:20:04 -0700249void CanvasContext::destroyHardwareResources() {
250 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700251 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700252 requireGlContext();
John Reckdcba6722014-07-08 13:59:49 -0700253 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700254 Caches::getInstance().flush(Caches::kFlushMode_Layers);
255 }
256}
257
258void CanvasContext::trimMemory(RenderThread& thread, int level) {
259 // No context means nothing to free
260 if (!thread.eglManager().hasEglContext()) return;
261
John Reck3c2b7fa2014-07-07 09:16:54 -0700262 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700263 if (level >= TRIM_MEMORY_COMPLETE) {
264 Caches::getInstance().flush(Caches::kFlushMode_Full);
265 thread.eglManager().destroy();
266 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
267 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700268 }
269}
270
John Reckfc53ef272014-02-11 10:40:25 -0800271void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800272 requireGlContext();
273 task->run();
274}
275
John Reck1949e792014-04-08 15:18:56 -0700276Layer* CanvasContext::createRenderLayer(int width, int height) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700277 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700278 return LayerRenderer::createRenderLayer(mRenderThread.renderState(), width, height);
John Reck1949e792014-04-08 15:18:56 -0700279}
280
281Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700282 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700283 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700284}
285
John Reck19b6bcf2014-02-14 20:03:38 -0800286void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700287 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800288}
289
John Reck3b202512014-06-23 13:13:08 -0700290void CanvasContext::setTextureAtlas(RenderThread& thread,
291 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
292 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700293}
294
John Reck23b797a2014-01-03 18:08:34 -0800295} /* namespace renderthread */
296} /* namespace uirenderer */
297} /* namespace android */