xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/HdrLayerInfoReporter.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2021 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG
18*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "HdrLayerInfoReporter"
19*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS
20*38e8c45fSAndroid Build Coastguard Worker 
21*38e8c45fSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <common/trace.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <inttypes.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker #include "HdrLayerInfoReporter.h"
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker namespace android {
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker using base::StringAppendF;
30*38e8c45fSAndroid Build Coastguard Worker 
dispatchHdrLayerInfo(const HdrLayerInfo & info)31*38e8c45fSAndroid Build Coastguard Worker void HdrLayerInfoReporter::dispatchHdrLayerInfo(const HdrLayerInfo& info) {
32*38e8c45fSAndroid Build Coastguard Worker     SFTRACE_CALL();
33*38e8c45fSAndroid Build Coastguard Worker     if (mHdrInfoHistory.size() == 0 || mHdrInfoHistory.back().info != info) {
34*38e8c45fSAndroid Build Coastguard Worker         mHdrInfoHistory.next() = EventHistoryEntry{info};
35*38e8c45fSAndroid Build Coastguard Worker     }
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker     std::vector<sp<gui::IHdrLayerInfoListener>> toInvoke;
38*38e8c45fSAndroid Build Coastguard Worker     {
39*38e8c45fSAndroid Build Coastguard Worker         std::scoped_lock lock(mMutex);
40*38e8c45fSAndroid Build Coastguard Worker         toInvoke.reserve(mListeners.size());
41*38e8c45fSAndroid Build Coastguard Worker         for (auto& [key, it] : mListeners) {
42*38e8c45fSAndroid Build Coastguard Worker             if (it.lastInfo != info) {
43*38e8c45fSAndroid Build Coastguard Worker                 it.lastInfo = info;
44*38e8c45fSAndroid Build Coastguard Worker                 toInvoke.push_back(it.listener);
45*38e8c45fSAndroid Build Coastguard Worker             }
46*38e8c45fSAndroid Build Coastguard Worker         }
47*38e8c45fSAndroid Build Coastguard Worker     }
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker     for (const auto& listener : toInvoke) {
50*38e8c45fSAndroid Build Coastguard Worker         SFTRACE_NAME("invoking onHdrLayerInfoChanged");
51*38e8c45fSAndroid Build Coastguard Worker         listener->onHdrLayerInfoChanged(info.numberOfHdrLayers, info.maxW, info.maxH, info.flags,
52*38e8c45fSAndroid Build Coastguard Worker                                         info.maxDesiredHdrSdrRatio);
53*38e8c45fSAndroid Build Coastguard Worker     }
54*38e8c45fSAndroid Build Coastguard Worker }
55*38e8c45fSAndroid Build Coastguard Worker 
binderDied(const wp<IBinder> & who)56*38e8c45fSAndroid Build Coastguard Worker void HdrLayerInfoReporter::binderDied(const wp<IBinder>& who) {
57*38e8c45fSAndroid Build Coastguard Worker     std::scoped_lock lock(mMutex);
58*38e8c45fSAndroid Build Coastguard Worker     mListeners.erase(who);
59*38e8c45fSAndroid Build Coastguard Worker }
60*38e8c45fSAndroid Build Coastguard Worker 
addListener(const sp<gui::IHdrLayerInfoListener> & listener)61*38e8c45fSAndroid Build Coastguard Worker void HdrLayerInfoReporter::addListener(const sp<gui::IHdrLayerInfoListener>& listener) {
62*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> asBinder = IInterface::asBinder(listener);
63*38e8c45fSAndroid Build Coastguard Worker     asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this));
64*38e8c45fSAndroid Build Coastguard Worker     std::lock_guard lock(mMutex);
65*38e8c45fSAndroid Build Coastguard Worker     mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, HdrLayerInfo{}});
66*38e8c45fSAndroid Build Coastguard Worker }
67*38e8c45fSAndroid Build Coastguard Worker 
removeListener(const sp<gui::IHdrLayerInfoListener> & listener)68*38e8c45fSAndroid Build Coastguard Worker void HdrLayerInfoReporter::removeListener(const sp<gui::IHdrLayerInfoListener>& listener) {
69*38e8c45fSAndroid Build Coastguard Worker     std::lock_guard lock(mMutex);
70*38e8c45fSAndroid Build Coastguard Worker     mListeners.erase(wp<IBinder>(IInterface::asBinder(listener)));
71*38e8c45fSAndroid Build Coastguard Worker }
72*38e8c45fSAndroid Build Coastguard Worker 
dump(std::string & result) const73*38e8c45fSAndroid Build Coastguard Worker void HdrLayerInfoReporter::dump(std::string& result) const {
74*38e8c45fSAndroid Build Coastguard Worker     for (size_t i = 0; i < mHdrInfoHistory.size(); i++) {
75*38e8c45fSAndroid Build Coastguard Worker         const auto& event = mHdrInfoHistory[i];
76*38e8c45fSAndroid Build Coastguard Worker         const auto& info = event.info;
77*38e8c45fSAndroid Build Coastguard Worker         StringAppendF(&result,
78*38e8c45fSAndroid Build Coastguard Worker                       "%" PRId64 ": numHdrLayers(%d), size(%dx%d), flags(%X), desiredRatio(%.2f)\n",
79*38e8c45fSAndroid Build Coastguard Worker                       event.timestamp, info.numberOfHdrLayers, info.maxW, info.maxH, info.flags,
80*38e8c45fSAndroid Build Coastguard Worker                       info.maxDesiredHdrSdrRatio);
81*38e8c45fSAndroid Build Coastguard Worker     }
82*38e8c45fSAndroid Build Coastguard Worker }
83*38e8c45fSAndroid Build Coastguard Worker 
84*38e8c45fSAndroid Build Coastguard Worker } // namespace android