1 /*
2  * Copyright 2022 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 <aidl/android/hardware/graphics/composer3/Composition.h>
20 #include <ftl/flags.h>
21 #include <gui/LayerState.h>
22 #include <renderengine/ExternalTexture.h>
23 #include "Scheduler/LayerInfo.h"
24 
25 #include "LayerCreationArgs.h"
26 #include "TransactionState.h"
27 
28 namespace android::surfaceflinger::frontend {
29 using namespace ftl::flag_operators;
30 
31 // Stores client requested states for a layer.
32 // This struct does not store any other states or states pertaining to
33 // other layers. Links to other layers that are part of the client
34 // requested state such as parent are translated to layer id so
35 // we can avoid extending the lifetime of layer handles.
36 struct RequestedLayerState : layer_state_t {
37     // Changes in state after merging with new state. This includes additional state
38     // changes found in layer_state_t::what.
39     enum class Changes : uint32_t {
40         Created = 1u << 0,
41         Destroyed = 1u << 1,
42         Hierarchy = 1u << 2,
43         Geometry = 1u << 3,
44         Content = 1u << 4,
45         Input = 1u << 5,
46         Z = 1u << 6,
47         Mirror = 1u << 7,
48         Parent = 1u << 8,
49         RelativeParent = 1u << 9,
50         Metadata = 1u << 10,
51         Visibility = 1u << 11,
52         AffectsChildren = 1u << 12,
53         FrameRate = 1u << 13,
54         VisibleRegion = 1u << 14,
55         Buffer = 1u << 15,
56         SidebandStream = 1u << 16,
57         Animation = 1u << 17,
58         BufferSize = 1u << 18,
59         GameMode = 1u << 19,
60         BufferUsageFlags = 1u << 20,
61     };
62 
63     static constexpr ftl::Flags<Changes> kMustComposite = Changes::Created | Changes::Destroyed |
64             Changes::Hierarchy | Changes::Geometry | Changes::Content | Changes::Input |
65             Changes::Z | Changes::Mirror | Changes::Parent | Changes::RelativeParent |
66             Changes::Metadata | Changes::Visibility | Changes::VisibleRegion | Changes::Buffer |
67             Changes::SidebandStream | Changes::Animation | Changes::BufferSize | Changes::GameMode |
68             Changes::BufferUsageFlags;
69     static Rect reduce(const Rect& win, const Region& exclude);
70     RequestedLayerState(const LayerCreationArgs&);
71     void merge(const ResolvedComposerState&);
72     void clearChanges();
73 
74     // Currently we only care about the primary display
75     ui::Transform getTransform(uint32_t displayRotationFlags) const;
76     ui::Size getUnrotatedBufferSize(uint32_t displayRotationFlags) const;
77     bool canBeDestroyed() const;
78     bool isRoot() const;
79     bool isHiddenByPolicy() const;
80     half4 getColor() const;
81     Rect getBufferSize(uint32_t displayRotationFlags) const;
82     Rect getCroppedBufferSize(const Rect& bufferSize) const;
83     Rect getBufferCrop() const;
84     std::string getDebugString() const;
85     std::string getDebugStringShort() const;
86     friend std::ostream& operator<<(std::ostream& os, const RequestedLayerState& obj);
87     aidl::android::hardware::graphics::composer3::Composition getCompositionType() const;
88     bool hasValidRelativeParent() const;
89     bool hasInputInfo() const;
90     bool hasBlur() const;
91     bool hasFrameUpdate() const;
92     bool hasReadyFrame() const;
93     bool hasSidebandStreamFrame() const;
94     bool willReleaseBufferOnLatch() const;
95     bool backpressureEnabled() const;
96     bool isSimpleBufferUpdate(const layer_state_t&) const;
97     bool isProtected() const;
98     bool hasSomethingToDraw() const;
99 
100     // Layer serial number.  This gives layers an explicit ordering, so we
101     // have a stable sort order when their layer stack and Z-order are
102     // the same.
103     const uint32_t id;
104     const std::string name;
105     bool canBeRoot = false;
106     const uint32_t layerCreationFlags;
107     // The owner of the layer. If created from a non system process, it will be the calling uid.
108     // If created from a system process, the value can be passed in.
109     const gui::Uid ownerUid;
110     // The owner pid of the layer. If created from a non system process, it will be the calling pid.
111     // If created from a system process, the value can be passed in.
112     const gui::Pid ownerPid;
113     bool dataspaceRequested;
114     bool hasColorTransform;
115     bool premultipliedAlpha{true};
116     // This layer can be a cursor on some displays.
117     bool potentialCursor{false};
118     bool protectedByApp{false}; // application requires protected path to external sink
119     ui::Transform requestedTransform;
120     std::shared_ptr<FenceTime> acquireFenceTime;
121     std::shared_ptr<renderengine::ExternalTexture> externalTexture;
122     gui::GameMode gameMode;
123     scheduler::LayerInfo::FrameRate requestedFrameRate;
124     uint32_t parentId = UNASSIGNED_LAYER_ID;
125     uint32_t relativeParentId = UNASSIGNED_LAYER_ID;
126     uint32_t layerIdToMirror = UNASSIGNED_LAYER_ID;
127     ui::LayerStack layerStackToMirror = ui::INVALID_LAYER_STACK;
128     uint32_t touchCropId = UNASSIGNED_LAYER_ID;
129     uint32_t bgColorLayerId = UNASSIGNED_LAYER_ID;
130     uint64_t barrierFrameNumber = 0;
131     uint32_t barrierProducerId = 0;
132     std::string debugName;
133 
134     // book keeping states
135     bool handleAlive = true;
136     bool isRelativeOf = false;
137     std::vector<uint32_t> mirrorIds{};
138     ftl::Flags<RequestedLayerState::Changes> changes;
139     bool bgColorLayer = false;
140 };
141 
142 } // namespace android::surfaceflinger::frontend
143