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 | #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 | |
Alec Mouri | 07b27ce | 2021-04-26 16:31:44 -0700 | [diff] [blame] | 25 | #include "Clock.h" |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 26 | #include "FrameTimeline/FrameTimeline.h" |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame^] | 27 | #include "FrontEnd/LayerHierarchy.h" |
Angel Aguayo | b398ee2 | 2021-10-14 00:39:15 +0000 | [diff] [blame] | 28 | #include "WpHash.h" |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | class Layer; |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 33 | class SurfaceFlinger; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 34 | |
| 35 | class FpsReporter : public IBinder::DeathRecipient { |
| 36 | public: |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame^] | 37 | FpsReporter(frametimeline::FrameTimeline& frameTimeline, |
Alec Mouri | 07b27ce | 2021-04-26 16:31:44 -0700 | [diff] [blame] | 38 | std::unique_ptr<Clock> clock = std::make_unique<SteadyClock>()); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 39 | |
| 40 | // Dispatches updated layer fps values for the registered listeners |
| 41 | // This method promotes Layer weak pointers and performs layer stack traversals, so mStateLock |
| 42 | // must be held when calling this method. |
Andy Yu | aef688b | 2024-01-23 17:14:23 -0800 | [diff] [blame^] | 43 | void dispatchLayerFps(const frontend::LayerHierarchy&) EXCLUDES(mMutex); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 44 | |
| 45 | // Override for IBinder::DeathRecipient |
| 46 | void binderDied(const wp<IBinder>&) override; |
| 47 | |
| 48 | // Registers an Fps listener that listens to fps updates for the provided layer |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 49 | void addListener(const sp<gui::IFpsListener>& listener, int32_t taskId); |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 50 | // Deregisters an Fps listener |
| 51 | void removeListener(const sp<gui::IFpsListener>& listener); |
| 52 | |
| 53 | private: |
| 54 | mutable std::mutex mMutex; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 55 | |
| 56 | struct TrackedListener { |
| 57 | sp<gui::IFpsListener> listener; |
Alec Mouri | a9a68a6 | 2021-03-04 19:14:50 -0800 | [diff] [blame] | 58 | int32_t taskId; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | frametimeline::FrameTimeline& mFrameTimeline; |
Alec Mouri | 07b27ce | 2021-04-26 16:31:44 -0700 | [diff] [blame] | 62 | static const constexpr std::chrono::steady_clock::duration kMinDispatchDuration = |
| 63 | std::chrono::milliseconds(500); |
| 64 | std::unique_ptr<Clock> mClock; |
| 65 | std::chrono::steady_clock::time_point mLastDispatch; |
Alec Mouri | adebf5c | 2021-01-05 12:57:36 -0800 | [diff] [blame] | 66 | std::unordered_map<wp<IBinder>, TrackedListener, WpHash> mListeners GUARDED_BY(mMutex); |
| 67 | }; |
| 68 | |
| 69 | } // namespace android |