• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 <optional>
20 #include <ostream>
21 #include <unordered_set>
22 
23 // TODO(b/129481165): remove the #pragma below and fix conversion issues
24 #pragma clang diagnostic push
25 #pragma clang diagnostic ignored "-Wconversion"
26 
27 #include <renderengine/LayerSettings.h>
28 
29 // TODO(b/129481165): remove the #pragma below and fix conversion issues
30 #pragma clang diagnostic pop // ignored "-Wconversion"
31 
32 #include <utils/RefBase.h>
33 #include <utils/Timers.h>
34 
35 namespace android {
36 
37 class Fence;
38 
39 namespace compositionengine {
40 
41 struct LayerFECompositionState;
42 
43 // Defines the interface used by the CompositionEngine to make requests
44 // of the front-end layer
45 class LayerFE : public virtual RefBase {
46 public:
47     // Gets the raw front-end composition state data for the layer
48     virtual const LayerFECompositionState* getCompositionState() const = 0;
49 
50     // Called before composition starts. Should return true if this layer has
51     // pending updates which would require an extra display refresh cycle to
52     // process.
53     virtual bool onPreComposition(nsecs_t refreshStartTime) = 0;
54 
55     // Used with latchCompositionState()
56     enum class StateSubset {
57         // Gets the basic geometry (bounds, transparent region, visibility,
58         // transforms, alpha) for the layer, for computing visibility and
59         // coverage.
60         BasicGeometry,
61 
62         // Gets the full geometry (crops, buffer transforms, metadata) and
63         // content (buffer or color) state for the layer.
64         GeometryAndContent,
65 
66         // Gets the per frame content (buffer or color) state for the layer.
67         Content,
68 
69         // Gets the cursor state for the layer.
70         Cursor,
71     };
72 
73     // Prepares the output-independent composition state for the layer. The
74     // StateSubset argument selects what portion of the state is actually needed
75     // by the CompositionEngine code, since computing everything may be
76     // expensive.
77     virtual void prepareCompositionState(StateSubset) = 0;
78 
79     struct ClientCompositionTargetSettings {
80         // The clip region, or visible region that is being rendered to
81         const Region& clip;
82 
83         // If true, the layer should use an identity transform for its position
84         // transform. Used only by the captureScreen API call.
85         const bool useIdentityTransform;
86 
87         // If set to true, the layer should enable filtering when rendering.
88         const bool needsFiltering;
89 
90         // If set to true, the buffer is being sent to a destination that is
91         // expected to treat the buffer contents as secure.
92         const bool isSecure;
93 
94         // If set to true, the target buffer has protected content support.
95         const bool supportsProtectedContent;
96 
97         // Modified by each call to prepareClientComposition to indicate the
98         // region of the target buffer that should be cleared.
99         Region& clearRegion;
100 
101         // Viewport of the target being rendered to. This is used to determine
102         // the shadow light position.
103         const Rect& viewport;
104 
105         // Dataspace of the output so we can optimize how to render the shadow
106         // by avoiding unnecessary color space conversions.
107         const ui::Dataspace dataspace;
108 
109         // True if the region excluding the shadow is visible.
110         const bool realContentIsVisible;
111 
112         // If set to true, change the layer settings to render a clear output.
113         // This may be requested by the HWC
114         const bool clearContent;
115     };
116 
117     // A superset of LayerSettings required by RenderEngine to compose a layer
118     // and buffer info to determine duplicate client composition requests.
119     struct LayerSettings : renderengine::LayerSettings {
120         // Currently latched buffer if, 0 if invalid.
121         uint64_t bufferId = 0;
122 
123         // Currently latched frame number, 0 if invalid.
124         uint64_t frameNumber = 0;
125     };
126 
127     // Returns the z-ordered list of LayerSettings to pass to RenderEngine::drawLayers. The list
128     // may contain shadows casted by the layer or the content of the layer itself.  If the layer
129     // does not render then an empty list will be returned.
130     virtual std::vector<LayerSettings> prepareClientCompositionList(
131             ClientCompositionTargetSettings&) = 0;
132 
133     // Called after the layer is displayed to update the presentation fence
134     virtual void onLayerDisplayed(const sp<Fence>&) = 0;
135 
136     // Gets some kind of identifier for the layer for debug purposes.
137     virtual const char* getDebugName() const = 0;
138 };
139 
140 // TODO(b/121291683): Specialize std::hash<> for sp<T> so these and others can
141 // be removed.
142 struct LayerFESpHash {
operatorLayerFESpHash143     size_t operator()(const sp<LayerFE>& p) const { return std::hash<LayerFE*>()(p.get()); }
144 };
145 
146 using LayerFESet = std::unordered_set<sp<LayerFE>, LayerFESpHash>;
147 
148 static inline bool operator==(const LayerFE::ClientCompositionTargetSettings& lhs,
149                               const LayerFE::ClientCompositionTargetSettings& rhs) {
150     return lhs.clip.hasSameRects(rhs.clip) &&
151             lhs.useIdentityTransform == rhs.useIdentityTransform &&
152             lhs.needsFiltering == rhs.needsFiltering && lhs.isSecure == rhs.isSecure &&
153             lhs.supportsProtectedContent == rhs.supportsProtectedContent &&
154             lhs.clearRegion.hasSameRects(rhs.clearRegion) && lhs.viewport == rhs.viewport &&
155             lhs.dataspace == rhs.dataspace &&
156             lhs.realContentIsVisible == rhs.realContentIsVisible &&
157             lhs.clearContent == rhs.clearContent;
158 }
159 
160 static inline bool operator==(const LayerFE::LayerSettings& lhs,
161                               const LayerFE::LayerSettings& rhs) {
162     return static_cast<const renderengine::LayerSettings&>(lhs) ==
163             static_cast<const renderengine::LayerSettings&>(rhs) &&
164             lhs.bufferId == rhs.bufferId && lhs.frameNumber == rhs.frameNumber;
165 }
166 
167 // Defining PrintTo helps with Google Tests.
PrintTo(const LayerFE::ClientCompositionTargetSettings & settings,::std::ostream * os)168 static inline void PrintTo(const LayerFE::ClientCompositionTargetSettings& settings,
169                            ::std::ostream* os) {
170     *os << "ClientCompositionTargetSettings{";
171     *os << "\n    .clip = \n";
172     PrintTo(settings.clip, os);
173     *os << "\n    .useIdentityTransform = " << settings.useIdentityTransform;
174     *os << "\n    .needsFiltering = " << settings.needsFiltering;
175     *os << "\n    .isSecure = " << settings.isSecure;
176     *os << "\n    .supportsProtectedContent = " << settings.supportsProtectedContent;
177     *os << "\n    .clearRegion = ";
178     PrintTo(settings.clearRegion, os);
179     *os << "\n    .viewport = ";
180     PrintTo(settings.viewport, os);
181     *os << "\n    .dataspace = ";
182     PrintTo(settings.dataspace, os);
183     *os << "\n    .realContentIsVisible = " << settings.realContentIsVisible;
184     *os << "\n    .clearContent = " << settings.clearContent;
185     *os << "\n}";
186 }
187 
PrintTo(const LayerFE::LayerSettings & settings,::std::ostream * os)188 static inline void PrintTo(const LayerFE::LayerSettings& settings, ::std::ostream* os) {
189     *os << "LayerFE::LayerSettings{";
190     PrintTo(static_cast<const renderengine::LayerSettings&>(settings), os);
191     *os << "\n    .bufferId = " << settings.bufferId;
192     *os << "\n    .frameNumber = " << settings.frameNumber;
193     *os << "\n}";
194 }
195 
196 } // namespace compositionengine
197 } // namespace android
198