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