| 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 |  | 
| Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 29 | FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger) | 
|  | 30 | : mFrameTimeline(frameTimeline), mFlinger(flinger) {} | 
| Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 31 |  | 
|  | 32 | void FpsReporter::dispatchLayerFps() const { | 
|  | 33 | std::vector<TrackedListener> localListeners; | 
|  | 34 | { | 
|  | 35 | std::scoped_lock lock(mMutex); | 
|  | 36 | if (mListeners.empty()) { | 
|  | 37 | return; | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | std::transform(mListeners.begin(), mListeners.end(), std::back_inserter(localListeners), | 
|  | 41 | [](const std::pair<wp<IBinder>, TrackedListener>& entry) { | 
|  | 42 | return entry.second; | 
|  | 43 | }); | 
|  | 44 | } | 
|  | 45 |  | 
| Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 46 | std::unordered_set<int32_t> seenTasks; | 
|  | 47 | std::vector<std::pair<TrackedListener, sp<Layer>>> listenersAndLayersToReport; | 
| Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 48 |  | 
| Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 49 | mFlinger.mCurrentState.traverse([&](Layer* layer) { | 
|  | 50 | auto& currentState = layer->getCurrentState(); | 
|  | 51 | if (currentState.metadata.has(METADATA_TASK_ID)) { | 
|  | 52 | int32_t taskId = currentState.metadata.getInt32(METADATA_TASK_ID, 0); | 
|  | 53 | if (seenTasks.count(taskId) == 0) { | 
|  | 54 | // localListeners is expected to be tiny | 
|  | 55 | for (TrackedListener& listener : localListeners) { | 
|  | 56 | if (listener.taskId == taskId) { | 
|  | 57 | seenTasks.insert(taskId); | 
|  | 58 | listenersAndLayersToReport.push_back({listener, sp<Layer>(layer)}); | 
|  | 59 | break; | 
|  | 60 | } | 
|  | 61 | } | 
|  | 62 | } | 
| Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 63 | } | 
| Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 64 | }); | 
|  | 65 |  | 
|  | 66 | for (const auto& [listener, layer] : listenersAndLayersToReport) { | 
|  | 67 | std::unordered_set<int32_t> layerIds; | 
|  | 68 |  | 
|  | 69 | layer->traverse(LayerVector::StateSet::Current, | 
|  | 70 | [&](Layer* layer) { layerIds.insert(layer->getSequence()); }); | 
|  | 71 |  | 
|  | 72 | listener.listener->onFpsReported(mFrameTimeline.computeFps(layerIds)); | 
| Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 73 | } | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | void FpsReporter::binderDied(const wp<IBinder>& who) { | 
|  | 77 | std::scoped_lock lock(mMutex); | 
|  | 78 | mListeners.erase(who); | 
|  | 79 | } | 
|  | 80 |  | 
| Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 81 | void FpsReporter::addListener(const sp<gui::IFpsListener>& listener, int32_t taskId) { | 
| Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 82 | sp<IBinder> asBinder = IInterface::asBinder(listener); | 
|  | 83 | asBinder->linkToDeath(this); | 
|  | 84 | std::lock_guard lock(mMutex); | 
| Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 85 | mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, taskId}); | 
| Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 | void FpsReporter::removeListener(const sp<gui::IFpsListener>& listener) { | 
|  | 89 | std::lock_guard lock(mMutex); | 
|  | 90 | mListeners.erase(wp<IBinder>(IInterface::asBinder(listener))); | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | } // namespace android |