| 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 |  | 
|  | 17 | #include "LayerVector.h" | 
|  | 18 | #include "Layer.h" | 
|  | 19 |  | 
|  | 20 | namespace android { | 
| Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 21 |  | 
| chaviw | fd46261 | 2018-05-31 16:11:27 -0700 | [diff] [blame] | 22 | LayerVector::LayerVector(const StateSet stateSet) : mStateSet(stateSet) {} | 
| Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 23 |  | 
| chaviw | fd46261 | 2018-05-31 16:11:27 -0700 | [diff] [blame] | 24 | LayerVector::LayerVector(const LayerVector& rhs, const StateSet stateSet) | 
|  | 25 | : SortedVector<sp<Layer>>(rhs), mStateSet(stateSet) {} | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 26 |  | 
| Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 27 | LayerVector::~LayerVector() = default; | 
|  | 28 |  | 
| chaviw | fd46261 | 2018-05-31 16:11:27 -0700 | [diff] [blame] | 29 | // This operator override is needed to prevent mStateSet from getting copied over. | 
|  | 30 | LayerVector& LayerVector::operator=(const LayerVector& rhs) { | 
|  | 31 | SortedVector::operator=(rhs); | 
|  | 32 | return *this; | 
|  | 33 | } | 
|  | 34 |  | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 35 | int 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 Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 41 | const auto& lState = | 
|  | 42 | (mStateSet == StateSet::Current) ? l->getCurrentState() : l->getDrawingState(); | 
|  | 43 | const auto& rState = | 
|  | 44 | (mStateSet == StateSet::Current) ? r->getCurrentState() : r->getDrawingState(); | 
| chaviw | fd46261 | 2018-05-31 16:11:27 -0700 | [diff] [blame] | 45 |  | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 46 | uint32_t ls = lState.layerStack; | 
|  | 47 | uint32_t rs = rState.layerStack; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 48 | if (ls != rs) | 
| Ivan Lozano | 80de6f3 | 2017-10-30 11:24:08 -0700 | [diff] [blame] | 49 | return (ls > rs) ? 1 : -1; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 50 |  | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 51 | int32_t lz = lState.z; | 
|  | 52 | int32_t rz = rState.z; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 53 | if (lz != rz) | 
| Ivan Lozano | 80de6f3 | 2017-10-30 11:24:08 -0700 | [diff] [blame] | 54 | return (lz > rz) ? 1 : -1; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 55 |  | 
| Ivan Lozano | 80de6f3 | 2017-10-30 11:24:08 -0700 | [diff] [blame] | 56 | if (l->sequence == r->sequence) | 
|  | 57 | return 0; | 
|  | 58 |  | 
|  | 59 | return (l->sequence > r->sequence) ? 1 : -1; | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
| Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 62 | void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const { | 
| Robert Carr | 1f0a16a | 2016-10-24 16:27:39 -0700 | [diff] [blame] | 63 | for (size_t i = 0; i < size(); i++) { | 
| Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 64 | const auto& layer = (*this)[i]; | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 65 | auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState() | 
|  | 66 | : layer->getDrawingState(); | 
|  | 67 | if (state.zOrderRelativeOf != nullptr) { | 
| Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 68 | continue; | 
|  | 69 | } | 
| Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 70 | layer->traverseInZOrder(stateSet, visitor); | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 71 | } | 
|  | 72 | } | 
|  | 73 |  | 
| Dan Stoza | 412903f | 2017-04-27 13:42:17 -0700 | [diff] [blame] | 74 | void LayerVector::traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const { | 
| Robert Carr | 2047fae | 2016-11-28 14:09:09 -0800 | [diff] [blame] | 75 | for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) { | 
| Robert Carr | db66e62 | 2017-04-10 16:55:57 -0700 | [diff] [blame] | 76 | const auto& layer = (*this)[i]; | 
| Lloyd Pique | 0449b0f | 2018-12-20 16:23:45 -0800 | [diff] [blame] | 77 | auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState() | 
|  | 78 | : layer->getDrawingState(); | 
|  | 79 | if (state.zOrderRelativeOf != nullptr) { | 
| 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 | } | 
|  | 85 | } // namespace android |