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 
22 LayerVector::LayerVector() = default;
23 
LayerVector(const LayerVector & rhs)24 LayerVector::LayerVector(const LayerVector& rhs) : SortedVector<sp<Layer>>(rhs) {
25 }
26 
27 LayerVector::~LayerVector() = default;
28 
do_compare(const void * lhs,const void * rhs) const29 int LayerVector::do_compare(const void* lhs, const void* rhs) const
30 {
31     // sort layers per layer-stack, then by z-order and finally by sequence
32     const auto& l = *reinterpret_cast<const sp<Layer>*>(lhs);
33     const auto& r = *reinterpret_cast<const sp<Layer>*>(rhs);
34 
35     uint32_t ls = l->getCurrentState().layerStack;
36     uint32_t rs = r->getCurrentState().layerStack;
37     if (ls != rs)
38         return ls - rs;
39 
40     uint32_t lz = l->getCurrentState().z;
41     uint32_t rz = r->getCurrentState().z;
42     if (lz != rz)
43         return lz - rz;
44 
45     return l->sequence - r->sequence;
46 }
47 
traverseInZOrder(StateSet stateSet,const Visitor & visitor) const48 void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const {
49     for (size_t i = 0; i < size(); i++) {
50         const auto& layer = (*this)[i];
51         auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
52                                                       : layer->getDrawingState();
53         if (state.zOrderRelativeOf != nullptr) {
54             continue;
55         }
56         layer->traverseInZOrder(stateSet, visitor);
57     }
58 }
59 
traverseInReverseZOrder(StateSet stateSet,const Visitor & visitor) const60 void LayerVector::traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const {
61     for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) {
62         const auto& layer = (*this)[i];
63         auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
64                                                       : layer->getDrawingState();
65         if (state.zOrderRelativeOf != nullptr) {
66             continue;
67         }
68         layer->traverseInReverseZOrder(stateSet, visitor);
69      }
70 }
71 } // namespace android
72