Add a GaussianBlurFilter to SkiaRE using Skia blur
This is using Skia's native blur algorithm which is a fully-correct blur
using separate X/Y convolution passes in a pre-baked shader.
The performance is significantly worse than Kawase or dual-filtered
Kawase but it sets the high bar for accuracy and the low bar for
performance.
This can be switched with KawaseBlurFilter by updating the source in
SkiaGLRenderEngine to instantiate GaussianBlurFilter instead.
Test: atest BlurTests
Bug: 185365391
Change-Id: I29fba84e408c75bc8ef7011b6e40f45cb78eabab
diff --git a/libs/renderengine/Android.bp b/libs/renderengine/Android.bp
index 336ff68..a62d2b9 100644
--- a/libs/renderengine/Android.bp
+++ b/libs/renderengine/Android.bp
@@ -93,6 +93,8 @@
"skia/debug/CommonPool.cpp",
"skia/debug/SkiaCapture.cpp",
"skia/debug/SkiaMemoryReporter.cpp",
+ "skia/filters/BlurFilter.cpp",
+ "skia/filters/GaussianBlurFilter.cpp",
"skia/filters/KawaseBlurFilter.cpp",
"skia/filters/LinearEffect.cpp",
"skia/filters/StretchShaderFactory.cpp"
diff --git a/libs/renderengine/skia/SkiaGLRenderEngine.cpp b/libs/renderengine/skia/SkiaGLRenderEngine.cpp
index e6fb94d..21d5603 100644
--- a/libs/renderengine/skia/SkiaGLRenderEngine.cpp
+++ b/libs/renderengine/skia/SkiaGLRenderEngine.cpp
@@ -53,6 +53,7 @@
#include "SkBlendMode.h"
#include "SkImageInfo.h"
#include "filters/BlurFilter.h"
+#include "filters/GaussianBlurFilter.h"
#include "filters/KawaseBlurFilter.h"
#include "filters/LinearEffect.h"
#include "log/log_main.h"
@@ -804,11 +805,11 @@
continue;
}
if (layer.backgroundBlurRadius > 0 &&
- layer.backgroundBlurRadius < BlurFilter::kMaxCrossFadeRadius) {
+ layer.backgroundBlurRadius < mBlurFilter->getMaxCrossFadeRadius()) {
requiresCompositionLayer = true;
}
for (auto region : layer.blurRegions) {
- if (region.blurRadius < BlurFilter::kMaxCrossFadeRadius) {
+ if (region.blurRadius < mBlurFilter->getMaxCrossFadeRadius()) {
requiresCompositionLayer = true;
}
}
diff --git a/libs/renderengine/skia/filters/BlurFilter.cpp b/libs/renderengine/skia/filters/BlurFilter.cpp
new file mode 100644
index 0000000..6746e47
--- /dev/null
+++ b/libs/renderengine/skia/filters/BlurFilter.cpp
@@ -0,0 +1,124 @@
+/*
+ * Copyright 2021 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.
+ */
+
+#define ATRACE_TAG ATRACE_TAG_GRAPHICS
+#include "BlurFilter.h"
+#include <SkCanvas.h>
+#include <SkData.h>
+#include <SkPaint.h>
+#include <SkRRect.h>
+#include <SkRuntimeEffect.h>
+#include <SkSize.h>
+#include <SkString.h>
+#include <SkSurface.h>
+#include <log/log.h>
+#include <utils/Trace.h>
+
+namespace android {
+namespace renderengine {
+namespace skia {
+
+static sk_sp<SkRuntimeEffect> createMixEffect() {
+ SkString mixString(R"(
+ uniform shader blurredInput;
+ uniform shader originalInput;
+ uniform float mixFactor;
+
+ half4 main(float2 xy) {
+ return half4(mix(originalInput.eval(xy), blurredInput.eval(xy), mixFactor));
+ }
+ )");
+
+ auto [mixEffect, mixError] = SkRuntimeEffect::MakeForShader(mixString);
+ if (!mixEffect) {
+ LOG_ALWAYS_FATAL("RuntimeShader error: %s", mixError.c_str());
+ }
+ return mixEffect;
+}
+
+static SkMatrix getShaderTransform(const SkCanvas* canvas, const SkRect& blurRect,
+ const float scale) {
+ // 1. Apply the blur shader matrix, which scales up the blurred surface to its real size
+ auto matrix = SkMatrix::Scale(scale, scale);
+ // 2. Since the blurred surface has the size of the layer, we align it with the
+ // top left corner of the layer position.
+ matrix.postConcat(SkMatrix::Translate(blurRect.fLeft, blurRect.fTop));
+ // 3. Finally, apply the inverse canvas matrix. The snapshot made in the BlurFilter is in the
+ // original surface orientation. The inverse matrix has to be applied to align the blur
+ // surface with the current orientation/position of the canvas.
+ SkMatrix drawInverse;
+ if (canvas != nullptr && canvas->getTotalMatrix().invert(&drawInverse)) {
+ matrix.postConcat(drawInverse);
+ }
+ return matrix;
+}
+
+BlurFilter::BlurFilter(const float maxCrossFadeRadius)
+ : mMaxCrossFadeRadius(maxCrossFadeRadius),
+ mMixEffect(maxCrossFadeRadius > 0 ? createMixEffect() : nullptr) {}
+
+float BlurFilter::getMaxCrossFadeRadius() const {
+ return mMaxCrossFadeRadius;
+}
+
+void BlurFilter::drawBlurRegion(SkCanvas* canvas, const SkRRect& effectRegion,
+ const uint32_t blurRadius, const float blurAlpha,
+ const SkRect& blurRect, sk_sp<SkImage> blurredImage,
+ sk_sp<SkImage> input) {
+ ATRACE_CALL();
+
+ SkPaint paint;
+ paint.setAlphaf(blurAlpha);
+
+ const auto blurMatrix = getShaderTransform(canvas, blurRect, kInverseInputScale);
+ SkSamplingOptions linearSampling(SkFilterMode::kLinear, SkMipmapMode::kNone);
+ const auto blurShader = blurredImage->makeShader(SkTileMode::kClamp, SkTileMode::kClamp,
+ linearSampling, &blurMatrix);
+
+ if (blurRadius < mMaxCrossFadeRadius) {
+ // For sampling Skia's API expects the inverse of what logically seems appropriate. In this
+ // case you might expect the matrix to simply be the canvas matrix.
+ SkMatrix inputMatrix;
+ if (!canvas->getTotalMatrix().invert(&inputMatrix)) {
+ ALOGE("matrix was unable to be inverted");
+ }
+
+ SkRuntimeShaderBuilder blurBuilder(mMixEffect);
+ blurBuilder.child("blurredInput") = blurShader;
+ blurBuilder.child("originalInput") =
+ input->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linearSampling,
+ inputMatrix);
+ blurBuilder.uniform("mixFactor") = blurRadius / mMaxCrossFadeRadius;
+
+ paint.setShader(blurBuilder.makeShader(nullptr, true));
+ } else {
+ paint.setShader(blurShader);
+ }
+
+ if (effectRegion.isRect()) {
+ if (blurAlpha == 1.0f) {
+ paint.setBlendMode(SkBlendMode::kSrc);
+ }
+ canvas->drawRect(effectRegion.rect(), paint);
+ } else {
+ paint.setAntiAlias(true);
+ canvas->drawRRect(effectRegion, paint);
+ }
+}
+
+} // namespace skia
+} // namespace renderengine
+} // namespace android
diff --git a/libs/renderengine/skia/filters/BlurFilter.h b/libs/renderengine/skia/filters/BlurFilter.h
index 1beadee..9cddc75 100644
--- a/libs/renderengine/skia/filters/BlurFilter.h
+++ b/libs/renderengine/skia/filters/BlurFilter.h
@@ -33,11 +33,8 @@
static constexpr float kInputScale = 0.25f;
// Downsample scale factor used to improve performance
static constexpr float kInverseInputScale = 1.0f / kInputScale;
- // To avoid downscaling artifacts, we interpolate the blurred fbo with the full composited
- // image, up to this radius.
- static constexpr float kMaxCrossFadeRadius = 10.0f;
- explicit BlurFilter(){}
+ explicit BlurFilter(float maxCrossFadeRadius = 10.0f);
virtual ~BlurFilter(){}
// Execute blur, saving it to a texture
@@ -54,10 +51,20 @@
* @param blurredImage down-sampled blurred content that was produced by the generate() method
* @param input original unblurred input that is used to crossfade with the blurredImage
*/
- virtual void drawBlurRegion(SkCanvas* canvas, const SkRRect& effectRegion,
+ void drawBlurRegion(SkCanvas* canvas, const SkRRect& effectRegion,
const uint32_t blurRadius, const float blurAlpha,
const SkRect& blurRect, sk_sp<SkImage> blurredImage,
- sk_sp<SkImage> input) = 0;
+ sk_sp<SkImage> input);
+
+ float getMaxCrossFadeRadius() const;
+
+private:
+ // To avoid downscaling artifacts, we interpolate the blurred fbo with the full composited
+ // image, up to this radius.
+ const float mMaxCrossFadeRadius;
+
+ // Optional blend used for crossfade only if mMaxCrossFadeRadius > 0
+ const sk_sp<SkRuntimeEffect> mMixEffect;
};
} // namespace skia
diff --git a/libs/renderengine/skia/filters/GaussianBlurFilter.cpp b/libs/renderengine/skia/filters/GaussianBlurFilter.cpp
new file mode 100644
index 0000000..55867a9
--- /dev/null
+++ b/libs/renderengine/skia/filters/GaussianBlurFilter.cpp
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2021 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.
+ */
+
+#define ATRACE_TAG ATRACE_TAG_GRAPHICS
+
+#include "GaussianBlurFilter.h"
+#include <SkCanvas.h>
+#include <SkData.h>
+#include <SkPaint.h>
+#include <SkRRect.h>
+#include <SkRuntimeEffect.h>
+#include <SkImageFilters.h>
+#include <SkSize.h>
+#include <SkString.h>
+#include <SkSurface.h>
+#include <log/log.h>
+#include <utils/Trace.h>
+
+namespace android {
+namespace renderengine {
+namespace skia {
+
+// This constant approximates the scaling done in the software path's
+// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
+static const float BLUR_SIGMA_SCALE = 0.57735f;
+
+GaussianBlurFilter::GaussianBlurFilter(): BlurFilter(/* maxCrossFadeRadius= */ 0.0f) {}
+
+sk_sp<SkImage> GaussianBlurFilter::generate(GrRecordingContext* context, const uint32_t blurRadius,
+ const sk_sp<SkImage> input, const SkRect& blurRect)
+ const {
+ // Create blur surface with the bit depth and colorspace of the original surface
+ SkImageInfo scaledInfo = input->imageInfo().makeWH(std::ceil(blurRect.width() * kInputScale),
+ std::ceil(blurRect.height() * kInputScale));
+ sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, scaledInfo);
+
+ SkPaint paint;
+ paint.setBlendMode(SkBlendMode::kSrc);
+ paint.setImageFilter(SkImageFilters::Blur(
+ blurRadius * kInputScale * BLUR_SIGMA_SCALE,
+ blurRadius * kInputScale * BLUR_SIGMA_SCALE,
+ SkTileMode::kClamp, nullptr));
+
+ surface->getCanvas()->drawImageRect(
+ input,
+ blurRect,
+ SkRect::MakeWH(scaledInfo.width(), scaledInfo.height()),
+ SkSamplingOptions{SkFilterMode::kLinear, SkMipmapMode::kNone},
+ &paint,
+ SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint);
+ return surface->makeImageSnapshot();
+}
+
+} // namespace skia
+} // namespace renderengine
+} // namespace android
diff --git a/libs/renderengine/skia/filters/GaussianBlurFilter.h b/libs/renderengine/skia/filters/GaussianBlurFilter.h
new file mode 100644
index 0000000..a4febd2
--- /dev/null
+++ b/libs/renderengine/skia/filters/GaussianBlurFilter.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2021 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 "BlurFilter.h"
+#include <SkCanvas.h>
+#include <SkImage.h>
+#include <SkRuntimeEffect.h>
+#include <SkSurface.h>
+
+using namespace std;
+
+namespace android {
+namespace renderengine {
+namespace skia {
+
+/**
+ * This is an implementation of a Gaussian blur using Skia's built-in GaussianBlur filter.
+ */
+class GaussianBlurFilter: public BlurFilter {
+public:
+ explicit GaussianBlurFilter();
+ virtual ~GaussianBlurFilter(){}
+
+ // Execute blur, saving it to a texture
+ sk_sp<SkImage> generate(GrRecordingContext* context, const uint32_t radius,
+ const sk_sp<SkImage> blurInput, const SkRect& blurRect) const override;
+
+};
+
+} // namespace skia
+} // namespace renderengine
+} // namespace android
diff --git a/libs/renderengine/skia/filters/KawaseBlurFilter.cpp b/libs/renderengine/skia/filters/KawaseBlurFilter.cpp
index e3a0e58..bfde06f 100644
--- a/libs/renderengine/skia/filters/KawaseBlurFilter.cpp
+++ b/libs/renderengine/skia/filters/KawaseBlurFilter.cpp
@@ -52,22 +52,6 @@
LOG_ALWAYS_FATAL("RuntimeShader error: %s", error.c_str());
}
mBlurEffect = std::move(blurEffect);
-
- SkString mixString(R"(
- uniform shader blurredInput;
- uniform shader originalInput;
- uniform float mixFactor;
-
- half4 main(float2 xy) {
- return half4(mix(originalInput.eval(xy), blurredInput.eval(xy), mixFactor));
- }
- )");
-
- auto [mixEffect, mixError] = SkRuntimeEffect::MakeForShader(mixString);
- if (!mixEffect) {
- LOG_ALWAYS_FATAL("RuntimeShader error: %s", mixError.c_str());
- }
- mMixEffect = std::move(mixEffect);
}
sk_sp<SkImage> KawaseBlurFilter::generate(GrRecordingContext* context, const uint32_t blurRadius,
@@ -110,67 +94,6 @@
return tmpBlur;
}
-static SkMatrix getShaderTransform(const SkCanvas* canvas, const SkRect& blurRect, float scale) {
- // 1. Apply the blur shader matrix, which scales up the blured surface to its real size
- auto matrix = SkMatrix::Scale(scale, scale);
- // 2. Since the blurred surface has the size of the layer, we align it with the
- // top left corner of the layer position.
- matrix.postConcat(SkMatrix::Translate(blurRect.fLeft, blurRect.fTop));
- // 3. Finally, apply the inverse canvas matrix. The snapshot made in the BlurFilter is in the
- // original surface orientation. The inverse matrix has to be applied to align the blur
- // surface with the current orientation/position of the canvas.
- SkMatrix drawInverse;
- if (canvas != nullptr && canvas->getTotalMatrix().invert(&drawInverse)) {
- matrix.postConcat(drawInverse);
- }
- return matrix;
-}
-
-void KawaseBlurFilter::drawBlurRegion(SkCanvas* canvas, const SkRRect& effectRegion,
- const uint32_t blurRadius, const float blurAlpha,
- const SkRect& blurRect, sk_sp<SkImage> blurredImage,
- sk_sp<SkImage> input) {
- ATRACE_CALL();
-
- SkPaint paint;
- paint.setAlphaf(blurAlpha);
-
- const auto blurMatrix = getShaderTransform(canvas, blurRect, kInverseInputScale);
- SkSamplingOptions linearSampling(SkFilterMode::kLinear, SkMipmapMode::kNone);
- const auto blurShader = blurredImage->makeShader(SkTileMode::kClamp, SkTileMode::kClamp,
- linearSampling, &blurMatrix);
-
- if (blurRadius < kMaxCrossFadeRadius) {
- // For sampling Skia's API expects the inverse of what logically seems appropriate. In this
- // case you might expect the matrix to simply be the canvas matrix.
- SkMatrix inputMatrix;
- if (!canvas->getTotalMatrix().invert(&inputMatrix)) {
- ALOGE("matrix was unable to be inverted");
- }
-
- SkRuntimeShaderBuilder blurBuilder(mMixEffect);
- blurBuilder.child("blurredInput") = blurShader;
- blurBuilder.child("originalInput") =
- input->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, linearSampling,
- inputMatrix);
- blurBuilder.uniform("mixFactor") = blurRadius / kMaxCrossFadeRadius;
-
- paint.setShader(blurBuilder.makeShader(nullptr, true));
- } else {
- paint.setShader(blurShader);
- }
-
- if (effectRegion.isRect()) {
- if (blurAlpha == 1.0f) {
- paint.setBlendMode(SkBlendMode::kSrc);
- }
- canvas->drawRect(effectRegion.rect(), paint);
- } else {
- paint.setAntiAlias(true);
- canvas->drawRRect(effectRegion, paint);
- }
-}
-
} // namespace skia
} // namespace renderengine
} // namespace android
diff --git a/libs/renderengine/skia/filters/KawaseBlurFilter.h b/libs/renderengine/skia/filters/KawaseBlurFilter.h
index d2731e3..0ac5ac8 100644
--- a/libs/renderengine/skia/filters/KawaseBlurFilter.h
+++ b/libs/renderengine/skia/filters/KawaseBlurFilter.h
@@ -45,23 +45,8 @@
sk_sp<SkImage> generate(GrRecordingContext* context, const uint32_t radius,
const sk_sp<SkImage> blurInput, const SkRect& blurRect) const override;
- /**
- * Draw the blurred content (from the generate method) into the canvas.
- * @param canvas is the destination/output for the blur
- * @param effectRegion the RoundRect in canvas coordinates that determines the blur coverage
- * @param blurRadius radius of the blur used to determine the intensity of the crossfade effect
- * @param blurAlpha alpha value applied to the effectRegion when the blur is drawn
- * @param blurRect bounds of the blurredImage translated into canvas coordinates
- * @param blurredImage down-sampled blurred content that was produced by the generate() method
- * @param input original unblurred input that is used to crossfade with the blurredImage
- */
- void drawBlurRegion(SkCanvas* canvas, const SkRRect& effectRegion, const uint32_t blurRadius,
- const float blurAlpha, const SkRect& blurRect, sk_sp<SkImage> blurredImage,
- sk_sp<SkImage> input) override;
-
private:
sk_sp<SkRuntimeEffect> mBlurEffect;
- sk_sp<SkRuntimeEffect> mMixEffect;
};
} // namespace skia