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

Commit d43909c7 authored by Xavier Ducrohet's avatar Xavier Ducrohet
Browse files

LayoutLib: add support for unsupported drawing modifiers.

DrawFilter, Rasterizer, ColorFilter and MaskFilter
are not supported but we need to provide their
JNI counterparts anyway, to at least display warnings
when they are used.

Also improved the API to query Paint for Shaders
and PathEffects, and clean up some code by
moving asserts into the DelegateManager.

Change-Id: I8942514565d28576d5608c6373bda25d86d42ff2
parent 20805343
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -65,7 +65,6 @@ public class BitmapShader_Delegate extends Shader_Delegate {
    /*package*/ static int nativeCreate(int native_bitmap, int shaderTileModeX,
            int shaderTileModeY) {
        Bitmap_Delegate bitmap = Bitmap_Delegate.getDelegate(native_bitmap);
        assert bitmap != null;
        if (bitmap == null) {
            return 0;
        }
+3 −26
Original line number Diff line number Diff line
@@ -139,7 +139,6 @@ public final class Bitmap_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate delegate = sManager.getDelegate(bitmap.mNativeBitmap);
        if (delegate == null) {
            assert false;
            return null;
        }

@@ -215,7 +214,6 @@ public final class Bitmap_Delegate {
    /*package*/ static Bitmap nativeCopy(int srcBitmap, int nativeConfig, boolean isMutable) {
        Bitmap_Delegate srcBmpDelegate = sManager.getDelegate(srcBitmap);
        if (srcBmpDelegate == null) {
            assert false;
            return null;
        }

@@ -258,7 +256,6 @@ public final class Bitmap_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return;
        }

@@ -278,7 +275,6 @@ public final class Bitmap_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return 0;
        }

@@ -289,7 +285,6 @@ public final class Bitmap_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return 0;
        }

@@ -300,7 +295,6 @@ public final class Bitmap_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return 0;
        }

@@ -311,7 +305,6 @@ public final class Bitmap_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return 0;
        }

@@ -322,7 +315,6 @@ public final class Bitmap_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return true;
        }

@@ -333,7 +325,6 @@ public final class Bitmap_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return 0;
        }

@@ -344,7 +335,6 @@ public final class Bitmap_Delegate {
            int stride, int x, int y, int width, int height) {
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return;
        }

