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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20 
21 #include "LayerVector.h"
22 #include "Layer.h"
23 
24 namespace android {
25 
LayerVector(const StateSet stateSet)26 LayerVector::LayerVector(const StateSet stateSet) : mStateSet(stateSet) {}
27 
LayerVector(const LayerVector & rhs,const StateSet stateSet)28 LayerVector::LayerVector(const LayerVector& rhs, const StateSet stateSet)
29       : SortedVector<sp<Layer>>(rhs), mStateSet(stateSet) {}
30 
31 LayerVector::~LayerVector() = default;
32 
33 // This operator override is needed to prevent mStateSet from getting copied over.
operator =(const LayerVector & rhs)34 LayerVector& LayerVector::operator=(const LayerVector& rhs) {
35     SortedVector::operator=(rhs);
36     return *this;
37 }
38 
do_compare(const void * lhs,const void * rhs) const39 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 
45     const auto& lState =
46             (mStateSet == StateSet::Current) ? l->getCurrentState() : l->getDrawingState();
47     const auto& rState =
48             (mStateSet == StateSet::Current) ? r->getCurrentState() : r->getDrawingState();
49 
50     uint32_t ls = lState.layerStack;
51     uint32_t rs = rState.layerStack;
52     if (ls != rs)
53         return (ls > rs) ? 1 : -1;
54 
55     int32_t lz = lState.z;
56     int32_t rz = rState.z;
57     if (lz != rz)
58         return (lz > rz) ? 1 : -1;
59 
60     if (l->sequence == r->sequence)
61         return 0;
62 
63     return (l->sequence > r->sequence) ? 1 : -1;
64 }
65 
traverseInZOrder(StateSet stateSet,const Visitor & visitor) const66 void LayerVector::traverseInZOrder(StateSet stateSet, const Visitor& visitor) const {
67     for (size_t i = 0; i < size(); i++) {
68         const auto& layer = (*this)[i];
69         auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
70                                                       : layer->getDrawingState();
71         if (state.isRelativeOf) {
72             continue;
73         }
74         layer->traverseInZOrder(stateSet, visitor);
75     }
76 }
77 
traverseInReverseZOrder(StateSet stateSet,const Visitor & visitor) const78 void LayerVector::traverseInReverseZOrder(StateSet stateSet, const Visitor& visitor) const {
79     for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) {
80         const auto& layer = (*this)[i];
81         auto& state = (stateSet == StateSet::Current) ? layer->getCurrentState()
82                                                       : layer->getDrawingState();
83         if (state.isRelativeOf) {
84             continue;
85         }
86         layer->traverseInReverseZOrder(stateSet, visitor);
87      }
88 }
89 
traverse(const Visitor & visitor) const90 void 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 
97 } // namespace android
98 
99 // TODO(b/129481165): remove the #pragma below and fix conversion issues
100 #pragma clang diagnostic pop // ignored "-Wconversion"
101