Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6c229613 authored by Nader Jawad's avatar Nader Jawad
Browse files

Added support for edge treatment parameters for BlurShader

Added configurable parameters for edge treatment for
BlurShader to determine how edge pixels are to be
computed as part of the blur kernel. This provides
the option to sample the edge pixels of the source
for larger windows as well as using transparent
(default behavior)

Fixes: 167714368
Test: Added CTS tests to verify results of edge treatment parameters
Change-Id: I3880ff4aa2e2a4eba831a0aa6d2ec77b07e84813
parent d5d39ae2
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -14292,6 +14292,7 @@ package android.graphics {
  public final class BlurShader extends android.graphics.Shader {
    ctor public BlurShader(float, float, @Nullable android.graphics.Shader);
    ctor public BlurShader(float, float, @Nullable android.graphics.Shader, @NonNull android.graphics.Shader.TileMode);
  }
  public class Camera {
@@ -15630,6 +15631,7 @@ package android.graphics {
  public enum Shader.TileMode {
    enum_constant public static final android.graphics.Shader.TileMode CLAMP;
    enum_constant public static final android.graphics.Shader.TileMode DECAL;
    enum_constant public static final android.graphics.Shader.TileMode MIRROR;
    enum_constant public static final android.graphics.Shader.TileMode REPEAT;
  }
+25 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.graphics;

import android.annotation.NonNull;
import android.annotation.Nullable;

/**
@@ -28,6 +29,7 @@ public final class BlurShader extends Shader {
    private final float mRadiusX;
    private final float mRadiusY;
    private final Shader mInputShader;
    private final TileMode mEdgeTreatment;

    private long mNativeInputShader = 0;

@@ -35,22 +37,42 @@ public final class BlurShader extends Shader {
     * Create a {@link BlurShader} that blurs the contents of the optional input shader
     * with the specified radius along the x and y axis. If no input shader is provided
     * then all drawing commands issued with a {@link android.graphics.Paint} that this
     * shader is installed in will be blurred
     * shader is installed in will be blurred.
     *
     * This uses a default {@link TileMode#DECAL} for edge treatment
     *
     * @param radiusX Radius of blur along the X axis
     * @param radiusY Radius of blur along the Y axis
     * @param inputShader Input shader that provides the content to be blurred
     */
    public BlurShader(float radiusX, float radiusY, @Nullable Shader inputShader) {
        this(radiusX, radiusY, inputShader, TileMode.DECAL);
    }

    /**
     * Create a {@link BlurShader} that blurs the contents of the optional input shader
     * with the specified radius along the x and y axis. If no input shader is provided
     * then all drawing commands issued with a {@link android.graphics.Paint} that this
     * shader is installed in will be blurred
     * @param radiusX Radius of blur along the X axis
     * @param radiusY Radius of blur along the Y axis
     * @param inputShader Input shader that provides the content to be blurred
     * @param edgeTreatment Policy for how to blur content near edges of the blur shader
     */
    public BlurShader(float radiusX, float radiusY, @Nullable Shader inputShader,
            @NonNull TileMode edgeTreatment) {
        mRadiusX = radiusX;
        mRadiusY = radiusY;
        mInputShader = inputShader;
        mEdgeTreatment = edgeTreatment;
    }

    /** @hide **/
    @Override
    protected long createNativeInstance(long nativeMatrix) {
        mNativeInputShader = mInputShader != null ? mInputShader.getNativeInstance() : 0;
        return nativeCreate(nativeMatrix, mRadiusX, mRadiusY, mNativeInputShader);
        return nativeCreate(nativeMatrix, mRadiusX, mRadiusY, mNativeInputShader,
                mEdgeTreatment.nativeInt);
    }

    /** @hide **/
@@ -61,5 +83,5 @@ public final class BlurShader extends Shader {
    }

    private static native long nativeCreate(long nativeMatrix, float radiusX, float radiusY,
            long inputShader);
            long inputShader, int edgeTreatment);
}
+5 −1
Original line number Diff line number Diff line
@@ -95,7 +95,11 @@ public class Shader {
         * repeat the shader's image horizontally and vertically, alternating
         * mirror images so that adjacent images always seam
         */
        MIRROR  (2);
        MIRROR(2),
        /**
         * Only draw within the original domain, return transparent-black everywhere else
         */
        DECAL(3);

        TileMode(int nativeInt) {
            this.nativeInt = nativeInt;
+3 −2
Original line number Diff line number Diff line
@@ -224,7 +224,7 @@ static jlong ComposeShader_create(JNIEnv* env, jobject o, jlong matrixPtr,
///////////////////////////////////////////////////////////////////////////////////////////////

static jlong BlurShader_create(JNIEnv* env , jobject o, jlong matrixPtr, jfloat sigmaX,
        jfloat sigmaY, jlong shaderHandle) {
        jfloat sigmaY, jlong shaderHandle, jint edgeTreatment) {
    auto* matrix = reinterpret_cast<const SkMatrix*>(matrixPtr);
    auto* inputShader = reinterpret_cast<Shader*>(shaderHandle);

@@ -232,6 +232,7 @@ static jlong BlurShader_create(JNIEnv* env , jobject o, jlong matrixPtr, jfloat
                sigmaX,
                sigmaY,
                inputShader,
                static_cast<SkTileMode>(edgeTreatment),
                matrix
            );
    return reinterpret_cast<jlong>(blurShader);
@@ -291,7 +292,7 @@ static const JNINativeMethod gBitmapShaderMethods[] = {
};

static const JNINativeMethod gBlurShaderMethods[] = {
    { "nativeCreate",      "(JFFJ)J", (void*)BlurShader_create }
    { "nativeCreate",      "(JFFJI)J", (void*)BlurShader_create }
};

static const JNINativeMethod gLinearGradientMethods[] = {
+3 −2
Original line number Diff line number Diff line
@@ -20,13 +20,14 @@
#include "utils/Blur.h"

namespace android::uirenderer {
BlurShader::BlurShader(float radiusX, float radiusY, Shader* inputShader, const SkMatrix* matrix)
BlurShader::BlurShader(float radiusX, float radiusY, Shader* inputShader, SkTileMode edgeTreatment,
        const SkMatrix* matrix)
    : Shader(matrix)
    , skImageFilter(
            SkImageFilters::Blur(
                    Blur::convertRadiusToSigma(radiusX),
                    Blur::convertRadiusToSigma(radiusY),
                    SkTileMode::kClamp,
                    edgeTreatment,
                    inputShader ? inputShader->asSkImageFilter() : nullptr,
                    nullptr)
            ) { }
Loading