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

Commit 397ef9a4 authored by Chris Craik's avatar Chris Craik
Browse files

Ensure RenderScript tracing is always balanced via try/finally

bug:21560057

Change-Id: I6709b81636822135848c10adca8ba425f3c2212e
parent 60582519
Loading
Loading
Loading
Loading
+348 −278
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import android.os.Trace;
 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
 * </div>
 **/

public class Allocation extends BaseObj {
    Type mType;
    Bitmap mBitmap;
@@ -455,6 +456,7 @@ public class Allocation extends BaseObj {
     *
     */
    public void syncAll(int srcLocation) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
            switch (srcLocation) {
                case USAGE_GRAPHICS_TEXTURE:
@@ -476,8 +478,10 @@ public class Allocation extends BaseObj {
            }
            mRS.validate();
            mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Send a buffer to the output stream.  The contents of the Allocation will
@@ -487,6 +491,7 @@ public class Allocation extends BaseObj {
     *
     */
    public void ioSend() {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
            if ((mUsage & USAGE_IO_OUTPUT) == 0) {
                throw new RSIllegalArgumentException(
@@ -494,8 +499,10 @@ public class Allocation extends BaseObj {
            }
            mRS.validate();
            mRS.nAllocationIoSend(getID(mRS));
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Receive the latest input into the Allocation. This operation
@@ -503,6 +510,7 @@ public class Allocation extends BaseObj {
     *
     */
    public void ioReceive() {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
            if ((mUsage & USAGE_IO_INPUT) == 0) {
                throw new RSIllegalArgumentException(
@@ -510,8 +518,10 @@ public class Allocation extends BaseObj {
            }
            mRS.validate();
            mRS.nAllocationIoReceive(getID(mRS));
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy an array of RS objects to the Allocation.
@@ -519,6 +529,7 @@ public class Allocation extends BaseObj {
     * @param d Source array.
     */
    public void copyFrom(BaseObj[] d) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
            mRS.validate();
            validateIsObject();
@@ -540,8 +551,10 @@ public class Allocation extends BaseObj {
                }
                copy1DRangeFromUnchecked(0, mCurrentCount, i);
            }
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    private void validateBitmapFormat(Bitmap b) {
        Bitmap.Config bc = b.getConfig();
@@ -599,6 +612,7 @@ public class Allocation extends BaseObj {
    }

    private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
            mRS.validate();
            if (mCurrentDimZ > 0) {
@@ -608,8 +622,10 @@ public class Allocation extends BaseObj {
            } else {
                copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
            }
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy into this Allocation from an array. This method does not guarantee
@@ -619,11 +635,14 @@ public class Allocation extends BaseObj {
     * @param array The source data array
     */
    public void copyFromUnchecked(Object array) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
            copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
                              java.lang.reflect.Array.getLength(array));
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy into this Allocation from an array. This method does not guarantee
@@ -679,11 +698,14 @@ public class Allocation extends BaseObj {
     * @param array The source data array
     */
    public void copyFrom(Object array) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
            copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
                              java.lang.reflect.Array.getLength(array));
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy into this Allocation from an array.  This variant is type checked
@@ -747,6 +769,7 @@ public class Allocation extends BaseObj {
     * @param b the source bitmap
     */
    public void copyFrom(Bitmap b) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
            mRS.validate();
            if (b.getConfig() == null) {
@@ -759,8 +782,10 @@ public class Allocation extends BaseObj {
            validateBitmapSize(b);
            validateBitmapFormat(b);
            mRS.nAllocationCopyFromBitmap(getID(mRS), b);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy an Allocation from an Allocation.  The types of both allocations
@@ -769,14 +794,17 @@ public class Allocation extends BaseObj {
     * @param a the source allocation
     */
    public void copyFrom(Allocation a) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
            mRS.validate();
            if (!mType.equals(a.getType())) {
                throw new RSIllegalArgumentException("Types of allocations must match.");
            }
            copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * This is only intended to be used by auto-generated code reflected from
@@ -890,6 +918,7 @@ public class Allocation extends BaseObj {

    private void copy1DRangeFromUnchecked(int off, int count, Object array,
                                          Element.DataType dt, int arrayLen) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
            final int dataSize = mType.mElement.getBytesSize() * count;
            // AutoPadding for Vec3 Element
@@ -900,8 +929,10 @@ public class Allocation extends BaseObj {
            data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
            mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
                                  mType.mElement.mType.mSize, usePadding);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy an array into part of this Allocation.  This method does not
@@ -1074,6 +1105,7 @@ public class Allocation extends BaseObj {

    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
                                  Element.DataType dt, int arrayLen) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
            mRS.validate();
            validate2DRange(xoff, yoff, w, h);
@@ -1095,8 +1127,10 @@ public class Allocation extends BaseObj {
            mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
                                  array, sizeBytes, dt,
                                  mType.mElement.mType.mSize, usePadding);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy from an array into a rectangular region in this Allocation.  The
@@ -1109,12 +1143,15 @@ public class Allocation extends BaseObj {
     * @param array Data to be placed into the Allocation
     */
    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
            copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
                                     validateObjectIsPrimitiveArray(array, true),
                                     java.lang.reflect.Array.getLength(array));
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy from an array into a rectangular region in this Allocation.  The
@@ -1194,6 +1231,7 @@ public class Allocation extends BaseObj {
     */
    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
                                Allocation data, int dataXoff, int dataYoff) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
            mRS.validate();
            validate2DRange(xoff, yoff, w, h);
@@ -1201,8 +1239,10 @@ public class Allocation extends BaseObj {
                                  mSelectedLOD, mSelectedFace.mID,
                                  w, h, data.getID(mRS), dataXoff, dataYoff,
                                  data.mSelectedLOD, data.mSelectedFace.mID);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy a {@link android.graphics.Bitmap} into an Allocation.  The height
@@ -1258,6 +1298,7 @@ public class Allocation extends BaseObj {
     */
    private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
                                          Object array, Element.DataType dt, int arrayLen) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
            mRS.validate();
            validate3DRange(xoff, yoff, zoff, w, h, d);
@@ -1279,8 +1320,10 @@ public class Allocation extends BaseObj {
            mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
                                  array, sizeBytes, dt,
                                  mType.mElement.mType.mSize, usePadding);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy a rectangular region from the array into the allocation.
@@ -1295,12 +1338,15 @@ public class Allocation extends BaseObj {
     * @param array to be placed into the allocation
     */
    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
            copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
                                     validateObjectIsPrimitiveArray(array, true),
                                     java.lang.reflect.Array.getLength(array));
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy a rectangular region into the allocation from another
@@ -1334,15 +1380,19 @@ public class Allocation extends BaseObj {
     * @param b The bitmap to be set from the Allocation.
     */
    public void copyTo(Bitmap b) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
            mRS.validate();
            validateBitmapFormat(b);
            validateBitmapSize(b);
            mRS.nAllocationCopyToBitmap(getID(mRS), b);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    private void copyTo(Object array, Element.DataType dt, int arrayLen) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
            mRS.validate();
            boolean usePadding = false;
@@ -1361,8 +1411,10 @@ public class Allocation extends BaseObj {
                }
            }
            mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy from the Allocation into an array.  The array must be at
@@ -1499,6 +1551,7 @@ public class Allocation extends BaseObj {

    private void copy1DRangeToUnchecked(int off, int count, Object array,
                                        Element.DataType dt, int arrayLen) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeToUnchecked");
            final int dataSize = mType.mElement.getBytesSize() * count;
            // AutoPadding for Vec3 Element
@@ -1509,8 +1562,10 @@ public class Allocation extends BaseObj {
            data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
            mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
                                  mType.mElement.mType.mSize, usePadding);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy part of this Allocation into an array.  This method does not
@@ -1649,6 +1704,7 @@ public class Allocation extends BaseObj {

    void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
                                Element.DataType dt, int arrayLen) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeToUnchecked");
            mRS.validate();
            validate2DRange(xoff, yoff, w, h);
@@ -1669,8 +1725,10 @@ public class Allocation extends BaseObj {
            }
            mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
                                  array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
     * Copy from a rectangular region in this Allocation into an array.
@@ -1757,6 +1815,7 @@ public class Allocation extends BaseObj {
     */
    private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
                                        Object array, Element.DataType dt, int arrayLen) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeToUnchecked");
            mRS.validate();
            validate3DRange(xoff, yoff, zoff, w, h, d);
@@ -1777,8 +1836,10 @@ public class Allocation extends BaseObj {
            }
            mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
                                  array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /*
     * Copy from a rectangular region in this Allocation into an array.
@@ -1815,6 +1876,7 @@ public class Allocation extends BaseObj {
     *              utilized
     */
    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
            rs.validate();
            if (type.getID(rs) == 0) {
@@ -1824,8 +1886,10 @@ public class Allocation extends BaseObj {
            if (id == 0) {
                throw new RSRuntimeException("Allocation creation failed.");
            }
        Trace.traceEnd(RenderScript.TRACE_TAG);
            return new Allocation(id, rs, type, usage);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
@@ -1869,6 +1933,7 @@ public class Allocation extends BaseObj {
     */
    static public Allocation createSized(RenderScript rs, Element e,
                                         int count, int usage) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
            rs.validate();
            Type.Builder b = new Type.Builder(rs, e);
@@ -1879,8 +1944,10 @@ public class Allocation extends BaseObj {
            if (id == 0) {
                throw new RSRuntimeException("Allocation creation failed.");
            }
        Trace.traceEnd(RenderScript.TRACE_TAG);
            return new Allocation(id, rs, t, usage);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**
@@ -1939,6 +2006,7 @@ public class Allocation extends BaseObj {
    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
                                              MipmapControl mips,
                                              int usage) {
        try {
            Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
            rs.validate();

@@ -1975,8 +2043,10 @@ public class Allocation extends BaseObj {
            if (id == 0) {
                throw new RSRuntimeException("Load failed.");
            }
        Trace.traceEnd(RenderScript.TRACE_TAG);
            return new Allocation(id, rs, t, usage);
        } finally {
            Trace.traceEnd(RenderScript.TRACE_TAG);
        }
    }

    /**