blob: 6ac4b14df1c8eb2bf27a65719dc31652e2ef93f4 [file] [log] [blame]
Robert Carr2047fae2016-11-28 14:09:09 -08001/*
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
20namespace android {
21LayerVector::LayerVector(const LayerVector& rhs) : SortedVector<sp<Layer>>(rhs) {
22}
23
24int LayerVector::do_compare(const void* lhs, const void* rhs) const
25{
26 // sort layers per layer-stack, then by z-order and finally by sequence
27 const auto& l = *reinterpret_cast<const sp<Layer>*>(lhs);
28 const auto& r = *reinterpret_cast<const sp<Layer>*>(rhs);
29
30 uint32_t ls = l->getCurrentState().layerStack;
31 uint32_t rs = r->getCurrentState().layerStack;
32 if (ls != rs)
33 return ls - rs;
34
35 uint32_t lz = l->getCurrentState().z;
36 uint32_t rz = r->getCurrentState().z;
37 if (lz != rz)
38 return lz - rz;
39
40 return l->sequence - r->sequence;
41}
42
43void LayerVector::traverseInZOrder(const std::function<void(Layer*)>& consume) const {
44 const size_t n = size();
45 for (size_t i = 0; i < n; i++) {
46 consume((*this)[i].get());
47 }
48}
49
50void LayerVector::traverseInReverseZOrder(const std::function<void(Layer*)>& consume) const {
51 for (auto i = static_cast<int64_t>(size()) - 1; i >= 0; i--) {
52 consume((*this)[i].get());
53 }
54}
55} // namespace android