blob: d0f8fbea22e3e60d96cd2e7639c074acac5f6fae [file] [log] [blame]
Robert Carr2047fae2016-11-28 14:09:09 -08001/*
2 * Copyright (C) 2016 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 "LayerVector.h"
18#include "Layer.h"
19
20namespace android {
Dan Stoza412903f2017-04-27 13:42:17 -070021
22LayerVector::LayerVector() = default;
23
Robert Carr2047fae2016-11-28 14:09:09 -080024LayerVector::LayerVector(const LayerVector& rhs) : SortedVector<sp<Layer>>(rhs) {
25}
26
Dan Stoza412903f2017-04-27 13:42:17 -070027LayerVector::~LayerVector() = default;
28
Robert Carr2047fae2016-11-28 14:09:09 -080029int LayerVector::do_compare(const void* lhs, const void* rhs) const
30{
31 // sort layers per layer-stack, then by z-order and finally by sequence
32 const auto& l = *reinterpret_cast<const sp<Layer>*>(lhs);
33 const auto& r = *reinterpret_cast<const sp<Layer>*>(rhs);
34
35 uint32_t ls = l->getCurrentState().layerStack;
36 uint32_t rs = r->getCurrentState().layerStack;
37 if (ls != rs)
Ivan Lozanoc6c2a8a2017-10-30 11:24:08 -070038 return (ls > rs) ? 1 : -1;
Robert Carr2047fae2016-11-28 14:09:09 -080039
40 uint32_t lz = l->getCurrentState().z;
41 uint32_t rz = r->getCurrentState().z;
42 if (lz != rz)
Ivan Lozanoc6c2a8a2017-10-30 11:24:08 -070043 return (lz > rz) ? 1 : -1;
Robert Carr2047fae2016-11-28 14:09:09 -080044
Ivan Lozanoc6c2a8a2017-10-30 11:24:08 -070045 if (l->sequence == r->sequence)
46 return 0;
47
48 return (l->sequence > r->sequence) ? 1 : -1;
Robert Carr2047fae2016-11-28 14:09:09 -080049}
50
Dan Stoza412903f2017-04-27 13:42:17 -070051void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -070052 for (size_t i = 0; i < size(); i++) {
Robert Carrdb66e622017-04-10 16:55:57 -070053 const auto& layer = (*this)[i];
Dan Stoza412903f2017-04-27 13:42:17 -070054 auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
55 : layer->getDrawingState();
56 if (state.zOrderRelativeOf != nullptr) {
Robert Carrdb66e622017-04-10 16:55:57 -070057 continue;
58 }
Dan Stoza412903f2017-04-27 13:42:17 -070059 layer->traverseInZOrder(stateSet, visitor);
Robert Carr2047fae2016-11-28 14:09:09 -080060 }
61}
62
Dan Stoza412903f2017-04-27 13:42:17 -070063void LayerVector::traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const {
Robert Carr2047fae2016-11-28 14:09:09 -080064 for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -070065 const auto& layer = (*this)[i];
Dan Stoza412903f2017-04-27 13:42:17 -070066 auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
67 : layer->getDrawingState();
68 if (state.zOrderRelativeOf != nullptr) {
Robert Carrdb66e622017-04-10 16:55:57 -070069 continue;
70 }
Dan Stoza412903f2017-04-27 13:42:17 -070071 layer->traverseInReverseZOrder(stateSet, visitor);
Robert Carr2047fae2016-11-28 14:09:09 -080072 }
73}
74} // namespace android