blob: 1cec295a31c111da199c72bade5571610710f5ba [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#pragma once
18
19#include <android-base/thread_annotations.h>
20#include <android/gui/IFpsListener.h>
21#include <binder/IBinder.h>
22
23#include <unordered_map>
24
25#include "FrameTimeline/FrameTimeline.h"
26
27namespace android {
28
29class Layer;
Alec Mouria9a68a62021-03-04 19:14:50 -080030class SurfaceFlinger;
Alec Mouriadebf5c2021-01-05 12:57:36 -080031
32class FpsReporter : public IBinder::DeathRecipient {
33public:
Alec Mouria9a68a62021-03-04 19:14:50 -080034 FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger);
Alec Mouriadebf5c2021-01-05 12:57:36 -080035
36 // Dispatches updated layer fps values for the registered listeners
37 // This method promotes Layer weak pointers and performs layer stack traversals, so mStateLock
38 // must be held when calling this method.
39 void dispatchLayerFps() const EXCLUDES(mMutex);
40
41 // Override for IBinder::DeathRecipient
42 void binderDied(const wp<IBinder>&) override;
43
44 // Registers an Fps listener that listens to fps updates for the provided layer
Alec Mouria9a68a62021-03-04 19:14:50 -080045 void addListener(const sp<gui::IFpsListener>& listener, int32_t taskId);
Alec Mouriadebf5c2021-01-05 12:57:36 -080046 // Deregisters an Fps listener
47 void removeListener(const sp<gui::IFpsListener>& listener);
48
49private:
50 mutable std::mutex mMutex;
51 struct WpHash {
52 size_t operator()(const wp<IBinder>& p) const {
53 return std::hash<IBinder*>()(p.unsafe_get());
54 }
55 };
56
57 struct TrackedListener {
58 sp<gui::IFpsListener> listener;
Alec Mouria9a68a62021-03-04 19:14:50 -080059 int32_t taskId;
Alec Mouriadebf5c2021-01-05 12:57:36 -080060 };
61
62 frametimeline::FrameTimeline& mFrameTimeline;
Alec Mouria9a68a62021-03-04 19:14:50 -080063 SurfaceFlinger& mFlinger;
Alec Mouriadebf5c2021-01-05 12:57:36 -080064 std::unordered_map<wp<IBinder>, TrackedListener, WpHash> mListeners GUARDED_BY(mMutex);
65};
66
67} // namespace android