Edge extension effect: shader reimplementation
X-axis activity transitions require the translation of the surfaces
involved. As this movement would create unwanted see-through, we would
have added side windows anchored to the moving activities, and textured
them by clamping their parents.
We are replacing the additional windows with the extension of the
surfaces, and filling the area without buffer with a shader.
See go/edge-extension-sksl for more info.
Bug: 322036393
Test: LayerSnapshotTest
Flag: EXEMPT (calls will be protected by wm shell with com.android.window.flags.edge_extension_shader)
Change-Id: I3682efd16a1b311d67a522bb85960f100948b2ea
diff --git a/libs/renderengine/skia/SkiaRenderEngine.cpp b/libs/renderengine/skia/SkiaRenderEngine.cpp
index 5f37125..a609f2d 100644
--- a/libs/renderengine/skia/SkiaRenderEngine.cpp
+++ b/libs/renderengine/skia/SkiaRenderEngine.cpp
@@ -507,16 +507,24 @@
const RuntimeEffectShaderParameters& parameters) {
// The given surface will be stretched by HWUI via matrix transformation
// which gets similar results for most surfaces
- // Determine later on if we need to leverage the stertch shader within
+ // Determine later on if we need to leverage the stretch shader within
// surface flinger
const auto& stretchEffect = parameters.layer.stretchEffect;
const auto& targetBuffer = parameters.layer.source.buffer.buffer;
+ const auto graphicBuffer = targetBuffer ? targetBuffer->getBuffer() : nullptr;
+
auto shader = parameters.shader;
- if (stretchEffect.hasEffect()) {
- const auto graphicBuffer = targetBuffer ? targetBuffer->getBuffer() : nullptr;
- if (graphicBuffer && shader) {
+ if (graphicBuffer && parameters.shader) {
+ if (stretchEffect.hasEffect()) {
shader = mStretchShaderFactory.createSkShader(shader, stretchEffect);
}
+ // The given surface requires to be filled outside of its buffer bounds if the edge
+ // extension is required
+ const auto& edgeExtensionEffect = parameters.layer.edgeExtensionEffect;
+ if (edgeExtensionEffect.hasEffect()) {
+ shader = mEdgeExtensionShaderFactory.createSkShader(shader, parameters.layer,
+ parameters.imageBounds);
+ }
}
if (parameters.requiresLinearEffect) {
@@ -566,8 +574,6 @@
parameters.layerDimmingRatio, 1.f));
}
- const auto targetBuffer = parameters.layer.source.buffer.buffer;
- const auto graphicBuffer = targetBuffer ? targetBuffer->getBuffer() : nullptr;
const auto hardwareBuffer = graphicBuffer ? graphicBuffer->toAHardwareBuffer() : nullptr;
return createLinearEffectShader(shader, effect, runtimeEffect, std::move(colorTransform),
parameters.display.maxLuminance,
@@ -1039,18 +1045,20 @@
toSkColorSpace(layerDataspace)));
}
- paint.setShader(createRuntimeEffectShader(
- RuntimeEffectShaderParameters{.shader = shader,
- .layer = layer,
- .display = display,
- .undoPremultipliedAlpha = !item.isOpaque &&
- item.usePremultipliedAlpha,
- .requiresLinearEffect = requiresLinearEffect,
- .layerDimmingRatio = dimInLinearSpace
- ? layerDimmingRatio
- : 1.f,
- .outputDataSpace = display.outputDataspace,
- .fakeOutputDataspace = fakeDataspace}));
+ SkRect imageBounds;
+ matrix.mapRect(&imageBounds, SkRect::Make(image->bounds()));
+
+ paint.setShader(createRuntimeEffectShader(RuntimeEffectShaderParameters{
+ .shader = shader,
+ .layer = layer,
+ .display = display,
+ .undoPremultipliedAlpha = !item.isOpaque && item.usePremultipliedAlpha,
+ .requiresLinearEffect = requiresLinearEffect,
+ .layerDimmingRatio = dimInLinearSpace ? layerDimmingRatio : 1.f,
+ .outputDataSpace = display.outputDataspace,
+ .fakeOutputDataspace = fakeDataspace,
+ .imageBounds = imageBounds,
+ }));
// Turn on dithering when dimming beyond this (arbitrary) threshold...
static constexpr float kDimmingThreshold = 0.9f;
@@ -1118,7 +1126,8 @@
.requiresLinearEffect = requiresLinearEffect,
.layerDimmingRatio = layerDimmingRatio,
.outputDataSpace = display.outputDataspace,
- .fakeOutputDataspace = fakeDataspace}));
+ .fakeOutputDataspace = fakeDataspace,
+ .imageBounds = SkRect::MakeEmpty()}));
}
if (layer.disableBlending) {
diff --git a/libs/renderengine/skia/SkiaRenderEngine.h b/libs/renderengine/skia/SkiaRenderEngine.h
index c8f9241..224a1ca 100644
--- a/libs/renderengine/skia/SkiaRenderEngine.h
+++ b/libs/renderengine/skia/SkiaRenderEngine.h
@@ -38,6 +38,7 @@
#include "compat/SkiaGpuContext.h"
#include "debug/SkiaCapture.h"
#include "filters/BlurFilter.h"
+#include "filters/EdgeExtensionShaderFactory.h"
#include "filters/LinearEffect.h"
#include "filters/StretchShaderFactory.h"
@@ -156,6 +157,7 @@
float layerDimmingRatio;
const ui::Dataspace outputDataSpace;
const ui::Dataspace fakeOutputDataspace;
+ const SkRect& imageBounds;
};
sk_sp<SkShader> createRuntimeEffectShader(const RuntimeEffectShaderParameters&);
@@ -175,6 +177,7 @@
AutoBackendTexture::CleanupManager mTextureCleanupMgr GUARDED_BY(mRenderingMutex);
StretchShaderFactory mStretchShaderFactory;
+ EdgeExtensionShaderFactory mEdgeExtensionShaderFactory;
sp<Fence> mLastDrawFence;
BlurFilter* mBlurFilter = nullptr;
diff --git a/libs/renderengine/skia/filters/EdgeExtensionShaderFactory.cpp b/libs/renderengine/skia/filters/EdgeExtensionShaderFactory.cpp
new file mode 100644
index 0000000..1dbcc29
--- /dev/null
+++ b/libs/renderengine/skia/filters/EdgeExtensionShaderFactory.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "EdgeExtensionShaderFactory.h"
+#include <SkPoint.h>
+#include <SkRuntimeEffect.h>
+#include <SkStream.h>
+#include <SkString.h>
+#include "log/log_main.h"
+
+namespace android::renderengine::skia {
+
+static const SkString edgeShader = SkString(R"(
+ uniform shader uContentTexture;
+ uniform vec2 uImgSize;
+
+ // TODO(b/214232209) oobTolerance is temporary and will be removed when the scrollbar will be
+ // hidden during the animation
+ const float oobTolerance = 15;
+ const int blurRadius = 3;
+ const float blurArea = float((2 * blurRadius + 1) * (2 * blurRadius + 1));
+
+ vec4 boxBlur(vec2 p) {
+ vec4 sumColors = vec4(0);
+
+ for (int i = -blurRadius; i <= blurRadius; i++) {
+ for (int j = -blurRadius; j <= blurRadius; j++) {
+ sumColors += uContentTexture.eval(p + vec2(i, j));
+ }
+ }
+ return sumColors / blurArea;
+ }
+
+ vec4 main(vec2 coord) {
+ vec2 nearestTexturePoint = clamp(coord, vec2(0, 0), uImgSize);
+ if (coord == nearestTexturePoint) {
+ return uContentTexture.eval(coord);
+ } else {
+ vec2 samplePoint = nearestTexturePoint + oobTolerance * normalize(
+ nearestTexturePoint - coord);
+ return boxBlur(samplePoint);
+ }
+ }
+)");
+
+sk_sp<SkShader> EdgeExtensionShaderFactory::createSkShader(const sk_sp<SkShader>& inputShader,
+ const LayerSettings& layer,
+ const SkRect& imageBounds) {
+ if (mBuilder == nullptr) {
+ const static SkRuntimeEffect::Result instance = SkRuntimeEffect::MakeForShader(edgeShader);
+ if (!instance.errorText.isEmpty()) {
+ ALOGE("EdgeExtensionShaderFactory terminated with an error: %s",
+ instance.errorText.c_str());
+ return nullptr;
+ }
+ mBuilder = std::make_unique<SkRuntimeShaderBuilder>(instance.effect);
+ }
+ mBuilder->child("uContentTexture") = inputShader;
+ if (imageBounds.isEmpty()) {
+ mBuilder->uniform("uImgSize") = SkPoint{layer.geometry.boundaries.getWidth(),
+ layer.geometry.boundaries.getHeight()};
+ } else {
+ mBuilder->uniform("uImgSize") = SkPoint{imageBounds.width(), imageBounds.height()};
+ }
+ return mBuilder->makeShader();
+}
+} // namespace android::renderengine::skia
\ No newline at end of file
diff --git a/libs/renderengine/skia/filters/EdgeExtensionShaderFactory.h b/libs/renderengine/skia/filters/EdgeExtensionShaderFactory.h
new file mode 100644
index 0000000..b0a8a93
--- /dev/null
+++ b/libs/renderengine/skia/filters/EdgeExtensionShaderFactory.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <SkImage.h>
+#include <SkRect.h>
+#include <SkRuntimeEffect.h>
+#include <SkShader.h>
+#include <renderengine/LayerSettings.h>
+#include <ui/EdgeExtensionEffect.h>
+
+namespace android::renderengine::skia {
+
+/**
+ * This shader is designed to prolong the texture of a surface whose bounds have been extended over
+ * the size of the texture. This shader is similar to the default clamp, but adds a blur effect and
+ * samples from close to the edge (compared to on the edge) to avoid weird artifacts when elements
+ * (in particular, scrollbars) touch the edge.
+ */
+class EdgeExtensionShaderFactory {
+public:
+ sk_sp<SkShader> createSkShader(const sk_sp<SkShader>& inputShader, const LayerSettings& layer,
+ const SkRect& imageBounds);
+
+private:
+ std::unique_ptr<SkRuntimeShaderBuilder> mBuilder;
+};
+} // namespace android::renderengine::skia