198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/*
21b370e358d16cc3b50b169511d6b387db09f972dJason Sams * Copyright (C) 2008-2012 The Android Open Source Project
398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * you may not use this file except in compliance with the License.
698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * You may obtain a copy of the License at
798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
1098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * Unless required by applicable law or agreed to in writing, software
1198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
1298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * See the License for the specific language governing permissions and
1498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * limitations under the License.
1598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams */
1698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
1798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspackage android.support.v8.renderscript;
1898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
19654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wangimport java.nio.ByteBuffer;
20b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wangimport java.util.concurrent.locks.ReentrantReadWriteLock;
21654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang
2298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.content.res.Resources;
2398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.graphics.Bitmap;
2498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.graphics.BitmapFactory;
25c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murrayimport android.graphics.Canvas;
2698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samsimport android.util.Log;
27b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wangimport android.view.Surface;
2898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams/**
307d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * <p> This class provides the primary method through which data is passed to
317d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * and from RenderScript kernels.  An Allocation provides the backing store for
3260c5b31f4448410221de043873b94797732afa66Stephen Hines * a given {@link android.support.v8.renderscript.Type}.  </p>
3398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
347d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * <p>An Allocation also contains a set of usage flags that denote how the
357d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * Allocation could be used. For example, an Allocation may have usage flags
367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * specifying that it can be used from a script as well as input to a {@link
3760c5b31f4448410221de043873b94797732afa66Stephen Hines * android.support.v8.renderscript.Sampler}. A developer must synchronize
3860c5b31f4448410221de043873b94797732afa66Stephen Hines * across these different usages using
3960c5b31f4448410221de043873b94797732afa66Stephen Hines * {@link android.support.v8.renderscript.Allocation#syncAll} in
407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * order to ensure that different users of the Allocation have a consistent view
417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * of memory. For example, in the case where an Allocation is used as the output
427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * of one kernel and as Sampler input in a later kernel, a developer must call
437d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
447d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * second kernel to ensure correctness.
4598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
467d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
477d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * more complex Element types, the {@link #copyFromUnchecked} methods can be
487d435ae5ba100be5710b685653cc351cab159c11Stephen Hines * used to copy from byte arrays or similar constructs.</p>
4998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams *
5098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * <div class="special reference">
5198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * <h3>Developer Guides</h3>
5260c5b31f4448410221de043873b94797732afa66Stephen Hines * <p>For more information about creating an application that uses
5360c5b31f4448410221de043873b94797732afa66Stephen Hines * RenderScript, read the
5460c5b31f4448410221de043873b94797732afa66Stephen Hines * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a>
5560c5b31f4448410221de043873b94797732afa66Stephen Hines * developer guide.</p>
5698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams * </div>
5798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams **/
5898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Samspublic class Allocation extends BaseObj {
5998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Type mType;
6098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Bitmap mBitmap;
6198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mUsage;
627720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    int mSize;
63654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang    Allocation mAdaptedAllocation;
64654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang    ByteBuffer mByteBuffer = null;
65654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang    long mByteBufferStride = 0;
6698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
6798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mConstrainedLOD;
6898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mConstrainedFace;
6998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mConstrainedY;
7098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mConstrainedZ;
7198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mReadAllowed = true;
7298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    boolean mWriteAllowed = true;
739eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang    boolean mAutoPadding = false;
7498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mSelectedY;
7598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mSelectedZ;
7698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mSelectedLOD;
7798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
7898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
7998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mCurrentDimX;
8098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mCurrentDimY;
8198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mCurrentDimZ;
8298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    int mCurrentCount;
8398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
847f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
857f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        final Class c = d.getClass();
867f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (!c.isArray()) {
877f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
887f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
897f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        final Class cmp = c.getComponentType();
907f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (!cmp.isPrimitive()) {
917f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
927f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
937f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
947f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (cmp == Long.TYPE) {
957f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            if (checkType) {
967f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                validateIsInt64();
977f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                return mType.mElement.mType;
987f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            }
997f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            return Element.DataType.SIGNED_64;
1007f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
1017f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
1027f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (cmp == Integer.TYPE) {
1037f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            if (checkType) {
1047f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                validateIsInt32();
1057f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                return mType.mElement.mType;
1067f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            }
1077f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            return Element.DataType.SIGNED_32;
1087f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
1097f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
1107f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (cmp == Short.TYPE) {
1117f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            if (checkType) {
1127f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                validateIsInt16();
1137f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                return mType.mElement.mType;
1147f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            }
1157f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            return Element.DataType.SIGNED_16;
1167f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
1177f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
1187f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (cmp == Byte.TYPE) {
1197f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            if (checkType) {
1207f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                validateIsInt8();
1217f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                return mType.mElement.mType;
1227f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            }
1237f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            return Element.DataType.SIGNED_8;
1247f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
1257f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
1267f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (cmp == Float.TYPE) {
1277f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            if (checkType) {
1287f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                validateIsFloat32();
1297f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            }
1307f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            return Element.DataType.FLOAT_32;
1317f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
1327f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
1337f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (cmp == Double.TYPE) {
1347f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            if (checkType) {
1357f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                validateIsFloat64();
1367f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            }
1377f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            return Element.DataType.FLOAT_64;
1387f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
1397f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        return null;
1407f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    }
1417f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
142059fede7f200350b6131fc131f76248085485722Miao Wang    /*
143059fede7f200350b6131fc131f76248085485722Miao Wang     * Hold reference to the shared allocation in compat context
144059fede7f200350b6131fc131f76248085485722Miao Wang     * for Incremental Support Lib.
145059fede7f200350b6131fc131f76248085485722Miao Wang     */
146059fede7f200350b6131fc131f76248085485722Miao Wang    long mIncCompatAllocation;
147059fede7f200350b6131fc131f76248085485722Miao Wang    boolean mIncAllocDestroyed;
14898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1497d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The usage of the Allocation.  These signal to RenderScript where to place
1507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * the Allocation in memory.
15198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
1527d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
1537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines
1547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
1557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation will be bound to and accessed by scripts.
15698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
15798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static final int USAGE_SCRIPT = 0x0001;
15898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
15998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1607d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation will be used as a texture source by one or more graphics
1617d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * programs.
16298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
16398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
16498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
16598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
16698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1677d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
1687d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * consumer.  This usage will cause the Allocation to be created as
1697d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * read-only.
170c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
171c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
172c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public static final int USAGE_IO_INPUT = 0x0020;
173c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
174c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
1757d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
1767d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * producer.  The dimensions and format of the {@link
1777d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.SurfaceTexture} will be forced to those of the
1787d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Allocation.
179c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
180c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
181c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public static final int USAGE_IO_OUTPUT = 0x0040;
182c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
183c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
1847d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * The Allocation's backing store will be inherited from another object
1857d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * (usually a {@link android.graphics.Bitmap}); copying to or from the
1867d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * original source Bitmap will cause a synchronization rather than a full
1877d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * copy.  {@link #syncAll} may also be used to synchronize the Allocation
1887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and the source Bitmap.
189c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
1907d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This is set by default for allocations created with {@link
1917d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #createFromBitmap} in API version 18 and higher.</p>
192c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
193c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
194c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public static final int USAGE_SHARED = 0x0080;
195c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
196c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
1977d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Controls mipmap behavior when using the bitmap creation and update
1987d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * functions.
19998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
20098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public enum MipmapControl {
20198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        /**
2027d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * No mipmaps will be generated and the type generated from the incoming
2037d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * bitmap will not contain additional LODs.
20498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams         */
20598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        MIPMAP_NONE(0),
20698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
20798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        /**
2087d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * A full mipmap chain will be created in script memory.  The Type of
2097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * the Allocation will contain a full mipmap chain.  On upload, the full
2107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * chain will be transferred.
21198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams         */
21298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        MIPMAP_FULL(1),
21398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
21498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        /**
2157d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * The Type of the Allocation will be the same as MIPMAP_NONE.  It will
2167d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * not contain mipmaps.  On upload, the allocation data will contain a
2177d435ae5ba100be5710b685653cc351cab159c11Stephen Hines         * full mipmap chain generated from the top level in script memory.
21898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams         */
21998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
22098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
22198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int mID;
22298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        MipmapControl(int id) {
22398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mID = id;
22498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
22598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
22698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
227059fede7f200350b6131fc131f76248085485722Miao Wang    /**
228059fede7f200350b6131fc131f76248085485722Miao Wang     * Getter & Setter for the dummy allocation for Inc Support Lib.
229059fede7f200350b6131fc131f76248085485722Miao Wang     *
230059fede7f200350b6131fc131f76248085485722Miao Wang     */
231059fede7f200350b6131fc131f76248085485722Miao Wang    public long getIncAllocID() {
232059fede7f200350b6131fc131f76248085485722Miao Wang        return mIncCompatAllocation;
233059fede7f200350b6131fc131f76248085485722Miao Wang    }
234059fede7f200350b6131fc131f76248085485722Miao Wang    public void setIncAllocID(long id) {
235059fede7f200350b6131fc131f76248085485722Miao Wang        mIncCompatAllocation = id;
236059fede7f200350b6131fc131f76248085485722Miao Wang    }
23798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2383d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    private long getIDSafe() {
23998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mAdaptedAllocation != null) {
24098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return mAdaptedAllocation.getID(mRS);
24198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
24298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return getID(mRS);
24398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
24498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
24598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
24698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams   /**
24760c5b31f4448410221de043873b94797732afa66Stephen Hines     * Get the {@link android.support.v8.renderscript.Element} of the {@link
24860c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Type} of the Allocation.
24998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
2507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Element
25198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
25298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
25398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Element getElement() {
25498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mType.getElement();
25598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
25698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
25798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
25898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Get the usage flags of the Allocation.
25998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
2607d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return usage this Allocation's set of the USAGE_* flags OR'd together
26198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
26298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
26398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public int getUsage() {
26498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mUsage;
26598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
26698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
26798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
268b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Specifies the mapping between the Allocation's cells and an array's elements
269b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * when data is copied from the Allocation to the array, or vice-versa.
270b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
271b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Only applies to an Allocation whose Element is a vector of length 3 (such as
272b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * {@link Element#U8_3} or {@link Element#RGB_888}). Enabling this feature may make
273b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * copying data from the Allocation to an array or vice-versa less efficient.
274b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
275b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> Vec3 Element cells are stored in an Allocation as Vec4 Element cells with
276b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the same {@link android.support.v8.renderscript.Element.DataType}, with the fourth vector
277b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * component treated as padding. When this feature is enabled, only the data components,
278b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * i.e. the first 3 vector components of each cell, will be mapped between the array
279b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and the Allocation. When disabled, explicit mapping of the padding components
280b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is required, as described in the following example.
281b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
282b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> For example, when copying an integer array to an Allocation of two {@link
283b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#I32_3} cells using {@link #copyFrom(int[])}:
284b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> When disabled:
285b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     The array must have at least 8 integers, with the first 4 integers copied
286b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     to the first cell of the Allocation, and the next 4 integers copied to
287b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     the second cell. The 4th and 8th integers are mapped as the padding components.
2884dadd8068cd8a4065abe1bb7d394a0d93861af9fMiao Wang     *
289b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> When enabled:
290b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     The array just needs to have at least 6 integers, with the first 3 integers
291b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     copied to the the first cell as data components, and the next 3 copied to
292b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     the second cell. There is no mapping for the padding components.
293b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
294b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> Similarly, when copying a byte array to an Allocation of two {@link
295b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#I32_3} cells, using {@link #copyFromUnchecked(int[])}:
296b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> When disabled:
297b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     The array must have at least 32 bytes, with the first 16 bytes copied
298b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     to the first cell of the Allocation, and the next 16 bytes copied to
299b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     the second cell. The 13th-16th and 29th-32nd bytes are mapped as padding
300b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     components.
301b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
302b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> When enabled:
303b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     The array just needs to have at least 24 bytes, with the first 12 bytes copied
304b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     to the first cell of the Allocation, and the next 12 bytes copied to
305b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *     the second cell. There is no mapping for the padding components.
306b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
307b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> Similar to copying data to an Allocation from an array, when copying data from an
308b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Allocation to an array, the padding components for Vec3 Element cells will not be
309b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * copied/mapped to the array if AutoPadding is enabled.
310b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
311b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> Default: Disabled.
3129eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang     *
3137bcaa62bca55f3ec054894e1425eeea1e3b2c0cdMiao Wang     * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
3149eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang     *
3159eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang     */
3169eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang    public void setAutoPadding(boolean useAutoPadding) {
3179eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        mAutoPadding = useAutoPadding;
3189eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang    }
3199eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang
3209eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang    /**
32198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * Get the size of the Allocation in bytes.
32298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
32398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return size of the Allocation in bytes.
32498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
32598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
32698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public int getBytesSize() {
3278379e65d0e9941d6157b8ed86e5d19b4a65c343dTim Murray        if (mType.mDimYuv != 0) {
3288379e65d0e9941d6157b8ed86e5d19b4a65c343dTim Murray            return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
3298379e65d0e9941d6157b8ed86e5d19b4a65c343dTim Murray        }
33098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mType.getCount() * mType.getElement().getBytesSize();
33198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
33298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
33398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void updateCacheInfo(Type t) {
33498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentDimX = t.getX();
33598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentDimY = t.getY();
33698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentDimZ = t.getZ();
33798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mCurrentCount = mCurrentDimX;
33898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mCurrentDimY > 1) {
33998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mCurrentCount *= mCurrentDimY;
34098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
34198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mCurrentDimZ > 1) {
34298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            mCurrentCount *= mCurrentDimZ;
34398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
34498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
34598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
3461b370e358d16cc3b50b169511d6b387db09f972dJason Sams    private void setBitmap(Bitmap b) {
3471b370e358d16cc3b50b169511d6b387db09f972dJason Sams        mBitmap = b;
3481b370e358d16cc3b50b169511d6b387db09f972dJason Sams    }
3491b370e358d16cc3b50b169511d6b387db09f972dJason Sams
3503d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    Allocation(long id, RenderScript rs, Type t, int usage) {
35198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        super(id, rs);
352c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((usage & ~(USAGE_SCRIPT |
353c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_GRAPHICS_TEXTURE |
354c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_IO_INPUT |
355c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_IO_OUTPUT |
356c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                       USAGE_SHARED)) != 0) {
35798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
35898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
35998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
360c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((usage & USAGE_IO_INPUT) != 0) {
361c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            mWriteAllowed = false;
362c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
363c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            if ((usage & ~(USAGE_IO_INPUT |
364c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                           USAGE_GRAPHICS_TEXTURE |
365c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                           USAGE_SCRIPT)) != 0) {
366c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                throw new RSIllegalArgumentException("Invalid usage combination.");
367c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            }
368c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
369c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
37098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mType = t;
37198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mUsage = usage;
372059fede7f200350b6131fc131f76248085485722Miao Wang        mIncCompatAllocation = 0;
373059fede7f200350b6131fc131f76248085485722Miao Wang        mIncAllocDestroyed = false;
37498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
37598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (t != null) {
3767f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            // TODO: A3D doesn't have Type info during creation, so we can't
3777f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            // calculate the size ahead of time. We can possibly add a method
3787f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            // to update the size in the future if it seems reasonable.
3797f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            mSize = mType.getCount() * mType.getElement().getBytesSize();
38098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            updateCacheInfo(t);
38198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
3827720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        if (RenderScript.sUseGCHooks == true) {
3837720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            try {
3847720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
3857720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            } catch (Exception e) {
3867720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
3877720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray                throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
3887720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            }
3897720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        }
3907720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    }
3917720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray
3927720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray    protected void finalize() throws Throwable {
3937720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        if (RenderScript.sUseGCHooks == true) {
3947720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray            RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
3957720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        }
3967720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray        super.finalize();
39798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
39898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
3993d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    private void validateIsInt64() {
4003d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang        if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
4013d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
4023d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            return;
4033d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang        }
4043d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang        throw new RSIllegalArgumentException(
4053d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            "64 bit integer source does not match allocation type " + mType.mElement.mType);
4063d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    }
4077720e5ceb6a4cc9ed330ad7c483f7eac08f554e6Tim Murray
40898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsInt32() {
40998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
41098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
41198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
41298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
41398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
41498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
41598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
41698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
41798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsInt16() {
41898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
41998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
42098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
42198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
42298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
42398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
42498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
42598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
42698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsInt8() {
42798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
42898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
42998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
43098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
43198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
43298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
43398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
43498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
43598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsFloat32() {
43698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
43798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
43898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
43998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
44098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
44198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
44298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
4437f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    private void validateIsFloat64() {
4447f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (mType.mElement.mType == Element.DataType.FLOAT_64) {
4457f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            return;
4467f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
4477f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        throw new RSIllegalArgumentException(
4487f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            "64 bit float source does not match allocation type " + mType.mElement.mType);
4497f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    }
4507f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
45198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateIsObject() {
45298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
45398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
45498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
45598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
45698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT)) {
45798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return;
45898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
45998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSIllegalArgumentException(
46098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
46198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
46298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
46398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
46460c5b31f4448410221de043873b94797732afa66Stephen Hines     * Get the {@link android.support.v8.renderscript.Type} of the Allocation.
46598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
46698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return Type
46798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
46898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
46998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public Type getType() {
47098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return mType;
47198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
47298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
47398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
4747d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Propagate changes from one usage of the Allocation to the
4757d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * other usages of the Allocation.
47698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
47798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
47898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void syncAll(int srcLocation) {
47998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        switch (srcLocation) {
48098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case USAGE_SCRIPT:
48198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case USAGE_GRAPHICS_TEXTURE:
48298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
48398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        default:
48498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
48598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
48698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
48798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
48898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
48998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
490c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
4917d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Send a buffer to the output stream.  The contents of the Allocation will
4927d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * be undefined after this operation. This operation is only valid if {@link
4937d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #USAGE_IO_OUTPUT} is set on the Allocation.
4947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
495c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
496c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
497c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public void ioSend() {
498c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
499c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            throw new RSIllegalArgumentException(
500c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                "Can only send buffer if IO_OUTPUT usage specified.");
501c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
502c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.validate();
503c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.nAllocationIoSend(getID(mRS));
504c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
505c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
506c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
507c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * Delete once code is updated.
508c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
509c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public void ioSendOutput() {
510c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        ioSend();
511c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
512c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
513b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Gets or creates a ByteBuffer that contains the raw data of the current Allocation.
514b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation is created with USAGE_IO_INPUT, the returned ByteBuffer
515b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * would contain the up-to-date data as READ ONLY.
516b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
517b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the Allocation has certain alignment. The size of each row including padding,
518b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * called stride, can be queried using the {@link #getStride()} method.
519b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
520b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Note: Operating on the ByteBuffer of a destroyed Allocation will triger errors.
521b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *       The ByteBuffer will be Read-Only for devices before Lollopop (API 21).
522b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
523b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @return ByteBuffer The ByteBuffer associated with raw data pointer of the Allocation.
524654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang     */
525654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang    public ByteBuffer getByteBuffer() {
526654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        int xBytesSize = mType.getX() * mType.getElement().getBytesSize();
527654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        // When running on devices before L, we need to construct the ByteBuffer
528654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        // and explicitly copy the data from the allocation to it.
529654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        if (mRS.getDispatchAPILevel() < 21) {
530654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            byte[] data = null;
531654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            if (mType.getZ() > 0) {
532654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                // TODO: add support for 3D allocations.
533654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                return null;
534654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            } else if (mType.getY() > 0) {
535654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                // 2D Allocation
536654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                data = new byte[xBytesSize * mType.getY()];
537654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                copy2DRangeToUnchecked(0, 0, mType.getX(), mType.getY(), data,
538654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                                       Element.DataType.SIGNED_8, xBytesSize * mType.getY());
539654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            } else {
540654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                // 1D Allocation
541654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                data = new byte[xBytesSize];
542654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                copy1DRangeToUnchecked(0, mType.getX(), data);
543654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            }
544654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            ByteBuffer bBuffer = ByteBuffer.wrap(data).asReadOnlyBuffer();
545654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            mByteBufferStride = xBytesSize;
546654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            return bBuffer;
547654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        }
548654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        // Create a new ByteBuffer if it is not initialized or using IO_INPUT.
549654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        if (mByteBuffer == null || (mUsage & USAGE_IO_INPUT) != 0) {
550654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            mByteBuffer = mRS.nAllocationGetByteBuffer(getID(mRS), xBytesSize, mType.getY(), mType.getZ());
551654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        }
552654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        return mByteBuffer;
553654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang    }
554654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang
555654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang    /**
556b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Gets the stride of the Allocation.
557b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
558b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the Allocation has certain alignment. The size of each row including such
559b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * padding is called stride.
560b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
561b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @return the stride. For 1D Allocation, the stride will be the number of
562b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *         bytes of this Allocation. For 2D and 3D Allocations, the stride
563b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *         will be the stride in X dimension measuring in bytes.
564654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang     */
565654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang    public long getStride() {
566654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        if (mByteBufferStride ==0) {
567654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            if (mRS.getDispatchAPILevel() > 21) {
568654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                mByteBufferStride = mRS.nAllocationGetStride(getID(mRS));
569654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            } else {
570654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang                mByteBufferStride = mType.getX() * mType.getElement().getBytesSize();
571654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang            }
572654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        }
573654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang        return mByteBufferStride;
574654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang    }
575654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang
576654520a64a21237eaf526f2d5aa24613b25fb0ccMiao Wang    /**
5777d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Receive the latest input into the Allocation. This operation
5787d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
579c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
580c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
581c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    public void ioReceive() {
582c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if ((mUsage & USAGE_IO_INPUT) == 0) {
583c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            throw new RSIllegalArgumentException(
584c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                "Can only receive if IO_INPUT usage specified.");
585c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
586c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.validate();
587c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        mRS.nAllocationIoReceive(getID(mRS));
588c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
58998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
59098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
5917d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an array of RS objects to the Allocation.
59298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
59398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d Source array.
59498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
59598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(BaseObj[] d) {
59698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
59798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsObject();
59898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (d.length != mCurrentCount) {
59998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
60098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
60198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
6023d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang
6033d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang        if (RenderScript.sPointerSize == 8) {
6043d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            long i[] = new long[d.length * 4];
6053d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            for (int ct=0; ct < d.length; ct++) {
6063d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang                i[ct * 4] = d[ct].getID(mRS);
6073d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            }
6083d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            copy1DRangeFromUnchecked(0, mCurrentCount, i);
6093d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang        } else {
6103d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            int i[] = new int[d.length];
6113d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            for (int ct=0; ct < d.length; ct++) {
6123d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang                i[ct] = (int)d[ct].getID(mRS);
6133d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            }
6143d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            copy1DRangeFromUnchecked(0, mCurrentCount, i);
61598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
61698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
61798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
61898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateBitmapFormat(Bitmap b) {
61998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Bitmap.Config bc = b.getConfig();
620c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (bc == null) {
621c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
622c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
62398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        switch (bc) {
62498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case ALPHA_8:
62598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
62698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
62798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
62898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
62998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
63098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
63198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
63298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
63398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case ARGB_8888:
63498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
63598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                (mType.getElement().getBytesSize() != 4)) {
63698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
63798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
63898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
63998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
64098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
64198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
64298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
64398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case RGB_565:
64498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
64598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                (mType.getElement().getBytesSize() != 2)) {
64698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
64798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
64898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
64998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
65098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
65198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
65298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
65398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        case ARGB_4444:
65498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
65598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                (mType.getElement().getBytesSize() != 2)) {
65698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
65798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mKind + ", type " +
65898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     mType.getElement().mType +
65998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
66098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     " bytes, passed bitmap was " + bc);
66198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
66298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            break;
66337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
66498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
66598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
66698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
66798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validateBitmapSize(Bitmap b) {
66898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
66998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
67098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
67198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
67298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
6737f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
6747f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        mRS.validate();
6757f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        if (mCurrentDimZ > 0) {
6767f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
6777f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        } else if (mCurrentDimY > 0) {
6787f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
6797f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        } else {
6807f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang            copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
6817f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        }
6827f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    }
6837f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
6847f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    /**
6857f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     * Copy into this Allocation from an array. This method does not guarantee
6867f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     * that the Allocation is compatible with the input buffer; it copies memory
6877f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     * without reinterpretation.
6887f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     *
689b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
690b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
691b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
692b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
693b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
694b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
695b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
696b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
697b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
698b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
699b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
700b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
701b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
702b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
703b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param array The source array
7047f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     */
7057f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    public void copyFromUnchecked(Object array) {
7067f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
7077f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                          java.lang.reflect.Array.getLength(array));
7087f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    }
7097f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
71098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7117d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
7127d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
7137d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
71498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
715b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
716b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
717b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
718b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
719b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
720b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
721b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
722b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
723b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
724b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
725b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
726b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
727b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
728b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
729b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
73098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
73198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(int[] d) {
7327f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
73398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
7347f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
73598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
7377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
7387d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
73998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
740b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
741b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
742b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
743b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
744b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
745b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
746b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
747b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
748b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
749b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
750b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
751b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
752b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
753b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
754b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
75598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
75698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(short[] d) {
7577f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
75898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
7597f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
76098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7617d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
7627d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
7637d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
76498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
765b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
766b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
767b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
768b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
769b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
770b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
771b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
772b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
773b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
774b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
775b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
776b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
777b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
778b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
779b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
78098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
78198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(byte[] d) {
7827f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
78398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
7847f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
78598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
7867d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array. This method does not guarantee
7877d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
7887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * without reinterpretation.
78998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
790b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
791b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
792b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
793b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
794b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
795b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
796b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
797b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
798b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
799b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
800b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
801b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
802b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
803b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
804b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
80598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
80698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFromUnchecked(float[] d) {
8077f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
8087f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    }
8097f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
8107f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
8117f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    /**
8127f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     * Copy into this Allocation from an array.  This variant is type checked
8137f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     * and will generate exceptions if the Allocation's {@link
814b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} does not match the array's
8157f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     * primitive type.
8167f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     *
817b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
818b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
819b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
820b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
821b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
822b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
823b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
824b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
825b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
826b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
827b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
828b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
829b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
830b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
831b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param array The source array
8327f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     */
8337f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    public void copyFrom(Object array) {
8347f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
8357f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                          java.lang.reflect.Array.getLength(array));
83698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
83798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
83898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
8397d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
8407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
841b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
842b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
843b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
844b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
845b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
846b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
847b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
848b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
849b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
850b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
851b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
85298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
853b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
854b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
855b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
856b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
857b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
858b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
85998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
86098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(int[] d) {
8617f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        validateIsInt32();
8627f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
86398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
86498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
86598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
8667d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
8677d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
868b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
869b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
870b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
871b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
872b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
873b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
874b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
875b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
876b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
877b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
878b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
879b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
880b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
881b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
882b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
883b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
88498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
885b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
88698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
88798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(short[] d) {
8887f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        validateIsInt16();
8897f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
89098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
89198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
89298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
8937d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
8947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
895b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
896b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
897b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
898b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
899b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
900b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
901b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
902b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
903b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
904b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
905b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
906b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
907b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
908b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
909b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
910b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
91198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
912b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
91398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
91498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(byte[] d) {
9157f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        validateIsInt8();
9167f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
91798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
91898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
91998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9207d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into this Allocation from an array.  This variant is type checked
9217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and will generate exceptions if the Allocation's {@link
922b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
923b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
92498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
925b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
926b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
927b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
928b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
929b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
930b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
931b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
932b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must be part of the array.
933b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
934b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
935b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
936b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
937b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
938b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
939b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
94098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
94198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(float[] d) {
9427f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        validateIsFloat32();
9437f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
94498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
94598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
94698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9477d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy into an Allocation from a {@link android.graphics.Bitmap}.  The
9487d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * height, width, and format of the bitmap must match the existing
9497d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * allocation.
9507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
9517d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
9527d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.Bitmap} used to create the Allocation with {@link
9537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
9547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * this will synchronize the Allocation with the latest data from the {@link
9557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
95698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
95798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b the source bitmap
95898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
95998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyFrom(Bitmap b) {
96098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
961c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (b.getConfig() == null) {
962c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
963c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Canvas c = new Canvas(newBitmap);
964c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            c.drawBitmap(b, 0, 0, null);
965c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            copyFrom(newBitmap);
966c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            return;
967c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
96898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapSize(b);
96998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapFormat(b);
97098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationCopyFromBitmap(getID(mRS), b);
97198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
97298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
97398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
9747d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy an Allocation from an Allocation.  The types of both allocations
9751b370e358d16cc3b50b169511d6b387db09f972dJason Sams     * must be identical.
9761b370e358d16cc3b50b169511d6b387db09f972dJason Sams     *
9771b370e358d16cc3b50b169511d6b387db09f972dJason Sams     * @param a the source allocation
9781b370e358d16cc3b50b169511d6b387db09f972dJason Sams     */
9791b370e358d16cc3b50b169511d6b387db09f972dJason Sams    public void copyFrom(Allocation a) {
9801b370e358d16cc3b50b169511d6b387db09f972dJason Sams        mRS.validate();
9811b370e358d16cc3b50b169511d6b387db09f972dJason Sams        if (!mType.equals(a.getType())) {
9821b370e358d16cc3b50b169511d6b387db09f972dJason Sams            throw new RSIllegalArgumentException("Types of allocations must match.");
9831b370e358d16cc3b50b169511d6b387db09f972dJason Sams        }
9841b370e358d16cc3b50b169511d6b387db09f972dJason Sams        copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
9851b370e358d16cc3b50b169511d6b387db09f972dJason Sams    }
9861b370e358d16cc3b50b169511d6b387db09f972dJason Sams
9871b370e358d16cc3b50b169511d6b387db09f972dJason Sams
9881b370e358d16cc3b50b169511d6b387db09f972dJason Sams    /**
9897d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * This is only intended to be used by auto-generated code reflected from
9907d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * the RenderScript script files and should not be used by developers.
99198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
99298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xoff
99398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param fp
99498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
99598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
99698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
99798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int eSize = mType.mElement.getBytesSize();
99898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        final byte[] data = fp.getData();
99995730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        int data_length = fp.getPos();
100098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
100195730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        int count = data_length / eSize;
100295730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        if ((eSize * count) != data_length) {
100395730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines            throw new RSIllegalArgumentException("Field packer length " + data_length +
100498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                               " not divisible by element size " + eSize + ".");
100598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
100698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
100798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
100898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
100998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
10107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * This is only intended to be used by auto-generated code reflected from
10117d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * the RenderScript script files.
101298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
101398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xoff
101498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param component_number
101598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param fp
101698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
101798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
101898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
101998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (component_number >= mType.mElement.mElements.length) {
102098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
102198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
102298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(xoff < 0) {
102398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
102498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
102598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
102698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        final byte[] data = fp.getData();
102795730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        int data_length = fp.getPos();
102898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int eSize = mType.mElement.mElements[component_number].getBytesSize();
102998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        eSize *= mType.mElement.mArraySizes[component_number];
103098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
103195730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines        if (data_length != eSize) {
103295730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
103398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                               " does not match component size " + eSize + ".");
103498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
103598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
103698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
103795730ed847182c0b424f87d14d4ce276496dfa66Stephen Hines                                     component_number, data, data_length);
103898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
103998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
10402b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
10412b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @hide
10422b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * This is only intended to be used by auto-generated code reflected from
10432b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * the RenderScript script files.
10442b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
10452b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param xoff
10462b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param yoff
10472b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param zoff
10482b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param component_number
10492b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param fp
10502b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
10512b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /*
10522b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
10532b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        mRS.validate();
10542b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        if (component_number >= mType.mElement.mElements.length) {
10552b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
10562b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
10572b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        if(xoff < 0) {
10582b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang            throw new RSIllegalArgumentException("Offset x must be >= 0.");
10592b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
10602b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        if(yoff < 0) {
10612b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang            throw new RSIllegalArgumentException("Offset y must be >= 0.");
10622b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
10632b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        if(zoff < 0) {
10642b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang            throw new RSIllegalArgumentException("Offset z must be >= 0.");
10652b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
10662b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
10672b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        final byte[] data = fp.getData();
10682b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        int data_length = fp.getPos();
10692b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        int eSize = mType.mElement.mElements[component_number].getBytesSize();
10702b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        eSize *= mType.mElement.mArraySizes[component_number];
10712b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
10722b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        if (data_length != eSize) {
10732b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
10742b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                                               " does not match component size " + eSize + ".");
10752b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
10762b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
10772b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
10782b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                                   component_number, data, data_length);
10792b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
10802b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    */
10812b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
10829eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang    private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
108398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
108498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(off < 0) {
108598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
108698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
108798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(count < 1) {
108898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
108998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
109098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if((off + count) > mCurrentCount) {
109198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
109298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                               ", got " + count + " at offset " + off + ".");
109398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
10949eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        if(usePadding) {
10959eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if(len < dataSize / 4 * 3) {
10969eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
10979eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
10989eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        } else {
10999eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if(len < dataSize) {
11009eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
11019eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
110298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
110398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
110498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
110598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
11067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Generate a mipmap chain. This is only valid if the Type of the Allocation
11077d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * includes mipmaps.
110898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
11097d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This function will generate a complete set of mipmaps from the top
11107d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * level LOD and place them into the script memory space.</p>
111198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
11127d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>If the Allocation is also using other memory spaces, a call to {@link
11137d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
111498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
111598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void generateMipmaps() {
111698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationGenerateMipmaps(getID(mRS));
111798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
111898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
11197f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    private void copy1DRangeFromUnchecked(int off, int count, Object array,
11207f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                          Element.DataType dt, int arrayLen) {
11217f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        final int dataSize = mType.mElement.getBytesSize() * count;
11229eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        // AutoPadding for Vec3 Element
11239eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        boolean usePadding = false;
11249eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
11259eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            usePadding = true;
11269eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        }
11279eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
11289eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
11299eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                              mType.mElement.mType.mSize, usePadding);
11307f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    }
11317f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
113298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1133b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
11347d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
113598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
1136b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1137b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1138b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1139b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1140b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1141b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1142b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1143b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1144b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1145b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1146b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1147b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1148b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1149b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
115098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
115198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
1152b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param array The source array
115398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
11547f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    public void copy1DRangeFromUnchecked(int off, int count, Object array) {
11557f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, array,
11567f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 validateObjectIsPrimitiveArray(array, false),
11577f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 java.lang.reflect.Array.getLength(array));
11583d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    }
11597f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
11603d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    /**
1161b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
11623d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
11633d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     *
1164b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1165b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1166b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1167b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1168b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1169b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1170b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1171b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1172b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1173b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1174b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1175b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1176b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1177b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
11783d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param off The offset of the first element to be copied.
11793d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param count The number of elements to be copied.
1180b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
11813d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     */
118298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
11837f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
118498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
11857f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
118698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1187b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
11887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
118998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
1190b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1191b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1192b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1193b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1194b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1195b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1196b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1197b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1198b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1199b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1200b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1201b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1202b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1203b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
120498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
120598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
1206b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
120798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
120898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
12097f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
121098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
12117f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
121298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1213b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
12147d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
121598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
1216b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1217b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1218b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1219b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1220b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1221b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1222b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1223b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1224b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1225b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1226b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1227b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1228b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1229b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
123098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
123198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
1232b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
123398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
123498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
12357f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
123698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
12377f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
123898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1239b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
12407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * guarantee that the Allocation is compatible with the input buffer.
124198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
1242b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1243b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1244b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1245b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1246b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1247b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1248b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1249b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1250b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1251b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1252b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1253b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1254b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1255b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
125698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
125798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
1258b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
125998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
126098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
12617f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
126298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
126398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
12647f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
126598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1266b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1267b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1268b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} does not match the component type
1269b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the array passed in.
1270b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1271b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1272b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1273b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1274b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1275b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1276b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1277b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1278b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1279b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1280b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1281b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1282b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1283b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
128498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
128598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
128698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
1287b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param array The source array.
128898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
12897f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    public void copy1DRangeFrom(int off, int count, Object array) {
12907f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, array,
12917f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 validateObjectIsPrimitiveArray(array, true),
12927f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 java.lang.reflect.Array.getLength(array));
12933d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    }
12943d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang
12953d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    /**
1296b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1297b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1298b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not an 32 bit integer nor a vector of 32 bit
1299b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1300b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1301b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1302b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1303b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1304b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1305b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1306b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1307b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1308b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1309b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1310b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1311b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1312b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1313b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
13143d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     *
13153d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param off The offset of the first element to be copied.
13163d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param count The number of elements to be copied.
1317b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
13183d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     */
131998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
132098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt32();
13217f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
132298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
132398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
132498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1325b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1326b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1327b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not an 16 bit integer nor a vector of 16 bit
1328b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1329b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1330b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1331b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1332b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1333b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1334b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1335b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1336b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1337b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1338b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1339b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1340b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1341b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1342b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
134398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
134498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
134598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
1346b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
134798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
134898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
134998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt16();
13507f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
135198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
135298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
135398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1354b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1355b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1356b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
1357b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1358b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1359b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1360b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1361b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1362b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1363b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1364b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1365b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1366b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1367b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1368b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1369b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1370b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1371b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
137298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
137398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
137498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
1375b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
137698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
137798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
137898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt8();
13797f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
138098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
138198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
138298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1383b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1384b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1385b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
1386b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
1387b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1388b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1389b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1390b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1391b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1392b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1393b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1394b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1395b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1396b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1397b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1398b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1399b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1400b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
140198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
140298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
140398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
1404b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array.
140598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
140698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
140798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsFloat32();
14087f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
140998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
141098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
141198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     /**
14127d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy part of an Allocation into this Allocation.
141398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
141498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param off The offset of the first element to be copied.
141598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param count The number of elements to be copied.
141698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param data the source data allocation.
141798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param dataOff off The offset of the first element in data to
141898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *          be copied.
141998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
142098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
142198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
142298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              mSelectedLOD, mSelectedFace.mID,
142398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              count, 1, data.getID(mRS), dataOff, 0,
142498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
142598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
142698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
142798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
142898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (mAdaptedAllocation != null) {
142998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
143098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        } else {
143198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
143298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (xoff < 0 || yoff < 0) {
143398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
143498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
143598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (h < 0 || w < 0) {
143698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
143798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
143898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
143998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
144098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            }
144198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
144298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
144398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
14447f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
14457f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                  Element.DataType dt, int arrayLen) {
144698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
144798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
14489eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h;
14499eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        // AutoPadding for Vec3 Element
14509eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        boolean usePadding = false;
14519eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        int sizeBytes = arrayLen * dt.mSize;
14529eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
14539eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
14549eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
14559eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
14569eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            usePadding = true;
14579eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            sizeBytes = dataSize;
14589eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        } else {
14599eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if (dataSize > sizeBytes) {
14609eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
14619eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
14629eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        }
14637f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
14649eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                              array, sizeBytes, dt,
14659eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                              mType.mElement.mType.mSize, usePadding);
146698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
146798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
14682da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    /**
14697d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1470b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array is assumed to be tightly packed. This variant is type checked
1471b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1472b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} does not match the input data type.
1473b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1474b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1475b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1476b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1477b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1478b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1479b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1480b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1481b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1482b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1483b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1484b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1485b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1486b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
14872da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     *
14887d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
14897d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
14907d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
14917d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
14927f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     * @param array Data to be placed into the Allocation
14932da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines     */
14947f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
14957f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
14967f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 validateObjectIsPrimitiveArray(array, true),
14977f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 java.lang.reflect.Array.getLength(array));
14982da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
14992da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
15007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
15017d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1502b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array is assumed to be tightly packed. This variant is type checked
1503b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1504b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
1505b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1506b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1507b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1508b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1509b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1510b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1511b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1512b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1513b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1514b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1515b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1516b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1517b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1518b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1519b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
15207d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
15217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
15227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
15237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
15247d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
15257d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
15267d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
15277f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
15287f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        validateIsInt8();
15297f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
15307f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 Element.DataType.SIGNED_8, data.length);
15312da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
15322da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
15337d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
15347d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1535b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array is assumed to be tightly packed. This variant is type checked
1536b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1537b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
1538b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1539b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1540b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1541b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1542b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1543b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1544b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1545b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1546b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1547b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1548b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1549b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1550b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1551b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1552b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
15537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
15547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
15557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
15567d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
15577d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
15587d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
15597d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
15607f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
15617f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        validateIsInt16();
15627f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
15637f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 Element.DataType.SIGNED_16, data.length);
15643d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    }
15653d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang
15663d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    /**
15673d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * Copy from an array into a rectangular region in this Allocation.  The
1568b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array is assumed to be tightly packed. This variant is type checked
1569b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1570b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
1571b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1572b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1573b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1574b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1575b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1576b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1577b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1578b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1579b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1580b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1581b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1582b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1583b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1584b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1585b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
15863d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     *
15873d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param xoff X offset of the region to update in this Allocation
15883d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param yoff Y offset of the region to update in this Allocation
15893d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param w Width of the region to update
15903d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param h Height of the region to update
15913d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param data to be placed into the Allocation
15923d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     */
15932da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
15942da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsInt32();
15957f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
15967f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 Element.DataType.SIGNED_32, data.length);
15972da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
15982da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
15997d435ae5ba100be5710b685653cc351cab159c11Stephen Hines    /**
16007d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1601b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array is assumed to be tightly packed. This variant is type checked
1602b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1603b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
1604b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
1605b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1606b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1607b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1608b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1609b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1610b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1611b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1612b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1613b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1614b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1615b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1616b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1617b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1618b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
16197d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
16207d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
16217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
16227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
16237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
16247d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data to be placed into the Allocation
16257d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     */
16262da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
16272da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines        validateIsFloat32();
16287f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
16297f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 Element.DataType.FLOAT_32, data.length);
16302da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines    }
16312da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines
163298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
16337d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy a rectangular region from an Allocation into a rectangular region in
16347d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * this Allocation.
163598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
16367d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region in this Allocation
16377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region in this Allocation
16387d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update.
16397d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update.
16407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data source Allocation.
16417d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataXoff X offset in source Allocation
16427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataYoff Y offset in source Allocation
164398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
164498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
164598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                Allocation data, int dataXoff, int dataYoff) {
164698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
164798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, w, h);
164898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
164998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              mSelectedLOD, mSelectedFace.mID,
165098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              w, h, data.getID(mRS), dataXoff, dataYoff,
165198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
165298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
165398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
165498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
16557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy a {@link android.graphics.Bitmap} into an Allocation.  The height
16567d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and width of the update will use the height and width of the {@link
16577d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * android.graphics.Bitmap}.
165898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
16597d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
16607d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
16617d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param data the Bitmap to be copied
166298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
166398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
166498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
1665c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (data.getConfig() == null) {
1666c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1667c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Canvas c = new Canvas(newBitmap);
1668c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            c.drawBitmap(data, 0, 0, null);
1669c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            copy2DRangeFrom(xoff, yoff, newBitmap);
167037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            return;
1671c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
167298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapFormat(data);
167398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
167498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
167598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
167698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
167737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
167837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        if (mAdaptedAllocation != null) {
167937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
168037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        } else {
168137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
168237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            if (xoff < 0 || yoff < 0 || zoff < 0) {
168337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
168437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            }
168537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            if (h < 0 || w < 0 || d < 0) {
168637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
168737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            }
168837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
168937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
169037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams            }
169137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        }
169237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
169337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
1694b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang    /**
1695b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a rectangular region from the array into the allocation.
1696b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * The array is assumed to be tightly packed.
1697b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1698b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * The data type of the array is not required to be the same as
1699b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the element data type.
1700b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     */
17017f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
17027f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                          Object array, Element.DataType dt, int arrayLen) {
170337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
170437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
17059eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h * d;
17069eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        // AutoPadding for Vec3 Element
17079eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        boolean usePadding = false;
17089eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        int sizeBytes = arrayLen * dt.mSize;
17099eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
17109eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
17119eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
17129eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
17139eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            usePadding = true;
17149eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            sizeBytes = dataSize;
17159eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        } else {
17169eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if (dataSize > sizeBytes) {
17179eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
17189eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
17199eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        }
17207f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
17219eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                              array, sizeBytes, dt,
17229eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                              mType.mElement.mType.mSize, usePadding);
172337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
172437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
172537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
1726b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from an array into a 3D region in this Allocation.  The
1727b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array is assumed to be tightly packed. This variant is type checked
1728b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1729b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} does not match the input data type.
1730b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1731b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * d * {@link #getElement}.{@link
1732b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
1733b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1734b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1735b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
1736b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1737b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1738b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1739b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
1740b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1741b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1742b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1743b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
17447d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
17457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
17467d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
17477d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param zoff Z offset of the region to update in this Allocation
17487d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update
17497d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update
17507d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param d Depth of the region to update
1751591c707ed160fefdb74b2097e4c62e82ae262fdbYing Wang     * @param array to be placed into the allocation
175237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
17537f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
17547f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
17557f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 validateObjectIsPrimitiveArray(array, true),
17567f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang                                 java.lang.reflect.Array.getLength(array));
175737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
175837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
175937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
176037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * Copy a rectangular region into the allocation from another
176137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * allocation.
176237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     *
17637d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param xoff X offset of the region to update in this Allocation
17647d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param yoff Y offset of the region to update in this Allocation
17657d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param zoff Z offset of the region to update in this Allocation
17667d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param w Width of the region to update.
17677d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param h Height of the region to update.
17687d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param d Depth of the region to update.
176937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     * @param data source allocation.
17707d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataXoff X offset of the region in the source Allocation
17717d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataYoff Y offset of the region in the source Allocation
17727d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param dataZoff Z offset of the region in the source Allocation
177337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams     */
177437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
177537ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                                Allocation data, int dataXoff, int dataYoff, int dataZoff) {
177637ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.validate();
177737ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
177837ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
177937ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
178037ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams                              data.mSelectedLOD);
178137ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    }
178237ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
178337ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams
178437ead9e1087bd091b52867d97cd7c2dd7b3f3a39Jason Sams    /**
17857d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Copy from the Allocation into a {@link android.graphics.Bitmap}.  The
17867d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * bitmap must match the dimensions of the Allocation.
178798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
178898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b The bitmap to be set from the Allocation.
178998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
179098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(Bitmap b) {
179198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.validate();
179298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapFormat(b);
179398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateBitmapSize(b);
179498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mRS.nAllocationCopyToBitmap(getID(mRS), b);
179598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
179698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
17977f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    private void copyTo(Object array, Element.DataType dt, int arrayLen) {
17987f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        mRS.validate();
17999eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        boolean usePadding = false;
18009eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
18019eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            usePadding = true;
18029eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        }
180349b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang        if (usePadding) {
180449b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang            if (dt.mSize * arrayLen < mSize / 4 * 3) {
180549b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang                throw new RSIllegalArgumentException(
180649b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang                    "Size of output array cannot be smaller than size of allocation.");
180749b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang            }
180849b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang        } else {
180949b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang            if (dt.mSize * arrayLen < mSize) {
181049b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang                throw new RSIllegalArgumentException(
181149b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang                    "Size of output array cannot be smaller than size of allocation.");
181249b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang            }
181349b9759d85dec36f10debafa3419ce7fff31d34eMiao Wang        }
18149eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
18157f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    }
18167f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
18177f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    /**
1818b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from the Allocation into an array. The method is type checked
1819b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1820b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} does not match the input data type.
1821b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1822b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1823b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
1824b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
1825b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1826b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1827b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1828b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1829b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells will be part of the array.
1830b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1831b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1832b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1833b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1834b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
18357f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     *
18367f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     * @param array The array to be set from the Allocation.
18377f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang     */
18387f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    public void copyTo(Object array) {
18397f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyTo(array, validateObjectIsPrimitiveArray(array, true),
18407f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang               java.lang.reflect.Array.getLength(array));
18417f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang    }
18427f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
184398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1844b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from the Allocation into a byte array. This variant is type checked
1845b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1846b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is neither an 8 bit integer nor a vector of 8 bit
1847b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1848b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1849b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1850b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
1851b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
1852b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1853b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1854b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1855b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1856b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells will be part of the array.
1857b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1858b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1859b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1860b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1861b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
186298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
186398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
186498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
186598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(byte[] d) {
186698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt8();
18677f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyTo(d, Element.DataType.SIGNED_8, d.length);
186898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
186998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
187098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1871b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from the Allocation into a short array. This variant is type checked
1872b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1873b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
1874b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1875b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1876b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1877b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
1878b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
1879b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1880b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1881b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1882b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1883b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells will be part of the array.
1884b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1885b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1886b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1887b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1888b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
188998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
189098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
189198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
189298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(short[] d) {
189398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt16();
18947f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyTo(d, Element.DataType.SIGNED_16, d.length);
18953d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    }
18963d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang
18973d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang    /**
1898b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from the Allocation into a int array. This variant is type checked
1899b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1900b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
1901b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1902b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1903b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1904b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
1905b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
1906b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1907b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1908b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1909b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1910b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells will be part of the array.
1911b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1912b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1913b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1914b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1915b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
19163d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     *
19173d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     * @param d The array to be set from the Allocation.
19183d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang     */
191998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(int[] d) {
192098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsInt32();
19217f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyTo(d, Element.DataType.SIGNED_32, d.length);
192298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
192398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
192498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
1925b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from the Allocation into a float array. This variant is type checked
1926b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
1927b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
1928b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
1929b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1930b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1931b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the Allocation {@link
1932b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * #getBytesSize getBytesSize()}.
1933b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1934b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1935b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
1936b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1937b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells will be part of the array.
1938b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
1939b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1940b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1941b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1942b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the cells must not be part of the array.
194398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
194498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param d The array to be set from the Allocation.
194598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
194698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    public void copyTo(float[] d) {
194798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        validateIsFloat32();
19487f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang        copyTo(d, Element.DataType.FLOAT_32, d.length);
194998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
195098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
19512b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
19522b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @hide
1953b600f3bcdbd9a8fecd18d273d94f9b05a438fdd2Miao Wang     * This is only intended to be used by auto-generated code reflected from
1954b600f3bcdbd9a8fecd18d273d94f9b05a438fdd2Miao Wang     * the RenderScript script files and should not be used by developers.
19552b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
19562b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param xoff
19572b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param yoff
19582b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param zoff
19592b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param component_number
1960b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param fp
19612b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
19622b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /*
1963b600f3bcdbd9a8fecd18d273d94f9b05a438fdd2Miao Wang    public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
19642b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        mRS.validate();
19652b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        if (component_number >= mType.mElement.mElements.length) {
19662b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
19672b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
19682b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        if(xoff < 0) {
19692b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang            throw new RSIllegalArgumentException("Offset x must be >= 0.");
19702b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
19712b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        if(yoff < 0) {
19722b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang            throw new RSIllegalArgumentException("Offset y must be >= 0.");
19732b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
19742b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        if(zoff < 0) {
19752b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang            throw new RSIllegalArgumentException("Offset z must be >= 0.");
19762b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
19772b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
1978b600f3bcdbd9a8fecd18d273d94f9b05a438fdd2Miao Wang        final byte[] data = fp.getData();
1979b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang        int data_length = data.length;
19802b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        int eSize = mType.mElement.mElements[component_number].getBytesSize();
19812b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        eSize *= mType.mElement.mArraySizes[component_number];
19822b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
1983b600f3bcdbd9a8fecd18d273d94f9b05a438fdd2Miao Wang        if (data_length != eSize) {
1984b600f3bcdbd9a8fecd18d273d94f9b05a438fdd2Miao Wang            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
1985b600f3bcdbd9a8fecd18d273d94f9b05a438fdd2Miao Wang                                               " does not match component size " + eSize + ".");
19862b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        }
19872b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
19882b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1989b600f3bcdbd9a8fecd18d273d94f9b05a438fdd2Miao Wang                                   component_number, data, data_length);
19902b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
19912b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    */
19922b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
19932b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    private void copy1DRangeToUnchecked(int off, int count, Object array,
19942b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                                        Element.DataType dt, int arrayLen) {
19952b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        final int dataSize = mType.mElement.getBytesSize() * count;
19969eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        // AutoPadding for Vec3 Element
19979eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        boolean usePadding = false;
19989eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
19999eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            usePadding = true;
20009eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        }
20019eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
20029eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
20039eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                              mType.mElement.mType.mSize, usePadding);
20042b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
20052b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
20062b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2007b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
20082b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * guarantee that the Allocation is compatible with the input buffer.
20092b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
2010b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2011b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2012b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2013b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2014b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2015b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2016b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2017b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2018b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2019b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2020b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2021b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2022b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2023b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
20242b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
20252b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2026b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param array The dest array
20272b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
20282b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeToUnchecked(int off, int count, Object array) {
20292b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, array,
20302b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               validateObjectIsPrimitiveArray(array, false),
20312b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               java.lang.reflect.Array.getLength(array));
20322b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
20332b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
20342b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2035b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
20362b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * guarantee that the Allocation is compatible with the input buffer.
20372b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
2038b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2039b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2040b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2041b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2042b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2043b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2044b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2045b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2046b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2047b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2048b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2049b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2050b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2051b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
20522b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
20532b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2054b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
20552b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
20562b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeToUnchecked(int off, int count, int[] d) {
20572b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
20582b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
20592b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
20602b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2061b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
20622b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * guarantee that the Allocation is compatible with the input buffer.
20632b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
2064b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2065b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2066b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2067b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2068b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2069b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2070b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2071b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2072b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2073b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2074b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2075b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2076b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2077b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
20782b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
20792b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2080b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
20812b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
20822b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeToUnchecked(int off, int count, short[] d) {
20832b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
20842b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
20852b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
20862b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2087b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
20882b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * guarantee that the Allocation is compatible with the input buffer.
20892b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
2090b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2091b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2092b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2093b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2094b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2095b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2096b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2097b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2098b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2099b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2100b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2101b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2102b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2103b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
21042b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
21052b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2106b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
21072b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
21082b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
21092b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
21102b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
21112b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
21122b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2113b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
21142b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * guarantee that the Allocation is compatible with the input buffer.
21152b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
2116b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2117b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2118b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2119b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2120b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2121b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2122b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2123b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2124b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2125b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2126b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2127b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2128b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2129b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
21302b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
21312b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2132b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
21332b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
21342b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeToUnchecked(int off, int count, float[] d) {
21352b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
21362b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
21372b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
21382b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
21392b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2140b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array.  This method is type checked
2141b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
2142b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} does not match the component type
2143b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the array passed in.
2144b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2145b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2146b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2147b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2148b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2149b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2150b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2151b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2152b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2153b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2154b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2155b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2156b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2157b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
21582b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
21592b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
21602b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2161b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param array The source array.
21622b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
21632b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeTo(int off, int count, Object array) {
21642b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, array,
21652b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               validateObjectIsPrimitiveArray(array, true),
21662b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               java.lang.reflect.Array.getLength(array));
21672b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
21682b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
21692b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2170b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array. This variant is type checked
2171b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
2172b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit integer nor a vector of 32 bit
2173b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
2174b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2175b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2176b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2177b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2178b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2179b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2180b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2181b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2182b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2183b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2184b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2185b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2186b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2187b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
21882b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
21892b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
21902b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2191b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
21922b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
21932b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeTo(int off, int count, int[] d) {
21942b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validateIsInt32();
21952b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
21962b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
21972b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
21982b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2199b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array. This variant is type checked
2200b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
2201b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is neither a 16 bit integer nor a vector of 16 bit
2202b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
2203b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2204b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2205b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2206b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2207b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2208b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2209b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2210b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2211b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2212b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2213b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2214b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2215b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2216b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
22172b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
22182b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
22192b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2220b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
22212b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
22222b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeTo(int off, int count, short[] d) {
22232b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validateIsInt16();
22242b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
22252b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
22262b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
22272b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2228b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array. This variant is type checked
2229b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
2230b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is neither an 8 bit integer nor a vector of 8 bit
2231b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
2232b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2233b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2234b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2235b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2236b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2237b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2238b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2239b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2240b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2241b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2242b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2243b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2244b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2245b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
22462b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
22472b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
22482b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2249b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array
22502b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
22512b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeTo(int off, int count, byte[] d) {
22522b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validateIsInt8();
22532b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
22542b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
22552b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
22562b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2257b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy a 1D region of this Allocation into an array. This variant is type checked
2258b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * and will generate exceptions if the Allocation's {@link
2259b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
2260b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
2261b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2262b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2263b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2264b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2265b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2266b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2267b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2268b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2269b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2270b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2271b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2272b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2273b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2274b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
22752b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
22762b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param off The offset of the first element to be copied.
22772b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param count The number of elements to be copied.
2278b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * @param d the source array.
22792b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
22802b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy1DRangeTo(int off, int count, float[] d) {
22812b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validateIsFloat32();
22822b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
22832b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
22842b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
22852b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
22862b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
22872b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                                Element.DataType dt, int arrayLen) {
22882b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        mRS.validate();
22892b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validate2DRange(xoff, yoff, w, h);
22909eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h;
22919eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        // AutoPadding for Vec3 Element
22929eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        boolean usePadding = false;
22939eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        int sizeBytes = arrayLen * dt.mSize;
22949eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
22959eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
22969eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
22979eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
22989eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            usePadding = true;
22999eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            sizeBytes = dataSize;
23009eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        } else {
23019eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if (dataSize > sizeBytes) {
23029eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
23039eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
23049eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        }
23052b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
23069eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                              array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
23072b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
23082b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
23092b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2310b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from a rectangular region in this Allocation into an array. This
2311b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * method is type checked and will generate exceptions if the Allocation's
2312b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * {@link android.support.v8.renderscript.Element} does not match the component type
2313b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the array passed in.
2314b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2315b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2316b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2317b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2318b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2319b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2320b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2321b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2322b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2323b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2324b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2325b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2326b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2327b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
23282b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
23292b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param xoff X offset of the region to copy in this Allocation
23302b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param yoff Y offset of the region to copy in this Allocation
23312b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param w Width of the region to copy
23322b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param h Height of the region to copy
23332b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param array Dest Array to be copied into
23342b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
23352b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
23362b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, array,
23372b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               validateObjectIsPrimitiveArray(array, true),
23382b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               java.lang.reflect.Array.getLength(array));
23392b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
23402b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
23412b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2342b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from a rectangular region in this Allocation into an array. This
2343b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * variant is type checked and will generate exceptions if the Allocation's
2344b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * {@link android.support.v8.renderscript.Element} is neither an 8 bit integer nor a vector
2345b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of 8 bit integers {@link android.support.v8.renderscript.Element.DataType}.
2346b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2347b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2348b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2349b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2350b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2351b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2352b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2353b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2354b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2355b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2356b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2357b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2358b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2359b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
23602b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
23612b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param xoff X offset of the region to copy in this Allocation
23622b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param yoff Y offset of the region to copy in this Allocation
23632b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param w Width of the region to copy
23642b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param h Height of the region to copy
2365591c707ed160fefdb74b2097e4c62e82ae262fdbYing Wang     * @param data Dest Array to be copied into
23662b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
23672b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
23682b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validateIsInt8();
23692b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
23702b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               Element.DataType.SIGNED_8, data.length);
23712b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
23722b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
23732b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2374b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from a rectangular region in this Allocation into an array. This
2375b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * variant is type checked and will generate exceptions if the Allocation's
2376b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * {@link android.support.v8.renderscript.Element} is neither a 16 bit integer nor a vector
2377b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of 16 bit integers {@link android.support.v8.renderscript.Element.DataType}.
2378b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2379b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2380b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2381b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2382b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2383b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2384b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2385b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2386b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2387b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2388b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2389b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2390b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2391b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
23922b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
23932b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param xoff X offset of the region to copy in this Allocation
23942b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param yoff Y offset of the region to copy in this Allocation
23952b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param w Width of the region to copy
23962b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param h Height of the region to copy
2397591c707ed160fefdb74b2097e4c62e82ae262fdbYing Wang     * @param data Dest Array to be copied into
23982b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
23992b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
24002b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validateIsInt16();
24012b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
24022b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               Element.DataType.SIGNED_16, data.length);
24032b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
24042b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
24052b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2406b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from a rectangular region in this Allocation into an array. This
2407b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * variant is type checked and will generate exceptions if the Allocation's
2408b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * {@link android.support.v8.renderscript.Element} is neither a 32 bit integer nor a vector
2409b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of 32 bit integers {@link android.support.v8.renderscript.Element.DataType}.
2410b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2411b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2412b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2413b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2414b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2415b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2416b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2417b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2418b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2419b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2420b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2421b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2422b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2423b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
24242b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
24252b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param xoff X offset of the region to copy in this Allocation
24262b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param yoff Y offset of the region to copy in this Allocation
24272b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param w Width of the region to copy
24282b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param h Height of the region to copy
2429591c707ed160fefdb74b2097e4c62e82ae262fdbYing Wang     * @param data Dest Array to be copied into
24302b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
24312b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
24322b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validateIsInt32();
24332b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
24342b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               Element.DataType.SIGNED_32, data.length);
24352b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
24362b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
24372b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2438b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from a rectangular region in this Allocation into an array. This
2439b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * variant is type checked and will generate exceptions if the Allocation's
2440b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * {@link android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector
2441b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
2442b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2443b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2444b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2445b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2446b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2447b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2448b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2449b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2450b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2451b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2452b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2453b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2454b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2455b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
24562b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
24572b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param xoff X offset of the region to copy in this Allocation
24582b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param yoff Y offset of the region to copy in this Allocation
24592b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param w Width of the region to copy
24602b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param h Height of the region to copy
2461591c707ed160fefdb74b2097e4c62e82ae262fdbYing Wang     * @param data Dest Array to be copied into
24622b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
24632b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
24642b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validateIsFloat32();
24652b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
24662b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                               Element.DataType.FLOAT_32, data.length);
24672b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
24682b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
24692b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
24702b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
2471b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from a 3D region in this Allocation into an array. This method does
2472b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * not guarantee that the Allocation is compatible with the input buffer.
2473b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * The array is assumed to be tightly packed.
24742b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
2475b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * The data type of the array is not required to be the same as
2476b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * the element data type.
24772b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
24782b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /*
24792b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
24802b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                                        Object array, Element.DataType dt, int arrayLen) {
24812b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        mRS.validate();
24822b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        validate3DRange(xoff, yoff, zoff, w, h, d);
24839eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h * d;
24849eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        // AutoPadding for Vec3 Element
24859eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        boolean usePadding = false;
24869eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        int sizeBytes = arrayLen * dt.mSize;
24879eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
24889eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
24899eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
24909eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
24919eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            usePadding = true;
24929eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            sizeBytes = dataSize;
24939eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        } else {
24949eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            if (dataSize > sizeBytes) {
24959eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
24969eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang            }
24979eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang        }
24982b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
24999eb28ebcc5777f3ea8d26065cbd2635775b93acfMiao Wang                              array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
25002b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
25012b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    */
25022b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
25032b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /**
25042b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @hide
2505b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Copy from a 3D region in this Allocation into an array. This
2506b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * method is type checked and will generate exceptions if the Allocation's
2507b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * {@link android.support.v8.renderscript.Element} does not match the component type
2508b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the array passed in.
2509b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2510b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> The size of the region is: w * h * d * {@link #getElement}.{@link
2511b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * Element#getBytesSize}.
2512b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2513b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2514b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * array in bytes must be at least the size of the region.
2515b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2516b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2517b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is disabled, then the size of the array in bytes must be at least the size
2518b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must be part of the array.
2519b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     *
2520b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2521b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2522b139bfeb57c3bb2b2042da201e5314c3f6fcd13fMiao Wang     * of the region. The padding bytes for the cells must not be part of the array.
25232b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     *
25242b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param xoff X offset of the region to copy in this Allocation
25252b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param yoff Y offset of the region to copy in this Allocation
25262b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param zoff Z offset of the region to copy in this Allocation
25272b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param w Width of the region to copy
25282b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param h Height of the region to copy
25292b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param d Depth of the region to copy
25302b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     * @param array Dest Array to be copied into
25312b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang     */
25322b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    /*
25332b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
25342b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang        copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
25352b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                                 validateObjectIsPrimitiveArray(array, true),
25362b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang                                 java.lang.reflect.Array.getLength(array));
25372b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    }
25382b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang    */
25392b7f06facd09f8f3ca3372733d316844767c8ecfMiao Wang
254098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    // creation
254198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
254298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
254398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static {
254498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        mBitmapOptions.inScaled = false;
254598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
254698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
254798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
25487d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a new Allocation with the given {@link
254960c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Type}, mipmap flag, and usage flags.
255098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
25517d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param type RenderScript type describing data layout
255298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the
255398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *             allocation
25547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param usage bit field specifying how the Allocation is
255598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
255698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
255798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
255898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
255998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (type.getID(rs) == 0) {
256098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSInvalidStateException("Bad Type");
256198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
256268171c40fc9a77c05da83453ac93a380960f36aaMiao Wang
256368171c40fc9a77c05da83453ac93a380960f36aaMiao Wang        if(!rs.usingIO() && (usage & (USAGE_IO_INPUT | USAGE_IO_INPUT)) != 0) {
256468171c40fc9a77c05da83453ac93a380960f36aaMiao Wang            throw new RSRuntimeException("USAGE_IO not supported, Allocation creation failed.");
256568171c40fc9a77c05da83453ac93a380960f36aaMiao Wang        }
256668171c40fc9a77c05da83453ac93a380960f36aaMiao Wang
25673d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang        long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
256898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (id == 0) {
256998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
257098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
257198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, type, usage);
257298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
257398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
257498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
25757d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation with the size specified by the type and no mipmaps
25767d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * generated by default
257798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
257898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
257998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param type renderscript type describing data layout
258098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the allocation is
258198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
258298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
258398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
258498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
258598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
258698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
258798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
258898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
258998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
25907d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation for use by scripts with a given {@link
259160c5b31f4448410221de043873b94797732afa66Stephen Hines     * android.support.v8.renderscript.Type} and no mipmaps
259298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
25937d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param rs Context to which the Allocation will belong.
25947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param type RenderScript Type describing data layout
259598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
259698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
259798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
259898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
259998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
260098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
260198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
260298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
26037d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation with a specified number of given elements
260498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
26057d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param rs Context to which the Allocation will belong.
26067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param e Element to use in the Allocation
26077d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param count the number of Elements in the Allocation
26087d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param usage bit field specifying how the Allocation is
260998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
261098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
261198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
261298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
261398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createSized(RenderScript rs, Element e,
261498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                         int count, int usage) {
261598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
261698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder b = new Type.Builder(rs, e);
261798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        b.setX(count);
261898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = b.create();
261998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
26203d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang        long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
262198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (id == 0) {
262298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
262398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
262498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, t, usage);
262598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
262698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
262798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
26287d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation with a specified number of given elements
262998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
26307d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param rs Context to which the Allocation will belong.
26317d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param e Element to use in the Allocation
26327d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param count the number of Elements in the Allocation
263398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
263498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation
263598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
263698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
263798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
263898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
263998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
264098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
264198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        final Bitmap.Config bc = b.getConfig();
264298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
264398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.A_8(rs);
264498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
264598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
264698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.RGBA_4444(rs);
264798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
264898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
264998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.RGBA_8888(rs);
265098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
265198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (bc == Bitmap.Config.RGB_565) {
265298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            return Element.RGB_565(rs);
265398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
265498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        throw new RSInvalidStateException("Bad bitmap type: " + bc);
265598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
265698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
265798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
265898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                       MipmapControl mip) {
265998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Element e = elementFromBitmap(rs, b);
266098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
266198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setX(b.getWidth());
266298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setY(b.getHeight());
266398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
266498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return tb.create();
266598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
266698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
266798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
26687d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
266998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
267098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
26717d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param b Bitmap source for the allocation data
267298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the
267398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *             allocation
267498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the allocation is
267598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *              utilized
267698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
26777d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing bitmap data
267898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
267998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
268098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
268198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                              MipmapControl mips,
268298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                              int usage) {
268398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
2684c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray
2685c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        // WAR undocumented color formats
2686c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        if (b.getConfig() == null) {
2687c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            if ((usage & USAGE_SHARED) != 0) {
2688c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray                throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
2689c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            }
2690c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
2691c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            Canvas c = new Canvas(newBitmap);
2692c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            c.drawBitmap(b, 0, 0, null);
2693c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray            return createFromBitmap(rs, newBitmap, mips, usage);
2694c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray        }
2695c5953678decd7963ed0515c2a0b8fcc92b3f3b9fTim Murray
269698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = typeFromBitmap(rs, b, mips);
269798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2698c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        // enable optimized bitmap path only with no mipmap and script-only usage
2699c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        if (mips == MipmapControl.MIPMAP_NONE &&
2700c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
27010f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines            usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
27023d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang            long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
2703c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            if (id == 0) {
2704c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                throw new RSRuntimeException("Load failed.");
2705c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            }
2706c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
2707c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            // keep a reference to the Bitmap around to prevent GC
2708c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            Allocation alloc = new Allocation(id, rs, t, usage);
2709c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            alloc.setBitmap(b);
2710c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            return alloc;
2711c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
2712c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
2713c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
27143d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang        long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
271598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (id == 0) {
271698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Load failed.");
271798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
271898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, t, usage);
271998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
272098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
272198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
272268171c40fc9a77c05da83453ac93a380960f36aaMiao Wang     * Associate a {@link android.view.Surface} with this Allocation. This
272368171c40fc9a77c05da83453ac93a380960f36aaMiao Wang     * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
272468171c40fc9a77c05da83453ac93a380960f36aaMiao Wang     *
272568171c40fc9a77c05da83453ac93a380960f36aaMiao Wang     * @param sur Surface to associate with allocation
272668171c40fc9a77c05da83453ac93a380960f36aaMiao Wang     */
272768171c40fc9a77c05da83453ac93a380960f36aaMiao Wang    public void setSurface(Surface sur) {
272868171c40fc9a77c05da83453ac93a380960f36aaMiao Wang        mRS.validate();
272968171c40fc9a77c05da83453ac93a380960f36aaMiao Wang        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
273068171c40fc9a77c05da83453ac93a380960f36aaMiao Wang            throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
273168171c40fc9a77c05da83453ac93a380960f36aaMiao Wang        }
27327f0ff145bed6c0c8202eaec304fa87b0eecae962Miao Wang
273368171c40fc9a77c05da83453ac93a380960f36aaMiao Wang        mRS.nAllocationSetSurface(getID(mRS), sur);
273468171c40fc9a77c05da83453ac93a380960f36aaMiao Wang    }
273568171c40fc9a77c05da83453ac93a380960f36aaMiao Wang
273668171c40fc9a77c05da83453ac93a380960f36aaMiao Wang    /**
27377d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
2738c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
27397d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This Allocation will be created with {@link #USAGE_SHARED}, and
27407d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link #USAGE_SCRIPT}.</p>
274198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
274298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
274398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b bitmap source for the allocation data
274498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
27457d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing bitmap data
274698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
274798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
274898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
274998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
27500f5bae87e2e3e3b0e66803122b5c4c7dd36d43ddStephen Hines                                USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
275198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
275298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
275398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
27547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
27557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * containing the horizontal list of cube faces. Each face must be a square,
27567d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * have the same size as all other faces, and have a width that is a power
27577d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * of 2.
275898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
275998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
27607d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @param b Bitmap with cubemap faces layed out in the following
276198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *          format: right, left, top, bottom, front, back
276298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
276398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the cubemap is utilized
276498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
276598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
276698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
276798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
276898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
276998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     MipmapControl mips,
277098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     int usage) {
277198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        rs.validate();
277298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
277398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int height = b.getHeight();
277498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int width = b.getWidth();
277598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
277698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (width % 6 != 0) {
277798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
277898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
277998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (width / 6 != height) {
278098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
278198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
278298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
278398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (!isPow2) {
278498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
278598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
278698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
278798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Element e = elementFromBitmap(rs, b);
278898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
278998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setX(height);
279098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setY(height);
279198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setFaces(true);
279298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
279398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = tb.create();
279498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
27953d9b60c9ae71c4c09df0b4e59c825a5d631e1254Miao Wang        long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
279698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if(id == 0) {
279798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
279898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
279998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return new Allocation(id, rs, t, usage);
280098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
280198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
280298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
28037d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
28047d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * from a {@link android.graphics.Bitmap} containing the horizontal list of
28057d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * cube faces. Each face must be a square, have the same size as all other
28067d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * faces, and have a width that is a power of 2.
280798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
280898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
280998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param b bitmap with cubemap faces layed out in the following
281098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *          format: right, left, top, bottom, front, back
281198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
281298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
281398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
281498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
281598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs,
281698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                     Bitmap b) {
281798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
281898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                       USAGE_GRAPHICS_TEXTURE);
281998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
282098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
282198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
28227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
28237d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * objects containing the cube faces. Each face must be a square, have the
28247d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * same size as all other faces, and have a width that is a power of 2.
282598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
282698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
282798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xpos cubemap face in the positive x direction
282898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xneg cubemap face in the negative x direction
282998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param ypos cubemap face in the positive y direction
283098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param yneg cubemap face in the negative y direction
283198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zpos cubemap face in the positive z direction
283298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zneg cubemap face in the negative z direction
283398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
283498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param usage bit field specifying how the cubemap is utilized
283598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
283698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
283798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
283898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
283998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
284098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xpos,
284198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xneg,
284298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap ypos,
284398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap yneg,
284498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zpos,
284598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zneg,
284698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        MipmapControl mips,
284798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        int usage) {
2848ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        /*
284998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        int height = xpos.getHeight();
285098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (xpos.getWidth() != height ||
285198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            xneg.getWidth() != height || xneg.getHeight() != height ||
285298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            ypos.getWidth() != height || ypos.getHeight() != height ||
285398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            yneg.getWidth() != height || yneg.getHeight() != height ||
285498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            zpos.getWidth() != height || zpos.getHeight() != height ||
285598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            zneg.getWidth() != height || zneg.getHeight() != height) {
285698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
285798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
285898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
285998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        if (!isPow2) {
286098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
286198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        }
286298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
286398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Element e = elementFromBitmap(rs, xpos);
286498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
286598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setX(height);
286698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setY(height);
286798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setFaces(true);
286898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
286998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Type t = tb.create();
287098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
287198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
287298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
287398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_X);
287498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(xpos);
287598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
287698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(xneg);
287798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
287898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(ypos);
287998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
288098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(yneg);
288198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
288298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(zpos);
288398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
288498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        adapter.copyFrom(zneg);
288598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
288698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return cubemap;
2887ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        */
2888ce8b0e674c93035013d1c33aaabc9bb6ceffde0fTim Murray        return null;
288998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
289098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
289198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    /**
28927d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a sampler input
28937d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * from 6 {@link android.graphics.Bitmap} objects containing the cube
28947d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * faces. Each face must be a square, have the same size as all other faces,
28957d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * and have a width that is a power of 2.
289698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
289798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param rs Context to which the allocation will belong.
289898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xpos cubemap face in the positive x direction
289998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param xneg cubemap face in the negative x direction
290098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param ypos cubemap face in the positive y direction
290198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param yneg cubemap face in the negative y direction
290298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zpos cubemap face in the positive z direction
290398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @param zneg cubemap face in the negative z direction
290498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
290598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     * @return allocation containing cubemap data
290698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     *
290798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams     */
290898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
290998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xpos,
291098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap xneg,
291198a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap ypos,
291298a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap yneg,
291398a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zpos,
291498a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                                        Bitmap zneg) {
291598a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
291698a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
291798a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams                                          USAGE_GRAPHICS_TEXTURE);
291898a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams    }
291998a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2920c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
29217d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation from the Bitmap referenced
29227d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * by resource ID.
2923c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
2924c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param rs Context to which the allocation will belong.
2925c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param res application resources
2926c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param id resource id to load the data from
2927c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param mips specifies desired mipmap behaviour for the
2928c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *             allocation
2929c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param usage bit field specifying how the allocation is
2930c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *              utilized
2931c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
29327d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing resource data
2933c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
2934c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
2935c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
2936c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      Resources res,
2937c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      int id,
2938c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      MipmapControl mips,
2939c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      int usage) {
294098a281354fe06d1f970d0521c9a08d9eb0aa1a45Jason Sams
2941c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        rs.validate();
29427d435ae5ba100be5710b685653cc351cab159c11Stephen Hines        if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
29437d435ae5ba100be5710b685653cc351cab159c11Stephen Hines            throw new RSIllegalArgumentException("Unsupported usage specified.");
29447d435ae5ba100be5710b685653cc351cab159c11Stephen Hines        }
2945c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        Bitmap b = BitmapFactory.decodeResource(res, id);
2946c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        Allocation alloc = createFromBitmap(rs, b, mips, usage);
2947c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        b.recycle();
2948c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        return alloc;
2949c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
2950c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
2951c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
29527d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates a non-mipmapped Allocation to use as a graphics texture from the
29537d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link android.graphics.Bitmap} referenced by resource ID.
29547d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     *
29557d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * <p>This allocation will be created with {@link #USAGE_SCRIPT} and
29567d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
2957c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
2958c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param rs Context to which the allocation will belong.
2959c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param res application resources
2960c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param id resource id to load the data from
2961c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
29627d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * @return Allocation containing resource data
2963c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
2964c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
2965c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
2966c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      Resources res,
2967c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                                      int id) {
2968c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        return createFromBitmapResource(rs, res, id,
2969c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                        MipmapControl.MIPMAP_NONE,
29707d435ae5ba100be5710b685653cc351cab159c11Stephen Hines                                        USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
2971c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
2972c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray
2973c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    /**
29747d435ae5ba100be5710b685653cc351cab159c11Stephen Hines     * Creates an Allocation containing string data encoded in UTF-8 format.
2975c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
2976c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param rs Context to which the allocation will belong.
2977c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param str string to create the allocation from
2978c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     * @param usage bit field specifying how the allocaiton is
2979c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *              utilized
2980c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     *
2981c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray     */
2982c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    static public Allocation createFromString(RenderScript rs,
2983c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                              String str,
2984c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray                                              int usage) {
2985c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        rs.validate();
2986c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        byte[] allocArray = null;
2987c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        try {
2988c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            allocArray = str.getBytes("UTF-8");
2989c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
2990c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            alloc.copyFrom(allocArray);
2991c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            return alloc;
2992c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
2993c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        catch (Exception e) {
2994c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray            throw new RSRuntimeException("Could not convert string to utf-8.");
2995c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray        }
2996c5b37c751f22199fd3cf14f6ef19ed4b9664510fTim Murray    }
299767fa9ab954e42ccba012eab0d550176f167fb872Miao Wang
299867fa9ab954e42ccba012eab0d550176f167fb872Miao Wang    /**
2999059fede7f200350b6131fc131f76248085485722Miao Wang     * Frees any native resources associated with this object.  The
3000059fede7f200350b6131fc131f76248085485722Miao Wang     * primary use is to force immediate cleanup of resources when it is
3001059fede7f200350b6131fc131f76248085485722Miao Wang     * believed the GC will not respond quickly enough.
300267fa9ab954e42ccba012eab0d550176f167fb872Miao Wang     * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
300367fa9ab954e42ccba012eab0d550176f167fb872Miao Wang     */
300467fa9ab954e42ccba012eab0d550176f167fb872Miao Wang    @Override
300567fa9ab954e42ccba012eab0d550176f167fb872Miao Wang    public void destroy() {
3006059fede7f200350b6131fc131f76248085485722Miao Wang        if (mIncCompatAllocation != 0) {
3007059fede7f200350b6131fc131f76248085485722Miao Wang            boolean shouldDestroy = false;
3008059fede7f200350b6131fc131f76248085485722Miao Wang            synchronized(this) {
3009059fede7f200350b6131fc131f76248085485722Miao Wang                if (!mIncAllocDestroyed) {
3010059fede7f200350b6131fc131f76248085485722Miao Wang                    shouldDestroy = true;
3011059fede7f200350b6131fc131f76248085485722Miao Wang                    mIncAllocDestroyed = true;
3012059fede7f200350b6131fc131f76248085485722Miao Wang                }
3013059fede7f200350b6131fc131f76248085485722Miao Wang            }
3014059fede7f200350b6131fc131f76248085485722Miao Wang
3015059fede7f200350b6131fc131f76248085485722Miao Wang            if (shouldDestroy) {
3016059fede7f200350b6131fc131f76248085485722Miao Wang                // must include nObjDestroy in the critical section
3017059fede7f200350b6131fc131f76248085485722Miao Wang                ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
3018059fede7f200350b6131fc131f76248085485722Miao Wang                rlock.lock();
3019059fede7f200350b6131fc131f76248085485722Miao Wang                if(mRS.isAlive()) {
3020059fede7f200350b6131fc131f76248085485722Miao Wang                    mRS.nIncObjDestroy(mIncCompatAllocation);
3021059fede7f200350b6131fc131f76248085485722Miao Wang                }
3022059fede7f200350b6131fc131f76248085485722Miao Wang                rlock.unlock();
3023059fede7f200350b6131fc131f76248085485722Miao Wang                mIncCompatAllocation = 0;
3024059fede7f200350b6131fc131f76248085485722Miao Wang            }
3025059fede7f200350b6131fc131f76248085485722Miao Wang        }
3026059fede7f200350b6131fc131f76248085485722Miao Wang        if ((mUsage & (USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
302767fa9ab954e42ccba012eab0d550176f167fb872Miao Wang            setSurface(null);
302867fa9ab954e42ccba012eab0d550176f167fb872Miao Wang        }
302967fa9ab954e42ccba012eab0d550176f167fb872Miao Wang        super.destroy();
303067fa9ab954e42ccba012eab0d550176f167fb872Miao Wang    }
3031059fede7f200350b6131fc131f76248085485722Miao Wang
30322da197aafb9325a3ec5e546c4a15b73ce592c0b9Stephen Hines}
3033