blob: 4ada2b637241bab0f5ea031270e584a4e1c5fe6f [file] [log] [blame]
John Reck88270902021-03-18 11:27:35 -04001/*
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
Angel Aguayob398ee22021-10-14 00:39:15 +000025#include "WpHash.h"
26
John Reck88270902021-03-18 11:27:35 -040027namespace android {
28
29class HdrLayerInfoReporter final : public IBinder::DeathRecipient {
30public:
31 struct HdrLayerInfo {
32 int32_t numberOfHdrLayers = 0;
33 int32_t maxW = 0;
34 int32_t maxH = 0;
35 int32_t flags = 0;
36
37 bool operator==(const HdrLayerInfo& other) const {
38 return numberOfHdrLayers == other.numberOfHdrLayers && maxW == other.maxW &&
39 maxH == other.maxH && flags == other.flags;
40 }
41
42 bool operator!=(const HdrLayerInfo& other) const { return !(*this == other); }
43 };
44
45 HdrLayerInfoReporter() = default;
46 ~HdrLayerInfoReporter() final = default;
47
48 // Dispatches updated layer fps values for the registered listeners
49 // This method promotes Layer weak pointers and performs layer stack traversals, so mStateLock
50 // must be held when calling this method.
51 void dispatchHdrLayerInfo(const HdrLayerInfo& info) EXCLUDES(mMutex);
52
53 // Override for IBinder::DeathRecipient
54 void binderDied(const wp<IBinder>&) override EXCLUDES(mMutex);
55
56 // Registers an Fps listener that listens to fps updates for the provided layer
57 void addListener(const sp<gui::IHdrLayerInfoListener>& listener) EXCLUDES(mMutex);
58 // Deregisters an Fps listener
59 void removeListener(const sp<gui::IHdrLayerInfoListener>& listener) EXCLUDES(mMutex);
60
61 bool hasListeners() const EXCLUDES(mMutex) {
62 std::scoped_lock lock(mMutex);
63 return !mListeners.empty();
64 }
65
66private:
67 mutable std::mutex mMutex;
John Reck88270902021-03-18 11:27:35 -040068
69 struct TrackedListener {
70 sp<gui::IHdrLayerInfoListener> listener;
71 HdrLayerInfo lastInfo;
72 };
73
74 std::unordered_map<wp<IBinder>, TrackedListener, WpHash> mListeners GUARDED_BY(mMutex);
75};
76
77} // namespace android