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/IHdrLayerInfoListener.h> 21 #include <binder/IBinder.h> 22 23 #include <unordered_map> 24 25 namespace android { 26 27 class HdrLayerInfoReporter final : public IBinder::DeathRecipient { 28 public: 29 struct HdrLayerInfo { 30 int32_t numberOfHdrLayers = 0; 31 int32_t maxW = 0; 32 int32_t maxH = 0; 33 int32_t flags = 0; 34 35 bool operator==(const HdrLayerInfo& other) const { 36 return numberOfHdrLayers == other.numberOfHdrLayers && maxW == other.maxW && 37 maxH == other.maxH && flags == other.flags; 38 } 39 40 bool operator!=(const HdrLayerInfo& other) const { return !(*this == other); } 41 }; 42 43 HdrLayerInfoReporter() = default; 44 ~HdrLayerInfoReporter() final = default; 45 46 // Dispatches updated layer fps values for the registered listeners 47 // This method promotes Layer weak pointers and performs layer stack traversals, so mStateLock 48 // must be held when calling this method. 49 void dispatchHdrLayerInfo(const HdrLayerInfo& info) EXCLUDES(mMutex); 50 51 // Override for IBinder::DeathRecipient 52 void binderDied(const wp<IBinder>&) override EXCLUDES(mMutex); 53 54 // Registers an Fps listener that listens to fps updates for the provided layer 55 void addListener(const sp<gui::IHdrLayerInfoListener>& listener) EXCLUDES(mMutex); 56 // Deregisters an Fps listener 57 void removeListener(const sp<gui::IHdrLayerInfoListener>& listener) EXCLUDES(mMutex); 58 hasListeners()59 bool hasListeners() const EXCLUDES(mMutex) { 60 std::scoped_lock lock(mMutex); 61 return !mListeners.empty(); 62 } 63 64 private: 65 mutable std::mutex mMutex; 66 struct WpHash { operatorWpHash67 size_t operator()(const wp<IBinder>& p) const { 68 return std::hash<IBinder*>()(p.unsafe_get()); 69 } 70 }; 71 72 struct TrackedListener { 73 sp<gui::IHdrLayerInfoListener> listener; 74 HdrLayerInfo lastInfo; 75 }; 76 77 std::unordered_map<wp<IBinder>, TrackedListener, WpHash> mListeners GUARDED_BY(mMutex); 78 }; 79 80 } // namespace android