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
19b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport java.io.IOException;
20b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport java.io.InputStream;
21b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.content.res.Resources;
22650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guyimport android.content.res.AssetManager;
23b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.graphics.Bitmap;
24b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.graphics.BitmapFactory;
25fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Samsimport android.view.Surface;
26615e7cee03d1a53072b0369fa9f247811ac56845Jason Samsimport android.graphics.SurfaceTexture;
27b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.util.Log;
28650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guyimport android.util.TypedValue;
29b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
309c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines/**
3111518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>
3211518acc8c416023d8c2192b441a1767205676d9Robert Ly * Memory allocation class for renderscript.  An allocation combines a
3311518acc8c416023d8c2192b441a1767205676d9Robert Ly * {@link android.renderscript.Type} with the memory to provide storage for user data and objects.
3411518acc8c416023d8c2192b441a1767205676d9Robert Ly * This implies that all memory in Renderscript is typed.
3511518acc8c416023d8c2192b441a1767205676d9Robert Ly * </p>
36a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams *
3711518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>Allocations are the primary way data moves into and out of scripts. Memory is user
3811518acc8c416023d8c2192b441a1767205676d9Robert Ly * synchronized and it's possible for allocations to exist in multiple memory spaces
3911518acc8c416023d8c2192b441a1767205676d9Robert Ly * concurrently. Currently those spaces are:</p>
4011518acc8c416023d8c2192b441a1767205676d9Robert Ly * <ul>
4111518acc8c416023d8c2192b441a1767205676d9Robert Ly * <li>Script: accessable by RS scripts.</li>
4211518acc8c416023d8c2192b441a1767205676d9Robert Ly * <li>Graphics Texture: accessable as a graphics texture.</li>
4311518acc8c416023d8c2192b441a1767205676d9Robert Ly * <li>Graphics Vertex: accessable as graphical vertex data.</li>
4411518acc8c416023d8c2192b441a1767205676d9Robert Ly * <li>Graphics Constants: Accessable as constants in user shaders</li>
4511518acc8c416023d8c2192b441a1767205676d9Robert Ly * </ul>
4611518acc8c416023d8c2192b441a1767205676d9Robert Ly * </p>
4711518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>
4811518acc8c416023d8c2192b441a1767205676d9Robert Ly * For example, when creating a allocation for a texture, the user can
4911518acc8c416023d8c2192b441a1767205676d9Robert Ly * specify its memory spaces as both script and textures. This means that it can both
5011518acc8c416023d8c2192b441a1767205676d9Robert Ly * be used as script binding and as a GPU texture for rendering. To maintain
5111518acc8c416023d8c2192b441a1767205676d9Robert Ly * synchronization if a script modifies an allocation used by other targets it must
5211518acc8c416023d8c2192b441a1767205676d9Robert Ly * call a synchronizing function to push the updates to the memory, otherwise the results
5311518acc8c416023d8c2192b441a1767205676d9Robert Ly * are undefined.
5411518acc8c416023d8c2192b441a1767205676d9Robert Ly * </p>
5511518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>By default, Android system side updates are always applied to the script accessable
5611518acc8c416023d8c2192b441a1767205676d9Robert Ly * memory. If this is not present, they are then applied to the various HW
5711518acc8c416023d8c2192b441a1767205676d9Robert Ly * memory types.  A {@link android.renderscript.Allocation#syncAll syncAll()}
5811518acc8c416023d8c2192b441a1767205676d9Robert Ly * call is necessary after the script data is updated to
5911518acc8c416023d8c2192b441a1767205676d9Robert Ly * keep the other memory spaces in sync.</p>
60a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams *
6111518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>Allocation data is uploaded in one of two primary ways. For simple
6211518acc8c416023d8c2192b441a1767205676d9Robert Ly * arrays there are copyFrom() functions that take an array from the control code and
6311518acc8c416023d8c2192b441a1767205676d9Robert Ly * copy it to the slave memory store. Both type checked and unchecked copies are provided.
6411518acc8c416023d8c2192b441a1767205676d9Robert Ly * The unchecked variants exist to allow apps to copy over arrays of structures from a
6511518acc8c416023d8c2192b441a1767205676d9Robert Ly * control language that does not support structures.</p>
66b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams *
673aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
683aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
693aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about creating an application that uses Renderscript, read the
703aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
713aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
72b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams **/
73b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samspublic class Allocation extends BaseObj {
7443ee06857bb7f99446d1d84f8789016c5d105558Jason Sams    Type mType;
758a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    Bitmap mBitmap;
765476b450e50939940dcf3f15c92335cee2fc572dJason Sams    int mUsage;
77ba862d1544a06528151550be1784a926ee986580Jason Sams    Allocation mAdaptedAllocation;
78ba862d1544a06528151550be1784a926ee986580Jason Sams
79ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedLOD;
80ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedFace;
81ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedY;
82ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedZ;
83615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams    boolean mReadAllowed = true;
84615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams    boolean mWriteAllowed = true;
85ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedY;
86ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedZ;
87ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedLOD;
88ba862d1544a06528151550be1784a926ee986580Jason Sams    Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
89ba862d1544a06528151550be1784a926ee986580Jason Sams
90ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimX;
91ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimY;
92ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimZ;
93ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentCount;
94ba862d1544a06528151550be1784a926ee986580Jason Sams
955476b450e50939940dcf3f15c92335cee2fc572dJason Sams
969c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
97f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * The usage of the allocation.  These signal to renderscript
98f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * where to place the allocation in memory.
99f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
100f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * SCRIPT The allocation will be bound to and accessed by
101f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * scripts.
102f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1035476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_SCRIPT = 0x0001;
104f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
1059c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
106163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * GRAPHICS_TEXTURE The allocation will be used as a texture
107836c4a58a7f03485ef433dcdb61837cbc0c39735Stephen Hines     * source by one or more graphics programs.
108f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
109f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1105476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
111f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
1129c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
113f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_VERTEX The allocation will be used as a graphics
114f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * mesh.
115f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
116f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1175476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
118f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
119f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
1209c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
121f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_CONSTANTS The allocation will be used as the source
122f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * of shader constants by one or more programs.
123f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
124f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1255476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
1265476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1279c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
128163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * USAGE_GRAPHICS_RENDER_TARGET The allocation will be used as a
1298e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     * target for offscreen rendering
1308e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     *
1318e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     */
1328e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk    public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
1338e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk
1349c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
135163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * USAGE_IO_INPUT The allocation will be used as SurfaceTexture
136163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * consumer.  This usage will cause the allocation to be created
137163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * read only.
138615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     *
139615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     */
140fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams    public static final int USAGE_IO_INPUT = 0x0020;
1419069ee88cb62a62395e895eb6b3333a1dfcead73Stephen Hines
1429c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
143163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * USAGE_IO_OUTPUT The allocation will be used as a
144163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * SurfaceTexture producer.  The dimensions and format of the
145163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * SurfaceTexture will be forced to those of the allocation.
146615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     *
147615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     */
148fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams    public static final int USAGE_IO_OUTPUT = 0x0040;
14943ee06857bb7f99446d1d84f8789016c5d105558Jason Sams
1509c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
151f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Controls mipmap behavior when using the bitmap creation and
152f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * update functions.
153f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1544ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public enum MipmapControl {
1559c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
156f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * No mipmaps will be generated and the type generated from the
157f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * incoming bitmap will not contain additional LODs.
158f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1595476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_NONE(0),
160f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
1619c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
162f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * A Full mipmap chain will be created in script memory.  The
163f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * type of the allocation will contain a full mipmap chain.  On
164f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * upload to graphics the full chain will be transfered.
165f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1665476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_FULL(1),
167f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
1689c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines        /**
169f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * The type of the allocation will be the same as MIPMAP_NONE.
170f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * It will not contain mipmaps.  On upload to graphics the
171f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * graphics copy of the allocation data will contain a full
172f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * mipmap chain generated from the top level in script memory.
173f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1745476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
1755476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1765476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int mID;
1774ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl(int id) {
1785476b450e50939940dcf3f15c92335cee2fc572dJason Sams            mID = id;
1795476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
180b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
181b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
18248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams
18348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    private int getIDSafe() {
18448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        if (mAdaptedAllocation != null) {
185e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams            return mAdaptedAllocation.getID(mRS);
18648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        }
187e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        return getID(mRS);
18848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    }
18948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams
19003d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams
1919c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines   /**
19203d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     * Get the element of the type of the Allocation.
19303d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
194918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return Element that describes the structure of data in the
195918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     *         allocation
19603d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
19703d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
19803d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams    public Element getElement() {
19903d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams        return mType.getElement();
20003d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams    }
20103d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams
2029c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
20303d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     * Get the usage flags of the Allocation.
20403d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
205918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return usage flags associated with the allocation. e.g.
206918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     *         script, texture, etc.
20703d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
20803d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
20903d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams    public int getUsage() {
21003d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams        return mUsage;
21103d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams    }
21203d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams
2139c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
21436c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     * Get the size of the Allocation in bytes.
21536c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     *
216918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return size of the Allocation in bytes.
21736c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     *
21836c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     */
219918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk    public int getBytesSize() {
220918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        return mType.getCount() * mType.getElement().getBytesSize();
22136c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams    }
22236c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams
223452a7661e8b06459b75493b441d33244939c1153Jason Sams    private void updateCacheInfo(Type t) {
224452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimX = t.getX();
225452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimY = t.getY();
226452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimZ = t.getZ();
227452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentCount = mCurrentDimX;
228452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (mCurrentDimY > 1) {
229452a7661e8b06459b75493b441d33244939c1153Jason Sams            mCurrentCount *= mCurrentDimY;
230452a7661e8b06459b75493b441d33244939c1153Jason Sams        }
231452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (mCurrentDimZ > 1) {
232452a7661e8b06459b75493b441d33244939c1153Jason Sams            mCurrentCount *= mCurrentDimZ;
233452a7661e8b06459b75493b441d33244939c1153Jason Sams        }
234452a7661e8b06459b75493b441d33244939c1153Jason Sams    }
235ba862d1544a06528151550be1784a926ee986580Jason Sams
2365476b450e50939940dcf3f15c92335cee2fc572dJason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
2370de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
23849a05d7b82956009f03acbb92a064eed054eb031Jason Sams        if ((usage & ~(USAGE_SCRIPT |
23949a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_TEXTURE |
24049a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_VERTEX |
2418e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk                       USAGE_GRAPHICS_CONSTANTS |
242615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                       USAGE_GRAPHICS_RENDER_TARGET |
243615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                       USAGE_IO_INPUT |
244615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                       USAGE_IO_OUTPUT)) != 0) {
2455476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
2465476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
247615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams
248fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams        if ((usage & USAGE_IO_INPUT) != 0) {
249615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams            mWriteAllowed = false;
250615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams
251fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams            if ((usage & ~(USAGE_IO_INPUT |
252615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                           USAGE_GRAPHICS_TEXTURE |
253615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                           USAGE_SCRIPT)) != 0) {
254615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams                throw new RSIllegalArgumentException("Invalid usage combination.");
255615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams            }
256615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams        }
257615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams
2585476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mType = t;
259615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams        mUsage = usage;
260ba862d1544a06528151550be1784a926ee986580Jason Sams
261452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (t != null) {
262452a7661e8b06459b75493b441d33244939c1153Jason Sams            updateCacheInfo(t);
263ba862d1544a06528151550be1784a926ee986580Jason Sams        }
26480a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk    }
26580a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk
266b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt32() {
267b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
268b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
269b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
270b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
271b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
272b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
273b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
274b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
275b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt16() {
276b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
277b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
278b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
279b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
280b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
281b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
282b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
283b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
284b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt8() {
285b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
286b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
287b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
288b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
289b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
290b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
291b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
292b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
293b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsFloat32() {
294b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
295b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
296b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
297b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
298b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
299b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
300b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
301b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsObject() {
302b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
303b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
304b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
305b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
306b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
307b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_MESH) ||
308b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
309b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
310b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
311b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
312b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
313b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
314b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
315b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
316b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
317b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
318dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    @Override
319dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    void updateFromNative() {
32006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        super.updateFromNative();
321e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int typeID = mRS.nAllocationGetType(getID(mRS));
322dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        if(typeID != 0) {
323dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType = new Type(typeID, mRS);
324dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType.updateFromNative();
325ad37cb26cd8d8a05077152ebc5b841a5629cfbbdJason Sams            updateCacheInfo(mType);
326dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        }
327dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    }
328dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk
3299c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
33003d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     * Get the type of the Allocation.
33103d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
33203d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     * @return Type
33303d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
33403d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
335ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    public Type getType() {
336ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams        return mType;
337ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    }
338ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams
3399c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
34036c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     * Propagate changes from one usage of the allocation to the
34103d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     * remaining usages of the allocation.
34203d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
34303d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
3445476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public void syncAll(int srcLocation) {
3455476b450e50939940dcf3f15c92335cee2fc572dJason Sams        switch (srcLocation) {
3465476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_SCRIPT:
3475476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_CONSTANTS:
3485476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_TEXTURE:
3495476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_VERTEX:
3505476b450e50939940dcf3f15c92335cee2fc572dJason Sams            break;
3515476b450e50939940dcf3f15c92335cee2fc572dJason Sams        default:
3525476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
3535476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
3545476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.validate();
35548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
3565476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
3575476b450e50939940dcf3f15c92335cee2fc572dJason Sams
3589c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
359163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * Send a buffer to the output stream.  The contents of the
360163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * Allocation will be undefined after this operation.
361163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     *
362163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     */
363c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams    public void ioSend() {
364163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
365163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams            throw new RSIllegalArgumentException(
366163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams                "Can only send buffer if IO_OUTPUT usage specified.");
367163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        }
368163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        mRS.validate();
369e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationIoSend(getID(mRS));
370163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams    }
371163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams
3729c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
373c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams     * Delete once code is updated.
374c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams     * @hide
375c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams     */
376c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams    public void ioSendOutput() {
377c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams        ioSend();
378c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams    }
379c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams
3809c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
381163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * Receive the latest input into the Allocation.
382163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     *
383163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     */
384c5f519c5ec57400f04f1d0bdc69f7bfddf47f8cbJason Sams    public void ioReceive() {
385163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        if ((mUsage & USAGE_IO_INPUT) == 0) {
386163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams            throw new RSIllegalArgumentException(
387fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams                "Can only receive if IO_INPUT usage specified.");
388163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        }
389163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        mRS.validate();
390e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationIoReceive(getID(mRS));
391163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams    }
392163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams
3939c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
39436c0f6465896c1ad2f5e4811c275c6aa9f8ae874Jason Sams     * Copy an array of RS objects to the allocation.
39503d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     *
39603d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     * @param d Source array.
39703d2d00dc8ce40528d232fdf81eb0b05e92023a7Jason Sams     */
398bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(BaseObj[] d) {
399bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        mRS.validate();
400b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsObject();
401ba862d1544a06528151550be1784a926ee986580Jason Sams        if (d.length != mCurrentCount) {
402bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
403ba862d1544a06528151550be1784a926ee986580Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
404bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
405bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        int i[] = new int[d.length];
406bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        for (int ct=0; ct < d.length; ct++) {
407e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams            i[ct] = d[ct].getID(mRS);
408bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
409ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, i);
410bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    }
411bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams
412fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapFormat(Bitmap b) {
413252c07802f7039f15f723751162e64a6621e6998Jason Sams        Bitmap.Config bc = b.getConfig();
414252c07802f7039f15f723751162e64a6621e6998Jason Sams        switch (bc) {
415252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ALPHA_8:
416252c07802f7039f15f723751162e64a6621e6998Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
417252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
418252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
419252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
420918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                                                     " of " + mType.getElement().getBytesSize() +
421252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
422252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
423252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
424252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_8888:
425252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
426918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                (mType.getElement().getBytesSize() != 4)) {
427252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
428252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
429252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
430918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                                                     " of " + mType.getElement().getBytesSize() +
431252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
432252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
433252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
434252c07802f7039f15f723751162e64a6621e6998Jason Sams        case RGB_565:
435252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
436918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                (mType.getElement().getBytesSize() != 2)) {
437252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
438252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
439252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
440918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                                                     " of " + mType.getElement().getBytesSize() +
441252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
442252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
443252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
444252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_4444:
445252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
446918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                (mType.getElement().getBytesSize() != 2)) {
447252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
448252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
449252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
450918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk                                                     " of " + mType.getElement().getBytesSize() +
451252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
452252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
453252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
454252c07802f7039f15f723751162e64a6621e6998Jason Sams
455252c07802f7039f15f723751162e64a6621e6998Jason Sams        }
4564ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
4574ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
458fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapSize(Bitmap b) {
459ba862d1544a06528151550be1784a926ee986580Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
460fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
461fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
462fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
463fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams
4649c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
4654fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
4664fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
4674fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
4684fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4694fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4704fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
4714fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(int[] d) {
4724fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
473ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
4744fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
4759c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
4764fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
4774fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
4784fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
4794fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4804fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4814fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
4824fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(short[] d) {
4834fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
484ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
4854fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
4869c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
4874fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
4884fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
4894fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
4904fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4914fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4924fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
4934fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(byte[] d) {
4944fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
495ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
4964fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
4979c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
4984fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
4994fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
5004fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
5014fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5024fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5034fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5044fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(float[] d) {
5054fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
506ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
5074fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
5084fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
5099c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
5104fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
5114fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
5124fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 32 bit integer type.
5134fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5144fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5154fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
516bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(int[] d) {
517771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
518ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
519768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5204fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
5219c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
5224fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
5234fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
5244fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 16 bit integer type.
5254fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5264fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5274fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
528bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(short[] d) {
529771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
530ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
531768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5324fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
5339c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
5344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
5354fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
5364fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 8 bit integer type.
5374fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5384fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5394fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
540bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(byte[] d) {
541771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
542ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
543b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
5444fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
5459c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
5464fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
5474fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
5484fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 32 bit float type.
5494fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5504fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5514fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
552bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(float[] d) {
553771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
554ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
555b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
5564fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
5579c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
5584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from a bitmap.  The height, width, and
5594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * format of the bitmap must match the existing allocation.
5604fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5614fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param b the source bitmap
5624fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
563bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(Bitmap b) {
564fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
565fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
566fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
567e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationCopyFromBitmap(getID(mRS), b);
5684ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
56926ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
5709c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
571fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
572fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
573fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
574fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
575fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
576fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
57721b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
578f70b0fc880edb7c47d5bcc97edbc125b575bc90dJason Sams        mRS.validate();
579918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        int eSize = mType.mElement.getBytesSize();
580a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        final byte[] data = fp.getData();
581a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
582a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int count = data.length / eSize;
583a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if ((eSize * count) != data.length) {
58406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer length " + data.length +
585a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               " not divisible by element size " + eSize + ".");
586a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
587ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
58849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    }
58949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
5909c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
591fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
592fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
593fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
594fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
595fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param component_number
596fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
597fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
59821b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
599f70b0fc880edb7c47d5bcc97edbc125b575bc90dJason Sams        mRS.validate();
60049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (component_number >= mType.mElement.mElements.length) {
60106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
60249bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
60349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if(xoff < 0) {
60406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
60549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
60649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
60749bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        final byte[] data = fp.getData();
608918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        int eSize = mType.mElement.mElements[component_number].getBytesSize();
609bf3c3f2ab1067b647038170cb201cdd1a9a1c6d2Alex Sakhartchouk        eSize *= mType.mElement.mArraySizes[component_number];
61049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
61149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (data.length != eSize) {
61206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
61349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams                                               " does not match component size " + eSize + ".");
61449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
61549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
61648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
617ba862d1544a06528151550be1784a926ee986580Jason Sams                                     component_number, data, data.length);
618a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams    }
619a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
620768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
621771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
622a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(off < 0) {
62306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
624a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
625a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(count < 1) {
62606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
627a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
628ba862d1544a06528151550be1784a926ee986580Jason Sams        if((off + count) > mCurrentCount) {
629ba862d1544a06528151550be1784a926ee986580Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
630a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               ", got " + count + " at offset " + off + ".");
631768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
632ba862d1544a06528151550be1784a926ee986580Jason Sams        if(len < dataSize) {
63306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
634768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
635b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
636b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
6379c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
638f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Generate a mipmap chain.  Requires the type of the allocation
639f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * include mipmaps.
640f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
641f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * This function will generate a complete set of mipmaps from
642f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * the top level lod and place them into the script memoryspace.
643f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
644f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * If the allocation is also using other memory spaces a
645f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * followup sync will be required.
646f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
647f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void generateMipmaps() {
648e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationGenerateMipmaps(getID(mRS));
649f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    }
650f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
6519c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
6524fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
6534fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
6544fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
6554fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6564fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6574fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
6584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
6604fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
661918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        int dataSize = mType.mElement.getBytesSize() * count;
662768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
66348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
664768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
6659c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
6664fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
6674fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
6684fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
6694fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6704fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6714fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
6724fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6734fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
6744fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
675918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        int dataSize = mType.mElement.getBytesSize() * count;
676768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 2, dataSize);
67748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
678768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
6799c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
6804fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
6814fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
6824fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
6834fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6844fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6854fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
6864fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6874fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
6884fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
689918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        int dataSize = mType.mElement.getBytesSize() * count;
690768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length, dataSize);
69148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
692768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
6939c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
6944fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
6954fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
6964fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
6974fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6984fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6994fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
7004fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
7014fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
7024fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
703918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk        int dataSize = mType.mElement.getBytesSize() * count;
704768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
70548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
706b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
707b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
7089c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
7094fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
7104fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
7114fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 32 bit integer type.
7124fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
7134fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
7144fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
7154fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
7164fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
717b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
718b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
719b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
720b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
7214fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
7229c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
7234fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
7244fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
7254fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 16 bit integer type.
7264fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
7274fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
7284fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
7294fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
7304fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
731b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
732b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
733b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
734b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
7354fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
7369c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
7374fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
7384fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
7394fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 8 bit integer type.
7404fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
7414fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
7424fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
7434fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
7444fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
745b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
746b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
747b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
748b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
7494fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
7509c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
7514fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
7524fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
7534fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 32 bit float type.
7544fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
7554fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
7564fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
757304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param d the source data array.
7584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
759b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
760b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
761b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
762b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
763b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
7649c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines     /**
765304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy part of an allocation from another allocation.
766304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
767304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param off The offset of the first element to be copied.
768304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param count The number of elements to be copied.
769304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param data the source data allocation.
770304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataOff off The offset of the first element in data to
771304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *          be copied.
772304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
773304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
77448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
775ba862d1544a06528151550be1784a926ee986580Jason Sams                              mSelectedLOD, mSelectedFace.mID,
776e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                              count, 1, data.getID(mRS), dataOff, 0,
777ba862d1544a06528151550be1784a926ee986580Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
778304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
779304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
780fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
781ba862d1544a06528151550be1784a926ee986580Jason Sams        if (mAdaptedAllocation != null) {
782ba862d1544a06528151550be1784a926ee986580Jason Sams
783ba862d1544a06528151550be1784a926ee986580Jason Sams        } else {
784ba862d1544a06528151550be1784a926ee986580Jason Sams
785ba862d1544a06528151550be1784a926ee986580Jason Sams            if (xoff < 0 || yoff < 0) {
786ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
787ba862d1544a06528151550be1784a926ee986580Jason Sams            }
788ba862d1544a06528151550be1784a926ee986580Jason Sams            if (h < 0 || w < 0) {
789ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
790ba862d1544a06528151550be1784a926ee986580Jason Sams            }
791ba862d1544a06528151550be1784a926ee986580Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
792ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
793ba862d1544a06528151550be1784a926ee986580Jason Sams            }
794fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
795fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
796768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
7979c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
798304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy a rectangular region from the array into the allocation.
799304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * The incoming array is assumed to be tightly packed.
800f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
801f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param xoff X offset of the region to update
802f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param yoff Y offset of the region to update
803f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param w Width of the incoming region to update
804f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param h Height of the incoming region to update
805f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param data to be placed into the allocation
806f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
807f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
808fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
809fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
81048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
811ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length);
812fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
813fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
814f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
815fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
816fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
81748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
818ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length * 2);
819fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
820fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
821f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
822771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
823fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
82448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
825ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length * 4);
826b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
827b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
828f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
829771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
830fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
83148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
832ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length * 4);
833b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
834b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
8359c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
836304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy a rectangular region into the allocation from another
837304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * allocation.
838304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
839304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param xoff X offset of the region to update.
840304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param yoff Y offset of the region to update.
841304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param w Width of the incoming region to update.
842304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param h Height of the incoming region to update.
843304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param data source allocation.
844304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataXoff X offset in data of the region to update.
845304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataYoff Y offset in data of the region to update.
846304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
847304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
848304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                                Allocation data, int dataXoff, int dataYoff) {
849304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        mRS.validate();
850304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        validate2DRange(xoff, yoff, w, h);
85148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
852ba862d1544a06528151550be1784a926ee986580Jason Sams                              mSelectedLOD, mSelectedFace.mID,
853e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams                              w, h, data.getID(mRS), dataXoff, dataYoff,
854ba862d1544a06528151550be1784a926ee986580Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
855304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
856304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
8579c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
858f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Copy a bitmap into an allocation.  The height and width of
859f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * the update will use the height and width of the incoming
860f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * bitmap.
861f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
862f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param xoff X offset of the region to update
863f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param yoff Y offset of the region to update
864f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param data the bitmap to be copied
865f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
866f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
867fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
868fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(data);
869fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
87048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
871fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
872fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
873fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
8749c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
87548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a Bitmap.  The bitmap must
87648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * match the dimensions of the Allocation.
87748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
87848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param b The bitmap to be set from the Allocation.
87948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
880fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(Bitmap b) {
881fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
882fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
883fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
884e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationCopyToBitmap(getID(mRS), b);
885fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
886fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
8879c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
88848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a byte array.  The array must
88948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be at least as large as the Allocation.  The allocation must
89048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be of an 8 bit elemental type.
89148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
89248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
89348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
894fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(byte[] d) {
895b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
896fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
897e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationRead(getID(mRS), d);
898fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
899fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
9009c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
90148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a short array.  The array must
90248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be at least as large as the Allocation.  The allocation must
90348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be of an 16 bit elemental type.
90448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
90548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
90648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
907fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(short[] d) {
908b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
909fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
910e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationRead(getID(mRS), d);
911fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
912fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
9139c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
91448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a int array.  The array must be
91548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * at least as large as the Allocation.  The allocation must be
91648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * of an 32 bit elemental type.
91748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
91848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
91948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
920fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(int[] d) {
921b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
922771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
923e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationRead(getID(mRS), d);
92440a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
92540a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
9269c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
92748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a float array.  The array must
92848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be at least as large as the Allocation.  The allocation must
92948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be of an 32 bit float elemental type.
93048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
93148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
93248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
933fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(float[] d) {
934b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
935771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
936e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationRead(getID(mRS), d);
93740a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
93840a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
9399c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
940f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Resize a 1D allocation.  The contents of the allocation are
941f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * preserved.  If new elements are allocated objects are created
942f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * with null contents and the new region is otherwise undefined.
943f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
944f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * If the new region is smaller the references of any objects
945f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * outside the new region will be released.
946f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
947f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * A new type will be created with the new dimension.
948f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
949f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param dimX The new size of the allocation.
950f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
95131a7e42f4baa059352f0db119de38428e655eab2Jason Sams    public synchronized void resize(int dimX) {
952bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
95306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
9545edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
955e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationResize1D(getID(mRS), dimX);
956d26297fa562d8bb203df1bb5e6ded7f62c56cdb7Jason Sams        mRS.finish();  // Necessary because resize is fifoed and update is async.
95731a7e42f4baa059352f0db119de38428e655eab2Jason Sams
958e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int typeID = mRS.nAllocationGetType(getID(mRS));
95931a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType = new Type(typeID, mRS);
96031a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType.updateFromNative();
961452a7661e8b06459b75493b441d33244939c1153Jason Sams        updateCacheInfo(mType);
9625edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
9635edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams
9649c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
965163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * Resize a 2D allocation.  The contents of the allocation are
966163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * preserved.  If new elements are allocated objects are created
967163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * with null contents and the new region is otherwise undefined.
968163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     *
969163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * If the new region is smaller the references of any objects
970163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * outside the new region will be released.
971163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     *
972163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * A new type will be created with the new dimension.
973163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     *
974163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * @hide
975163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * @param dimX The new size of the allocation.
976163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     * @param dimY The new size of the allocation.
977163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     */
9785edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    public void resize(int dimX, int dimY) {
979163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        if ((mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
980163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams            throw new RSInvalidStateException(
981163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams                "Resize only support for 2D allocations at this time.");
9825edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
9835edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if (mType.getY() == 0) {
984163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams            throw new RSInvalidStateException(
985163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams                "Resize only support for 2D allocations at this time.");
9865edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
987e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationResize2D(getID(mRS), dimX, dimY);
988163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        mRS.finish();  // Necessary because resize is fifoed and update is async.
989163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams
990e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int typeID = mRS.nAllocationGetType(getID(mRS));
991163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        mType = new Type(typeID, mRS);
992163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        mType.updateFromNative();
993163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        updateCacheInfo(mType);
9945edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
99540a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
996bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
997b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
998b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    // creation
999b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
100049a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
1001b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    static {
1002b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        mBitmapOptions.inScaled = false;
1003b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
1004b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
10059c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1006623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1007623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
1008623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
1009623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
1010623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1011623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1012623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1013623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
1014771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
1015e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        if (type.getID(rs) == 0) {
101606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Bad Type");
101706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        }
1018e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
1019857d0c7017da763a420e825fffa2f260eb982c97Jason Sams        if (id == 0) {
1020857d0c7017da763a420e825fffa2f260eb982c97Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
1021857d0c7017da763a420e825fffa2f260eb982c97Jason Sams        }
1022857d0c7017da763a420e825fffa2f260eb982c97Jason Sams        return new Allocation(id, rs, type, usage);
1023857d0c7017da763a420e825fffa2f260eb982c97Jason Sams    }
1024857d0c7017da763a420e825fffa2f260eb982c97Jason Sams
10259c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1026623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with the size specified by
1027623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the type and no mipmaps generated by default
1028623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1029f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1030623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
1031623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1032623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1033623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1034623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
1035623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1036e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
1037e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
1038e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    }
1039e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams
10409c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1041623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation for use by the script with
1042623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the size specified by the type and no mipmaps generated by
1043623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * default
1044623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1045f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1046623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
1047623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1048623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
1049623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
10505476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
1051d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
10525476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
10531bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams
10549c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1055623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with a specified number of
1056623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * given elements
1057623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1058f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1059623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param e describes what each element of an allocation is
1060623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param count specifies the number of element in the allocation
1061623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1062623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1063623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1064623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
1065623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
10665476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e,
10675476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                         int count, int usage) {
1068771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
1069768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type.Builder b = new Type.Builder(rs, e);
1070bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        b.setX(count);
1071768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type t = b.create();
1072768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
1073e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
10745476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
107506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
1076b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
10775476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
10785476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
10795476b450e50939940dcf3f15c92335cee2fc572dJason Sams
10809c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1081623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with a specified number of
1082623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * given elements
1083623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1084f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1085623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param e describes what each element of an allocation is
1086623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param count specifies the number of element in the allocation
1087623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1088623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
1089623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
10905476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
1091d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
1092b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
1093b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
109449a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
10958a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        final Bitmap.Config bc = b.getConfig();
10968a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
10978a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.A_8(rs);
10988a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
10998a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
11008a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_4444(rs);
11018a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
11028a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
11038a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_8888(rs);
11048a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
11058a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.RGB_565) {
11068a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGB_565(rs);
11078a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
11084bd1a3dbcad2ae424293e276434b45ebee97248dJeff Sharkey        throw new RSInvalidStateException("Bad bitmap type: " + bc);
11098a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
11108a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
111149a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
11124ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                       MipmapControl mip) {
11138a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Element e = elementFromBitmap(rs, b);
11148a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
1115bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(b.getWidth());
1116bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(b.getHeight());
11174ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
11188a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        return tb.create();
11198a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
11208a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
11219c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1122623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation from a bitmap
1123623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1124f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1125623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap source for the allocation data
1126623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
1127623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
1128623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1129623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1130623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1131623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing bitmap data
1132623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1133623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
113467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
11354ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                              MipmapControl mips,
11365476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
1137771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
11385476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Type t = typeFromBitmap(rs, b, mips);
11398a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
1140e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
11415476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
114206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Load failed.");
1143718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        }
11445476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
11455476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
11465476b450e50939940dcf3f15c92335cee2fc572dJason Sams
11479c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1148615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     *
1149615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     *
1150615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     * @hide
1151615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     *
1152615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams     */
1153615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams    public SurfaceTexture getSurfaceTexture() {
1154fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams        if ((mUsage & USAGE_IO_INPUT) == 0) {
1155615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams            throw new RSInvalidStateException("Allocation is not a surface texture.");
1156615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams        }
1157615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams
1158e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int id = mRS.nAllocationGetSurfaceTextureID(getID(mRS));
1159fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams        SurfaceTexture st = new SurfaceTexture(id);
1160e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationGetSurfaceTextureID2(getID(mRS), st);
1161615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams
1162fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3bJason Sams        return st;
1163615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams    }
1164615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams
11659c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1166918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * For allocations used with io operations, returns the handle
1167918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * onto a raw buffer that is being managed by the screen
1168918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * compositor.
1169fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams     *
1170918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @return Surface object associated with allocation
1171fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams     *
1172163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams     */
1173fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams    public Surface getSurface() {
1174fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams        return new Surface(getSurfaceTexture());
1175fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams    }
1176fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams
11779c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1178918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * Associate a surface for io output with this allocation
1179918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     *
1180918e840628a0b40a95fd42618f604ea5a44aebaeAlex Sakhartchouk     * @param sur Surface to associate with allocation
1181fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams     */
1182fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams    public void setSurface(Surface sur) {
1183fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams        mRS.validate();
1184163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1185163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams            throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1186163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams        }
1187163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams
1188e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationSetSurface(getID(mRS), sur);
1189163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams    }
1190163766cbe798baf8e86eb8201bc6c3def931d59aJason Sams
11919c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1192fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams     * @hide
1193fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams     */
1194fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams    public void setSurfaceTexture(SurfaceTexture st) {
1195fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams        mRS.validate();
1196fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
1197fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams            throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
1198fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams        }
1199fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams
1200fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams        Surface s = new Surface(st);
1201e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        mRS.nAllocationSetSurface(getID(mRS), s);
1202fb9aa9f3ef11878caaf6fcfb5ea49ffb9f7abf33Jason Sams    }
1203615e7cee03d1a53072b0369fa9f247811ac56845Jason Sams
12049c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1205623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped renderscript allocation to use as a
1206623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture
1207623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1208f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1209623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap source for the allocation data
1210623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1211623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing bitmap data
1212623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1213623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
12146d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
12156d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
12166d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                USAGE_GRAPHICS_TEXTURE);
12178a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
12188a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
12199c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1220623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a cubemap allocation from a bitmap containing the
1221623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * horizontal list of cube faces. Each individual face must be
1222623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the same size and power of 2
1223623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1224f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1225623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap with cubemap faces layed out in the following
1226623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
1227623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
1228623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
1229623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1230623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1231623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1232623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
123367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
12344ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                     MipmapControl mips,
12355476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     int usage) {
123667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        rs.validate();
12375476b450e50939940dcf3f15c92335cee2fc572dJason Sams
123867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int height = b.getHeight();
123967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int width = b.getWidth();
124067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
1241fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width % 6 != 0) {
124267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
124367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
1244fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width / 6 != height) {
1245dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
124667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
1247fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
124867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (!isPow2) {
124967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
125067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
125167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
125267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Element e = elementFromBitmap(rs, b);
125367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
1254fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setX(height);
1255fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setY(height);
1256bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setFaces(true);
12574ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
125867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type t = tb.create();
125967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
1260e07694b24f7d12d72b084b6651356681ebd0efd6Jason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
126167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if(id == 0) {
126267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
126367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
12645476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
12655476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
12665476b450e50939940dcf3f15c92335cee2fc572dJason Sams
12679c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1268623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped cubemap allocation for use as a
1269623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from a bitmap containing the horizontal list
1270623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * of cube faces. Each individual face must be the same size and
1271623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1272623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1273f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1274623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap with cubemap faces layed out in the following
1275623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
1276623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1277623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1278623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1279623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1280dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs,
1281dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                     Bitmap b) {
12826d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1283fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk                                       USAGE_GRAPHICS_TEXTURE);
128467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
128567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
12869c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1287623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a cubemap allocation from 6 bitmaps containing
1288623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the cube faces. All the faces must be the same size and
1289623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1290623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1291f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1292623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1293623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1294623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1295623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1296623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1297623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1298623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
1299623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
1300623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1301623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1302623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1303623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1304dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1305dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1306dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1307dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1308dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1309dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1310dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg,
1311dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        MipmapControl mips,
1312dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        int usage) {
1313dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        int height = xpos.getHeight();
1314dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (xpos.getWidth() != height ||
1315dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            xneg.getWidth() != height || xneg.getHeight() != height ||
1316dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            ypos.getWidth() != height || ypos.getHeight() != height ||
1317dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            yneg.getWidth() != height || yneg.getHeight() != height ||
1318dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zpos.getWidth() != height || zpos.getHeight() != height ||
1319dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zneg.getWidth() != height || zneg.getHeight() != height) {
1320dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
1321dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1322dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
1323dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (!isPow2) {
1324dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1325dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1326dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1327dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Element e = elementFromBitmap(rs, xpos);
1328dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
1329dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setX(height);
1330dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setY(height);
1331dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setFaces(true);
1332dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1333dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type t = tb.create();
1334dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1335dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1336dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
133720fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_X);
1338dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xpos);
1339dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1340dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xneg);
134120fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
1342dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(ypos);
1343dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1344dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(yneg);
134520fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
1346dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zpos);
1347dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1348dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zneg);
1349dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1350dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return cubemap;
1351dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1352dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
13539c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1354623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped cubemap allocation for use as a
1355623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from 6 bitmaps containing
1356623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the cube faces. All the faces must be the same size and
1357623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1358623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1359f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1360623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1361623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1362623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1363623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1364623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1365623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1366623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1367623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1368623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1369623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1370dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1371dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1372dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1373dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1374dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1375dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1376dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg) {
1377dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1378dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
1379dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          USAGE_GRAPHICS_TEXTURE);
1380dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1381dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
13829c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1383623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation from the bitmap referenced
1384623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * by resource id
1385623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1386f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1387623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1388623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1389623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
1390623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
1391623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1392623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1393623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1394623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing resource data
1395623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1396623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
13975476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
13985476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
13995476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int id,
14004ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                      MipmapControl mips,
14015476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int usage) {
1402b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1403771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
14045476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Bitmap b = BitmapFactory.decodeResource(res, id);
14055476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Allocation alloc = createFromBitmap(rs, b, mips, usage);
14065476b450e50939940dcf3f15c92335cee2fc572dJason Sams        b.recycle();
14075476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return alloc;
14085476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
1409650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
14109c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1411623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped renderscript allocation to use as a
1412623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from the bitmap referenced by resource id
1413623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1414f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1415623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1416623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1417623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1418623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing resource data
1419623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1420623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
14215476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
14225476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
14236d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                                      int id) {
14246d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmapResource(rs, res, id,
14256d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        MipmapControl.MIPMAP_NONE,
14266d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        USAGE_GRAPHICS_TEXTURE);
14276d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    }
14286d8eb266dd398abf0511685fdaf98abba3396174Jason Sams
14299c9ad3f8c218954e46aab81f9af7834cea5675caStephen Hines    /**
1430623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation containing string data
1431623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * encoded in UTF-8 format
1432623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1433f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1434623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param str string to create the allocation from
1435623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocaiton is
1436623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1437623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1438623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
14395476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromString(RenderScript rs,
14405476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              String str,
14415476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
14425476b450e50939940dcf3f15c92335cee2fc572dJason Sams        rs.validate();
14439b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        byte[] allocArray = null;
14449b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        try {
14459b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            allocArray = str.getBytes("UTF-8");
14465476b450e50939940dcf3f15c92335cee2fc572dJason Sams            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
1447bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            alloc.copyFrom(allocArray);
14489b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            return alloc;
14499b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
14509b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        catch (Exception e) {
145106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Could not convert string to utf-8.");
14529b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
14539b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk    }
1454b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams}
1455b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1456b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1457