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 | #undef LOG_TAG |
| 18 | #define LOG_TAG "HdrLayerInfoReporter" |
| 19 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 20 | |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 21 | #include <android-base/stringprintf.h> |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 22 | #include <common/trace.h> |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 23 | #include <inttypes.h> |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 24 | |
| 25 | #include "HdrLayerInfoReporter.h" |
| 26 | |
| 27 | namespace android { |
| 28 | |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 29 | using base::StringAppendF; |
| 30 | |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 31 | void HdrLayerInfoReporter::dispatchHdrLayerInfo(const HdrLayerInfo& info) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 32 | SFTRACE_CALL(); |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 33 | if (mHdrInfoHistory.size() == 0 || mHdrInfoHistory.back().info != info) { |
| 34 | mHdrInfoHistory.next() = EventHistoryEntry{info}; |
| 35 | } |
| 36 | |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 37 | std::vector<sp<gui::IHdrLayerInfoListener>> toInvoke; |
| 38 | { |
| 39 | std::scoped_lock lock(mMutex); |
| 40 | toInvoke.reserve(mListeners.size()); |
| 41 | for (auto& [key, it] : mListeners) { |
| 42 | if (it.lastInfo != info) { |
| 43 | it.lastInfo = info; |
| 44 | toInvoke.push_back(it.listener); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | for (const auto& listener : toInvoke) { |
Vishnu Nair | be0ad90 | 2024-06-27 23:38:43 +0000 | [diff] [blame] | 50 | SFTRACE_NAME("invoking onHdrLayerInfoChanged"); |
Alec Mouri | c68c61a | 2023-02-09 17:35:36 +0000 | [diff] [blame] | 51 | listener->onHdrLayerInfoChanged(info.numberOfHdrLayers, info.maxW, info.maxH, info.flags, |
| 52 | info.maxDesiredHdrSdrRatio); |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | void HdrLayerInfoReporter::binderDied(const wp<IBinder>& who) { |
| 57 | std::scoped_lock lock(mMutex); |
| 58 | mListeners.erase(who); |
| 59 | } |
| 60 | |
| 61 | void HdrLayerInfoReporter::addListener(const sp<gui::IHdrLayerInfoListener>& listener) { |
| 62 | sp<IBinder> asBinder = IInterface::asBinder(listener); |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 63 | asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this)); |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 64 | std::lock_guard lock(mMutex); |
| 65 | mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, HdrLayerInfo{}}); |
| 66 | } |
| 67 | |
| 68 | void HdrLayerInfoReporter::removeListener(const sp<gui::IHdrLayerInfoListener>& listener) { |
| 69 | std::lock_guard lock(mMutex); |
| 70 | mListeners.erase(wp<IBinder>(IInterface::asBinder(listener))); |
| 71 | } |
| 72 | |
John Reck | 2a3d29d | 2023-08-17 17:45:01 -0400 | [diff] [blame] | 73 | void HdrLayerInfoReporter::dump(std::string& result) const { |
| 74 | for (size_t i = 0; i < mHdrInfoHistory.size(); i++) { |
| 75 | const auto& event = mHdrInfoHistory[i]; |
| 76 | const auto& info = event.info; |
| 77 | StringAppendF(&result, |
| 78 | "%" PRId64 ": numHdrLayers(%d), size(%dx%d), flags(%X), desiredRatio(%.2f)\n", |
| 79 | event.timestamp, info.numberOfHdrLayers, info.maxW, info.maxH, info.flags, |
| 80 | info.maxDesiredHdrSdrRatio); |
| 81 | } |
| 82 | } |
| 83 | |
John Reck | 8827090 | 2021-03-18 11:27:35 -0400 | [diff] [blame] | 84 | } // namespace android |