Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 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 | |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 21 | #include <algorithm> |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 22 | |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 23 | #include "FpsReporter.h" |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 24 | #include "Layer.h" |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 25 | #include "SurfaceFlinger.h" |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 29 | FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline, std::unique_ptr<Clock> clock) |
| 30 | : mFrameTimeline(frameTimeline), mClock(std::move(clock)) { |
Alec Mouri | 07b27ce | 2021-04-26 16:31:44 -0700 | [diff] [blame] | 31 | LOG_ALWAYS_FATAL_IF(mClock == nullptr, "Passed in null clock when constructing FpsReporter!"); |
| 32 | } |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 33 | |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 34 | void FpsReporter::dispatchLayerFps(const frontend::LayerHierarchy& layerHierarchy) { |
Alec Mouri | 07b27ce | 2021-04-26 16:31:44 -0700 | [diff] [blame] | 35 | const auto now = mClock->now(); |
| 36 | if (now - mLastDispatch < kMinDispatchDuration) { |
| 37 | return; |
| 38 | } |
| 39 | |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 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 | |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 53 | std::unordered_set<int32_t> seenTasks; |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 54 | std::vector<std::pair<TrackedListener, const frontend::LayerHierarchy*>> |
| 55 | listenersAndLayersToReport; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 56 | |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 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); |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 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); |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 70 | listenersAndLayersToReport.push_back({listener, &hierarchy}); |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 75 | } |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 76 | return true; |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 77 | }); |
| 78 | |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 79 | for (const auto& [listener, hierarchy] : listenersAndLayersToReport) { |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 80 | std::unordered_set<int32_t> layerIds; |
| 81 | |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame] | 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 | }); |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 90 | |
| 91 | listener.listener->onFpsReported(mFrameTimeline.computeFps(layerIds)); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 92 | } |
Alec Mouri | 07b27ce | 2021-04-26 16:31:44 -0700 | [diff] [blame] | 93 | |
| 94 | mLastDispatch = now; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void FpsReporter::binderDied(const wp<IBinder>& who) { |
| 98 | std::scoped_lock lock(mMutex); |
| 99 | mListeners.erase(who); |
| 100 | } |
| 101 | |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 102 | void FpsReporter::addListener(const sp<gui::IFpsListener>& listener, int32_t taskId) { |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 103 | sp<IBinder> asBinder = IInterface::asBinder(listener); |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 104 | asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this)); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 105 | std::lock_guard lock(mMutex); |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 106 | mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, taskId}); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 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 | |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 114 | } // namespace android |