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/gui/BnWindowInfosReportedListener.h>
20 #include <android/gui/IWindowInfosListener.h>
21 #include <android/gui/IWindowInfosReportedListener.h>
22 #include <binder/IBinder.h>
23 #include <utils/Mutex.h>
24 #include <unordered_map>
25 
26 namespace android {
27 
28 class SurfaceFlinger;
29 
30 class WindowInfosListenerInvoker : public IBinder::DeathRecipient {
31 public:
32     WindowInfosListenerInvoker(const sp<SurfaceFlinger>& sf);
33     void addWindowInfosListener(const sp<gui::IWindowInfosListener>& windowInfosListener);
34     void removeWindowInfosListener(const sp<gui::IWindowInfosListener>& windowInfosListener);
35 
36     void windowInfosChanged(const std::vector<gui::WindowInfo>& windowInfos, bool shouldSync);
37 
38 protected:
39     void binderDied(const wp<IBinder>& who) override;
40 
41 private:
42     void windowInfosReported();
43 
44     struct WpHash {
operatorWpHash45         size_t operator()(const wp<IBinder>& p) const {
46             return std::hash<IBinder*>()(p.unsafe_get());
47         }
48     };
49 
50     const sp<SurfaceFlinger> mSf;
51     std::mutex mListenersMutex;
52     std::unordered_map<wp<IBinder>, const sp<gui::IWindowInfosListener>, WpHash>
53             mWindowInfosListeners GUARDED_BY(mListenersMutex);
54     sp<gui::IWindowInfosReportedListener> mWindowInfosReportedListener;
55     std::atomic<size_t> mCallbacksPending{0};
56 };
57 } // namespace android