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 {
21
LayerVector(const StateSet stateSet)22 LayerVector::LayerVector(const StateSet stateSet) : mStateSet(stateSet) {}
23
LayerVector(const LayerVector & rhs,const StateSet stateSet)24 LayerVector::LayerVector(const LayerVector& rhs, const StateSet stateSet)
25 : SortedVector<sp<Layer>>(rhs), mStateSet(stateSet) {}
26
27 LayerVector::~LayerVector() = default;
28
29 // This operator override is needed to prevent mStateSet from getting copied over.
operator =(const LayerVector & rhs)30 LayerVector& LayerVector::operator=(const LayerVector& rhs) {
31 SortedVector::operator=(rhs);
32 return *this;
33 }
34
do_compare(const void * lhs,const void * rhs) const35 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
41 const auto& lState =
42 (mStateSet == StateSet::Current) ? l->getCurrentState() : l->getDrawingState();
43 const auto& rState =
44 (mStateSet == StateSet::Current) ? r->getCurrentState() : r->getDrawingState();
45
46 uint32_t ls = lState.layerStack;
47 uint32_t rs = rState.layerStack;
48 if (ls != rs)
49 return (ls > rs) ? 1 : -1;
50
51 int32_t lz = lState.z;
52 int32_t rz = rState.z;
53 if (lz != rz)
54 return (lz > rz) ? 1 : -1;
55
56 if (l->sequence == r->sequence)
57 return 0;
58
59 return (l->sequence > r->sequence) ? 1 : -1;
60 }
61
traverseInZOrder(StateSet stateSet,const Visitor & visitor) const62 void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const {
63 for (size_t i = 0; i < size(); i++) {
64 const auto& layer = (*this)[i];
65 auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
66 : layer->getDrawingState();
67 if (state.zOrderRelativeOf != nullptr) {
68 continue;
69 }
70 layer->traverseInZOrder(stateSet, visitor);
71 }
72 }
73
traverseInReverseZOrder(StateSet stateSet,const Visitor & visitor) const74 void LayerVector::traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const {
75 for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) {
76 const auto& layer = (*this)[i];
77 auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
78 : layer->getDrawingState();
79 if (state.zOrderRelativeOf != nullptr) {
80 continue;
81 }
82 layer->traverseInReverseZOrder(stateSet, visitor);
83 }
84 }
85 } // namespace android
86