blob: 9aa0dea18b6a9bff7ccfc4543eaaa6390849dc13 [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 Reck38e0c322015-11-10 12:19:17 -080017#include <GpuMemoryTracker.h>
John Reck23b797a2014-01-03 18:08:34 -080018#include "CanvasContext.h"
19
John Reckd04794a2015-05-08 10:04:36 -070020#include "AnimationContext.h"
21#include "Caches.h"
22#include "DeferredLayerUpdater.h"
John Reck3b202512014-06-23 13:13:08 -070023#include "EglManager.h"
Chris Craik0b7e8242015-10-28 16:50:44 -070024#include "LayerUpdateQueue.h"
John Reckd04794a2015-05-08 10:04:36 -070025#include "LayerRenderer.h"
26#include "OpenGLRenderer.h"
27#include "Properties.h"
Chris Craik764045d2016-07-06 17:14:05 -070028#include "Readback.h"
John Reck4f02bf42014-01-03 18:09:17 -080029#include "RenderThread.h"
sergeyvdccca442016-03-21 15:38:21 -070030#include "hwui/Canvas.h"
John Reckd04794a2015-05-08 10:04:36 -070031#include "renderstate/RenderState.h"
32#include "renderstate/Stencil.h"
John Recke248bd12015-08-05 13:53:53 -070033#include "protos/hwui.pb.h"
John Reck9372ac32016-01-19 11:46:52 -080034#include "utils/GLUtils.h"
John Recke486d932015-10-28 09:21:19 -070035#include "utils/TimeUtils.h"
John Recke248bd12015-08-05 13:53:53 -070036
37#include <cutils/properties.h>
38#include <google/protobuf/io/zero_copy_stream_impl.h>
39#include <private/hwui/DrawGlInfo.h>
40#include <strings.h>
John Reck23b797a2014-01-03 18:08:34 -080041
Chris Craik65fe5ee2015-01-26 18:06:29 -080042#include <algorithm>
John Recke248bd12015-08-05 13:53:53 -070043#include <fcntl.h>
44#include <sys/stat.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080045
Colin Cross290b23a2015-11-05 14:10:47 -080046#include <cstdlib>
47
John Reckf47a5942014-06-30 16:20:04 -070048#define TRIM_MEMORY_COMPLETE 80
49#define TRIM_MEMORY_UI_HIDDEN 20
50
John Recke248bd12015-08-05 13:53:53 -070051#define ENABLE_RENDERNODE_SERIALIZATION false
52
John Reck149173d2015-08-10 09:52:29 -070053#define LOG_FRAMETIME_MMA 0
54
55#if LOG_FRAMETIME_MMA
56static float sBenchMma = 0;
57static int sFrameCount = 0;
58static const float NANOS_PER_MILLIS_F = 1000000.0f;
59#endif
60
John Reck23b797a2014-01-03 18:08:34 -080061namespace android {
62namespace uirenderer {
63namespace renderthread {
64
Stan Iliev03de0742016-07-07 12:35:54 -040065CanvasContext* CanvasContext::create(RenderThread& thread,
66 bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory) {
67
68 auto renderType = Properties::getRenderPipelineType();
69 switch (renderType) {
70 case RenderPipelineType::OpenGL:
71 return new CanvasContext(thread, translucent, rootRenderNode, contextFactory);
72 case RenderPipelineType::SkiaGL:
73 //TODO: implement SKIA GL
74 LOG_ALWAYS_FATAL("skiaGL canvas type not implemented.");
75 break;
76 case RenderPipelineType::Vulkan:
77 //TODO: implement Vulkan
78 LOG_ALWAYS_FATAL("Vulkan canvas type not implemented.");
79 break;
80 default:
81 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType);
82 break;
83 }
84 return nullptr;
85}
86
John Reck119907c2014-08-14 09:02:01 -070087CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
88 RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck3b202512014-06-23 13:13:08 -070089 : mRenderThread(thread)
90 , mEglManager(thread.eglManager())
John Reck4f02bf42014-01-03 18:09:17 -080091 , mOpaque(!translucent)
Chris Craik51d6a3d2014-12-22 17:16:56 -080092 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
John Reck4c9e59d2015-05-12 07:17:50 -070093 , mJankTracker(thread.timeLord().frameIntervalNanos())
Skuhneea7a7fb2015-08-28 07:10:31 -070094 , mProfiler(mFrames)
Skuhneb8160872015-09-22 09:51:39 -070095 , mContentDrawBounds(0, 0, 0, 0) {
Skuhneea7a7fb2015-08-28 07:10:31 -070096 mRenderNodes.emplace_back(rootRenderNode);
John Reck443a7142014-09-04 17:40:05 -070097 mRenderThread.renderState().registerCanvasContext(this);
John Reckb36016c2015-03-11 08:50:53 -070098 mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
John Reck23b797a2014-01-03 18:08:34 -080099}
100
101CanvasContext::~CanvasContext() {
John Reck51f2d602016-04-06 07:50:47 -0700102 destroy(nullptr);
John Reck443a7142014-09-04 17:40:05 -0700103 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -0800104}
105
John Reck51f2d602016-04-06 07:50:47 -0700106void CanvasContext::destroy(TreeObserver* observer) {
John Reck17035b02014-09-03 07:39:53 -0700107 stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800108 setSurface(nullptr);
John Reck51f2d602016-04-06 07:50:47 -0700109 freePrefetchedLayers(observer);
110 destroyHardwareResources(observer);
John Recke2478d42014-09-03 16:46:05 -0700111 mAnimationContext->destroy();
Chris Craik6246d2782016-03-29 15:01:41 -0700112#if !HWUI_NEW_OPS
John Reck4f02bf42014-01-03 18:09:17 -0800113 if (mCanvas) {
114 delete mCanvas;
Chris Craikd41c4d82015-01-05 15:51:13 -0800115 mCanvas = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -0800116 }
Chris Craik6246d2782016-03-29 15:01:41 -0700117#endif
John Reck23b797a2014-01-03 18:08:34 -0800118}
119
John Reckf6481082016-02-02 15:18:23 -0800120void CanvasContext::setSurface(Surface* surface) {
John Reckfbc8df02014-11-14 16:18:41 -0800121 ATRACE_CALL();
122
John Reckf6481082016-02-02 15:18:23 -0800123 mNativeSurface = surface;
John Recka5dda642014-05-22 15:43:54 -0700124
John Reck23b797a2014-01-03 18:08:34 -0800125 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -0700126 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -0800127 mEglSurface = EGL_NO_SURFACE;
128 }
129
John Reckf6481082016-02-02 15:18:23 -0800130 if (surface) {
131 mEglSurface = mEglManager.createSurface(surface);
John Reck23b797a2014-01-03 18:08:34 -0800132 }
133
John Reck28912a52016-04-18 14:34:18 -0700134 mFrameNumber = -1;
135
John Reck23b797a2014-01-03 18:08:34 -0800136 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -0700137 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
138 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -0800139 mHaveNewSurface = true;
John Recke486d932015-10-28 09:21:19 -0700140 mSwapHistory.clear();
John Reck368cdd82014-05-07 13:11:00 -0700141 } else {
142 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -0800143 }
John Reck23b797a2014-01-03 18:08:34 -0800144}
145
John Reck1125d1f2014-10-23 11:02:19 -0700146void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
147 mSwapBehavior = swapBehavior;
148}
149
John Reckf6481082016-02-02 15:18:23 -0800150void CanvasContext::initialize(Surface* surface) {
151 setSurface(surface);
Chris Craikb565df12015-10-05 13:00:52 -0700152#if !HWUI_NEW_OPS
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100153 if (mCanvas) return;
John Reck3b202512014-06-23 13:13:08 -0700154 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800155 mCanvas->initProperties();
Chris Craikb565df12015-10-05 13:00:52 -0700156#endif
John Reck4f02bf42014-01-03 18:09:17 -0800157}
158
John Reckf6481082016-02-02 15:18:23 -0800159void CanvasContext::updateSurface(Surface* surface) {
160 setSurface(surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700161}
162
John Reckf6481082016-02-02 15:18:23 -0800163bool CanvasContext::pauseSurface(Surface* surface) {
John Reck01a5ea32014-12-03 13:01:07 -0800164 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800165}
166
John Reck8afcc762016-04-13 10:24:06 -0700167void CanvasContext::setStopped(bool stopped) {
168 if (mStopped != stopped) {
169 mStopped = stopped;
170 if (mStopped) {
171 mRenderThread.removeFrameCallback(this);
172 if (mEglManager.isCurrent(mEglSurface)) {
173 mEglManager.makeCurrent(EGL_NO_SURFACE);
174 }
John Reck306f3312016-06-10 16:01:55 -0700175 } else if (mIsDirty && hasSurface()) {
176 mRenderThread.postFrameCallback(this);
John Reck8afcc762016-04-13 10:24:06 -0700177 }
178 }
179}
180
John Reckab1080c2016-06-21 16:24:20 -0700181void CanvasContext::setup(float lightRadius,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000182 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
Chris Craik98787e62015-11-13 10:55:30 -0800183#if HWUI_NEW_OPS
Chris Craik6e068c012016-01-15 16:15:30 -0800184 mLightGeometry.radius = lightRadius;
Chris Craik98787e62015-11-13 10:55:30 -0800185 mLightInfo.ambientShadowAlpha = ambientShadowAlpha;
186 mLightInfo.spotShadowAlpha = spotShadowAlpha;
187#else
John Reck4f02bf42014-01-03 18:09:17 -0800188 if (!mCanvas) return;
Alan Viverette50210d92015-05-14 18:05:36 -0700189 mCanvas->initLight(lightRadius, ambientShadowAlpha, spotShadowAlpha);
Chris Craik98787e62015-11-13 10:55:30 -0800190#endif
Alan Viverette50210d92015-05-14 18:05:36 -0700191}
192
193void CanvasContext::setLightCenter(const Vector3& lightCenter) {
Chris Craik98787e62015-11-13 10:55:30 -0800194#if HWUI_NEW_OPS
Chris Craik6e068c012016-01-15 16:15:30 -0800195 mLightGeometry.center = lightCenter;
Chris Craik98787e62015-11-13 10:55:30 -0800196#else
Alan Viverette50210d92015-05-14 18:05:36 -0700197 if (!mCanvas) return;
198 mCanvas->setLightCenter(lightCenter);
Chris Craik98787e62015-11-13 10:55:30 -0800199#endif
John Reck4f02bf42014-01-03 18:09:17 -0800200}
201
John Reck63a06672014-05-07 13:45:54 -0700202void CanvasContext::setOpaque(bool opaque) {
203 mOpaque = opaque;
204}
205
John Reck8afcc762016-04-13 10:24:06 -0700206bool CanvasContext::makeCurrent() {
207 if (mStopped) return false;
208
John Reckdbc9a862014-04-17 20:25:13 -0700209 // TODO: Figure out why this workaround is needed, see b/13913604
210 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reckf2dcc2a2015-07-16 09:17:59 -0700211 EGLint error = 0;
212 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface, &error);
213 if (error) {
214 setSurface(nullptr);
215 }
John Reck8afcc762016-04-13 10:24:06 -0700216 return !error;
John Reck860d1552014-04-11 19:15:05 -0700217}
218
John Reckbf3c6022015-06-02 15:55:00 -0700219static bool wasSkipped(FrameInfo* info) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700220 return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
John Reckbf3c6022015-06-02 15:55:00 -0700221}
222
John Reck0def73a2016-07-01 16:19:13 -0700223bool CanvasContext::isSwapChainStuffed() {
224 if (mSwapHistory.size() != mSwapHistory.capacity()) {
225 // We want at least 3 frames of history before attempting to
226 // guess if the queue is stuffed
227 return false;
228 }
229 nsecs_t frameInterval = mRenderThread.timeLord().frameIntervalNanos();
230 auto& swapA = mSwapHistory[0];
231
232 // Was there a happy queue & dequeue time? If so, don't
233 // consider it stuffed
234 if (swapA.dequeueDuration < 3_ms
235 && swapA.queueDuration < 3_ms) {
236 return false;
237 }
238
239 for (size_t i = 1; i < mSwapHistory.size(); i++) {
240 auto& swapB = mSwapHistory[i];
241
242 // If there's a frameInterval gap we effectively already dropped a frame,
243 // so consider the queue healthy.
244 if (swapA.swapCompletedTime - swapB.swapCompletedTime > frameInterval) {
245 return false;
246 }
247
248 // Was there a happy queue & dequeue time? If so, don't
249 // consider it stuffed
250 if (swapB.dequeueDuration < 3_ms
251 && swapB.queueDuration < 3_ms) {
252 return false;
253 }
254
255 swapA = swapB;
256 }
257
258 // All signs point to a stuffed swap chain
259 return true;
260}
261
Skuhneea7a7fb2015-08-28 07:10:31 -0700262void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
263 int64_t syncQueued, RenderNode* target) {
John Reckf9be7792014-05-02 18:21:16 -0700264 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700265
John Reckbf3c6022015-06-02 15:55:00 -0700266 // If the previous frame was dropped we don't need to hold onto it, so
267 // just keep using the previous frame's structure instead
268 if (!wasSkipped(mCurrentFrameInfo)) {
269 mCurrentFrameInfo = &mFrames.next();
270 }
John Reckba6adf62015-02-19 14:36:50 -0800271 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
John Reckbe3fba02015-07-06 13:49:58 -0700272 mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
John Reckba6adf62015-02-19 14:36:50 -0800273 mCurrentFrameInfo->markSyncStart();
274
John Recke4267ea2014-06-03 15:53:15 -0700275 info.damageAccumulator = &mDamageAccumulator;
Chris Craik0b7e8242015-10-28 16:50:44 -0700276#if HWUI_NEW_OPS
277 info.layerUpdateQueue = &mLayerUpdateQueue;
278#else
John Reck25fbb3f2014-06-12 13:46:45 -0700279 info.renderer = mCanvas;
Chris Craik0b7e8242015-10-28 16:50:44 -0700280#endif
John Reck00e79c92015-07-21 10:23:59 -0700281
John Reckec845a22014-09-05 15:23:38 -0700282 mAnimationContext->startFrame(info.mode);
Skuhneea7a7fb2015-08-28 07:10:31 -0700283 for (const sp<RenderNode>& node : mRenderNodes) {
284 // Only the primary target node will be drawn full - all other nodes would get drawn in
285 // real time mode. In case of a window, the primary node is the window content and the other
286 // node(s) are non client / filler nodes.
287 info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY);
288 node->prepareTree(info);
John Reck975591a2016-01-22 16:28:07 -0800289 GL_CHECKPOINT(MODERATE);
Skuhneea7a7fb2015-08-28 07:10:31 -0700290 }
John Reck119907c2014-08-14 09:02:01 -0700291 mAnimationContext->runRemainingAnimations(info);
John Reck975591a2016-01-22 16:28:07 -0800292 GL_CHECKPOINT(MODERATE);
John Recke45b1fd2014-04-15 09:50:16 -0700293
John Reck51f2d602016-04-06 07:50:47 -0700294 freePrefetchedLayers(info.observer);
John Reck975591a2016-01-22 16:28:07 -0800295 GL_CHECKPOINT(MODERATE);
John Reck998a6d82014-08-28 15:35:53 -0700296
John Reck306f3312016-06-10 16:01:55 -0700297 mIsDirty = true;
298
John Reckf6481082016-02-02 15:18:23 -0800299 if (CC_UNLIKELY(!mNativeSurface.get())) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700300 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaa95a882014-11-07 11:02:07 -0800301 info.out.canDrawThisFrame = false;
302 return;
303 }
304
John Reckf1480762016-07-03 18:28:25 -0700305 if (CC_LIKELY(mSwapHistory.size() && !Properties::forceDrawFrame)) {
John Recke486d932015-10-28 09:21:19 -0700306 nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
John Reck0def73a2016-07-01 16:19:13 -0700307 SwapHistory& lastSwap = mSwapHistory.back();
308 int durationUs;
309 mNativeSurface->query(NATIVE_WINDOW_LAST_DEQUEUE_DURATION, &durationUs);
310 lastSwap.dequeueDuration = us2ns(durationUs);
311 mNativeSurface->query(NATIVE_WINDOW_LAST_QUEUE_DURATION, &durationUs);
312 lastSwap.queueDuration = us2ns(durationUs);
John Reck52b783f2015-11-24 11:12:55 -0800313 nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
John Recke486d932015-10-28 09:21:19 -0700314 // The slight fudge-factor is to deal with cases where
315 // the vsync was estimated due to being slow handling the signal.
316 // See the logic in TimeLord#computeFrameTimeNanos or in
317 // Choreographer.java for details on when this happens
318 if (vsyncDelta < 2_ms) {
319 // Already drew for this vsync pulse, UI draw request missed
320 // the deadline for RT animations
321 info.out.canDrawThisFrame = false;
John Reck0def73a2016-07-01 16:19:13 -0700322 } else if (vsyncDelta >= mRenderThread.timeLord().frameIntervalNanos()) {
323 // It's been at least an entire frame interval, assume
324 // the buffer queue is fine
John Recke486d932015-10-28 09:21:19 -0700325 info.out.canDrawThisFrame = true;
326 } else {
John Reck0def73a2016-07-01 16:19:13 -0700327 info.out.canDrawThisFrame = !isSwapChainStuffed();
John Recke486d932015-10-28 09:21:19 -0700328 }
329 } else {
330 info.out.canDrawThisFrame = true;
331 }
John Recka5dda642014-05-22 15:43:54 -0700332
John Reckaef9dc82015-05-08 14:10:57 -0700333 if (!info.out.canDrawThisFrame) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700334 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaef9dc82015-05-08 14:10:57 -0700335 }
336
John Recka5dda642014-05-22 15:43:54 -0700337 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700338 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700339 // If animationsNeedsRedraw is set don't bother posting for an RT anim
340 // as we will just end up fighting the UI thread.
341 mRenderThread.postFrameCallback(this);
342 }
John Recke45b1fd2014-04-15 09:50:16 -0700343 }
344}
345
John Reckf47a5942014-06-30 16:20:04 -0700346void CanvasContext::stopDrawing() {
347 mRenderThread.removeFrameCallback(this);
Doris Liu67ce99b2016-05-17 16:50:31 -0700348 mAnimationContext->detachAnimators();
John Reckf47a5942014-06-30 16:20:04 -0700349}
350
John Recka5dda642014-05-22 15:43:54 -0700351void CanvasContext::notifyFramePending() {
352 ATRACE_CALL();
353 mRenderThread.pushBackFrameCallback(this);
354}
355
John Recke4267ea2014-06-03 15:53:15 -0700356void CanvasContext::draw() {
Chris Craikb565df12015-10-05 13:00:52 -0700357#if !HWUI_NEW_OPS
John Reck4f02bf42014-01-03 18:09:17 -0800358 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700359 "drawRenderNode called on a context with no canvas or surface!");
Chris Craikb565df12015-10-05 13:00:52 -0700360#endif
John Reck4f02bf42014-01-03 18:09:17 -0800361
John Recke4267ea2014-06-03 15:53:15 -0700362 SkRect dirty;
363 mDamageAccumulator.finish(&dirty);
364
John Reck6d4d0db2015-08-03 15:34:52 -0700365 // TODO: Re-enable after figuring out cause of b/22592975
366// if (dirty.isEmpty() && Properties::skipEmptyFrames) {
367// mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
368// return;
369// }
John Reck240ff622015-04-28 13:50:00 -0700370
John Reck240ff622015-04-28 13:50:00 -0700371 mCurrentFrameInfo->markIssueDrawCommandsStart();
372
John Reck149173d2015-08-10 09:52:29 -0700373 Frame frame = mEglManager.beginFrame(mEglSurface);
Chris Craikb565df12015-10-05 13:00:52 -0700374
John Reck77c40102015-10-26 15:49:47 -0700375 if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) {
Chris Craik64e445b2015-09-02 14:23:49 -0700376 // can't rely on prior content of window if viewport size changes
John Recke4267ea2014-06-03 15:53:15 -0700377 dirty.setEmpty();
John Reck77c40102015-10-26 15:49:47 -0700378 mLastFrameWidth = frame.width();
379 mLastFrameHeight = frame.height();
John Reck149173d2015-08-10 09:52:29 -0700380 } else if (mHaveNewSurface || frame.bufferAge() == 0) {
381 // New surface needs a full draw
John Recke4267ea2014-06-03 15:53:15 -0700382 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700383 } else {
John Reck149173d2015-08-10 09:52:29 -0700384 if (!dirty.isEmpty() && !dirty.intersect(0, 0, frame.width(), frame.height())) {
John Reck0a973302014-07-16 13:29:45 -0700385 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
John Reck149173d2015-08-10 09:52:29 -0700386 SK_RECT_ARGS(dirty), frame.width(), frame.height());
John Reck0a973302014-07-16 13:29:45 -0700387 dirty.setEmpty();
388 }
John Recke4267ea2014-06-03 15:53:15 -0700389 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800390 }
391
John Reck149173d2015-08-10 09:52:29 -0700392 if (dirty.isEmpty()) {
393 dirty.set(0, 0, frame.width(), frame.height());
Season Li13d1b4a2015-07-29 17:16:19 -0700394 }
395
John Reck149173d2015-08-10 09:52:29 -0700396 // At this point dirty is the area of the screen to update. However,
397 // the area of the frame we need to repaint is potentially different, so
398 // stash the screen area for later
399 SkRect screenDirty(dirty);
400
401 // If the buffer age is 0 we do a full-screen repaint (handled above)
402 // If the buffer age is 1 the buffer contents are the same as they were
403 // last frame so there's nothing to union() against
404 // Therefore we only care about the > 1 case.
405 if (frame.bufferAge() > 1) {
John Recke486d932015-10-28 09:21:19 -0700406 if (frame.bufferAge() > (int) mSwapHistory.size()) {
John Reck149173d2015-08-10 09:52:29 -0700407 // We don't have enough history to handle this old of a buffer
408 // Just do a full-draw
409 dirty.set(0, 0, frame.width(), frame.height());
410 } else {
411 // At this point we haven't yet added the latest frame
412 // to the damage history (happens below)
413 // So we need to damage
John Recke486d932015-10-28 09:21:19 -0700414 for (int i = mSwapHistory.size() - 1;
415 i > ((int) mSwapHistory.size()) - frame.bufferAge(); i--) {
416 dirty.join(mSwapHistory[i].damage);
John Reck149173d2015-08-10 09:52:29 -0700417 }
418 }
John Reck4f02bf42014-01-03 18:09:17 -0800419 }
420
John Reck149173d2015-08-10 09:52:29 -0700421 mEglManager.damageFrame(frame, dirty);
Chris Craikddf22152015-10-14 17:42:47 -0700422
423#if HWUI_NEW_OPS
Chris Craik3a5811b2016-03-22 15:03:08 -0700424 auto& caches = Caches::getInstance();
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700425 FrameBuilder frameBuilder(dirty, frame.width(), frame.height(), mLightGeometry, caches);
426
427 frameBuilder.deferLayers(mLayerUpdateQueue);
Chris Craik0b7e8242015-10-28 16:50:44 -0700428 mLayerUpdateQueue.clear();
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700429
430 frameBuilder.deferRenderNodeScene(mRenderNodes, mContentDrawBounds);
431
Chris Craik1dfa0702016-03-04 15:59:24 -0800432 BakedOpRenderer renderer(caches, mRenderThread.renderState(),
Chris Craik98787e62015-11-13 10:55:30 -0800433 mOpaque, mLightInfo);
Chris Craikf158b492016-01-12 14:45:08 -0800434 frameBuilder.replayBakedOps<BakedOpDispatcher>(renderer);
Chris Craik1dfa0702016-03-04 15:59:24 -0800435 profiler().draw(&renderer);
Chris Craik5854b342015-10-26 15:49:56 -0700436 bool drew = renderer.didDraw();
Chris Craikddf22152015-10-14 17:42:47 -0700437
Chris Craik1dfa0702016-03-04 15:59:24 -0800438 // post frame cleanup
439 caches.clearGarbage();
440 caches.pathCache.trim();
441 caches.tessellationCache.trim();
442
443#if DEBUG_MEMORY_USAGE
444 mCaches.dumpMemoryUsage();
445#else
446 if (CC_UNLIKELY(Properties::debugLevel & kDebugMemory)) {
447 caches.dumpMemoryUsage();
448 }
449#endif
450
Chris Craikddf22152015-10-14 17:42:47 -0700451#else
Chris Craik64e445b2015-09-02 14:23:49 -0700452 mCanvas->prepareDirty(frame.width(), frame.height(),
453 dirty.fLeft, dirty.fTop, dirty.fRight, dirty.fBottom, mOpaque);
John Reck149173d2015-08-10 09:52:29 -0700454
John Reck4f02bf42014-01-03 18:09:17 -0800455 Rect outBounds;
Robert Carrb6c26242015-10-22 12:03:28 -0700456 // It there are multiple render nodes, they are laid out as follows:
457 // #0 - backdrop (content + caption)
Skuhneb8160872015-09-22 09:51:39 -0700458 // #1 - content (positioned at (0,0) and clipped to - its bounds mContentDrawBounds)
Robert Carrb6c26242015-10-22 12:03:28 -0700459 // #2 - additional overlay nodes
Skuhneea7a7fb2015-08-28 07:10:31 -0700460 // Usually the backdrop cannot be seen since it will be entirely covered by the content. While
461 // resizing however it might become partially visible. The following render loop will crop the
Robert Carrb6c26242015-10-22 12:03:28 -0700462 // backdrop against the content and draw the remaining part of it. It will then draw the content
463 // cropped to the backdrop (since that indicates a shrinking of the window).
464 //
465 // Additional nodes will be drawn on top with no particular clipping semantics.
466
Skuhneea7a7fb2015-08-28 07:10:31 -0700467 // The bounds of the backdrop against which the content should be clipped.
Skuhneb8160872015-09-22 09:51:39 -0700468 Rect backdropBounds = mContentDrawBounds;
469 // Usually the contents bounds should be mContentDrawBounds - however - we will
470 // move it towards the fixed edge to give it a more stable appearance (for the moment).
471 Rect contentBounds;
Skuhneea7a7fb2015-08-28 07:10:31 -0700472 // If there is no content bounds we ignore the layering as stated above and start with 2.
Robert Carrb6c26242015-10-22 12:03:28 -0700473 int layer = (mContentDrawBounds.isEmpty() || mRenderNodes.size() == 1) ? 2 : 0;
Skuhneea7a7fb2015-08-28 07:10:31 -0700474 // Draw all render nodes. Note that
475 for (const sp<RenderNode>& node : mRenderNodes) {
476 if (layer == 0) { // Backdrop.
Skuhneb8160872015-09-22 09:51:39 -0700477 // Draw the backdrop clipped to the inverse content bounds, but assume that the content
478 // was moved to the upper left corner.
Skuhneea7a7fb2015-08-28 07:10:31 -0700479 const RenderProperties& properties = node->properties();
480 Rect targetBounds(properties.getLeft(), properties.getTop(),
481 properties.getRight(), properties.getBottom());
Skuhneb8160872015-09-22 09:51:39 -0700482 // Move the content bounds towards the fixed corner of the backdrop.
483 const int x = targetBounds.left;
484 const int y = targetBounds.top;
485 contentBounds.set(x, y, x + mContentDrawBounds.getWidth(),
486 y + mContentDrawBounds.getHeight());
Skuhneea7a7fb2015-08-28 07:10:31 -0700487 // Remember the intersection of the target bounds and the intersection bounds against
488 // which we have to crop the content.
Skuhneb8160872015-09-22 09:51:39 -0700489 backdropBounds.set(x, y, x + backdropBounds.getWidth(), y + backdropBounds.getHeight());
Chris Craikac02eb92015-10-05 12:23:46 -0700490 backdropBounds.doIntersect(targetBounds);
Skuhneea7a7fb2015-08-28 07:10:31 -0700491 // Check if we have to draw something on the left side ...
Skuhneb8160872015-09-22 09:51:39 -0700492 if (targetBounds.left < contentBounds.left) {
Florin Malitaeecff562015-12-21 10:43:01 -0500493 mCanvas->save(SaveFlags::Clip);
Skuhneea7a7fb2015-08-28 07:10:31 -0700494 if (mCanvas->clipRect(targetBounds.left, targetBounds.top,
Skuhneb8160872015-09-22 09:51:39 -0700495 contentBounds.left, targetBounds.bottom,
Skuhneea7a7fb2015-08-28 07:10:31 -0700496 SkRegion::kIntersect_Op)) {
497 mCanvas->drawRenderNode(node.get(), outBounds);
498 }
499 // Reduce the target area by the area we have just painted.
Skuhneb8160872015-09-22 09:51:39 -0700500 targetBounds.left = std::min(contentBounds.left, targetBounds.right);
Skuhneea7a7fb2015-08-28 07:10:31 -0700501 mCanvas->restore();
502 }
503 // ... or on the right side ...
Skuhneb8160872015-09-22 09:51:39 -0700504 if (targetBounds.right > contentBounds.right &&
Skuhneea7a7fb2015-08-28 07:10:31 -0700505 !targetBounds.isEmpty()) {
Florin Malitaeecff562015-12-21 10:43:01 -0500506 mCanvas->save(SaveFlags::Clip);
Skuhneb8160872015-09-22 09:51:39 -0700507 if (mCanvas->clipRect(contentBounds.right, targetBounds.top,
Skuhneea7a7fb2015-08-28 07:10:31 -0700508 targetBounds.right, targetBounds.bottom,
509 SkRegion::kIntersect_Op)) {
510 mCanvas->drawRenderNode(node.get(), outBounds);
511 }
512 // Reduce the target area by the area we have just painted.
Skuhneb8160872015-09-22 09:51:39 -0700513 targetBounds.right = std::max(targetBounds.left, contentBounds.right);
Skuhneea7a7fb2015-08-28 07:10:31 -0700514 mCanvas->restore();
515 }
516 // ... or at the top ...
Skuhneb8160872015-09-22 09:51:39 -0700517 if (targetBounds.top < contentBounds.top &&
Skuhneea7a7fb2015-08-28 07:10:31 -0700518 !targetBounds.isEmpty()) {
Florin Malitaeecff562015-12-21 10:43:01 -0500519 mCanvas->save(SaveFlags::Clip);
Skuhneea7a7fb2015-08-28 07:10:31 -0700520 if (mCanvas->clipRect(targetBounds.left, targetBounds.top, targetBounds.right,
Skuhneb8160872015-09-22 09:51:39 -0700521 contentBounds.top,
Skuhneea7a7fb2015-08-28 07:10:31 -0700522 SkRegion::kIntersect_Op)) {
523 mCanvas->drawRenderNode(node.get(), outBounds);
524 }
525 // Reduce the target area by the area we have just painted.
Skuhneb8160872015-09-22 09:51:39 -0700526 targetBounds.top = std::min(contentBounds.top, targetBounds.bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700527 mCanvas->restore();
528 }
529 // ... or at the bottom.
Skuhneb8160872015-09-22 09:51:39 -0700530 if (targetBounds.bottom > contentBounds.bottom &&
Skuhneea7a7fb2015-08-28 07:10:31 -0700531 !targetBounds.isEmpty()) {
Florin Malitaeecff562015-12-21 10:43:01 -0500532 mCanvas->save(SaveFlags::Clip);
Skuhneb8160872015-09-22 09:51:39 -0700533 if (mCanvas->clipRect(targetBounds.left, contentBounds.bottom, targetBounds.right,
Skuhneea7a7fb2015-08-28 07:10:31 -0700534 targetBounds.bottom, SkRegion::kIntersect_Op)) {
535 mCanvas->drawRenderNode(node.get(), outBounds);
536 }
537 mCanvas->restore();
538 }
539 } else if (layer == 1) { // Content
540 // It gets cropped against the bounds of the backdrop to stay inside.
Florin Malitaeecff562015-12-21 10:43:01 -0500541 mCanvas->save(SaveFlags::MatrixClip);
Skuhneb8160872015-09-22 09:51:39 -0700542
543 // We shift and clip the content to match its final location in the window.
544 const float left = mContentDrawBounds.left;
545 const float top = mContentDrawBounds.top;
546 const float dx = backdropBounds.left - left;
547 const float dy = backdropBounds.top - top;
548 const float width = backdropBounds.getWidth();
549 const float height = backdropBounds.getHeight();
Robert Carrb6c26242015-10-22 12:03:28 -0700550
Skuhneb8160872015-09-22 09:51:39 -0700551 mCanvas->translate(dx, dy);
552 if (mCanvas->clipRect(left, top, left + width, top + height, SkRegion::kIntersect_Op)) {
Skuhneea7a7fb2015-08-28 07:10:31 -0700553 mCanvas->drawRenderNode(node.get(), outBounds);
554 }
555 mCanvas->restore();
556 } else { // draw the rest on top at will!
557 mCanvas->drawRenderNode(node.get(), outBounds);
558 }
559 layer++;
560 }
John Reck4f02bf42014-01-03 18:09:17 -0800561
John Reckfe5e7b72014-05-23 17:42:28 -0700562 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800563
Tom Hudson107843d2014-09-08 11:26:26 -0400564 bool drew = mCanvas->finish();
Chris Craikddf22152015-10-14 17:42:47 -0700565#endif
John Reck9372ac32016-01-19 11:46:52 -0800566
John Reck38f6c032016-03-17 10:23:49 -0700567 waitOnFences();
568
John Reck975591a2016-01-22 16:28:07 -0800569 GL_CHECKPOINT(LOW);
John Reck9372ac32016-01-19 11:46:52 -0800570
John Reckba6adf62015-02-19 14:36:50 -0800571 // Even if we decided to cancel the frame, from the perspective of jank
572 // metrics the frame was swapped at this point
573 mCurrentFrameInfo->markSwapBuffers();
John Reck306f3312016-06-10 16:01:55 -0700574 mIsDirty = false;
John Reckba6adf62015-02-19 14:36:50 -0800575
John Reckc96955d2016-02-26 14:56:44 -0800576 if (drew || mEglManager.damageRequiresSwap()) {
John Reck149173d2015-08-10 09:52:29 -0700577 if (CC_UNLIKELY(!mEglManager.swapBuffers(frame, screenDirty))) {
578 setSurface(nullptr);
579 }
John Recke486d932015-10-28 09:21:19 -0700580 SwapHistory& swap = mSwapHistory.next();
581 swap.damage = screenDirty;
John Reck0def73a2016-07-01 16:19:13 -0700582 swap.swapCompletedTime = systemTime(CLOCK_MONOTONIC);
John Recke486d932015-10-28 09:21:19 -0700583 swap.vsyncTime = mRenderThread.timeLord().latestVsync();
John Reck149173d2015-08-10 09:52:29 -0700584 mHaveNewSurface = false;
John Reck28912a52016-04-18 14:34:18 -0700585 mFrameNumber = -1;
John Reck4f02bf42014-01-03 18:09:17 -0800586 }
John Reckfe5e7b72014-05-23 17:42:28 -0700587
John Reckba6adf62015-02-19 14:36:50 -0800588 // TODO: Use a fence for real completion?
589 mCurrentFrameInfo->markFrameCompleted();
John Reck149173d2015-08-10 09:52:29 -0700590
591#if LOG_FRAMETIME_MMA
592 float thisFrame = mCurrentFrameInfo->duration(
593 FrameInfoIndex::IssueDrawCommandsStart,
594 FrameInfoIndex::FrameCompleted) / NANOS_PER_MILLIS_F;
595 if (sFrameCount) {
596 sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
597 } else {
598 sBenchMma = thisFrame;
599 }
600 if (++sFrameCount == 10) {
601 sFrameCount = 1;
602 ALOGD("Average frame time: %.4f", sBenchMma);
603 }
604#endif
605
John Reckedc524c2015-03-18 15:24:33 -0700606 mJankTracker.addFrame(*mCurrentFrameInfo);
John Reckba6adf62015-02-19 14:36:50 -0800607 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
Andres Morales910beb82016-02-02 16:19:40 -0800608 if (CC_UNLIKELY(mFrameMetricsReporter.get() != nullptr)) {
609 mFrameMetricsReporter->reportFrameMetrics(mCurrentFrameInfo->data());
Andres Morales06f5bc72015-12-15 15:21:31 -0800610 }
John Reck38e0c322015-11-10 12:19:17 -0800611
612 GpuMemoryTracker::onFrameCompleted();
John Reck4f02bf42014-01-03 18:09:17 -0800613}
614
John Recke45b1fd2014-04-15 09:50:16 -0700615// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700616void CanvasContext::doFrame() {
Chris Craikadfeec92015-12-15 19:17:32 -0800617#if HWUI_NEW_OPS
618 if (CC_UNLIKELY(mEglSurface == EGL_NO_SURFACE)) return;
619#else
620 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) return;
621#endif
Skuhneea7a7fb2015-08-28 07:10:31 -0700622 prepareAndDraw(nullptr);
623}
John Reck368cdd82014-05-07 13:11:00 -0700624
Skuhneea7a7fb2015-08-28 07:10:31 -0700625void CanvasContext::prepareAndDraw(RenderNode* node) {
John Recke45b1fd2014-04-15 09:50:16 -0700626 ATRACE_CALL();
627
Matthew Bouyack7f667e72016-01-12 12:01:48 -0800628 nsecs_t vsync = mRenderThread.timeLord().computeFrameTimeNanos();
John Reckba6adf62015-02-19 14:36:50 -0800629 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
630 UiFrameInfoBuilder(frameInfo)
Chris Craik1b54fb22015-06-02 17:40:58 -0700631 .addFlag(FrameInfoFlags::RTAnimation)
Matthew Bouyack7f667e72016-01-12 12:01:48 -0800632 .setVsync(vsync, vsync);
John Reckfe5e7b72014-05-23 17:42:28 -0700633
Chris Craike2e53a72015-10-28 15:55:40 -0700634 TreeInfo info(TreeInfo::MODE_RT_ONLY, *this);
Skuhneea7a7fb2015-08-28 07:10:31 -0700635 prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC), node);
John Recka5dda642014-05-22 15:43:54 -0700636 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700637 draw();
John Recka5dda642014-05-22 15:43:54 -0700638 }
John Recke45b1fd2014-04-15 09:50:16 -0700639}
640
John Reck3b202512014-06-23 13:13:08 -0700641void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700642 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700643 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700644 if (thread.eglManager().hasEglContext()) {
John Reck0d1f6342014-03-28 20:30:27 -0700645 mode = DrawGlInfo::kModeProcess;
646 }
John Reck6f07a0d2014-04-16 21:31:25 -0700647
Chris Craikd41c4d82015-01-05 15:51:13 -0800648 thread.renderState().invokeFunctor(functor, mode, nullptr);
John Reck23b797a2014-01-03 18:08:34 -0800649}
650
John Reck998a6d82014-08-28 15:35:53 -0700651void CanvasContext::markLayerInUse(RenderNode* node) {
John Reck51f2d602016-04-06 07:50:47 -0700652 if (mPrefetchedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800653 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700654 }
655}
656
John Reck51f2d602016-04-06 07:50:47 -0700657void CanvasContext::freePrefetchedLayers(TreeObserver* observer) {
658 if (mPrefetchedLayers.size()) {
659 for (auto& node : mPrefetchedLayers) {
660 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...",
661 node->getName());
662 node->destroyHardwareResources(observer);
663 node->decStrong(observer);
664 }
665 mPrefetchedLayers.clear();
John Reck998a6d82014-08-28 15:35:53 -0700666 }
667}
668
John Reck51f2d602016-04-06 07:50:47 -0700669void CanvasContext::buildLayer(RenderNode* node, TreeObserver* observer) {
John Reck3e824952014-08-20 10:08:39 -0700670 ATRACE_CALL();
Chris Craik6246d2782016-03-29 15:01:41 -0700671 if (!mEglManager.hasEglContext()) return;
672#if !HWUI_NEW_OPS
673 if (!mCanvas) return;
674#endif
675
John Reck3e824952014-08-20 10:08:39 -0700676 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
677 stopDrawing();
678
Chris Craike2e53a72015-10-28 15:55:40 -0700679 TreeInfo info(TreeInfo::MODE_FULL, *this);
John Reck3e824952014-08-20 10:08:39 -0700680 info.damageAccumulator = &mDamageAccumulator;
John Reck51f2d602016-04-06 07:50:47 -0700681 info.observer = observer;
Chris Craik0b7e8242015-10-28 16:50:44 -0700682#if HWUI_NEW_OPS
683 info.layerUpdateQueue = &mLayerUpdateQueue;
684#else
John Reck3e824952014-08-20 10:08:39 -0700685 info.renderer = mCanvas;
Chris Craik0b7e8242015-10-28 16:50:44 -0700686#endif
John Reck9eb9f6f2014-08-21 11:23:05 -0700687 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700688 node->prepareTree(info);
689 SkRect ignore;
690 mDamageAccumulator.finish(&ignore);
691 // Tickle the GENERIC property on node to mark it as dirty for damaging
692 // purposes when the frame is actually drawn
693 node->setPropertyFieldsDirty(RenderNode::GENERIC);
694
Chris Craik98787e62015-11-13 10:55:30 -0800695#if HWUI_NEW_OPS
Chris Craik6246d2782016-03-29 15:01:41 -0700696 static const std::vector< sp<RenderNode> > emptyNodeList;
697 auto& caches = Caches::getInstance();
Chris Craik9cd1bbe2016-04-14 16:08:25 -0700698 FrameBuilder frameBuilder(mLayerUpdateQueue, mLightGeometry, caches);
Chris Craik6246d2782016-03-29 15:01:41 -0700699 mLayerUpdateQueue.clear();
700 BakedOpRenderer renderer(caches, mRenderThread.renderState(),
701 mOpaque, mLightInfo);
702 LOG_ALWAYS_FATAL_IF(renderer.didDraw(), "shouldn't draw in buildlayer case");
703 frameBuilder.replayBakedOps<BakedOpDispatcher>(renderer);
Chris Craik98787e62015-11-13 10:55:30 -0800704#else
John Reck443a7142014-09-04 17:40:05 -0700705 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700706 mCanvas->flushLayerUpdates();
Chris Craik98787e62015-11-13 10:55:30 -0800707#endif
John Reck998a6d82014-08-28 15:35:53 -0700708
Chris Craikd41c4d82015-01-05 15:51:13 -0800709 node->incStrong(nullptr);
John Reck51f2d602016-04-06 07:50:47 -0700710 mPrefetchedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700711}
712
John Reck19b6bcf2014-02-14 20:03:38 -0800713bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
John Reck68bfe0a2014-06-24 15:34:58 -0700714 layer->apply();
Chris Craik764045d2016-07-06 17:14:05 -0700715 return Readback::copyTextureLayerInto(mRenderThread, *(layer->backingLayer()), bitmap)
716 == CopyResult::Success;
John Reck19b6bcf2014-02-14 20:03:38 -0800717}
718
John Reck51f2d602016-04-06 07:50:47 -0700719void CanvasContext::destroyHardwareResources(TreeObserver* observer) {
John Reckf47a5942014-06-30 16:20:04 -0700720 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700721 if (mEglManager.hasEglContext()) {
John Reck51f2d602016-04-06 07:50:47 -0700722 freePrefetchedLayers(observer);
Skuhneea7a7fb2015-08-28 07:10:31 -0700723 for (const sp<RenderNode>& node : mRenderNodes) {
John Reck51f2d602016-04-06 07:50:47 -0700724 node->destroyHardwareResources(observer);
Skuhneea7a7fb2015-08-28 07:10:31 -0700725 }
John Reck00e79c92015-07-21 10:23:59 -0700726 Caches& caches = Caches::getInstance();
727 // Make sure to release all the textures we were owning as there won't
728 // be another draw
729 caches.textureCache.resetMarkInUse(this);
Chris Craik9fded232015-11-11 16:42:34 -0800730 mRenderThread.renderState().flush(Caches::FlushMode::Layers);
John Reckf47a5942014-06-30 16:20:04 -0700731 }
732}
733
734void CanvasContext::trimMemory(RenderThread& thread, int level) {
735 // No context means nothing to free
736 if (!thread.eglManager().hasEglContext()) return;
737
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200738 ATRACE_CALL();
John Reckf47a5942014-06-30 16:20:04 -0700739 if (level >= TRIM_MEMORY_COMPLETE) {
Chris Craik9fded232015-11-11 16:42:34 -0800740 thread.renderState().flush(Caches::FlushMode::Full);
John Reckf47a5942014-06-30 16:20:04 -0700741 thread.eglManager().destroy();
742 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
Chris Craik9fded232015-11-11 16:42:34 -0800743 thread.renderState().flush(Caches::FlushMode::Moderate);
John Recke1628b72014-05-23 15:11:19 -0700744 }
745}
746
John Reck1949e792014-04-08 15:18:56 -0700747Layer* CanvasContext::createTextureLayer() {
John Reck8afcc762016-04-13 10:24:06 -0700748 mEglManager.initialize();
John Reck3b202512014-06-23 13:13:08 -0700749 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700750}
751
John Reck3b202512014-06-23 13:13:08 -0700752void CanvasContext::setTextureAtlas(RenderThread& thread,
753 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
754 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700755}
756
John Reckba6adf62015-02-19 14:36:50 -0800757void CanvasContext::dumpFrames(int fd) {
758 FILE* file = fdopen(fd, "a");
John Reck4db3d172015-06-02 15:58:43 -0700759 fprintf(file, "\n\n---PROFILEDATA---\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700760 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck2a8bb052015-06-03 09:52:01 -0700761 fprintf(file, "%s", FrameInfoNames[i].c_str());
John Reck4db3d172015-06-02 15:58:43 -0700762 fprintf(file, ",");
763 }
John Reckba6adf62015-02-19 14:36:50 -0800764 for (size_t i = 0; i < mFrames.size(); i++) {
765 FrameInfo& frame = mFrames[i];
Chris Craik1b54fb22015-06-02 17:40:58 -0700766 if (frame[FrameInfoIndex::SyncStart] == 0) {
John Reckba6adf62015-02-19 14:36:50 -0800767 continue;
768 }
769 fprintf(file, "\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700770 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800771 fprintf(file, "%" PRId64 ",", frame[i]);
772 }
773 }
774 fprintf(file, "\n---PROFILEDATA---\n\n");
775 fflush(file);
776}
777
778void CanvasContext::resetFrameStats() {
779 mFrames.clear();
780 mRenderThread.jankTracker().reset();
781}
782
John Recke248bd12015-08-05 13:53:53 -0700783void CanvasContext::serializeDisplayListTree() {
784#if ENABLE_RENDERNODE_SERIALIZATION
785 using namespace google::protobuf::io;
786 char package[128];
787 // Check whether tracing is enabled for this process.
788 FILE * file = fopen("/proc/self/cmdline", "r");
789 if (file) {
790 if (!fgets(package, 128, file)) {
791 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
792 fclose(file);
793 return;
794 }
795 fclose(file);
796 } else {
797 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
798 errno);
799 return;
800 }
801 char path[1024];
802 snprintf(path, 1024, "/data/data/%s/cache/rendertree_dump", package);
803 int fd = open(path, O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH);
804 if (fd == -1) {
805 ALOGD("Failed to open '%s'", path);
806 return;
807 }
808 proto::RenderNode tree;
809 // TODO: Streaming writes?
810 mRootRenderNode->copyTo(&tree);
811 std::string data = tree.SerializeAsString();
812 write(fd, data.c_str(), data.length());
813 close(fd);
814#endif
815}
816
John Reck38f6c032016-03-17 10:23:49 -0700817void CanvasContext::waitOnFences() {
818 if (mFrameFences.size()) {
819 ATRACE_CALL();
820 for (auto& fence : mFrameFences) {
821 fence->getResult();
822 }
823 mFrameFences.clear();
824 }
825}
826
827class CanvasContext::FuncTaskProcessor : public TaskProcessor<bool> {
828public:
Chih-Hung Hsiehd53e3be2016-05-03 10:02:51 -0700829 explicit FuncTaskProcessor(Caches& caches)
John Reck38f6c032016-03-17 10:23:49 -0700830 : TaskProcessor<bool>(&caches.tasks) {}
831
832 virtual void onProcess(const sp<Task<bool> >& task) override {
833 FuncTask* t = static_cast<FuncTask*>(task.get());
834 t->func();
835 task->setResult(true);
836 }
837};
838
839void CanvasContext::enqueueFrameWork(std::function<void()>&& func) {
840 if (!mFrameWorkProcessor.get()) {
841 mFrameWorkProcessor = new FuncTaskProcessor(Caches::getInstance());
842 }
843 sp<FuncTask> task(new FuncTask());
844 task->func = func;
John Reck7b570de2016-06-27 13:27:23 -0700845 mFrameFences.push_back(task);
John Reck38f6c032016-03-17 10:23:49 -0700846 mFrameWorkProcessor->add(task);
847}
848
John Reck28912a52016-04-18 14:34:18 -0700849int64_t CanvasContext::getFrameNumber() {
850 // mFrameNumber is reset to -1 when the surface changes or we swap buffers
851 if (mFrameNumber == -1 && mNativeSurface.get()) {
852 mFrameNumber = static_cast<int64_t>(mNativeSurface->getNextFrameNumber());
853 }
854 return mFrameNumber;
855}
856
Stan Iliev03de0742016-07-07 12:35:54 -0400857bool CanvasContext::isSkiaEnabled() {
858 auto renderType = Properties::getRenderPipelineType();
859 return RenderPipelineType::SkiaGL == renderType || RenderPipelineType::Vulkan == renderType;
860}
861
John Reck23b797a2014-01-03 18:08:34 -0800862} /* namespace renderthread */
863} /* namespace uirenderer */
864} /* namespace android */