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 <optional>
20 #include <unordered_set>
21 
22 #include <android/gui/BnWindowInfosPublisher.h>
23 #include <android/gui/IWindowInfosListener.h>
24 #include <android/gui/IWindowInfosReportedListener.h>
25 #include <binder/IBinder.h>
26 #include <ftl/small_map.h>
27 #include <ftl/small_vector.h>
28 #include <gui/SpHash.h>
29 #include <utils/Mutex.h>
30 
31 #include "scheduler/VsyncId.h"
32 
33 namespace android {
34 
35 using WindowInfosReportedListenerSet =
36         std::unordered_set<sp<gui::IWindowInfosReportedListener>,
37                            gui::SpHash<gui::IWindowInfosReportedListener>>;
38 
39 class WindowInfosListenerInvoker : public gui::BnWindowInfosPublisher,
40                                    public IBinder::DeathRecipient {
41 public:
42     void addWindowInfosListener(sp<gui::IWindowInfosListener>, gui::WindowInfosListenerInfo*);
43     void removeWindowInfosListener(const sp<gui::IWindowInfosListener>& windowInfosListener);
44 
45     void windowInfosChanged(gui::WindowInfosUpdate update,
46                             WindowInfosReportedListenerSet windowInfosReportedListeners,
47                             bool forceImmediateCall);
48 
49     binder::Status ackWindowInfosReceived(int64_t, int64_t) override;
50 
51     struct DebugInfo {
52         VsyncId maxSendDelayVsyncId;
53         nsecs_t maxSendDelayDuration;
54         size_t pendingMessageCount;
55     };
56     DebugInfo getDebugInfo();
57 
58 protected:
59     void binderDied(const wp<IBinder>& who) override;
60 
61 private:
62     static constexpr size_t kStaticCapacity = 3;
63     std::atomic<int64_t> mNextListenerId{0};
64     ftl::SmallMap<wp<IBinder>, const std::pair<int64_t, sp<gui::IWindowInfosListener>>,
65                   kStaticCapacity>
66             mWindowInfosListeners;
67 
68     std::optional<gui::WindowInfosUpdate> mDelayedUpdate;
69     WindowInfosReportedListenerSet mReportedListeners;
70     void eraseListenerAndAckMessages(const wp<IBinder>&);
71 
72     struct UnackedState {
73         ftl::SmallVector<int64_t, kStaticCapacity> unackedListenerIds;
74         WindowInfosReportedListenerSet reportedListeners;
75     };
76     ftl::SmallMap<int64_t /* vsyncId */, UnackedState, 5> mUnackedState;
77 
78     DebugInfo mDebugInfo;
79     struct DelayInfo {
80         int64_t vsyncId;
81         nsecs_t frameTime;
82     };
83     std::optional<DelayInfo> mDelayInfo;
84     void updateMaxSendDelay();
85 };
86 
87 } // namespace android
88