blob: 155cf4da5853edf1d59e20901c181f9c425d6fed [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
Alec Mouri07b27ce2021-04-26 16:31:44 -070029FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger,
30 std::unique_ptr<Clock> clock)
31 : mFrameTimeline(frameTimeline), mFlinger(flinger), mClock(std::move(clock)) {
32 LOG_ALWAYS_FATAL_IF(mClock == nullptr, "Passed in null clock when constructing FpsReporter!");
33}
Alec Mouriadebf5c2021-01-05 12:57:36 -080034
Alec Mouri07b27ce2021-04-26 16:31:44 -070035void FpsReporter::dispatchLayerFps() {
36 const auto now = mClock->now();
37 if (now - mLastDispatch < kMinDispatchDuration) {
38 return;
39 }
40
Alec Mouriadebf5c2021-01-05 12:57:36 -080041 std::vector<TrackedListener> localListeners;
42 {
43 std::scoped_lock lock(mMutex);
44 if (mListeners.empty()) {
45 return;
46 }
47
48 std::transform(mListeners.begin(), mListeners.end(), std::back_inserter(localListeners),
49 [](const std::pair<wp<IBinder>, TrackedListener>& entry) {
50 return entry.second;
51 });
52 }
53
Alec Mouria9a68a62021-03-04 19:14:50 -080054 std::unordered_set<int32_t> seenTasks;
55 std::vector<std::pair<TrackedListener, sp<Layer>>> listenersAndLayersToReport;
Alec Mouriadebf5c2021-01-05 12:57:36 -080056
Alec Mouria9a68a62021-03-04 19:14:50 -080057 mFlinger.mCurrentState.traverse([&](Layer* layer) {
Robert Carr6a160312021-05-17 12:08:20 -070058 auto& currentState = layer->getDrawingState();
Huihong Luod3d8f8e2022-03-08 14:48:46 -080059 if (currentState.metadata.has(gui::METADATA_TASK_ID)) {
60 int32_t taskId = currentState.metadata.getInt32(gui::METADATA_TASK_ID, 0);
Alec Mouria9a68a62021-03-04 19:14:50 -080061 if (seenTasks.count(taskId) == 0) {
62 // localListeners is expected to be tiny
63 for (TrackedListener& listener : localListeners) {
64 if (listener.taskId == taskId) {
65 seenTasks.insert(taskId);
Ady Abrahamd11bade2022-08-01 16:18:03 -070066 listenersAndLayersToReport.push_back(
67 {listener, sp<Layer>::fromExisting(layer)});
Alec Mouria9a68a62021-03-04 19:14:50 -080068 break;
69 }
70 }
71 }
Alec Mouriadebf5c2021-01-05 12:57:36 -080072 }
Alec Mouria9a68a62021-03-04 19:14:50 -080073 });
74
75 for (const auto& [listener, layer] : listenersAndLayersToReport) {
76 std::unordered_set<int32_t> layerIds;
77
78 layer->traverse(LayerVector::StateSet::Current,
79 [&](Layer* layer) { layerIds.insert(layer->getSequence()); });
80
81 listener.listener->onFpsReported(mFrameTimeline.computeFps(layerIds));
Alec Mouriadebf5c2021-01-05 12:57:36 -080082 }
Alec Mouri07b27ce2021-04-26 16:31:44 -070083
84 mLastDispatch = now;
Alec Mouriadebf5c2021-01-05 12:57:36 -080085}
86
87void FpsReporter::binderDied(const wp<IBinder>& who) {
88 std::scoped_lock lock(mMutex);
89 mListeners.erase(who);
90}
91
Alec Mouria9a68a62021-03-04 19:14:50 -080092void FpsReporter::addListener(const sp<gui::IFpsListener>& listener, int32_t taskId) {
Alec Mouriadebf5c2021-01-05 12:57:36 -080093 sp<IBinder> asBinder = IInterface::asBinder(listener);
Ady Abrahamd11bade2022-08-01 16:18:03 -070094 asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this));
Alec Mouriadebf5c2021-01-05 12:57:36 -080095 std::lock_guard lock(mMutex);
Alec Mouria9a68a62021-03-04 19:14:50 -080096 mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, taskId});
Alec Mouriadebf5c2021-01-05 12:57:36 -080097}
98
99void FpsReporter::removeListener(const sp<gui::IFpsListener>& listener) {
100 std::lock_guard lock(mMutex);
101 mListeners.erase(wp<IBinder>(IInterface::asBinder(listener)));
102}
103
Robert Carr6a160312021-05-17 12:08:20 -0700104} // namespace android