1b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams/*
29069ee88cb62a62395e895eb6b3333a1dfcead73Stephen Hines * Copyright (C) 2008-2012 The Android Open Source Project
3b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams *
4b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * you may not use this file except in compliance with the License.
6b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * You may obtain a copy of the License at
7b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams *
8b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams *
10b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * Unless required by applicable law or agreed to in writing, software
11b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * See the License for the specific language governing permissions and
14b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * limitations under the License.
15b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams */
16b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
17b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samspackage android.renderscript;
18b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
19739c8263a10d34f5acba7fce2052012d1545d10bJason Samsimport java.util.HashMap;
20b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.content.res.Resources;
21b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.graphics.Bitmap;
22b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.graphics.BitmapFactory;
23fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Samsimport android.view.Surface;
24b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.util.Log;
25abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murrayimport android.graphics.Canvas;
266d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murrayimport android.os.Trace;
27b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
289c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines/**
29c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * <p> This class provides the primary method through which data is passed to
30c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * and from RenderScript kernels.  An Allocation provides the backing store for
31c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * a given {@link android.renderscript.Type}.  </p>
32a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams *
33c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * <p>An Allocation also contains a set of usage flags that denote how the
34c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * Allocation could be used. For example, an Allocation may have usage flags
35c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * specifying that it can be used from a script as well as input to a {@link
36c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * android.renderscript.Sampler}. A developer must synchronize across these
37c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * different usages using {@link android.renderscript.Allocation#syncAll} in
38c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * order to ensure that different users of the Allocation have a consistent view
39c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * of memory. For example, in the case where an Allocation is used as the output
40c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * of one kernel and as Sampler input in a later kernel, a developer must call
41c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
42c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * second kernel to ensure correctness.
43a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams *
44c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * <p>An Allocation can be populated with the {@link #copyFrom} routines. For
45c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * more complex Element types, the {@link #copyFromUnchecked} methods can be
46c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * used to copy from byte arrays or similar constructs.</p>
47b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams *
483aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
493aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
50c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * <p>For more information about creating an application that uses RenderScript, read the
51c11e25c4e653124def1fb18e203b894f42106cbeTim Murray * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
523aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
53b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams **/
54b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samspublic class Allocation extends BaseObj {
5543ee06857bb7f99446d1d84f8789016c5d105558Jason Sams    Type mType;
568a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    Bitmap mBitmap;
575476b450e50939940dcf3f15c92335cee2fc572dJason Sams    int mUsage;
58ba862d1544a06528151550be1784a926ee986580Jason Sams    Allocation mAdaptedAllocation;
592f2472c42be8e80f00019594036744d902e09dcbTim Murray    int mSize;
60ba862d1544a06528151550be1784a926ee986580Jason Sams
61ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedLOD;
62ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedFace;
63ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedY;
64ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedZ;
65615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams    boolean mReadAllowed = true;
66615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams    boolean mWriteAllowed = true;
67ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedY;
68ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedZ;
69ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedLOD;
70ba862d1544a06528151550be1784a926ee986580Jason Sams    Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
71ba862d1544a06528151550be1784a926ee986580Jason Sams
72ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimX;
73ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimY;
74ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimZ;
75ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentCount;
76460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray    static HashMap<Long, Allocation> mAllocationMap =
77460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray            new HashMap<Long, Allocation>();
7842ef238208679813daa204bf110f776ab02c2bebJason Sams    OnBufferAvailableListener mBufferNotifier;
79ba862d1544a06528151550be1784a926ee986580Jason Sams
803042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
813042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        final Class c = d.getClass();
823042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (!c.isArray()) {
833042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
843042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
853042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        final Class cmp = c.getComponentType();
863042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (!cmp.isPrimitive()) {
873042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
883042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
893042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
903042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (cmp == Long.TYPE) {
913042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            if (checkType) {
923042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                validateIsInt64();
933042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                return mType.mElement.mType;
943042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            }
953042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            return Element.DataType.SIGNED_64;
963042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
973042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
983042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (cmp == Integer.TYPE) {
993042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            if (checkType) {
1003042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                validateIsInt32();
1013042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                return mType.mElement.mType;
1023042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            }
1033042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            return Element.DataType.SIGNED_32;
1043042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
1053042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
1063042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (cmp == Short.TYPE) {
1073042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            if (checkType) {
1083042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                validateIsInt16();
1093042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                return mType.mElement.mType;
1103042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            }
1113042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            return Element.DataType.SIGNED_16;
1123042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
1133042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
1143042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (cmp == Byte.TYPE) {
1153042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            if (checkType) {
1163042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                validateIsInt8();
1173042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                return mType.mElement.mType;
1183042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            }
1193042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            return Element.DataType.SIGNED_8;
1203042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
1213042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
1223042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (cmp == Float.TYPE) {
1233042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            if (checkType) {
1243042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                validateIsFloat32();
1253042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            }
1263042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            return Element.DataType.FLOAT_32;
1273042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
1283042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
1293042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (cmp == Double.TYPE) {
1303042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            if (checkType) {
1313042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                validateIsFloat64();
1323042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            }
1333042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            return Element.DataType.FLOAT_64;
1343042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
1353042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        return null;
1363042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
1373042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
1383042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
1399c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
140c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * The usage of the Allocation.  These signal to RenderScript where to place
141c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * the Allocation in memory.
142f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
143c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     */
144c11e25c4e653124def1fb18e203b894f42106cbeTim Murray
145c11e25c4e653124def1fb18e203b894f42106cbeTim Murray    /**
146c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * The Allocation will be bound to and accessed by scripts.
147f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1485476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_SCRIPT = 0x0001;
149f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
1509c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
151c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * The Allocation will be used as a texture source by one or more graphics
152c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * programs.
153f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
154f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1555476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
156f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
1579c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
158c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * The Allocation will be used as a graphics mesh.
159c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     *
160c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * This was deprecated in API level 16.
161f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
162f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1635476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
164f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
165f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
1669c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
167c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * The Allocation will be used as the source of shader constants by one or
168c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * more programs.
169c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     *
170c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * This was deprecated in API level 16.
171f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
172f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1735476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
1745476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1759c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
176c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * The Allocation will be used as a target for offscreen rendering
177c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     *
178c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * This was deprecated in API level 16.
1798e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     *
1808e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     */
1818e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk    public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
1828e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk
1839c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1843a1b8e41162e3a01627b077aab2599353a07779dJason Sams     * The Allocation will be used as a {@link android.view.Surface}
1853a1b8e41162e3a01627b077aab2599353a07779dJason Sams     * consumer.  This usage will cause the Allocation to be created
1863a1b8e41162e3a01627b077aab2599353a07779dJason Sams     * as read-only.
187615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     *
188615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     */
189fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams    public static final int USAGE_IO_INPUT = 0x0020;
1909069ee88cb62a62395e895eb6b3333a1dfcead73Stephen Hines
1919c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1923a1b8e41162e3a01627b077aab2599353a07779dJason Sams     * The Allocation will be used as a {@link android.view.Surface}
193c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * producer.  The dimensions and format of the {@link
1943a1b8e41162e3a01627b077aab2599353a07779dJason Sams     * android.view.Surface} will be forced to those of the
195c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Allocation.
196615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     *
197615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     */
198fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams    public static final int USAGE_IO_OUTPUT = 0x0040;
19943ee06857bb7f99446d1d84f8789016c5d105558Jason Sams
2009c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
201c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * The Allocation's backing store will be inherited from another object
202c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * (usually a {@link android.graphics.Bitmap}); copying to or from the
203c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * original source Bitmap will cause a synchronization rather than a full
204c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * copy.  {@link #syncAll} may also be used to synchronize the Allocation
205c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and the source Bitmap.
20600bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray     *
207c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * <p>This is set by default for allocations created with {@link
208c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * #createFromBitmap} in API version 18 and higher.</p>
20900bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray     *
21000bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray     */
21100bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray    public static final int USAGE_SHARED = 0x0080;
21200bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray
21300bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray    /**
214c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Controls mipmap behavior when using the bitmap creation and update
215c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * functions.
216f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
2174ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public enum MipmapControl {
2189c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
219c11e25c4e653124def1fb18e203b894f42106cbeTim Murray         * No mipmaps will be generated and the type generated from the incoming
220c11e25c4e653124def1fb18e203b894f42106cbeTim Murray         * bitmap will not contain additional LODs.
221f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
2225476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_NONE(0),
223f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
2249c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
225c11e25c4e653124def1fb18e203b894f42106cbeTim Murray         * A full mipmap chain will be created in script memory.  The Type of
226c11e25c4e653124def1fb18e203b894f42106cbeTim Murray         * the Allocation will contain a full mipmap chain.  On upload, the full
227c11e25c4e653124def1fb18e203b894f42106cbeTim Murray         * chain will be transferred.
228f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
2295476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_FULL(1),
230f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
2319c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
232c11e25c4e653124def1fb18e203b894f42106cbeTim Murray         * The Type of the Allocation will be the same as MIPMAP_NONE.  It will
233c11e25c4e653124def1fb18e203b894f42106cbeTim Murray         * not contain mipmaps.  On upload, the allocation data will contain a
234c11e25c4e653124def1fb18e203b894f42106cbeTim Murray         * full mipmap chain generated from the top level in script memory.
235f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
2365476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
2375476b450e50939940dcf3f15c92335cee2fc572dJason Sams
2385476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int mID;
2394ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl(int id) {
2405476b450e50939940dcf3f15c92335cee2fc572dJason Sams            mID = id;
2415476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
242b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
243b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
24448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams
245460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray    private long getIDSafe() {
24648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        if (mAdaptedAllocation != null) {
247e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams            return mAdaptedAllocation.getID(mRS);
24848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        }
249e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        return getID(mRS);
25048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    }
25148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams
25203d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams
2539c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines   /**
254c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Get the {@link android.renderscript.Element} of the {@link
255c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * android.renderscript.Type} of the Allocation.
25603d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
257c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @return Element
25803d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
25903d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
26003d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams    public Element getElement() {
26103d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams        return mType.getElement();
26203d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams    }
26303d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams
2649c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
26503d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     * Get the usage flags of the Allocation.
26603d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
267c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @return usage this Allocation's set of the USAGE_* flags OR'd together
26803d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
26903d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
27003d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams    public int getUsage() {
27103d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams        return mUsage;
27203d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams    }
27303d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams
2749c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
27536c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     * Get the size of the Allocation in bytes.
27636c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     *
277918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return size of the Allocation in bytes.
27836c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     *
27936c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     */
280918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk    public int getBytesSize() {
28104f0d6e3394ed8518db51307a9b66f9b44477a46Tim Murray        if (mType.mDimYuv != 0) {
28204f0d6e3394ed8518db51307a9b66f9b44477a46Tim Murray            return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
28304f0d6e3394ed8518db51307a9b66f9b44477a46Tim Murray        }
284918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        return mType.getCount() * mType.getElement().getBytesSize();
28536c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams    }
28636c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams
287452a7661e8b06459b75493b441d33244939c1153Jason Sams    private void updateCacheInfo(Type t) {
288452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimX = t.getX();
289452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimY = t.getY();
290452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimZ = t.getZ();
291452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentCount = mCurrentDimX;
292452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (mCurrentDimY > 1) {
293452a7661e8b06459b75493b441d33244939c1153Jason Sams            mCurrentCount *= mCurrentDimY;
294452a7661e8b06459b75493b441d33244939c1153Jason Sams        }
295452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (mCurrentDimZ > 1) {
296452a7661e8b06459b75493b441d33244939c1153Jason Sams            mCurrentCount *= mCurrentDimZ;
297452a7661e8b06459b75493b441d33244939c1153Jason Sams        }
298452a7661e8b06459b75493b441d33244939c1153Jason Sams    }
299ba862d1544a06528151550be1784a926ee986580Jason Sams
300a314551d69098537337c970da615a8f8af58e1f1Tim Murray    private void setBitmap(Bitmap b) {
301a314551d69098537337c970da615a8f8af58e1f1Tim Murray        mBitmap = b;
302a314551d69098537337c970da615a8f8af58e1f1Tim Murray    }
303a314551d69098537337c970da615a8f8af58e1f1Tim Murray
304460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray    Allocation(long id, RenderScript rs, Type t, int usage) {
3050de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
30649a05d7b82956009f03acbb92a064eed054eb031Jason Sams        if ((usage & ~(USAGE_SCRIPT |
30749a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_TEXTURE |
30849a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_VERTEX |
3098e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk                       USAGE_GRAPHICS_CONSTANTS |
310615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                       USAGE_GRAPHICS_RENDER_TARGET |
311615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                       USAGE_IO_INPUT |
31200bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray                       USAGE_IO_OUTPUT |
31300bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray                       USAGE_SHARED)) != 0) {
3145476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
3155476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
316615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams
317fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams        if ((usage & USAGE_IO_INPUT) != 0) {
318615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams            mWriteAllowed = false;
319615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams
320fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams            if ((usage & ~(USAGE_IO_INPUT |
321615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                           USAGE_GRAPHICS_TEXTURE |
322615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                           USAGE_SCRIPT)) != 0) {
323615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                throw new RSIllegalArgumentException("Invalid usage combination.");
324615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams            }
325615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams        }
3269bf189228fdb0ec14b284f8bd543d5f9137997ccJason Sams
3275476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mType = t;
328615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams        mUsage = usage;
329ba862d1544a06528151550be1784a926ee986580Jason Sams
330452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (t != null) {
33188990da58d0231b5a1b9a27b32c37839e95c7487Stephen Hines            // TODO: A3D doesn't have Type info during creation, so we can't
33288990da58d0231b5a1b9a27b32c37839e95c7487Stephen Hines            // calculate the size ahead of time. We can possibly add a method
33388990da58d0231b5a1b9a27b32c37839e95c7487Stephen Hines            // to update the size in the future if it seems reasonable.
33488990da58d0231b5a1b9a27b32c37839e95c7487Stephen Hines            mSize = mType.getCount() * mType.getElement().getBytesSize();
335452a7661e8b06459b75493b441d33244939c1153Jason Sams            updateCacheInfo(t);
336ba862d1544a06528151550be1784a926ee986580Jason Sams        }
3372f2472c42be8e80f00019594036744d902e09dcbTim Murray        try {
3382f2472c42be8e80f00019594036744d902e09dcbTim Murray            RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
3392f2472c42be8e80f00019594036744d902e09dcbTim Murray        } catch (Exception e) {
3402f2472c42be8e80f00019594036744d902e09dcbTim Murray            Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
3412f2472c42be8e80f00019594036744d902e09dcbTim Murray            throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
3422f2472c42be8e80f00019594036744d902e09dcbTim Murray        }
3432f2472c42be8e80f00019594036744d902e09dcbTim Murray    }
3442f2472c42be8e80f00019594036744d902e09dcbTim Murray
3452f2472c42be8e80f00019594036744d902e09dcbTim Murray    protected void finalize() throws Throwable {
3462f2472c42be8e80f00019594036744d902e09dcbTim Murray        RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
3472f2472c42be8e80f00019594036744d902e09dcbTim Murray        super.finalize();
34880a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk    }
34980a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk
3503042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    private void validateIsInt64() {
3513042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
3523042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
3533042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            return;
3543042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
3553042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        throw new RSIllegalArgumentException(
3563042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            "64 bit integer source does not match allocation type " + mType.mElement.mType);
3573042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
3583042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
359b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt32() {
360b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
361b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
362b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
363b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
364b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
365b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
366b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
367b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
368b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt16() {
369b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
370b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
371b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
372b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
373b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
374b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
375b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
376b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
377b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt8() {
378b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
379b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
380b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
381b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
382b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
383b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
384b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
385b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
386b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsFloat32() {
387b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
388b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
389b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
390b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
391b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
392b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
393b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
3943042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    private void validateIsFloat64() {
3953042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_64) {
3963042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            return;
3973042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
3983042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        throw new RSIllegalArgumentException(
3993042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            "64 bit float source does not match allocation type " + mType.mElement.mType);
4003042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
4013042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
402b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsObject() {
403b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
404b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
405b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
406b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
407b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
408b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_MESH) ||
409b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
410b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
411b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
412b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
413b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
414b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
415b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
416b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
417b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
418b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
419dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    @Override
420dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    void updateFromNative() {
42106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        super.updateFromNative();
422460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray        long typeID = mRS.nAllocationGetType(getID(mRS));
423dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        if(typeID != 0) {
424dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType = new Type(typeID, mRS);
425dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType.updateFromNative();
426ad37cb26cd8d8a05077152ebc5b841a5629cfbbdJason Sams            updateCacheInfo(mType);
427dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        }
428dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    }
429dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk
4309c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
431c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Get the {@link android.renderscript.Type} of the Allocation.
43203d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
43303d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     * @return Type
43403d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
43503d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
436ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    public Type getType() {
437ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams        return mType;
438ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    }
439ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams
4409c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
441c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Propagate changes from one usage of the Allocation to the
442c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * other usages of the Allocation.
44303d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
44403d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
4455476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public void syncAll(int srcLocation) {
4466d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
4475476b450e50939940dcf3f15c92335cee2fc572dJason Sams        switch (srcLocation) {
44878e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray        case USAGE_GRAPHICS_TEXTURE:
4495476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_SCRIPT:
45078e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray            if ((mUsage & USAGE_SHARED) != 0) {
45178e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray                copyFrom(mBitmap);
45278e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray            }
45378e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray            break;
4545476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_CONSTANTS:
4555476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_VERTEX:
4565476b450e50939940dcf3f15c92335cee2fc572dJason Sams            break;
45778e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray        case USAGE_SHARED:
45878e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray            if ((mUsage & USAGE_SHARED) != 0) {
45978e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray                copyTo(mBitmap);
46078e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray            }
46178e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray            break;
4625476b450e50939940dcf3f15c92335cee2fc572dJason Sams        default:
4635476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
4645476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
4655476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.validate();
46648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
4676d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
4685476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
4695476b450e50939940dcf3f15c92335cee2fc572dJason Sams
4709c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
471c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Send a buffer to the output stream.  The contents of the Allocation will
472c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * be undefined after this operation. This operation is only valid if {@link
473c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * #USAGE_IO_OUTPUT} is set on the Allocation.
474c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     *
475163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     *
476163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     */
477c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams    public void ioSend() {
4786d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
479163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
480163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams            throw new RSIllegalArgumentException(
481163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams                "Can only send buffer if IO_OUTPUT usage specified.");
482163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        }
483163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        mRS.validate();
484e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationIoSend(getID(mRS));
4856d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
486163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams    }
487163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams
4889c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
489c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Receive the latest input into the Allocation. This operation
490c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
491163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     *
492163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     */
493c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams    public void ioReceive() {
4946d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
495163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        if ((mUsage & USAGE_IO_INPUT) == 0) {
496163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams            throw new RSIllegalArgumentException(
497fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams                "Can only receive if IO_INPUT usage specified.");
498163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        }
499163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        mRS.validate();
500e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationIoReceive(getID(mRS));
5016d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
502163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams    }
503163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams
5049c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
505c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an array of RS objects to the Allocation.
50603d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
50703d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     * @param d Source array.
50803d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
509bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(BaseObj[] d) {
5106d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
511bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        mRS.validate();
512b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsObject();
513ba862d1544a06528151550be1784a926ee986580Jason Sams        if (d.length != mCurrentCount) {
514bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
515ba862d1544a06528151550be1784a926ee986580Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
516bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
517460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray
5183de3dc79e569bac3205e9af1ffe726964b845358Tim Murray        if (RenderScript.sPointerSize == 8) {
5193de3dc79e569bac3205e9af1ffe726964b845358Tim Murray            long i[] = new long[d.length * 4];
5203de3dc79e569bac3205e9af1ffe726964b845358Tim Murray            for (int ct=0; ct < d.length; ct++) {
5213de3dc79e569bac3205e9af1ffe726964b845358Tim Murray                i[ct * 4] = d[ct].getID(mRS);
5223de3dc79e569bac3205e9af1ffe726964b845358Tim Murray            }
5233de3dc79e569bac3205e9af1ffe726964b845358Tim Murray            copy1DRangeFromUnchecked(0, mCurrentCount, i);
5243de3dc79e569bac3205e9af1ffe726964b845358Tim Murray        } else {
5253de3dc79e569bac3205e9af1ffe726964b845358Tim Murray            int i[] = new int[d.length];
5263de3dc79e569bac3205e9af1ffe726964b845358Tim Murray            for (int ct=0; ct < d.length; ct++) {
5273de3dc79e569bac3205e9af1ffe726964b845358Tim Murray                i[ct] = (int)d[ct].getID(mRS);
5283de3dc79e569bac3205e9af1ffe726964b845358Tim Murray            }
5293de3dc79e569bac3205e9af1ffe726964b845358Tim Murray            copy1DRangeFromUnchecked(0, mCurrentCount, i);
530bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
5316d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
532bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    }
533bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams
534fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapFormat(Bitmap b) {
535252c07802f7039f15f723751162e64a6621e6998Jason Sams        Bitmap.Config bc = b.getConfig();
536abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray        if (bc == null) {
537abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
538abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray        }
539252c07802f7039f15f723751162e64a6621e6998Jason Sams        switch (bc) {
540252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ALPHA_8:
541252c07802f7039f15f723751162e64a6621e6998Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
542252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
543252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
544252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
545918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                                                     " of " + mType.getElement().getBytesSize() +
546252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
547252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
548252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
549252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_8888:
550252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
551918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                (mType.getElement().getBytesSize() != 4)) {
552252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
553252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
554252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
555918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                                                     " of " + mType.getElement().getBytesSize() +
556252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
557252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
558252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
559252c07802f7039f15f723751162e64a6621e6998Jason Sams        case RGB_565:
560252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
561918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                (mType.getElement().getBytesSize() != 2)) {
562252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
563252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
564252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
565918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                                                     " of " + mType.getElement().getBytesSize() +
566252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
567252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
568252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
569252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_4444:
570252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
571918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                (mType.getElement().getBytesSize() != 2)) {
572252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
573252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
574252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
575918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                                                     " of " + mType.getElement().getBytesSize() +
576252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
577252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
578252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
579252c07802f7039f15f723751162e64a6621e6998Jason Sams
580252c07802f7039f15f723751162e64a6621e6998Jason Sams        }
5814ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
5824ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
583fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapSize(Bitmap b) {
584ba862d1544a06528151550be1784a926ee986580Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
585fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
586fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
587fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
588fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams
5893042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
5903042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
5913042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        mRS.validate();
5923042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        if (mCurrentDimZ > 0) {
5933042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
5943042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        } else if (mCurrentDimY > 0) {
5953042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
5963042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        } else {
5973042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams            copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
5983042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        }
5993042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceEnd(RenderScript.TRACE_TAG);
6003042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
6013042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
6029c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
603c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy into this Allocation from an array. This method does not guarantee
604c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * that the Allocation is compatible with the input buffer; it copies memory
605c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * without reinterpretation.
6064fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6073042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param array The source data array
6084fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
6093042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    public void copyFromUnchecked(Object array) {
6106d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
6113042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
6123042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                          java.lang.reflect.Array.getLength(array));
6136d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
6144fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
6156d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray
6169c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
617c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy into this Allocation from an array. This method does not guarantee
618c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * that the Allocation is compatible with the input buffer; it copies memory
619c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * without reinterpretation.
6204fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6214fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6224fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
6233042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    public void copyFromUnchecked(int[] d) {
6243042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
6253042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
6263042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
6273042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    /**
6283042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * Copy into this Allocation from an array. This method does not guarantee
6293042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * that the Allocation is compatible with the input buffer; it copies memory
6303042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * without reinterpretation.
6313042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     *
6323042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param d the source data array
6333042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     */
6344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(short[] d) {
6353042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
6364fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
6376d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray
6389c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
639c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy into this Allocation from an array. This method does not guarantee
640c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * that the Allocation is compatible with the input buffer; it copies memory
641c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * without reinterpretation.
6424fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6434fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6444fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
6454fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(byte[] d) {
6463042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
6474fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
6486d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray
6499c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
650c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy into this Allocation from an array. This method does not guarantee
651c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * that the Allocation is compatible with the input buffer; it copies memory
652c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * without reinterpretation.
6534fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6544fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6554fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
6564fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(float[] d) {
6573042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
6584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
6594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
6606d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray
6619c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
662c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy into this Allocation from an array.  This variant is type checked
663c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and will generate exceptions if the Allocation's {@link
6643042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * android.renderscript.Element} does not match the array's
6653042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * primitive type.
6664fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6671622981ca44693152e3f95e93a8e22a2216f65a1Ying Wang     * @param array The source data array
6684fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
6693042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    public void copyFrom(Object array) {
6706d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
6713042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
6723042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                          java.lang.reflect.Array.getLength(array));
6736d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
674768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
6754fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
6769c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
677c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy into this Allocation from an array.  This variant is type checked
678c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and will generate exceptions if the Allocation's {@link
6793042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * android.renderscript.Element} is not a 32 bit integer type.
6803042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     *
6813042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param d the source data array
6823042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     */
6833042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    public void copyFrom(int[] d) {
6843042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        validateIsInt32();
6853042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
6863042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
6873042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
6883042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    /**
6893042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * Copy into this Allocation from an array.  This variant is type checked
6903042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * and will generate exceptions if the Allocation's {@link
691c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * android.renderscript.Element} is not a 16 bit integer type.
6924fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6934fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6944fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
695bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(short[] d) {
6963042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        validateIsInt16();
6973042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
698768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
6994fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
7009c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
701c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy into this Allocation from an array.  This variant is type checked
702c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and will generate exceptions if the Allocation's {@link
703c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * android.renderscript.Element} is not an 8 bit integer type.
7044fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
7054fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
7064fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
707bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(byte[] d) {
7083042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        validateIsInt8();
7093042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
710b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
7114fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
7129c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
713c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy into this Allocation from an array.  This variant is type checked
714c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and will generate exceptions if the Allocation's {@link
715c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * android.renderscript.Element} is not a 32 bit float type.
7164fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
7174fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
7184fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
719bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(float[] d) {
7203042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        validateIsFloat32();
7213042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
722b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
7234fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
7249c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
725c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy into an Allocation from a {@link android.graphics.Bitmap}.  The
726c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * height, width, and format of the bitmap must match the existing
727c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * allocation.
728c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     *
729c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * <p>If the {@link android.graphics.Bitmap} is the same as the {@link
730c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * android.graphics.Bitmap} used to create the Allocation with {@link
731c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
732c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * this will synchronize the Allocation with the latest data from the {@link
733c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
7344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
7354fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param b the source bitmap
7364fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
737bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(Bitmap b) {
7386d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
739fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
740abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray        if (b.getConfig() == null) {
741abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
742abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            Canvas c = new Canvas(newBitmap);
743abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            c.drawBitmap(b, 0, 0, null);
744abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            copyFrom(newBitmap);
745abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            return;
746abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray        }
747fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
748fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
749e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationCopyFromBitmap(getID(mRS), b);
7506d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
7514ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
75226ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
7539c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
754c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an Allocation from an Allocation.  The types of both allocations
755f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray     * must be identical.
756f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray     *
757f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray     * @param a the source allocation
758f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray     */
759f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray    public void copyFrom(Allocation a) {
7606d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
761f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray        mRS.validate();
762f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray        if (!mType.equals(a.getType())) {
763f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray            throw new RSIllegalArgumentException("Types of allocations must match.");
764f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray        }
765f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray        copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
7666d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
767f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray    }
768f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray
769f671fb0b35e945e5fc1416603e3b9a1c61d9dab9Tim Murray    /**
770c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * This is only intended to be used by auto-generated code reflected from
771c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * the RenderScript script files and should not be used by developers.
772fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
773fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
774fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
775fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
77621b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
777f70b0fc880edb7c47d5bcc97edbc125b575bc90dJason Sams        mRS.validate();
778918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        int eSize = mType.mElement.getBytesSize();
779a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        final byte[] data = fp.getData();
780fa1275a6e743c4661cc3d5e05bb89559a8e59727Stephen Hines        int data_length = fp.getPos();
781a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
782fa1275a6e743c4661cc3d5e05bb89559a8e59727Stephen Hines        int count = data_length / eSize;
783fa1275a6e743c4661cc3d5e05bb89559a8e59727Stephen Hines        if ((eSize * count) != data_length) {
784fa1275a6e743c4661cc3d5e05bb89559a8e59727Stephen Hines            throw new RSIllegalArgumentException("Field packer length " + data_length +
785a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               " not divisible by element size " + eSize + ".");
786a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
787ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
78849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    }
78949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
7909c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
791c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * This is only intended to be used by auto-generated code reflected from
792c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * the RenderScript script files.
793fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
794fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
795fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param component_number
796fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
797fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
79821b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
799f70b0fc880edb7c47d5bcc97edbc125b575bc90dJason Sams        mRS.validate();
80049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (component_number >= mType.mElement.mElements.length) {
80106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
80249bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
80349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if(xoff < 0) {
80406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
80549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
80649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
80749bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        final byte[] data = fp.getData();
808fa1275a6e743c4661cc3d5e05bb89559a8e59727Stephen Hines        int data_length = fp.getPos();
809918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        int eSize = mType.mElement.mElements[component_number].getBytesSize();
810bf3c3f2ab1067b647038170cb201cdd1a9a1c6d2Alex Sakhartchouk        eSize *= mType.mElement.mArraySizes[component_number];
81149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
812fa1275a6e743c4661cc3d5e05bb89559a8e59727Stephen Hines        if (data_length != eSize) {
813fa1275a6e743c4661cc3d5e05bb89559a8e59727Stephen Hines            throw new RSIllegalArgumentException("Field packer sizelength " + data_length +
81449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams                                               " does not match component size " + eSize + ".");
81549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
81649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
81748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
818fa1275a6e743c4661cc3d5e05bb89559a8e59727Stephen Hines                                     component_number, data, data_length);
819a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams    }
820a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
821768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
822771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
823a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(off < 0) {
82406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
825a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
826a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(count < 1) {
82706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
828a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
829ba862d1544a06528151550be1784a926ee986580Jason Sams        if((off + count) > mCurrentCount) {
830ba862d1544a06528151550be1784a926ee986580Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
831a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               ", got " + count + " at offset " + off + ".");
832768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
833ba862d1544a06528151550be1784a926ee986580Jason Sams        if(len < dataSize) {
83406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
835768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
836b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
837b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
8389c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
839c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Generate a mipmap chain. This is only valid if the Type of the Allocation
840c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * includes mipmaps.
841f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
842c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * <p>This function will generate a complete set of mipmaps from the top
843c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * level LOD and place them into the script memory space.</p>
844f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
845c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * <p>If the Allocation is also using other memory spaces, a call to {@link
846c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
847f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
848f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void generateMipmaps() {
849e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationGenerateMipmaps(getID(mRS));
850f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    }
851f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
8523042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    private void copy1DRangeFromUnchecked(int off, int count, Object array,
8533042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                          Element.DataType dt, int arrayLen) {
8543042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
8553042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        final int dataSize = mType.mElement.getBytesSize() * count;
8563042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        data1DChecks(off, count, arrayLen * dt.mSize, dataSize);
8573042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt);
8583042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceEnd(RenderScript.TRACE_TAG);
8593042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
8603042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
8613042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    /**
8623042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * Copy an array into part of this Allocation.  This method does not
8633042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * guarantee that the Allocation is compatible with the input buffer.
8643042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     *
8653042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param off The offset of the first element to be copied.
8663042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param count The number of elements to be copied.
8673042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param array The source data array
8683042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     */
8693042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    public void copy1DRangeFromUnchecked(int off, int count, Object array) {
8703042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, array,
8713042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 validateObjectIsPrimitiveArray(array, false),
8723042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 java.lang.reflect.Array.getLength(array));
8733042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
8743042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
8759c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
876c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an array into part of this Allocation.  This method does not
877c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * guarantee that the Allocation is compatible with the input buffer.
8784fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
8794fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
8804fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
8814fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
8824fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
8834fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
8843042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
885768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
8866d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray
8879c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
888c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an array into part of this Allocation.  This method does not
889c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * guarantee that the Allocation is compatible with the input buffer.
8904fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
8914fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
8924fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
8934fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
8944fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
8954fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
8963042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
897768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
8986d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray
8999c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
900c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an array into part of this Allocation.  This method does not
901c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * guarantee that the Allocation is compatible with the input buffer.
9024fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
9034fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
9044fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
9054fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
9064fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
9074fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
9083042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
909768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
9106d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray
9119c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
912c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an array into part of this Allocation.  This method does not
913c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * guarantee that the Allocation is compatible with the input buffer.
9144fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
9154fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
9164fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
9174fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
9184fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
9194fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
9203042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
9213042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
9223042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
9233042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
9243042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    /**
9253042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * Copy an array into part of this Allocation.  This variant is type checked
9263042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * and will generate exceptions if the Allocation type does not
9273042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * match the component type of the array passed in.
9283042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     *
9293042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param off The offset of the first element to be copied.
9303042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param count The number of elements to be copied.
9313042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param array The source data array.
9323042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     */
9333042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    public void copy1DRangeFrom(int off, int count, Object array) {
9343042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, array,
9353042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 validateObjectIsPrimitiveArray(array, true),
9363042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 java.lang.reflect.Array.getLength(array));
937b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
938b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
9399c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
940c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an array into part of this Allocation.  This variant is type checked
941c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and will generate exceptions if the Allocation type is not a 32 bit
942c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * integer type.
9434fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
9444fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
9454fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
9464fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
9474fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
948b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
949b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
9503042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
951b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
9524fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
9539c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
954c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an array into part of this Allocation.  This variant is type checked
955c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and will generate exceptions if the Allocation type is not a 16 bit
956c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * integer type.
9574fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
9584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
9594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
9604fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
9614fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
962b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
963b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
9643042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
965b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
9664fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
9679c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
968c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an array into part of this Allocation.  This variant is type checked
969c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and will generate exceptions if the Allocation type is not an 8 bit
970c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * integer type.
9714fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
9724fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
9734fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
9744fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
9754fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
976b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
977b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
9783042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
979b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
9804fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
9819c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
982c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy an array into part of this Allocation.  This variant is type checked
983c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and will generate exceptions if the Allocation type is not a 32 bit float
984c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * type.
9854fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
9864fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
9874fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
988304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param d the source data array.
9894fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
990b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
991b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
9923042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
993b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
9943042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
9959c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines     /**
996c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy part of an Allocation into this Allocation.
997304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
998304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param off The offset of the first element to be copied.
999304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param count The number of elements to be copied.
1000304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param data the source data allocation.
1001304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataOff off The offset of the first element in data to
1002304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *          be copied.
1003304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
1004304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
10056d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
100648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
1007ba862d1544a06528151550be1784a926ee986580Jason Sams                              mSelectedLOD, mSelectedFace.mID,
1008e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                              count, 1, data.getID(mRS), dataOff, 0,
1009ba862d1544a06528151550be1784a926ee986580Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
1010304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
1011304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
1012fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
1013ba862d1544a06528151550be1784a926ee986580Jason Sams        if (mAdaptedAllocation != null) {
1014ba862d1544a06528151550be1784a926ee986580Jason Sams
1015ba862d1544a06528151550be1784a926ee986580Jason Sams        } else {
1016ba862d1544a06528151550be1784a926ee986580Jason Sams
1017ba862d1544a06528151550be1784a926ee986580Jason Sams            if (xoff < 0 || yoff < 0) {
1018ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
1019ba862d1544a06528151550be1784a926ee986580Jason Sams            }
1020ba862d1544a06528151550be1784a926ee986580Jason Sams            if (h < 0 || w < 0) {
1021ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
1022ba862d1544a06528151550be1784a926ee986580Jason Sams            }
1023ba862d1544a06528151550be1784a926ee986580Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
1024ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
1025ba862d1544a06528151550be1784a926ee986580Jason Sams            }
1026fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
1027fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
1028768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
10293042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
10303042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                  Element.DataType dt, int arrayLen) {
10316d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
1032771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
1033fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
10343042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
10353042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                              array, arrayLen * dt.mSize, dt);
10366d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
1037b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
1038b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
10393042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    /**
10403042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * Copy from an array into a rectangular region in this Allocation.  The
10413042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * array is assumed to be tightly packed.
10423042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     *
10433042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param xoff X offset of the region to update in this Allocation
10443042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param yoff Y offset of the region to update in this Allocation
10453042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param w Width of the region to update
10463042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param h Height of the region to update
10471622981ca44693152e3f95e93a8e22a2216f65a1Ying Wang     * @param array Data to be placed into the Allocation
10483042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     */
10493042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
10503042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
10513042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
10523042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 validateObjectIsPrimitiveArray(array, true),
10533042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 java.lang.reflect.Array.getLength(array));
10546d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
1055b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
1056b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1057a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines    /**
1058c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy from an array into a rectangular region in this Allocation.  The
1059c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * array is assumed to be tightly packed.
1060a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines     *
1061c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param xoff X offset of the region to update in this Allocation
1062c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param yoff Y offset of the region to update in this Allocation
1063c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param w Width of the region to update
1064c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param h Height of the region to update
1065c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param data to be placed into the Allocation
1066a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines     */
1067a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
10685f528becb09b0956bcad15b32639f87b87f40f13Stephen Hines        validateIsInt8();
10693042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
10703042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 Element.DataType.SIGNED_8, data.length);
1071a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines    }
1072a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines
1073c11e25c4e653124def1fb18e203b894f42106cbeTim Murray    /**
1074c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy from an array into a rectangular region in this Allocation.  The
1075c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * array is assumed to be tightly packed.
1076c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     *
1077c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param xoff X offset of the region to update in this Allocation
1078c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param yoff Y offset of the region to update in this Allocation
1079c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param w Width of the region to update
1080c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param h Height of the region to update
1081c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param data to be placed into the Allocation
1082c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     */
1083a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
10845f528becb09b0956bcad15b32639f87b87f40f13Stephen Hines        validateIsInt16();
10853042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
10863042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 Element.DataType.SIGNED_16, data.length);
1087a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines    }
1088a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines
1089c11e25c4e653124def1fb18e203b894f42106cbeTim Murray    /**
1090c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy from an array into a rectangular region in this Allocation.  The
1091c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * array is assumed to be tightly packed.
1092c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     *
1093c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param xoff X offset of the region to update in this Allocation
1094c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param yoff Y offset of the region to update in this Allocation
1095c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param w Width of the region to update
1096c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param h Height of the region to update
1097c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param data to be placed into the Allocation
1098c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     */
1099a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
11005f528becb09b0956bcad15b32639f87b87f40f13Stephen Hines        validateIsInt32();
11013042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
11023042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 Element.DataType.SIGNED_32, data.length);
1103a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines    }
1104a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines
1105c11e25c4e653124def1fb18e203b894f42106cbeTim Murray    /**
1106c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy from an array into a rectangular region in this Allocation.  The
1107c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * array is assumed to be tightly packed.
1108c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     *
1109c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param xoff X offset of the region to update in this Allocation
1110c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param yoff Y offset of the region to update in this Allocation
1111c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param w Width of the region to update
1112c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param h Height of the region to update
1113c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param data to be placed into the Allocation
1114c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     */
1115a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
11165f528becb09b0956bcad15b32639f87b87f40f13Stephen Hines        validateIsFloat32();
11173042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
11183042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 Element.DataType.FLOAT_32, data.length);
1119a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines    }
1120a9a7b3740598c79792cbcebca6a00d1595324ae9Stephen Hines
11219c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1122c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy a rectangular region from an Allocation into a rectangular region in
1123c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * this Allocation.
1124304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
1125c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param xoff X offset of the region in this Allocation
1126c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param yoff Y offset of the region in this Allocation
1127c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param w Width of the region to update.
1128c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param h Height of the region to update.
1129c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param data source Allocation.
1130c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param dataXoff X offset in source Allocation
1131c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param dataYoff Y offset in source Allocation
1132304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
1133304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
1134304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                                Allocation data, int dataXoff, int dataYoff) {
11356d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1136304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        mRS.validate();
1137304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        validate2DRange(xoff, yoff, w, h);
113848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
1139ba862d1544a06528151550be1784a926ee986580Jason Sams                              mSelectedLOD, mSelectedFace.mID,
1140e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                              w, h, data.getID(mRS), dataXoff, dataYoff,
1141ba862d1544a06528151550be1784a926ee986580Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
11426d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
1143304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
1144304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
11459c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1146c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy a {@link android.graphics.Bitmap} into an Allocation.  The height
1147c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and width of the update will use the height and width of the {@link
1148c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * android.graphics.Bitmap}.
1149f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
1150c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param xoff X offset of the region to update in this Allocation
1151c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param yoff Y offset of the region to update in this Allocation
1152c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param data the Bitmap to be copied
1153f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1154f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
11556d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
1156fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
1157abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray        if (data.getConfig() == null) {
1158abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
1159abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            Canvas c = new Canvas(newBitmap);
1160abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            c.drawBitmap(data, 0, 0, null);
1161abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            copy2DRangeFrom(xoff, yoff, newBitmap);
1162b05d689653b1bc94836f716f29e9578677c29075Jason Sams            return;
1163abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray        }
1164fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(data);
1165fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
116648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
11676d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
1168fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
1169fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
1170b05d689653b1bc94836f716f29e9578677c29075Jason Sams    private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
1171b05d689653b1bc94836f716f29e9578677c29075Jason Sams        if (mAdaptedAllocation != null) {
1172b05d689653b1bc94836f716f29e9578677c29075Jason Sams
1173b05d689653b1bc94836f716f29e9578677c29075Jason Sams        } else {
1174b05d689653b1bc94836f716f29e9578677c29075Jason Sams
1175b05d689653b1bc94836f716f29e9578677c29075Jason Sams            if (xoff < 0 || yoff < 0 || zoff < 0) {
1176b05d689653b1bc94836f716f29e9578677c29075Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
1177b05d689653b1bc94836f716f29e9578677c29075Jason Sams            }
1178b05d689653b1bc94836f716f29e9578677c29075Jason Sams            if (h < 0 || w < 0 || d < 0) {
1179b05d689653b1bc94836f716f29e9578677c29075Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
1180b05d689653b1bc94836f716f29e9578677c29075Jason Sams            }
1181b05d689653b1bc94836f716f29e9578677c29075Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
1182b05d689653b1bc94836f716f29e9578677c29075Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
1183b05d689653b1bc94836f716f29e9578677c29075Jason Sams            }
1184b05d689653b1bc94836f716f29e9578677c29075Jason Sams        }
1185b05d689653b1bc94836f716f29e9578677c29075Jason Sams    }
1186b05d689653b1bc94836f716f29e9578677c29075Jason Sams
1187b05d689653b1bc94836f716f29e9578677c29075Jason Sams    /**
1188b05d689653b1bc94836f716f29e9578677c29075Jason Sams     * @hide
1189b05d689653b1bc94836f716f29e9578677c29075Jason Sams     *
1190b05d689653b1bc94836f716f29e9578677c29075Jason Sams     */
11913042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
11923042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                          Object array, Element.DataType dt, int arrayLen) {
11933042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
1194b05d689653b1bc94836f716f29e9578677c29075Jason Sams        mRS.validate();
1195b05d689653b1bc94836f716f29e9578677c29075Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
11963042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
11973042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                              array, arrayLen * dt.mSize, dt);
11983042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceEnd(RenderScript.TRACE_TAG);
1199b05d689653b1bc94836f716f29e9578677c29075Jason Sams    }
1200b05d689653b1bc94836f716f29e9578677c29075Jason Sams
1201b05d689653b1bc94836f716f29e9578677c29075Jason Sams    /**
1202b05d689653b1bc94836f716f29e9578677c29075Jason Sams     * @hide
1203b05d689653b1bc94836f716f29e9578677c29075Jason Sams     * Copy a rectangular region from the array into the allocation.
1204c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * The array is assumed to be tightly packed.
1205c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     *
1206c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param xoff X offset of the region to update in this Allocation
1207c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param yoff Y offset of the region to update in this Allocation
1208c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param zoff Z offset of the region to update in this Allocation
1209c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param w Width of the region to update
1210c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param h Height of the region to update
1211c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param d Depth of the region to update
1212b05d689653b1bc94836f716f29e9578677c29075Jason Sams     * @param data to be placed into the allocation
1213b05d689653b1bc94836f716f29e9578677c29075Jason Sams     */
12143042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
12153042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
12163042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
12173042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 validateObjectIsPrimitiveArray(array, true),
12183042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams                                 java.lang.reflect.Array.getLength(array));
12193042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceEnd(RenderScript.TRACE_TAG);
1220b05d689653b1bc94836f716f29e9578677c29075Jason Sams    }
1221b05d689653b1bc94836f716f29e9578677c29075Jason Sams
1222b05d689653b1bc94836f716f29e9578677c29075Jason Sams    /**
1223b05d689653b1bc94836f716f29e9578677c29075Jason Sams     * @hide
1224b05d689653b1bc94836f716f29e9578677c29075Jason Sams     * Copy a rectangular region into the allocation from another
1225b05d689653b1bc94836f716f29e9578677c29075Jason Sams     * allocation.
1226b05d689653b1bc94836f716f29e9578677c29075Jason Sams     *
1227c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param xoff X offset of the region to update in this Allocation
1228c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param yoff Y offset of the region to update in this Allocation
1229c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param zoff Z offset of the region to update in this Allocation
1230c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param w Width of the region to update.
1231c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param h Height of the region to update.
1232c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param d Depth of the region to update.
1233b05d689653b1bc94836f716f29e9578677c29075Jason Sams     * @param data source allocation.
1234c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param dataXoff X offset of the region in the source Allocation
1235c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param dataYoff Y offset of the region in the source Allocation
1236c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param dataZoff Z offset of the region in the source Allocation
1237b05d689653b1bc94836f716f29e9578677c29075Jason Sams     */
1238b05d689653b1bc94836f716f29e9578677c29075Jason Sams    public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
1239b05d689653b1bc94836f716f29e9578677c29075Jason Sams                                Allocation data, int dataXoff, int dataYoff, int dataZoff) {
1240b05d689653b1bc94836f716f29e9578677c29075Jason Sams        mRS.validate();
1241b05d689653b1bc94836f716f29e9578677c29075Jason Sams        validate3DRange(xoff, yoff, zoff, w, h, d);
1242b05d689653b1bc94836f716f29e9578677c29075Jason Sams        mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
1243b05d689653b1bc94836f716f29e9578677c29075Jason Sams                              w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
1244b05d689653b1bc94836f716f29e9578677c29075Jason Sams                              data.mSelectedLOD);
1245b05d689653b1bc94836f716f29e9578677c29075Jason Sams    }
1246b05d689653b1bc94836f716f29e9578677c29075Jason Sams
1247fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
12489c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1249c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy from the Allocation into a {@link android.graphics.Bitmap}.  The
1250c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * bitmap must match the dimensions of the Allocation.
125148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
125248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param b The bitmap to be set from the Allocation.
125348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
1254fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(Bitmap b) {
12556d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1256fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
1257fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
1258fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
1259e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationCopyToBitmap(getID(mRS), b);
12606d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
1261fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
1262fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
12633042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    private void copyTo(Object array, Element.DataType dt, int arrayLen) {
12643042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
1265b590b354ef407c1c60288d2f8c3950d2f53d14faMiao Wang        if (dt.mSize * arrayLen < mSize) {
1266b590b354ef407c1c60288d2f8c3950d2f53d14faMiao Wang            throw new RSIllegalArgumentException(
1267b590b354ef407c1c60288d2f8c3950d2f53d14faMiao Wang                "Size of output array cannot be smaller than size of allocation.");
1268b590b354ef407c1c60288d2f8c3950d2f53d14faMiao Wang        }
12693042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        mRS.validate();
12703042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        mRS.nAllocationRead(getID(mRS), array, dt);
12713042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        Trace.traceEnd(RenderScript.TRACE_TAG);
12723042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
12733042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
12743042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    /**
12753042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * Copy from the Allocation into an array.  The array must be at
12763042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * least as large as the Allocation.  The
12773042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * {@link android.renderscript.Element} must match the component
12783042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * type of the array passed in.
12793042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     *
12803042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     * @param array The array to be set from the Allocation.
12813042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams     */
12823042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    public void copyTo(Object array) {
12833042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyTo(array, validateObjectIsPrimitiveArray(array, true),
12843042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams               java.lang.reflect.Array.getLength(array));
12853042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams    }
12863042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams
12879c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1288c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy from the Allocation into a byte array.  The array must be at least
1289c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * as large as the Allocation.  The allocation must be of an 8 bit integer
1290c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * {@link android.renderscript.Element} type.
129148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
129248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
129348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
1294fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(byte[] d) {
1295b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
12963042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyTo(d, Element.DataType.SIGNED_8, d.length);
1297fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
1298fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
12999c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1300c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy from the Allocation into a short array.  The array must be at least
1301c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * as large as the Allocation.  The allocation must be of an 16 bit integer
1302c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * {@link android.renderscript.Element} type.
130348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
130448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
130548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
1306fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(short[] d) {
1307b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
13083042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyTo(d, Element.DataType.SIGNED_16, d.length);
1309fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
1310fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
13119c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1312c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy from the Allocation into a int array.  The array must be at least as
1313c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * large as the Allocation.  The allocation must be of an 32 bit integer
1314c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * {@link android.renderscript.Element} type.
131548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
131648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
131748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
1318fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(int[] d) {
1319b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
13203042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyTo(d, Element.DataType.SIGNED_32, d.length);
132140a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
132240a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
13239c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1324c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Copy from the Allocation into a float array.  The array must be at least
1325c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * as large as the Allocation.  The allocation must be of an 32 bit float
1326c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * {@link android.renderscript.Element} type.
132748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
132848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
132948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
1330fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(float[] d) {
1331b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
13323042d2692ff6cd7597f1f66bfd9350b825b5502aJason Sams        copyTo(d, Element.DataType.FLOAT_32, d.length);
133340a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
133440a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
13359c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1336c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Resize a 1D allocation.  The contents of the allocation are preserved.
1337c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * If new elements are allocated objects are created with null contents and
1338c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * the new region is otherwise undefined.
1339f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
1340c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * <p>If the new region is smaller the references of any objects outside the
1341c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * new region will be released.</p>
1342f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
1343c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * <p>A new type will be created with the new dimension.</p>
1344f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
1345f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param dimX The new size of the allocation.
1346b05d689653b1bc94836f716f29e9578677c29075Jason Sams     *
1347c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @deprecated RenderScript objects should be immutable once created.  The
1348cd38b763d530d80aab1075dfe3b01be83abed362Tim Murray     * replacement is to create a new allocation and copy the contents. This
1349cd38b763d530d80aab1075dfe3b01be83abed362Tim Murray     * function will throw an exception if API 21 or higher is used.
1350f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
135131a7e42f4baa059352f0db119de38428e655eab2Jason Sams    public synchronized void resize(int dimX) {
1352cd38b763d530d80aab1075dfe3b01be83abed362Tim Murray        if (mRS.getApplicationContext().getApplicationInfo().targetSdkVersion >= 21) {
1353cd38b763d530d80aab1075dfe3b01be83abed362Tim Murray            throw new RSRuntimeException("Resize is not allowed in API 21+.");
1354cd38b763d530d80aab1075dfe3b01be83abed362Tim Murray        }
1355bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
135606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
13575edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
1358e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationResize1D(getID(mRS), dimX);
1359d26297fa562d8bb203df1bb5e6ded7f62c56cdb7Jason Sams        mRS.finish();  // Necessary because resize is fifoed and update is async.
136031a7e42f4baa059352f0db119de38428e655eab2Jason Sams
1361460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray        long typeID = mRS.nAllocationGetType(getID(mRS));
136231a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType = new Type(typeID, mRS);
136331a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType.updateFromNative();
1364452a7661e8b06459b75493b441d33244939c1153Jason Sams        updateCacheInfo(mType);
13655edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
13665edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams
1367b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1368b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    // creation
1369b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
137049a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
1371b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    static {
1372b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        mBitmapOptions.inScaled = false;
1373b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
1374b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
13759c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1376c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates a new Allocation with the given {@link
1377c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * android.renderscript.Type}, mipmap flag, and usage flags.
1378623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1379c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param type RenderScript type describing data layout
1380623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
1381623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
1382c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param usage bit field specifying how the Allocation is
1383623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1384623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1385623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
13866d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
1387771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
1388e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        if (type.getID(rs) == 0) {
138906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Bad Type");
139006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        }
1391460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray        long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
1392857d0c7017da763a420e825fffa2f260eb982c97Jason Sams        if (id == 0) {
1393857d0c7017da763a420e825fffa2f260eb982c97Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
1394857d0c7017da763a420e825fffa2f260eb982c97Jason Sams        }
13956d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
1396857d0c7017da763a420e825fffa2f260eb982c97Jason Sams        return new Allocation(id, rs, type, usage);
1397857d0c7017da763a420e825fffa2f260eb982c97Jason Sams    }
1398857d0c7017da763a420e825fffa2f260eb982c97Jason Sams
13999c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1400c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates an Allocation with the size specified by the type and no mipmaps
1401c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * generated by default
1402623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1403f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1404623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
1405623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1406623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1407623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1408623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
1409623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1410e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1411e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1412e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    }
1413e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams
14149c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1415c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates an Allocation for use by scripts with a given {@link
1416c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * android.renderscript.Type} and no mipmaps
1417623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1418c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param rs Context to which the Allocation will belong.
1419c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param type RenderScript Type describing data layout
1420623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1421623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
1422623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
14235476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
1424d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
14255476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
14261bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams
14279c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1428c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates an Allocation with a specified number of given elements
1429623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1430c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param rs Context to which the Allocation will belong.
1431c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param e Element to use in the Allocation
1432c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param count the number of Elements in the Allocation
1433c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param usage bit field specifying how the Allocation is
1434623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1435623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1436623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
1437623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
14385476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e,
14395476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                         int count, int usage) {
14406d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
1441771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
1442768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type.Builder b = new Type.Builder(rs, e);
1443bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        b.setX(count);
1444768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type t = b.create();
1445768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
1446460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray        long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
14475476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
144806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
1449b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
14506d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
14515476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
14525476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
14535476b450e50939940dcf3f15c92335cee2fc572dJason Sams
14549c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1455c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates an Allocation with a specified number of given elements
1456623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1457c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param rs Context to which the Allocation will belong.
1458c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param e Element to use in the Allocation
1459c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param count the number of Elements in the Allocation
1460623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1461623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
1462623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
14635476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
1464d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
1465b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
1466b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
146749a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
14688a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        final Bitmap.Config bc = b.getConfig();
14698a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
14708a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.A_8(rs);
14718a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
14728a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
14738a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_4444(rs);
14748a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
14758a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
14768a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_8888(rs);
14778a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
14788a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.RGB_565) {
14798a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGB_565(rs);
14808a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
14814bd1a3dbcad2ae424293e276434b45ebee97248dJeff Sharkey        throw new RSInvalidStateException("Bad bitmap type: " + bc);
14828a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
14838a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
148449a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
14854ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                       MipmapControl mip) {
14868a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Element e = elementFromBitmap(rs, b);
14878a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
1488bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(b.getWidth());
1489bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(b.getHeight());
14904ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
14918a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        return tb.create();
14928a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
14938a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
14949c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1495c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates an Allocation from a {@link android.graphics.Bitmap}.
1496623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1497f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1498c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param b Bitmap source for the allocation data
1499623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
1500623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
1501623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1502623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1503623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1504c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @return Allocation containing bitmap data
1505623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1506623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
150767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
15084ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                              MipmapControl mips,
15095476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
15106d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
1511771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
1512abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray
1513abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray        // WAR undocumented color formats
1514abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray        if (b.getConfig() == null) {
1515abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            if ((usage & USAGE_SHARED) != 0) {
1516abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray                throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
1517abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            }
1518abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
1519abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            Canvas c = new Canvas(newBitmap);
1520abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            c.drawBitmap(b, 0, 0, null);
1521abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray            return createFromBitmap(rs, newBitmap, mips, usage);
1522abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray        }
1523abd5db9d37839bb47c77e03b28d00042582fbfe2Tim Murray
15245476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Type t = typeFromBitmap(rs, b, mips);
15258a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
1526a314551d69098537337c970da615a8f8af58e1f1Tim Murray        // enable optimized bitmap path only with no mipmap and script-only usage
1527a314551d69098537337c970da615a8f8af58e1f1Tim Murray        if (mips == MipmapControl.MIPMAP_NONE &&
1528a314551d69098537337c970da615a8f8af58e1f1Tim Murray            t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
152978e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray            usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
1530460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray            long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
1531a314551d69098537337c970da615a8f8af58e1f1Tim Murray            if (id == 0) {
1532a314551d69098537337c970da615a8f8af58e1f1Tim Murray                throw new RSRuntimeException("Load failed.");
1533a314551d69098537337c970da615a8f8af58e1f1Tim Murray            }
1534a314551d69098537337c970da615a8f8af58e1f1Tim Murray
1535a314551d69098537337c970da615a8f8af58e1f1Tim Murray            // keep a reference to the Bitmap around to prevent GC
1536a314551d69098537337c970da615a8f8af58e1f1Tim Murray            Allocation alloc = new Allocation(id, rs, t, usage);
1537a314551d69098537337c970da615a8f8af58e1f1Tim Murray            alloc.setBitmap(b);
1538a314551d69098537337c970da615a8f8af58e1f1Tim Murray            return alloc;
1539a314551d69098537337c970da615a8f8af58e1f1Tim Murray        }
1540a314551d69098537337c970da615a8f8af58e1f1Tim Murray
15419bf189228fdb0ec14b284f8bd543d5f9137997ccJason Sams
1542460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray        long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
15435476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
154406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Load failed.");
1545718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        }
15466d7a53cbddbffba30f7e9f82ced9c1ab46214f5aTim Murray        Trace.traceEnd(RenderScript.TRACE_TAG);
15475476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
15485476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
15495476b450e50939940dcf3f15c92335cee2fc572dJason Sams
15509c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1551c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Returns the handle to a raw buffer that is being managed by the screen
1552c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * compositor. This operation is only valid for Allocations with {@link
1553c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * #USAGE_IO_INPUT}.
1554fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams     *
1555918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return Surface object associated with allocation
1556fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams     *
1557163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     */
1558fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams    public Surface getSurface() {
155972226e0543461133b9e177a3e78ae50b0c65e797Jason Sams        if ((mUsage & USAGE_IO_INPUT) == 0) {
156072226e0543461133b9e177a3e78ae50b0c65e797Jason Sams            throw new RSInvalidStateException("Allocation is not a surface texture.");
156172226e0543461133b9e177a3e78ae50b0c65e797Jason Sams        }
156272226e0543461133b9e177a3e78ae50b0c65e797Jason Sams        return mRS.nAllocationGetSurface(getID(mRS));
1563fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams    }
1564fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams
15659c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1566c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Associate a {@link android.view.Surface} with this Allocation. This
1567c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
1568918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     *
1569918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @param sur Surface to associate with allocation
1570fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams     */
1571fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams    public void setSurface(Surface sur) {
1572fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams        mRS.validate();
1573163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1574163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams            throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1575163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        }
1576163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams
1577e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationSetSurface(getID(mRS), sur);
1578163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams    }
1579163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams
15809c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1581c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates an Allocation from a {@link android.graphics.Bitmap}.
158200bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray     *
1583c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * <p>With target API version 18 or greater, this Allocation will be created
1584c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
1585c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
1586c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
1587623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1588f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1589623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap source for the allocation data
1590623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1591c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @return Allocation containing bitmap data
1592623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1593623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
15946d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
159500bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray        if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
159600bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray            return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
159778e64945602fc9df6f4e3f8050a794b17ba0f22cTim Murray                                    USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
159800bb454bd0b26c05e1dea5651b5edfd9c69ef2e0Tim Murray        }
15996d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
16006d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                USAGE_GRAPHICS_TEXTURE);
16018a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
16028a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
16039c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1604c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
1605c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * containing the horizontal list of cube faces. Each face must be a square,
1606c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * have the same size as all other faces, and have a width that is a power
1607c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * of 2.
1608623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1609f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1610c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @param b Bitmap with cubemap faces layed out in the following
1611623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
1612623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
1613623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
1614623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1615623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1616623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1617623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
161867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
16194ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                     MipmapControl mips,
16205476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     int usage) {
162167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        rs.validate();
16225476b450e50939940dcf3f15c92335cee2fc572dJason Sams
162367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int height = b.getHeight();
162467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int width = b.getWidth();
162567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
1626fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width % 6 != 0) {
162767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
162867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
1629fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width / 6 != height) {
1630dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
163167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
1632fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
163367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (!isPow2) {
163467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
163567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
163667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
163767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Element e = elementFromBitmap(rs, b);
163867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
1639fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setX(height);
1640fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setY(height);
1641bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setFaces(true);
16424ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
164367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type t = tb.create();
164467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
1645460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray        long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
164667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if(id == 0) {
164767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
164867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
16495476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
16505476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
16515476b450e50939940dcf3f15c92335cee2fc572dJason Sams
16529c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1653c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates a non-mipmapped cubemap Allocation for use as a graphics texture
1654c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * from a {@link android.graphics.Bitmap} containing the horizontal list of
1655c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * cube faces. Each face must be a square, have the same size as all other
1656c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * faces, and have a width that is a power of 2.
1657623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1658f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1659623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap with cubemap faces layed out in the following
1660623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
1661623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1662623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1663623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1664623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1665dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs,
1666dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                     Bitmap b) {
16676d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1668fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk                                       USAGE_GRAPHICS_TEXTURE);
166967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
167067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
16719c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1672c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
1673c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * objects containing the cube faces. Each face must be a square, have the
1674c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * same size as all other faces, and have a width that is a power of 2.
1675623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1676f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1677623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1678623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1679623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1680623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1681623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1682623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1683623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
1684623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
1685623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1686623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1687623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1688623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1689dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1690dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1691dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1692dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1693dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1694dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1695dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg,
1696dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        MipmapControl mips,
1697dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        int usage) {
1698dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        int height = xpos.getHeight();
1699dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (xpos.getWidth() != height ||
1700dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            xneg.getWidth() != height || xneg.getHeight() != height ||
1701dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            ypos.getWidth() != height || ypos.getHeight() != height ||
1702dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            yneg.getWidth() != height || yneg.getHeight() != height ||
1703dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zpos.getWidth() != height || zpos.getHeight() != height ||
1704dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zneg.getWidth() != height || zneg.getHeight() != height) {
1705dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
1706dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1707dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
1708dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (!isPow2) {
1709dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1710dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1711dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1712dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Element e = elementFromBitmap(rs, xpos);
1713dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
1714dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setX(height);
1715dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setY(height);
1716dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setFaces(true);
1717dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1718dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type t = tb.create();
1719dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1720dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1721dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
172220fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_X);
1723dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xpos);
1724dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1725dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xneg);
172620fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
1727dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(ypos);
1728dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1729dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(yneg);
173020fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
1731dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zpos);
1732dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1733dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zneg);
1734dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1735dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return cubemap;
1736dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1737dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
17389c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1739c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates a non-mipmapped cubemap Allocation for use as a sampler input
1740c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * from 6 {@link android.graphics.Bitmap} objects containing the cube
1741c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * faces. Each face must be a square, have the same size as all other faces,
1742c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * and have a width that is a power of 2.
1743623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1744f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1745623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1746623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1747623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1748623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1749623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1750623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1751623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1752623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1753623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1754623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1755dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1756dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1757dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1758dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1759dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1760dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1761dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg) {
1762dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1763dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
1764dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          USAGE_GRAPHICS_TEXTURE);
1765dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1766dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
17679c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1768c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates an Allocation from the Bitmap referenced
1769c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * by resource ID.
1770623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1771f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1772623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1773623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1774623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
1775623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
1776623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1777623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1778623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1779c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @return Allocation containing resource data
1780623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1781623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
17825476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
17835476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
17845476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int id,
17854ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                      MipmapControl mips,
17865476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int usage) {
1787b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1788771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
17893ece2f3268a0629383b37604d0712e3f48968ad9Jason Sams        if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
17903ece2f3268a0629383b37604d0712e3f48968ad9Jason Sams            throw new RSIllegalArgumentException("Unsupported usage specified.");
17913ece2f3268a0629383b37604d0712e3f48968ad9Jason Sams        }
17925476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Bitmap b = BitmapFactory.decodeResource(res, id);
17935476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Allocation alloc = createFromBitmap(rs, b, mips, usage);
17945476b450e50939940dcf3f15c92335cee2fc572dJason Sams        b.recycle();
17955476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return alloc;
17965476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
1797650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
17989c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1799c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates a non-mipmapped Allocation to use as a graphics texture from the
1800c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * {@link android.graphics.Bitmap} referenced by resource ID.
1801623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1802c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * <p>With target API version 18 or greater, this allocation will be created
1803c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
1804c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * target API version 17 or lower, this allocation will be created with
1805c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * {@link #USAGE_GRAPHICS_TEXTURE}.</p>
1806455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams     *
1807f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1808623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1809623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1810623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1811c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * @return Allocation containing resource data
1812623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1813623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
18145476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
18155476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
18166d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                                      int id) {
1817455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams        if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
1818455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams            return createFromBitmapResource(rs, res, id,
1819455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams                                            MipmapControl.MIPMAP_NONE,
18203ece2f3268a0629383b37604d0712e3f48968ad9Jason Sams                                            USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
1821455d64426615e5d269a3bd05cd91b67c3811fcdfJason Sams        }
18226d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmapResource(rs, res, id,
18236d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        MipmapControl.MIPMAP_NONE,
18246d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        USAGE_GRAPHICS_TEXTURE);
18256d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    }
18266d8eb266dd398abf0511685fdaf98abba3396174Jason Sams
18279c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1828c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Creates an Allocation containing string data encoded in UTF-8 format.
1829623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1830f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1831623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param str string to create the allocation from
1832623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocaiton is
1833623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1834623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1835623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
18365476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromString(RenderScript rs,
18375476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              String str,
18385476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
18395476b450e50939940dcf3f15c92335cee2fc572dJason Sams        rs.validate();
18409b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        byte[] allocArray = null;
18419b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        try {
18429b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            allocArray = str.getBytes("UTF-8");
18435476b450e50939940dcf3f15c92335cee2fc572dJason Sams            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
1844bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            alloc.copyFrom(allocArray);
18459b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            return alloc;
18469b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
18479b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        catch (Exception e) {
184806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Could not convert string to utf-8.");
18499b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
18509b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk    }
1851739c8263a10d34f5acba7fce2052012d1545d10bJason Sams
1852739c8263a10d34f5acba7fce2052012d1545d10bJason Sams    /**
1853c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Interface to handle notification when new buffers are available via
1854c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * {@link #USAGE_IO_INPUT}. An application will receive one notification
1855c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * when a buffer is available. Additional buffers will not trigger new
1856c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * notifications until a buffer is processed.
1857739c8263a10d34f5acba7fce2052012d1545d10bJason Sams     */
185842ef238208679813daa204bf110f776ab02c2bebJason Sams    public interface OnBufferAvailableListener {
1859739c8263a10d34f5acba7fce2052012d1545d10bJason Sams        public void onBufferAvailable(Allocation a);
1860739c8263a10d34f5acba7fce2052012d1545d10bJason Sams    }
1861739c8263a10d34f5acba7fce2052012d1545d10bJason Sams
1862739c8263a10d34f5acba7fce2052012d1545d10bJason Sams    /**
1863c11e25c4e653124def1fb18e203b894f42106cbeTim Murray     * Set a notification handler for {@link #USAGE_IO_INPUT}.
1864739c8263a10d34f5acba7fce2052012d1545d10bJason Sams     *
186542ef238208679813daa204bf110f776ab02c2bebJason Sams     * @param callback instance of the OnBufferAvailableListener
186642ef238208679813daa204bf110f776ab02c2bebJason Sams     *                 class to be called when buffer arrive.
1867739c8263a10d34f5acba7fce2052012d1545d10bJason Sams     */
186842ef238208679813daa204bf110f776ab02c2bebJason Sams    public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
1869739c8263a10d34f5acba7fce2052012d1545d10bJason Sams        synchronized(mAllocationMap) {
1870460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray            mAllocationMap.put(new Long(getID(mRS)), this);
1871739c8263a10d34f5acba7fce2052012d1545d10bJason Sams            mBufferNotifier = callback;
1872739c8263a10d34f5acba7fce2052012d1545d10bJason Sams        }
1873739c8263a10d34f5acba7fce2052012d1545d10bJason Sams    }
1874739c8263a10d34f5acba7fce2052012d1545d10bJason Sams
1875b730d866a0f4f56c103420f818f7f5ec6b8fd618Tim Murray    static void sendBufferNotification(long id) {
1876739c8263a10d34f5acba7fce2052012d1545d10bJason Sams        synchronized(mAllocationMap) {
1877460a04971c494fec39ffcb38e873bb8fdd82d113Tim Murray            Allocation a = mAllocationMap.get(new Long(id));
1878739c8263a10d34f5acba7fce2052012d1545d10bJason Sams
1879739c8263a10d34f5acba7fce2052012d1545d10bJason Sams            if ((a != null) && (a.mBufferNotifier != null)) {
1880739c8263a10d34f5acba7fce2052012d1545d10bJason Sams                a.mBufferNotifier.onBufferAvailable(a);
1881739c8263a10d34f5acba7fce2052012d1545d10bJason Sams            }
1882739c8263a10d34f5acba7fce2052012d1545d10bJason Sams        }
1883739c8263a10d34f5acba7fce2052012d1545d10bJason Sams    }
1884739c8263a10d34f5acba7fce2052012d1545d10bJason Sams
1885b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams}
1886