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 
21 #include <android-base/stringprintf.h>
22 #include <inttypes.h>
23 #include <utils/Trace.h>
24 
25 #include "HdrLayerInfoReporter.h"
26 
27 namespace android {
28 
29 using base::StringAppendF;
30 
dispatchHdrLayerInfo(const HdrLayerInfo & info)31 void HdrLayerInfoReporter::dispatchHdrLayerInfo(const HdrLayerInfo& info) {
32     ATRACE_CALL();
33     if (mHdrInfoHistory.size() == 0 || mHdrInfoHistory.back().info != info) {
34         mHdrInfoHistory.next() = EventHistoryEntry{info};
35     }
36 
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) {
50         ATRACE_NAME("invoking onHdrLayerInfoChanged");
51         listener->onHdrLayerInfoChanged(info.numberOfHdrLayers, info.maxW, info.maxH, info.flags,
52                                         info.maxDesiredHdrSdrRatio);
53     }
54 }
55 
binderDied(const wp<IBinder> & who)56 void HdrLayerInfoReporter::binderDied(const wp<IBinder>& who) {
57     std::scoped_lock lock(mMutex);
58     mListeners.erase(who);
59 }
60 
addListener(const sp<gui::IHdrLayerInfoListener> & listener)61 void HdrLayerInfoReporter::addListener(const sp<gui::IHdrLayerInfoListener>& listener) {
62     sp<IBinder> asBinder = IInterface::asBinder(listener);
63     asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this));
64     std::lock_guard lock(mMutex);
65     mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, HdrLayerInfo{}});
66 }
67 
removeListener(const sp<gui::IHdrLayerInfoListener> & listener)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 
dump(std::string & result) const73 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 
84 } // namespace android