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