• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 #pragma once
18 
19 #include <cstdint>
20 
21 #include <ftl/cast.h>
22 #include <ftl/string.h>
23 #include <log/log.h>
24 
25 namespace android::ui {
26 
27 // A LayerStack identifies a Z-ordered group of layers. A layer can only be associated to a single
28 // LayerStack, but a LayerStack can be associated to multiple displays, mirroring the same content.
29 struct LayerStack {
30     uint32_t id = UINT32_MAX;
31 
32     template <typename T>
fromValueLayerStack33     static constexpr LayerStack fromValue(T v) {
34         if (ftl::cast_safety<uint32_t>(v) == ftl::CastSafety::kSafe) {
35             return {static_cast<uint32_t>(v)};
36         }
37 
38         ALOGW("Invalid layer stack %s", ftl::to_string(v).c_str());
39         return {};
40     }
41 };
42 
43 constexpr LayerStack INVALID_LAYER_STACK;
44 constexpr LayerStack DEFAULT_LAYER_STACK{0u};
45 
46 inline bool operator==(LayerStack lhs, LayerStack rhs) {
47     return lhs.id == rhs.id;
48 }
49 
50 inline bool operator!=(LayerStack lhs, LayerStack rhs) {
51     return !(lhs == rhs);
52 }
53 
54 inline bool operator>(LayerStack lhs, LayerStack rhs) {
55     return lhs.id > rhs.id;
56 }
57 
58 inline bool operator<(LayerStack lhs, LayerStack rhs) {
59     return lhs.id < rhs.id;
60 }
61 
62 // A LayerFilter determines if a layer is included for output to a display.
63 struct LayerFilter {
64     LayerStack layerStack;
65 
66     // True if the layer is only output to internal displays, i.e. excluded from screenshots, screen
67     // recordings, and mirroring to virtual or external displays. Used for display cutout overlays.
68     bool toInternalDisplay = false;
69 
70     // Returns true if the input filter can be output to this filter.
includesLayerFilter71     bool includes(LayerFilter other) const {
72         // The layer stacks must match.
73         if (other.layerStack == INVALID_LAYER_STACK || other.layerStack != layerStack) {
74             return false;
75         }
76 
77         // The output must be to an internal display if the input filter has that constraint.
78         return !other.toInternalDisplay || toInternalDisplay;
79     }
80 };
81 
82 } // namespace android::ui
83