blob: 84945247a654cee305d23baa40be157450cbef17 [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
chaviwfd462612018-05-31 16:11:27 -070022LayerVector::LayerVector(const StateSet stateSet) : mStateSet(stateSet) {}
Dan Stoza412903f2017-04-27 13:42:17 -070023
chaviwfd462612018-05-31 16:11:27 -070024LayerVector::LayerVector(const LayerVector& rhs, const StateSet stateSet)
25 : SortedVector<sp<Layer>>(rhs), mStateSet(stateSet) {}
Robert Carr2047fae2016-11-28 14:09:09 -080026
Dan Stoza412903f2017-04-27 13:42:17 -070027LayerVector::~LayerVector() = default;
28
chaviwfd462612018-05-31 16:11:27 -070029// This operator override is needed to prevent mStateSet from getting copied over.
30LayerVector& LayerVector::operator=(const LayerVector& rhs) {
31 SortedVector::operator=(rhs);
32 return *this;
33}
34
Robert Carr2047fae2016-11-28 14:09:09 -080035int LayerVector::do_compare(const void* lhs, const void* rhs) const
36{
37 // sort layers per layer-stack, then by z-order and finally by sequence
38 const auto& l = *reinterpret_cast<const sp<Layer>*>(lhs);
39 const auto& r = *reinterpret_cast<const sp<Layer>*>(rhs);
40
Lloyd Pique0449b0f2018-12-20 16:23:45 -080041 const auto& lState =
42 (mStateSet == StateSet::Current) ? l->getCurrentState() : l->getDrawingState();
43 const auto& rState =
44 (mStateSet == StateSet::Current) ? r->getCurrentState() : r->getDrawingState();
chaviwfd462612018-05-31 16:11:27 -070045
Lloyd Pique0449b0f2018-12-20 16:23:45 -080046 uint32_t ls = lState.layerStack;
47 uint32_t rs = rState.layerStack;
Robert Carr2047fae2016-11-28 14:09:09 -080048 if (ls != rs)
Ivan Lozano80de6f32017-10-30 11:24:08 -070049 return (ls > rs) ? 1 : -1;
Robert Carr2047fae2016-11-28 14:09:09 -080050
Lloyd Pique0449b0f2018-12-20 16:23:45 -080051 int32_t lz = lState.z;
52 int32_t rz = rState.z;
Robert Carr2047fae2016-11-28 14:09:09 -080053 if (lz != rz)
Ivan Lozano80de6f32017-10-30 11:24:08 -070054 return (lz > rz) ? 1 : -1;
Robert Carr2047fae2016-11-28 14:09:09 -080055
Ivan Lozano80de6f32017-10-30 11:24:08 -070056 if (l->sequence == r->sequence)
57 return 0;
58
59 return (l->sequence > r->sequence) ? 1 : -1;
Robert Carr2047fae2016-11-28 14:09:09 -080060}
61
Dan Stoza412903f2017-04-27 13:42:17 -070062void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -070063 for (size_t i = 0; i < size(); i++) {
Robert Carrdb66e622017-04-10 16:55:57 -070064 const auto& layer = (*this)[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -080065 auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
66 : layer->getDrawingState();
67 if (state.zOrderRelativeOf != nullptr) {
Robert Carrdb66e622017-04-10 16:55:57 -070068 continue;
69 }
Dan Stoza412903f2017-04-27 13:42:17 -070070 layer->traverseInZOrder(stateSet, visitor);
Robert Carr2047fae2016-11-28 14:09:09 -080071 }
72}
73
Dan Stoza412903f2017-04-27 13:42:17 -070074void LayerVector::traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const {
Robert Carr2047fae2016-11-28 14:09:09 -080075 for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -070076 const auto& layer = (*this)[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -080077 auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
78 : layer->getDrawingState();
79 if (state.zOrderRelativeOf != nullptr) {
Robert Carrdb66e622017-04-10 16:55:57 -070080 continue;
81 }
Dan Stoza412903f2017-04-27 13:42:17 -070082 layer->traverseInReverseZOrder(stateSet, visitor);
Robert Carr2047fae2016-11-28 14:09:09 -080083 }
84}
85} // namespace android