John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 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> |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 22 | #include <utils/Timers.h> |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 23 | |
| 24 | #include <unordered_map> |
| 25 | |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 26 | #include "Utils/RingBuffer.h" |
Angel Aguayo | b398ee2 | 2021-10-14 00:39:15 +0000 | [diff] [blame] | 27 | #include "WpHash.h" |
| 28 | |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 29 | namespace android { |
| 30 | |
| 31 | class HdrLayerInfoReporter final : public IBinder::DeathRecipient { |
| 32 | public: |
| 33 | struct HdrLayerInfo { |
| 34 | int32_t numberOfHdrLayers = 0; |
| 35 | int32_t maxW = 0; |
| 36 | int32_t maxH = 0; |
| 37 | int32_t flags = 0; |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 38 | // Counter-intuitively a value of "1" means "as much as you can give me" due to "1" being |
| 39 | // the default value for all layers, so any HDR layer with a value of 1.f means no |
| 40 | // reduced maximum has been requested |
| 41 | // TODO: Should the max desired ratio have a better meaning for HLG/PQ so this can be |
| 42 | // eliminated? If we assume an SDR white point of even just 100 nits for those content |
| 43 | // then HLG could have a meaningful max ratio of 10.f and PQ of 100.f instead of needing |
| 44 | // to treat 1.f as "uncapped" |
| 45 | // With peak display brightnesses exceeding 1,000 nits currently, HLG's request could |
| 46 | // actually be satisfied in some ambient conditions such that limiting that max for that |
| 47 | // content in theory makes sense |
Alec Mouri | c68c61a | 2023-02-09 17:35:36 +0000 | [diff] [blame] | 48 | float maxDesiredHdrSdrRatio = 0.f; |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 49 | |
| 50 | bool operator==(const HdrLayerInfo& other) const { |
| 51 | return numberOfHdrLayers == other.numberOfHdrLayers && maxW == other.maxW && |
Alec Mouri | c68c61a | 2023-02-09 17:35:36 +0000 | [diff] [blame] | 52 | maxH == other.maxH && flags == other.flags && |
| 53 | maxDesiredHdrSdrRatio == other.maxDesiredHdrSdrRatio; |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | bool operator!=(const HdrLayerInfo& other) const { return !(*this == other); } |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 57 | |
| 58 | void mergeDesiredRatio(float update) { |
Alec Mouri | c68c61a | 2023-02-09 17:35:36 +0000 | [diff] [blame] | 59 | maxDesiredHdrSdrRatio = std::max(maxDesiredHdrSdrRatio, update); |
John Reck | 6879659 | 2023-01-25 13:47:12 -0500 | [diff] [blame] | 60 | } |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | HdrLayerInfoReporter() = default; |
| 64 | ~HdrLayerInfoReporter() final = default; |
| 65 | |
| 66 | // Dispatches updated layer fps values for the registered listeners |
| 67 | // This method promotes Layer weak pointers and performs layer stack traversals, so mStateLock |
| 68 | // must be held when calling this method. |
| 69 | void dispatchHdrLayerInfo(const HdrLayerInfo& info) EXCLUDES(mMutex); |
| 70 | |
| 71 | // Override for IBinder::DeathRecipient |
| 72 | void binderDied(const wp<IBinder>&) override EXCLUDES(mMutex); |
| 73 | |
| 74 | // Registers an Fps listener that listens to fps updates for the provided layer |
| 75 | void addListener(const sp<gui::IHdrLayerInfoListener>& listener) EXCLUDES(mMutex); |
| 76 | // Deregisters an Fps listener |
| 77 | void removeListener(const sp<gui::IHdrLayerInfoListener>& listener) EXCLUDES(mMutex); |
| 78 | |
| 79 | bool hasListeners() const EXCLUDES(mMutex) { |
| 80 | std::scoped_lock lock(mMutex); |
| 81 | return !mListeners.empty(); |
| 82 | } |
| 83 | |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 84 | void dump(std::string& result) const; |
| 85 | |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 86 | private: |
| 87 | mutable std::mutex mMutex; |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 88 | |
| 89 | struct TrackedListener { |
| 90 | sp<gui::IHdrLayerInfoListener> listener; |
| 91 | HdrLayerInfo lastInfo; |
| 92 | }; |
| 93 | |
| 94 | std::unordered_map<wp<IBinder>, TrackedListener, WpHash> mListeners GUARDED_BY(mMutex); |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 95 | |
| 96 | struct EventHistoryEntry { |
| 97 | nsecs_t timestamp = -1; |
| 98 | HdrLayerInfo info; |
| 99 | |
| 100 | EventHistoryEntry() {} |
| 101 | |
| 102 | EventHistoryEntry(const HdrLayerInfo& info) : info(info) { timestamp = systemTime(); } |
| 103 | }; |
| 104 | |
| 105 | utils::RingBuffer<EventHistoryEntry, 32> mHdrInfoHistory; |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | } // namespace android |