@@ -355,7 +345,6 @@ public final class Bitmap_Delegate {
    /*package*/ static void nativeSetPixel(int nativeBitmap, int x, int y, int color) {
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return;
        }

@@ -366,7 +355,6 @@ public final class Bitmap_Delegate {
            int stride, int x, int y, int width, int height) {
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return;
        }

@@ -386,7 +374,6 @@ public final class Bitmap_Delegate {
    /*package*/ static int nativeGenerationId(int nativeBitmap) {
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return 0;
        }

@@ -414,20 +401,13 @@ public final class Bitmap_Delegate {
            int[] offsetXY) {
        Bitmap_Delegate bitmap = sManager.getDelegate(nativeBitmap);
        if (bitmap == null) {
            assert false;
            return null;
        }

        Paint_Delegate paint = null;
        if (nativePaint > 0) {
            paint = Paint_Delegate.getDelegate(nativePaint);
            if (paint == null) {
                assert false;
                return null;
            }
        }
        // get the paint which can be null if nativePaint is 0.
        Paint_Delegate paint = Paint_Delegate.getDelegate(nativePaint);

        if (paint != null && paint.getMaskFilter() != 0) {
        if (paint != null && paint.getMaskFilter() != null) {
            Bridge.getLog().fidelityWarning(null,
                    "MaskFilter not supported in Bitmap.extractAlpha",
                    null);
@@ -452,7 +432,6 @@ public final class Bitmap_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate delegate = sManager.getDelegate(nativeBitmap);
        if (delegate == null) {
            assert false;
            return;
        }

@@ -462,13 +441,11 @@ public final class Bitmap_Delegate {
    /*package*/ static boolean nativeSameAs(int nb0, int nb1) {
        Bitmap_Delegate delegate1 = sManager.getDelegate(nb0);
        if (delegate1 == null) {
            assert false;
            return false;
        }

        Bitmap_Delegate delegate2 = sManager.getDelegate(nb1);
        if (delegate2 == null) {
            assert false;
            return false;
        }

+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 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.
 */

package android.graphics;

import com.android.layoutlib.bridge.impl.DelegateManager;

/**
 * Delegate implementing the native methods of android.graphics.BlurMaskFilter
 *
 * Through the layoutlib_create tool, the original native methods of BlurMaskFilter have
 * been replaced by calls to methods of the same name in this delegate class.
 *
 * This class behaves like the original native implementation, but in Java, keeping previously
 * native data into its own objects and mapping them to int that are sent back and forth between
 * it and the original BlurMaskFilter class.
 *
 * Because this extends {@link MaskFilter_Delegate}, there's no need to use a
 * {@link DelegateManager}, as all the Shader classes will be added to the manager
 * owned by {@link MaskFilter_Delegate}.
 *
 * @see MaskFilter_Delegate
 *
 */
public class BlurMaskFilter_Delegate extends MaskFilter_Delegate {

    // ---- delegate data ----

    // ---- Public Helper methods ----

    @Override
    public boolean isSupported() {
        return false;
    }

    @Override
    public String getSupportMessage() {
        return "Blur Mask Filters are not supported.";
    }

    // ---- native methods ----

    /*package*/ static int nativeConstructor(float radius, int style) {
        BlurMaskFilter_Delegate newDelegate = new BlurMaskFilter_Delegate();
        return sManager.addDelegate(newDelegate);
    }

    // ---- Private delegate/helper methods ----
}
+45 −76
Original line number Diff line number Diff line
@@ -61,6 +61,8 @@ public final class Canvas_Delegate {
    private Bitmap_Delegate mBitmap;
    private GcSnapshot mSnapshot;

    private int mDrawFilter = 0;

    // ---- Public Helper methods ----

    /**
@@ -84,6 +86,15 @@ public final class Canvas_Delegate {
        return mSnapshot;
    }

    /**
     * Returns the {@link DrawFilter} delegate or null if none have been set.
     *
     * @return the delegate or null.
     */
    public DrawFilter_Delegate getDrawFilter() {
        return DrawFilter_Delegate.getDelegate(mDrawFilter);
    }

    // ---- native methods ----

    /*package*/ static boolean isOpaque(Canvas thisCanvas) {
@@ -95,7 +106,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return 0;
        }

@@ -106,7 +116,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return 0;
        }

@@ -117,7 +126,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

@@ -128,7 +136,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

@@ -139,7 +146,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

@@ -150,7 +156,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

@@ -184,7 +189,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return false;
        }

@@ -205,7 +209,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return 0;
        }

@@ -216,7 +219,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

@@ -227,7 +229,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return 0;
        }

@@ -238,7 +239,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(thisCanvas.mNativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

@@ -296,14 +296,12 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

        // get the delegate from the native int.
        Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(bitmap);
        if (bitmapDelegate == null) {
            assert false;
            return;
        }

@@ -315,13 +313,11 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return 0;
        }

        Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(paint);
        if (paintDelegate == null) {
            assert false;
            return 0;
        }

@@ -334,13 +330,11 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return 0;
        }

        Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(paint);
        if (paintDelegate == null) {
            assert false;
            return 0;
        }

@@ -354,7 +348,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return 0;
        }

@@ -367,7 +360,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return 0;
        }

@@ -379,13 +371,11 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

        Matrix_Delegate matrixDelegate = Matrix_Delegate.getDelegate(nMatrix);
        if (matrixDelegate == null) {
            assert false;
            return;
        }

@@ -408,12 +398,12 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

        Matrix_Delegate matrixDelegate = Matrix_Delegate.getDelegate(nMatrix);
        if (matrixDelegate == null) {
            assert false;
            return;
        }

        // get the current top graphics2D object.
@@ -441,7 +431,7 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas);
        if (canvasDelegate == null) {
            assert false;
            return false;
        }

        return canvasDelegate.clipRect(left, top, right, bottom, regionOp);
@@ -463,8 +453,24 @@ public final class Canvas_Delegate {

    /*package*/ static void nativeSetDrawFilter(int nativeCanvas,
                                                   int nativeFilter) {
        // FIXME
        throw new UnsupportedOperationException();
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            return;
        }

        canvasDelegate.mDrawFilter = nativeFilter;

        // get the delegate only because we don't support them at all for the moment, so
        // we can display the message now.

        DrawFilter_Delegate filterDelegate = DrawFilter_Delegate.getDelegate(nativeFilter);
        if (canvasDelegate == null) {
            return;
        }

        if (filterDelegate.isSupported() == false) {
            Bridge.getLog().fidelityWarning(null, filterDelegate.getSupportMessage(), null);
        }
    }

    /*package*/ static boolean native_getClipBounds(int nativeCanvas,
@@ -472,7 +478,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return false;
        }

@@ -533,7 +538,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

