blob: a47348fbc42828b29147fc936a28c8b052e57825 [file] [log] [blame]
Alec Mouriadebf5c2021-01-05 12:57:36 -08001/*
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
Alec Mouria9a68a62021-03-04 19:14:50 -080021#include <algorithm>
Alec Mouriadebf5c2021-01-05 12:57:36 -080022
Alec Mouria9a68a62021-03-04 19:14:50 -080023#include "FpsReporter.h"
Alec Mouriadebf5c2021-01-05 12:57:36 -080024#include "Layer.h"
Alec Mouria9a68a62021-03-04 19:14:50 -080025#include "SurfaceFlinger.h"
Alec Mouriadebf5c2021-01-05 12:57:36 -080026
27namespace android {
28
Andy Yuaef688b2024-01-23 17:14:23 -080029FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline, std::unique_ptr<Clock> clock)
30 : mFrameTimeline(frameTimeline), mClock(std::move(clock)) {
Alec Mouri07b27ce2021-04-26 16:31:44 -070031 LOG_ALWAYS_FATAL_IF(mClock == nullptr, "Passed in null clock when constructing FpsReporter!");
32}
Alec Mouriadebf5c2021-01-05 12:57:36 -080033
Andy Yuaef688b2024-01-23 17:14:23 -080034void FpsReporter::dispatchLayerFps(const frontend::LayerHierarchy& layerHierarchy) {
Alec Mouri07b27ce2021-04-26 16:31:44 -070035 const auto now = mClock->now();
36 if (now - mLastDispatch < kMinDispatchDuration) {
37 return;
38 }
39
Alec Mouriadebf5c2021-01-05 12:57:36 -080040 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
Alec Mouria9a68a62021-03-04 19:14:50 -080053 std::unordered_set<int32_t> seenTasks;
Andy Yuaef688b2024-01-23 17:14:23 -080054 std::vector<std::pair<TrackedListener, const frontend::LayerHierarchy*>>
55 listenersAndLayersToReport;
Alec Mouriadebf5c2021-01-05 12:57:36 -080056
Andy Yuaef688b2024-01-23 17:14:23 -080057 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);
Alec Mouria9a68a62021-03-04 19:14:50 -080065 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);
Andy Yuaef688b2024-01-23 17:14:23 -080070 listenersAndLayersToReport.push_back({listener, &hierarchy});
Alec Mouria9a68a62021-03-04 19:14:50 -080071 break;
72 }
73 }
74 }
Alec Mouriadebf5c2021-01-05 12:57:36 -080075 }
Andy Yuaef688b2024-01-23 17:14:23 -080076 return true;
Alec Mouria9a68a62021-03-04 19:14:50 -080077 });
78
Andy Yuaef688b2024-01-23 17:14:23 -080079 for (const auto& [listener, hierarchy] : listenersAndLayersToReport) {
Alec Mouria9a68a62021-03-04 19:14:50 -080080 std::unordered_set<int32_t> layerIds;
81
Andy Yuaef688b2024-01-23 17:14:23 -080082 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 });
Alec Mouria9a68a62021-03-04 19:14:50 -080090
91 listener.listener->onFpsReported(mFrameTimeline.computeFps(layerIds));
Alec Mouriadebf5c2021-01-05 12:57:36 -080092 }
Alec Mouri07b27ce2021-04-26 16:31:44 -070093
94 mLastDispatch = now;
Alec Mouriadebf5c2021-01-05 12:57:36 -080095}
96
97void FpsReporter::binderDied(const wp<IBinder>& who) {
98 std::scoped_lock lock(mMutex);
99 mListeners.erase(who);
100}
101
Alec Mouria9a68a62021-03-04 19:14:50 -0800102void FpsReporter::addListener(const sp<gui::IFpsListener>& listener, int32_t taskId) {
Alec Mouriadebf5c2021-01-05 12:57:36 -0800103 sp<IBinder> asBinder = IInterface::asBinder(listener);
Ady Abrahamd11bade2022-08-01 16:18:03 -0700104 asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this));
Alec Mouriadebf5c2021-01-05 12:57:36 -0800105 std::lock_guard lock(mMutex);
Alec Mouria9a68a62021-03-04 19:14:50 -0800106 mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, taskId});
Alec Mouriadebf5c2021-01-05 12:57:36 -0800107}
108
109void FpsReporter::removeListener(const sp<gui::IFpsListener>& listener) {
110 std::lock_guard lock(mMutex);
111 mListeners.erase(wp<IBinder>(IInterface::asBinder(listener)));
112}
113
Robert Carr6a160312021-05-17 12:08:20 -0700114} // namespace android