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