blob: 438b1aa36212e2d13d10079a8fa2130933eef29c [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
Alec Mouri07b27ce2021-04-26 16:31:44 -070025#include "Clock.h"
Alec Mouriadebf5c2021-01-05 12:57:36 -080026#include "FrameTimeline/FrameTimeline.h"
Angel Aguayob398ee22021-10-14 00:39:15 +000027#include "WpHash.h"
Alec Mouriadebf5c2021-01-05 12:57:36 -080028
29namespace android {
30
31class Layer;
Alec Mouria9a68a62021-03-04 19:14:50 -080032class SurfaceFlinger;
Alec Mouriadebf5c2021-01-05 12:57:36 -080033
34class FpsReporter : public IBinder::DeathRecipient {
35public:
Alec Mouri07b27ce2021-04-26 16:31:44 -070036 FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger,
37 std::unique_ptr<Clock> clock = std::make_unique<SteadyClock>());
Alec Mouriadebf5c2021-01-05 12:57:36 -080038
39 // Dispatches updated layer fps values for the registered listeners
40 // This method promotes Layer weak pointers and performs layer stack traversals, so mStateLock
41 // must be held when calling this method.
Alec Mouri07b27ce2021-04-26 16:31:44 -070042 void dispatchLayerFps() EXCLUDES(mMutex);
Alec Mouriadebf5c2021-01-05 12:57:36 -080043
44 // Override for IBinder::DeathRecipient
45 void binderDied(const wp<IBinder>&) override;
46
47 // Registers an Fps listener that listens to fps updates for the provided layer
Alec Mouria9a68a62021-03-04 19:14:50 -080048 void addListener(const sp<gui::IFpsListener>& listener, int32_t taskId);
Alec Mouriadebf5c2021-01-05 12:57:36 -080049 // Deregisters an Fps listener
50 void removeListener(const sp<gui::IFpsListener>& listener);
51
52private:
53 mutable std::mutex mMutex;
Alec Mouriadebf5c2021-01-05 12:57:36 -080054
55 struct TrackedListener {
56 sp<gui::IFpsListener> listener;
Alec Mouria9a68a62021-03-04 19:14:50 -080057 int32_t taskId;
Alec Mouriadebf5c2021-01-05 12:57:36 -080058 };
59
60 frametimeline::FrameTimeline& mFrameTimeline;
Alec Mouria9a68a62021-03-04 19:14:50 -080061 SurfaceFlinger& mFlinger;
Alec Mouri07b27ce2021-04-26 16:31:44 -070062 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 Mouriadebf5c2021-01-05 12:57:36 -080066 std::unordered_map<wp<IBinder>, TrackedListener, WpHash> mListeners GUARDED_BY(mMutex);
67};
68
69} // namespace android