blob: 9b0be49beb07250c731faf767730cc4fd49b9d7c [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"
John Reckd04794a2015-05-08 10:04:36 -070024#include "LayerRenderer.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070025#include "LayerUpdateQueue.h"
John Reckd04794a2015-05-08 10:04:36 -070026#include "Properties.h"
Chris Craik764045d2016-07-06 17:14:05 -070027#include "Readback.h"
John Reck4f02bf42014-01-03 18:09:17 -080028#include "RenderThread.h"
sergeyvdccca442016-03-21 15:38:21 -070029#include "hwui/Canvas.h"
John Reckd04794a2015-05-08 10:04:36 -070030#include "renderstate/RenderState.h"
31#include "renderstate/Stencil.h"
John Recke248bd12015-08-05 13:53:53 -070032#include "protos/hwui.pb.h"
Stan Iliev768e3932016-07-08 21:34:52 -040033#include "OpenGLPipeline.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();
Stan Iliev768e3932016-07-08 21:34:52 -040069
Stan Iliev03de0742016-07-07 12:35:54 -040070 switch (renderType) {
71 case RenderPipelineType::OpenGL:
Stan Iliev768e3932016-07-08 21:34:52 -040072 return new CanvasContext(thread, translucent, rootRenderNode, contextFactory,
73 std::make_unique<OpenGLPipeline>(thread));
Stan Iliev03de0742016-07-07 12:35:54 -040074 case RenderPipelineType::SkiaGL:
75 //TODO: implement SKIA GL
76 LOG_ALWAYS_FATAL("skiaGL canvas type not implemented.");
77 break;
Stan Iliev8a33e402016-07-08 09:57:49 -040078 case RenderPipelineType::SkiaVulkan:
Stan Iliev03de0742016-07-07 12:35:54 -040079 //TODO: implement Vulkan
80 LOG_ALWAYS_FATAL("Vulkan canvas type not implemented.");
81 break;
82 default:
83 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType);
84 break;
85 }
86 return nullptr;
87}
88
John Reck119907c2014-08-14 09:02:01 -070089CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
Stan Iliev768e3932016-07-08 21:34:52 -040090 RenderNode* rootRenderNode, IContextFactory* contextFactory,
91 std::unique_ptr<IRenderPipeline> renderPipeline)
John Reck3b202512014-06-23 13:13:08 -070092 : mRenderThread(thread)
John Reck4f02bf42014-01-03 18:09:17 -080093 , mOpaque(!translucent)
Chris Craik51d6a3d2014-12-22 17:16:56 -080094 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
John Reck4c9e59d2015-05-12 07:17:50 -070095 , mJankTracker(thread.timeLord().frameIntervalNanos())
Skuhneea7a7fb2015-08-28 07:10:31 -070096 , mProfiler(mFrames)
Stan Iliev768e3932016-07-08 21:34:52 -040097 , mContentDrawBounds(0, 0, 0, 0)
98 , mRenderPipeline(std::move(renderPipeline)) {
Skuhneea7a7fb2015-08-28 07:10:31 -070099 mRenderNodes.emplace_back(rootRenderNode);
John Reck443a7142014-09-04 17:40:05 -0700100 mRenderThread.renderState().registerCanvasContext(this);
John Reckb36016c2015-03-11 08:50:53 -0700101 mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
John Reck23b797a2014-01-03 18:08:34 -0800102}
103
104CanvasContext::~CanvasContext() {
John Reck51f2d602016-04-06 07:50:47 -0700105 destroy(nullptr);
John Reck443a7142014-09-04 17:40:05 -0700106 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -0800107}
108
John Reck51f2d602016-04-06 07:50:47 -0700109void CanvasContext::destroy(TreeObserver* observer) {
John Reck17035b02014-09-03 07:39:53 -0700110 stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800111 setSurface(nullptr);
John Reck51f2d602016-04-06 07:50:47 -0700112 freePrefetchedLayers(observer);
113 destroyHardwareResources(observer);
John Recke2478d42014-09-03 16:46:05 -0700114 mAnimationContext->destroy();
John Reck23b797a2014-01-03 18:08:34 -0800115}
116
John Reckf6481082016-02-02 15:18:23 -0800117void CanvasContext::setSurface(Surface* surface) {
John Reckfbc8df02014-11-14 16:18:41 -0800118 ATRACE_CALL();
119
John Reckf6481082016-02-02 15:18:23 -0800120 mNativeSurface = surface;
John Recka5dda642014-05-22 15:43:54 -0700121
Stan Iliev768e3932016-07-08 21:34:52 -0400122 bool hasSurface = mRenderPipeline->setSurface(surface, mSwapBehavior);
John Reck23b797a2014-01-03 18:08:34 -0800123
John Reck28912a52016-04-18 14:34:18 -0700124 mFrameNumber = -1;
125
Stan Iliev768e3932016-07-08 21:34:52 -0400126 if (hasSurface) {
127 mHaveNewSurface = true;
128 mSwapHistory.clear();
John Reck368cdd82014-05-07 13:11:00 -0700129 } else {
Stan Iliev768e3932016-07-08 21:34:52 -0400130 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -0800131 }
John Reck23b797a2014-01-03 18:08:34 -0800132}
133
John Reck1125d1f2014-10-23 11:02:19 -0700134void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
135 mSwapBehavior = swapBehavior;
136}
137
John Reckf6481082016-02-02 15:18:23 -0800138void CanvasContext::initialize(Surface* surface) {
139 setSurface(surface);
John Reck4f02bf42014-01-03 18:09:17 -0800140}
141
John Reckf6481082016-02-02 15:18:23 -0800142void CanvasContext::updateSurface(Surface* surface) {
143 setSurface(surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700144}
145
John Reckf6481082016-02-02 15:18:23 -0800146bool CanvasContext::pauseSurface(Surface* surface) {
John Reck01a5ea32014-12-03 13:01:07 -0800147 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800148}
149
John Reck8afcc762016-04-13 10:24:06 -0700150void CanvasContext::setStopped(bool stopped) {
151 if (mStopped != stopped) {
152 mStopped = stopped;
153 if (mStopped) {
154 mRenderThread.removeFrameCallback(this);
Stan Iliev768e3932016-07-08 21:34:52 -0400155 mRenderPipeline->onStop();
John Reck306f3312016-06-10 16:01:55 -0700156 } else if (mIsDirty && hasSurface()) {
157 mRenderThread.postFrameCallback(this);
John Reck8afcc762016-04-13 10:24:06 -0700158 }
159 }
160}
161
John Reckab1080c2016-06-21 16:24:20 -0700162void CanvasContext::setup(float lightRadius,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000163 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
Chris Craik6e068c012016-01-15 16:15:30 -0800164 mLightGeometry.radius = lightRadius;
Chris Craik98787e62015-11-13 10:55:30 -0800165 mLightInfo.ambientShadowAlpha = ambientShadowAlpha;
166 mLightInfo.spotShadowAlpha = spotShadowAlpha;
Alan Viverette50210d92015-05-14 18:05:36 -0700167}
168
169void CanvasContext::setLightCenter(const Vector3& lightCenter) {
Chris Craik6e068c012016-01-15 16:15:30 -0800170 mLightGeometry.center = lightCenter;
John Reck4f02bf42014-01-03 18:09:17 -0800171}
172
John Reck63a06672014-05-07 13:45:54 -0700173void CanvasContext::setOpaque(bool opaque) {
174 mOpaque = opaque;
175}
176
John Reck8afcc762016-04-13 10:24:06 -0700177bool CanvasContext::makeCurrent() {
178 if (mStopped) return false;
179
Stan Iliev768e3932016-07-08 21:34:52 -0400180 auto result = mRenderPipeline->makeCurrent();
181 switch (result) {
182 case MakeCurrentResult::AlreadyCurrent:
183 return true;
184 case MakeCurrentResult::Failed:
185 mHaveNewSurface = true;
186 setSurface(nullptr);
187 return false;
188 case MakeCurrentResult::Succeeded:
189 mHaveNewSurface = true;
190 return true;
191 default:
192 LOG_ALWAYS_FATAL("unexpected result %d from IRenderPipeline::makeCurrent",
193 (int32_t) result);
John Reckf2dcc2a2015-07-16 09:17:59 -0700194 }
Stan Iliev768e3932016-07-08 21:34:52 -0400195
196 return true;
John Reck860d1552014-04-11 19:15:05 -0700197}
198
John Reckbf3c6022015-06-02 15:55:00 -0700199static bool wasSkipped(FrameInfo* info) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700200 return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
John Reckbf3c6022015-06-02 15:55:00 -0700201}
202
John Reck0def73a2016-07-01 16:19:13 -0700203bool CanvasContext::isSwapChainStuffed() {
204 if (mSwapHistory.size() != mSwapHistory.capacity()) {
205 // We want at least 3 frames of history before attempting to
206 // guess if the queue is stuffed
207 return false;
208 }
209 nsecs_t frameInterval = mRenderThread.timeLord().frameIntervalNanos();
210 auto& swapA = mSwapHistory[0];
211
212 // Was there a happy queue & dequeue time? If so, don't
213 // consider it stuffed
214 if (swapA.dequeueDuration < 3_ms
215 && swapA.queueDuration < 3_ms) {
216 return false;
217 }
218
219 for (size_t i = 1; i < mSwapHistory.size(); i++) {
220 auto& swapB = mSwapHistory[i];
221
Chris Craik31635682016-07-19 17:59:12 -0700222 // If there's a multi-frameInterval gap we effectively already dropped a frame,
John Reck0def73a2016-07-01 16:19:13 -0700223 // so consider the queue healthy.
Chris Craik31635682016-07-19 17:59:12 -0700224 if (swapA.swapCompletedTime - swapB.swapCompletedTime > frameInterval * 3) {
John Reck0def73a2016-07-01 16:19:13 -0700225 return false;
226 }
227
228 // Was there a happy queue & dequeue time? If so, don't
229 // consider it stuffed
230 if (swapB.dequeueDuration < 3_ms
231 && swapB.queueDuration < 3_ms) {
232 return false;
233 }
234
235 swapA = swapB;
236 }
237
238 // All signs point to a stuffed swap chain
Tim Murrayffde62742016-07-18 14:11:28 -0700239 ATRACE_NAME("swap chain stuffed");
John Reck0def73a2016-07-01 16:19:13 -0700240 return true;
241}
242
Skuhneea7a7fb2015-08-28 07:10:31 -0700243void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
244 int64_t syncQueued, RenderNode* target) {
John Reckf9be7792014-05-02 18:21:16 -0700245 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700246
John Reckbf3c6022015-06-02 15:55:00 -0700247 // If the previous frame was dropped we don't need to hold onto it, so
248 // just keep using the previous frame's structure instead
249 if (!wasSkipped(mCurrentFrameInfo)) {
250 mCurrentFrameInfo = &mFrames.next();
251 }
John Reckba6adf62015-02-19 14:36:50 -0800252 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
John Reckbe3fba02015-07-06 13:49:58 -0700253 mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
John Reckba6adf62015-02-19 14:36:50 -0800254 mCurrentFrameInfo->markSyncStart();
255
John Recke4267ea2014-06-03 15:53:15 -0700256 info.damageAccumulator = &mDamageAccumulator;
Chris Craik0b7e8242015-10-28 16:50:44 -0700257 info.layerUpdateQueue = &mLayerUpdateQueue;
John Reck00e79c92015-07-21 10:23:59 -0700258
John Reckec845a22014-09-05 15:23:38 -0700259 mAnimationContext->startFrame(info.mode);
Skuhneea7a7fb2015-08-28 07:10:31 -0700260 for (const sp<RenderNode>& node : mRenderNodes) {
261 // Only the primary target node will be drawn full - all other nodes would get drawn in
262 // real time mode. In case of a window, the primary node is the window content and the other
263 // node(s) are non client / filler nodes.
264 info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY);
265 node->prepareTree(info);
John Reck975591a2016-01-22 16:28:07 -0800266 GL_CHECKPOINT(MODERATE);
Skuhneea7a7fb2015-08-28 07:10:31 -0700267 }
John Reck119907c2014-08-14 09:02:01 -0700268 mAnimationContext->runRemainingAnimations(info);
John Reck975591a2016-01-22 16:28:07 -0800269 GL_CHECKPOINT(MODERATE);
John Recke45b1fd2014-04-15 09:50:16 -0700270
John Reck51f2d602016-04-06 07:50:47 -0700271 freePrefetchedLayers(info.observer);
John Reck975591a2016-01-22 16:28:07 -0800272 GL_CHECKPOINT(MODERATE);
John Reck998a6d82014-08-28 15:35:53 -0700273
John Reck306f3312016-06-10 16:01:55 -0700274 mIsDirty = true;
275
John Reckf6481082016-02-02 15:18:23 -0800276 if (CC_UNLIKELY(!mNativeSurface.get())) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700277 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaa95a882014-11-07 11:02:07 -0800278 info.out.canDrawThisFrame = false;
279 return;
280 }
281
John Reckf1480762016-07-03 18:28:25 -0700282 if (CC_LIKELY(mSwapHistory.size() && !Properties::forceDrawFrame)) {
John Recke486d932015-10-28 09:21:19 -0700283 nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
John Reck0def73a2016-07-01 16:19:13 -0700284 SwapHistory& lastSwap = mSwapHistory.back();
285 int durationUs;
286 mNativeSurface->query(NATIVE_WINDOW_LAST_DEQUEUE_DURATION, &durationUs);
287 lastSwap.dequeueDuration = us2ns(durationUs);
288 mNativeSurface->query(NATIVE_WINDOW_LAST_QUEUE_DURATION, &durationUs);
289 lastSwap.queueDuration = us2ns(durationUs);
John Reck52b783f2015-11-24 11:12:55 -0800290 nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
John Recke486d932015-10-28 09:21:19 -0700291 // The slight fudge-factor is to deal with cases where
292 // the vsync was estimated due to being slow handling the signal.
293 // See the logic in TimeLord#computeFrameTimeNanos or in
294 // Choreographer.java for details on when this happens
295 if (vsyncDelta < 2_ms) {
296 // Already drew for this vsync pulse, UI draw request missed
297 // the deadline for RT animations
298 info.out.canDrawThisFrame = false;
Chris Craik31635682016-07-19 17:59:12 -0700299 } else if (vsyncDelta >= mRenderThread.timeLord().frameIntervalNanos() * 3
300 || (latestVsync - mLastDropVsync) < 500_ms) {
301 // It's been several frame intervals, assume the buffer queue is fine
302 // or the last drop was too recent
John Recke486d932015-10-28 09:21:19 -0700303 info.out.canDrawThisFrame = true;
304 } else {
John Reck0def73a2016-07-01 16:19:13 -0700305 info.out.canDrawThisFrame = !isSwapChainStuffed();
Chris Craik31635682016-07-19 17:59:12 -0700306 if (!info.out.canDrawThisFrame) {
307 // dropping frame
308 mLastDropVsync = mRenderThread.timeLord().latestVsync();
309 }
John Recke486d932015-10-28 09:21:19 -0700310 }
311 } else {
312 info.out.canDrawThisFrame = true;
313 }
John Recka5dda642014-05-22 15:43:54 -0700314
John Reckaef9dc82015-05-08 14:10:57 -0700315 if (!info.out.canDrawThisFrame) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700316 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaef9dc82015-05-08 14:10:57 -0700317 }
318
John Recka5dda642014-05-22 15:43:54 -0700319 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700320 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700321 // If animationsNeedsRedraw is set don't bother posting for an RT anim
322 // as we will just end up fighting the UI thread.
323 mRenderThread.postFrameCallback(this);
324 }
John Recke45b1fd2014-04-15 09:50:16 -0700325 }
326}
327
John Reckf47a5942014-06-30 16:20:04 -0700328void CanvasContext::stopDrawing() {
329 mRenderThread.removeFrameCallback(this);
Doris Liu67ce99b2016-05-17 16:50:31 -0700330 mAnimationContext->detachAnimators();
John Reckf47a5942014-06-30 16:20:04 -0700331}
332
John Recka5dda642014-05-22 15:43:54 -0700333void CanvasContext::notifyFramePending() {
334 ATRACE_CALL();
335 mRenderThread.pushBackFrameCallback(this);
336}
337
John Recke4267ea2014-06-03 15:53:15 -0700338void CanvasContext::draw() {
John Recke4267ea2014-06-03 15:53:15 -0700339 SkRect dirty;
340 mDamageAccumulator.finish(&dirty);
341
John Reck6d4d0db2015-08-03 15:34:52 -0700342 // TODO: Re-enable after figuring out cause of b/22592975
343// if (dirty.isEmpty() && Properties::skipEmptyFrames) {
344// mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
345// return;
346// }
John Reck240ff622015-04-28 13:50:00 -0700347
John Reck240ff622015-04-28 13:50:00 -0700348 mCurrentFrameInfo->markIssueDrawCommandsStart();
349
Stan Iliev768e3932016-07-08 21:34:52 -0400350 Frame frame = mRenderPipeline->getFrame();
Chris Craikb565df12015-10-05 13:00:52 -0700351
Stan Iliev768e3932016-07-08 21:34:52 -0400352 SkRect windowDirty = computeDirtyRect(frame, &dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800353
Stan Iliev768e3932016-07-08 21:34:52 -0400354 bool drew = mRenderPipeline->draw(frame, windowDirty, dirty, mLightGeometry, &mLayerUpdateQueue,
355 mContentDrawBounds, mOpaque, mLightInfo, mRenderNodes, &(profiler()));
Chris Craik1dfa0702016-03-04 15:59:24 -0800356
John Reck38f6c032016-03-17 10:23:49 -0700357 waitOnFences();
358
Stan Iliev768e3932016-07-08 21:34:52 -0400359 bool requireSwap = false;
360 bool didSwap = mRenderPipeline->swapBuffers(frame, drew, windowDirty, mCurrentFrameInfo,
361 &requireSwap);
John Reck9372ac32016-01-19 11:46:52 -0800362
John Reck306f3312016-06-10 16:01:55 -0700363 mIsDirty = false;
John Reckba6adf62015-02-19 14:36:50 -0800364
Stan Iliev768e3932016-07-08 21:34:52 -0400365 if (requireSwap) {
366 if (!didSwap) { //some error happened
John Reck149173d2015-08-10 09:52:29 -0700367 setSurface(nullptr);
368 }
John Recke486d932015-10-28 09:21:19 -0700369 SwapHistory& swap = mSwapHistory.next();
Stan Iliev768e3932016-07-08 21:34:52 -0400370 swap.damage = windowDirty;
John Reck0def73a2016-07-01 16:19:13 -0700371 swap.swapCompletedTime = systemTime(CLOCK_MONOTONIC);
John Recke486d932015-10-28 09:21:19 -0700372 swap.vsyncTime = mRenderThread.timeLord().latestVsync();
John Reck149173d2015-08-10 09:52:29 -0700373 mHaveNewSurface = false;
John Reck28912a52016-04-18 14:34:18 -0700374 mFrameNumber = -1;
John Reck4f02bf42014-01-03 18:09:17 -0800375 }
John Reckfe5e7b72014-05-23 17:42:28 -0700376
John Reckba6adf62015-02-19 14:36:50 -0800377 // TODO: Use a fence for real completion?
378 mCurrentFrameInfo->markFrameCompleted();
John Reck149173d2015-08-10 09:52:29 -0700379
380#if LOG_FRAMETIME_MMA
381 float thisFrame = mCurrentFrameInfo->duration(
382 FrameInfoIndex::IssueDrawCommandsStart,
383 FrameInfoIndex::FrameCompleted) / NANOS_PER_MILLIS_F;
384 if (sFrameCount) {
385 sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
386 } else {
387 sBenchMma = thisFrame;
388 }
389 if (++sFrameCount == 10) {
390 sFrameCount = 1;
391 ALOGD("Average frame time: %.4f", sBenchMma);
392 }
393#endif
394
John Reckedc524c2015-03-18 15:24:33 -0700395 mJankTracker.addFrame(*mCurrentFrameInfo);
John Reckba6adf62015-02-19 14:36:50 -0800396 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
Andres Morales910beb82016-02-02 16:19:40 -0800397 if (CC_UNLIKELY(mFrameMetricsReporter.get() != nullptr)) {
398 mFrameMetricsReporter->reportFrameMetrics(mCurrentFrameInfo->data());
Andres Morales06f5bc72015-12-15 15:21:31 -0800399 }
John Reck38e0c322015-11-10 12:19:17 -0800400
401 GpuMemoryTracker::onFrameCompleted();
John Reck4f02bf42014-01-03 18:09:17 -0800402}
403
John Recke45b1fd2014-04-15 09:50:16 -0700404// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700405void CanvasContext::doFrame() {
Stan Iliev768e3932016-07-08 21:34:52 -0400406 if (!mRenderPipeline->isSurfaceReady()) return;
Skuhneea7a7fb2015-08-28 07:10:31 -0700407 prepareAndDraw(nullptr);
408}
John Reck368cdd82014-05-07 13:11:00 -0700409
Skuhneea7a7fb2015-08-28 07:10:31 -0700410void CanvasContext::prepareAndDraw(RenderNode* node) {
John Recke45b1fd2014-04-15 09:50:16 -0700411 ATRACE_CALL();
412
Matthew Bouyack7f667e72016-01-12 12:01:48 -0800413 nsecs_t vsync = mRenderThread.timeLord().computeFrameTimeNanos();
John Reckba6adf62015-02-19 14:36:50 -0800414 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
415 UiFrameInfoBuilder(frameInfo)
Chris Craik1b54fb22015-06-02 17:40:58 -0700416 .addFlag(FrameInfoFlags::RTAnimation)
Matthew Bouyack7f667e72016-01-12 12:01:48 -0800417 .setVsync(vsync, vsync);
John Reckfe5e7b72014-05-23 17:42:28 -0700418
Chris Craike2e53a72015-10-28 15:55:40 -0700419 TreeInfo info(TreeInfo::MODE_RT_ONLY, *this);
Skuhneea7a7fb2015-08-28 07:10:31 -0700420 prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC), node);
John Recka5dda642014-05-22 15:43:54 -0700421 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700422 draw();
John Recka5dda642014-05-22 15:43:54 -0700423 }
John Recke45b1fd2014-04-15 09:50:16 -0700424}
425
John Reck3b202512014-06-23 13:13:08 -0700426void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700427 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700428 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700429 if (thread.eglManager().hasEglContext()) {
John Reck0d1f6342014-03-28 20:30:27 -0700430 mode = DrawGlInfo::kModeProcess;
431 }
John Reck6f07a0d2014-04-16 21:31:25 -0700432
Chris Craikd41c4d82015-01-05 15:51:13 -0800433 thread.renderState().invokeFunctor(functor, mode, nullptr);
John Reck23b797a2014-01-03 18:08:34 -0800434}
435
John Reck998a6d82014-08-28 15:35:53 -0700436void CanvasContext::markLayerInUse(RenderNode* node) {
John Reck51f2d602016-04-06 07:50:47 -0700437 if (mPrefetchedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800438 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700439 }
440}
441
John Reck51f2d602016-04-06 07:50:47 -0700442void CanvasContext::freePrefetchedLayers(TreeObserver* observer) {
443 if (mPrefetchedLayers.size()) {
444 for (auto& node : mPrefetchedLayers) {
445 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...",
446 node->getName());
447 node->destroyHardwareResources(observer);
448 node->decStrong(observer);
449 }
450 mPrefetchedLayers.clear();
John Reck998a6d82014-08-28 15:35:53 -0700451 }
452}
453
John Reck51f2d602016-04-06 07:50:47 -0700454void CanvasContext::buildLayer(RenderNode* node, TreeObserver* observer) {
John Reck3e824952014-08-20 10:08:39 -0700455 ATRACE_CALL();
Stan Iliev768e3932016-07-08 21:34:52 -0400456 if (!mRenderPipeline->isContextReady()) return;
Chris Craik6246d2782016-03-29 15:01:41 -0700457
John Reck3e824952014-08-20 10:08:39 -0700458 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
459 stopDrawing();
460
Chris Craike2e53a72015-10-28 15:55:40 -0700461 TreeInfo info(TreeInfo::MODE_FULL, *this);
John Reck3e824952014-08-20 10:08:39 -0700462 info.damageAccumulator = &mDamageAccumulator;
John Reck51f2d602016-04-06 07:50:47 -0700463 info.observer = observer;
Chris Craik0b7e8242015-10-28 16:50:44 -0700464 info.layerUpdateQueue = &mLayerUpdateQueue;
John Reck9eb9f6f2014-08-21 11:23:05 -0700465 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700466 node->prepareTree(info);
467 SkRect ignore;
468 mDamageAccumulator.finish(&ignore);
469 // Tickle the GENERIC property on node to mark it as dirty for damaging
470 // purposes when the frame is actually drawn
471 node->setPropertyFieldsDirty(RenderNode::GENERIC);
472
Stan Iliev768e3932016-07-08 21:34:52 -0400473 mRenderPipeline->renderLayers(mLightGeometry, &mLayerUpdateQueue, mOpaque, mLightInfo);
John Reck998a6d82014-08-28 15:35:53 -0700474
Chris Craikd41c4d82015-01-05 15:51:13 -0800475 node->incStrong(nullptr);
John Reck51f2d602016-04-06 07:50:47 -0700476 mPrefetchedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700477}
478
John Reck19b6bcf2014-02-14 20:03:38 -0800479bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
Stan Iliev768e3932016-07-08 21:34:52 -0400480 return mRenderPipeline->copyLayerInto(layer, bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800481}
482
John Reck51f2d602016-04-06 07:50:47 -0700483void CanvasContext::destroyHardwareResources(TreeObserver* observer) {
John Reckf47a5942014-06-30 16:20:04 -0700484 stopDrawing();
Stan Iliev768e3932016-07-08 21:34:52 -0400485 if (mRenderPipeline->isContextReady()) {
John Reck51f2d602016-04-06 07:50:47 -0700486 freePrefetchedLayers(observer);
Skuhneea7a7fb2015-08-28 07:10:31 -0700487 for (const sp<RenderNode>& node : mRenderNodes) {
John Reck51f2d602016-04-06 07:50:47 -0700488 node->destroyHardwareResources(observer);
Skuhneea7a7fb2015-08-28 07:10:31 -0700489 }
Stan Iliev768e3932016-07-08 21:34:52 -0400490 mRenderPipeline->onDestroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700491 }
492}
493
494void CanvasContext::trimMemory(RenderThread& thread, int level) {
495 // No context means nothing to free
496 if (!thread.eglManager().hasEglContext()) return;
497
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200498 ATRACE_CALL();
John Reckf47a5942014-06-30 16:20:04 -0700499 if (level >= TRIM_MEMORY_COMPLETE) {
Chris Craik9fded232015-11-11 16:42:34 -0800500 thread.renderState().flush(Caches::FlushMode::Full);
John Reckf47a5942014-06-30 16:20:04 -0700501 thread.eglManager().destroy();
502 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
Chris Craik9fded232015-11-11 16:42:34 -0800503 thread.renderState().flush(Caches::FlushMode::Moderate);
John Recke1628b72014-05-23 15:11:19 -0700504 }
505}
506
John Reck1949e792014-04-08 15:18:56 -0700507Layer* CanvasContext::createTextureLayer() {
Stan Iliev768e3932016-07-08 21:34:52 -0400508 return mRenderPipeline->createTextureLayer();
John Reck1949e792014-04-08 15:18:56 -0700509}
510
John Reck3b202512014-06-23 13:13:08 -0700511void CanvasContext::setTextureAtlas(RenderThread& thread,
512 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
513 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700514}
515
John Reckba6adf62015-02-19 14:36:50 -0800516void CanvasContext::dumpFrames(int fd) {
517 FILE* file = fdopen(fd, "a");
John Reck4db3d172015-06-02 15:58:43 -0700518 fprintf(file, "\n\n---PROFILEDATA---\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700519 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck2a8bb052015-06-03 09:52:01 -0700520 fprintf(file, "%s", FrameInfoNames[i].c_str());
John Reck4db3d172015-06-02 15:58:43 -0700521 fprintf(file, ",");
522 }
John Reckba6adf62015-02-19 14:36:50 -0800523 for (size_t i = 0; i < mFrames.size(); i++) {
524 FrameInfo& frame = mFrames[i];
Chris Craik1b54fb22015-06-02 17:40:58 -0700525 if (frame[FrameInfoIndex::SyncStart] == 0) {
John Reckba6adf62015-02-19 14:36:50 -0800526 continue;
527 }
528 fprintf(file, "\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700529 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800530 fprintf(file, "%" PRId64 ",", frame[i]);
531 }
532 }
533 fprintf(file, "\n---PROFILEDATA---\n\n");
534 fflush(file);
535}
536
537void CanvasContext::resetFrameStats() {
538 mFrames.clear();
539 mRenderThread.jankTracker().reset();
540}
541
John Recke248bd12015-08-05 13:53:53 -0700542void CanvasContext::serializeDisplayListTree() {
543#if ENABLE_RENDERNODE_SERIALIZATION
544 using namespace google::protobuf::io;
545 char package[128];
546 // Check whether tracing is enabled for this process.
547 FILE * file = fopen("/proc/self/cmdline", "r");
548 if (file) {
549 if (!fgets(package, 128, file)) {
550 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
551 fclose(file);
552 return;
553 }
554 fclose(file);
555 } else {
556 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
557 errno);
558 return;
559 }
560 char path[1024];
561 snprintf(path, 1024, "/data/data/%s/cache/rendertree_dump", package);
562 int fd = open(path, O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH);
563 if (fd == -1) {
564 ALOGD("Failed to open '%s'", path);
565 return;
566 }
567 proto::RenderNode tree;
568 // TODO: Streaming writes?
569 mRootRenderNode->copyTo(&tree);
570 std::string data = tree.SerializeAsString();
571 write(fd, data.c_str(), data.length());
572 close(fd);
573#endif
574}
575
John Reck38f6c032016-03-17 10:23:49 -0700576void CanvasContext::waitOnFences() {
577 if (mFrameFences.size()) {
578 ATRACE_CALL();
579 for (auto& fence : mFrameFences) {
580 fence->getResult();
581 }
582 mFrameFences.clear();
583 }
584}
585
586class CanvasContext::FuncTaskProcessor : public TaskProcessor<bool> {
587public:
Stan Iliev768e3932016-07-08 21:34:52 -0400588 explicit FuncTaskProcessor(TaskManager* taskManager)
589 : TaskProcessor<bool>(taskManager) {}
John Reck38f6c032016-03-17 10:23:49 -0700590
591 virtual void onProcess(const sp<Task<bool> >& task) override {
592 FuncTask* t = static_cast<FuncTask*>(task.get());
593 t->func();
594 task->setResult(true);
595 }
596};
597
598void CanvasContext::enqueueFrameWork(std::function<void()>&& func) {
599 if (!mFrameWorkProcessor.get()) {
Stan Iliev768e3932016-07-08 21:34:52 -0400600 mFrameWorkProcessor = new FuncTaskProcessor(mRenderPipeline->getTaskManager());
John Reck38f6c032016-03-17 10:23:49 -0700601 }
602 sp<FuncTask> task(new FuncTask());
603 task->func = func;
John Reck7b570de2016-06-27 13:27:23 -0700604 mFrameFences.push_back(task);
John Reck38f6c032016-03-17 10:23:49 -0700605 mFrameWorkProcessor->add(task);
606}
607
John Reck28912a52016-04-18 14:34:18 -0700608int64_t CanvasContext::getFrameNumber() {
609 // mFrameNumber is reset to -1 when the surface changes or we swap buffers
610 if (mFrameNumber == -1 && mNativeSurface.get()) {
611 mFrameNumber = static_cast<int64_t>(mNativeSurface->getNextFrameNumber());
612 }
613 return mFrameNumber;
614}
615
Stan Iliev768e3932016-07-08 21:34:52 -0400616SkRect CanvasContext::computeDirtyRect(const Frame& frame, SkRect* dirty) {
617 if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) {
618 // can't rely on prior content of window if viewport size changes
619 dirty->setEmpty();
620 mLastFrameWidth = frame.width();
621 mLastFrameHeight = frame.height();
622 } else if (mHaveNewSurface || frame.bufferAge() == 0) {
623 // New surface needs a full draw
624 dirty->setEmpty();
625 } else {
626 if (!dirty->isEmpty() && !dirty->intersect(0, 0, frame.width(), frame.height())) {
627 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
628 SK_RECT_ARGS(*dirty), frame.width(), frame.height());
629 dirty->setEmpty();
630 }
631 profiler().unionDirty(dirty);
632 }
633
634 if (dirty->isEmpty()) {
635 dirty->set(0, 0, frame.width(), frame.height());
636 }
637
638 // At this point dirty is the area of the window to update. However,
639 // the area of the frame we need to repaint is potentially different, so
640 // stash the screen area for later
641 SkRect windowDirty(*dirty);
642
643 // If the buffer age is 0 we do a full-screen repaint (handled above)
644 // If the buffer age is 1 the buffer contents are the same as they were
645 // last frame so there's nothing to union() against
646 // Therefore we only care about the > 1 case.
647 if (frame.bufferAge() > 1) {
648 if (frame.bufferAge() > (int) mSwapHistory.size()) {
649 // We don't have enough history to handle this old of a buffer
650 // Just do a full-draw
651 dirty->set(0, 0, frame.width(), frame.height());
652 } else {
653 // At this point we haven't yet added the latest frame
654 // to the damage history (happens below)
655 // So we need to damage
656 for (int i = mSwapHistory.size() - 1;
657 i > ((int) mSwapHistory.size()) - frame.bufferAge(); i--) {
658 dirty->join(mSwapHistory[i].damage);
659 }
660 }
661 }
662
663 return windowDirty;
664}
665
John Reck23b797a2014-01-03 18:08:34 -0800666} /* namespace renderthread */
667} /* namespace uirenderer */
668} /* namespace android */