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

Commit 06d29848 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 1bdb3be9
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
@@ -891,6 +919,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
@@ -901,8 +930,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
@@ -1075,6 +1106,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);
@@ -1096,8 +1128,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
@@ -1110,12 +1144,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
@@ -1195,6 +1232,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);
@@ -1202,8 +1240,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
@@ -1256,6 +1296,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);
@@ -1277,8 +1318,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);
        }
    }

    /**
     * @hide
@@ -1294,12 +1337,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);
        }
    }

    /**
     * @hide
@@ -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
@@ -1498,6 +1550,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
@@ -1508,8 +1561,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);
        }
    }

    /**
     * @hide
@@ -1658,6 +1713,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);
@@ -1678,8 +1734,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);
        }
    }

    /**
     * @hide
@@ -1768,6 +1826,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);
@@ -1788,8 +1847,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);
        }
    }

    /**
     * @hide
@@ -1827,6 +1888,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) {
@@ -1836,8 +1898,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);
        }
    }

    /**
@@ -1881,6 +1945,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);
@@ -1891,8 +1956,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);
        }
    }

    /**
@@ -1951,6 +2018,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();

@@ -1987,8 +2055,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);
        }
    }

    /**