blob: 43a4397899fc56cc88ca68a2cc48e2ddc52ccf8a [file] [log] [blame]
Vishnu Naire14c6b32022-08-06 04:20:15 +00001/*
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// #define LOG_NDEBUG 0
18#undef LOG_TAG
Vishnu Nairc6384702023-07-31 12:22:20 -070019#define LOG_TAG "SurfaceFlinger"
Vishnu Naire14c6b32022-08-06 04:20:15 +000020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
22#include <gui/GLConsumer.h>
23#include <gui/TraceUtils.h>
24#include <math/vec3.h>
25#include <system/window.h>
26#include <utils/Log.h>
27
Vishnu Naire14c6b32022-08-06 04:20:15 +000028#include "LayerFE.h"
Leon Scroggins III85d4b222023-05-09 13:58:18 -040029#include "SurfaceFlinger.h"
Vishnu Naire14c6b32022-08-06 04:20:15 +000030
31namespace android {
32
33namespace {
34constexpr float defaultMaxLuminance = 1000.0;
35
36constexpr mat4 inverseOrientation(uint32_t transform) {
37 const mat4 flipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
38 const mat4 flipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
39 const mat4 rot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
40 mat4 tr;
41
42 if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
43 tr = tr * rot90;
44 }
45 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
46 tr = tr * flipH;
47 }
48 if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
49 tr = tr * flipV;
50 }
51 return inverse(tr);
52}
53
54FloatRect reduce(const FloatRect& win, const Region& exclude) {
55 if (CC_LIKELY(exclude.isEmpty())) {
56 return win;
57 }
58 // Convert through Rect (by rounding) for lack of FloatRegion
59 return Region(Rect{win}).subtract(exclude).getBounds().toFloatRect();
60}
61
62// Computes the transform matrix using the setFilteringEnabled to determine whether the
63// transform matrix should be computed for use with bilinear filtering.
64void getDrawingTransformMatrix(const std::shared_ptr<renderengine::ExternalTexture>& buffer,
65 Rect bufferCrop, uint32_t bufferTransform, bool filteringEnabled,
66 float outMatrix[16]) {
67 if (!buffer) {
68 ALOGE("Buffer should not be null!");
69 return;
70 }
71 GLConsumer::computeTransformMatrix(outMatrix, static_cast<float>(buffer->getWidth()),
72 static_cast<float>(buffer->getHeight()),
73 buffer->getPixelFormat(), bufferCrop, bufferTransform,
74 filteringEnabled);
75}
76
77} // namespace
78
79LayerFE::LayerFE(const std::string& name) : mName(name) {}
80
81const compositionengine::LayerFECompositionState* LayerFE::getCompositionState() const {
82 return mSnapshot.get();
83}
84
Melody Hsuc949cde2024-03-12 01:43:34 +000085bool LayerFE::onPreComposition(bool) {
Vishnu Naire14c6b32022-08-06 04:20:15 +000086 return mSnapshot->hasReadyFrame;
87}
88
89std::optional<compositionengine::LayerFE::LayerSettings> LayerFE::prepareClientComposition(
90 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) const {
91 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
92 prepareClientCompositionInternal(targetSettings);
93 // Nothing to render.
94 if (!layerSettings) {
95 return {};
96 }
97
98 // HWC requests to clear this layer.
99 if (targetSettings.clearContent) {
100 prepareClearClientComposition(*layerSettings, false /* blackout */);
101 return layerSettings;
102 }
103
104 // set the shadow for the layer if needed
105 prepareShadowClientComposition(*layerSettings, targetSettings.viewport);
106
107 return layerSettings;
108}
109
110std::optional<compositionengine::LayerFE::LayerSettings> LayerFE::prepareClientCompositionInternal(
111 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) const {
112 ATRACE_CALL();
113 compositionengine::LayerFE::LayerSettings layerSettings;
114 layerSettings.geometry.boundaries =
115 reduce(mSnapshot->geomLayerBounds, mSnapshot->transparentRegionHint);
116 layerSettings.geometry.positionTransform = mSnapshot->geomLayerTransform.asMatrix4();
117
118 // skip drawing content if the targetSettings indicate the content will be occluded
119 const bool drawContent = targetSettings.realContentIsVisible || targetSettings.clearContent;
120 layerSettings.skipContentDraw = !drawContent;
121
122 if (!mSnapshot->colorTransformIsIdentity) {
123 layerSettings.colorTransform = mSnapshot->colorTransform;
124 }
125
126 const auto& roundedCornerState = mSnapshot->roundedCorner;
127 layerSettings.geometry.roundedCornersRadius = roundedCornerState.radius;
128 layerSettings.geometry.roundedCornersCrop = roundedCornerState.cropRect;
129
130 layerSettings.alpha = mSnapshot->alpha;
131 layerSettings.sourceDataspace = mSnapshot->dataspace;
132
133 // Override the dataspace transfer from 170M to sRGB if the device configuration requests this.
134 // We do this here instead of in buffer info so that dumpsys can still report layers that are
135 // using the 170M transfer.
136 if (targetSettings.treat170mAsSrgb &&
137 (layerSettings.sourceDataspace & HAL_DATASPACE_TRANSFER_MASK) ==
138 HAL_DATASPACE_TRANSFER_SMPTE_170M) {
139 layerSettings.sourceDataspace = static_cast<ui::Dataspace>(
140 (layerSettings.sourceDataspace & HAL_DATASPACE_STANDARD_MASK) |
141 (layerSettings.sourceDataspace & HAL_DATASPACE_RANGE_MASK) |
142 HAL_DATASPACE_TRANSFER_SRGB);
143 }
144
145 layerSettings.whitePointNits = targetSettings.whitePointNits;
146 switch (targetSettings.blurSetting) {
147 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Enabled:
148 layerSettings.backgroundBlurRadius = mSnapshot->backgroundBlurRadius;
149 layerSettings.blurRegions = mSnapshot->blurRegions;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000150 layerSettings.blurRegionTransform = mSnapshot->localTransformInverse.asMatrix4();
Vishnu Naire14c6b32022-08-06 04:20:15 +0000151 break;
152 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BackgroundBlurOnly:
153 layerSettings.backgroundBlurRadius = mSnapshot->backgroundBlurRadius;
154 break;
155 case LayerFE::ClientCompositionTargetSettings::BlurSetting::BlurRegionsOnly:
156 layerSettings.blurRegions = mSnapshot->blurRegions;
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000157 layerSettings.blurRegionTransform = mSnapshot->localTransformInverse.asMatrix4();
Vishnu Naire14c6b32022-08-06 04:20:15 +0000158 break;
159 case LayerFE::ClientCompositionTargetSettings::BlurSetting::Disabled:
160 default:
161 break;
162 }
163 layerSettings.stretchEffect = mSnapshot->stretchEffect;
164 // Record the name of the layer for debugging further down the stack.
165 layerSettings.name = mSnapshot->name;
166
167 if (hasEffect() && !hasBufferOrSidebandStream()) {
168 prepareEffectsClientComposition(layerSettings, targetSettings);
169 return layerSettings;
170 }
171
172 prepareBufferStateClientComposition(layerSettings, targetSettings);
173 return layerSettings;
174}
175
176void LayerFE::prepareClearClientComposition(LayerFE::LayerSettings& layerSettings,
177 bool blackout) const {
178 layerSettings.source.buffer.buffer = nullptr;
179 layerSettings.source.solidColor = half3(0.0f, 0.0f, 0.0f);
180 layerSettings.disableBlending = true;
181 layerSettings.bufferId = 0;
182 layerSettings.frameNumber = 0;
183
184 // If layer is blacked out, force alpha to 1 so that we draw a black color layer.
185 layerSettings.alpha = blackout ? 1.0f : 0.0f;
186 layerSettings.name = mSnapshot->name;
187}
188
189void LayerFE::prepareEffectsClientComposition(
190 compositionengine::LayerFE::LayerSettings& layerSettings,
191 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) const {
192 // If fill bounds are occluded or the fill color is invalid skip the fill settings.
193 if (targetSettings.realContentIsVisible && fillsColor()) {
194 // Set color for color fill settings.
195 layerSettings.source.solidColor = mSnapshot->color.rgb;
196 } else if (hasBlur() || drawShadows()) {
197 layerSettings.skipContentDraw = true;
198 }
199}
200
201void LayerFE::prepareBufferStateClientComposition(
202 compositionengine::LayerFE::LayerSettings& layerSettings,
203 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) const {
204 ATRACE_CALL();
205 if (CC_UNLIKELY(!mSnapshot->externalTexture)) {
206 // If there is no buffer for the layer or we have sidebandstream where there is no
207 // activeBuffer, then we need to return LayerSettings.
208 return;
209 }
Chavi Weingarten18fa7c62023-11-28 21:16:03 +0000210 bool blackOutLayer;
211 if (FlagManager::getInstance().display_protected()) {
212 blackOutLayer = (mSnapshot->hasProtectedContent && !targetSettings.isProtected) ||
213 (mSnapshot->isSecure && !targetSettings.isSecure);
214 } else {
215 blackOutLayer = (mSnapshot->hasProtectedContent && !targetSettings.isProtected) ||
216 ((mSnapshot->isSecure || mSnapshot->hasProtectedContent) &&
217 !targetSettings.isSecure);
218 }
Vishnu Naire14c6b32022-08-06 04:20:15 +0000219 const bool bufferCanBeUsedAsHwTexture =
220 mSnapshot->externalTexture->getUsage() & GraphicBuffer::USAGE_HW_TEXTURE;
221 if (blackOutLayer || !bufferCanBeUsedAsHwTexture) {
222 ALOGE_IF(!bufferCanBeUsedAsHwTexture, "%s is blacked out as buffer is not gpu readable",
223 mSnapshot->name.c_str());
224 prepareClearClientComposition(layerSettings, true /* blackout */);
225 return;
226 }
227
228 layerSettings.source.buffer.buffer = mSnapshot->externalTexture;
229 layerSettings.source.buffer.isOpaque = mSnapshot->contentOpaque;
230 layerSettings.source.buffer.fence = mSnapshot->acquireFence;
Vishnu Naire14c6b32022-08-06 04:20:15 +0000231 layerSettings.source.buffer.usePremultipliedAlpha = mSnapshot->premultipliedAlpha;
Vishnu Naire14c6b32022-08-06 04:20:15 +0000232 bool hasSmpte2086 = mSnapshot->hdrMetadata.validTypes & HdrMetadata::SMPTE2086;
233 bool hasCta861_3 = mSnapshot->hdrMetadata.validTypes & HdrMetadata::CTA861_3;
234 float maxLuminance = 0.f;
235 if (hasSmpte2086 && hasCta861_3) {
236 maxLuminance = std::min(mSnapshot->hdrMetadata.smpte2086.maxLuminance,
237 mSnapshot->hdrMetadata.cta8613.maxContentLightLevel);
238 } else if (hasSmpte2086) {
239 maxLuminance = mSnapshot->hdrMetadata.smpte2086.maxLuminance;
240 } else if (hasCta861_3) {
241 maxLuminance = mSnapshot->hdrMetadata.cta8613.maxContentLightLevel;
242 } else {
243 switch (layerSettings.sourceDataspace & HAL_DATASPACE_TRANSFER_MASK) {
244 case HAL_DATASPACE_TRANSFER_ST2084:
245 case HAL_DATASPACE_TRANSFER_HLG:
246 // Behavior-match previous releases for HDR content
247 maxLuminance = defaultMaxLuminance;
248 break;
249 }
250 }
251 layerSettings.source.buffer.maxLuminanceNits = maxLuminance;
252 layerSettings.frameNumber = mSnapshot->frameNumber;
253 layerSettings.bufferId = mSnapshot->externalTexture->getId();
254
Sally Qi380ac3e2023-10-10 20:27:02 +0000255 const bool useFiltering = targetSettings.needsFiltering ||
256 mSnapshot->geomLayerTransform.needsBilinearFiltering();
257
Vishnu Naire14c6b32022-08-06 04:20:15 +0000258 // Query the texture matrix given our current filtering mode.
259 float textureMatrix[16];
260 getDrawingTransformMatrix(layerSettings.source.buffer.buffer, mSnapshot->geomContentCrop,
Sally Qi380ac3e2023-10-10 20:27:02 +0000261 mSnapshot->geomBufferTransform, useFiltering,
Patrick Williams278a88f2023-01-27 16:52:40 -0600262 textureMatrix);
Vishnu Naire14c6b32022-08-06 04:20:15 +0000263
264 if (mSnapshot->geomBufferUsesDisplayInverseTransform) {
265 /*
266 * the code below applies the primary display's inverse transform to
267 * the texture transform
268 */
Leon Scroggins III85d4b222023-05-09 13:58:18 -0400269 uint32_t transform = SurfaceFlinger::getActiveDisplayRotationFlags();
Vishnu Naire14c6b32022-08-06 04:20:15 +0000270 mat4 tr = inverseOrientation(transform);
271
272 /**
273 * TODO(b/36727915): This is basically a hack.
274 *
275 * Ensure that regardless of the parent transformation,
276 * this buffer is always transformed from native display
277 * orientation to display orientation. For example, in the case
278 * of a camera where the buffer remains in native orientation,
279 * we want the pixels to always be upright.
280 */
Vishnu Nair8fc721b2022-12-22 20:06:32 +0000281 const auto parentTransform = mSnapshot->parentTransform;
Vishnu Naire14c6b32022-08-06 04:20:15 +0000282 tr = tr * inverseOrientation(parentTransform.getOrientation());
283
284 // and finally apply it to the original texture matrix
285 const mat4 texTransform(mat4(static_cast<const float*>(textureMatrix)) * tr);
286 memcpy(textureMatrix, texTransform.asArray(), sizeof(textureMatrix));
287 }
288
289 const Rect win{layerSettings.geometry.boundaries};
290 float bufferWidth = static_cast<float>(mSnapshot->bufferSize.getWidth());
291 float bufferHeight = static_cast<float>(mSnapshot->bufferSize.getHeight());
292
293 // Layers can have a "buffer size" of [0, 0, -1, -1] when no display frame has
294 // been set and there is no parent layer bounds. In that case, the scale is meaningless so
295 // ignore them.
296 if (!mSnapshot->bufferSize.isValid()) {
297 bufferWidth = float(win.right) - float(win.left);
298 bufferHeight = float(win.bottom) - float(win.top);
299 }
300
301 const float scaleHeight = (float(win.bottom) - float(win.top)) / bufferHeight;
302 const float scaleWidth = (float(win.right) - float(win.left)) / bufferWidth;
303 const float translateY = float(win.top) / bufferHeight;
304 const float translateX = float(win.left) / bufferWidth;
305
306 // Flip y-coordinates because GLConsumer expects OpenGL convention.
307 mat4 tr = mat4::translate(vec4(.5f, .5f, 0.f, 1.f)) * mat4::scale(vec4(1.f, -1.f, 1.f, 1.f)) *
308 mat4::translate(vec4(-.5f, -.5f, 0.f, 1.f)) *
309 mat4::translate(vec4(translateX, translateY, 0.f, 1.f)) *
310 mat4::scale(vec4(scaleWidth, scaleHeight, 1.0f, 1.0f));
311
Sally Qi380ac3e2023-10-10 20:27:02 +0000312 layerSettings.source.buffer.useTextureFiltering = useFiltering;
Vishnu Naire14c6b32022-08-06 04:20:15 +0000313 layerSettings.source.buffer.textureTransform =
314 mat4(static_cast<const float*>(textureMatrix)) * tr;
315
316 return;
317}
318
319void LayerFE::prepareShadowClientComposition(LayerFE::LayerSettings& caster,
320 const Rect& layerStackRect) const {
Vishnu Naird9e4f462023-10-06 04:05:45 +0000321 ShadowSettings state = mSnapshot->shadowSettings;
Vishnu Naire14c6b32022-08-06 04:20:15 +0000322 if (state.length <= 0.f || (state.ambientColor.a <= 0.f && state.spotColor.a <= 0.f)) {
323 return;
324 }
325
326 // Shift the spot light x-position to the middle of the display and then
327 // offset it by casting layer's screen pos.
328 state.lightPos.x =
329 (static_cast<float>(layerStackRect.width()) / 2.f) - mSnapshot->transformedBounds.left;
330 state.lightPos.y -= mSnapshot->transformedBounds.top;
331 caster.shadow = state;
332}
333
Vishnu Nair7ee4f462023-04-19 09:54:09 -0700334void LayerFE::onLayerDisplayed(ftl::SharedFuture<FenceResult> futureFenceResult,
335 ui::LayerStack layerStack) {
336 mCompositionResult.releaseFences.emplace_back(std::move(futureFenceResult), layerStack);
Vishnu Naire14c6b32022-08-06 04:20:15 +0000337}
338
339CompositionResult&& LayerFE::stealCompositionResult() {
340 return std::move(mCompositionResult);
341}
342
343const char* LayerFE::getDebugName() const {
344 return mName.c_str();
345}
346
347const LayerMetadata* LayerFE::getMetadata() const {
348 return &mSnapshot->layerMetadata;
349}
350
351const LayerMetadata* LayerFE::getRelativeMetadata() const {
352 return &mSnapshot->relativeLayerMetadata;
353}
354
355int32_t LayerFE::getSequence() const {
Vishnu Nair269f69d2023-09-08 11:45:26 -0700356 return static_cast<int32_t>(mSnapshot->uniqueSequence);
Vishnu Naire14c6b32022-08-06 04:20:15 +0000357}
358
359bool LayerFE::hasRoundedCorners() const {
360 return mSnapshot->roundedCorner.hasRoundedCorners();
361}
362
363void LayerFE::setWasClientComposed(const sp<Fence>& fence) {
364 mCompositionResult.lastClientCompositionFence = fence;
365}
366
367bool LayerFE::hasBufferOrSidebandStream() const {
368 return mSnapshot->externalTexture || mSnapshot->sidebandStream;
369}
370
371bool LayerFE::fillsColor() const {
372 return mSnapshot->color.r >= 0.0_hf && mSnapshot->color.g >= 0.0_hf &&
373 mSnapshot->color.b >= 0.0_hf;
374}
375
376bool LayerFE::hasBlur() const {
377 return mSnapshot->backgroundBlurRadius > 0 || mSnapshot->blurRegions.size() > 0;
378}
379
380bool LayerFE::drawShadows() const {
381 return mSnapshot->shadowSettings.length > 0.f &&
382 (mSnapshot->shadowSettings.ambientColor.a > 0 ||
383 mSnapshot->shadowSettings.spotColor.a > 0);
384};
385
386const sp<GraphicBuffer> LayerFE::getBuffer() const {
387 return mSnapshot->externalTexture ? mSnapshot->externalTexture->getBuffer() : nullptr;
388}
389
390} // namespace android