blob: c7dbf88d3280173d82f8a1d3cbe2c2a641d0f0a3 [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
21#include "FpsReporter.h"
22
23#include "Layer.h"
24
25namespace android {
26
27FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline)
28 : mFrameTimeline(frameTimeline) {}
29
30void FpsReporter::dispatchLayerFps() const {
31 std::vector<TrackedListener> localListeners;
32 {
33 std::scoped_lock lock(mMutex);
34 if (mListeners.empty()) {
35 return;
36 }
37
38 std::transform(mListeners.begin(), mListeners.end(), std::back_inserter(localListeners),
39 [](const std::pair<wp<IBinder>, TrackedListener>& entry) {
40 return entry.second;
41 });
42 }
43
44 for (const auto& listener : localListeners) {
45 sp<Layer> promotedLayer = listener.layer.promote();
46 if (promotedLayer != nullptr) {
47 std::unordered_set<int32_t> layerIds;
48
49 promotedLayer->traverse(LayerVector::StateSet::Drawing,
50 [&](Layer* layer) { layerIds.insert(layer->getSequence()); });
51
52 listener.listener->onFpsReported(mFrameTimeline.computeFps(layerIds));
53 }
54 }
55}
56
57void FpsReporter::binderDied(const wp<IBinder>& who) {
58 std::scoped_lock lock(mMutex);
59 mListeners.erase(who);
60}
61
62void FpsReporter::addListener(const sp<gui::IFpsListener>& listener, const wp<Layer>& layer) {
63 sp<IBinder> asBinder = IInterface::asBinder(listener);
64 asBinder->linkToDeath(this);
65 std::lock_guard lock(mMutex);
66 mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, layer});
67}
68
69void FpsReporter::removeListener(const sp<gui::IFpsListener>& listener) {
70 std::lock_guard lock(mMutex);
71 mListeners.erase(wp<IBinder>(IInterface::asBinder(listener)));
72}
73
74} // namespace android