145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams/*
28fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams * Copyright (C) 2008-2012 The Android Open Source Project
345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * you may not use this file except in compliance with the License.
645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * You may obtain a copy of the License at
745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
1045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * Unless required by applicable law or agreed to in writing, software
1145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
1245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * See the License for the specific language governing permissions and
1445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * limitations under the License.
1545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams */
1645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1745d443665f5ce7efa934706a89883f0cc87f3513Jason Samspackage android.support.v8.renderscript;
1845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1929f8636ee81c93588204e54273df97d8326b103cMiao Wangimport java.nio.ByteBuffer;
20e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wangimport java.util.concurrent.locks.ReentrantReadWriteLock;
2129f8636ee81c93588204e54273df97d8326b103cMiao Wang
2245d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.content.res.Resources;
2345d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.graphics.Bitmap;
2445d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.graphics.BitmapFactory;
25c806aeedb20cb1a725092657ce3358eb4008222bTim Murrayimport android.graphics.Canvas;
2645d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.util.Log;
27e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wangimport android.view.Surface;
2845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
2945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams/**
30032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * <p> This class provides the primary method through which data is passed to
31032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * and from RenderScript kernels.  An Allocation provides the backing store for
322192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * a given {@link android.support.v8.renderscript.Type}.  </p>
3345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
34032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * <p>An Allocation also contains a set of usage flags that denote how the
35032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * Allocation could be used. For example, an Allocation may have usage flags
36032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * specifying that it can be used from a script as well as input to a {@link
372192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * android.support.v8.renderscript.Sampler}. A developer must synchronize
382192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * across these different usages using
392192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * {@link android.support.v8.renderscript.Allocation#syncAll} in
40032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * order to ensure that different users of the Allocation have a consistent view
41032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * of memory. For example, in the case where an Allocation is used as the output
42032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * of one kernel and as Sampler input in a later kernel, a developer must call
43032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
44032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * second kernel to ensure correctness.
4545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
46032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
47032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * more complex Element types, the {@link #copyFromUnchecked} methods can be
48032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * used to copy from byte arrays or similar constructs.</p>
4945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
5045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * <div class="special reference">
5145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * <h3>Developer Guides</h3>
522192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * <p>For more information about creating an application that uses
532192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * RenderScript, read the
542192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a>
552192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * developer guide.</p>
5645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * </div>
5745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams **/
5845d443665f5ce7efa934706a89883f0cc87f3513Jason Samspublic class Allocation extends BaseObj {
5945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Type mType;
6045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Bitmap mBitmap;
6145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mUsage;
62d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray    int mSize;
6329f8636ee81c93588204e54273df97d8326b103cMiao Wang    Allocation mAdaptedAllocation;
6429f8636ee81c93588204e54273df97d8326b103cMiao Wang    ByteBuffer mByteBuffer = null;
6529f8636ee81c93588204e54273df97d8326b103cMiao Wang    long mByteBufferStride = 0;
6645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
6745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mConstrainedLOD;
6845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mConstrainedFace;
6945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mConstrainedY;
7045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mConstrainedZ;
7145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mReadAllowed = true;
7245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    boolean mWriteAllowed = true;
732e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    boolean mAutoPadding = false;
7445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mSelectedY;
7545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mSelectedZ;
7645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mSelectedLOD;
7745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
7845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
7945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mCurrentDimX;
8045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mCurrentDimY;
8145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mCurrentDimZ;
8245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mCurrentCount;
8345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
848352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
858352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        final Class c = d.getClass();
868352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (!c.isArray()) {
878352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
888352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
898352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        final Class cmp = c.getComponentType();
908352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (!cmp.isPrimitive()) {
918352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
928352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
938352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
948352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (cmp == Long.TYPE) {
958352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            if (checkType) {
968352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                validateIsInt64();
978352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                return mType.mElement.mType;
988352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            }
998352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            return Element.DataType.SIGNED_64;
1008352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
1018352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
1028352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (cmp == Integer.TYPE) {
1038352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            if (checkType) {
1048352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                validateIsInt32();
1058352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                return mType.mElement.mType;
1068352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            }
1078352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            return Element.DataType.SIGNED_32;
1088352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
1098352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
1108352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (cmp == Short.TYPE) {
1118352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            if (checkType) {
1128352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                validateIsInt16();
1138352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                return mType.mElement.mType;
1148352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            }
1158352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            return Element.DataType.SIGNED_16;
1168352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
1178352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
1188352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (cmp == Byte.TYPE) {
1198352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            if (checkType) {
1208352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                validateIsInt8();
1218352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                return mType.mElement.mType;
1228352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            }
1238352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            return Element.DataType.SIGNED_8;
1248352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
1258352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
1268352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (cmp == Float.TYPE) {
1278352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            if (checkType) {
1288352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                validateIsFloat32();
1298352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            }
1308352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            return Element.DataType.FLOAT_32;
1318352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
1328352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
1338352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (cmp == Double.TYPE) {
1348352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            if (checkType) {
1358352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                validateIsFloat64();
1368352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            }
1378352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            return Element.DataType.FLOAT_64;
1388352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
1398352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        return null;
1408352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
1418352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
1426f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang    /*
1436f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     * Hold reference to the shared allocation in compat context
1446f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     * for Incremental Support Lib.
1456f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     */
1466f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang    long mIncCompatAllocation;
1476f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang    boolean mIncAllocDestroyed;
14845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
149032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The usage of the Allocation.  These signal to RenderScript where to place
150032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * the Allocation in memory.
15145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
152032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
153032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines
154032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
155032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation will be bound to and accessed by scripts.
15645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
15745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public static final int USAGE_SCRIPT = 0x0001;
15845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
15945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
160032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation will be used as a texture source by one or more graphics
161032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * programs.
16245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
16345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
16445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
16545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
16645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
167032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
168032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * consumer.  This usage will cause the Allocation to be created as
169032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * read-only.
1709c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1719c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
1729c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public static final int USAGE_IO_INPUT = 0x0020;
1739c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
1749c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
175032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation will be used as a {@link android.graphics.SurfaceTexture}
176032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * producer.  The dimensions and format of the {@link
177032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.SurfaceTexture} will be forced to those of the
178032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Allocation.
1799c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1809c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
1819c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public static final int USAGE_IO_OUTPUT = 0x0040;
1829c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
1839c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
184032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The Allocation's backing store will be inherited from another object
185032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * (usually a {@link android.graphics.Bitmap}); copying to or from the
186032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * original source Bitmap will cause a synchronization rather than a full
187032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * copy.  {@link #syncAll} may also be used to synchronize the Allocation
188032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and the source Bitmap.
1899c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
190032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This is set by default for allocations created with {@link
191032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #createFromBitmap} in API version 18 and higher.</p>
1929c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1939c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
1949c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public static final int USAGE_SHARED = 0x0080;
1959c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
1969c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
197032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Controls mipmap behavior when using the bitmap creation and update
198032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * functions.
19945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
20045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public enum MipmapControl {
20145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        /**
202032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * No mipmaps will be generated and the type generated from the incoming
203032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * bitmap will not contain additional LODs.
20445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams         */
20545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        MIPMAP_NONE(0),
20645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
20745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        /**
208032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * A full mipmap chain will be created in script memory.  The Type of
209032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * the Allocation will contain a full mipmap chain.  On upload, the full
210032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * chain will be transferred.
21145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams         */
21245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        MIPMAP_FULL(1),
21345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
21445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        /**
215032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * The Type of the Allocation will be the same as MIPMAP_NONE.  It will
216032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * not contain mipmaps.  On upload, the allocation data will contain a
217032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines         * full mipmap chain generated from the top level in script memory.
21845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams         */
21945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
22045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
22145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int mID;
22245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        MipmapControl(int id) {
22345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            mID = id;
22445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
22545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
22645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
2276f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang    /**
2286f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     * Getter & Setter for the dummy allocation for Inc Support Lib.
2296f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     *
2306f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     */
2316f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang    public long getIncAllocID() {
2326f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        return mIncCompatAllocation;
2336f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang    }
2346f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang    public void setIncAllocID(long id) {
2356f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        mIncCompatAllocation = id;
2366f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang    }
23745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
238bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    private long getIDSafe() {
23945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mAdaptedAllocation != null) {
24045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return mAdaptedAllocation.getID(mRS);
24145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
24245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return getID(mRS);
24345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
24445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
24545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
24645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams   /**
2472192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * Get the {@link android.support.v8.renderscript.Element} of the {@link
2482192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Type} of the Allocation.
24945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
250032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Element
25145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
25245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
25345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public Element getElement() {
25445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mType.getElement();
25545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
25645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
25745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
25845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * Get the usage flags of the Allocation.
25945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
260032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return usage this Allocation's set of the USAGE_* flags OR'd together
26145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
26245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
26345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public int getUsage() {
26445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mUsage;
26545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
26645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
26745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
268e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Specifies the mapping between the Allocation's cells and an array's elements
269e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * when data is copied from the Allocation to the array, or vice-versa.
270e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
271e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Only applies to an Allocation whose Element is a vector of length 3 (such as
272e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * {@link Element#U8_3} or {@link Element#RGB_888}). Enabling this feature may make
273e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * copying data from the Allocation to an array or vice-versa less efficient.
274e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
275e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> Vec3 Element cells are stored in an Allocation as Vec4 Element cells with
276e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the same {@link android.support.v8.renderscript.Element.DataType}, with the fourth vector
277e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * component treated as padding. When this feature is enabled, only the data components,
278e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * i.e. the first 3 vector components of each cell, will be mapped between the array
279e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and the Allocation. When disabled, explicit mapping of the padding components
280e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is required, as described in the following example.
281e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
282e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> For example, when copying an integer array to an Allocation of two {@link
283e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#I32_3} cells using {@link #copyFrom(int[])}:
284e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> When disabled:
285e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     The array must have at least 8 integers, with the first 4 integers copied
286e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     to the first cell of the Allocation, and the next 4 integers copied to
287e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     the second cell. The 4th and 8th integers are mapped as the padding components.
2880247ee75ca3d8bcdbeaca3d1731e7e21020c5deaMiao Wang     *
289e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> When enabled:
290e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     The array just needs to have at least 6 integers, with the first 3 integers
291e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     copied to the the first cell as data components, and the next 3 copied to
292e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     the second cell. There is no mapping for the padding components.
293e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
294e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> Similarly, when copying a byte array to an Allocation of two {@link
295e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#I32_3} cells, using {@link #copyFromUnchecked(int[])}:
296e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> When disabled:
297e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     The array must have at least 32 bytes, with the first 16 bytes copied
298e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     to the first cell of the Allocation, and the next 16 bytes copied to
299e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     the second cell. The 13th-16th and 29th-32nd bytes are mapped as padding
300e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     components.
301e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
302e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> When enabled:
303e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     The array just needs to have at least 24 bytes, with the first 12 bytes copied
304e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     to the first cell of the Allocation, and the next 12 bytes copied to
305e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *     the second cell. There is no mapping for the padding components.
306e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
307e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> Similar to copying data to an Allocation from an array, when copying data from an
308e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Allocation to an array, the padding components for Vec3 Element cells will not be
309e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * copied/mapped to the array if AutoPadding is enabled.
310e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
311e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> Default: Disabled.
3122e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang     *
313564639543231f50436ed36ac2b10a9228acef731Miao Wang     * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
3142e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang     *
3152e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang     */
3162e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    public void setAutoPadding(boolean useAutoPadding) {
3172e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        mAutoPadding = useAutoPadding;
3182e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    }
3192e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang
3202e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    /**
32145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * Get the size of the Allocation in bytes.
32245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
32345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return size of the Allocation in bytes.
32445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
32545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
32645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public int getBytesSize() {
327c5641d63cef417ab9f17d0886b7c1938f58c11b0Tim Murray        if (mType.mDimYuv != 0) {
328c5641d63cef417ab9f17d0886b7c1938f58c11b0Tim Murray            return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
329c5641d63cef417ab9f17d0886b7c1938f58c11b0Tim Murray        }
33045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mType.getCount() * mType.getElement().getBytesSize();
33145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
33245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
33345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void updateCacheInfo(Type t) {
33445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentDimX = t.getX();
33545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentDimY = t.getY();
33645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentDimZ = t.getZ();
33745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentCount = mCurrentDimX;
33845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mCurrentDimY > 1) {
33945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            mCurrentCount *= mCurrentDimY;
34045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
34145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mCurrentDimZ > 1) {
34245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            mCurrentCount *= mCurrentDimZ;
34345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
34445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
34545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
3468fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    private void setBitmap(Bitmap b) {
3478fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        mBitmap = b;
3488fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    }
3498fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams
350bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    Allocation(long id, RenderScript rs, Type t, int usage) {
35145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        super(id, rs);
3529c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((usage & ~(USAGE_SCRIPT |
3539c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_GRAPHICS_TEXTURE |
3549c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_IO_INPUT |
3559c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_IO_OUTPUT |
3569c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_SHARED)) != 0) {
35745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
35845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
35945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
3609c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((usage & USAGE_IO_INPUT) != 0) {
3619c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            mWriteAllowed = false;
3629c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
3639c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            if ((usage & ~(USAGE_IO_INPUT |
3649c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                           USAGE_GRAPHICS_TEXTURE |
3659c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                           USAGE_SCRIPT)) != 0) {
3669c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                throw new RSIllegalArgumentException("Invalid usage combination.");
3679c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            }
3689c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
3699c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
37045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mType = t;
37145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mUsage = usage;
3726f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        mIncCompatAllocation = 0;
3736f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        mIncAllocDestroyed = false;
37445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
37545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (t != null) {
3768352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            // TODO: A3D doesn't have Type info during creation, so we can't
3778352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            // calculate the size ahead of time. We can possibly add a method
3788352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            // to update the size in the future if it seems reasonable.
3798352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            mSize = mType.getCount() * mType.getElement().getBytesSize();
38045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            updateCacheInfo(t);
38145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
382d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        if (RenderScript.sUseGCHooks == true) {
383d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            try {
384d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray                RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
385d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            } catch (Exception e) {
386d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray                Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
387d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray                throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
388d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            }
389d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        }
390d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray    }
391d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray
392d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray    protected void finalize() throws Throwable {
393d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        if (RenderScript.sUseGCHooks == true) {
394d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
395d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        }
396d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        super.finalize();
39745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
39845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
399bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    private void validateIsInt64() {
400bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
401bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
402bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            return;
403bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        }
404bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        throw new RSIllegalArgumentException(
405bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            "64 bit integer source does not match allocation type " + mType.mElement.mType);
406bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
407d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray
40845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsInt32() {
40945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
41045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
41145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
41245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
41345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
41445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
41545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
41645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
41745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsInt16() {
41845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
41945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
42045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
42145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
42245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
42345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
42445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
42545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
42645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsInt8() {
42745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
42845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
42945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
43045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
43145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
43245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
43345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
43445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
43545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsFloat32() {
43645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
43745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
43845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
43945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
44045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
44145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
44245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
4438352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void validateIsFloat64() {
4448352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (mType.mElement.mType == Element.DataType.FLOAT_64) {
4458352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            return;
4468352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
4478352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        throw new RSIllegalArgumentException(
4488352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            "64 bit float source does not match allocation type " + mType.mElement.mType);
4498352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
4508352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
45145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsObject() {
45245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
45345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
45445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
45545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
45645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT)) {
45745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
45845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
45945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
46045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
46145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
46245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
46345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
4642192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * Get the {@link android.support.v8.renderscript.Type} of the Allocation.
46545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
46645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return Type
46745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
46845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
46945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public Type getType() {
47045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mType;
47145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
47245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
47345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
474032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Propagate changes from one usage of the Allocation to the
475032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * other usages of the Allocation.
47645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
47745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
47845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void syncAll(int srcLocation) {
47945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        switch (srcLocation) {
48045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case USAGE_SCRIPT:
48145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case USAGE_GRAPHICS_TEXTURE:
48245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
48345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        default:
48445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
48545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
48645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
48745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
48845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
48945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
4909c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
491032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Send a buffer to the output stream.  The contents of the Allocation will
492032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * be undefined after this operation. This operation is only valid if {@link
493032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #USAGE_IO_OUTPUT} is set on the Allocation.
494032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
4959c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
4969c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
4979c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public void ioSend() {
4989c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
4999c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            throw new RSIllegalArgumentException(
5009c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                "Can only send buffer if IO_OUTPUT usage specified.");
5019c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
5029c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.validate();
5039c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.nAllocationIoSend(getID(mRS));
5049c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
5059c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
5069c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
5079c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * Delete once code is updated.
5089c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
5099c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public void ioSendOutput() {
5109c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        ioSend();
5119c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
5129c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
513e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Gets or creates a ByteBuffer that contains the raw data of the current Allocation.
514e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation is created with USAGE_IO_INPUT, the returned ByteBuffer
515e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * would contain the up-to-date data as READ ONLY.
516e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
517e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the Allocation has certain alignment. The size of each row including padding,
518e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * called stride, can be queried using the {@link #getStride()} method.
519e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
520e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Note: Operating on the ByteBuffer of a destroyed Allocation will triger errors.
521e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *       The ByteBuffer will be Read-Only for devices before Lollopop (API 21).
522e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
523e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @return ByteBuffer The ByteBuffer associated with raw data pointer of the Allocation.
52429f8636ee81c93588204e54273df97d8326b103cMiao Wang     */
52529f8636ee81c93588204e54273df97d8326b103cMiao Wang    public ByteBuffer getByteBuffer() {
52629f8636ee81c93588204e54273df97d8326b103cMiao Wang        int xBytesSize = mType.getX() * mType.getElement().getBytesSize();
52729f8636ee81c93588204e54273df97d8326b103cMiao Wang        // When running on devices before L, we need to construct the ByteBuffer
52829f8636ee81c93588204e54273df97d8326b103cMiao Wang        // and explicitly copy the data from the allocation to it.
52929f8636ee81c93588204e54273df97d8326b103cMiao Wang        if (mRS.getDispatchAPILevel() < 21) {
53029f8636ee81c93588204e54273df97d8326b103cMiao Wang            byte[] data = null;
53129f8636ee81c93588204e54273df97d8326b103cMiao Wang            if (mType.getZ() > 0) {
53229f8636ee81c93588204e54273df97d8326b103cMiao Wang                // TODO: add support for 3D allocations.
53329f8636ee81c93588204e54273df97d8326b103cMiao Wang                return null;
53429f8636ee81c93588204e54273df97d8326b103cMiao Wang            } else if (mType.getY() > 0) {
53529f8636ee81c93588204e54273df97d8326b103cMiao Wang                // 2D Allocation
53629f8636ee81c93588204e54273df97d8326b103cMiao Wang                data = new byte[xBytesSize * mType.getY()];
53729f8636ee81c93588204e54273df97d8326b103cMiao Wang                copy2DRangeToUnchecked(0, 0, mType.getX(), mType.getY(), data,
53829f8636ee81c93588204e54273df97d8326b103cMiao Wang                                       Element.DataType.SIGNED_8, xBytesSize * mType.getY());
53929f8636ee81c93588204e54273df97d8326b103cMiao Wang            } else {
54029f8636ee81c93588204e54273df97d8326b103cMiao Wang                // 1D Allocation
54129f8636ee81c93588204e54273df97d8326b103cMiao Wang                data = new byte[xBytesSize];
54229f8636ee81c93588204e54273df97d8326b103cMiao Wang                copy1DRangeToUnchecked(0, mType.getX(), data);
54329f8636ee81c93588204e54273df97d8326b103cMiao Wang            }
54429f8636ee81c93588204e54273df97d8326b103cMiao Wang            ByteBuffer bBuffer = ByteBuffer.wrap(data).asReadOnlyBuffer();
54529f8636ee81c93588204e54273df97d8326b103cMiao Wang            mByteBufferStride = xBytesSize;
54629f8636ee81c93588204e54273df97d8326b103cMiao Wang            return bBuffer;
54729f8636ee81c93588204e54273df97d8326b103cMiao Wang        }
54829f8636ee81c93588204e54273df97d8326b103cMiao Wang        // Create a new ByteBuffer if it is not initialized or using IO_INPUT.
54929f8636ee81c93588204e54273df97d8326b103cMiao Wang        if (mByteBuffer == null || (mUsage & USAGE_IO_INPUT) != 0) {
55029f8636ee81c93588204e54273df97d8326b103cMiao Wang            mByteBuffer = mRS.nAllocationGetByteBuffer(getID(mRS), xBytesSize, mType.getY(), mType.getZ());
55129f8636ee81c93588204e54273df97d8326b103cMiao Wang        }
55229f8636ee81c93588204e54273df97d8326b103cMiao Wang        return mByteBuffer;
55329f8636ee81c93588204e54273df97d8326b103cMiao Wang    }
55429f8636ee81c93588204e54273df97d8326b103cMiao Wang
55529f8636ee81c93588204e54273df97d8326b103cMiao Wang    /**
556e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Gets the stride of the Allocation.
557e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * For a 2D or 3D Allocation, the raw data maybe padded so that each row of
558e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the Allocation has certain alignment. The size of each row including such
559e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * padding is called stride.
560e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
561e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @return the stride. For 1D Allocation, the stride will be the number of
562e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *         bytes of this Allocation. For 2D and 3D Allocations, the stride
563e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *         will be the stride in X dimension measuring in bytes.
56429f8636ee81c93588204e54273df97d8326b103cMiao Wang     */
56529f8636ee81c93588204e54273df97d8326b103cMiao Wang    public long getStride() {
56629f8636ee81c93588204e54273df97d8326b103cMiao Wang        if (mByteBufferStride ==0) {
56729f8636ee81c93588204e54273df97d8326b103cMiao Wang            if (mRS.getDispatchAPILevel() > 21) {
56829f8636ee81c93588204e54273df97d8326b103cMiao Wang                mByteBufferStride = mRS.nAllocationGetStride(getID(mRS));
56929f8636ee81c93588204e54273df97d8326b103cMiao Wang            } else {
57029f8636ee81c93588204e54273df97d8326b103cMiao Wang                mByteBufferStride = mType.getX() * mType.getElement().getBytesSize();
57129f8636ee81c93588204e54273df97d8326b103cMiao Wang            }
57229f8636ee81c93588204e54273df97d8326b103cMiao Wang        }
57329f8636ee81c93588204e54273df97d8326b103cMiao Wang        return mByteBufferStride;
57429f8636ee81c93588204e54273df97d8326b103cMiao Wang    }
57529f8636ee81c93588204e54273df97d8326b103cMiao Wang
57629f8636ee81c93588204e54273df97d8326b103cMiao Wang    /**
577032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Receive the latest input into the Allocation. This operation
578032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
5799c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
5809c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
5819c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public void ioReceive() {
5829c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((mUsage & USAGE_IO_INPUT) == 0) {
5839c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            throw new RSIllegalArgumentException(
5849c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                "Can only receive if IO_INPUT usage specified.");
5859c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
5869c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.validate();
5879c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.nAllocationIoReceive(getID(mRS));
5889c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
58945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
59045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
591032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array of RS objects to the Allocation.
59245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
59345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d Source array.
59445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
59545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(BaseObj[] d) {
59645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
59745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsObject();
59845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (d.length != mCurrentCount) {
59945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
60045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
60145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
602bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang
603bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        if (RenderScript.sPointerSize == 8) {
604bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            long i[] = new long[d.length * 4];
605bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            for (int ct=0; ct < d.length; ct++) {
606bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang                i[ct * 4] = d[ct].getID(mRS);
607bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            }
608bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            copy1DRangeFromUnchecked(0, mCurrentCount, i);
609bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        } else {
610bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            int i[] = new int[d.length];
611bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            for (int ct=0; ct < d.length; ct++) {
612bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang                i[ct] = (int)d[ct].getID(mRS);
613bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            }
614bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            copy1DRangeFromUnchecked(0, mCurrentCount, i);
61545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
61645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
61745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
61845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateBitmapFormat(Bitmap b) {
61945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Bitmap.Config bc = b.getConfig();
620c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (bc == null) {
621c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
622c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
62345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        switch (bc) {
62445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case ALPHA_8:
62545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
62645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
62745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
62845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
62945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
63045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
63145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
63245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
63345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case ARGB_8888:
63445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
63545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                (mType.getElement().getBytesSize() != 4)) {
63645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
63745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
63845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
63945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
64045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
64145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
64245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
64345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case RGB_565:
64445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
64545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                (mType.getElement().getBytesSize() != 2)) {
64645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
64745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
64845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
64945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
65045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
65145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
65245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
65345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case ARGB_4444:
65445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
65545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                (mType.getElement().getBytesSize() != 2)) {
65645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
65745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
65845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
65945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
66045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
66145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
66245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
663302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
66445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
66545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
66645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
66745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateBitmapSize(Bitmap b) {
66845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
66945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
67045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
67145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
67245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
6738352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
6748352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        mRS.validate();
6758352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (mCurrentDimZ > 0) {
6768352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
6778352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        } else if (mCurrentDimY > 0) {
6788352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
6798352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        } else {
6808352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
6818352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
6828352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
6838352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
6848352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    /**
6858352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * Copy into this Allocation from an array. This method does not guarantee
6868352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * that the Allocation is compatible with the input buffer; it copies memory
6878352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * without reinterpretation.
6888352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     *
689e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
690e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
691e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
692e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
693e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
694e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
695e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
696e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
697e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
698e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
699e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
700e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
701e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
702e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
703e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param array The source array
7048352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     */
7058352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copyFromUnchecked(Object array) {
7068352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
7078352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                          java.lang.reflect.Array.getLength(array));
7088352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
7098352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
71045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
711032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
712032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
713032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
71445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
715e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
716e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
717e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
718e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
719e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
720e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
721e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
722e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
723e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
724e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
725e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
726e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
727e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
728e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
729e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
73045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
73145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(int[] d) {
7328352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
73345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
7348352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
73545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
736032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
737032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
738032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
73945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
740e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
741e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
742e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
743e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
744e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
745e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
746e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
747e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
748e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
749e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
750e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
751e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
752e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
753e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
754e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
75545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
75645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(short[] d) {
7578352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
75845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
7598352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
76045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
761032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
762032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
763032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
76445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
765e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
766e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
767e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
768e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
769e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
770e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
771e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
772e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
773e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
774e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
775e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
776e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
777e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
778e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
779e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
78045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
78145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(byte[] d) {
7828352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
78345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
7848352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
78545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
786032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
787032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
788032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
78945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
790e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
791e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
792e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
793e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
794e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
795e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
796e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
797e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
798e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
799e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
800e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
801e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
802e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
803e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
804e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
80545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
80645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(float[] d) {
8078352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
8088352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
8098352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
8108352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
8118352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    /**
8128352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * Copy into this Allocation from an array.  This variant is type checked
8138352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * and will generate exceptions if the Allocation's {@link
814e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} does not match the array's
8158352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * primitive type.
8168352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     *
817e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
818e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
819e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
820e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
821e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
822e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
823e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
824e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
825e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
826e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
827e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
828e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
829e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
830e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
831e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param array The source array
8328352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     */
8338352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copyFrom(Object array) {
8348352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
8358352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                          java.lang.reflect.Array.getLength(array));
83645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
83745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
83845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
839032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
840032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
841e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
842e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
843e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
844e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
845e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
846e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
847e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
848e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
849e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
850e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
851e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
85245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
853e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
854e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
855e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
856e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
857e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
858e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
85945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
86045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(int[] d) {
8618352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt32();
8628352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
86345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
86445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
86545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
866032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
867032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
868e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
869e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
870e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
871e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
872e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
873e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
874e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
875e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
876e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
877e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
878e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
879e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
880e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
881e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
882e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
883e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
88445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
885e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
88645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
88745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(short[] d) {
8888352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt16();
8898352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
89045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
89145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
89245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
893032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
894032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
895e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
896e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
897e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
898e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
899e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
900e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
901e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
902e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
903e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
904e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
905e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
906e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
907e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
908e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
909e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
910e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
91145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
912e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
91345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
91445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(byte[] d) {
9158352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt8();
9168352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
91745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
91845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
91945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
920032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
921032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
922e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
923e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
92445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
925e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
926e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
927e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
928e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
929e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
930e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
931e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
932e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must be part of the array.
933e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
934e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
935e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
936e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
937e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
938e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
939e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
94045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
94145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(float[] d) {
9428352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsFloat32();
9438352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
94445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
94545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
94645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
947032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into an Allocation from a {@link android.graphics.Bitmap}.  The
948032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * height, width, and format of the bitmap must match the existing
949032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * allocation.
950032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
951032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
952032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.Bitmap} used to create the Allocation with {@link
953032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
954032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * this will synchronize the Allocation with the latest data from the {@link
955032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
95645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
95745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b the source bitmap
95845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
95945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(Bitmap b) {
96045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
961c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (b.getConfig() == null) {
962c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
963c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Canvas c = new Canvas(newBitmap);
964c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            c.drawBitmap(b, 0, 0, null);
965c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            copyFrom(newBitmap);
966c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            return;
967c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
96845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapSize(b);
96945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapFormat(b);
97045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationCopyFromBitmap(getID(mRS), b);
97145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
97245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
97345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
974032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an Allocation from an Allocation.  The types of both allocations
9758fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     * must be identical.
9768fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     *
9778fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     * @param a the source allocation
9788fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     */
9798fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    public void copyFrom(Allocation a) {
9808fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        mRS.validate();
9818fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        if (!mType.equals(a.getType())) {
9828fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams            throw new RSIllegalArgumentException("Types of allocations must match.");
9838fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        }
9848fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
9858fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    }
9868fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams
9878fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams
9888fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    /**
989032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * This is only intended to be used by auto-generated code reflected from
990032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * the RenderScript script files and should not be used by developers.
99145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
99245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xoff
99345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param fp
99445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
99545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
99645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
99745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int eSize = mType.mElement.getBytesSize();
99845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        final byte[] data = fp.getData();
999908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        int data_length = fp.getPos();
100045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1001908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        int count = data_length / eSize;
1002908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        if ((eSize * count) != data_length) {
1003908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines            throw new RSIllegalArgumentException("Field packer length " + data_length +
100445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                               " not divisible by element size " + eSize + ".");
100545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
100645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
100745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
100845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
100945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1010032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * This is only intended to be used by auto-generated code reflected from
1011032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * the RenderScript script files.
101245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
101345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xoff
101445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param component_number
101545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param fp
101645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
101745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
101845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
101945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (component_number >= mType.mElement.mElements.length) {
102045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
102145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
102245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(xoff < 0) {
102345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
102445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
102545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
102645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        final byte[] data = fp.getData();
1027908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        int data_length = fp.getPos();
102845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int eSize = mType.mElement.mElements[component_number].getBytesSize();
102945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        eSize *= mType.mElement.mArraySizes[component_number];
103045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1031908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        if (data_length != eSize) {
1032908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
103345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                               " does not match component size " + eSize + ".");
103445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
103545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
103645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
1037908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines                                     component_number, data, data_length);
103845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
103945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1040244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1041244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1042244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * This is only intended to be used by auto-generated code reflected from
1043244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * the RenderScript script files.
1044244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1045244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff
1046244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff
1047244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param zoff
1048244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param component_number
1049244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param fp
1050244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1051244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /*
1052244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
1053244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.validate();
1054244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if (component_number >= mType.mElement.mElements.length) {
1055244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1056244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1057244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(xoff < 0) {
1058244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset x must be >= 0.");
1059244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1060244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(yoff < 0) {
1061244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset y must be >= 0.");
1062244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1063244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(zoff < 0) {
1064244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset z must be >= 0.");
1065244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1066244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1067244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        final byte[] data = fp.getData();
1068244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        int data_length = fp.getPos();
1069244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        int eSize = mType.mElement.mElements[component_number].getBytesSize();
1070244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        eSize *= mType.mElement.mArraySizes[component_number];
1071244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1072244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if (data_length != eSize) {
1073244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
1074244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                               " does not match component size " + eSize + ".");
1075244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1076244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1077244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1078244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                   component_number, data, data_length);
1079244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1080244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    */
1081244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
10822e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
108345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
108445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(off < 0) {
108545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
108645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
108745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(count < 1) {
108845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
108945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
109045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if((off + count) > mCurrentCount) {
109145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
109245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                               ", got " + count + " at offset " + off + ".");
109345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
10942e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if(usePadding) {
10952e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if(len < dataSize / 4 * 3) {
10962e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
10972e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
10982e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
10992e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if(len < dataSize) {
11002e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
11012e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
110245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
110345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
110445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
110545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1106032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Generate a mipmap chain. This is only valid if the Type of the Allocation
1107032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * includes mipmaps.
110845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1109032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This function will generate a complete set of mipmaps from the top
1110032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * level LOD and place them into the script memory space.</p>
111145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1112032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>If the Allocation is also using other memory spaces, a call to {@link
1113032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
111445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
111545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void generateMipmaps() {
111645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationGenerateMipmaps(getID(mRS));
111745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
111845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
11198352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void copy1DRangeFromUnchecked(int off, int count, Object array,
11208352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                          Element.DataType dt, int arrayLen) {
11218352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        final int dataSize = mType.mElement.getBytesSize() * count;
11222e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
11232e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
11242e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
11252e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
11262e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
11272e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
11282e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
11292e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              mType.mElement.mType.mSize, usePadding);
11308352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
11318352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
113245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1133e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
1134032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
113545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1136e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1137e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1138e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1139e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1140e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1141e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1142e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1143e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1144e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1145e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1146e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1147e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1148e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1149e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
115045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
115145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
1152e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param array The source array
115345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
11548352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy1DRangeFromUnchecked(int off, int count, Object array) {
11558352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, array,
11568352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 validateObjectIsPrimitiveArray(array, false),
11578352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 java.lang.reflect.Array.getLength(array));
1158bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
11598352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
1160bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    /**
1161e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
1162bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
1163bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     *
1164e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1165e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1166e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1167e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1168e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1169e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1170e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1171e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1172e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1173e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1174e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1175e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1176e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1177e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1178bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param off The offset of the first element to be copied.
1179bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param count The number of elements to be copied.
1180e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
1181bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     */
118245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
11838352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
118445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
11858352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
118645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1187e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
1188032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
118945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1190e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1191e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1192e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1193e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1194e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1195e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1196e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1197e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1198e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1199e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1200e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1201e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1202e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1203e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
120445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
120545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
1206e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
120745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
120845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
12098352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
121045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
12118352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
121245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1213e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
1214032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
121545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1216e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1217e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1218e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1219e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1220e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1221e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1222e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1223e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1224e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1225e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1226e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1227e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1228e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1229e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
123045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
123145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
1232e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
123345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
123445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
12358352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
123645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
12378352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
123845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1239e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This method does not
1240032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
124145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1242e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1243e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1244e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1245e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1246e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1247e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1248e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1249e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1250e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1251e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1252e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1253e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1254e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1255e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
125645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
125745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
1258e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
125945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
126045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
12618352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
126245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
126345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
12648352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
126545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1266e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1267e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1268e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} does not match the component type
1269e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the array passed in.
1270e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1271e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1272e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1273e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1274e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1275e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1276e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1277e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1278e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1279e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1280e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1281e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1282e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1283e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
128445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
128545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
128645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
1287e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param array The source array.
128845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
12898352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy1DRangeFrom(int off, int count, Object array) {
12908352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, array,
12918352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 validateObjectIsPrimitiveArray(array, true),
12928352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 java.lang.reflect.Array.getLength(array));
1293bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
1294bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang
1295bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    /**
1296e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1297e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1298e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not an 32 bit integer nor a vector of 32 bit
1299e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1300e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1301e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1302e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1303e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1304e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1305e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1306e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1307e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1308e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1309e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1310e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1311e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1312e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1313e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1314bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     *
1315bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param off The offset of the first element to be copied.
1316bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param count The number of elements to be copied.
1317e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
1318bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     */
131945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
132045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt32();
13218352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
132245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
132345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
132445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1325e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1326e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1327e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not an 16 bit integer nor a vector of 16 bit
1328e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1329e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1330e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1331e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1332e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1333e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1334e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1335e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1336e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1337e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1338e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1339e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1340e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1341e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1342e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
134345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
134445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
134545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
1346e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
134745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
134845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
134945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt16();
13508352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
135145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
135245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
135345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1354e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1355e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1356e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
1357e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1358e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1359e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1360e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1361e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1362e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1363e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1364e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1365e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1366e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1367e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1368e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1369e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1370e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1371e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
137245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
137345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
137445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
1375e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
137645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
137745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
137845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt8();
13798352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
138045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
138145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
138245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1383e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy an array into a 1D region of this Allocation.  This variant is type checked
1384e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1385e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
1386e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
1387e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1388e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
1389e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1390e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1391e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1392e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1393e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1394e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1395e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1396e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1397e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1398e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1399e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1400e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
140145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
140245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
140345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
1404e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array.
140545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
140645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
140745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsFloat32();
14088352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
140945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
141045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
141145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     /**
1412032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy part of an Allocation into this Allocation.
141345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
141445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
141545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
141645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param data the source data allocation.
141745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param dataOff off The offset of the first element in data to
141845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *          be copied.
141945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
142045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
142145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
142245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              mSelectedLOD, mSelectedFace.mID,
142345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              count, 1, data.getID(mRS), dataOff, 0,
142445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
142545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
142645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
142745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
142845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mAdaptedAllocation != null) {
142945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
143045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        } else {
143145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
143245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (xoff < 0 || yoff < 0) {
143345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
143445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
143545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (h < 0 || w < 0) {
143645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
143745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
143845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
143945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
144045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
144145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
144245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
144345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
14448352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
14458352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                  Element.DataType dt, int arrayLen) {
144645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
144745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, w, h);
14482e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h;
14492e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
14502e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
14512e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        int sizeBytes = arrayLen * dt.mSize;
14522e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
14532e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
14542e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
14552e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
14562e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
14572e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            sizeBytes = dataSize;
14582e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
14592e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize > sizeBytes) {
14602e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
14612e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
14622e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
14638352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
14642e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              array, sizeBytes, dt,
14652e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              mType.mElement.mType.mSize, usePadding);
146645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
146745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1468dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    /**
1469032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1470e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array is assumed to be tightly packed. This variant is type checked
1471e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1472e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} does not match the input data type.
1473e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1474e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1475e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1476e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1477e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1478e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1479e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1480e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1481e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1482e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1483e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1484e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1485e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1486e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1487dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines     *
1488032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1489032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1490032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1491032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
14928352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * @param array Data to be placed into the Allocation
1493dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines     */
14948352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
14958352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
14968352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 validateObjectIsPrimitiveArray(array, true),
14978352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 java.lang.reflect.Array.getLength(array));
1498dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
1499dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
1500032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
1501032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1502e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array is assumed to be tightly packed. This variant is type checked
1503e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1504e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not an 8 bit integer nor a vector of 8 bit
1505e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1506e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1507e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1508e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1509e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1510e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1511e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1512e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1513e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1514e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1515e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1516e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1517e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1518e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1519e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1520032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1521032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1522032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1523032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1524032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
1525032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
1526032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
15278352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
15288352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt8();
15298352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
15308352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 Element.DataType.SIGNED_8, data.length);
1531dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
1532dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
1533032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
1534032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1535e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array is assumed to be tightly packed. This variant is type checked
1536e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1537e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
1538e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1539e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1540e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1541e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1542e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1543e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1544e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1545e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1546e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1547e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1548e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1549e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1550e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1551e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1552e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1553032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1554032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1555032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1556032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1557032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
1558032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
1559032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
15608352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
15618352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt16();
15628352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
15638352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 Element.DataType.SIGNED_16, data.length);
1564bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
1565bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang
1566bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    /**
1567bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * Copy from an array into a rectangular region in this Allocation.  The
1568e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array is assumed to be tightly packed. This variant is type checked
1569e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1570e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
1571e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1572e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1573e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1574e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1575e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1576e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1577e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1578e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1579e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1580e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1581e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1582e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1583e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1584e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1585e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1586bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     *
1587bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param xoff X offset of the region to update in this Allocation
1588bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param yoff Y offset of the region to update in this Allocation
1589bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param w Width of the region to update
1590bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param h Height of the region to update
1591bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param data to be placed into the Allocation
1592bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     */
1593dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
1594dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        validateIsInt32();
15958352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
15968352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 Element.DataType.SIGNED_32, data.length);
1597dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
1598dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
1599032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
1600032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1601e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array is assumed to be tightly packed. This variant is type checked
1602e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1603e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
1604e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
1605e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1606e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
1607e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1608e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1609e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1610e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1611e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1612e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1613e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1614e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1615e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1616e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1617e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1618e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1619032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1620032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1621032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1622032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1623032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
1624032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
1625032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
1626dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
1627dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        validateIsFloat32();
16288352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
16298352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 Element.DataType.FLOAT_32, data.length);
1630dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
1631dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
163245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1633032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy a rectangular region from an Allocation into a rectangular region in
1634032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * this Allocation.
163545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1636032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region in this Allocation
1637032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region in this Allocation
1638032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update.
1639032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update.
1640032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data source Allocation.
1641032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataXoff X offset in source Allocation
1642032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataYoff Y offset in source Allocation
164345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
164445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
164545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                Allocation data, int dataXoff, int dataYoff) {
164645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
164745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, w, h);
164845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
164945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              mSelectedLOD, mSelectedFace.mID,
165045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              w, h, data.getID(mRS), dataXoff, dataYoff,
165145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
165245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
165345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
165445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1655032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy a {@link android.graphics.Bitmap} into an Allocation.  The height
1656032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and width of the update will use the height and width of the {@link
1657032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.Bitmap}.
165845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1659032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1660032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1661032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data the Bitmap to be copied
166245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
166345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
166445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
1665c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (data.getConfig() == null) {
1666c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1667c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Canvas c = new Canvas(newBitmap);
1668c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            c.drawBitmap(data, 0, 0, null);
1669c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            copy2DRangeFrom(xoff, yoff, newBitmap);
1670302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            return;
1671c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
167245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapFormat(data);
167345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
167445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
167545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
167645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1677302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1678302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mAdaptedAllocation != null) {
1679302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1680302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else {
1681302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1682302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            if (xoff < 0 || yoff < 0 || zoff < 0) {
1683302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
1684302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            }
1685302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            if (h < 0 || w < 0 || d < 0) {
1686302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
1687302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            }
1688302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1689302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
1690302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            }
1691302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        }
1692302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1693302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1694e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang    /**
1695e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a rectangular region from the array into the allocation.
1696e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * The array is assumed to be tightly packed.
1697e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1698e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * The data type of the array is not required to be the same as
1699e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the element data type.
1700e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     */
17018352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
17028352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                          Object array, Element.DataType dt, int arrayLen) {
1703302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.validate();
1704302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
17052e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h * d;
17062e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
17072e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
17082e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        int sizeBytes = arrayLen * dt.mSize;
17092e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
17102e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
17112e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
17122e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
17132e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
17142e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            sizeBytes = dataSize;
17152e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
17162e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize > sizeBytes) {
17172e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
17182e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
17192e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
17208352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
17212e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              array, sizeBytes, dt,
17222e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              mType.mElement.mType.mSize, usePadding);
1723302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1724302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1725302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1726e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from an array into a 3D region in this Allocation.  The
1727e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array is assumed to be tightly packed. This variant is type checked
1728e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1729e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} does not match the input data type.
1730e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1731e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * d * {@link #getElement}.{@link
1732e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
1733e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1734e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1735e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
1736e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1737e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1738e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1739e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
1740e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1741e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1742e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1743e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
1744032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1745032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1746032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1747032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param zoff Z offset of the region to update in this Allocation
1748032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1749032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
1750032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param d Depth of the region to update
17514e9c0ef208a45fea383115e40b023b0642673d08Ying Wang     * @param array to be placed into the allocation
1752302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
17538352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
17548352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
17558352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 validateObjectIsPrimitiveArray(array, true),
17568352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 java.lang.reflect.Array.getLength(array));
1757302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1758302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1759302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1760302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * Copy a rectangular region into the allocation from another
1761302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * allocation.
1762302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1763032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1764032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1765032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param zoff Z offset of the region to update in this Allocation
1766032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update.
1767032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update.
1768032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param d Depth of the region to update.
1769302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @param data source allocation.
1770032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataXoff X offset of the region in the source Allocation
1771032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataYoff Y offset of the region in the source Allocation
1772032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataZoff Z offset of the region in the source Allocation
1773302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1774302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1775302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                                Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1776302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.validate();
1777302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
1778302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1779302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1780302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              data.mSelectedLOD);
1781302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1782302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1783302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1784302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1785032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a {@link android.graphics.Bitmap}.  The
1786032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * bitmap must match the dimensions of the Allocation.
178745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
178845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b The bitmap to be set from the Allocation.
178945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
179045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(Bitmap b) {
179145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
179245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapFormat(b);
179345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapSize(b);
179445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationCopyToBitmap(getID(mRS), b);
179545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
179645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
17978352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void copyTo(Object array, Element.DataType dt, int arrayLen) {
17988352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        mRS.validate();
17992e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
18002e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
18012e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
18022e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
1803a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang        if (usePadding) {
1804a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang            if (dt.mSize * arrayLen < mSize / 4 * 3) {
1805a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang                throw new RSIllegalArgumentException(
1806a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang                    "Size of output array cannot be smaller than size of allocation.");
1807a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang            }
1808a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang        } else {
1809a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang            if (dt.mSize * arrayLen < mSize) {
1810a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang                throw new RSIllegalArgumentException(
1811a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang                    "Size of output array cannot be smaller than size of allocation.");
1812a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang            }
1813a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang        }
18142e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
18158352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
18168352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
18178352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    /**
1818e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from the Allocation into an array. The method is type checked
1819e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1820e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} does not match the input data type.
1821e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1822e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1823e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
1824e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
1825e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1826e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1827e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1828e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1829e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells will be part of the array.
1830e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1831e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1832e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1833e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1834e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
18358352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     *
18368352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * @param array The array to be set from the Allocation.
18378352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     */
18388352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copyTo(Object array) {
18398352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(array, validateObjectIsPrimitiveArray(array, true),
18408352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang               java.lang.reflect.Array.getLength(array));
18418352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
18428352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
184345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1844e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from the Allocation into a byte array. This variant is type checked
1845e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1846e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is neither an 8 bit integer nor a vector of 8 bit
1847e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1848e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1849e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1850e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
1851e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
1852e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1853e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1854e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1855e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1856e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells will be part of the array.
1857e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1858e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1859e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1860e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1861e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
186245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
186345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
186445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
186545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(byte[] d) {
186645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt8();
18678352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(d, Element.DataType.SIGNED_8, d.length);
186845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
186945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
187045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1871e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from the Allocation into a short array. This variant is type checked
1872e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1873e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not a 16 bit integer nor a vector of 16 bit
1874e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1875e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1876e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1877e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
1878e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
1879e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1880e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1881e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1882e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1883e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells will be part of the array.
1884e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1885e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1886e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1887e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1888e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
188945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
189045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
189145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
189245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(short[] d) {
189345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt16();
18948352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(d, Element.DataType.SIGNED_16, d.length);
1895bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
1896bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang
1897bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    /**
1898e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from the Allocation into a int array. This variant is type checked
1899e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1900e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is not a 32 bit integer nor a vector of 32 bit
1901e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
1902e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1903e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1904e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
1905e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
1906e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1907e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1908e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1909e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1910e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells will be part of the array.
1911e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1912e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1913e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1914e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1915e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
1916bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     *
1917bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param d The array to be set from the Allocation.
1918bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     */
191945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(int[] d) {
192045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt32();
19218352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(d, Element.DataType.SIGNED_32, d.length);
192245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
192345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
192445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1925e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from the Allocation into a float array. This variant is type checked
1926e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
1927e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
1928e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
1929e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1930e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
1931e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the Allocation {@link
1932e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * #getBytesSize getBytesSize()}.
1933e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1934e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1935e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
1936e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1937e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells will be part of the array.
1938e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
1939e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
1940e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
1941e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the Allocation {@link #getBytesSize getBytesSize()}. The padding bytes for
1942e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the cells must not be part of the array.
194345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
194445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
194545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
194645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(float[] d) {
194745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsFloat32();
19488352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(d, Element.DataType.FLOAT_32, d.length);
194945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
195045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1951244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1952244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
19533bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang     * This is only intended to be used by auto-generated code reflected from
19543bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang     * the RenderScript script files and should not be used by developers.
1955244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1956244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff
1957244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff
1958244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param zoff
1959244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param component_number
1960e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param fp
1961244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1962244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /*
19633bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang    public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
1964244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.validate();
1965244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if (component_number >= mType.mElement.mElements.length) {
1966244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1967244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1968244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(xoff < 0) {
1969244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset x must be >= 0.");
1970244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1971244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(yoff < 0) {
1972244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset y must be >= 0.");
1973244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1974244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(zoff < 0) {
1975244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset z must be >= 0.");
1976244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1977244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
19783bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang        final byte[] data = fp.getData();
1979e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang        int data_length = data.length;
1980244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        int eSize = mType.mElement.mElements[component_number].getBytesSize();
1981244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        eSize *= mType.mElement.mArraySizes[component_number];
1982244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
19833bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang        if (data_length != eSize) {
19843bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
19853bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang                                               " does not match component size " + eSize + ".");
1986244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1987244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1988244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
19893bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang                                   component_number, data, data_length);
1990244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1991244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    */
1992244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1993244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    private void copy1DRangeToUnchecked(int off, int count, Object array,
1994244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                        Element.DataType dt, int arrayLen) {
1995244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        final int dataSize = mType.mElement.getBytesSize() * count;
19962e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
19972e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
19982e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
19992e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
20002e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
20012e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
20022e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
20032e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              mType.mElement.mType.mSize, usePadding);
2004244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2005244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2006244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2007e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
2008244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
2009244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2010e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2011e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2012e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2013e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2014e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2015e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2016e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2017e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2018e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2019e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2020e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2021e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2022e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2023e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2024244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2025244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2026e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param array The dest array
2027244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2028244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, Object array) {
2029244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, array,
2030244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               validateObjectIsPrimitiveArray(array, false),
2031244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               java.lang.reflect.Array.getLength(array));
2032244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2033244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2034244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2035e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
2036244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
2037244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2038e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2039e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2040e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2041e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2042e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2043e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2044e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2045e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2046e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2047e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2048e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2049e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2050e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2051e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2052244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2053244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2054e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
2055244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2056244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, int[] d) {
2057244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
2058244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2059244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2060244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2061e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
2062244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
2063244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2064e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2065e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2066e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2067e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2068e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2069e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2070e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2071e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2072e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2073e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2074e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2075e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2076e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2077e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2078244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2079244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2080e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
2081244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2082244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, short[] d) {
2083244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
2084244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2085244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2086244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2087e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
2088244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
2089244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2090e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2091e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2092e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2093e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2094e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2095e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2096e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2097e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2098e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2099e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2100e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2101e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2102e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2103e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2104244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2105244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2106e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
2107244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2108244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
2109244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
2110244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2111244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2112244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2113e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array.  This method does not
2114244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
2115244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2116e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2117e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2118e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2119e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2120e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2121e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2122e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2123e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2124e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2125e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2126e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2127e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2128e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2129e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2130244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2131244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2132e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
2133244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2134244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, float[] d) {
2135244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
2136244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2137244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2138244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2139244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2140e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array.  This method is type checked
2141e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
2142e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} does not match the component type
2143e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the array passed in.
2144e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2145e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2146e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2147e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2148e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2149e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2150e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2151e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2152e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2153e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2154e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2155e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2156e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2157e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2158244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2159244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2160244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2161e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param array The source array.
2162244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2163244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, Object array) {
2164244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, array,
2165244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               validateObjectIsPrimitiveArray(array, true),
2166244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               java.lang.reflect.Array.getLength(array));
2167244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2168244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2169244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2170e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array. This variant is type checked
2171e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
2172e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit integer nor a vector of 32 bit
2173e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
2174e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2175e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2176e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2177e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2178e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2179e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2180e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2181e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2182e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2183e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2184e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2185e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2186e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2187e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2188244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2189244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2190244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2191e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
2192244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2193244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, int[] d) {
2194244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt32();
2195244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
2196244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2197244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2198244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2199e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array. This variant is type checked
2200e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
2201e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is neither a 16 bit integer nor a vector of 16 bit
2202e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
2203e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2204e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2205e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2206e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2207e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2208e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2209e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2210e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2211e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2212e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2213e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2214e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2215e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2216e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2217244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2218244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2219244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2220e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
2221244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2222244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, short[] d) {
2223244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt16();
2224244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
2225244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2226244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2227244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2228e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array. This variant is type checked
2229e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
2230e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is neither an 8 bit integer nor a vector of 8 bit
2231e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * integers {@link android.support.v8.renderscript.Element.DataType}.
2232e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2233e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2234e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2235e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2236e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2237e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2238e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2239e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2240e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2241e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2242e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2243e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2244e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2245e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2246244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2247244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2248244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2249e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array
2250244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2251244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, byte[] d) {
2252244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt8();
2253244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
2254244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2255244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2256244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2257e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy a 1D region of this Allocation into an array. This variant is type checked
2258e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * and will generate exceptions if the Allocation's {@link
2259e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector of
2260e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
2261e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2262e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: count * {@link #getElement}.{@link
2263e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2264e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2265e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2266e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2267e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2268e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2269e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2270e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2271e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2272e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2273e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2274e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2275244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2276244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
2277244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
2278e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * @param d the source array.
2279244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2280244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, float[] d) {
2281244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsFloat32();
2282244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
2283244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2284244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2285244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2286244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
2287244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                Element.DataType dt, int arrayLen) {
2288244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.validate();
2289244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validate2DRange(xoff, yoff, w, h);
22902e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h;
22912e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
22922e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
22932e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        int sizeBytes = arrayLen * dt.mSize;
22942e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
22952e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
22962e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
22972e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
22982e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
22992e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            sizeBytes = dataSize;
23002e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
23012e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize > sizeBytes) {
23022e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
23032e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
23042e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
2305244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
23062e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
2307244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2308244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2309244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2310e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from a rectangular region in this Allocation into an array. This
2311e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * method is type checked and will generate exceptions if the Allocation's
2312e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * {@link android.support.v8.renderscript.Element} does not match the component type
2313e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the array passed in.
2314e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2315e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2316e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2317e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2318e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2319e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2320e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2321e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2322e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2323e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2324e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2325e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2326e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2327e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2328244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2329244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
2330244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
2331244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
2332244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
2333244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array Dest Array to be copied into
2334244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2335244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
2336244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, array,
2337244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               validateObjectIsPrimitiveArray(array, true),
2338244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               java.lang.reflect.Array.getLength(array));
2339244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2340244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2341244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2342e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from a rectangular region in this Allocation into an array. This
2343e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * variant is type checked and will generate exceptions if the Allocation's
2344e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * {@link android.support.v8.renderscript.Element} is neither an 8 bit integer nor a vector
2345e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of 8 bit integers {@link android.support.v8.renderscript.Element.DataType}.
2346e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2347e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2348e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2349e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2350e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2351e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2352e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2353e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2354e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2355e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2356e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2357e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2358e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2359e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2360244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2361244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
2362244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
2363244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
2364244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
23654e9c0ef208a45fea383115e40b023b0642673d08Ying Wang     * @param data Dest Array to be copied into
2366244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2367244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
2368244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt8();
2369244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2370244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               Element.DataType.SIGNED_8, data.length);
2371244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2372244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2373244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2374e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from a rectangular region in this Allocation into an array. This
2375e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * variant is type checked and will generate exceptions if the Allocation's
2376e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * {@link android.support.v8.renderscript.Element} is neither a 16 bit integer nor a vector
2377e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of 16 bit integers {@link android.support.v8.renderscript.Element.DataType}.
2378e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2379e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2380e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2381e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2382e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2383e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2384e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2385e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2386e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2387e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2388e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2389e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2390e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2391e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2392244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2393244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
2394244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
2395244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
2396244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
23974e9c0ef208a45fea383115e40b023b0642673d08Ying Wang     * @param data Dest Array to be copied into
2398244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2399244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
2400244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt16();
2401244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2402244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               Element.DataType.SIGNED_16, data.length);
2403244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2404244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2405244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2406e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from a rectangular region in this Allocation into an array. This
2407e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * variant is type checked and will generate exceptions if the Allocation's
2408e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * {@link android.support.v8.renderscript.Element} is neither a 32 bit integer nor a vector
2409e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of 32 bit integers {@link android.support.v8.renderscript.Element.DataType}.
2410e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2411e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2412e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2413e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2414e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2415e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2416e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2417e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2418e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2419e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2420e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2421e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2422e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2423e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2424244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2425244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
2426244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
2427244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
2428244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
24294e9c0ef208a45fea383115e40b023b0642673d08Ying Wang     * @param data Dest Array to be copied into
2430244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2431244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
2432244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt32();
2433244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2434244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               Element.DataType.SIGNED_32, data.length);
2435244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2436244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2437244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2438e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from a rectangular region in this Allocation into an array. This
2439e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * variant is type checked and will generate exceptions if the Allocation's
2440e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * {@link android.support.v8.renderscript.Element} is neither a 32 bit float nor a vector
2441e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of 32 bit floats {@link android.support.v8.renderscript.Element.DataType}.
2442e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2443e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * {@link #getElement}.{@link
2444e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2445e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2446e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2447e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2448e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2449e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2450e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2451e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2452e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2453e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2454e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2455e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2456244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2457244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
2458244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
2459244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
2460244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
24614e9c0ef208a45fea383115e40b023b0642673d08Ying Wang     * @param data Dest Array to be copied into
2462244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2463244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
2464244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsFloat32();
2465244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
2466244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               Element.DataType.FLOAT_32, data.length);
2467244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2468244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2469244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2470244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2471e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from a 3D region in this Allocation into an array. This method does
2472e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * not guarantee that the Allocation is compatible with the input buffer.
2473e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * The array is assumed to be tightly packed.
2474244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2475e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * The data type of the array is not required to be the same as
2476e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * the element data type.
2477244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2478244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /*
2479244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
2480244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                        Object array, Element.DataType dt, int arrayLen) {
2481244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.validate();
2482244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validate3DRange(xoff, yoff, zoff, w, h, d);
24832e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h * d;
24842e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
24852e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
24862e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        int sizeBytes = arrayLen * dt.mSize;
24872e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
24882e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
24892e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
24902e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
24912e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
24922e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            sizeBytes = dataSize;
24932e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
24942e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize > sizeBytes) {
24952e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
24962e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
24972e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
2498244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
24992e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
2500244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2501244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    */
2502244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
2503244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
2504244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
2505e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Copy from a 3D region in this Allocation into an array. This
2506e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * method is type checked and will generate exceptions if the Allocation's
2507e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * {@link android.support.v8.renderscript.Element} does not match the component type
2508e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the array passed in.
2509e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2510e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> The size of the region is: w * h * d * {@link #getElement}.{@link
2511e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * Element#getBytesSize}.
2512e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2513e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation does not have Vec3 Elements, then the size of the
2514e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * array in bytes must be at least the size of the region.
2515e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2516e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2517e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is disabled, then the size of the array in bytes must be at least the size
2518e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must be part of the array.
2519e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     *
2520e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * <p> If the Allocation has Vec3 Elements and {@link #setAutoPadding AutoPadding}
2521e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * is enabled, then the size of the array in bytes must be at least 3/4 the size
2522e12a2cab5f472e047ffcdde5a832b03347f92bf8Miao Wang     * of the region. The padding bytes for the cells must not be part of the array.
2523244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
2524244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
2525244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
2526244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param zoff Z offset of the region to copy in this Allocation
2527244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
2528244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
2529244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d Depth of the region to copy
2530244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array Dest Array to be copied into
2531244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
2532244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /*
2533244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
2534244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
2535244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                 validateObjectIsPrimitiveArray(array, true),
2536244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                 java.lang.reflect.Array.getLength(array));
2537244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
2538244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    */
2539244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
254045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    // creation
254145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
254245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
254345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static {
254445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mBitmapOptions.inScaled = false;
254545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
254645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
254745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2548032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a new Allocation with the given {@link
25492192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Type}, mipmap flag, and usage flags.
255045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
2551032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param type RenderScript type describing data layout
255245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the
255345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *             allocation
2554032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param usage bit field specifying how the Allocation is
255545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
255645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
255745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
255845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
255945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (type.getID(rs) == 0) {
256045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSInvalidStateException("Bad Type");
256145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
2562dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang
2563dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        if(!rs.usingIO() && (usage & (USAGE_IO_INPUT | USAGE_IO_INPUT)) != 0) {
2564dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang            throw new RSRuntimeException("USAGE_IO not supported, Allocation creation failed.");
2565dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        }
2566dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang
2567bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
256845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (id == 0) {
256945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
257045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
257145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, type, usage);
257245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
257345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
257445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2575032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation with the size specified by the type and no mipmaps
2576032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * generated by default
257745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
257845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
257945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param type renderscript type describing data layout
258045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the allocation is
258145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
258245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
258345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
258445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
258545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
258645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
258745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
258845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
258945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2590032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation for use by scripts with a given {@link
25912192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Type} and no mipmaps
259245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
2593032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param rs Context to which the Allocation will belong.
2594032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param type RenderScript Type describing data layout
259545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
259645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
259745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
259845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
259945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
260045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
260145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
260245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2603032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation with a specified number of given elements
260445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
2605032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param rs Context to which the Allocation will belong.
2606032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param e Element to use in the Allocation
2607032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param count the number of Elements in the Allocation
2608032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param usage bit field specifying how the Allocation is
260945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
261045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
261145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
261245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
261345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createSized(RenderScript rs, Element e,
261445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                         int count, int usage) {
261545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
261645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder b = new Type.Builder(rs, e);
261745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        b.setX(count);
261845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = b.create();
261945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
2620bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
262145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (id == 0) {
262245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
262345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
262445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, t, usage);
262545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
262645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
262745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2628032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation with a specified number of given elements
262945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
2630032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param rs Context to which the Allocation will belong.
2631032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param e Element to use in the Allocation
2632032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param count the number of Elements in the Allocation
263345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
263445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
263545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
263645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
263745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
263845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
263945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
264045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
264145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        final Bitmap.Config bc = b.getConfig();
264245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
264345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.A_8(rs);
264445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
264545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
264645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.RGBA_4444(rs);
264745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
264845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
264945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.RGBA_8888(rs);
265045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
265145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.RGB_565) {
265245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.RGB_565(rs);
265345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
265445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSInvalidStateException("Bad bitmap type: " + bc);
265545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
265645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
265745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
265845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                       MipmapControl mip) {
265945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Element e = elementFromBitmap(rs, b);
266045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
266145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setX(b.getWidth());
266245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setY(b.getHeight());
266345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
266445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return tb.create();
266545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
266645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
266745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2668032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
266945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
267045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
2671032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param b Bitmap source for the allocation data
267245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the
267345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *             allocation
267445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the allocation is
267545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
267645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
2677032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing bitmap data
267845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
267945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
268045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
268145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                              MipmapControl mips,
268245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                              int usage) {
268345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
2684c806aeedb20cb1a725092657ce3358eb4008222bTim Murray
2685c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        // WAR undocumented color formats
2686c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (b.getConfig() == null) {
2687c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            if ((usage & USAGE_SHARED) != 0) {
2688c806aeedb20cb1a725092657ce3358eb4008222bTim Murray                throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
2689c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            }
2690c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
2691c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Canvas c = new Canvas(newBitmap);
2692c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            c.drawBitmap(b, 0, 0, null);
2693c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            return createFromBitmap(rs, newBitmap, mips, usage);
2694c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
2695c806aeedb20cb1a725092657ce3358eb4008222bTim Murray
269645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = typeFromBitmap(rs, b, mips);
269745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
26989c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        // enable optimized bitmap path only with no mipmap and script-only usage
26999c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if (mips == MipmapControl.MIPMAP_NONE &&
27009c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
2701257cecb5e24c9af71ba4d621887e88e46b0ac411Stephen Hines            usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
2702bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
27039c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            if (id == 0) {
27049c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                throw new RSRuntimeException("Load failed.");
27059c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            }
27069c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
27079c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            // keep a reference to the Bitmap around to prevent GC
27089c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            Allocation alloc = new Allocation(id, rs, t, usage);
27099c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            alloc.setBitmap(b);
27109c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            return alloc;
27119c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
27129c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
27139c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
2714bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
271545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (id == 0) {
271645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Load failed.");
271745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
271845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, t, usage);
271945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
272045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
272145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2722dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     * Associate a {@link android.view.Surface} with this Allocation. This
2723dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
2724dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     *
2725dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     * @param sur Surface to associate with allocation
2726dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     */
2727dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    public void setSurface(Surface sur) {
2728dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        mRS.validate();
2729dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
2730dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang            throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
2731dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        }
27328352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
2733dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        mRS.nAllocationSetSurface(getID(mRS), sur);
2734dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    }
2735dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang
2736dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    /**
2737032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
27389c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
2739032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This Allocation will be created with {@link #USAGE_SHARED}, and
2740032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * {@link #USAGE_SCRIPT}.</p>
274145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
274245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
274345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b bitmap source for the allocation data
274445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
2745032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing bitmap data
274645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
274745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
274845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
274945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
2750257cecb5e24c9af71ba4d621887e88e46b0ac411Stephen Hines                                USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
275145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
275245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
275345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2754032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
2755032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * containing the horizontal list of cube faces. Each face must be a square,
2756032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * have the same size as all other faces, and have a width that is a power
2757032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * of 2.
275845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
275945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
2760032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param b Bitmap with cubemap faces layed out in the following
276145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *          format: right, left, top, bottom, front, back
276245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
276345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the cubemap is utilized
276445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
276545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
276645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
276745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
276845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
276945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     MipmapControl mips,
277045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     int usage) {
277145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
277245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
277345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int height = b.getHeight();
277445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int width = b.getWidth();
277545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
277645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (width % 6 != 0) {
277745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
277845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
277945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (width / 6 != height) {
278045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
278145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
278245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
278345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (!isPow2) {
278445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
278545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
278645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
278745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Element e = elementFromBitmap(rs, b);
278845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
278945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setX(height);
279045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setY(height);
279145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setFaces(true);
279245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
279345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = tb.create();
279445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
2795bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
279645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(id == 0) {
279745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
279845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
279945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, t, usage);
280045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
280145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
280245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2803032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
2804032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * from a {@link android.graphics.Bitmap} containing the horizontal list of
2805032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * cube faces. Each face must be a square, have the same size as all other
2806032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * faces, and have a width that is a power of 2.
280745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
280845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
280945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b bitmap with cubemap faces layed out in the following
281045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *          format: right, left, top, bottom, front, back
281145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
281245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
281345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
281445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
281545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs,
281645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     Bitmap b) {
281745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
281845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                       USAGE_GRAPHICS_TEXTURE);
281945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
282045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
282145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2822032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
2823032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * objects containing the cube faces. Each face must be a square, have the
2824032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * same size as all other faces, and have a width that is a power of 2.
282545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
282645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
282745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xpos cubemap face in the positive x direction
282845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xneg cubemap face in the negative x direction
282945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param ypos cubemap face in the positive y direction
283045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param yneg cubemap face in the negative y direction
283145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zpos cubemap face in the positive z direction
283245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zneg cubemap face in the negative z direction
283345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
283445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the cubemap is utilized
283545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
283645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
283745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
283845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
283945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
284045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xpos,
284145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xneg,
284245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap ypos,
284345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap yneg,
284445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zpos,
284545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zneg,
284645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        MipmapControl mips,
284745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        int usage) {
2848baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        /*
284945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int height = xpos.getHeight();
285045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (xpos.getWidth() != height ||
285145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            xneg.getWidth() != height || xneg.getHeight() != height ||
285245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            ypos.getWidth() != height || ypos.getHeight() != height ||
285345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            yneg.getWidth() != height || yneg.getHeight() != height ||
285445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            zpos.getWidth() != height || zpos.getHeight() != height ||
285545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            zneg.getWidth() != height || zneg.getHeight() != height) {
285645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
285745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
285845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
285945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (!isPow2) {
286045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
286145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
286245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
286345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Element e = elementFromBitmap(rs, xpos);
286445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
286545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setX(height);
286645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setY(height);
286745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setFaces(true);
286845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
286945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = tb.create();
287045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
287145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
287245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
287345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_X);
287445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(xpos);
287545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
287645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(xneg);
287745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
287845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(ypos);
287945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
288045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(yneg);
288145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
288245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(zpos);
288345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
288445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(zneg);
288545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
288645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return cubemap;
2887baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        */
2888baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        return null;
288945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
289045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
289145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2892032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a sampler input
2893032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * from 6 {@link android.graphics.Bitmap} objects containing the cube
2894032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * faces. Each face must be a square, have the same size as all other faces,
2895032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and have a width that is a power of 2.
289645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
289745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
289845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xpos cubemap face in the positive x direction
289945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xneg cubemap face in the negative x direction
290045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param ypos cubemap face in the positive y direction
290145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param yneg cubemap face in the negative y direction
290245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zpos cubemap face in the positive z direction
290345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zneg cubemap face in the negative z direction
290445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
290545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
290645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
290745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
290845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
290945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xpos,
291045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xneg,
291145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap ypos,
291245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap yneg,
291345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zpos,
291445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zneg) {
291545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
291645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
291745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                          USAGE_GRAPHICS_TEXTURE);
291845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
291945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
29209c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
2921032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation from the Bitmap referenced
2922032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * by resource ID.
29239c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
29249c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param rs Context to which the allocation will belong.
29259c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param res application resources
29269c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param id resource id to load the data from
29279c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param mips specifies desired mipmap behaviour for the
29289c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *             allocation
29299c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param usage bit field specifying how the allocation is
29309c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *              utilized
29319c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
2932032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing resource data
29339c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
29349c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
29359c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
29369c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      Resources res,
29379c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      int id,
29389c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      MipmapControl mips,
29399c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      int usage) {
294045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
29419c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        rs.validate();
2942032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines        if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
2943032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines            throw new RSIllegalArgumentException("Unsupported usage specified.");
2944032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines        }
29459c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        Bitmap b = BitmapFactory.decodeResource(res, id);
29469c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        Allocation alloc = createFromBitmap(rs, b, mips, usage);
29479c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        b.recycle();
29489c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        return alloc;
29499c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
29509c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
29519c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
2952032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a non-mipmapped Allocation to use as a graphics texture from the
2953032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * {@link android.graphics.Bitmap} referenced by resource ID.
2954032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
2955032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This allocation will be created with {@link #USAGE_SCRIPT} and
2956032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
29579c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
29589c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param rs Context to which the allocation will belong.
29599c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param res application resources
29609c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param id resource id to load the data from
29619c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
2962032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing resource data
29639c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
29649c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
29659c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
29669c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      Resources res,
29679c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      int id) {
29689c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        return createFromBitmapResource(rs, res, id,
29699c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                        MipmapControl.MIPMAP_NONE,
2970032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines                                        USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
29719c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
29729c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
29739c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
2974032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation containing string data encoded in UTF-8 format.
29759c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
29769c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param rs Context to which the allocation will belong.
29779c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param str string to create the allocation from
29789c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param usage bit field specifying how the allocaiton is
29799c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *              utilized
29809c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
29819c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
29829c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    static public Allocation createFromString(RenderScript rs,
29839c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                              String str,
29849c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                              int usage) {
29859c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        rs.validate();
29869c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        byte[] allocArray = null;
29879c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        try {
29889c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            allocArray = str.getBytes("UTF-8");
29899c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
29909c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            alloc.copyFrom(allocArray);
29919c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            return alloc;
29929c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
29939c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        catch (Exception e) {
29949c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            throw new RSRuntimeException("Could not convert string to utf-8.");
29959c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
29969c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
29976a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang
29986a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang    /**
29996f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     * Frees any native resources associated with this object.  The
30006f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     * primary use is to force immediate cleanup of resources when it is
30016f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     * believed the GC will not respond quickly enough.
30026a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang     * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
30036a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang     */
30046a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang    @Override
30056a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang    public void destroy() {
30066f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        if (mIncCompatAllocation != 0) {
30076f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            boolean shouldDestroy = false;
30086f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            synchronized(this) {
30096f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                if (!mIncAllocDestroyed) {
30106f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                    shouldDestroy = true;
30116f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                    mIncAllocDestroyed = true;
30126f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                }
30136f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            }
30146f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang
30156f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            if (shouldDestroy) {
30166f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                // must include nObjDestroy in the critical section
30176f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
30186f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                rlock.lock();
30196f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                if(mRS.isAlive()) {
30206f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                    mRS.nIncObjDestroy(mIncCompatAllocation);
30216f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                }
30226f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                rlock.unlock();
30236f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                mIncCompatAllocation = 0;
30246f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            }
30256f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        }
30266f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        if ((mUsage & (USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
30276a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang            setSurface(null);
30286a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang        }
30296a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang        super.destroy();
30306a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang    }
30316f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang
3032dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines}
3033