• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 #include "ActivePictureTracker.h"
18 
19 #include <algorithm>
20 
21 #include "Layer.h"
22 #include "LayerFE.h"
23 
24 namespace android {
25 
26 using gui::ActivePicture;
27 using gui::IActivePictureListener;
28 
onLayerComposed(const Layer & layer,const LayerFE & layerFE,const CompositionResult & result)29 void ActivePictureTracker::onLayerComposed(const Layer& layer, const LayerFE& layerFE,
30                                            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 
updateAndNotifyListeners(const Listeners & listenersToAdd,const Listeners & listenersToRemove)45 void 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 
updateListeners(const Listeners & listenersToAdd,const Listeners & listenersToRemove)59 ActivePictureTracker::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 
updateAndHasChanged()82 bool ActivePictureTracker::updateAndHasChanged() {
83     bool hasChanged = true;
84     if (mNewActivePictures.size() == mOldActivePictures.size()) {
85         auto compare = [](const ActivePicture& lhs, const ActivePicture& rhs) -> int {
86             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 
getActivePictures() const102 const std::vector<ActivePicture>& ActivePictureTracker::getActivePictures() const {
103     return mOldActivePictures;
104 }
105 
106 } // namespace android
107