1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "CanvasContext.h"
18
19 #include <apex/window.h>
20 #include <fcntl.h>
21 #include <gui/TraceUtils.h>
22 #include <strings.h>
23 #include <sys/stat.h>
24 #include <ui/Fence.h>
25
26 #include <algorithm>
27 #include <cstdint>
28 #include <cstdlib>
29 #include <functional>
30
31 #include "../Properties.h"
32 #include "AnimationContext.h"
33 #include "Frame.h"
34 #include "LayerUpdateQueue.h"
35 #include "Properties.h"
36 #include "RenderThread.h"
37 #include "hwui/Canvas.h"
38 #include "pipeline/skia/SkiaOpenGLPipeline.h"
39 #include "pipeline/skia/SkiaPipeline.h"
40 #include "pipeline/skia/SkiaVulkanPipeline.h"
41 #include "thread/CommonPool.h"
42 #include "utils/GLUtils.h"
43 #include "utils/TimeUtils.h"
44
45 #define TRIM_MEMORY_COMPLETE 80
46 #define TRIM_MEMORY_UI_HIDDEN 20
47
48 #define LOG_FRAMETIME_MMA 0
49
50 #if LOG_FRAMETIME_MMA
51 static float sBenchMma = 0;
52 static int sFrameCount = 0;
53 static const float NANOS_PER_MILLIS_F = 1000000.0f;
54 #endif
55
56 namespace android {
57 namespace uirenderer {
58 namespace renderthread {
59
60 namespace {
61 class ScopedActiveContext {
62 public:
ScopedActiveContext(CanvasContext * context)63 ScopedActiveContext(CanvasContext* context) { sActiveContext = context; }
64
~ScopedActiveContext()65 ~ScopedActiveContext() { sActiveContext = nullptr; }
66
getActiveContext()67 static CanvasContext* getActiveContext() { return sActiveContext; }
68
69 private:
70 static CanvasContext* sActiveContext;
71 };
72
73 CanvasContext* ScopedActiveContext::sActiveContext = nullptr;
74 } /* namespace */
75
create(RenderThread & thread,bool translucent,RenderNode * rootRenderNode,IContextFactory * contextFactory)76 CanvasContext* CanvasContext::create(RenderThread& thread, bool translucent,
77 RenderNode* rootRenderNode, IContextFactory* contextFactory) {
78 auto renderType = Properties::getRenderPipelineType();
79
80 switch (renderType) {
81 case RenderPipelineType::SkiaGL:
82 return new CanvasContext(thread, translucent, rootRenderNode, contextFactory,
83 std::make_unique<skiapipeline::SkiaOpenGLPipeline>(thread));
84 case RenderPipelineType::SkiaVulkan:
85 return new CanvasContext(thread, translucent, rootRenderNode, contextFactory,
86 std::make_unique<skiapipeline::SkiaVulkanPipeline>(thread));
87 default:
88 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
89 break;
90 }
91 return nullptr;
92 }
93
invokeFunctor(const RenderThread & thread,Functor * functor)94 void CanvasContext::invokeFunctor(const RenderThread& thread, Functor* functor) {
95 ATRACE_CALL();
96 auto renderType = Properties::getRenderPipelineType();
97 switch (renderType) {
98 case RenderPipelineType::SkiaGL:
99 skiapipeline::SkiaOpenGLPipeline::invokeFunctor(thread, functor);
100 break;
101 case RenderPipelineType::SkiaVulkan:
102 skiapipeline::SkiaVulkanPipeline::invokeFunctor(thread, functor);
103 break;
104 default:
105 LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t)renderType);
106 break;
107 }
108 }
109
prepareToDraw(const RenderThread & thread,Bitmap * bitmap)110 void CanvasContext::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) {
111 skiapipeline::SkiaPipeline::prepareToDraw(thread, bitmap);
112 }
113
CanvasContext(RenderThread & thread,bool translucent,RenderNode * rootRenderNode,IContextFactory * contextFactory,std::unique_ptr<IRenderPipeline> renderPipeline)114 CanvasContext::CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
115 IContextFactory* contextFactory,
116 std::unique_ptr<IRenderPipeline> renderPipeline)
117 : mRenderThread(thread)
118 , mGenerationID(0)
119 , mOpaque(!translucent)
120 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
121 , mJankTracker(&thread.globalProfileData())
122 , mProfiler(mJankTracker.frames(), thread.timeLord().frameIntervalNanos())
123 , mContentDrawBounds(0, 0, 0, 0)
124 , mRenderPipeline(std::move(renderPipeline)) {
125 rootRenderNode->makeRoot();
126 mRenderNodes.emplace_back(rootRenderNode);
127 mProfiler.setDensity(DeviceInfo::getDensity());
128 }
129
~CanvasContext()130 CanvasContext::~CanvasContext() {
131 destroy();
132 for (auto& node : mRenderNodes) {
133 node->clearRoot();
134 }
135 mRenderNodes.clear();
136 }
137
addRenderNode(RenderNode * node,bool placeFront)138 void CanvasContext::addRenderNode(RenderNode* node, bool placeFront) {
139 int pos = placeFront ? 0 : static_cast<int>(mRenderNodes.size());
140 node->makeRoot();
141 mRenderNodes.emplace(mRenderNodes.begin() + pos, node);
142 }
143
removeRenderNode(RenderNode * node)144 void CanvasContext::removeRenderNode(RenderNode* node) {
145 node->clearRoot();
146 mRenderNodes.erase(std::remove(mRenderNodes.begin(), mRenderNodes.end(), node),
147 mRenderNodes.end());
148 }
149
destroy()150 void CanvasContext::destroy() {
151 stopDrawing();
152 setSurface(nullptr);
153 setSurfaceControl(nullptr);
154 freePrefetchedLayers();
155 destroyHardwareResources();
156 mAnimationContext->destroy();
157 }
158
setBufferCount(ANativeWindow * window)159 static void setBufferCount(ANativeWindow* window) {
160 int query_value;
161 int err = window->query(window, NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &query_value);
162 if (err != 0 || query_value < 0) {
163 ALOGE("window->query failed: %s (%d) value=%d", strerror(-err), err, query_value);
164 return;
165 }
166 auto min_undequeued_buffers = static_cast<uint32_t>(query_value);
167
168 // We only need to set min_undequeued + 2 because the renderahead amount was already factored into the
169 // query for min_undequeued
170 int bufferCount = min_undequeued_buffers + 2;
171 native_window_set_buffer_count(window, bufferCount);
172 }
173
setSurface(ANativeWindow * window,bool enableTimeout)174 void CanvasContext::setSurface(ANativeWindow* window, bool enableTimeout) {
175 ATRACE_CALL();
176
177 if (window) {
178 mNativeSurface = std::make_unique<ReliableSurface>(window);
179 mNativeSurface->init();
180 if (enableTimeout) {
181 // TODO: Fix error handling & re-shorten timeout
182 ANativeWindow_setDequeueTimeout(window, 4000_ms);
183 }
184 } else {
185 mNativeSurface = nullptr;
186 }
187 setupPipelineSurface();
188 }
189
setSurfaceControl(ASurfaceControl * surfaceControl)190 void CanvasContext::setSurfaceControl(ASurfaceControl* surfaceControl) {
191 if (surfaceControl == mSurfaceControl) return;
192
193 auto funcs = mRenderThread.getASurfaceControlFunctions();
194
195 if (surfaceControl == nullptr) {
196 setASurfaceTransactionCallback(nullptr);
197 setPrepareSurfaceControlForWebviewCallback(nullptr);
198 }
199
200 if (mSurfaceControl != nullptr) {
201 funcs.unregisterListenerFunc(this, &onSurfaceStatsAvailable);
202 funcs.releaseFunc(mSurfaceControl);
203 }
204 mSurfaceControl = surfaceControl;
205 mSurfaceControlGenerationId++;
206 mExpectSurfaceStats = surfaceControl != nullptr;
207 if (mExpectSurfaceStats) {
208 funcs.acquireFunc(mSurfaceControl);
209 funcs.registerListenerFunc(surfaceControl, mSurfaceControlGenerationId, this,
210 &onSurfaceStatsAvailable);
211 }
212 }
213
setupPipelineSurface()214 void CanvasContext::setupPipelineSurface() {
215 bool hasSurface = mRenderPipeline->setSurface(
216 mNativeSurface ? mNativeSurface->getNativeWindow() : nullptr, mSwapBehavior);
217
218 if (mNativeSurface && !mNativeSurface->didSetExtraBuffers()) {
219 setBufferCount(mNativeSurface->getNativeWindow());
220
221 }
222
223 mFrameNumber = 0;
224
225 if (mNativeSurface != nullptr && hasSurface) {
226 mHaveNewSurface = true;
227 mSwapHistory.clear();
228 // Enable frame stats after the surface has been bound to the appropriate graphics API.
229 // Order is important when new and old surfaces are the same, because old surface has
230 // its frame stats disabled automatically.
231 native_window_enable_frame_timestamps(mNativeSurface->getNativeWindow(), true);
232 } else {
233 mRenderThread.removeFrameCallback(this);
234 mGenerationID++;
235 }
236 }
237
setSwapBehavior(SwapBehavior swapBehavior)238 void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
239 mSwapBehavior = swapBehavior;
240 }
241
pauseSurface()242 bool CanvasContext::pauseSurface() {
243 mGenerationID++;
244 return mRenderThread.removeFrameCallback(this);
245 }
246
setStopped(bool stopped)247 void CanvasContext::setStopped(bool stopped) {
248 if (mStopped != stopped) {
249 mStopped = stopped;
250 if (mStopped) {
251 mGenerationID++;
252 mRenderThread.removeFrameCallback(this);
253 mRenderPipeline->onStop();
254 } else if (mIsDirty && hasSurface()) {
255 mRenderThread.postFrameCallback(this);
256 }
257 }
258 }
259
allocateBuffers()260 void CanvasContext::allocateBuffers() {
261 if (mNativeSurface && Properties::isDrawingEnabled()) {
262 ANativeWindow_tryAllocateBuffers(mNativeSurface->getNativeWindow());
263 }
264 }
265
setLightAlpha(uint8_t ambientShadowAlpha,uint8_t spotShadowAlpha)266 void CanvasContext::setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
267 mLightInfo.ambientShadowAlpha = ambientShadowAlpha;
268 mLightInfo.spotShadowAlpha = spotShadowAlpha;
269 }
270
setLightGeometry(const Vector3 & lightCenter,float lightRadius)271 void CanvasContext::setLightGeometry(const Vector3& lightCenter, float lightRadius) {
272 mLightGeometry.center = lightCenter;
273 mLightGeometry.radius = lightRadius;
274 }
275
setOpaque(bool opaque)276 void CanvasContext::setOpaque(bool opaque) {
277 mOpaque = opaque;
278 }
279
setColorMode(ColorMode mode)280 void CanvasContext::setColorMode(ColorMode mode) {
281 mRenderPipeline->setSurfaceColorProperties(mode);
282 setupPipelineSurface();
283 }
284
makeCurrent()285 bool CanvasContext::makeCurrent() {
286 if (mStopped) return false;
287
288 auto result = mRenderPipeline->makeCurrent();
289 switch (result) {
290 case MakeCurrentResult::AlreadyCurrent:
291 return true;
292 case MakeCurrentResult::Failed:
293 mHaveNewSurface = true;
294 setSurface(nullptr);
295 return false;
296 case MakeCurrentResult::Succeeded:
297 mHaveNewSurface = true;
298 return true;
299 default:
300 LOG_ALWAYS_FATAL("unexpected result %d from IRenderPipeline::makeCurrent",
301 (int32_t)result);
302 }
303
304 return true;
305 }
306
wasSkipped(FrameInfo * info)307 static bool wasSkipped(FrameInfo* info) {
308 return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
309 }
310
isSwapChainStuffed()311 bool CanvasContext::isSwapChainStuffed() {
312 static const auto SLOW_THRESHOLD = 6_ms;
313
314 if (mSwapHistory.size() != mSwapHistory.capacity()) {
315 // We want at least 3 frames of history before attempting to
316 // guess if the queue is stuffed
317 return false;
318 }
319 nsecs_t frameInterval = mRenderThread.timeLord().frameIntervalNanos();
320 auto& swapA = mSwapHistory[0];
321
322 // Was there a happy queue & dequeue time? If so, don't
323 // consider it stuffed
324 if (swapA.dequeueDuration < SLOW_THRESHOLD && swapA.queueDuration < SLOW_THRESHOLD) {
325 return false;
326 }
327
328 for (size_t i = 1; i < mSwapHistory.size(); i++) {
329 auto& swapB = mSwapHistory[i];
330
331 // If there's a multi-frameInterval gap we effectively already dropped a frame,
332 // so consider the queue healthy.
333 if (std::abs(swapA.swapCompletedTime - swapB.swapCompletedTime) > frameInterval * 3) {
334 return false;
335 }
336
337 // Was there a happy queue & dequeue time? If so, don't
338 // consider it stuffed
339 if (swapB.dequeueDuration < SLOW_THRESHOLD && swapB.queueDuration < SLOW_THRESHOLD) {
340 return false;
341 }
342
343 swapA = swapB;
344 }
345
346 // All signs point to a stuffed swap chain
347 ATRACE_NAME("swap chain stuffed");
348 return true;
349 }
350
prepareTree(TreeInfo & info,int64_t * uiFrameInfo,int64_t syncQueued,RenderNode * target)351 void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued,
352 RenderNode* target) {
353 mRenderThread.removeFrameCallback(this);
354
355 // If the previous frame was dropped we don't need to hold onto it, so
356 // just keep using the previous frame's structure instead
357 if (!wasSkipped(mCurrentFrameInfo)) {
358 mCurrentFrameInfo = mJankTracker.startFrame();
359 }
360
361 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
362 mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
363 mCurrentFrameInfo->markSyncStart();
364
365 info.damageAccumulator = &mDamageAccumulator;
366 info.layerUpdateQueue = &mLayerUpdateQueue;
367 info.damageGenerationId = mDamageId++;
368 info.out.canDrawThisFrame = true;
369
370 mAnimationContext->startFrame(info.mode);
371 for (const sp<RenderNode>& node : mRenderNodes) {
372 // Only the primary target node will be drawn full - all other nodes would get drawn in
373 // real time mode. In case of a window, the primary node is the window content and the other
374 // node(s) are non client / filler nodes.
375 info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY);
376 node->prepareTree(info);
377 GL_CHECKPOINT(MODERATE);
378 }
379 mAnimationContext->runRemainingAnimations(info);
380 GL_CHECKPOINT(MODERATE);
381
382 freePrefetchedLayers();
383 GL_CHECKPOINT(MODERATE);
384
385 mIsDirty = true;
386
387 if (CC_UNLIKELY(!hasSurface())) {
388 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
389 info.out.canDrawThisFrame = false;
390 return;
391 }
392
393 if (CC_LIKELY(mSwapHistory.size() && !info.forceDrawFrame)) {
394 nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
395 SwapHistory& lastSwap = mSwapHistory.back();
396 nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
397 // The slight fudge-factor is to deal with cases where
398 // the vsync was estimated due to being slow handling the signal.
399 // See the logic in TimeLord#computeFrameTimeNanos or in
400 // Choreographer.java for details on when this happens
401 if (vsyncDelta < 2_ms) {
402 // Already drew for this vsync pulse, UI draw request missed
403 // the deadline for RT animations
404 info.out.canDrawThisFrame = false;
405 }
406 } else {
407 info.out.canDrawThisFrame = true;
408 }
409
410 // TODO: Do we need to abort out if the backdrop is added but not ready? Should that even
411 // be an allowable combination?
412 if (mRenderNodes.size() > 2 && !mRenderNodes[1]->isRenderable()) {
413 info.out.canDrawThisFrame = false;
414 }
415
416 if (info.out.canDrawThisFrame) {
417 int err = mNativeSurface->reserveNext();
418 if (err != OK) {
419 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
420 info.out.canDrawThisFrame = false;
421 ALOGW("reserveNext failed, error = %d (%s)", err, strerror(-err));
422 if (err != TIMED_OUT) {
423 // A timed out surface can still recover, but assume others are permanently dead.
424 setSurface(nullptr);
425 return;
426 }
427 }
428 } else {
429 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
430 }
431
432 bool postedFrameCallback = false;
433 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
434 if (CC_UNLIKELY(!Properties::enableRTAnimations)) {
435 info.out.requiresUiRedraw = true;
436 }
437 if (!info.out.requiresUiRedraw) {
438 // If animationsNeedsRedraw is set don't bother posting for an RT anim
439 // as we will just end up fighting the UI thread.
440 mRenderThread.postFrameCallback(this);
441 postedFrameCallback = true;
442 }
443 }
444
445 if (!postedFrameCallback &&
446 info.out.animatedImageDelay != TreeInfo::Out::kNoAnimatedImageDelay) {
447 // Subtract the time of one frame so it can be displayed on time.
448 const nsecs_t kFrameTime = mRenderThread.timeLord().frameIntervalNanos();
449 if (info.out.animatedImageDelay <= kFrameTime) {
450 mRenderThread.postFrameCallback(this);
451 } else {
452 const auto delay = info.out.animatedImageDelay - kFrameTime;
453 int genId = mGenerationID;
454 mRenderThread.queue().postDelayed(delay, [this, genId]() {
455 if (mGenerationID == genId) {
456 mRenderThread.postFrameCallback(this);
457 }
458 });
459 }
460 }
461 }
462
stopDrawing()463 void CanvasContext::stopDrawing() {
464 cleanupResources();
465 mRenderThread.removeFrameCallback(this);
466 mAnimationContext->pauseAnimators();
467 mGenerationID++;
468 }
469
notifyFramePending()470 void CanvasContext::notifyFramePending() {
471 ATRACE_CALL();
472 mRenderThread.pushBackFrameCallback(this);
473 }
474
draw()475 nsecs_t CanvasContext::draw() {
476 if (auto grContext = getGrContext()) {
477 if (grContext->abandoned()) {
478 LOG_ALWAYS_FATAL("GrContext is abandoned/device lost at start of CanvasContext::draw");
479 return 0;
480 }
481 }
482 SkRect dirty;
483 mDamageAccumulator.finish(&dirty);
484
485 if (!Properties::isDrawingEnabled() ||
486 (dirty.isEmpty() && Properties::skipEmptyFrames && !surfaceRequiresRedraw())) {
487 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
488 if (auto grContext = getGrContext()) {
489 // Submit to ensure that any texture uploads complete and Skia can
490 // free its staging buffers.
491 grContext->flushAndSubmit();
492 }
493
494 // Notify the callbacks, even if there's nothing to draw so they aren't waiting
495 // indefinitely
496 waitOnFences();
497 for (auto& func : mFrameCommitCallbacks) {
498 std::invoke(func, false /* didProduceBuffer */);
499 }
500 mFrameCommitCallbacks.clear();
501 return 0;
502 }
503
504 ScopedActiveContext activeContext(this);
505 mCurrentFrameInfo->set(FrameInfoIndex::FrameInterval) =
506 mRenderThread.timeLord().frameIntervalNanos();
507
508 mCurrentFrameInfo->markIssueDrawCommandsStart();
509
510 Frame frame = mRenderPipeline->getFrame();
511 SkRect windowDirty = computeDirtyRect(frame, &dirty);
512
513 ATRACE_FORMAT("Drawing " RECT_STRING, SK_RECT_ARGS(dirty));
514
515 IRenderPipeline::DrawResult drawResult;
516 {
517 // FrameInfoVisualizer accesses the frame events, which cannot be mutated mid-draw
518 // or it can lead to memory corruption.
519 // This lock is overly broad, but it's the quickest fix since this mutex is otherwise
520 // not visible to IRenderPipeline much less FrameInfoVisualizer. And since this is
521 // the thread we're primarily concerned about being responsive, this being too broad
522 // shouldn't pose a performance issue.
523 std::scoped_lock lock(mFrameMetricsReporterMutex);
524 drawResult = mRenderPipeline->draw(frame, windowDirty, dirty, mLightGeometry,
525 &mLayerUpdateQueue, mContentDrawBounds, mOpaque,
526 mLightInfo, mRenderNodes, &(profiler()));
527 }
528
529 uint64_t frameCompleteNr = getFrameNumber();
530
531 waitOnFences();
532
533 if (mNativeSurface) {
534 // TODO(b/165985262): measure performance impact
535 const auto vsyncId = mCurrentFrameInfo->get(FrameInfoIndex::FrameTimelineVsyncId);
536 if (vsyncId != UiFrameInfoBuilder::INVALID_VSYNC_ID) {
537 const auto inputEventId =
538 static_cast<int32_t>(mCurrentFrameInfo->get(FrameInfoIndex::InputEventId));
539 native_window_set_frame_timeline_info(
540 mNativeSurface->getNativeWindow(), frameCompleteNr, vsyncId, inputEventId,
541 mCurrentFrameInfo->get(FrameInfoIndex::FrameStartTime));
542 }
543 }
544
545 bool requireSwap = false;
546 int error = OK;
547 bool didSwap = mRenderPipeline->swapBuffers(frame, drawResult.success, windowDirty,
548 mCurrentFrameInfo, &requireSwap);
549
550 mCurrentFrameInfo->set(FrameInfoIndex::CommandSubmissionCompleted) = std::max(
551 drawResult.commandSubmissionTime, mCurrentFrameInfo->get(FrameInfoIndex::SwapBuffers));
552
553 mIsDirty = false;
554
555 if (requireSwap) {
556 bool didDraw = true;
557 // Handle any swapchain errors
558 error = mNativeSurface->getAndClearError();
559 if (error == TIMED_OUT) {
560 // Try again
561 mRenderThread.postFrameCallback(this);
562 // But since this frame didn't happen, we need to mark full damage in the swap
563 // history
564 didDraw = false;
565
566 } else if (error != OK || !didSwap) {
567 // Unknown error, abandon the surface
568 setSurface(nullptr);
569 didDraw = false;
570 }
571
572 SwapHistory& swap = mSwapHistory.next();
573 if (didDraw) {
574 swap.damage = windowDirty;
575 } else {
576 float max = static_cast<float>(INT_MAX);
577 swap.damage = SkRect::MakeWH(max, max);
578 }
579 swap.swapCompletedTime = systemTime(SYSTEM_TIME_MONOTONIC);
580 swap.vsyncTime = mRenderThread.timeLord().latestVsync();
581 if (didDraw) {
582 nsecs_t dequeueStart =
583 ANativeWindow_getLastDequeueStartTime(mNativeSurface->getNativeWindow());
584 if (dequeueStart < mCurrentFrameInfo->get(FrameInfoIndex::SyncStart)) {
585 // Ignoring dequeue duration as it happened prior to frame render start
586 // and thus is not part of the frame.
587 swap.dequeueDuration = 0;
588 } else {
589 swap.dequeueDuration =
590 ANativeWindow_getLastDequeueDuration(mNativeSurface->getNativeWindow());
591 }
592 swap.queueDuration =
593 ANativeWindow_getLastQueueDuration(mNativeSurface->getNativeWindow());
594 } else {
595 swap.dequeueDuration = 0;
596 swap.queueDuration = 0;
597 }
598 mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = swap.dequeueDuration;
599 mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = swap.queueDuration;
600 mHaveNewSurface = false;
601 mFrameNumber = 0;
602 } else {
603 mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = 0;
604 mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = 0;
605 }
606
607 mCurrentFrameInfo->markSwapBuffersCompleted();
608
609 #if LOG_FRAMETIME_MMA
610 float thisFrame = mCurrentFrameInfo->duration(FrameInfoIndex::IssueDrawCommandsStart,
611 FrameInfoIndex::FrameCompleted) /
612 NANOS_PER_MILLIS_F;
613 if (sFrameCount) {
614 sBenchMma = ((9 * sBenchMma) + thisFrame) / 10;
615 } else {
616 sBenchMma = thisFrame;
617 }
618 if (++sFrameCount == 10) {
619 sFrameCount = 1;
620 ALOGD("Average frame time: %.4f", sBenchMma);
621 }
622 #endif
623
624 if (didSwap) {
625 for (auto& func : mFrameCommitCallbacks) {
626 std::invoke(func, true /* didProduceBuffer */);
627 }
628 mFrameCommitCallbacks.clear();
629 }
630
631 if (requireSwap) {
632 if (mExpectSurfaceStats) {
633 reportMetricsWithPresentTime();
634 { // acquire lock
635 std::lock_guard lock(mLast4FrameMetricsInfosMutex);
636 FrameMetricsInfo& next = mLast4FrameMetricsInfos.next();
637 next.frameInfo = mCurrentFrameInfo;
638 next.frameNumber = frameCompleteNr;
639 next.surfaceId = mSurfaceControlGenerationId;
640 } // release lock
641 } else {
642 mCurrentFrameInfo->markFrameCompleted();
643 mCurrentFrameInfo->set(FrameInfoIndex::GpuCompleted)
644 = mCurrentFrameInfo->get(FrameInfoIndex::FrameCompleted);
645 std::scoped_lock lock(mFrameMetricsReporterMutex);
646 mJankTracker.finishFrame(*mCurrentFrameInfo, mFrameMetricsReporter, frameCompleteNr,
647 mSurfaceControlGenerationId);
648 }
649 }
650
651 cleanupResources();
652 mRenderThread.cacheManager().onFrameCompleted();
653 return mCurrentFrameInfo->get(FrameInfoIndex::DequeueBufferDuration);
654 }
655
cleanupResources()656 void CanvasContext::cleanupResources() {
657 auto& tracker = mJankTracker.frames();
658 auto size = tracker.size();
659 auto capacity = tracker.capacity();
660 if (size == capacity) {
661 nsecs_t nowNanos = systemTime(SYSTEM_TIME_MONOTONIC);
662 nsecs_t frameCompleteNanos =
663 tracker[0].get(FrameInfoIndex::FrameCompleted);
664 nsecs_t frameDiffNanos = nowNanos - frameCompleteNanos;
665 nsecs_t cleanupMillis = ns2ms(std::max(frameDiffNanos, 10_s));
666 mRenderThread.cacheManager().performDeferredCleanup(cleanupMillis);
667 }
668 }
669
reportMetricsWithPresentTime()670 void CanvasContext::reportMetricsWithPresentTime() {
671 { // acquire lock
672 std::scoped_lock lock(mFrameMetricsReporterMutex);
673 if (mFrameMetricsReporter == nullptr) {
674 return;
675 }
676 } // release lock
677 if (mNativeSurface == nullptr) {
678 return;
679 }
680 ATRACE_CALL();
681 FrameInfo* forthBehind;
682 int64_t frameNumber;
683 int32_t surfaceControlId;
684
685 { // acquire lock
686 std::scoped_lock lock(mLast4FrameMetricsInfosMutex);
687 if (mLast4FrameMetricsInfos.size() != mLast4FrameMetricsInfos.capacity()) {
688 // Not enough frames yet
689 return;
690 }
691 auto frameMetricsInfo = mLast4FrameMetricsInfos.front();
692 forthBehind = frameMetricsInfo.frameInfo;
693 frameNumber = frameMetricsInfo.frameNumber;
694 surfaceControlId = frameMetricsInfo.surfaceId;
695 } // release lock
696
697 nsecs_t presentTime = 0;
698 native_window_get_frame_timestamps(
699 mNativeSurface->getNativeWindow(), frameNumber, nullptr /*outRequestedPresentTime*/,
700 nullptr /*outAcquireTime*/, nullptr /*outLatchTime*/,
701 nullptr /*outFirstRefreshStartTime*/, nullptr /*outLastRefreshStartTime*/,
702 nullptr /*outGpuCompositionDoneTime*/, &presentTime, nullptr /*outDequeueReadyTime*/,
703 nullptr /*outReleaseTime*/);
704
705 forthBehind->set(FrameInfoIndex::DisplayPresentTime) = presentTime;
706 { // acquire lock
707 std::scoped_lock lock(mFrameMetricsReporterMutex);
708 if (mFrameMetricsReporter != nullptr) {
709 mFrameMetricsReporter->reportFrameMetrics(forthBehind->data(), true /*hasPresentTime*/,
710 frameNumber, surfaceControlId);
711 }
712 } // release lock
713 }
714
addFrameMetricsObserver(FrameMetricsObserver * observer)715 void CanvasContext::addFrameMetricsObserver(FrameMetricsObserver* observer) {
716 std::scoped_lock lock(mFrameMetricsReporterMutex);
717 if (mFrameMetricsReporter.get() == nullptr) {
718 mFrameMetricsReporter.reset(new FrameMetricsReporter());
719 }
720
721 // We want to make sure we aren't reporting frames that have already been queued by the
722 // BufferQueueProducer on the rendner thread but are still pending the callback to report their
723 // their frame metrics.
724 uint64_t nextFrameNumber = getFrameNumber();
725 observer->reportMetricsFrom(nextFrameNumber, mSurfaceControlGenerationId);
726 mFrameMetricsReporter->addObserver(observer);
727 }
728
removeFrameMetricsObserver(FrameMetricsObserver * observer)729 void CanvasContext::removeFrameMetricsObserver(FrameMetricsObserver* observer) {
730 std::scoped_lock lock(mFrameMetricsReporterMutex);
731 if (mFrameMetricsReporter.get() != nullptr) {
732 mFrameMetricsReporter->removeObserver(observer);
733 if (!mFrameMetricsReporter->hasObservers()) {
734 mFrameMetricsReporter.reset(nullptr);
735 }
736 }
737 }
738
getFrameInfoFromLast4(uint64_t frameNumber,uint32_t surfaceControlId)739 FrameInfo* CanvasContext::getFrameInfoFromLast4(uint64_t frameNumber, uint32_t surfaceControlId) {
740 std::scoped_lock lock(mLast4FrameMetricsInfosMutex);
741 for (size_t i = 0; i < mLast4FrameMetricsInfos.size(); i++) {
742 if (mLast4FrameMetricsInfos[i].frameNumber == frameNumber &&
743 mLast4FrameMetricsInfos[i].surfaceId == surfaceControlId) {
744 return mLast4FrameMetricsInfos[i].frameInfo;
745 }
746 }
747
748 return nullptr;
749 }
750
onSurfaceStatsAvailable(void * context,int32_t surfaceControlId,ASurfaceControlStats * stats)751 void CanvasContext::onSurfaceStatsAvailable(void* context, int32_t surfaceControlId,
752 ASurfaceControlStats* stats) {
753 auto* instance = static_cast<CanvasContext*>(context);
754
755 const ASurfaceControlFunctions& functions =
756 instance->mRenderThread.getASurfaceControlFunctions();
757
758 nsecs_t gpuCompleteTime = functions.getAcquireTimeFunc(stats);
759 if (gpuCompleteTime == Fence::SIGNAL_TIME_PENDING) {
760 gpuCompleteTime = -1;
761 }
762 uint64_t frameNumber = functions.getFrameNumberFunc(stats);
763
764 FrameInfo* frameInfo = instance->getFrameInfoFromLast4(frameNumber, surfaceControlId);
765
766 if (frameInfo != nullptr) {
767 std::scoped_lock lock(instance->mFrameMetricsReporterMutex);
768 frameInfo->set(FrameInfoIndex::FrameCompleted) = std::max(gpuCompleteTime,
769 frameInfo->get(FrameInfoIndex::SwapBuffersCompleted));
770 frameInfo->set(FrameInfoIndex::GpuCompleted) = std::max(
771 gpuCompleteTime, frameInfo->get(FrameInfoIndex::CommandSubmissionCompleted));
772 instance->mJankTracker.finishFrame(*frameInfo, instance->mFrameMetricsReporter, frameNumber,
773 surfaceControlId);
774 }
775 }
776
777 // Called by choreographer to do an RT-driven animation
doFrame()778 void CanvasContext::doFrame() {
779 if (!mRenderPipeline->isSurfaceReady()) return;
780 prepareAndDraw(nullptr);
781 }
782
getNextFrameSize() const783 SkISize CanvasContext::getNextFrameSize() const {
784 static constexpr SkISize defaultFrameSize = {INT32_MAX, INT32_MAX};
785 if (mNativeSurface == nullptr) {
786 return defaultFrameSize;
787 }
788 ANativeWindow* anw = mNativeSurface->getNativeWindow();
789
790 SkISize size;
791 size.fWidth = ANativeWindow_getWidth(anw);
792 size.fHeight = ANativeWindow_getHeight(anw);
793 return size;
794 }
795
getPixelSnapMatrix() const796 const SkM44& CanvasContext::getPixelSnapMatrix() const {
797 return mRenderPipeline->getPixelSnapMatrix();
798 }
799
prepareAndDraw(RenderNode * node)800 void CanvasContext::prepareAndDraw(RenderNode* node) {
801 ATRACE_CALL();
802
803 nsecs_t vsync = mRenderThread.timeLord().computeFrameTimeNanos();
804 int64_t vsyncId = mRenderThread.timeLord().lastVsyncId();
805 int64_t frameDeadline = mRenderThread.timeLord().lastFrameDeadline();
806 int64_t frameInterval = mRenderThread.timeLord().frameIntervalNanos();
807 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
808 UiFrameInfoBuilder(frameInfo)
809 .addFlag(FrameInfoFlags::RTAnimation)
810 .setVsync(vsync, vsync, vsyncId, frameDeadline, frameInterval);
811
812 TreeInfo info(TreeInfo::MODE_RT_ONLY, *this);
813 prepareTree(info, frameInfo, systemTime(SYSTEM_TIME_MONOTONIC), node);
814 if (info.out.canDrawThisFrame) {
815 draw();
816 } else {
817 // wait on fences so tasks don't overlap next frame
818 waitOnFences();
819 }
820 }
821
markLayerInUse(RenderNode * node)822 void CanvasContext::markLayerInUse(RenderNode* node) {
823 if (mPrefetchedLayers.erase(node)) {
824 node->decStrong(nullptr);
825 }
826 }
827
freePrefetchedLayers()828 void CanvasContext::freePrefetchedLayers() {
829 if (mPrefetchedLayers.size()) {
830 for (auto& node : mPrefetchedLayers) {
831 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...",
832 node->getName());
833 node->destroyLayers();
834 node->decStrong(nullptr);
835 }
836 mPrefetchedLayers.clear();
837 }
838 }
839
buildLayer(RenderNode * node)840 void CanvasContext::buildLayer(RenderNode* node) {
841 ATRACE_CALL();
842 if (!mRenderPipeline->isContextReady()) return;
843
844 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
845 stopDrawing();
846
847 TreeInfo info(TreeInfo::MODE_FULL, *this);
848 info.damageAccumulator = &mDamageAccumulator;
849 info.layerUpdateQueue = &mLayerUpdateQueue;
850 info.runAnimations = false;
851 node->prepareTree(info);
852 SkRect ignore;
853 mDamageAccumulator.finish(&ignore);
854 // Tickle the GENERIC property on node to mark it as dirty for damaging
855 // purposes when the frame is actually drawn
856 node->setPropertyFieldsDirty(RenderNode::GENERIC);
857
858 mRenderPipeline->renderLayers(mLightGeometry, &mLayerUpdateQueue, mOpaque, mLightInfo);
859
860 node->incStrong(nullptr);
861 mPrefetchedLayers.insert(node);
862 }
863
destroyHardwareResources()864 void CanvasContext::destroyHardwareResources() {
865 stopDrawing();
866 if (mRenderPipeline->isContextReady()) {
867 freePrefetchedLayers();
868 for (const sp<RenderNode>& node : mRenderNodes) {
869 node->destroyHardwareResources();
870 }
871 mRenderPipeline->onDestroyHardwareResources();
872 }
873 }
874
trimMemory(RenderThread & thread,int level)875 void CanvasContext::trimMemory(RenderThread& thread, int level) {
876 ATRACE_CALL();
877 if (!thread.getGrContext()) return;
878 ATRACE_CALL();
879 if (level >= TRIM_MEMORY_COMPLETE) {
880 thread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete);
881 thread.destroyRenderingContext();
882 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
883 thread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::UiHidden);
884 }
885 }
886
createTextureLayer()887 DeferredLayerUpdater* CanvasContext::createTextureLayer() {
888 return mRenderPipeline->createTextureLayer();
889 }
890
dumpFrames(int fd)891 void CanvasContext::dumpFrames(int fd) {
892 mJankTracker.dumpStats(fd);
893 mJankTracker.dumpFrames(fd);
894 }
895
resetFrameStats()896 void CanvasContext::resetFrameStats() {
897 mJankTracker.reset();
898 }
899
setName(const std::string && name)900 void CanvasContext::setName(const std::string&& name) {
901 mJankTracker.setDescription(JankTrackerType::Window, std::move(name));
902 }
903
waitOnFences()904 void CanvasContext::waitOnFences() {
905 if (mFrameFences.size()) {
906 ATRACE_CALL();
907 for (auto& fence : mFrameFences) {
908 fence.get();
909 }
910 mFrameFences.clear();
911 }
912 }
913
enqueueFrameWork(std::function<void ()> && func)914 void CanvasContext::enqueueFrameWork(std::function<void()>&& func) {
915 mFrameFences.push_back(CommonPool::async(std::move(func)));
916 }
917
getFrameNumber()918 uint64_t CanvasContext::getFrameNumber() {
919 // mFrameNumber is reset to 0 when the surface changes or we swap buffers
920 if (mFrameNumber == 0 && mNativeSurface.get()) {
921 mFrameNumber = ANativeWindow_getNextFrameId(mNativeSurface->getNativeWindow());
922 }
923 return mFrameNumber;
924 }
925
surfaceRequiresRedraw()926 bool CanvasContext::surfaceRequiresRedraw() {
927 if (!mNativeSurface) return false;
928 if (mHaveNewSurface) return true;
929
930 ANativeWindow* anw = mNativeSurface->getNativeWindow();
931 const int width = ANativeWindow_getWidth(anw);
932 const int height = ANativeWindow_getHeight(anw);
933
934 return width != mLastFrameWidth || height != mLastFrameHeight;
935 }
936
computeDirtyRect(const Frame & frame,SkRect * dirty)937 SkRect CanvasContext::computeDirtyRect(const Frame& frame, SkRect* dirty) {
938 if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) {
939 // can't rely on prior content of window if viewport size changes
940 dirty->setEmpty();
941 mLastFrameWidth = frame.width();
942 mLastFrameHeight = frame.height();
943 } else if (mHaveNewSurface || frame.bufferAge() == 0) {
944 // New surface needs a full draw
945 dirty->setEmpty();
946 } else {
947 if (!dirty->isEmpty() && !dirty->intersect(SkRect::MakeIWH(frame.width(), frame.height()))) {
948 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?", SK_RECT_ARGS(*dirty),
949 frame.width(), frame.height());
950 dirty->setEmpty();
951 }
952 profiler().unionDirty(dirty);
953 }
954
955 if (dirty->isEmpty()) {
956 dirty->setIWH(frame.width(), frame.height());
957 }
958
959 // At this point dirty is the area of the window to update. However,
960 // the area of the frame we need to repaint is potentially different, so
961 // stash the screen area for later
962 SkRect windowDirty(*dirty);
963
964 // If the buffer age is 0 we do a full-screen repaint (handled above)
965 // If the buffer age is 1 the buffer contents are the same as they were
966 // last frame so there's nothing to union() against
967 // Therefore we only care about the > 1 case.
968 if (frame.bufferAge() > 1) {
969 if (frame.bufferAge() > (int)mSwapHistory.size()) {
970 // We don't have enough history to handle this old of a buffer
971 // Just do a full-draw
972 dirty->setIWH(frame.width(), frame.height());
973 } else {
974 // At this point we haven't yet added the latest frame
975 // to the damage history (happens below)
976 // So we need to damage
977 for (int i = mSwapHistory.size() - 1;
978 i > ((int)mSwapHistory.size()) - frame.bufferAge(); i--) {
979 dirty->join(mSwapHistory[i].damage);
980 }
981 }
982 }
983
984 return windowDirty;
985 }
986
getActiveContext()987 CanvasContext* CanvasContext::getActiveContext() {
988 return ScopedActiveContext::getActiveContext();
989 }
990
mergeTransaction(ASurfaceTransaction * transaction,ASurfaceControl * control)991 bool CanvasContext::mergeTransaction(ASurfaceTransaction* transaction, ASurfaceControl* control) {
992 if (!mASurfaceTransactionCallback) return false;
993 return std::invoke(mASurfaceTransactionCallback, reinterpret_cast<int64_t>(transaction),
994 reinterpret_cast<int64_t>(control), getFrameNumber());
995 }
996
prepareSurfaceControlForWebview()997 void CanvasContext::prepareSurfaceControlForWebview() {
998 if (mPrepareSurfaceControlForWebviewCallback) {
999 std::invoke(mPrepareSurfaceControlForWebviewCallback);
1000 }
1001 }
1002
1003 } /* namespace renderthread */
1004 } /* namespace uirenderer */
1005 } /* namespace android */
1006