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