1 /*
2 * Copyright 2021 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 #undef LOG_TAG
18 #define LOG_TAG "FpsReporter"
19 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20
21 #include <algorithm>
22
23 #include "FpsReporter.h"
24 #include "Layer.h"
25 #include "SurfaceFlinger.h"
26
27 namespace android {
28
FpsReporter(frametimeline::FrameTimeline & frameTimeline,std::unique_ptr<Clock> clock)29 FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline, std::unique_ptr<Clock> clock)
30 : mFrameTimeline(frameTimeline), mClock(std::move(clock)) {
31 LOG_ALWAYS_FATAL_IF(mClock == nullptr, "Passed in null clock when constructing FpsReporter!");
32 }
33
dispatchLayerFps(const frontend::LayerHierarchy & layerHierarchy)34 void FpsReporter::dispatchLayerFps(const frontend::LayerHierarchy& layerHierarchy) {
35 const auto now = mClock->now();
36 if (now - mLastDispatch < kMinDispatchDuration) {
37 return;
38 }
39
40 std::vector<TrackedListener> localListeners;
41 {
42 std::scoped_lock lock(mMutex);
43 if (mListeners.empty()) {
44 return;
45 }
46
47 std::transform(mListeners.begin(), mListeners.end(), std::back_inserter(localListeners),
48 [](const std::pair<wp<IBinder>, TrackedListener>& entry) {
49 return entry.second;
50 });
51 }
52
53 std::unordered_set<int32_t> seenTasks;
54 std::vector<std::pair<TrackedListener, const frontend::LayerHierarchy*>>
55 listenersAndLayersToReport;
56
57 layerHierarchy.traverse([&](const frontend::LayerHierarchy& hierarchy,
58 const frontend::LayerHierarchy::TraversalPath& traversalPath) {
59 if (traversalPath.variant == frontend::LayerHierarchy::Variant::Detached) {
60 return false;
61 }
62 const auto& metadata = hierarchy.getLayer()->metadata;
63 if (metadata.has(gui::METADATA_TASK_ID)) {
64 int32_t taskId = metadata.getInt32(gui::METADATA_TASK_ID, 0);
65 if (seenTasks.count(taskId) == 0) {
66 // localListeners is expected to be tiny
67 for (TrackedListener& listener : localListeners) {
68 if (listener.taskId == taskId) {
69 seenTasks.insert(taskId);
70 listenersAndLayersToReport.push_back({listener, &hierarchy});
71 break;
72 }
73 }
74 }
75 }
76 return true;
77 });
78
79 for (const auto& [listener, hierarchy] : listenersAndLayersToReport) {
80 std::unordered_set<int32_t> layerIds;
81
82 hierarchy->traverse([&](const frontend::LayerHierarchy& hierarchy,
83 const frontend::LayerHierarchy::TraversalPath& traversalPath) {
84 if (traversalPath.variant == frontend::LayerHierarchy::Variant::Detached) {
85 return false;
86 }
87 layerIds.insert(static_cast<int32_t>(hierarchy.getLayer()->id));
88 return true;
89 });
90
91 listener.listener->onFpsReported(mFrameTimeline.computeFps(layerIds));
92 }
93
94 mLastDispatch = now;
95 }
96
binderDied(const wp<IBinder> & who)97 void FpsReporter::binderDied(const wp<IBinder>& who) {
98 std::scoped_lock lock(mMutex);
99 mListeners.erase(who);
100 }
101
addListener(const sp<gui::IFpsListener> & listener,int32_t taskId)102 void FpsReporter::addListener(const sp<gui::IFpsListener>& listener, int32_t taskId) {
103 sp<IBinder> asBinder = IInterface::asBinder(listener);
104 asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this));
105 std::lock_guard lock(mMutex);
106 mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, taskId});
107 }
108
removeListener(const sp<gui::IFpsListener> & listener)109 void FpsReporter::removeListener(const sp<gui::IFpsListener>& listener) {
110 std::lock_guard lock(mMutex);
111 mListeners.erase(wp<IBinder>(IInterface::asBinder(listener)));
112 }
113
114 } // namespace android
115