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

Commit 866cf65c authored by Leon Scroggins III's avatar Leon Scroggins III
Browse files

Make updateLocalMatrix replace the current Matrix.

Fixes a bug introduced in I3c3316377874e89fccc85afb864bc038b0ef3890.

CreateLocalMatrixShader combines the existing matrix with the new
matrix, which is not what we want. Keep track of the original
SkShader at all times, and always create the local matrix shader
with the original. Store the SkShader with a local matrix as
Shader.native_with_local_matrix.

Make Shader.native_instance private. Instead of allowing direct
access, add an init() method which sets it, and getNativeInstance(),
which returns either native_instance or native_with_local_matrix,
as appropriate.

Make Shader subclasses call init(), instead of setting native_instance
directly.

Pass native_with_local_matrix pointer to nativeSetLocalMatrix and
nativeDestructor, which unrefs it (if not null).

Since nativeSetLocalMatrix no longer replaces the original, do not
unref it.

Add a comment to Shader.updateLocalMatrix that it does not affect
ComposeShaders created with this Shader. (This should have been a
part of I3c3316377874e89fccc85afb864bc038b0ef3890.)

BUG:16293121
Change-Id: Ieb31c7e1fe99081f6b81493178f4a18d3c5df643
parent 2b122164
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -50,15 +50,20 @@ static jint Color_HSVToColor(JNIEnv* env, jobject, jint alpha, jfloatArray hsvAr

///////////////////////////////////////////////////////////////////////////////////////////////

static void Shader_destructor(JNIEnv* env, jobject o, jlong shaderHandle)
static void Shader_destructor(JNIEnv* env, jobject o, jlong shaderHandle, jlong shaderWithLMHandle)
{
    SkShader* shader = reinterpret_cast<SkShader*>(shaderHandle);
    SkSafeUnref(shader);
    SkShader* shaderWithLM = reinterpret_cast<SkShader*>(shaderWithLMHandle);
    SkSafeUnref(shaderWithLM);
}

static jlong Shader_setLocalMatrix(JNIEnv* env, jobject o, jlong shaderHandle,
        jlong matrixHandle)
        jlong oldLocalMatrixShaderHandle, jlong matrixHandle)
{
    // The old shader with local matrix is no longer needed, so unref it.
    SkSafeUnref(reinterpret_cast<SkShader*>(oldLocalMatrixShaderHandle));

    SkShader* shader       = reinterpret_cast<SkShader*>(shaderHandle);
    const SkMatrix* matrix = reinterpret_cast<SkMatrix*>(matrixHandle);
    if (shader) {
@@ -66,7 +71,6 @@ static jlong Shader_setLocalMatrix(JNIEnv* env, jobject o, jlong shaderHandle,
            matrix = &SkMatrix::I();
        }
        SkShader* newShader = SkShader::CreateLocalMatrixShader(shader, *matrix);
        shader->unref();
        shader = newShader;
    }
    return reinterpret_cast<jlong>(shader);
@@ -239,8 +243,8 @@ static JNINativeMethod gColorMethods[] = {
};

static JNINativeMethod gShaderMethods[] = {
    { "nativeDestructor",        "(J)V",    (void*)Shader_destructor        },
    { "nativeSetLocalMatrix",    "(JJ)J",   (void*)Shader_setLocalMatrix    }
    { "nativeDestructor",        "(JJ)V",    (void*)Shader_destructor        },
    { "nativeSetLocalMatrix",    "(JJJ)J",   (void*)Shader_setLocalMatrix    }
};

static JNINativeMethod gBitmapShaderMethods[] = {
+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ public class BitmapShader extends Shader {
        mTileX = tileX;
        mTileY = tileY;
        final long b = bitmap.ni();
        native_instance = nativeCreate(b, tileX.nativeInt, tileY.nativeInt);
        init(nativeCreate(b, tileX.nativeInt, tileY.nativeInt));
    }

    /**
+4 −4
Original line number Diff line number Diff line
@@ -53,8 +53,8 @@ public class ComposeShader extends Shader {
        mShaderA = shaderA;
        mShaderB = shaderB;
        mXferMode = mode;
        native_instance = nativeCreate1(shaderA.native_instance, shaderB.native_instance,
                (mode != null) ? mode.native_instance : 0);
        init(nativeCreate1(shaderA.getNativeInstance(), shaderB.getNativeInstance(),
                (mode != null) ? mode.native_instance : 0));
    }

    /** Create a new compose shader, given shaders A, B, and a combining PorterDuff mode.
@@ -69,8 +69,8 @@ public class ComposeShader extends Shader {
        mShaderA = shaderA;
        mShaderB = shaderB;
        mPorterDuffMode = mode;
        native_instance = nativeCreate2(shaderA.native_instance, shaderB.native_instance,
                mode.nativeInt);
        init(nativeCreate2(shaderA.getNativeInstance(), shaderB.getNativeInstance(),
                mode.nativeInt));
    }

    /**
+2 −2
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ public class LinearGradient extends Shader {
        mColors = colors;
        mPositions = positions;
        mTileMode = tile;
        native_instance = nativeCreate1(x0, y0, x1, y1, colors, positions, tile.nativeInt);
        init(nativeCreate1(x0, y0, x1, y1, colors, positions, tile.nativeInt));
    }

    /** Create a shader that draws a linear gradient along a line.
@@ -87,7 +87,7 @@ public class LinearGradient extends Shader {
        mColor0 = color0;
        mColor1 = color1;
        mTileMode = tile;
        native_instance = nativeCreate2(x0, y0, x1, y1, color0, color1, tile.nativeInt);
        init(nativeCreate2(x0, y0, x1, y1, color0, color1, tile.nativeInt));
    }

    /**
+1 −1
Original line number Diff line number Diff line
@@ -919,7 +919,7 @@ public class Paint {
    public Shader setShader(Shader shader) {
        long shaderNative = 0;
        if (shader != null)
            shaderNative = shader.native_instance;
            shaderNative = shader.getNativeInstance();
        native_setShader(mNativePaint, shaderNative);
        mShader = shader;
        return shader;
Loading