blob: 03deb0ac213b343b720f297001b1b6b7f556f3b7 [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"
John Reck3b202512014-06-23 13:13:08 -070022#include "EglManager.h"
Chris Craik5e00c7c2016-07-06 16:10:09 -070023#include "LayerUpdateQueue.h"
John Reckd04794a2015-05-08 10:04:36 -070024#include "Properties.h"
John Reck4f02bf42014-01-03 18:09:17 -080025#include "RenderThread.h"
sergeyvdccca442016-03-21 15:38:21 -070026#include "hwui/Canvas.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"
Stan Iliev768e3932016-07-08 21:34:52 -040030#include "OpenGLPipeline.h"
John Reck9372ac32016-01-19 11:46:52 -080031#include "utils/GLUtils.h"
John Recke486d932015-10-28 09:21:19 -070032#include "utils/TimeUtils.h"
John Recke248bd12015-08-05 13:53:53 -070033
34#include <cutils/properties.h>
35#include <google/protobuf/io/zero_copy_stream_impl.h>
36#include <private/hwui/DrawGlInfo.h>
37#include <strings.h>
John Reck23b797a2014-01-03 18:08:34 -080038
Chris Craik65fe5ee2015-01-26 18:06:29 -080039#include <algorithm>
John Recke248bd12015-08-05 13:53:53 -070040#include <fcntl.h>
41#include <sys/stat.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080042
Colin Cross290b23a2015-11-05 14:10:47 -080043#include <cstdlib>
44
John Reckf47a5942014-06-30 16:20:04 -070045#define TRIM_MEMORY_COMPLETE 80
46#define TRIM_MEMORY_UI_HIDDEN 20
47
John Recke248bd12015-08-05 13:53:53 -070048#define ENABLE_RENDERNODE_SERIALIZATION false
49
John Reck149173d2015-08-10 09:52:29 -070050#define LOG_FRAMETIME_MMA 0
51
52#if LOG_FRAMETIME_MMA
53static float sBenchMma = 0;
54static int sFrameCount = 0;
55static const float NANOS_PER_MILLIS_F = 1000000.0f;
56#endif
57
John Reck23b797a2014-01-03 18:08:34 -080058namespace android {
59namespace uirenderer {
60namespace renderthread {
61
Stan Iliev03de0742016-07-07 12:35:54 -040062CanvasContext* CanvasContext::create(RenderThread& thread,
63 bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory) {
64
65 auto renderType = Properties::getRenderPipelineType();
Stan Iliev768e3932016-07-08 21:34:52 -040066
Stan Iliev03de0742016-07-07 12:35:54 -040067 switch (renderType) {
68 case RenderPipelineType::OpenGL:
Stan Iliev768e3932016-07-08 21:34:52 -040069 return new CanvasContext(thread, translucent, rootRenderNode, contextFactory,
70 std::make_unique<OpenGLPipeline>(thread));
Stan Iliev03de0742016-07-07 12:35:54 -040071 case RenderPipelineType::SkiaGL:
72 //TODO: implement SKIA GL
73 LOG_ALWAYS_FATAL("skiaGL canvas type not implemented.");
74 break;
Stan Iliev8a33e402016-07-08 09:57:49 -040075 case RenderPipelineType::SkiaVulkan:
Stan Iliev03de0742016-07-07 12:35:54 -040076 //TODO: implement Vulkan
77 LOG_ALWAYS_FATAL("Vulkan canvas type not implemented.");
78 break;
79 default:
80 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType);
81 break;
82 }
83 return nullptr;
84}
85
Derek Sollenberger6a21ca52016-09-28 13:39:55 -040086void CanvasContext::destroyLayer(RenderNode* node) {
87 auto renderType = Properties::getRenderPipelineType();
88 switch (renderType) {
89 case RenderPipelineType::OpenGL:
90 OpenGLPipeline::destroyLayer(node);
91 break;
92 default:
93 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType);
94 break;
95 }
96}
97
Derek Sollenbergerdaf72292016-10-25 12:09:18 -040098void CanvasContext::invokeFunctor(const RenderThread& thread, Functor* functor) {
99 ATRACE_CALL();
100 auto renderType = Properties::getRenderPipelineType();
101 switch (renderType) {
102 case RenderPipelineType::OpenGL:
103 OpenGLPipeline::invokeFunctor(thread, functor);
104 break;
105 default:
106 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType);
107 break;
108 }
109}
110
111void CanvasContext::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
112 auto renderType = Properties::getRenderPipelineType();
113 switch (renderType) {
114 case RenderPipelineType::OpenGL:
115 OpenGLPipeline::prepareToDraw(thread, bitmap);
116 break;
117 default:
118 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType);
119 break;
120 }
121}
122
John Reck119907c2014-08-14 09:02:01 -0700123CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
Stan Iliev768e3932016-07-08 21:34:52 -0400124 RenderNode* rootRenderNode, IContextFactory* contextFactory,
125 std::unique_ptr<IRenderPipeline> renderPipeline)
John Reck3b202512014-06-23 13:13:08 -0700126 : mRenderThread(thread)
John Reck4f02bf42014-01-03 18:09:17 -0800127 , mOpaque(!translucent)
Chris Craik51d6a3d2014-12-22 17:16:56 -0800128 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
John Reck2d5b8d72016-07-28 15:36:11 -0700129 , mJankTracker(thread.mainDisplayInfo())
Skuhneea7a7fb2015-08-28 07:10:31 -0700130 , mProfiler(mFrames)
Stan Iliev768e3932016-07-08 21:34:52 -0400131 , mContentDrawBounds(0, 0, 0, 0)
132 , mRenderPipeline(std::move(renderPipeline)) {
Skuhneea7a7fb2015-08-28 07:10:31 -0700133 mRenderNodes.emplace_back(rootRenderNode);
John Reck443a7142014-09-04 17:40:05 -0700134 mRenderThread.renderState().registerCanvasContext(this);
John Reckb36016c2015-03-11 08:50:53 -0700135 mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
John Reck23b797a2014-01-03 18:08:34 -0800136}
137
138CanvasContext::~CanvasContext() {
John Reck51f2d602016-04-06 07:50:47 -0700139 destroy(nullptr);
John Reck443a7142014-09-04 17:40:05 -0700140 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -0800141}
142
John Reck51f2d602016-04-06 07:50:47 -0700143void CanvasContext::destroy(TreeObserver* observer) {
John Reck17035b02014-09-03 07:39:53 -0700144 stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -0800145 setSurface(nullptr);
John Reck51f2d602016-04-06 07:50:47 -0700146 freePrefetchedLayers(observer);
147 destroyHardwareResources(observer);
John Recke2478d42014-09-03 16:46:05 -0700148 mAnimationContext->destroy();
John Reck23b797a2014-01-03 18:08:34 -0800149}
150
John Reckf6481082016-02-02 15:18:23 -0800151void CanvasContext::setSurface(Surface* surface) {
John Reckfbc8df02014-11-14 16:18:41 -0800152 ATRACE_CALL();
153
John Reckf6481082016-02-02 15:18:23 -0800154 mNativeSurface = surface;
John Recka5dda642014-05-22 15:43:54 -0700155
Stan Iliev768e3932016-07-08 21:34:52 -0400156 bool hasSurface = mRenderPipeline->setSurface(surface, mSwapBehavior);
John Reck23b797a2014-01-03 18:08:34 -0800157
John Reck28912a52016-04-18 14:34:18 -0700158 mFrameNumber = -1;
159
Stan Iliev768e3932016-07-08 21:34:52 -0400160 if (hasSurface) {
161 mHaveNewSurface = true;
162 mSwapHistory.clear();
John Reck368cdd82014-05-07 13:11:00 -0700163 } else {
Stan Iliev768e3932016-07-08 21:34:52 -0400164 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -0800165 }
John Reck23b797a2014-01-03 18:08:34 -0800166}
167
John Reck1125d1f2014-10-23 11:02:19 -0700168void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
169 mSwapBehavior = swapBehavior;
170}
171
John Reckf6481082016-02-02 15:18:23 -0800172void CanvasContext::initialize(Surface* surface) {
173 setSurface(surface);
John Reck4f02bf42014-01-03 18:09:17 -0800174}
175
John Reckf6481082016-02-02 15:18:23 -0800176void CanvasContext::updateSurface(Surface* surface) {
177 setSurface(surface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700178}
179
John Reckf6481082016-02-02 15:18:23 -0800180bool CanvasContext::pauseSurface(Surface* surface) {
John Reck01a5ea32014-12-03 13:01:07 -0800181 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800182}
183
John Reck8afcc762016-04-13 10:24:06 -0700184void CanvasContext::setStopped(bool stopped) {
185 if (mStopped != stopped) {
186 mStopped = stopped;
187 if (mStopped) {
188 mRenderThread.removeFrameCallback(this);
Stan Iliev768e3932016-07-08 21:34:52 -0400189 mRenderPipeline->onStop();
John Reck306f3312016-06-10 16:01:55 -0700190 } else if (mIsDirty && hasSurface()) {
191 mRenderThread.postFrameCallback(this);
John Reck8afcc762016-04-13 10:24:06 -0700192 }
193 }
194}
195
John Reckab1080c2016-06-21 16:24:20 -0700196void CanvasContext::setup(float lightRadius,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000197 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
Chris Craik6e068c012016-01-15 16:15:30 -0800198 mLightGeometry.radius = lightRadius;
Chris Craik98787e62015-11-13 10:55:30 -0800199 mLightInfo.ambientShadowAlpha = ambientShadowAlpha;
200 mLightInfo.spotShadowAlpha = spotShadowAlpha;
Alan Viverette50210d92015-05-14 18:05:36 -0700201}
202
203void CanvasContext::setLightCenter(const Vector3& lightCenter) {
Chris Craik6e068c012016-01-15 16:15:30 -0800204 mLightGeometry.center = lightCenter;
John Reck4f02bf42014-01-03 18:09:17 -0800205}
206
John Reck63a06672014-05-07 13:45:54 -0700207void CanvasContext::setOpaque(bool opaque) {
208 mOpaque = opaque;
209}
210
John Reck8afcc762016-04-13 10:24:06 -0700211bool CanvasContext::makeCurrent() {
212 if (mStopped) return false;
213
Stan Iliev768e3932016-07-08 21:34:52 -0400214 auto result = mRenderPipeline->makeCurrent();
215 switch (result) {
216 case MakeCurrentResult::AlreadyCurrent:
217 return true;
218 case MakeCurrentResult::Failed:
219 mHaveNewSurface = true;
220 setSurface(nullptr);
221 return false;
222 case MakeCurrentResult::Succeeded:
223 mHaveNewSurface = true;
224 return true;
225 default:
226 LOG_ALWAYS_FATAL("unexpected result %d from IRenderPipeline::makeCurrent",
227 (int32_t) result);
John Reckf2dcc2a2015-07-16 09:17:59 -0700228 }
Stan Iliev768e3932016-07-08 21:34:52 -0400229
230 return true;
John Reck860d1552014-04-11 19:15:05 -0700231}
232
John Reckbf3c6022015-06-02 15:55:00 -0700233static bool wasSkipped(FrameInfo* info) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700234 return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
John Reckbf3c6022015-06-02 15:55:00 -0700235}
236
John Reck0def73a2016-07-01 16:19:13 -0700237bool CanvasContext::isSwapChainStuffed() {
John Recka3d795a2016-07-27 19:28:05 -0700238 static const auto SLOW_THRESHOLD = 6_ms;
239
John Reck0def73a2016-07-01 16:19:13 -0700240 if (mSwapHistory.size() != mSwapHistory.capacity()) {
241 // We want at least 3 frames of history before attempting to
242 // guess if the queue is stuffed
243 return false;
244 }
245 nsecs_t frameInterval = mRenderThread.timeLord().frameIntervalNanos();
246 auto& swapA = mSwapHistory[0];
247
248 // Was there a happy queue & dequeue time? If so, don't
249 // consider it stuffed
John Recka3d795a2016-07-27 19:28:05 -0700250 if (swapA.dequeueDuration < SLOW_THRESHOLD
251 && swapA.queueDuration < SLOW_THRESHOLD) {
John Reck0def73a2016-07-01 16:19:13 -0700252 return false;
253 }
254
255 for (size_t i = 1; i < mSwapHistory.size(); i++) {
256 auto& swapB = mSwapHistory[i];
257
Chris Craik31635682016-07-19 17:59:12 -0700258 // If there's a multi-frameInterval gap we effectively already dropped a frame,
John Reck0def73a2016-07-01 16:19:13 -0700259 // so consider the queue healthy.
Chris Craik31635682016-07-19 17:59:12 -0700260 if (swapA.swapCompletedTime - swapB.swapCompletedTime > frameInterval * 3) {
John Reck0def73a2016-07-01 16:19:13 -0700261 return false;
262 }
263
264 // Was there a happy queue & dequeue time? If so, don't
265 // consider it stuffed
John Recka3d795a2016-07-27 19:28:05 -0700266 if (swapB.dequeueDuration < SLOW_THRESHOLD
267 && swapB.queueDuration < SLOW_THRESHOLD) {
John Reck0def73a2016-07-01 16:19:13 -0700268 return false;
269 }
270
271 swapA = swapB;
272 }
273
274 // All signs point to a stuffed swap chain
Tim Murrayffde62742016-07-18 14:11:28 -0700275 ATRACE_NAME("swap chain stuffed");
John Reck0def73a2016-07-01 16:19:13 -0700276 return true;
277}
278
Skuhneea7a7fb2015-08-28 07:10:31 -0700279void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
280 int64_t syncQueued, RenderNode* target) {
John Reckf9be7792014-05-02 18:21:16 -0700281 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700282
John Reckbf3c6022015-06-02 15:55:00 -0700283 // If the previous frame was dropped we don't need to hold onto it, so
284 // just keep using the previous frame's structure instead
285 if (!wasSkipped(mCurrentFrameInfo)) {
286 mCurrentFrameInfo = &mFrames.next();
287 }
John Reckba6adf62015-02-19 14:36:50 -0800288 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
John Reckbe3fba02015-07-06 13:49:58 -0700289 mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
John Reckba6adf62015-02-19 14:36:50 -0800290 mCurrentFrameInfo->markSyncStart();
291
John Recke4267ea2014-06-03 15:53:15 -0700292 info.damageAccumulator = &mDamageAccumulator;
Chris Craik0b7e8242015-10-28 16:50:44 -0700293 info.layerUpdateQueue = &mLayerUpdateQueue;
John Reck00e79c92015-07-21 10:23:59 -0700294
John Reckec845a22014-09-05 15:23:38 -0700295 mAnimationContext->startFrame(info.mode);
Skuhneea7a7fb2015-08-28 07:10:31 -0700296 for (const sp<RenderNode>& node : mRenderNodes) {
297 // Only the primary target node will be drawn full - all other nodes would get drawn in
298 // real time mode. In case of a window, the primary node is the window content and the other
299 // node(s) are non client / filler nodes.
300 info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY);
301 node->prepareTree(info);
John Reck975591a2016-01-22 16:28:07 -0800302 GL_CHECKPOINT(MODERATE);
Skuhneea7a7fb2015-08-28 07:10:31 -0700303 }
John Reck119907c2014-08-14 09:02:01 -0700304 mAnimationContext->runRemainingAnimations(info);
John Reck975591a2016-01-22 16:28:07 -0800305 GL_CHECKPOINT(MODERATE);
John Recke45b1fd2014-04-15 09:50:16 -0700306
John Reck51f2d602016-04-06 07:50:47 -0700307 freePrefetchedLayers(info.observer);
John Reck975591a2016-01-22 16:28:07 -0800308 GL_CHECKPOINT(MODERATE);
John Reck998a6d82014-08-28 15:35:53 -0700309
John Reck306f3312016-06-10 16:01:55 -0700310 mIsDirty = true;
311
John Reckf6481082016-02-02 15:18:23 -0800312 if (CC_UNLIKELY(!mNativeSurface.get())) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700313 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaa95a882014-11-07 11:02:07 -0800314 info.out.canDrawThisFrame = false;
315 return;
316 }
317
John Reckf1480762016-07-03 18:28:25 -0700318 if (CC_LIKELY(mSwapHistory.size() && !Properties::forceDrawFrame)) {
John Recke486d932015-10-28 09:21:19 -0700319 nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
John Reck0def73a2016-07-01 16:19:13 -0700320 SwapHistory& lastSwap = mSwapHistory.back();
John Reck52b783f2015-11-24 11:12:55 -0800321 nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
John Recke486d932015-10-28 09:21:19 -0700322 // The slight fudge-factor is to deal with cases where
323 // the vsync was estimated due to being slow handling the signal.
324 // See the logic in TimeLord#computeFrameTimeNanos or in
325 // Choreographer.java for details on when this happens
326 if (vsyncDelta < 2_ms) {
327 // Already drew for this vsync pulse, UI draw request missed
328 // the deadline for RT animations
329 info.out.canDrawThisFrame = false;
Chris Craik31635682016-07-19 17:59:12 -0700330 } else if (vsyncDelta >= mRenderThread.timeLord().frameIntervalNanos() * 3
331 || (latestVsync - mLastDropVsync) < 500_ms) {
332 // It's been several frame intervals, assume the buffer queue is fine
333 // or the last drop was too recent
John Recke486d932015-10-28 09:21:19 -0700334 info.out.canDrawThisFrame = true;
335 } else {
John Reck0def73a2016-07-01 16:19:13 -0700336 info.out.canDrawThisFrame = !isSwapChainStuffed();
Chris Craik31635682016-07-19 17:59:12 -0700337 if (!info.out.canDrawThisFrame) {
338 // dropping frame
339 mLastDropVsync = mRenderThread.timeLord().latestVsync();
340 }
John Recke486d932015-10-28 09:21:19 -0700341 }
342 } else {
343 info.out.canDrawThisFrame = true;
344 }
John Recka5dda642014-05-22 15:43:54 -0700345
John Reckaef9dc82015-05-08 14:10:57 -0700346 if (!info.out.canDrawThisFrame) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700347 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaef9dc82015-05-08 14:10:57 -0700348 }
349
John Recka5dda642014-05-22 15:43:54 -0700350 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700351 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700352 // If animationsNeedsRedraw is set don't bother posting for an RT anim
353 // as we will just end up fighting the UI thread.
354 mRenderThread.postFrameCallback(this);
355 }
John Recke45b1fd2014-04-15 09:50:16 -0700356 }
357}
358
John Reckf47a5942014-06-30 16:20:04 -0700359void CanvasContext::stopDrawing() {
360 mRenderThread.removeFrameCallback(this);
Doris Liuc82e8792016-07-29 16:45:24 -0700361 mAnimationContext->pauseAnimators();
John Reckf47a5942014-06-30 16:20:04 -0700362}
363
John Recka5dda642014-05-22 15:43:54 -0700364void CanvasContext::notifyFramePending() {
365 ATRACE_CALL();
366 mRenderThread.pushBackFrameCallback(this);
367}
368
John Recke4267ea2014-06-03 15:53:15 -0700369void CanvasContext::draw() {
John Recke4267ea2014-06-03 15:53:15 -0700370 SkRect dirty;
371 mDamageAccumulator.finish(&dirty);
372
John Reck6d4d0db2015-08-03 15:34:52 -0700373 // TODO: Re-enable after figuring out cause of b/22592975
374// if (dirty.isEmpty() && Properties::skipEmptyFrames) {
375// mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
376// return;
377// }
John Reck240ff622015-04-28 13:50:00 -0700378
John Reck240ff622015-04-28 13:50:00 -0700379 mCurrentFrameInfo->markIssueDrawCommandsStart();
380
Stan Iliev768e3932016-07-08 21:34:52 -0400381 Frame frame = mRenderPipeline->getFrame();
Chris Craikb565df12015-10-05 13:00:52 -0700382
Stan Iliev768e3932016-07-08 21:34:52 -0400383 SkRect windowDirty = computeDirtyRect(frame, &dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800384
Stan Iliev768e3932016-07-08 21:34:52 -0400385 bool drew = mRenderPipeline->draw(frame, windowDirty, dirty, mLightGeometry, &mLayerUpdateQueue,
386 mContentDrawBounds, mOpaque, mLightInfo, mRenderNodes, &(profiler()));
Chris Craik1dfa0702016-03-04 15:59:24 -0800387
John Reck38f6c032016-03-17 10:23:49 -0700388 waitOnFences();
389
Stan Iliev768e3932016-07-08 21:34:52 -0400390 bool requireSwap = false;
391 bool didSwap = mRenderPipeline->swapBuffers(frame, drew, windowDirty, mCurrentFrameInfo,
392 &requireSwap);
John Reck9372ac32016-01-19 11:46:52 -0800393
John Reck306f3312016-06-10 16:01:55 -0700394 mIsDirty = false;
John Reckba6adf62015-02-19 14:36:50 -0800395
Stan Iliev768e3932016-07-08 21:34:52 -0400396 if (requireSwap) {
397 if (!didSwap) { //some error happened
John Reck149173d2015-08-10 09:52:29 -0700398 setSurface(nullptr);
399 }
John Recke486d932015-10-28 09:21:19 -0700400 SwapHistory& swap = mSwapHistory.next();
Stan Iliev768e3932016-07-08 21:34:52 -0400401 swap.damage = windowDirty;
John Reck0def73a2016-07-01 16:19:13 -0700402 swap.swapCompletedTime = systemTime(CLOCK_MONOTONIC);
John Recke486d932015-10-28 09:21:19 -0700403 swap.vsyncTime = mRenderThread.timeLord().latestVsync();
John Reck882d5152016-08-01 14:41:08 -0700404 if (mNativeSurface.get()) {
405 int durationUs;
406 mNativeSurface->query(NATIVE_WINDOW_LAST_DEQUEUE_DURATION, &durationUs);
407 swap.dequeueDuration = us2ns(durationUs);
408 mNativeSurface->query(NATIVE_WINDOW_LAST_QUEUE_DURATION, &durationUs);
409 swap.queueDuration = us2ns(durationUs);
410 } else {
411 swap.dequeueDuration = 0;
412 swap.queueDuration = 0;
413 }
John Reck2d5b8d72016-07-28 15:36:11 -0700414 mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration)
415 = swap.dequeueDuration;
416 mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration)
417 = swap.queueDuration;
John Reck149173d2015-08-10 09:52:29 -0700418 mHaveNewSurface = false;
John Reck28912a52016-04-18 14:34:18 -0700419 mFrameNumber = -1;
John Reck70e89c92016-08-05 10:50:36 -0700420 } else {
421 mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = 0;
422 mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = 0;
John Reck4f02bf42014-01-03 18:09:17 -0800423 }
John Reckfe5e7b72014-05-23 17:42:28 -0700424
John Reckba6adf62015-02-19 14:36:50 -0800425 // TODO: Use a fence for real completion?
426 mCurrentFrameInfo->markFrameCompleted();
John Reck149173d2015-08-10 09:52:29 -0700427
428#if LOG_FRAMETIME_MMA
429 float thisFrame = mCurrentFrameInfo->duration(
430 FrameInfoIndex::IssueDrawCommandsStart,
431 FrameInfoIndex::FrameCompleted) / NANOS_PER_MILLIS_F;
432 if (sFrameCount) {
433 sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
434 } else {
435 sBenchMma = thisFrame;
436 }
437 if (++sFrameCount == 10) {
438 sFrameCount = 1;
439 ALOGD("Average frame time: %.4f", sBenchMma);
440 }
441#endif
442
John Reckedc524c2015-03-18 15:24:33 -0700443 mJankTracker.addFrame(*mCurrentFrameInfo);
John Reckba6adf62015-02-19 14:36:50 -0800444 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
Andres Morales910beb82016-02-02 16:19:40 -0800445 if (CC_UNLIKELY(mFrameMetricsReporter.get() != nullptr)) {
446 mFrameMetricsReporter->reportFrameMetrics(mCurrentFrameInfo->data());
Andres Morales06f5bc72015-12-15 15:21:31 -0800447 }
John Reck38e0c322015-11-10 12:19:17 -0800448
449 GpuMemoryTracker::onFrameCompleted();
sergeyvaf102be2016-09-09 18:02:07 -0700450#ifdef BUGREPORT_FONT_CACHE_USAGE
Derek Sollenbergerdaf72292016-10-25 12:09:18 -0400451 auto renderType = Properties::getRenderPipelineType();
452 if (RenderPipelineType::OpenGL == renderType) {
453 Caches& caches = Caches::getInstance();
454 caches.fontRenderer.getFontRenderer().historyTracker().frameCompleted();
455 }
sergeyvaf102be2016-09-09 18:02:07 -0700456#endif
457
John Reck4f02bf42014-01-03 18:09:17 -0800458}
459
John Recke45b1fd2014-04-15 09:50:16 -0700460// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700461void CanvasContext::doFrame() {
Stan Iliev768e3932016-07-08 21:34:52 -0400462 if (!mRenderPipeline->isSurfaceReady()) return;
Skuhneea7a7fb2015-08-28 07:10:31 -0700463 prepareAndDraw(nullptr);
464}
John Reck368cdd82014-05-07 13:11:00 -0700465
Skuhneea7a7fb2015-08-28 07:10:31 -0700466void CanvasContext::prepareAndDraw(RenderNode* node) {
John Recke45b1fd2014-04-15 09:50:16 -0700467 ATRACE_CALL();
468
Matthew Bouyack7f667e72016-01-12 12:01:48 -0800469 nsecs_t vsync = mRenderThread.timeLord().computeFrameTimeNanos();
John Reckba6adf62015-02-19 14:36:50 -0800470 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
471 UiFrameInfoBuilder(frameInfo)
Chris Craik1b54fb22015-06-02 17:40:58 -0700472 .addFlag(FrameInfoFlags::RTAnimation)
Matthew Bouyack7f667e72016-01-12 12:01:48 -0800473 .setVsync(vsync, vsync);
John Reckfe5e7b72014-05-23 17:42:28 -0700474
Chris Craike2e53a72015-10-28 15:55:40 -0700475 TreeInfo info(TreeInfo::MODE_RT_ONLY, *this);
Skuhneea7a7fb2015-08-28 07:10:31 -0700476 prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC), node);
John Recka5dda642014-05-22 15:43:54 -0700477 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700478 draw();
Chris Craik06e2e9c2016-08-31 17:32:46 -0700479 } else {
480 // wait on fences so tasks don't overlap next frame
481 waitOnFences();
John Recka5dda642014-05-22 15:43:54 -0700482 }
John Recke45b1fd2014-04-15 09:50:16 -0700483}
484
John Reck998a6d82014-08-28 15:35:53 -0700485void CanvasContext::markLayerInUse(RenderNode* node) {
John Reck51f2d602016-04-06 07:50:47 -0700486 if (mPrefetchedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800487 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700488 }
489}
490
John Reck51f2d602016-04-06 07:50:47 -0700491void CanvasContext::freePrefetchedLayers(TreeObserver* observer) {
492 if (mPrefetchedLayers.size()) {
493 for (auto& node : mPrefetchedLayers) {
494 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...",
495 node->getName());
496 node->destroyHardwareResources(observer);
497 node->decStrong(observer);
498 }
499 mPrefetchedLayers.clear();
John Reck998a6d82014-08-28 15:35:53 -0700500 }
501}
502
John Reck51f2d602016-04-06 07:50:47 -0700503void CanvasContext::buildLayer(RenderNode* node, TreeObserver* observer) {
John Reck3e824952014-08-20 10:08:39 -0700504 ATRACE_CALL();
Stan Iliev768e3932016-07-08 21:34:52 -0400505 if (!mRenderPipeline->isContextReady()) return;
Chris Craik6246d2782016-03-29 15:01:41 -0700506
John Reck3e824952014-08-20 10:08:39 -0700507 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
508 stopDrawing();
509
Chris Craike2e53a72015-10-28 15:55:40 -0700510 TreeInfo info(TreeInfo::MODE_FULL, *this);
John Reck3e824952014-08-20 10:08:39 -0700511 info.damageAccumulator = &mDamageAccumulator;
John Reck51f2d602016-04-06 07:50:47 -0700512 info.observer = observer;
Chris Craik0b7e8242015-10-28 16:50:44 -0700513 info.layerUpdateQueue = &mLayerUpdateQueue;
John Reck9eb9f6f2014-08-21 11:23:05 -0700514 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700515 node->prepareTree(info);
516 SkRect ignore;
517 mDamageAccumulator.finish(&ignore);
518 // Tickle the GENERIC property on node to mark it as dirty for damaging
519 // purposes when the frame is actually drawn
520 node->setPropertyFieldsDirty(RenderNode::GENERIC);
521
Stan Iliev768e3932016-07-08 21:34:52 -0400522 mRenderPipeline->renderLayers(mLightGeometry, &mLayerUpdateQueue, mOpaque, mLightInfo);
John Reck998a6d82014-08-28 15:35:53 -0700523
Chris Craikd41c4d82015-01-05 15:51:13 -0800524 node->incStrong(nullptr);
John Reck51f2d602016-04-06 07:50:47 -0700525 mPrefetchedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700526}
527
John Reck19b6bcf2014-02-14 20:03:38 -0800528bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
Stan Iliev768e3932016-07-08 21:34:52 -0400529 return mRenderPipeline->copyLayerInto(layer, bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800530}
531
John Reck51f2d602016-04-06 07:50:47 -0700532void CanvasContext::destroyHardwareResources(TreeObserver* observer) {
John Reckf47a5942014-06-30 16:20:04 -0700533 stopDrawing();
Stan Iliev768e3932016-07-08 21:34:52 -0400534 if (mRenderPipeline->isContextReady()) {
John Reck51f2d602016-04-06 07:50:47 -0700535 freePrefetchedLayers(observer);
Skuhneea7a7fb2015-08-28 07:10:31 -0700536 for (const sp<RenderNode>& node : mRenderNodes) {
John Reck51f2d602016-04-06 07:50:47 -0700537 node->destroyHardwareResources(observer);
Skuhneea7a7fb2015-08-28 07:10:31 -0700538 }
Stan Iliev768e3932016-07-08 21:34:52 -0400539 mRenderPipeline->onDestroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700540 }
541}
542
543void CanvasContext::trimMemory(RenderThread& thread, int level) {
544 // No context means nothing to free
545 if (!thread.eglManager().hasEglContext()) return;
546
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200547 ATRACE_CALL();
John Reckf47a5942014-06-30 16:20:04 -0700548 if (level >= TRIM_MEMORY_COMPLETE) {
Chris Craik9fded232015-11-11 16:42:34 -0800549 thread.renderState().flush(Caches::FlushMode::Full);
John Reckf47a5942014-06-30 16:20:04 -0700550 thread.eglManager().destroy();
551 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
Chris Craik9fded232015-11-11 16:42:34 -0800552 thread.renderState().flush(Caches::FlushMode::Moderate);
John Recke1628b72014-05-23 15:11:19 -0700553 }
554}
555
Derek Sollenberger56ad6ec2016-07-22 12:13:32 -0400556DeferredLayerUpdater* CanvasContext::createTextureLayer() {
Stan Iliev768e3932016-07-08 21:34:52 -0400557 return mRenderPipeline->createTextureLayer();
John Reck1949e792014-04-08 15:18:56 -0700558}
559
John Reckba6adf62015-02-19 14:36:50 -0800560void CanvasContext::dumpFrames(int fd) {
561 FILE* file = fdopen(fd, "a");
John Reck4db3d172015-06-02 15:58:43 -0700562 fprintf(file, "\n\n---PROFILEDATA---\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700563 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck2a8bb052015-06-03 09:52:01 -0700564 fprintf(file, "%s", FrameInfoNames[i].c_str());
John Reck4db3d172015-06-02 15:58:43 -0700565 fprintf(file, ",");
566 }
John Reckba6adf62015-02-19 14:36:50 -0800567 for (size_t i = 0; i < mFrames.size(); i++) {
568 FrameInfo& frame = mFrames[i];
Chris Craik1b54fb22015-06-02 17:40:58 -0700569 if (frame[FrameInfoIndex::SyncStart] == 0) {
John Reckba6adf62015-02-19 14:36:50 -0800570 continue;
571 }
572 fprintf(file, "\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700573 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800574 fprintf(file, "%" PRId64 ",", frame[i]);
575 }
576 }
577 fprintf(file, "\n---PROFILEDATA---\n\n");
578 fflush(file);
579}
580
581void CanvasContext::resetFrameStats() {
582 mFrames.clear();
583 mRenderThread.jankTracker().reset();
584}
585
John Recke248bd12015-08-05 13:53:53 -0700586void CanvasContext::serializeDisplayListTree() {
587#if ENABLE_RENDERNODE_SERIALIZATION
588 using namespace google::protobuf::io;
589 char package[128];
590 // Check whether tracing is enabled for this process.
591 FILE * file = fopen("/proc/self/cmdline", "r");
592 if (file) {
593 if (!fgets(package, 128, file)) {
594 ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno);
595 fclose(file);
596 return;
597 }
598 fclose(file);
599 } else {
600 ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno),
601 errno);
602 return;
603 }
604 char path[1024];
605 snprintf(path, 1024, "/data/data/%s/cache/rendertree_dump", package);
606 int fd = open(path, O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH);
607 if (fd == -1) {
608 ALOGD("Failed to open '%s'", path);
609 return;
610 }
611 proto::RenderNode tree;
612 // TODO: Streaming writes?
613 mRootRenderNode->copyTo(&tree);
614 std::string data = tree.SerializeAsString();
615 write(fd, data.c_str(), data.length());
616 close(fd);
617#endif
618}
619
John Reck38f6c032016-03-17 10:23:49 -0700620void CanvasContext::waitOnFences() {
621 if (mFrameFences.size()) {
622 ATRACE_CALL();
623 for (auto& fence : mFrameFences) {
624 fence->getResult();
625 }
626 mFrameFences.clear();
627 }
628}
629
630class CanvasContext::FuncTaskProcessor : public TaskProcessor<bool> {
631public:
Stan Iliev768e3932016-07-08 21:34:52 -0400632 explicit FuncTaskProcessor(TaskManager* taskManager)
633 : TaskProcessor<bool>(taskManager) {}
John Reck38f6c032016-03-17 10:23:49 -0700634
635 virtual void onProcess(const sp<Task<bool> >& task) override {
636 FuncTask* t = static_cast<FuncTask*>(task.get());
637 t->func();
638 task->setResult(true);
639 }
640};
641
642void CanvasContext::enqueueFrameWork(std::function<void()>&& func) {
643 if (!mFrameWorkProcessor.get()) {
Stan Iliev768e3932016-07-08 21:34:52 -0400644 mFrameWorkProcessor = new FuncTaskProcessor(mRenderPipeline->getTaskManager());
John Reck38f6c032016-03-17 10:23:49 -0700645 }
646 sp<FuncTask> task(new FuncTask());
647 task->func = func;
John Reck7b570de2016-06-27 13:27:23 -0700648 mFrameFences.push_back(task);
John Reck38f6c032016-03-17 10:23:49 -0700649 mFrameWorkProcessor->add(task);
650}
651
John Reck28912a52016-04-18 14:34:18 -0700652int64_t CanvasContext::getFrameNumber() {
653 // mFrameNumber is reset to -1 when the surface changes or we swap buffers
654 if (mFrameNumber == -1 && mNativeSurface.get()) {
655 mFrameNumber = static_cast<int64_t>(mNativeSurface->getNextFrameNumber());
656 }
657 return mFrameNumber;
658}
659
Stan Iliev768e3932016-07-08 21:34:52 -0400660SkRect CanvasContext::computeDirtyRect(const Frame& frame, SkRect* dirty) {
661 if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) {
662 // can't rely on prior content of window if viewport size changes
663 dirty->setEmpty();
664 mLastFrameWidth = frame.width();
665 mLastFrameHeight = frame.height();
666 } else if (mHaveNewSurface || frame.bufferAge() == 0) {
667 // New surface needs a full draw
668 dirty->setEmpty();
669 } else {
670 if (!dirty->isEmpty() && !dirty->intersect(0, 0, frame.width(), frame.height())) {
671 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
672 SK_RECT_ARGS(*dirty), frame.width(), frame.height());
673 dirty->setEmpty();
674 }
675 profiler().unionDirty(dirty);
676 }
677
678 if (dirty->isEmpty()) {
679 dirty->set(0, 0, frame.width(), frame.height());
680 }
681
682 // At this point dirty is the area of the window to update. However,
683 // the area of the frame we need to repaint is potentially different, so
684 // stash the screen area for later
685 SkRect windowDirty(*dirty);
686
687 // If the buffer age is 0 we do a full-screen repaint (handled above)
688 // If the buffer age is 1 the buffer contents are the same as they were
689 // last frame so there's nothing to union() against
690 // Therefore we only care about the > 1 case.
691 if (frame.bufferAge() > 1) {
692 if (frame.bufferAge() > (int) mSwapHistory.size()) {
693 // We don't have enough history to handle this old of a buffer
694 // Just do a full-draw
695 dirty->set(0, 0, frame.width(), frame.height());
696 } else {
697 // At this point we haven't yet added the latest frame
698 // to the damage history (happens below)
699 // So we need to damage
700 for (int i = mSwapHistory.size() - 1;
701 i > ((int) mSwapHistory.size()) - frame.bufferAge(); i--) {
702 dirty->join(mSwapHistory[i].damage);
703 }
704 }
705 }
706
707 return windowDirty;
708}
709
John Reck23b797a2014-01-03 18:08:34 -0800710} /* namespace renderthread */
711} /* namespace uirenderer */
712} /* namespace android */