blob: 4e6fa66ed81479cdb7b909b9d31d0c40c03b085d [file] [log] [blame]
Brian Lindahlf5fdff82024-11-01 09:28:47 -06001/*
2 * Copyright 2024 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
Brian Lindahle8334902024-12-06 08:25:05 -070017#include "ActivePictureTracker.h"
Brian Lindahlf5fdff82024-11-01 09:28:47 -060018
19#include <algorithm>
20
21#include "Layer.h"
22#include "LayerFE.h"
23
24namespace android {
25
Brian Lindahlf1cfbf62024-12-06 08:07:16 -070026using gui::ActivePicture;
27using gui::IActivePictureListener;
28
Brian Lindahle8334902024-12-06 08:25:05 -070029void ActivePictureTracker::onLayerComposed(const Layer& layer, const LayerFE& layerFE,
Brian Lindahlf5fdff82024-11-01 09:28:47 -060030 const CompositionResult& result) {
31 if (result.wasPictureProfileCommitted) {
32 gui::ActivePicture picture;
33 picture.layerId = int32_t(layer.sequence);
34 picture.ownerUid = int32_t(layer.getOwnerUid());
35 // TODO(b/337330263): Why does LayerFE coming from SF have a null composition state?
36 if (layerFE.getCompositionState()) {
37 picture.pictureProfileId = layerFE.getCompositionState()->pictureProfileHandle.getId();
38 } else {
39 picture.pictureProfileId = result.pictureProfileHandle.getId();
40 }
41 mNewActivePictures.push_back(picture);
42 }
43}
44
Brian Lindahlf1cfbf62024-12-06 08:07:16 -070045void ActivePictureTracker::updateAndNotifyListeners(const Listeners& listenersToAdd,
46 const Listeners& listenersToRemove) {
47 Listeners newListeners = updateListeners(listenersToAdd, listenersToRemove);
48 if (updateAndHasChanged()) {
49 for (auto listener : mListeners) {
50 listener->onActivePicturesChanged(getActivePictures());
51 }
52 } else {
53 for (auto listener : newListeners) {
54 listener->onActivePicturesChanged(getActivePictures());
55 }
56 }
57}
58
59ActivePictureTracker::Listeners ActivePictureTracker::updateListeners(
60 const Listeners& listenersToAdd, const Listeners& listenersToRemove) {
61 Listeners newListeners;
62 for (auto listener : listenersToRemove) {
63 std::erase_if(mListeners, [listener](const sp<IActivePictureListener>& otherListener) {
64 return IInterface::asBinder(listener) == IInterface::asBinder(otherListener);
65 });
66 }
67 for (auto listener : listenersToAdd) {
68 if (std::find_if(mListeners.begin(), mListeners.end(),
69 [listener](const sp<IActivePictureListener>& otherListener) {
70 return IInterface::asBinder(listener) ==
71 IInterface::asBinder(otherListener);
72 }) == mListeners.end()) {
73 newListeners.push_back(listener);
74 }
75 }
76 for (auto listener : newListeners) {
77 mListeners.push_back(listener);
78 }
79 return newListeners;
80}
81
Brian Lindahle8334902024-12-06 08:25:05 -070082bool ActivePictureTracker::updateAndHasChanged() {
Brian Lindahlf5fdff82024-11-01 09:28:47 -060083 bool hasChanged = true;
84 if (mNewActivePictures.size() == mOldActivePictures.size()) {
Brian Lindahlf1cfbf62024-12-06 08:07:16 -070085 auto compare = [](const ActivePicture& lhs, const ActivePicture& rhs) -> int {
Brian Lindahlf5fdff82024-11-01 09:28:47 -060086 if (lhs.layerId == rhs.layerId) {
87 return lhs.pictureProfileId < rhs.pictureProfileId;
88 }
89 return lhs.layerId < rhs.layerId;
90 };
91 std::sort(mNewActivePictures.begin(), mNewActivePictures.end(), compare);
92 if (std::equal(mNewActivePictures.begin(), mNewActivePictures.end(),
93 mOldActivePictures.begin())) {
94 hasChanged = false;
95 }
96 }
97 std::swap(mOldActivePictures, mNewActivePictures);
98 mNewActivePictures.resize(0);
99 return hasChanged;
100}
101
Brian Lindahlf1cfbf62024-12-06 08:07:16 -0700102const std::vector<ActivePicture>& ActivePictureTracker::getActivePictures() const {
Brian Lindahlf5fdff82024-11-01 09:28:47 -0600103 return mOldActivePictures;
104}
105
106} // namespace android