Allocation.java revision 20fbd01335f3a41ab78e0bb9f70124665afb1e3b
1b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams/*
2b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * Copyright (C) 2008 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;
25b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.util.Log;
26650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guyimport android.util.TypedValue;
27b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
28b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams/**
2911518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>
3011518acc8c416023d8c2192b441a1767205676d9Robert Ly * Memory allocation class for renderscript.  An allocation combines a
3111518acc8c416023d8c2192b441a1767205676d9Robert Ly * {@link android.renderscript.Type} with the memory to provide storage for user data and objects.
3211518acc8c416023d8c2192b441a1767205676d9Robert Ly * This implies that all memory in Renderscript is typed.
3311518acc8c416023d8c2192b441a1767205676d9Robert Ly * </p>
34a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams *
3511518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>Allocations are the primary way data moves into and out of scripts. Memory is user
3611518acc8c416023d8c2192b441a1767205676d9Robert Ly * synchronized and it's possible for allocations to exist in multiple memory spaces
3711518acc8c416023d8c2192b441a1767205676d9Robert Ly * concurrently. Currently those spaces are:</p>
3811518acc8c416023d8c2192b441a1767205676d9Robert Ly * <ul>
3911518acc8c416023d8c2192b441a1767205676d9Robert Ly * <li>Script: accessable by RS scripts.</li>
4011518acc8c416023d8c2192b441a1767205676d9Robert Ly * <li>Graphics Texture: accessable as a graphics texture.</li>
4111518acc8c416023d8c2192b441a1767205676d9Robert Ly * <li>Graphics Vertex: accessable as graphical vertex data.</li>
4211518acc8c416023d8c2192b441a1767205676d9Robert Ly * <li>Graphics Constants: Accessable as constants in user shaders</li>
4311518acc8c416023d8c2192b441a1767205676d9Robert Ly * </ul>
4411518acc8c416023d8c2192b441a1767205676d9Robert Ly * </p>
4511518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>
4611518acc8c416023d8c2192b441a1767205676d9Robert Ly * For example, when creating a allocation for a texture, the user can
4711518acc8c416023d8c2192b441a1767205676d9Robert Ly * specify its memory spaces as both script and textures. This means that it can both
4811518acc8c416023d8c2192b441a1767205676d9Robert Ly * be used as script binding and as a GPU texture for rendering. To maintain
4911518acc8c416023d8c2192b441a1767205676d9Robert Ly * synchronization if a script modifies an allocation used by other targets it must
5011518acc8c416023d8c2192b441a1767205676d9Robert Ly * call a synchronizing function to push the updates to the memory, otherwise the results
5111518acc8c416023d8c2192b441a1767205676d9Robert Ly * are undefined.
5211518acc8c416023d8c2192b441a1767205676d9Robert Ly * </p>
5311518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>By default, Android system side updates are always applied to the script accessable
5411518acc8c416023d8c2192b441a1767205676d9Robert Ly * memory. If this is not present, they are then applied to the various HW
5511518acc8c416023d8c2192b441a1767205676d9Robert Ly * memory types.  A {@link android.renderscript.Allocation#syncAll syncAll()}
5611518acc8c416023d8c2192b441a1767205676d9Robert Ly * call is necessary after the script data is updated to
5711518acc8c416023d8c2192b441a1767205676d9Robert Ly * keep the other memory spaces in sync.</p>
58a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams *
5911518acc8c416023d8c2192b441a1767205676d9Robert Ly * <p>Allocation data is uploaded in one of two primary ways. For simple
6011518acc8c416023d8c2192b441a1767205676d9Robert Ly * arrays there are copyFrom() functions that take an array from the control code and
6111518acc8c416023d8c2192b441a1767205676d9Robert Ly * copy it to the slave memory store. Both type checked and unchecked copies are provided.
6211518acc8c416023d8c2192b441a1767205676d9Robert Ly * The unchecked variants exist to allow apps to copy over arrays of structures from a
6311518acc8c416023d8c2192b441a1767205676d9Robert Ly * control language that does not support structures.</p>
64b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams *
65b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams **/
66b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samspublic class Allocation extends BaseObj {
6743ee06857bb7f99446d1d84f8789016c5d105558Jason Sams    Type mType;
688a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    Bitmap mBitmap;
695476b450e50939940dcf3f15c92335cee2fc572dJason Sams    int mUsage;
705476b450e50939940dcf3f15c92335cee2fc572dJason Sams
71f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
72f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * The usage of the allocation.  These signal to renderscript
73f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * where to place the allocation in memory.
74f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
75f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * SCRIPT The allocation will be bound to and accessed by
76f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * scripts.
77f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
785476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_SCRIPT = 0x0001;
79f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
80f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
81f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_TEXTURE The allcation will be used as a texture
82f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * source by one or more graphcics programs.
83f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
84f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
855476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
86f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
87f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
88f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_VERTEX The allocation will be used as a graphics
89f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * mesh.
90f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
91f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
925476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
93f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
94f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
95f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
96f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_CONSTANTS The allocation will be used as the source
97f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * of shader constants by one or more programs.
98f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
99f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1005476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
1015476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1028e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk    /**
1038e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     * USAGE_GRAPHICS_RENDER_TARGET The allcation will be used as a
1048e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     * target for offscreen rendering
1058e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     *
1068e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     */
1078e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk    public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
1088e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk
10943ee06857bb7f99446d1d84f8789016c5d105558Jason Sams
110f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
111f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Controls mipmap behavior when using the bitmap creation and
112f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * update functions.
113f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1144ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public enum MipmapControl {
115f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        /**
116f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * No mipmaps will be generated and the type generated from the
117f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * incoming bitmap will not contain additional LODs.
118f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1195476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_NONE(0),
120f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
121f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        /**
122f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * A Full mipmap chain will be created in script memory.  The
123f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * type of the allocation will contain a full mipmap chain.  On
124f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * upload to graphics the full chain will be transfered.
125f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1265476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_FULL(1),
127f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
128f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        /**
129f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * The type of the allocation will be the same as MIPMAP_NONE.
130f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * It will not contain mipmaps.  On upload to graphics the
131f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * graphics copy of the allocation data will contain a full
132f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * mipmap chain generated from the top level in script memory.
133f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1345476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
1355476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1365476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int mID;
1374ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl(int id) {
1385476b450e50939940dcf3f15c92335cee2fc572dJason Sams            mID = id;
1395476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
140b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
141b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1425476b450e50939940dcf3f15c92335cee2fc572dJason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
1430de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
14449a05d7b82956009f03acbb92a064eed054eb031Jason Sams        if ((usage & ~(USAGE_SCRIPT |
14549a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_TEXTURE |
14649a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_VERTEX |
1478e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk                       USAGE_GRAPHICS_CONSTANTS |
1488e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk                       USAGE_GRAPHICS_RENDER_TARGET)) != 0) {
1495476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
1505476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
1515476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mType = t;
15280a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk    }
15380a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk
154b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt32() {
155b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
156b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
157b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
158b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
159b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
160b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
161b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
162b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
163b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt16() {
164b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
165b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
166b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
167b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
168b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
169b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
170b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
171b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
172b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt8() {
173b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
174b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
175b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
176b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
177b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
178b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
179b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
180b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
181b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsFloat32() {
182b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
183b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
184b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
185b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
186b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
187b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
188b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
189b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsObject() {
190b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
191b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
192b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
193b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
194b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
195b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_MESH) ||
196b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
197b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
198b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
199b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
200b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
201b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
202b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
203b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
204b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
205b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
206dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    @Override
207dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    void updateFromNative() {
20806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        super.updateFromNative();
20906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
210dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        if(typeID != 0) {
211dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType = new Type(typeID, mRS);
212dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType.updateFromNative();
213dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        }
214dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    }
215dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk
216ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    public Type getType() {
217ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams        return mType;
218ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    }
219ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams
2205476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public void syncAll(int srcLocation) {
2215476b450e50939940dcf3f15c92335cee2fc572dJason Sams        switch (srcLocation) {
2225476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_SCRIPT:
2235476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_CONSTANTS:
2245476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_TEXTURE:
2255476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_VERTEX:
2265476b450e50939940dcf3f15c92335cee2fc572dJason Sams            break;
2275476b450e50939940dcf3f15c92335cee2fc572dJason Sams        default:
2285476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
2295476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
2305476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.validate();
2315476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.nAllocationSyncAll(getID(), srcLocation);
2325476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
2335476b450e50939940dcf3f15c92335cee2fc572dJason Sams
234bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(BaseObj[] d) {
235bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        mRS.validate();
236b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsObject();
237bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if (d.length != mType.getCount()) {
238bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
239bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams                                                 mType.getCount() + ", array length = " + d.length);
240bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
241bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        int i[] = new int[d.length];
242bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        for (int ct=0; ct < d.length; ct++) {
243bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            i[ct] = d[ct].getID();
244bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
245ed5bab93c6851dc4143d6e0d23ebf288e026936bJason Sams        copy1DRangeFromUnchecked(0, mType.getCount(), i);
246bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    }
247bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams
248fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapFormat(Bitmap b) {
249252c07802f7039f15f723751162e64a6621e6998Jason Sams        Bitmap.Config bc = b.getConfig();
250252c07802f7039f15f723751162e64a6621e6998Jason Sams        switch (bc) {
251252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ALPHA_8:
252252c07802f7039f15f723751162e64a6621e6998Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
253252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
254252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
255252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
256252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
257252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
258252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
259252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
260252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_8888:
261252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
262252c07802f7039f15f723751162e64a6621e6998Jason Sams                (mType.getElement().getSizeBytes() != 4)) {
263252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
264252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
265252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
266252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
267252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
268252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
269252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
270252c07802f7039f15f723751162e64a6621e6998Jason Sams        case RGB_565:
271252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
272252c07802f7039f15f723751162e64a6621e6998Jason Sams                (mType.getElement().getSizeBytes() != 2)) {
273252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
274252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
275252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
276252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
277252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
278252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
279252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
280252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_4444:
281252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
282252c07802f7039f15f723751162e64a6621e6998Jason Sams                (mType.getElement().getSizeBytes() != 2)) {
283252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
284252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
285252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
286252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
287252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
288252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
289252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
290252c07802f7039f15f723751162e64a6621e6998Jason Sams
291252c07802f7039f15f723751162e64a6621e6998Jason Sams        }
2924ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
2934ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
294fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapSize(Bitmap b) {
295fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        if(mType.getX() != b.getWidth() ||
296fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams           mType.getY() != b.getHeight()) {
297fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
298fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
299fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
300fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams
3014fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3024fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3034fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3044fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3054fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3064fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3074fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3084fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(int[] d) {
3094fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
3104fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        copy1DRangeFromUnchecked(0, mType.getCount(), d);
3114fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3124fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3134fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3144fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3154fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3164fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3174fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3184fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3194fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(short[] d) {
3204fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
3214fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        copy1DRangeFromUnchecked(0, mType.getCount(), d);
3224fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3234fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3244fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3254fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3264fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3274fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3284fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3294fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3304fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(byte[] d) {
3314fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
3324fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        copy1DRangeFromUnchecked(0, mType.getCount(), d);
3334fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3354fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3364fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3374fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3384fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3394fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3404fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3414fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(float[] d) {
3424fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
3434fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        copy1DRangeFromUnchecked(0, mType.getCount(), d);
3444fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3454fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
3464fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3474fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
3484fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
3494fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 32 bit integer type.
3504fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3514fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3524fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
353bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(int[] d) {
354771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
355fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        copy1DRangeFrom(0, mType.getCount(), d);
356768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
3574fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
3584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
3604fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
3614fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 16 bit integer type.
3624fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3634fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3644fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
365bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(short[] d) {
366771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
367fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        copy1DRangeFrom(0, mType.getCount(), d);
368768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
3694fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
3704fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3714fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
3724fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
3734fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 8 bit integer type.
3744fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3754fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3764fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
377bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(byte[] d) {
378771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
379fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        copy1DRangeFrom(0, mType.getCount(), d);
380b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
3814fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
3824fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3834fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
3844fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
3854fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 32 bit float type.
3864fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3874fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3884fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
389bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(float[] d) {
390771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
391fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        copy1DRangeFrom(0, mType.getCount(), d);
392b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
3934fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
3944fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3954fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from a bitmap.  The height, width, and
3964fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * format of the bitmap must match the existing allocation.
3974fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3984fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param b the source bitmap
3994fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
400bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(Bitmap b) {
401fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
402fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
403fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
4044ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.nAllocationCopyFromBitmap(getID(), b);
4054ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
40626ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
407fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    /**
408fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
409fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
410fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
411fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
412fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
413fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
41421b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
415a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int eSize = mType.mElement.getSizeBytes();
416a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        final byte[] data = fp.getData();
417a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
418a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int count = data.length / eSize;
419a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if ((eSize * count) != data.length) {
42006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer length " + data.length +
421a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               " not divisible by element size " + eSize + ".");
422a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
42349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        data1DChecks(xoff, count, data.length, data.length);
42449a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
42549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    }
42649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
427fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    /**
428fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
429fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
430fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
431fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
432fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param component_number
433fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
434fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
43521b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
43649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (component_number >= mType.mElement.mElements.length) {
43706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
43849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
43949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if(xoff < 0) {
44006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
44149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
44249bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
44349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        final byte[] data = fp.getData();
44449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        int eSize = mType.mElement.mElements[component_number].getSizeBytes();
44549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
44649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (data.length != eSize) {
44706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
44849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams                                               " does not match component size " + eSize + ".");
44949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
45049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
45149a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationElementData1D(getID(), xoff, 0, component_number, data, data.length);
452a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams    }
453a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
454768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
455771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
456a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(off < 0) {
45706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
458a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
459a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(count < 1) {
46006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
461a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
462bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if((off + count) > mType.getCount()) {
463bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
464a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               ", got " + count + " at offset " + off + ".");
465768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
466768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        if((len) < dataSize) {
46706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
468768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
469b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
470b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
471f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
472f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Generate a mipmap chain.  Requires the type of the allocation
473f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * include mipmaps.
474f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
475f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * This function will generate a complete set of mipmaps from
476f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * the top level lod and place them into the script memoryspace.
477f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
478f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * If the allocation is also using other memory spaces a
479f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * followup sync will be required.
480f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
481f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void generateMipmaps() {
482f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        mRS.nAllocationGenerateMipmaps(getID());
483f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    }
484f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
4854fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
4864fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
4874fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
4884fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
4894fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4904fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
4914fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
4924fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4934fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
4944fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
495768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
496768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
49749a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
498768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
4994fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5004fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5014fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5024fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5034fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5044fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5054fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5064fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5074fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5084fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
509768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
510768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 2, dataSize);
51149a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
512768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5134fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5144fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5154fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5164fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5174fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5184fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5194fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5204fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5214fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5224fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
523768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
524768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length, dataSize);
52549a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
526768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5274fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5284fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5294fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5304fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5314fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5324fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5334fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5354fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5364fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
537768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
538768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
53949a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
540b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
541b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
5424fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5434fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5444fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
5454fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 32 bit integer type.
5464fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5474fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5484fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5494fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5504fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
551b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
552b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
553b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
554b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
5554fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
5564fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5574fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
5594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 16 bit integer type.
5604fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5614fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5624fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5634fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5644fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
565b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
566b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
567b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
568b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
5694fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
5704fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5714fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5724fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
5734fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 8 bit integer type.
5744fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5754fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5764fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5774fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5784fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
579b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
580b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
581b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
582b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
5834fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
5844fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5854fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5864fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
5874fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 32 bit float type.
5884fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5894fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5904fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
591304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param d the source data array.
5924fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
593b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
594b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
595b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
596b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
597b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
598304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     /**
599304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy part of an allocation from another allocation.
600304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
601304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param off The offset of the first element to be copied.
602304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param count The number of elements to be copied.
603304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param data the source data allocation.
604304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataOff off The offset of the first element in data to
605304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *          be copied.
606304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
607304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
608304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        mRS.nAllocationData2D(getID(), off, 0,
60920fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines                              0, Type.CubemapFace.POSITIVE_X.mID,
610304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                              count, 1, data.getID(), dataOff, 0,
61120fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines                              0, Type.CubemapFace.POSITIVE_X.mID);
612304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
613304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
614fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
615fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        if (xoff < 0 || yoff < 0) {
616fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams            throw new RSIllegalArgumentException("Offset cannot be negative.");
617fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
618fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        if (h < 0 || w < 0) {
619fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams            throw new RSIllegalArgumentException("Height or width cannot be negative.");
620fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
621fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        if ((xoff + w) > mType.mDimX ||
622fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams            (yoff + h) > mType.mDimY) {
623fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams            throw new RSIllegalArgumentException("Updated region larger than allocation.");
624fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
625fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
626768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
627f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
628304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy a rectangular region from the array into the allocation.
629304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * The incoming array is assumed to be tightly packed.
630f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
631f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param xoff X offset of the region to update
632f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param yoff Y offset of the region to update
633f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param w Width of the incoming region to update
634f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param h Height of the incoming region to update
635f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param data to be placed into the allocation
636f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
637f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
638fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
639fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
640f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length);
641fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
642fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
643f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
644fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
645fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
646f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 2);
647fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
648fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
649f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
650771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
651fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
652f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
653b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
654b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
655f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
656771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
657fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
658f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
659b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
660b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
661f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
662304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy a rectangular region into the allocation from another
663304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * allocation.
664304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
665304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param xoff X offset of the region to update.
666304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param yoff Y offset of the region to update.
667304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param w Width of the incoming region to update.
668304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param h Height of the incoming region to update.
669304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param data source allocation.
670304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataXoff X offset in data of the region to update.
671304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataYoff Y offset in data of the region to update.
672304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
673304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
674304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                                Allocation data, int dataXoff, int dataYoff) {
675304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        mRS.validate();
676304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        validate2DRange(xoff, yoff, w, h);
677304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        mRS.nAllocationData2D(getID(), xoff, yoff,
67820fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines                              0, Type.CubemapFace.POSITIVE_X.mID,
679304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                              w, h, data.getID(), dataXoff, dataYoff,
68020fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines                              0, Type.CubemapFace.POSITIVE_X.mID);
681304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
682304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
683304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    /**
684f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Copy a bitmap into an allocation.  The height and width of
685f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * the update will use the height and width of the incoming
686f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * bitmap.
687f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
688f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param xoff X offset of the region to update
689f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param yoff Y offset of the region to update
690f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param data the bitmap to be copied
691f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
692f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
693fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
694fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(data);
695fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
696f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, data);
697fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
698fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
699fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
700fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(Bitmap b) {
701fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
702fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
703fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
704fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationCopyToBitmap(getID(), b);
705fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
706fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
707fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(byte[] d) {
708b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
709fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
710fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationRead(getID(), d);
711fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
712fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
713fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(short[] d) {
714b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
715fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
716fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationRead(getID(), d);
717fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
718fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
719fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(int[] d) {
720b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
721771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
72206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
72340a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
72440a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
725fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(float[] d) {
726b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
727771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
72806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
72940a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
73040a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
731f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
732f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Resize a 1D allocation.  The contents of the allocation are
733f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * preserved.  If new elements are allocated objects are created
734f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * with null contents and the new region is otherwise undefined.
735f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
736f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * If the new region is smaller the references of any objects
737f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * outside the new region will be released.
738f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
739f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * A new type will be created with the new dimension.
740f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
741f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param dimX The new size of the allocation.
742f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
74331a7e42f4baa059352f0db119de38428e655eab2Jason Sams    public synchronized void resize(int dimX) {
744bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
74506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
7465edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
74706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize1D(getID(), dimX);
748d26297fa562d8bb203df1bb5e6ded7f62c56cdb7Jason Sams        mRS.finish();  // Necessary because resize is fifoed and update is async.
74931a7e42f4baa059352f0db119de38428e655eab2Jason Sams
75006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
75131a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType = new Type(typeID, mRS);
75231a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType.updateFromNative();
7535edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
7545edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams
7555edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    /*
7565edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    public void resize(int dimX, int dimY) {
7575edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
75806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
7595edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
7605edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if (mType.getY() == 0) {
76106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
7625edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
76306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize2D(getID(), dimX, dimY);
7645edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
7655edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    */
76640a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
767bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
768b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
769b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    // creation
770b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
77149a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
772b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    static {
773b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        mBitmapOptions.inScaled = false;
774b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
775b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
776623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
777623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
778623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
779623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
780623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
781623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
782623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
783623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
784623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
785771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
7865476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (type.getID() == 0) {
78706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Bad Type");
78806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        }
789623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk        int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage);
7905476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
79106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
7921bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams        }
7935476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, type, usage);
794b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
795b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
796623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
797623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with the size specified by
798623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the type and no mipmaps generated by default
799623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
800f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
801623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
802623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
803623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
804623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
805623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
806623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
807e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
808e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
809e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    }
810e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams
811623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
812623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation for use by the script with
813623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the size specified by the type and no mipmaps generated by
814623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * default
815623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
816f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
817623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
818623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
819623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
820623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
8215476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
822d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
8235476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
8241bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams
825623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
826623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with a specified number of
827623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * given elements
828623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
829f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
830623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param e describes what each element of an allocation is
831623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param count specifies the number of element in the allocation
832623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
833623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
834623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
835623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
836623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
8375476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e,
8385476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                         int count, int usage) {
839771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
840768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type.Builder b = new Type.Builder(rs, e);
841bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        b.setX(count);
842768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type t = b.create();
843768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
844d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
8455476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
84606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
847b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
8485476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
8495476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
8505476b450e50939940dcf3f15c92335cee2fc572dJason Sams
851623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
852623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with a specified number of
853623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * given elements
854623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
855f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
856623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param e describes what each element of an allocation is
857623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param count specifies the number of element in the allocation
858623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
859623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
860623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
8615476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
862d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
863b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
864b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
86549a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
8668a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        final Bitmap.Config bc = b.getConfig();
8678a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
8688a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.A_8(rs);
8698a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
8708a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
8718a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_4444(rs);
8728a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
8738a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
8748a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_8888(rs);
8758a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
8768a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.RGB_565) {
8778a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGB_565(rs);
8788a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
8794bd1a3dbcad2ae424293e276434b45ebee97248dJeff Sharkey        throw new RSInvalidStateException("Bad bitmap type: " + bc);
8808a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
8818a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
88249a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
8834ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                       MipmapControl mip) {
8848a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Element e = elementFromBitmap(rs, b);
8858a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
886bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(b.getWidth());
887bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(b.getHeight());
8884ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
8898a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        return tb.create();
8908a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
8918a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
892623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
893623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation from a bitmap
894623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
895f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
896623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap source for the allocation data
897623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
898623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
899623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
900623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
901623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
902623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing bitmap data
903623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
904623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
90567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
9064ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                              MipmapControl mips,
9075476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
908771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
9095476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Type t = typeFromBitmap(rs, b, mips);
9108a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
9115476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
9125476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
91306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Load failed.");
914718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        }
9155476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
9165476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
9175476b450e50939940dcf3f15c92335cee2fc572dJason Sams
918623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
919623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped renderscript allocation to use as a
920623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture
921623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
922f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
923623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap source for the allocation data
924623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
925623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing bitmap data
926623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
927623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
9286d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
9296d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
9306d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                USAGE_GRAPHICS_TEXTURE);
9318a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
9328a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
933fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    /**
934623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a cubemap allocation from a bitmap containing the
935623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * horizontal list of cube faces. Each individual face must be
936623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the same size and power of 2
937623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
938f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
939623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap with cubemap faces layed out in the following
940623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
941623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
942623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
943623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
944623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
945623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
946623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
94767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
9484ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                     MipmapControl mips,
9495476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     int usage) {
95067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        rs.validate();
9515476b450e50939940dcf3f15c92335cee2fc572dJason Sams
95267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int height = b.getHeight();
95367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int width = b.getWidth();
95467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
955fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width % 6 != 0) {
95667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
95767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
958fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width / 6 != height) {
959dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
96067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
961fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
96267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (!isPow2) {
96367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
96467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
96567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
96667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Element e = elementFromBitmap(rs, b);
96767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
968fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setX(height);
969fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setY(height);
970bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setFaces(true);
9714ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
97267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type t = tb.create();
97367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
9745476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
97567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if(id == 0) {
97667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
97767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
9785476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
9795476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
9805476b450e50939940dcf3f15c92335cee2fc572dJason Sams
981623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
982623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped cubemap allocation for use as a
983623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from a bitmap containing the horizontal list
984623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * of cube faces. Each individual face must be the same size and
985623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
986623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
987f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
988623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap with cubemap faces layed out in the following
989623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
990623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
991623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
992623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
993623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
994dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs,
995dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                     Bitmap b) {
9966d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
997fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk                                       USAGE_GRAPHICS_TEXTURE);
99867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
99967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
1000623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1001623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a cubemap allocation from 6 bitmaps containing
1002623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the cube faces. All the faces must be the same size and
1003623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1004623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1005f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1006623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1007623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1008623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1009623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1010623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1011623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1012623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
1013623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
1014623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1015623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1016623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1017623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1018dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1019dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1020dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1021dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1022dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1023dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1024dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg,
1025dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        MipmapControl mips,
1026dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        int usage) {
1027dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        int height = xpos.getHeight();
1028dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (xpos.getWidth() != height ||
1029dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            xneg.getWidth() != height || xneg.getHeight() != height ||
1030dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            ypos.getWidth() != height || ypos.getHeight() != height ||
1031dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            yneg.getWidth() != height || yneg.getHeight() != height ||
1032dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zpos.getWidth() != height || zpos.getHeight() != height ||
1033dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zneg.getWidth() != height || zneg.getHeight() != height) {
1034dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
1035dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1036dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
1037dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (!isPow2) {
1038dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1039dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1040dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1041dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Element e = elementFromBitmap(rs, xpos);
1042dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
1043dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setX(height);
1044dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setY(height);
1045dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setFaces(true);
1046dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1047dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type t = tb.create();
1048dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1049dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1050dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
105120fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_X);
1052dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xpos);
1053dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1054dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xneg);
105520fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
1056dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(ypos);
1057dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1058dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(yneg);
105920fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
1060dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zpos);
1061dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1062dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zneg);
1063dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1064dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return cubemap;
1065dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1066dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1067623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1068623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped cubemap allocation for use as a
1069623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from 6 bitmaps containing
1070623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the cube faces. All the faces must be the same size and
1071623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1072623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1073f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1074623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1075623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1076623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1077623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1078623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1079623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1080623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1081623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1082623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1083623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1084dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1085dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1086dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1087dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1088dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1089dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1090dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg) {
1091dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1092dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
1093dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          USAGE_GRAPHICS_TEXTURE);
1094dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1095dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1096623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1097623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation from the bitmap referenced
1098623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * by resource id
1099623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1100f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1101623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1102623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1103623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
1104623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
1105623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1106623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1107623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1108623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing resource data
1109623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1110623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
11115476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
11125476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
11135476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int id,
11144ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                      MipmapControl mips,
11155476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int usage) {
1116b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1117771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
11185476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Bitmap b = BitmapFactory.decodeResource(res, id);
11195476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Allocation alloc = createFromBitmap(rs, b, mips, usage);
11205476b450e50939940dcf3f15c92335cee2fc572dJason Sams        b.recycle();
11215476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return alloc;
11225476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
1123650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
1124623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1125623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped renderscript allocation to use as a
1126623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from the bitmap referenced by resource id
1127623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1128f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1129623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1130623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1131623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1132623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing resource data
1133623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1134623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
11355476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
11365476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
11376d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                                      int id) {
11386d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmapResource(rs, res, id,
11396d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        MipmapControl.MIPMAP_NONE,
11406d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        USAGE_GRAPHICS_TEXTURE);
11416d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    }
11426d8eb266dd398abf0511685fdaf98abba3396174Jason Sams
1143623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1144623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation containing string data
1145623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * encoded in UTF-8 format
1146623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1147f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1148623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param str string to create the allocation from
1149623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocaiton is
1150623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1151623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1152623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
11535476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromString(RenderScript rs,
11545476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              String str,
11555476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
11565476b450e50939940dcf3f15c92335cee2fc572dJason Sams        rs.validate();
11579b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        byte[] allocArray = null;
11589b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        try {
11599b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            allocArray = str.getBytes("UTF-8");
11605476b450e50939940dcf3f15c92335cee2fc572dJason Sams            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
1161bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            alloc.copyFrom(allocArray);
11629b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            return alloc;
11639b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
11649b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        catch (Exception e) {
116506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Could not convert string to utf-8.");
11669b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
11679b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk    }
1168b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams}
1169b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1170b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1171