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 #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 "Clock.h"
26 #include "FrameTimeline/FrameTimeline.h"
27 
28 namespace android {
29 
30 class Layer;
31 class SurfaceFlinger;
32 
33 class FpsReporter : public IBinder::DeathRecipient {
34 public:
35     FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger,
36                 std::unique_ptr<Clock> clock = std::make_unique<SteadyClock>());
37 
38     // Dispatches updated layer fps values for the registered listeners
39     // This method promotes Layer weak pointers and performs layer stack traversals, so mStateLock
40     // must be held when calling this method.
41     void dispatchLayerFps() EXCLUDES(mMutex);
42 
43     // Override for IBinder::DeathRecipient
44     void binderDied(const wp<IBinder>&) override;
45 
46     // Registers an Fps listener that listens to fps updates for the provided layer
47     void addListener(const sp<gui::IFpsListener>& listener, int32_t taskId);
48     // Deregisters an Fps listener
49     void removeListener(const sp<gui::IFpsListener>& listener);
50 
51 private:
52     mutable std::mutex mMutex;
53     struct WpHash {
operatorWpHash54         size_t operator()(const wp<IBinder>& p) const {
55             return std::hash<IBinder*>()(p.unsafe_get());
56         }
57     };
58 
59     struct TrackedListener {
60         sp<gui::IFpsListener> listener;
61         int32_t taskId;
62     };
63 
64     frametimeline::FrameTimeline& mFrameTimeline;
65     SurfaceFlinger& mFlinger;
66     static const constexpr std::chrono::steady_clock::duration kMinDispatchDuration =
67             std::chrono::milliseconds(500);
68     std::unique_ptr<Clock> mClock;
69     std::chrono::steady_clock::time_point mLastDispatch;
70     std::unordered_map<wp<IBinder>, TrackedListener, WpHash> mListeners GUARDED_BY(mMutex);
71 };
72 
73 } // namespace android