@@ -676,7 +680,6 @@ public final class Canvas_Delegate {
                                               int paint) {
        final Path_Delegate pathDelegate = Path_Delegate.getDelegate(path);
        if (pathDelegate == null) {
            assert false;
            return;
        }

@@ -708,7 +711,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(bitmap);
        if (bitmapDelegate == null) {
            assert false;
            return;
        }

@@ -729,7 +731,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(bitmap);
        if (bitmapDelegate == null) {
            assert false;
            return;
        }

@@ -754,7 +755,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(bitmap);
        if (bitmapDelegate == null) {
            assert false;
            return;
        }

@@ -800,24 +800,15 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

        // get the delegate from the native int.
        Paint_Delegate paintDelegate = null;
        if (nPaint > 0) {
            paintDelegate = Paint_Delegate.getDelegate(nPaint);
            if (paintDelegate == null) {
                assert false;
                return;
            }
        }
        // get the delegate from the native int, which can be null
        Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(nPaint);

        // get the delegate from the native int.
        Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(nBitmap);
        if (bitmapDelegate == null) {
            assert false;
            return;
        }

@@ -825,7 +816,6 @@ public final class Canvas_Delegate {

        Matrix_Delegate matrixDelegate = Matrix_Delegate.getDelegate(nMatrix);
        if (matrixDelegate == null) {
            assert false;
            return;
        }

@@ -1029,7 +1019,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int so that it can be disposed.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

@@ -1051,19 +1040,11 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

        // paint could be 0 meaning no paint
        Paint_Delegate paintDelegate = null;
        if (nPaint > 0) {
            paintDelegate = Paint_Delegate.getDelegate(nPaint);
            if (paintDelegate == null) {
                assert false;
                return;
            }
        }
        // get the paint which can be null if nPaint is 0;
        Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(nPaint);

        canvasDelegate.getSnapshot().draw(drawable, paintDelegate, compositeOnly, forceSrcMode);
    }
@@ -1078,7 +1059,6 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

@@ -1161,19 +1141,11 @@ public final class Canvas_Delegate {
        // get the delegate from the native int.
        Canvas_Delegate canvasDelegate = sManager.getDelegate(nativeCanvas);
        if (canvasDelegate == null) {
            assert false;
            return;
        }

        // get the delegate from the native int.
        Paint_Delegate paintDelegate = null;
        if (nativePaintOrZero > 0) {
            paintDelegate = Paint_Delegate.getDelegate(nativePaintOrZero);
            if (paintDelegate == null) {
                assert false;
                return;
            }
        }
        // get the paint, which could be null if the int is 0
        Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(nativePaintOrZero);

        final BufferedImage image = getImageToDraw(bitmap, paintDelegate, sBoolOut);

@@ -1221,9 +1193,7 @@ public final class Canvas_Delegate {
            // - Create a different bitmap to draw in which all the alpha channel values is set
            //   to 0xFF.
            if (paint != null) {
                int xfermode = paint.getXfermode();
                if (xfermode > 0) {
                    Xfermode_Delegate xfermodeDelegate = Xfermode_Delegate.getDelegate(xfermode);
                Xfermode_Delegate xfermodeDelegate = paint.getXfermode();
                if (xfermodeDelegate instanceof PorterDuffXfermode_Delegate) {
                    PorterDuff.Mode mode =
                        ((PorterDuffXfermode_Delegate)xfermodeDelegate).getMode();
@@ -1232,7 +1202,6 @@ public final class Canvas_Delegate {
                            mode == PorterDuff.Mode.SRC;
                }
            }
            }

            // if we can't force SRC mode, then create a temp bitmap of TYPE_RGB
            if (forceSrcMode[0] == false) {
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 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.
 */

package android.graphics;

import com.android.layoutlib.bridge.impl.DelegateManager;

/**
 * Delegate implementing the native methods of android.graphics.ColorFilter
 *
 * Through the layoutlib_create tool, the original native methods of ColorFilter have been replaced
 * by calls to methods of the same name in this delegate class.
 *
 * This class behaves like the original native implementation, but in Java, keeping previously
 * native data into its own objects and mapping them to int that are sent back and forth between
 * it and the original ColorFilter class.
 *
 * This also serve as a base class for all ColorFilter delegate classes.
 *
 * @see DelegateManager
 *
 */
public abstract class ColorFilter_Delegate {

    // ---- delegate manager ----
    protected static final DelegateManager<ColorFilter_Delegate> sManager =
            new DelegateManager<ColorFilter_Delegate>();

    // ---- delegate helper data ----

    // ---- delegate data ----

    // ---- Public Helper methods ----

    public static ColorFilter_Delegate getDelegate(int nativeShader) {
        return sManager.getDelegate(nativeShader);
    }

    public abstract boolean isSupported();
    public abstract String getSupportMessage();

    // ---- native methods ----

    /*package*/ static void finalizer(int native_instance, int nativeColorFilter) {
        sManager.removeDelegate(native_instance);
    }

    // ---- Private delegate/helper methods ----
}
Loading