Allocation.java revision 564639543231f50436ed36ac2b10a9228acef731
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
1945d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport java.io.IOException;
2045d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport java.io.InputStream;
216f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wangimport java.util.concurrent.locks.ReentrantReadWriteLock;
2245d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.content.res.Resources;
2345d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.content.res.AssetManager;
2445d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.graphics.Bitmap;
2545d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.graphics.BitmapFactory;
26c806aeedb20cb1a725092657ce3358eb4008222bTim Murrayimport android.graphics.Canvas;
279c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murrayimport android.view.Surface;
2845d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.util.Log;
2945d443665f5ce7efa934706a89883f0cc87f3513Jason Samsimport android.util.TypedValue;
3045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
3145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams/**
32032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * <p> This class provides the primary method through which data is passed to
33032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * and from RenderScript kernels.  An Allocation provides the backing store for
342192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * a given {@link android.support.v8.renderscript.Type}.  </p>
3545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
36032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * <p>An Allocation also contains a set of usage flags that denote how the
37032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * Allocation could be used. For example, an Allocation may have usage flags
38032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * specifying that it can be used from a script as well as input to a {@link
392192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * android.support.v8.renderscript.Sampler}. A developer must synchronize
402192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * across these different usages using
412192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * {@link android.support.v8.renderscript.Allocation#syncAll} in
42032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * order to ensure that different users of the Allocation have a consistent view
43032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * of memory. For example, in the case where an Allocation is used as the output
44032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * of one kernel and as Sampler input in a later kernel, a developer must call
45032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
46032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * second kernel to ensure correctness.
4745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
48032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
49032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * more complex Element types, the {@link #copyFromUnchecked} methods can be
50032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines * used to copy from byte arrays or similar constructs.</p>
5145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams *
5245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * <div class="special reference">
5345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * <h3>Developer Guides</h3>
542192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * <p>For more information about creating an application that uses
552192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * RenderScript, read the
562192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a>
572192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines * developer guide.</p>
5845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams * </div>
5945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams **/
6045d443665f5ce7efa934706a89883f0cc87f3513Jason Samspublic class Allocation extends BaseObj {
6145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Type mType;
6245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Bitmap mBitmap;
6345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    int mUsage;
6445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    Allocation mAdaptedAllocation;
65d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray    int mSize;
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    /**
2682e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang     * @hide
2692e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang     * Enable/Disable AutoPadding for Vec3 elements.
2702e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang     *
271564639543231f50436ed36ac2b10a9228acef731Miao Wang     * @param useAutoPadding True: enable AutoPadding; False: disable AutoPadding
2722e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang     *
2732e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang     */
2742e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    public void setAutoPadding(boolean useAutoPadding) {
2752e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        mAutoPadding = useAutoPadding;
2762e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    }
2772e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang
2782e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    /**
27945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * Get the size of the Allocation in bytes.
28045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
28145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return size of the Allocation in bytes.
28245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
28345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
28445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public int getBytesSize() {
285c5641d63cef417ab9f17d0886b7c1938f58c11b0Tim Murray        if (mType.mDimYuv != 0) {
286c5641d63cef417ab9f17d0886b7c1938f58c11b0Tim Murray            return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
287c5641d63cef417ab9f17d0886b7c1938f58c11b0Tim Murray        }
28845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mType.getCount() * mType.getElement().getBytesSize();
28945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
29045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
29145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void updateCacheInfo(Type t) {
29245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentDimX = t.getX();
29345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentDimY = t.getY();
29445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentDimZ = t.getZ();
29545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mCurrentCount = mCurrentDimX;
29645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mCurrentDimY > 1) {
29745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            mCurrentCount *= mCurrentDimY;
29845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
29945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mCurrentDimZ > 1) {
30045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            mCurrentCount *= mCurrentDimZ;
30145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
30245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
30345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
3048fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    private void setBitmap(Bitmap b) {
3058fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        mBitmap = b;
3068fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    }
3078fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams
308bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    Allocation(long id, RenderScript rs, Type t, int usage) {
30945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        super(id, rs);
3109c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((usage & ~(USAGE_SCRIPT |
3119c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_GRAPHICS_TEXTURE |
3129c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_IO_INPUT |
3139c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_IO_OUTPUT |
3149c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                       USAGE_SHARED)) != 0) {
31545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
31645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
31745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
3189c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((usage & USAGE_IO_INPUT) != 0) {
3199c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            mWriteAllowed = false;
3209c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
3219c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            if ((usage & ~(USAGE_IO_INPUT |
3229c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                           USAGE_GRAPHICS_TEXTURE |
3239c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                           USAGE_SCRIPT)) != 0) {
3249c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                throw new RSIllegalArgumentException("Invalid usage combination.");
3259c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            }
3269c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
3279c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
32845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mType = t;
32945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mUsage = usage;
3306f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        mIncCompatAllocation = 0;
3316f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        mIncAllocDestroyed = false;
33245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
33345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (t != null) {
3348352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            // TODO: A3D doesn't have Type info during creation, so we can't
3358352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            // calculate the size ahead of time. We can possibly add a method
3368352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            // to update the size in the future if it seems reasonable.
3378352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            mSize = mType.getCount() * mType.getElement().getBytesSize();
33845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            updateCacheInfo(t);
33945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
340d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        if (RenderScript.sUseGCHooks == true) {
341d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            try {
342d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray                RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
343d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            } catch (Exception e) {
344d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray                Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
345d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray                throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
346d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            }
347d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        }
348d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray    }
349d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray
350d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray    protected void finalize() throws Throwable {
351d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        if (RenderScript.sUseGCHooks == true) {
352d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray            RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
353d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        }
354d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray        super.finalize();
35545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
35645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
357bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    private void validateIsInt64() {
358bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
359bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
360bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            return;
361bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        }
362bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        throw new RSIllegalArgumentException(
363bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            "64 bit integer source does not match allocation type " + mType.mElement.mType);
364bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
365d176abf6992ea9b34a01ba1e8b232ac4ac08db31Tim Murray
36645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsInt32() {
36745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
36845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
36945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
37045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
37145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
37245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
37345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
37445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
37545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsInt16() {
37645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
37745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
37845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
37945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
38045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
38145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
38245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
38345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
38445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsInt8() {
38545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
38645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
38745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
38845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
38945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
39045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
39145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
39245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
39345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsFloat32() {
39445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
39545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
39645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
39745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
39845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
39945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
40045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
4018352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void validateIsFloat64() {
4028352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (mType.mElement.mType == Element.DataType.FLOAT_64) {
4038352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            return;
4048352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
4058352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        throw new RSIllegalArgumentException(
4068352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            "64 bit float source does not match allocation type " + mType.mElement.mType);
4078352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
4088352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
40945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateIsObject() {
41045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
41145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
41245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
41345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
41445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT)) {
41545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return;
41645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
41745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSIllegalArgumentException(
41845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
41945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
42045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
42145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
4222192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * Get the {@link android.support.v8.renderscript.Type} of the Allocation.
42345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
42445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return Type
42545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
42645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
42745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public Type getType() {
42845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return mType;
42945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
43045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
43145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
432032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Propagate changes from one usage of the Allocation to the
433032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * other usages of the Allocation.
43445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
43545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
43645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void syncAll(int srcLocation) {
43745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        switch (srcLocation) {
43845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case USAGE_SCRIPT:
43945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case USAGE_GRAPHICS_TEXTURE:
44045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
44145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        default:
44245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
44345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
44445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
44545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
44645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
44745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
4489c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
449032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Send a buffer to the output stream.  The contents of the Allocation will
450032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * be undefined after this operation. This operation is only valid if {@link
451032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #USAGE_IO_OUTPUT} is set on the Allocation.
452032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
4539c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
4549c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
4559c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public void ioSend() {
4569c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
4579c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            throw new RSIllegalArgumentException(
4589c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                "Can only send buffer if IO_OUTPUT usage specified.");
4599c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
4609c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.validate();
4619c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.nAllocationIoSend(getID(mRS));
4629c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
4639c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
4649c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
4659c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * Delete once code is updated.
4669c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @hide
4679c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
4689c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public void ioSendOutput() {
4699c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        ioSend();
4709c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
4719c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
4729c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
473032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Receive the latest input into the Allocation. This operation
474032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
4759c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
4769c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
4779c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    public void ioReceive() {
4789c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if ((mUsage & USAGE_IO_INPUT) == 0) {
4799c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            throw new RSIllegalArgumentException(
4809c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                "Can only receive if IO_INPUT usage specified.");
4819c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
4829c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.validate();
4839c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        mRS.nAllocationIoReceive(getID(mRS));
4849c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
48545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
48645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
487032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array of RS objects to the Allocation.
48845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
48945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d Source array.
49045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
49145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(BaseObj[] d) {
49245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
49345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsObject();
49445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (d.length != mCurrentCount) {
49545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
49645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
49745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
498bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang
499bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        if (RenderScript.sPointerSize == 8) {
500bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            long i[] = new long[d.length * 4];
501bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            for (int ct=0; ct < d.length; ct++) {
502bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang                i[ct * 4] = d[ct].getID(mRS);
503bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            }
504bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            copy1DRangeFromUnchecked(0, mCurrentCount, i);
505bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        } else {
506bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            int i[] = new int[d.length];
507bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            for (int ct=0; ct < d.length; ct++) {
508bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang                i[ct] = (int)d[ct].getID(mRS);
509bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            }
510bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            copy1DRangeFromUnchecked(0, mCurrentCount, i);
51145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
51245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
51345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
51445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateBitmapFormat(Bitmap b) {
51545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Bitmap.Config bc = b.getConfig();
516c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (bc == null) {
517c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
518c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
51945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        switch (bc) {
52045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case ALPHA_8:
52145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
52245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
52345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
52445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
52545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
52645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
52745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
52845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
52945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case ARGB_8888:
53045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
53145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                (mType.getElement().getBytesSize() != 4)) {
53245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
53345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
53445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
53545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
53645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
53745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
53845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
53945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case RGB_565:
54045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
54145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                (mType.getElement().getBytesSize() != 2)) {
54245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
54345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
54445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
54545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
54645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
54745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
54845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
54945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        case ARGB_4444:
55045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
55145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                (mType.getElement().getBytesSize() != 2)) {
55245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
55345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mKind + ", type " +
55445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     mType.getElement().mType +
55545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " of " + mType.getElement().getBytesSize() +
55645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     " bytes, passed bitmap was " + bc);
55745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
55845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            break;
559302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
56045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
56145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
56245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
56345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validateBitmapSize(Bitmap b) {
56445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
56545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
56645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
56745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
56845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
5698352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
5708352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        mRS.validate();
5718352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        if (mCurrentDimZ > 0) {
5728352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
5738352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        } else if (mCurrentDimY > 0) {
5748352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
5758352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        } else {
5768352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang            copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
5778352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        }
5788352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
5798352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
5808352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    /**
5818352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * Copy into this Allocation from an array. This method does not guarantee
5828352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * that the Allocation is compatible with the input buffer; it copies memory
5838352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * without reinterpretation.
5848352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     *
5858352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * @param array The source data array
5868352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     */
5878352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copyFromUnchecked(Object array) {
5888352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
5898352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                          java.lang.reflect.Array.getLength(array));
5908352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
5918352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
59245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
593032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
594032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
595032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
59645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
59745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
59845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
59945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(int[] d) {
6008352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
60145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
6028352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
60345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
604032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
605032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
606032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
60745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
60845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
60945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
61045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(short[] d) {
6118352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
61245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
6138352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
61445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
615032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
616032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
617032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
61845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
61945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
62045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
62145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(byte[] d) {
6228352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
62345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
6248352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
62545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
626032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array. This method does not guarantee
627032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * that the Allocation is compatible with the input buffer; it copies memory
628032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * without reinterpretation.
62945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
63045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
63145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
63245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFromUnchecked(float[] d) {
6338352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
6348352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
6358352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
6368352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
6378352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    /**
6388352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * Copy into this Allocation from an array.  This variant is type checked
6398352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * and will generate exceptions if the Allocation's {@link
6408352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * android.renderscript.Element} does not match the array's
6418352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * primitive type.
6428352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     *
6438352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * @param array The source data array
6448352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     */
6458352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copyFrom(Object array) {
6468352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
6478352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                          java.lang.reflect.Array.getLength(array));
64845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
64945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
65045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
651032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
652032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
6532192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Element} is not a 32 bit integer type.
65445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
65545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
65645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
65745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(int[] d) {
6588352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt32();
6598352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
66045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
66145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
66245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
663032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
664032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
6652192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Element} is not a 16 bit integer type.
66645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
66745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
66845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
66945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(short[] d) {
6708352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt16();
6718352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
67245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
67345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
67445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
675032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
676032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
6772192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Element} is not an 8 bit integer type.
67845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
67945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
68045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
68145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(byte[] d) {
6828352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt8();
6838352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
68445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
68545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
68645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
687032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into this Allocation from an array.  This variant is type checked
688032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation's {@link
6892192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Element} is not a 32 bit float type.
69045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
69145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
69245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
69345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(float[] d) {
6948352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsFloat32();
6958352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
69645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
69745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
69845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
699032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy into an Allocation from a {@link android.graphics.Bitmap}.  The
700032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * height, width, and format of the bitmap must match the existing
701032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * allocation.
702032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
703032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
704032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.Bitmap} used to create the Allocation with {@link
705032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
706032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * this will synchronize the Allocation with the latest data from the {@link
707032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
70845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
70945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b the source bitmap
71045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
71145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyFrom(Bitmap b) {
71245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
713c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (b.getConfig() == null) {
714c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
715c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Canvas c = new Canvas(newBitmap);
716c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            c.drawBitmap(b, 0, 0, null);
717c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            copyFrom(newBitmap);
718c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            return;
719c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
72045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapSize(b);
72145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapFormat(b);
72245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationCopyFromBitmap(getID(mRS), b);
72345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
72445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
72545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
726032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an Allocation from an Allocation.  The types of both allocations
7278fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     * must be identical.
7288fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     *
7298fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     * @param a the source allocation
7308fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams     */
7318fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    public void copyFrom(Allocation a) {
7328fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        mRS.validate();
7338fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        if (!mType.equals(a.getType())) {
7348fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams            throw new RSIllegalArgumentException("Types of allocations must match.");
7358fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        }
7368fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams        copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
7378fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    }
7388fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams
7398fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams
7408fafeb11cf19fdada0add886e99ab0af5d3ddabeJason Sams    /**
741032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * This is only intended to be used by auto-generated code reflected from
742032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * the RenderScript script files and should not be used by developers.
74345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
74445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xoff
74545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param fp
74645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
74745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
74845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
74945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int eSize = mType.mElement.getBytesSize();
75045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        final byte[] data = fp.getData();
751908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        int data_length = fp.getPos();
75245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
753908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        int count = data_length / eSize;
754908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        if ((eSize * count) != data_length) {
755908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines            throw new RSIllegalArgumentException("Field packer length " + data_length +
75645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                               " not divisible by element size " + eSize + ".");
75745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
75845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
75945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
76045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
76145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
762032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * This is only intended to be used by auto-generated code reflected from
763032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * the RenderScript script files.
76445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
76545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xoff
76645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param component_number
76745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param fp
76845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
76945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
77045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
77145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (component_number >= mType.mElement.mElements.length) {
77245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
77345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
77445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(xoff < 0) {
77545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
77645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
77745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
77845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        final byte[] data = fp.getData();
779908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        int data_length = fp.getPos();
78045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int eSize = mType.mElement.mElements[component_number].getBytesSize();
78145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        eSize *= mType.mElement.mArraySizes[component_number];
78245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
783908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines        if (data_length != eSize) {
784908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
78545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                               " does not match component size " + eSize + ".");
78645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
78745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
78845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
789908f238ae73283a7f3d49ddf7a562ccbd1a9ac44Stephen Hines                                     component_number, data, data_length);
79045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
79145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
792244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
793244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
794244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * This is only intended to be used by auto-generated code reflected from
795244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * the RenderScript script files.
796244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
797244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff
798244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff
799244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param zoff
800244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param component_number
801244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param fp
802244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
803244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /*
804244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void setFromFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
805244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.validate();
806244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if (component_number >= mType.mElement.mElements.length) {
807244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
808244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
809244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(xoff < 0) {
810244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset x must be >= 0.");
811244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
812244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(yoff < 0) {
813244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset y must be >= 0.");
814244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
815244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(zoff < 0) {
816244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset z must be >= 0.");
817244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
818244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
819244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        final byte[] data = fp.getData();
820244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        int data_length = fp.getPos();
821244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        int eSize = mType.mElement.mElements[component_number].getBytesSize();
822244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        eSize *= mType.mElement.mArraySizes[component_number];
823244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
824244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if (data_length != eSize) {
825244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
826244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                               " does not match component size " + eSize + ".");
827244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
828244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
829244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.nAllocationElementData(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
830244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                   component_number, data, data_length);
831244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
832244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    */
833244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
8342e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang    private void data1DChecks(int off, int count, int len, int dataSize, boolean usePadding) {
83545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
83645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(off < 0) {
83745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
83845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
83945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(count < 1) {
84045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
84145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
84245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if((off + count) > mCurrentCount) {
84345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
84445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                               ", got " + count + " at offset " + off + ".");
84545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
8462e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if(usePadding) {
8472e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if(len < dataSize / 4 * 3) {
8482e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
8492e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
8502e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
8512e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if(len < dataSize) {
8522e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
8532e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
85445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
85545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
85645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
85745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
858032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Generate a mipmap chain. This is only valid if the Type of the Allocation
859032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * includes mipmaps.
86045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
861032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This function will generate a complete set of mipmaps from the top
862032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * level LOD and place them into the script memory space.</p>
86345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
864032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>If the Allocation is also using other memory spaces, a call to {@link
865032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
86645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
86745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void generateMipmaps() {
86845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationGenerateMipmaps(getID(mRS));
86945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
87045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
8718352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void copy1DRangeFromUnchecked(int off, int count, Object array,
8728352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                          Element.DataType dt, int arrayLen) {
8738352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        final int dataSize = mType.mElement.getBytesSize() * count;
8742e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
8752e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
8762e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
8772e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
8782e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
8792e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
8802e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
8812e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              mType.mElement.mType.mSize, usePadding);
8828352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
8838352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
88445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
885032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This method does not
886032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
88745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
88845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
88945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
8908352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * @param array The source data array
89145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
8928352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy1DRangeFromUnchecked(int off, int count, Object array) {
8938352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, array,
8948352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 validateObjectIsPrimitiveArray(array, false),
8958352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 java.lang.reflect.Array.getLength(array));
896bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
8978352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
898bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    /**
899bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * Copy an array into part of this Allocation.  This method does not
900bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
901bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     *
902bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param off The offset of the first element to be copied.
903bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param count The number of elements to be copied.
904bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param d the source data array
905bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     */
90645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
9078352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
90845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
9098352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
91045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
911032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This method does not
912032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
91345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
91445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
91545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
91645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
91745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
91845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
9198352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
92045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
9218352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
92245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
923032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This method does not
924032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
92545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
92645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
92745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
92845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
92945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
93045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
9318352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
93245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
9338352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
93445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
935032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This method does not
936032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * guarantee that the Allocation is compatible with the input buffer.
93745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
93845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
93945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
94045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
94145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
94245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
9438352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
94445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
94545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
9468352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
94745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
948032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
9498352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * and will generate exceptions if the Allocation type does not
9508352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * match the component type of the array passed in.
95145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
95245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
95345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
9548352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * @param array The source data array.
95545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
9568352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy1DRangeFrom(int off, int count, Object array) {
9578352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, array,
9588352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 validateObjectIsPrimitiveArray(array, true),
9598352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 java.lang.reflect.Array.getLength(array));
960bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
961bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang
962bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    /**
963bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * Copy an array into part of this Allocation.  This variant is type checked
964bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * and will generate exceptions if the Allocation type is not a 32 bit
965bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * integer type.
966bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     *
967bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param off The offset of the first element to be copied.
968bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param count The number of elements to be copied.
969bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param d the source data array
970bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     */
97145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
97245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt32();
9738352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
97445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
97545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
97645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
977032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
978032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation type is not a 16 bit
979032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * integer type.
98045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
98145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
98245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
98345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
98445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
98545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
98645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt16();
9878352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
98845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
98945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
99045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
991032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
992032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation type is not an 8 bit
993032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * integer type.
99445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
99545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
99645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
99745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array
99845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
99945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
100045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt8();
10018352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
100245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
100345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
100445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1005032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy an array into part of this Allocation.  This variant is type checked
1006032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and will generate exceptions if the Allocation type is not a 32 bit float
1007032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * type.
100845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
100945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
101045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
101145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d the source data array.
101245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
101345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
101445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsFloat32();
10158352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
101645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
101745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
101845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     /**
1019032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy part of an Allocation into this Allocation.
102045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
102145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param off The offset of the first element to be copied.
102245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param count The number of elements to be copied.
102345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param data the source data allocation.
102445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param dataOff off The offset of the first element in data to
102545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *          be copied.
102645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
102745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
102845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
102945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              mSelectedLOD, mSelectedFace.mID,
103045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              count, 1, data.getID(mRS), dataOff, 0,
103145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
103245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
103345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
103445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
103545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (mAdaptedAllocation != null) {
103645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
103745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        } else {
103845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
103945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (xoff < 0 || yoff < 0) {
104045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
104145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
104245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (h < 0 || w < 0) {
104345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
104445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
104545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
104645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
104745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            }
104845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
104945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
105045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
10518352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
10528352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                  Element.DataType dt, int arrayLen) {
105345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
105445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, w, h);
10552e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h;
10562e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
10572e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
10582e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        int sizeBytes = arrayLen * dt.mSize;
10592e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
10602e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
10612e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
10622e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
10632e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
10642e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            sizeBytes = dataSize;
10652e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
10662e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize > sizeBytes) {
10672e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
10682e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
10692e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
10708352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
10712e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              array, sizeBytes, dt,
10722e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              mType.mElement.mType.mSize, usePadding);
107345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
107445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1075dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    /**
1076032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1077032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * array is assumed to be tightly packed.
1078dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines     *
1079032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1080032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1081032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1082032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
10838352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * @param array Data to be placed into the Allocation
1084dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines     */
10858352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
10868352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
10878352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 validateObjectIsPrimitiveArray(array, true),
10888352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 java.lang.reflect.Array.getLength(array));
1089dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
1090dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
1091032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
1092032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1093032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * array is assumed to be tightly packed.
1094032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1095032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1096032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1097032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1098032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
1099032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
1100032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
11018352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
11028352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt8();
11038352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
11048352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 Element.DataType.SIGNED_8, data.length);
1105dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
1106dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
1107032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
1108032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1109032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * array is assumed to be tightly packed.
1110032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1111032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1112032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1113032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1114032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
1115032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
1116032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
11178352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
11188352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        validateIsInt16();
11198352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
11208352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 Element.DataType.SIGNED_16, data.length);
1121bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
1122bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang
1123bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    /**
1124bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * Copy from an array into a rectangular region in this Allocation.  The
1125bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * array is assumed to be tightly packed.
1126bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     *
1127bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param xoff X offset of the region to update in this Allocation
1128bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param yoff Y offset of the region to update in this Allocation
1129bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param w Width of the region to update
1130bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param h Height of the region to update
1131bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param data to be placed into the Allocation
1132bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     */
1133dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
1134dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        validateIsInt32();
11358352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
11368352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 Element.DataType.SIGNED_32, data.length);
1137dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
1138dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
1139032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines    /**
1140032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from an array into a rectangular region in this Allocation.  The
1141032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * array is assumed to be tightly packed.
1142032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1143032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1144032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1145032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1146032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
1147032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data to be placed into the Allocation
1148032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     */
1149dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
1150dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines        validateIsFloat32();
11518352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
11528352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 Element.DataType.FLOAT_32, data.length);
1153dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines    }
1154dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines
115545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1156032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy a rectangular region from an Allocation into a rectangular region in
1157032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * this Allocation.
115845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1159032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region in this Allocation
1160032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region in this Allocation
1161032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update.
1162032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update.
1163032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data source Allocation.
1164032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataXoff X offset in source Allocation
1165032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataYoff Y offset in source Allocation
116645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
116745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
116845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                Allocation data, int dataXoff, int dataYoff) {
116945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
117045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, w, h);
117145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
117245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              mSelectedLOD, mSelectedFace.mID,
117345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              w, h, data.getID(mRS), dataXoff, dataYoff,
117445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
117545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
117645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
117745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1178032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy a {@link android.graphics.Bitmap} into an Allocation.  The height
1179032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and width of the update will use the height and width of the {@link
1180032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * android.graphics.Bitmap}.
118145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1182032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1183032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1184032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param data the Bitmap to be copied
118545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
118645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
118745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
1188c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (data.getConfig() == null) {
1189c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1190c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Canvas c = new Canvas(newBitmap);
1191c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            c.drawBitmap(data, 0, 0, null);
1192c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            copy2DRangeFrom(xoff, yoff, newBitmap);
1193302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            return;
1194c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
119545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapFormat(data);
119645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
119745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
119845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
119945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1200302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1201302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        if (mAdaptedAllocation != null) {
1202302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1203302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        } else {
1204302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1205302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            if (xoff < 0 || yoff < 0 || zoff < 0) {
1206302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
1207302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            }
1208302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            if (h < 0 || w < 0 || d < 0) {
1209302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
1210302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            }
1211302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1212302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
1213302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams            }
1214302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        }
1215302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1216302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1217302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1218302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1219302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1220302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
12218352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
12228352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                          Object array, Element.DataType dt, int arrayLen) {
1223302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.validate();
1224302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
12252e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h * d;
12262e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
12272e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
12282e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        int sizeBytes = arrayLen * dt.mSize;
12292e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
12302e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
12312e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
12322e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
12332e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
12342e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            sizeBytes = dataSize;
12352e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
12362e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize > sizeBytes) {
12372e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
12382e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
12392e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
12408352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
12412e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              array, sizeBytes, dt,
12422e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              mType.mElement.mType.mSize, usePadding);
1243302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1244302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1245302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1246302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1247302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * Copy a rectangular region from the array into the allocation.
1248032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * The array is assumed to be tightly packed.
1249032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
1250032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1251032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1252032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param zoff Z offset of the region to update in this Allocation
1253032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update
1254032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update
1255032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param d Depth of the region to update
1256302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @param data to be placed into the allocation
1257302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
12588352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
12598352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
12608352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 validateObjectIsPrimitiveArray(array, true),
12618352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang                                 java.lang.reflect.Array.getLength(array));
1262302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1263302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1264302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1265302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @hide
1266302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * Copy a rectangular region into the allocation from another
1267302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * allocation.
1268302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     *
1269032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param xoff X offset of the region to update in this Allocation
1270032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param yoff Y offset of the region to update in this Allocation
1271032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param zoff Z offset of the region to update in this Allocation
1272032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param w Width of the region to update.
1273032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param h Height of the region to update.
1274032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param d Depth of the region to update.
1275302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     * @param data source allocation.
1276032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataXoff X offset of the region in the source Allocation
1277032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataYoff Y offset of the region in the source Allocation
1278032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param dataZoff Z offset of the region in the source Allocation
1279302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams     */
1280302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1281302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                                Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1282302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.validate();
1283302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
1284302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1285302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1286302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams                              data.mSelectedLOD);
1287302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    }
1288302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1289302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
1290302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams    /**
1291032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a {@link android.graphics.Bitmap}.  The
1292032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * bitmap must match the dimensions of the Allocation.
129345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
129445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b The bitmap to be set from the Allocation.
129545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
129645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(Bitmap b) {
129745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.validate();
129845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapFormat(b);
129945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateBitmapSize(b);
130045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mRS.nAllocationCopyToBitmap(getID(mRS), b);
130145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
130245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
13038352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    private void copyTo(Object array, Element.DataType dt, int arrayLen) {
13048352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        mRS.validate();
13052e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
13062e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
13072e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
13082e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
1309a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang        if (usePadding) {
1310a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang            if (dt.mSize * arrayLen < mSize / 4 * 3) {
1311a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang                throw new RSIllegalArgumentException(
1312a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang                    "Size of output array cannot be smaller than size of allocation.");
1313a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang            }
1314a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang        } else {
1315a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang            if (dt.mSize * arrayLen < mSize) {
1316a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang                throw new RSIllegalArgumentException(
1317a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang                    "Size of output array cannot be smaller than size of allocation.");
1318a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang            }
1319a4f4a00cdd01fe58862f7ee7d81a9fd445f9dbf7Miao Wang        }
13202e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        mRS.nAllocationRead(getID(mRS), array, dt, mType.mElement.mType.mSize, usePadding);
13218352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
13228352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
13238352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    /**
13248352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * Copy from the Allocation into an array.  The array must be at
13258352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * least as large as the Allocation.  The
13268352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * {@link android.renderscript.Element} must match the component
13278352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * type of the array passed in.
13288352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     *
13298352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     * @param array The array to be set from the Allocation.
13308352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang     */
13318352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    public void copyTo(Object array) {
13328352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(array, validateObjectIsPrimitiveArray(array, true),
13338352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang               java.lang.reflect.Array.getLength(array));
13348352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang    }
13358352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
133645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1337032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a byte array.  The array must be at least
1338032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * as large as the Allocation.  The allocation must be of an 8 bit integer
13392192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
134045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
134145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
134245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
134345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(byte[] d) {
134445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt8();
13458352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(d, Element.DataType.SIGNED_8, d.length);
134645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
134745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
134845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1349032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a short array.  The array must be at least
1350032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * as large as the Allocation.  The allocation must be of an 16 bit integer
13512192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
135245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
135345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
135445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
135545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(short[] d) {
135645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt16();
13578352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(d, Element.DataType.SIGNED_16, d.length);
1358bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    }
1359bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang
1360bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang    /**
1361bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * Copy from the Allocation into a int array.  The array must be at least as
1362bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * large as the Allocation.  The allocation must be of an 32 bit integer
1363bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * {@link android.support.v8.renderscript.Element} type.
1364bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     *
1365bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     * @param d The array to be set from the Allocation.
1366bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang     */
136745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(int[] d) {
136845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsInt32();
13698352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(d, Element.DataType.SIGNED_32, d.length);
137045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
137145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
137245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1373032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Copy from the Allocation into a float array.  The array must be at least
1374032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * as large as the Allocation.  The allocation must be of an 32 bit float
13752192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * {@link android.support.v8.renderscript.Element} type.
137645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
137745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param d The array to be set from the Allocation.
137845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
137945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    public void copyTo(float[] d) {
138045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        validateIsFloat32();
13818352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang        copyTo(d, Element.DataType.FLOAT_32, d.length);
138245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
138345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1384244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1385244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
13863bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang     * This is only intended to be used by auto-generated code reflected from
13873bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang     * the RenderScript script files and should not be used by developers.
1388244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1389244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff
1390244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff
1391244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param zoff
1392244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param component_number
1393244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array
1394244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1395244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /*
13963bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang    public void copyToFieldPacker(int xoff, int yoff, int zoff, int component_number, FieldPacker fp) {
1397244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.validate();
1398244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if (component_number >= mType.mElement.mElements.length) {
1399244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
1400244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1401244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(xoff < 0) {
1402244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset x must be >= 0.");
1403244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1404244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(yoff < 0) {
1405244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset y must be >= 0.");
1406244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1407244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        if(zoff < 0) {
1408244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang            throw new RSIllegalArgumentException("Offset z must be >= 0.");
1409244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1410244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
14113bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang        final byte[] data = fp.getData();
14123bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang        int data_length = fp.getPos();
1413244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        int eSize = mType.mElement.mElements[component_number].getBytesSize();
1414244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        eSize *= mType.mElement.mArraySizes[component_number];
1415244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
14163bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang        if (data_length != eSize) {
14173bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
14183bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang                                               " does not match component size " + eSize + ".");
1419244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        }
1420244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1421244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.nAllocationElementRead(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
14223bc17a5878f014a61d67e38901f310fd77aeaeacMiao Wang                                   component_number, data, data_length);
1423244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1424244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    */
1425244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1426244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    private void copy1DRangeToUnchecked(int off, int count, Object array,
1427244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                        Element.DataType dt, int arrayLen) {
1428244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        final int dataSize = mType.mElement.getBytesSize() * count;
14292e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
14302e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
14312e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
14322e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
14332e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
14342e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        data1DChecks(off, count, arrayLen * dt.mSize, dataSize, usePadding);
14352e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        mRS.nAllocationRead1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt,
14362e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              mType.mElement.mType.mSize, usePadding);
1437244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1438244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1439244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1440244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1441244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1442244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
1443244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1444244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1445244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1446244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array The dest data array
1447244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1448244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, Object array) {
1449244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, array,
1450244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               validateObjectIsPrimitiveArray(array, false),
1451244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               java.lang.reflect.Array.getLength(array));
1452244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1453244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1454244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1455244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1456244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1457244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
1458244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1459244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1460244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1461244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d the source data array
1462244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1463244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, int[] d) {
1464244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
1465244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1466244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1467244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1468244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1469244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1470244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
1471244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1472244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1473244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1474244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d the source data array
1475244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1476244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, short[] d) {
1477244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
1478244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1479244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1480244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1481244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1482244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1483244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
1484244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1485244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1486244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1487244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d the source data array
1488244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1489244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, byte[] d) {
1490244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
1491244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1492244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1493244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1494244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1495244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1496244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * guarantee that the Allocation is compatible with the input buffer.
1497244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1498244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1499244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1500244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d the source data array
1501244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1502244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeToUnchecked(int off, int count, float[] d) {
1503244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
1504244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1505244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1506244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1507244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1508244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1509244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1510244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * and will generate exceptions if the Allocation type does not
1511244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * match the component type of the array passed in.
1512244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1513244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1514244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1515244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array The source data array.
1516244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1517244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, Object array) {
1518244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, array,
1519244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               validateObjectIsPrimitiveArray(array, true),
1520244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               java.lang.reflect.Array.getLength(array));
1521244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1522244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1523244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1524244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1525244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1526244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * and will generate exceptions if the Allocation type is not a 32 bit
1527244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * integer type.
1528244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1529244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1530244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1531244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d the source data array
1532244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1533244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, int[] d) {
1534244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt32();
1535244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
1536244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1537244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1538244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1539244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1540244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1541244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * and will generate exceptions if the Allocation type is not a 16 bit
1542244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * integer type.
1543244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1544244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1545244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1546244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d the source data array
1547244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1548244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, short[] d) {
1549244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt16();
1550244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
1551244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1552244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1553244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1554244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1555244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1556244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * and will generate exceptions if the Allocation type is not an 8 bit
1557244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * integer type.
1558244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1559244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1560244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1561244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d the source data array
1562244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1563244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, byte[] d) {
1564244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt8();
1565244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
1566244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1567244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1568244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1569244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1570244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy part of this Allocation into an array.  This method does not
1571244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * and will generate exceptions if the Allocation type is not a 32 bit float
1572244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * type.
1573244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1574244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param off The offset of the first element to be copied.
1575244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param count The number of elements to be copied.
1576244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d the source data array.
1577244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1578244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy1DRangeTo(int off, int count, float[] d) {
1579244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsFloat32();
1580244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy1DRangeToUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
1581244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1582244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1583244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1584244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    void copy2DRangeToUnchecked(int xoff, int yoff, int w, int h, Object array,
1585244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                Element.DataType dt, int arrayLen) {
1586244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.validate();
1587244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validate2DRange(xoff, yoff, w, h);
15882e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h;
15892e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
15902e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
15912e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        int sizeBytes = arrayLen * dt.mSize;
15922e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
15932e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
15942e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
15952e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
15962e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
15972e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            sizeBytes = dataSize;
15982e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
15992e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize > sizeBytes) {
16002e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
16012e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
16022e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
1603244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.nAllocationRead2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
16042e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1605244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1606244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1607244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1608244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1609244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy from a rectangular region in this Allocation into an array.
1610244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1611244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
1612244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
1613244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
1614244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
1615244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array Dest Array to be copied into
1616244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1617244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, Object array) {
1618244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, array,
1619244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               validateObjectIsPrimitiveArray(array, true),
1620244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               java.lang.reflect.Array.getLength(array));
1621244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1622244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1623244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1624244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1625244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy from a rectangular region in this Allocation into an array.
1626244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1627244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
1628244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
1629244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
1630244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
1631244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array Dest Array to be copied into
1632244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1633244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, byte[] data) {
1634244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt8();
1635244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1636244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               Element.DataType.SIGNED_8, data.length);
1637244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1638244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1639244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1640244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1641244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy from a rectangular region in this Allocation into an array.
1642244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1643244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
1644244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
1645244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
1646244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
1647244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array Dest Array to be copied into
1648244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1649244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, short[] data) {
1650244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt16();
1651244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1652244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               Element.DataType.SIGNED_16, data.length);
1653244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1654244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1655244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1656244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1657244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy from a rectangular region in this Allocation into an array.
1658244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1659244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
1660244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
1661244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
1662244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
1663244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array Dest Array to be copied into
1664244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1665244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, int[] data) {
1666244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsInt32();
1667244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1668244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               Element.DataType.SIGNED_32, data.length);
1669244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1670244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1671244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1672244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1673244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy from a rectangular region in this Allocation into an array.
1674244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1675244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
1676244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
1677244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
1678244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
1679244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array Dest Array to be copied into
1680244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1681244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy2DRangeTo(int xoff, int yoff, int w, int h, float[] data) {
1682244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validateIsFloat32();
1683244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy2DRangeToUnchecked(xoff, yoff, w, h, data,
1684244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                               Element.DataType.FLOAT_32, data.length);
1685244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1686244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1687244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1688244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1689244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1690244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1691244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1692244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /*
1693244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    private void copy3DRangeToUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
1694244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                        Object array, Element.DataType dt, int arrayLen) {
1695244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.validate();
1696244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        validate3DRange(xoff, yoff, zoff, w, h, d);
16972e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        final int dataSize = mType.mElement.getBytesSize() * w * h * d;
16982e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        // AutoPadding for Vec3 Element
16992e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        boolean usePadding = false;
17002e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        int sizeBytes = arrayLen * dt.mSize;
17012e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        if (mAutoPadding && (mType.getElement().getVectorSize() == 3)) {
17022e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize / 4 * 3 > sizeBytes) {
17032e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
17042e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
17052e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            usePadding = true;
17062e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            sizeBytes = dataSize;
17072e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        } else {
17082e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            if (dataSize > sizeBytes) {
17092e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                throw new RSIllegalArgumentException("Array too small for allocation type.");
17102e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang            }
17112e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang        }
1712244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        mRS.nAllocationRead3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
17132e1cfff791e07a1caa8cb5ece86e8627f4be8b3fMiao Wang                              array, sizeBytes, dt, mType.mElement.mType.mSize, usePadding);
1714244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1715244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    */
1716244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
1717244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /**
1718244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @hide
1719244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * Copy from a rectangular region in this Allocation into an array.
1720244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     *
1721244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param xoff X offset of the region to copy in this Allocation
1722244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param yoff Y offset of the region to copy in this Allocation
1723244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param zoff Z offset of the region to copy in this Allocation
1724244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param w Width of the region to copy
1725244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param h Height of the region to copy
1726244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param d Depth of the region to copy
1727244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     * @param array Dest Array to be copied into
1728244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang     */
1729244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    /*
1730244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    public void copy3DRangeTo(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
1731244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang        copy3DRangeToUnchecked(xoff, yoff, zoff, w, h, d, array,
1732244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                 validateObjectIsPrimitiveArray(array, true),
1733244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang                                 java.lang.reflect.Array.getLength(array));
1734244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    }
1735244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang    */
1736244b72fe088a6393f37dea49ff89cf9df738a492Miao Wang
173745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    // creation
173845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
173945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
174045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static {
174145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        mBitmapOptions.inScaled = false;
174245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
174345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
174445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1745032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a new Allocation with the given {@link
17462192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Type}, mipmap flag, and usage flags.
174745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1748032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param type RenderScript type describing data layout
174945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the
175045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *             allocation
1751032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param usage bit field specifying how the Allocation is
175245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
175345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
175445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
175545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
175645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (type.getID(rs) == 0) {
175745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSInvalidStateException("Bad Type");
175845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
1759dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang
1760dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        if(!rs.usingIO() && (usage & (USAGE_IO_INPUT | USAGE_IO_INPUT)) != 0) {
1761dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang            throw new RSRuntimeException("USAGE_IO not supported, Allocation creation failed.");
1762dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        }
1763dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang
1764bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
176545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (id == 0) {
176645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
176745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
176845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, type, usage);
176945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
177045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
177145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1772032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation with the size specified by the type and no mipmaps
1773032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * generated by default
177445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
177545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
177645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param type renderscript type describing data layout
177745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the allocation is
177845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
177945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
178045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
178145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
178245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
178345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
178445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
178545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
178645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1787032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation for use by scripts with a given {@link
17882192d04dde96e02b9c908d3cc846c8c881ff54a1Stephen Hines     * android.support.v8.renderscript.Type} and no mipmaps
178945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1790032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param rs Context to which the Allocation will belong.
1791032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param type RenderScript Type describing data layout
179245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
179345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
179445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
179545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
179645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
179745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
179845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
179945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1800032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation with a specified number of given elements
180145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1802032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param rs Context to which the Allocation will belong.
1803032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param e Element to use in the Allocation
1804032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param count the number of Elements in the Allocation
1805032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param usage bit field specifying how the Allocation is
180645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
180745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
180845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
180945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
181045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createSized(RenderScript rs, Element e,
181145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                         int count, int usage) {
181245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
181345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder b = new Type.Builder(rs, e);
181445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        b.setX(count);
181545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = b.create();
181645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1817bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
181845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (id == 0) {
181945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
182045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
182145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, t, usage);
182245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
182345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
182445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1825032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation with a specified number of given elements
182645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1827032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param rs Context to which the Allocation will belong.
1828032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param e Element to use in the Allocation
1829032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param count the number of Elements in the Allocation
183045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
183145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation
183245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
183345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
183445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
183545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
183645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
183745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
183845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        final Bitmap.Config bc = b.getConfig();
183945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
184045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.A_8(rs);
184145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
184245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
184345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.RGBA_4444(rs);
184445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
184545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
184645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.RGBA_8888(rs);
184745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
184845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (bc == Bitmap.Config.RGB_565) {
184945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            return Element.RGB_565(rs);
185045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
185145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        throw new RSInvalidStateException("Bad bitmap type: " + bc);
185245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
185345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
185445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
185545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                       MipmapControl mip) {
185645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Element e = elementFromBitmap(rs, b);
185745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
185845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setX(b.getWidth());
185945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setY(b.getHeight());
186045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
186145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return tb.create();
186245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
186345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
186445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1865032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
186645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
186745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
1868032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param b Bitmap source for the allocation data
186945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the
187045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *             allocation
187145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the allocation is
187245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *              utilized
187345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1874032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing bitmap data
187545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
187645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
187745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
187845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                              MipmapControl mips,
187945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                              int usage) {
188045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
1881c806aeedb20cb1a725092657ce3358eb4008222bTim Murray
1882c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        // WAR undocumented color formats
1883c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        if (b.getConfig() == null) {
1884c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            if ((usage & USAGE_SHARED) != 0) {
1885c806aeedb20cb1a725092657ce3358eb4008222bTim Murray                throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1886c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            }
1887c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1888c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            Canvas c = new Canvas(newBitmap);
1889c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            c.drawBitmap(b, 0, 0, null);
1890c806aeedb20cb1a725092657ce3358eb4008222bTim Murray            return createFromBitmap(rs, newBitmap, mips, usage);
1891c806aeedb20cb1a725092657ce3358eb4008222bTim Murray        }
1892c806aeedb20cb1a725092657ce3358eb4008222bTim Murray
189345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = typeFromBitmap(rs, b, mips);
189445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
18959c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        // enable optimized bitmap path only with no mipmap and script-only usage
18969c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        if (mips == MipmapControl.MIPMAP_NONE &&
18979c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
1898257cecb5e24c9af71ba4d621887e88e46b0ac411Stephen Hines            usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
1899bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang            long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
19009c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            if (id == 0) {
19019c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                throw new RSRuntimeException("Load failed.");
19029c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            }
19039c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
19049c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            // keep a reference to the Bitmap around to prevent GC
19059c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            Allocation alloc = new Allocation(id, rs, t, usage);
19069c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            alloc.setBitmap(b);
19079c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            return alloc;
19089c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
19099c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
19109c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
1911bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
191245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (id == 0) {
191345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Load failed.");
191445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
191545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, t, usage);
191645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
191745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
191845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1919dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     * Associate a {@link android.view.Surface} with this Allocation. This
1920dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
1921dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     *
1922dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     * @param sur Surface to associate with allocation
1923dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang     */
1924dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    public void setSurface(Surface sur) {
1925dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        mRS.validate();
1926dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1927dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang            throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1928dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        }
19298352bdceb7f0030593db04a06ba8caebf81eaeeaMiao Wang
1930dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang        mRS.nAllocationSetSurface(getID(mRS), sur);
1931dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    }
1932dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang
1933dd12e72228ea20b7fdea4dbf32c88291aef6d552Miao Wang    /**
1934032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation from a {@link android.graphics.Bitmap}.
19359c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
1936032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This Allocation will be created with {@link #USAGE_SHARED}, and
1937032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * {@link #USAGE_SCRIPT}.</p>
193845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
193945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
194045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b bitmap source for the allocation data
194145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
1942032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing bitmap data
194345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
194445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
194545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
194645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1947257cecb5e24c9af71ba4d621887e88e46b0ac411Stephen Hines                                USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
194845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
194945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
195045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
1951032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1952032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * containing the horizontal list of cube faces. Each face must be a square,
1953032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * have the same size as all other faces, and have a width that is a power
1954032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * of 2.
195545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
195645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
1957032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @param b Bitmap with cubemap faces layed out in the following
195845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *          format: right, left, top, bottom, front, back
195945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
196045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the cubemap is utilized
196145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
196245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
196345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
196445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
196545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
196645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     MipmapControl mips,
196745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     int usage) {
196845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        rs.validate();
196945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
197045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int height = b.getHeight();
197145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int width = b.getWidth();
197245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
197345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (width % 6 != 0) {
197445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
197545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
197645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (width / 6 != height) {
197745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
197845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
197945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
198045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (!isPow2) {
198145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
198245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
198345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
198445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Element e = elementFromBitmap(rs, b);
198545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
198645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setX(height);
198745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setY(height);
198845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setFaces(true);
198945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
199045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = tb.create();
199145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
1992bec39b6de685a7dddb1925c7e9f83fae20388de3Miao Wang        long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
199345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if(id == 0) {
199445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
199545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
199645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return new Allocation(id, rs, t, usage);
199745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
199845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
199945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2000032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
2001032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * from a {@link android.graphics.Bitmap} containing the horizontal list of
2002032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * cube faces. Each face must be a square, have the same size as all other
2003032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * faces, and have a width that is a power of 2.
200445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
200545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
200645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param b bitmap with cubemap faces layed out in the following
200745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *          format: right, left, top, bottom, front, back
200845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
200945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
201045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
201145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
201245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs,
201345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                     Bitmap b) {
201445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
201545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                       USAGE_GRAPHICS_TEXTURE);
201645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
201745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
201845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2019032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
2020032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * objects containing the cube faces. Each face must be a square, have the
2021032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * same size as all other faces, and have a width that is a power of 2.
202245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
202345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
202445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xpos cubemap face in the positive x direction
202545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xneg cubemap face in the negative x direction
202645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param ypos cubemap face in the positive y direction
202745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param yneg cubemap face in the negative y direction
202845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zpos cubemap face in the positive z direction
202945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zneg cubemap face in the negative z direction
203045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param mips specifies desired mipmap behaviour for the cubemap
203145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param usage bit field specifying how the cubemap is utilized
203245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
203345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
203445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
203545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
203645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
203745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xpos,
203845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xneg,
203945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap ypos,
204045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap yneg,
204145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zpos,
204245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zneg,
204345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        MipmapControl mips,
204445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        int usage) {
2045baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        /*
204645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        int height = xpos.getHeight();
204745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (xpos.getWidth() != height ||
204845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            xneg.getWidth() != height || xneg.getHeight() != height ||
204945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            ypos.getWidth() != height || ypos.getHeight() != height ||
205045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            yneg.getWidth() != height || yneg.getHeight() != height ||
205145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            zpos.getWidth() != height || zpos.getHeight() != height ||
205245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            zneg.getWidth() != height || zneg.getHeight() != height) {
205345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only square cube map faces supported");
205445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
205545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        boolean isPow2 = (height & (height - 1)) == 0;
205645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        if (!isPow2) {
205745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
205845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        }
205945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
206045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Element e = elementFromBitmap(rs, xpos);
206145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
206245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setX(height);
206345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setY(height);
206445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setFaces(true);
206545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
206645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Type t = tb.create();
206745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
206845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
206945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
207045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_X);
207145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(xpos);
207245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
207345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(xneg);
207445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
207545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(ypos);
207645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
207745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(yneg);
207845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
207945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(zpos);
208045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
208145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        adapter.copyFrom(zneg);
208245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
208345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return cubemap;
2084baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        */
2085baf4b4a16ff2ab85653438b93db9af03d4375f2dTim Murray        return null;
208645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
208745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
208845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    /**
2089032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a non-mipmapped cubemap Allocation for use as a sampler input
2090032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * from 6 {@link android.graphics.Bitmap} objects containing the cube
2091032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * faces. Each face must be a square, have the same size as all other faces,
2092032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * and have a width that is a power of 2.
209345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
209445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param rs Context to which the allocation will belong.
209545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xpos cubemap face in the positive x direction
209645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param xneg cubemap face in the negative x direction
209745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param ypos cubemap face in the positive y direction
209845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param yneg cubemap face in the negative y direction
209945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zpos cubemap face in the positive z direction
210045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @param zneg cubemap face in the negative z direction
210145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
210245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     * @return allocation containing cubemap data
210345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     *
210445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams     */
210545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
210645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xpos,
210745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap xneg,
210845d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap ypos,
210945d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap yneg,
211045d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zpos,
211145d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                                        Bitmap zneg) {
211245d443665f5ce7efa934706a89883f0cc87f3513Jason Sams        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
211345d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
211445d443665f5ce7efa934706a89883f0cc87f3513Jason Sams                                          USAGE_GRAPHICS_TEXTURE);
211545d443665f5ce7efa934706a89883f0cc87f3513Jason Sams    }
211645d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
21179c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
2118032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation from the Bitmap referenced
2119032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * by resource ID.
21209c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
21219c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param rs Context to which the allocation will belong.
21229c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param res application resources
21239c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param id resource id to load the data from
21249c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param mips specifies desired mipmap behaviour for the
21259c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *             allocation
21269c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param usage bit field specifying how the allocation is
21279c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *              utilized
21289c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
2129032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing resource data
21309c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
21319c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
21329c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
21339c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      Resources res,
21349c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      int id,
21359c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      MipmapControl mips,
21369c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      int usage) {
213745d443665f5ce7efa934706a89883f0cc87f3513Jason Sams
21389c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        rs.validate();
2139032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines        if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
2140032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines            throw new RSIllegalArgumentException("Unsupported usage specified.");
2141032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines        }
21429c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        Bitmap b = BitmapFactory.decodeResource(res, id);
21439c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        Allocation alloc = createFromBitmap(rs, b, mips, usage);
21449c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        b.recycle();
21459c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        return alloc;
21469c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
21479c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
21489c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
2149032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates a non-mipmapped Allocation to use as a graphics texture from the
2150032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * {@link android.graphics.Bitmap} referenced by resource ID.
2151032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     *
2152032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * <p>This allocation will be created with {@link #USAGE_SCRIPT} and
2153032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
21549c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
21559c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param rs Context to which the allocation will belong.
21569c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param res application resources
21579c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param id resource id to load the data from
21589c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
2159032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * @return Allocation containing resource data
21609c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
21619c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
21629c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    static public Allocation createFromBitmapResource(RenderScript rs,
21639c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      Resources res,
21649c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                                      int id) {
21659c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        return createFromBitmapResource(rs, res, id,
21669c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                        MipmapControl.MIPMAP_NONE,
2167032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines                                        USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
21689c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
21699c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray
21709c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    /**
2171032b2c2c8a3cf2c55f6f08557f2648d799766c4eStephen Hines     * Creates an Allocation containing string data encoded in UTF-8 format.
21729c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
21739c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param rs Context to which the allocation will belong.
21749c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param str string to create the allocation from
21759c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     * @param usage bit field specifying how the allocaiton is
21769c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *              utilized
21779c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     *
21789c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray     */
21799c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    static public Allocation createFromString(RenderScript rs,
21809c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                              String str,
21819c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray                                              int usage) {
21829c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        rs.validate();
21839c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        byte[] allocArray = null;
21849c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        try {
21859c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            allocArray = str.getBytes("UTF-8");
21869c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
21879c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            alloc.copyFrom(allocArray);
21889c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            return alloc;
21899c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
21909c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        catch (Exception e) {
21919c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray            throw new RSRuntimeException("Could not convert string to utf-8.");
21929c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray        }
21939c49e0a50bb720374cf9df2087ffd1ab3973f660Tim Murray    }
21946a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang
21956a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang    /**
21966f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     * Frees any native resources associated with this object.  The
21976f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     * primary use is to force immediate cleanup of resources when it is
21986f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang     * believed the GC will not respond quickly enough.
21996a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang     * For USAGE_IO_OUTPUT, destroy() implies setSurface(null).
22006a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang     */
22016a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang    @Override
22026a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang    public void destroy() {
22036f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        if (mIncCompatAllocation != 0) {
22046f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            boolean shouldDestroy = false;
22056f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            synchronized(this) {
22066f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                if (!mIncAllocDestroyed) {
22076f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                    shouldDestroy = true;
22086f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                    mIncAllocDestroyed = true;
22096f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                }
22106f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            }
22116f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang
22126f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            if (shouldDestroy) {
22136f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                // must include nObjDestroy in the critical section
22146f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                ReentrantReadWriteLock.ReadLock rlock = mRS.mRWLock.readLock();
22156f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                rlock.lock();
22166f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                if(mRS.isAlive()) {
22176f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                    mRS.nIncObjDestroy(mIncCompatAllocation);
22186f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                }
22196f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                rlock.unlock();
22206f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang                mIncCompatAllocation = 0;
22216f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang            }
22226f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        }
22236f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang        if ((mUsage & (USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
22246a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang            setSurface(null);
22256a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang        }
22266a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang        super.destroy();
22276a9c942955a1a316f35f71c6f4088a6fb70d187cMiao Wang    }
22286f5555db1af436bb5aad430e6e00aa5b69d5ca6cMiao Wang
2229dfe51945d97812921083af3693deaa4bde6ab11fStephen Hines}
2230302bdd023dfccd7c63c429be4b6edb46314b6293Jason Sams
2231