blob: 9b949204081cb1b00c26ee0e04203ad5aab1076e [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Robert Carr2047fae2016-11-28 14:09:09 -080021#include "LayerVector.h"
22#include "Layer.h"
23
24namespace android {
Dan Stoza412903f2017-04-27 13:42:17 -070025
chaviwfd462612018-05-31 16:11:27 -070026LayerVector::LayerVector(const StateSet stateSet) : mStateSet(stateSet) {}
Dan Stoza412903f2017-04-27 13:42:17 -070027
chaviwfd462612018-05-31 16:11:27 -070028LayerVector::LayerVector(const LayerVector& rhs, const StateSet stateSet)
29 : SortedVector<sp<Layer>>(rhs), mStateSet(stateSet) {}
Robert Carr2047fae2016-11-28 14:09:09 -080030
Dan Stoza412903f2017-04-27 13:42:17 -070031LayerVector::~LayerVector() = default;
32
chaviwfd462612018-05-31 16:11:27 -070033// This operator override is needed to prevent mStateSet from getting copied over.
34LayerVector& LayerVector::operator=(const LayerVector& rhs) {
35 SortedVector::operator=(rhs);
36 return *this;
37}
38
Robert Carr2047fae2016-11-28 14:09:09 -080039int LayerVector::do_compare(const void* lhs, const void* rhs) const
40{
41 // sort layers per layer-stack, then by z-order and finally by sequence
42 const auto& l = *reinterpret_cast<const sp<Layer>*>(lhs);
43 const auto& r = *reinterpret_cast<const sp<Layer>*>(rhs);
44
Lloyd Pique0449b0f2018-12-20 16:23:45 -080045 const auto& lState =
46 (mStateSet == StateSet::Current) ? l->getCurrentState() : l->getDrawingState();
47 const auto& rState =
48 (mStateSet == StateSet::Current) ? r->getCurrentState() : r->getDrawingState();
chaviwfd462612018-05-31 16:11:27 -070049
Lloyd Pique0449b0f2018-12-20 16:23:45 -080050 uint32_t ls = lState.layerStack;
51 uint32_t rs = rState.layerStack;
Robert Carr2047fae2016-11-28 14:09:09 -080052 if (ls != rs)
Ivan Lozano80de6f32017-10-30 11:24:08 -070053 return (ls > rs) ? 1 : -1;
Robert Carr2047fae2016-11-28 14:09:09 -080054
Lloyd Pique0449b0f2018-12-20 16:23:45 -080055 int32_t lz = lState.z;
56 int32_t rz = rState.z;
Robert Carr2047fae2016-11-28 14:09:09 -080057 if (lz != rz)
Ivan Lozano80de6f32017-10-30 11:24:08 -070058 return (lz > rz) ? 1 : -1;
Robert Carr2047fae2016-11-28 14:09:09 -080059
Ivan Lozano80de6f32017-10-30 11:24:08 -070060 if (l->sequence == r->sequence)
61 return 0;
62
63 return (l->sequence > r->sequence) ? 1 : -1;
Robert Carr2047fae2016-11-28 14:09:09 -080064}
65
Dan Stoza412903f2017-04-27 13:42:17 -070066void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const {
Robert Carr1f0a16a2016-10-24 16:27:39 -070067 for (size_t i = 0; i < size(); i++) {
Robert Carrdb66e622017-04-10 16:55:57 -070068 const auto& layer = (*this)[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -080069 auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
70 : layer->getDrawingState();
chaviwe5ac40f2019-09-24 16:36:55 -070071 if (state.isRelativeOf) {
Robert Carrdb66e622017-04-10 16:55:57 -070072 continue;
73 }
Dan Stoza412903f2017-04-27 13:42:17 -070074 layer->traverseInZOrder(stateSet, visitor);
Robert Carr2047fae2016-11-28 14:09:09 -080075 }
76}
77
Dan Stoza412903f2017-04-27 13:42:17 -070078void LayerVector::traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const {
Robert Carr2047fae2016-11-28 14:09:09 -080079 for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) {
Robert Carrdb66e622017-04-10 16:55:57 -070080 const auto& layer = (*this)[i];
Lloyd Pique0449b0f2018-12-20 16:23:45 -080081 auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
82 : layer->getDrawingState();
chaviwe5ac40f2019-09-24 16:36:55 -070083 if (state.isRelativeOf) {
Robert Carrdb66e622017-04-10 16:55:57 -070084 continue;
85 }
Dan Stoza412903f2017-04-27 13:42:17 -070086 layer->traverseInReverseZOrder(stateSet, visitor);
Robert Carr2047fae2016-11-28 14:09:09 -080087 }
88}
Edgar Arriaga844fa672020-01-16 14:21:42 -080089
90void LayerVector::traverse(const Visitor& visitor) const {
91 for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) {
92 const auto& layer = (*this)[i];
93 layer->traverse(mStateSet, visitor);
94 }
95}
96
Robert Carr2047fae2016-11-28 14:09:09 -080097} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080098
99// TODO(b/129481165): remove the #pragma below and fix conversion issues
100#pragma clang diagnostic pop // ignored "-Wconversion"