blob: 85921bb72d00933ae389abb4df6f48f3257af1a9 [file] [log] [blame]
John Reck88270902021-03-18 11:27:35 -04001/*
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 Reck2a3d29d2023-08-17 17:45:01 -040021#include <android-base/stringprintf.h>
Vishnu Nairbe0ad902024-06-27 23:38:43 +000022#include <common/trace.h>
John Reck2a3d29d2023-08-17 17:45:01 -040023#include <inttypes.h>
John Reck88270902021-03-18 11:27:35 -040024
25#include "HdrLayerInfoReporter.h"
26
27namespace android {
28
John Reck2a3d29d2023-08-17 17:45:01 -040029using base::StringAppendF;
30
John Reck88270902021-03-18 11:27:35 -040031void HdrLayerInfoReporter::dispatchHdrLayerInfo(const HdrLayerInfo& info) {
Vishnu Nairbe0ad902024-06-27 23:38:43 +000032 SFTRACE_CALL();
John Reck2a3d29d2023-08-17 17:45:01 -040033 if (mHdrInfoHistory.size() == 0 || mHdrInfoHistory.back().info != info) {
34 mHdrInfoHistory.next() = EventHistoryEntry{info};
35 }
36
John Reck88270902021-03-18 11:27:35 -040037 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 Nairbe0ad902024-06-27 23:38:43 +000050 SFTRACE_NAME("invoking onHdrLayerInfoChanged");
Alec Mouric68c61a2023-02-09 17:35:36 +000051 listener->onHdrLayerInfoChanged(info.numberOfHdrLayers, info.maxW, info.maxH, info.flags,
52 info.maxDesiredHdrSdrRatio);
John Reck88270902021-03-18 11:27:35 -040053 }
54}
55
56void HdrLayerInfoReporter::binderDied(const wp<IBinder>& who) {
57 std::scoped_lock lock(mMutex);
58 mListeners.erase(who);
59}
60
61void HdrLayerInfoReporter::addListener(const sp<gui::IHdrLayerInfoListener>& listener) {
62 sp<IBinder> asBinder = IInterface::asBinder(listener);
Ady Abrahamd11bade2022-08-01 16:18:03 -070063 asBinder->linkToDeath(sp<DeathRecipient>::fromExisting(this));
John Reck88270902021-03-18 11:27:35 -040064 std::lock_guard lock(mMutex);
65 mListeners.emplace(wp<IBinder>(asBinder), TrackedListener{listener, HdrLayerInfo{}});
66}
67
68void HdrLayerInfoReporter::removeListener(const sp<gui::IHdrLayerInfoListener>& listener) {
69 std::lock_guard lock(mMutex);
70 mListeners.erase(wp<IBinder>(IInterface::asBinder(listener)));
71}
72
John Reck2a3d29d2023-08-17 17:45:01 -040073void 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 Reck88270902021-03-18 11:27:35 -040084} // namespace android