blob: b74b5088c14fbda41dc34c05116da21bbb98f19a [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 Reckd04794a2015-05-08 10:04:36 -070019#include "AnimationContext.h"
20#include "Caches.h"
21#include "DeferredLayerUpdater.h"
John Reck3b202512014-06-23 13:13:08 -070022#include "EglManager.h"
John Reckd04794a2015-05-08 10:04:36 -070023#include "LayerRenderer.h"
24#include "OpenGLRenderer.h"
25#include "Properties.h"
John Reck4f02bf42014-01-03 18:09:17 -080026#include "RenderThread.h"
John Reckd04794a2015-05-08 10:04:36 -070027#include "renderstate/RenderState.h"
28#include "renderstate/Stencil.h"
John Recke248bd12015-08-05 13:53:53 -070029#include "protos/hwui.pb.h"
30
31#include <cutils/properties.h>
32#include <google/protobuf/io/zero_copy_stream_impl.h>
33#include <private/hwui/DrawGlInfo.h>
34#include <strings.h>
John Reck23b797a2014-01-03 18:08:34 -080035
Chris Craik65fe5ee2015-01-26 18:06:29 -080036#include <algorithm>
John Recke248bd12015-08-05 13:53:53 -070037#include <fcntl.h>
38#include <sys/stat.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080039
John Reckf47a5942014-06-30 16:20:04 -070040#define TRIM_MEMORY_COMPLETE 80
41#define TRIM_MEMORY_UI_HIDDEN 20
42
John Recke248bd12015-08-05 13:53:53 -070043#define ENABLE_RENDERNODE_SERIALIZATION false
44
John Reck149173d2015-08-10 09:52:29 -070045#define LOG_FRAMETIME_MMA 0
46
47#if LOG_FRAMETIME_MMA
48static float sBenchMma = 0;
49static int sFrameCount = 0;
50static const float NANOS_PER_MILLIS_F = 1000000.0f;
51#endif
52
John Reck23b797a2014-01-03 18:08:34 -080053namespace android {
54namespace uirenderer {
55namespace renderthread {
56
John Reck119907c2014-08-14 09:02:01 -070057CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
58 RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck3b202512014-06-23 13:13:08 -070059 : mRenderThread(thread)
60 , mEglManager(thread.eglManager())
John Reck4f02bf42014-01-03 18:09:17 -080061 , mOpaque(!translucent)
Chris Craik51d6a3d2014-12-22 17:16:56 -080062 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
John Reckba6adf62015-02-19 14:36:50 -080063 , mRootRenderNode(rootRenderNode)
John Reck4c9e59d2015-05-12 07:17:50 -070064 , mJankTracker(thread.timeLord().frameIntervalNanos())
65 , mProfiler(mFrames) {
John Reck443a7142014-09-04 17:40:05 -070066 mRenderThread.renderState().registerCanvasContext(this);
John Reckb36016c2015-03-11 08:50:53 -070067 mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
John Reck23b797a2014-01-03 18:08:34 -080068}
69
70CanvasContext::~CanvasContext() {
John Reck17035b02014-09-03 07:39:53 -070071 destroy();
John Reck443a7142014-09-04 17:40:05 -070072 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -080073}
74
John Reck17035b02014-09-03 07:39:53 -070075void CanvasContext::destroy() {
76 stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -080077 setSurface(nullptr);
John Reck17035b02014-09-03 07:39:53 -070078 freePrefetechedLayers();
79 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070080 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080081 if (mCanvas) {
82 delete mCanvas;
Chris Craikd41c4d82015-01-05 15:51:13 -080083 mCanvas = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080084 }
John Reck23b797a2014-01-03 18:08:34 -080085}
86
John Recka5dda642014-05-22 15:43:54 -070087void CanvasContext::setSurface(ANativeWindow* window) {
John Reckfbc8df02014-11-14 16:18:41 -080088 ATRACE_CALL();
89
John Recka5dda642014-05-22 15:43:54 -070090 mNativeWindow = window;
91
John Reck23b797a2014-01-03 18:08:34 -080092 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070093 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080094 mEglSurface = EGL_NO_SURFACE;
95 }
96
97 if (window) {
John Reck3b202512014-06-23 13:13:08 -070098 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080099 }
100
101 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -0700102 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
103 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -0800104 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -0700105 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -0700106 } else {
107 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -0800108 }
John Reck23b797a2014-01-03 18:08:34 -0800109}
110
John Reckf7d9c1d2014-04-09 10:01:03 -0700111void CanvasContext::requireSurface() {
112 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
113 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700114 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800115}
116
John Reck1125d1f2014-10-23 11:02:19 -0700117void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
118 mSwapBehavior = swapBehavior;
119}
120
John Recka5dda642014-05-22 15:43:54 -0700121bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800122 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700123 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700124 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800125 mCanvas->initProperties();
126 return true;
127}
128
John Recka5dda642014-05-22 15:43:54 -0700129void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800130 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700131}
132
John Reck9fb42f02014-12-04 13:51:41 -0800133bool CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck01a5ea32014-12-03 13:01:07 -0800134 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800135}
136
Chris Craik284b2432014-09-18 16:05:35 -0700137// TODO: don't pass viewport size, it's automatic via EGL
Alan Viverette50210d92015-05-14 18:05:36 -0700138void CanvasContext::setup(int width, int height, float lightRadius,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000139 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800140 if (!mCanvas) return;
Alan Viverette50210d92015-05-14 18:05:36 -0700141 mCanvas->initLight(lightRadius, ambientShadowAlpha, spotShadowAlpha);
142}
143
144void CanvasContext::setLightCenter(const Vector3& lightCenter) {
145 if (!mCanvas) return;
146 mCanvas->setLightCenter(lightCenter);
John Reck4f02bf42014-01-03 18:09:17 -0800147}
148
John Reck63a06672014-05-07 13:45:54 -0700149void CanvasContext::setOpaque(bool opaque) {
150 mOpaque = opaque;
151}
152
John Reck860d1552014-04-11 19:15:05 -0700153void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700154 // TODO: Figure out why this workaround is needed, see b/13913604
155 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reckf2dcc2a2015-07-16 09:17:59 -0700156 EGLint error = 0;
157 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface, &error);
158 if (error) {
159 setSurface(nullptr);
160 }
John Reck860d1552014-04-11 19:15:05 -0700161}
162
John Reck68bfe0a2014-06-24 15:34:58 -0700163void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
164 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700165 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
166 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
167 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800168 }
169}
170
John Reckbf3c6022015-06-02 15:55:00 -0700171static bool wasSkipped(FrameInfo* info) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700172 return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
John Reckbf3c6022015-06-02 15:55:00 -0700173}
174
John Reckbe3fba02015-07-06 13:49:58 -0700175void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued) {
John Reckf9be7792014-05-02 18:21:16 -0700176 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700177
John Reckbf3c6022015-06-02 15:55:00 -0700178 // If the previous frame was dropped we don't need to hold onto it, so
179 // just keep using the previous frame's structure instead
180 if (!wasSkipped(mCurrentFrameInfo)) {
181 mCurrentFrameInfo = &mFrames.next();
182 }
John Reckba6adf62015-02-19 14:36:50 -0800183 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
John Reckbe3fba02015-07-06 13:49:58 -0700184 mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
John Reckba6adf62015-02-19 14:36:50 -0800185 mCurrentFrameInfo->markSyncStart();
186
John Recke4267ea2014-06-03 15:53:15 -0700187 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700188 info.renderer = mCanvas;
John Reck00e79c92015-07-21 10:23:59 -0700189 info.canvasContext = this;
190
John Reckec845a22014-09-05 15:23:38 -0700191 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700192 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700193 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700194
John Reck00e79c92015-07-21 10:23:59 -0700195 freePrefetechedLayers();
John Reck998a6d82014-08-28 15:35:53 -0700196
John Reckaa95a882014-11-07 11:02:07 -0800197 if (CC_UNLIKELY(!mNativeWindow.get())) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700198 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaa95a882014-11-07 11:02:07 -0800199 info.out.canDrawThisFrame = false;
200 return;
201 }
202
John Recka5dda642014-05-22 15:43:54 -0700203 int runningBehind = 0;
204 // TODO: This query is moderately expensive, investigate adding some sort
205 // of fast-path based off when we last called eglSwapBuffers() as well as
206 // last vsync time. Or something.
207 mNativeWindow->query(mNativeWindow.get(),
208 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
209 info.out.canDrawThisFrame = !runningBehind;
210
John Reckaef9dc82015-05-08 14:10:57 -0700211 if (!info.out.canDrawThisFrame) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700212 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaef9dc82015-05-08 14:10:57 -0700213 }
214
John Recka5dda642014-05-22 15:43:54 -0700215 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700216 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700217 // If animationsNeedsRedraw is set don't bother posting for an RT anim
218 // as we will just end up fighting the UI thread.
219 mRenderThread.postFrameCallback(this);
220 }
John Recke45b1fd2014-04-15 09:50:16 -0700221 }
222}
223
John Reckf47a5942014-06-30 16:20:04 -0700224void CanvasContext::stopDrawing() {
225 mRenderThread.removeFrameCallback(this);
226}
227
John Recka5dda642014-05-22 15:43:54 -0700228void CanvasContext::notifyFramePending() {
229 ATRACE_CALL();
230 mRenderThread.pushBackFrameCallback(this);
231}
232
John Recke4267ea2014-06-03 15:53:15 -0700233void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800234 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700235 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800236
John Recke4267ea2014-06-03 15:53:15 -0700237 SkRect dirty;
238 mDamageAccumulator.finish(&dirty);
239
John Reck6d4d0db2015-08-03 15:34:52 -0700240 // TODO: Re-enable after figuring out cause of b/22592975
241// if (dirty.isEmpty() && Properties::skipEmptyFrames) {
242// mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
243// return;
244// }
John Reck240ff622015-04-28 13:50:00 -0700245
John Reck240ff622015-04-28 13:50:00 -0700246 mCurrentFrameInfo->markIssueDrawCommandsStart();
247
John Reck149173d2015-08-10 09:52:29 -0700248 Frame frame = mEglManager.beginFrame(mEglSurface);
249 if (frame.width() != mCanvas->getViewportWidth()
250 || frame.height() != mCanvas->getViewportHeight()) {
Chris Craik64e445b2015-09-02 14:23:49 -0700251 // can't rely on prior content of window if viewport size changes
John Recke4267ea2014-06-03 15:53:15 -0700252 dirty.setEmpty();
John Reck149173d2015-08-10 09:52:29 -0700253 } else if (mHaveNewSurface || frame.bufferAge() == 0) {
254 // New surface needs a full draw
John Recke4267ea2014-06-03 15:53:15 -0700255 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700256 } else {
John Reck149173d2015-08-10 09:52:29 -0700257 if (!dirty.isEmpty() && !dirty.intersect(0, 0, frame.width(), frame.height())) {
John Reck0a973302014-07-16 13:29:45 -0700258 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
John Reck149173d2015-08-10 09:52:29 -0700259 SK_RECT_ARGS(dirty), frame.width(), frame.height());
John Reck0a973302014-07-16 13:29:45 -0700260 dirty.setEmpty();
261 }
John Recke4267ea2014-06-03 15:53:15 -0700262 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800263 }
264
John Reck149173d2015-08-10 09:52:29 -0700265 if (dirty.isEmpty()) {
266 dirty.set(0, 0, frame.width(), frame.height());
Season Li13d1b4a2015-07-29 17:16:19 -0700267 }
268
John Reck149173d2015-08-10 09:52:29 -0700269 // At this point dirty is the area of the screen to update. However,
270 // the area of the frame we need to repaint is potentially different, so
271 // stash the screen area for later
272 SkRect screenDirty(dirty);
273
274 // If the buffer age is 0 we do a full-screen repaint (handled above)
275 // If the buffer age is 1 the buffer contents are the same as they were
276 // last frame so there's nothing to union() against
277 // Therefore we only care about the > 1 case.
278 if (frame.bufferAge() > 1) {
279 if (frame.bufferAge() > (int) mDamageHistory.size()) {
280 // We don't have enough history to handle this old of a buffer
281 // Just do a full-draw
282 dirty.set(0, 0, frame.width(), frame.height());
283 } else {
284 // At this point we haven't yet added the latest frame
285 // to the damage history (happens below)
286 // So we need to damage
287 for (int i = mDamageHistory.size() - 1;
288 i > ((int) mDamageHistory.size()) - frame.bufferAge(); i--) {
289 dirty.join(mDamageHistory[i]);
290 }
291 }
John Reck4f02bf42014-01-03 18:09:17 -0800292 }
293
John Reck149173d2015-08-10 09:52:29 -0700294 // Add the screen damage to the ring buffer.
295 mDamageHistory.next() = screenDirty;
296
297 mEglManager.damageFrame(frame, dirty);
Chris Craik64e445b2015-09-02 14:23:49 -0700298 mCanvas->prepareDirty(frame.width(), frame.height(),
299 dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom, mOpaque);
John Reck149173d2015-08-10 09:52:29 -0700300
John Reck4f02bf42014-01-03 18:09:17 -0800301 Rect outBounds;
Tom Hudson107843d2014-09-08 11:26:26 -0400302 mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800303
John Reckfe5e7b72014-05-23 17:42:28 -0700304 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800305
Tom Hudson107843d2014-09-08 11:26:26 -0400306 bool drew = mCanvas->finish();
John Reck4f02bf42014-01-03 18:09:17 -0800307
John Reckba6adf62015-02-19 14:36:50 -0800308 // Even if we decided to cancel the frame, from the perspective of jank
309 // metrics the frame was swapped at this point
310 mCurrentFrameInfo->markSwapBuffers();
311
Tom Hudson107843d2014-09-08 11:26:26 -0400312 if (drew) {
John Reck149173d2015-08-10 09:52:29 -0700313 if (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty))) {
314 setSurface(nullptr);
315 }
316 mHaveNewSurface = false;
John Reck4f02bf42014-01-03 18:09:17 -0800317 }
John Reckfe5e7b72014-05-23 17:42:28 -0700318
John Reckba6adf62015-02-19 14:36:50 -0800319 // TODO: Use a fence for real completion?
320 mCurrentFrameInfo->markFrameCompleted();
John Reck149173d2015-08-10 09:52:29 -0700321
322#if LOG_FRAMETIME_MMA
323 float thisFrame = mCurrentFrameInfo->duration(
324 FrameInfoIndex::IssueDrawCommandsStart,
325 FrameInfoIndex::FrameCompleted) / NANOS_PER_MILLIS_F;
326 if (sFrameCount) {
327 sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
328 } else {
329 sBenchMma = thisFrame;
330 }
331 if (++sFrameCount == 10) {
332 sFrameCount = 1;
333 ALOGD("Average frame time: %.4f", sBenchMma);
334 }
335#endif
336
John Reckedc524c2015-03-18 15:24:33 -0700337 mJankTracker.addFrame(*mCurrentFrameInfo);
John Reckba6adf62015-02-19 14:36:50 -0800338 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
John Reck4f02bf42014-01-03 18:09:17 -0800339}
340
John Recke45b1fd2014-04-15 09:50:16 -0700341// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700342void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700343 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
344 return;
345 }
346
John Recke45b1fd2014-04-15 09:50:16 -0700347 ATRACE_CALL();
348
John Reckba6adf62015-02-19 14:36:50 -0800349 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
350 UiFrameInfoBuilder(frameInfo)
Chris Craik1b54fb22015-06-02 17:40:58 -0700351 .addFlag(FrameInfoFlags::RTAnimation)
John Reckba6adf62015-02-19 14:36:50 -0800352 .setVsync(mRenderThread.timeLord().computeFrameTimeNanos(),
353 mRenderThread.timeLord().latestVsync());
John Reckfe5e7b72014-05-23 17:42:28 -0700354
John Reck3b202512014-06-23 13:13:08 -0700355 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Reckbe3fba02015-07-06 13:49:58 -0700356 prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC));
John Recka5dda642014-05-22 15:43:54 -0700357 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700358 draw();
John Recka5dda642014-05-22 15:43:54 -0700359 }
John Recke45b1fd2014-04-15 09:50:16 -0700360}
361
John Reck3b202512014-06-23 13:13:08 -0700362void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700363 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700364 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700365 if (thread.eglManager().hasEglContext()) {
John Reck0d1f6342014-03-28 20:30:27 -0700366 mode = DrawGlInfo::kModeProcess;
367 }
John Reck6f07a0d2014-04-16 21:31:25 -0700368
Chris Craikd41c4d82015-01-05 15:51:13 -0800369 thread.renderState().invokeFunctor(functor, mode, nullptr);
John Reck23b797a2014-01-03 18:08:34 -0800370}
371
John Reck998a6d82014-08-28 15:35:53 -0700372void CanvasContext::markLayerInUse(RenderNode* node) {
373 if (mPrefetechedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800374 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700375 }
376}
377
378static void destroyPrefetechedNode(RenderNode* node) {
379 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
380 node->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800381 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700382}
383
384void CanvasContext::freePrefetechedLayers() {
385 if (mPrefetechedLayers.size()) {
John Reck998a6d82014-08-28 15:35:53 -0700386 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
387 mPrefetechedLayers.clear();
388 }
389}
390
John Reck3e824952014-08-20 10:08:39 -0700391void CanvasContext::buildLayer(RenderNode* node) {
392 ATRACE_CALL();
393 if (!mEglManager.hasEglContext() || !mCanvas) {
394 return;
395 }
John Reck3e824952014-08-20 10:08:39 -0700396 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
397 stopDrawing();
398
399 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700400 info.damageAccumulator = &mDamageAccumulator;
401 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700402 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700403 node->prepareTree(info);
404 SkRect ignore;
405 mDamageAccumulator.finish(&ignore);
406 // Tickle the GENERIC property on node to mark it as dirty for damaging
407 // purposes when the frame is actually drawn
408 node->setPropertyFieldsDirty(RenderNode::GENERIC);
409
John Reck443a7142014-09-04 17:40:05 -0700410 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700411 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700412
Chris Craikd41c4d82015-01-05 15:51:13 -0800413 node->incStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700414 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700415}
416
John Reck19b6bcf2014-02-14 20:03:38 -0800417bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
John Reck68bfe0a2014-06-24 15:34:58 -0700418 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700419 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800420}
421
John Reckf47a5942014-06-30 16:20:04 -0700422void CanvasContext::destroyHardwareResources() {
423 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700424 if (mEglManager.hasEglContext()) {
John Reckdff99572014-08-29 09:59:43 -0700425 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700426 mRootRenderNode->destroyHardwareResources();
John Reck00e79c92015-07-21 10:23:59 -0700427 Caches& caches = Caches::getInstance();
428 // Make sure to release all the textures we were owning as there won't
429 // be another draw
430 caches.textureCache.resetMarkInUse(this);
Chris Craikb9ce116d2015-08-20 15:14:06 -0700431 caches.flush(Caches::FlushMode::Layers);
John Reckf47a5942014-06-30 16:20:04 -0700432 }
433}
434
435void CanvasContext::trimMemory(RenderThread& thread, int level) {
436 // No context means nothing to free
437 if (!thread.eglManager().hasEglContext()) return;
438
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200439 ATRACE_CALL();
John Reckf47a5942014-06-30 16:20:04 -0700440 if (level >= TRIM_MEMORY_COMPLETE) {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700441 Caches::getInstance().flush(Caches::FlushMode::Full);
John Reckf47a5942014-06-30 16:20:04 -0700442 thread.eglManager().destroy();
443 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700444 Caches::getInstance().flush(Caches::FlushMode::Moderate);
John Recke1628b72014-05-23 15:11:19 -0700445 }
446}
447
John Reckfc53ef272014-02-11 10:40:25 -0800448void CanvasContext::runWithGlContext(RenderTask* task) {
John Reckd7db4d72015-05-20 07:18:55 -0700449 LOG_ALWAYS_FATAL_IF(!mEglManager.hasEglContext(),
450 "GL context not initialized!");
John Reck19b6bcf2014-02-14 20:03:38 -0800451 task->run();
452}
453
John Reck1949e792014-04-08 15:18:56 -0700454Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700455 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700456 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700457}
458
John Reck3b202512014-06-23 13:13:08 -0700459void CanvasContext::setTextureAtlas(RenderThread& thread,
460 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
461 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700462}
463
John Reckba6adf62015-02-19 14:36:50 -0800464void CanvasContext::dumpFrames(int fd) {
465 FILE* file = fdopen(fd, "a");
John Reck4db3d172015-06-02 15:58:43 -0700466 fprintf(file, "\n\n---PROFILEDATA---\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700467 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck2a8bb052015-06-03 09:52:01 -0700468 fprintf(file, "%s", FrameInfoNames[i].c_str());
John Reck4db3d172015-06-02 15:58:43 -0700469 fprintf(file, ",");
470 }
John Reckba6adf62015-02-19 14:36:50 -0800471 for (size_t i = 0; i < mFrames.size(); i++) {
472 FrameInfo& frame = mFrames[i];
Chris Craik1b54fb22015-06-02 17:40:58 -0700473 if (frame[FrameInfoIndex::SyncStart] == 0) {
John Reckba6adf62015-02-19 14:36:50 -0800474 continue;
475 }
476 fprintf(file, "\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700477 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800478 fprintf(file, "%" PRId64 ",", frame[i]);
479 }
480 }
481 fprintf(file, "\n---PROFILEDATA---\n\n");
482 fflush(file);
483}
484
485void CanvasContext::resetFrameStats() {
486 mFrames.clear();
487 mRenderThread.jankTracker().reset();
488}
489
John Recke248bd12015-08-05 13:53:53 -0700490void CanvasContext::serializeDisplayListTree() {
491#if ENABLE_RENDERNODE_SERIALIZATION
492 using namespace google::protobuf::io;
493 char package[128];
494 // Check whether tracing is enabled for this process.
495 FILE * file = fopen("/proc/self/cmdline", "r");
496 if (file) {
497 if (!fgets(package, 128, file)) {
498 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
499 fclose(file);
500 return;
501 }
502 fclose(file);
503 } else {
504 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
505 errno);
506 return;
507 }
508 char path[1024];
509 snprintf(path, 1024, "/data/data/%s/cache/rendertree_dump", package);
510 int fd = open(path, O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH);
511 if (fd == -1) {
512 ALOGD("Failed to open '%s'", path);
513 return;
514 }
515 proto::RenderNode tree;
516 // TODO: Streaming writes?
517 mRootRenderNode->copyTo(&tree);
518 std::string data = tree.SerializeAsString();
519 write(fd, data.c_str(), data.length());
520 close(fd);
521#endif
522}
523
John Reck23b797a2014-01-03 18:08:34 -0800524} /* namespace renderthread */
525} /* namespace uirenderer */
526} /* namespace android */