Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 1 | /* |
| 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 Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 21 | #include "LayerVector.h" |
| 22 | #include "Layer.h" |
| 23 | |
| 24 | namespace android { |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 25 | |
chaviw | fd46261 | 2018-05-31 16:11:27 -0700 | [diff] [blame] | 26 | LayerVector::LayerVector(const StateSet stateSet) : mStateSet(stateSet) {} |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 27 | |
chaviw | fd46261 | 2018-05-31 16:11:27 -0700 | [diff] [blame] | 28 | LayerVector::LayerVector(const LayerVector& rhs, const StateSet stateSet) |
| 29 | : SortedVector<sp<Layer>>(rhs), mStateSet(stateSet) {} |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 30 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 31 | LayerVector::~LayerVector() = default; |
| 32 | |
chaviw | fd46261 | 2018-05-31 16:11:27 -0700 | [diff] [blame] | 33 | // This operator override is needed to prevent mStateSet from getting copied over. |
| 34 | LayerVector& LayerVector::operator=(const LayerVector& rhs) { |
| 35 | SortedVector::operator=(rhs); |
| 36 | return *this; |
| 37 | } |
| 38 | |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 39 | int 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 | |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 45 | const auto& lState = l->getDrawingState(); |
| 46 | const auto& rState = r->getDrawingState(); |
chaviw | fd46261 | 2018-05-31 16:11:27 -0700 | [diff] [blame] | 47 | |
Dominik Laskowski | 29fa146 | 2021-04-27 15:51:50 -0700 | [diff] [blame] | 48 | const auto ls = lState.layerStack; |
| 49 | const auto rs = rState.layerStack; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 50 | if (ls != rs) |
Ivan Lozano | 80de6f3 | 2017-10-30 11:24:08 -0700 | [diff] [blame] | 51 | return (ls > rs) ? 1 : -1; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 52 | |
Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 53 | int32_t lz = lState.z; |
| 54 | int32_t rz = rState.z; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 55 | if (lz != rz) |
Ivan Lozano | 80de6f3 | 2017-10-30 11:24:08 -0700 | [diff] [blame] | 56 | return (lz > rz) ? 1 : -1; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 57 | |
Ivan Lozano | 80de6f3 | 2017-10-30 11:24:08 -0700 | [diff] [blame] | 58 | if (l->sequence == r->sequence) |
| 59 | return 0; |
| 60 | |
| 61 | return (l->sequence > r->sequence) ? 1 : -1; |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 64 | void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const { |
Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 65 | for (size_t i = 0; i < size(); i++) { |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 66 | const auto& layer = (*this)[i]; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 67 | auto& state = layer->getDrawingState(); |
chaviw | e5ac40f | 2019-09-24 16:36:55 -0700 | [diff] [blame] | 68 | if (state.isRelativeOf) { |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 69 | continue; |
| 70 | } |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 71 | layer->traverseInZOrder(stateSet, visitor); |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 75 | void LayerVector::traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const { |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 76 | for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) { |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 77 | const auto& layer = (*this)[i]; |
Robert Carr | 6a16031 | 2021-05-17 12:08:20 -0700 | [diff] [blame] | 78 | auto& state = layer->getDrawingState(); |
chaviw | e5ac40f | 2019-09-24 16:36:55 -0700 | [diff] [blame] | 79 | if (state.isRelativeOf) { |
Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 80 | continue; |
| 81 | } |
Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 82 | layer->traverseInReverseZOrder(stateSet, visitor); |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
Edgar Arriaga | 844fa67 | 2020-01-16 14:21:42 -0800 | [diff] [blame] | 85 | |
| 86 | void LayerVector::traverse(const Visitor& visitor) const { |
| 87 | for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) { |
| 88 | const auto& layer = (*this)[i]; |
| 89 | layer->traverse(mStateSet, visitor); |
| 90 | } |
| 91 | } |
| 92 | |
Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 93 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 94 | |
| 95 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 96 | #pragma clang diagnostic pop // ignored "-Wconversion" |