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;
70ba862d1544a06528151550be1784a926ee986580Jason Sams    Allocation mAdaptedAllocation;
71ba862d1544a06528151550be1784a926ee986580Jason Sams
72ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedLOD;
73ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedFace;
74ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedY;
75ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedZ;
76ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedY;
77ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedZ;
78ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedLOD;
79ba862d1544a06528151550be1784a926ee986580Jason Sams    Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
80ba862d1544a06528151550be1784a926ee986580Jason Sams
81ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimX;
82ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimY;
83ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimZ;
84ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentCount;
85ba862d1544a06528151550be1784a926ee986580Jason Sams
865476b450e50939940dcf3f15c92335cee2fc572dJason Sams
87f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
88f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * The usage of the allocation.  These signal to renderscript
89f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * where to place the allocation in memory.
90f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
91f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * SCRIPT The allocation will be bound to and accessed by
92f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * scripts.
93f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
945476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_SCRIPT = 0x0001;
95f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
96f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
97f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_TEXTURE The allcation will be used as a texture
98836c4a58a7f03485ef433dcdb61837cbc0c39735Stephen Hines     * source by one or more graphics programs.
99f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
100f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1015476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
102f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
103f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
104f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_VERTEX The allocation will be used as a graphics
105f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * mesh.
106f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
107f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1085476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
109f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
110f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
111f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
112f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_CONSTANTS The allocation will be used as the source
113f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * of shader constants by one or more programs.
114f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
115f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1165476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
1175476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1188e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk    /**
1198e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     * USAGE_GRAPHICS_RENDER_TARGET The allcation will be used as a
1208e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     * target for offscreen rendering
1218e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     *
1228e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     */
1238e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk    public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
1248e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk
12543ee06857bb7f99446d1d84f8789016c5d105558Jason Sams
126f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
127f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Controls mipmap behavior when using the bitmap creation and
128f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * update functions.
129f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1304ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public enum MipmapControl {
131f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        /**
132f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * No mipmaps will be generated and the type generated from the
133f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * incoming bitmap will not contain additional LODs.
134f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1355476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_NONE(0),
136f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
137f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        /**
138f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * A Full mipmap chain will be created in script memory.  The
139f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * type of the allocation will contain a full mipmap chain.  On
140f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * upload to graphics the full chain will be transfered.
141f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1425476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_FULL(1),
143f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
144f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        /**
145f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * The type of the allocation will be the same as MIPMAP_NONE.
146f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * It will not contain mipmaps.  On upload to graphics the
147f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * graphics copy of the allocation data will contain a full
148f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * mipmap chain generated from the top level in script memory.
149f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1505476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
1515476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1525476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int mID;
1534ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl(int id) {
1545476b450e50939940dcf3f15c92335cee2fc572dJason Sams            mID = id;
1555476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
156b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
157b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
15848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams
15948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    private int getIDSafe() {
16048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        if (mAdaptedAllocation != null) {
16148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams            return mAdaptedAllocation.getID();
16248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        }
16348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        return getID();
16448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    }
16548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams
166452a7661e8b06459b75493b441d33244939c1153Jason Sams    private void updateCacheInfo(Type t) {
167452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimX = t.getX();
168452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimY = t.getY();
169452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimZ = t.getZ();
170452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentCount = mCurrentDimX;
171452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (mCurrentDimY > 1) {
172452a7661e8b06459b75493b441d33244939c1153Jason Sams            mCurrentCount *= mCurrentDimY;
173452a7661e8b06459b75493b441d33244939c1153Jason Sams        }
174452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (mCurrentDimZ > 1) {
175452a7661e8b06459b75493b441d33244939c1153Jason Sams            mCurrentCount *= mCurrentDimZ;
176452a7661e8b06459b75493b441d33244939c1153Jason Sams        }
177452a7661e8b06459b75493b441d33244939c1153Jason Sams    }
178ba862d1544a06528151550be1784a926ee986580Jason Sams
1795476b450e50939940dcf3f15c92335cee2fc572dJason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
1800de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
18149a05d7b82956009f03acbb92a064eed054eb031Jason Sams        if ((usage & ~(USAGE_SCRIPT |
18249a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_TEXTURE |
18349a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_VERTEX |
1848e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk                       USAGE_GRAPHICS_CONSTANTS |
1858e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk                       USAGE_GRAPHICS_RENDER_TARGET)) != 0) {
1865476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
1875476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
1885476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mType = t;
189ba862d1544a06528151550be1784a926ee986580Jason Sams
190452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (t != null) {
191452a7661e8b06459b75493b441d33244939c1153Jason Sams            updateCacheInfo(t);
192ba862d1544a06528151550be1784a926ee986580Jason Sams        }
19380a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk    }
19480a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk
195b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt32() {
196b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
197b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
198b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
199b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
200b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
201b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
202b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
203b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
204b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt16() {
205b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
206b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
207b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
208b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
209b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
210b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
211b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
212b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
213b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt8() {
214b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
215b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
216b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
217b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
218b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
219b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
220b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
221b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
222b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsFloat32() {
223b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
224b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
225b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
226b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
227b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
228b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
229b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
230b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsObject() {
231b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
232b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
233b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
234b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
235b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
236b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_MESH) ||
237b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
238b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
239b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
240b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
241b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
242b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
243b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
244b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
245b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
246b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
247dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    @Override
248dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    void updateFromNative() {
24906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        super.updateFromNative();
25006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
251dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        if(typeID != 0) {
252dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType = new Type(typeID, mRS);
253dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType.updateFromNative();
254ad37cb26cd8d8a05077152ebc5b841a5629cfbbdJason Sams            updateCacheInfo(mType);
255dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        }
256dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    }
257dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk
258ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    public Type getType() {
259ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams        return mType;
260ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    }
261ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams
2625476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public void syncAll(int srcLocation) {
2635476b450e50939940dcf3f15c92335cee2fc572dJason Sams        switch (srcLocation) {
2645476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_SCRIPT:
2655476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_CONSTANTS:
2665476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_TEXTURE:
2675476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_VERTEX:
2685476b450e50939940dcf3f15c92335cee2fc572dJason Sams            break;
2695476b450e50939940dcf3f15c92335cee2fc572dJason Sams        default:
2705476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
2715476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
2725476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.validate();
27348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
2745476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
2755476b450e50939940dcf3f15c92335cee2fc572dJason Sams
276bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(BaseObj[] d) {
277bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        mRS.validate();
278b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsObject();
279ba862d1544a06528151550be1784a926ee986580Jason Sams        if (d.length != mCurrentCount) {
280bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
281ba862d1544a06528151550be1784a926ee986580Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
282bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
283bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        int i[] = new int[d.length];
284bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        for (int ct=0; ct < d.length; ct++) {
285bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            i[ct] = d[ct].getID();
286bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
287ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, i);
288bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    }
289bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams
290fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapFormat(Bitmap b) {
291252c07802f7039f15f723751162e64a6621e6998Jason Sams        Bitmap.Config bc = b.getConfig();
292252c07802f7039f15f723751162e64a6621e6998Jason Sams        switch (bc) {
293252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ALPHA_8:
294252c07802f7039f15f723751162e64a6621e6998Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
295252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
296252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
297252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
298252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
299252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
300252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
301252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
302252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_8888:
303252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
304252c07802f7039f15f723751162e64a6621e6998Jason Sams                (mType.getElement().getSizeBytes() != 4)) {
305252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
306252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
307252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
308252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
309252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
310252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
311252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
312252c07802f7039f15f723751162e64a6621e6998Jason Sams        case RGB_565:
313252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
314252c07802f7039f15f723751162e64a6621e6998Jason Sams                (mType.getElement().getSizeBytes() != 2)) {
315252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
316252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
317252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
318252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
319252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
320252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
321252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
322252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_4444:
323252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
324252c07802f7039f15f723751162e64a6621e6998Jason Sams                (mType.getElement().getSizeBytes() != 2)) {
325252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
326252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
327252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
328252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
329252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
330252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
331252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
332252c07802f7039f15f723751162e64a6621e6998Jason Sams
333252c07802f7039f15f723751162e64a6621e6998Jason Sams        }
3344ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
3354ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
336fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapSize(Bitmap b) {
337ba862d1544a06528151550be1784a926ee986580Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
338fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
339fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
340fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
341fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams
3424fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3434fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3444fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3454fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3464fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3474fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3484fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3494fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(int[] d) {
3504fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
351ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
3524fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3534fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3544fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3554fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3564fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3574fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3604fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(short[] d) {
3614fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
362ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
3634fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3644fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3654fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3664fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3674fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3684fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3694fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3704fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3714fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(byte[] d) {
3724fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
373ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
3744fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3754fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3764fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3774fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3784fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3794fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3804fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3814fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3824fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(float[] d) {
3834fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
384ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
3854fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3864fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
3874fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3884fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
3894fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
3904fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 32 bit integer type.
3914fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3924fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3934fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
394bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(int[] d) {
395771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
396ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
397768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
3984fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
3994fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
4004fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
4014fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
4024fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 16 bit integer type.
4034fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4044fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4054fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
406bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(short[] d) {
407771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
408ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
409768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
4104fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
4114fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
4124fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
4134fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
4144fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 8 bit integer type.
4154fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4164fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4174fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
418bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(byte[] d) {
419771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
420ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
421b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
4224fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
4234fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
4244fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
4254fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
4264fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 32 bit float type.
4274fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4284fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4294fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
430bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(float[] d) {
431771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
432ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
433b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
4344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
4354fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
4364fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from a bitmap.  The height, width, and
4374fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * format of the bitmap must match the existing allocation.
4384fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4394fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param b the source bitmap
4404fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
441bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(Bitmap b) {
442fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
443fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
444fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
4454ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.nAllocationCopyFromBitmap(getID(), b);
4464ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
44726ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
448fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    /**
449fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
450fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
451fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
452fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
453fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
454fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
45521b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
456a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int eSize = mType.mElement.getSizeBytes();
457a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        final byte[] data = fp.getData();
458a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
459a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int count = data.length / eSize;
460a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if ((eSize * count) != data.length) {
46106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer length " + data.length +
462a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               " not divisible by element size " + eSize + ".");
463a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
464ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
46549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    }
46649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
467fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    /**
468fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
469fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
470fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
471fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
472fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param component_number
473fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
474fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
47521b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
47649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (component_number >= mType.mElement.mElements.length) {
47706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
47849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
47949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if(xoff < 0) {
48006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
48149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
48249bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
48349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        final byte[] data = fp.getData();
48449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        int eSize = mType.mElement.mElements[component_number].getSizeBytes();
48549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
48649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (data.length != eSize) {
48706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
48849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams                                               " does not match component size " + eSize + ".");
48949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
49049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
49148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
492ba862d1544a06528151550be1784a926ee986580Jason Sams                                     component_number, data, data.length);
493a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams    }
494a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
495768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
496771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
497a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(off < 0) {
49806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
499a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
500a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(count < 1) {
50106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
502a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
503ba862d1544a06528151550be1784a926ee986580Jason Sams        if((off + count) > mCurrentCount) {
504ba862d1544a06528151550be1784a926ee986580Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
505a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               ", got " + count + " at offset " + off + ".");
506768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
507ba862d1544a06528151550be1784a926ee986580Jason Sams        if(len < dataSize) {
50806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
509768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
510b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
511b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
512f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
513f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Generate a mipmap chain.  Requires the type of the allocation
514f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * include mipmaps.
515f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
516f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * This function will generate a complete set of mipmaps from
517f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * the top level lod and place them into the script memoryspace.
518f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
519f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * If the allocation is also using other memory spaces a
520f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * followup sync will be required.
521f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
522f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void generateMipmaps() {
523f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        mRS.nAllocationGenerateMipmaps(getID());
524f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    }
525f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
5264fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5274fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5284fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5294fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5304fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5314fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5324fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5334fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5354fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
536768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
537768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
53848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
539768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5404fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5414fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5424fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5434fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5444fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5454fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5464fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5474fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5484fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5494fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
550768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
551768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 2, dataSize);
55248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
553768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5544fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5554fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5564fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5574fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5604fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5614fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5624fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5634fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
564768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
565768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length, dataSize);
56648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
567768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5684fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5694fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5704fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5714fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5724fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5734fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5744fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5754fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5764fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5774fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
578768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
579768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
58048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
581b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
582b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
5834fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5844fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5854fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
5864fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 32 bit integer type.
5874fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5884fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5894fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5904fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5914fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
592b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
593b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
594b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
595b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
5964fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
5974fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5984fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5994fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
6004fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 16 bit integer type.
6014fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6024fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6034fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
6044fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6054fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
606b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
607b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
608b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
609b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
6104fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
6114fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
6124fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
6134fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
6144fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 8 bit integer type.
6154fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6164fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6174fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
6184fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6194fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
620b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
621b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
622b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
623b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
6244fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
6254fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
6264fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
6274fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
6284fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 32 bit float type.
6294fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6304fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6314fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
632304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param d the source data array.
6334fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
634b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
635b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
636b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
637b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
638b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
639304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     /**
640304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy part of an allocation from another allocation.
641304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
642304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param off The offset of the first element to be copied.
643304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param count The number of elements to be copied.
644304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param data the source data allocation.
645304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataOff off The offset of the first element in data to
646304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *          be copied.
647304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
648304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
64948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
650ba862d1544a06528151550be1784a926ee986580Jason Sams                              mSelectedLOD, mSelectedFace.mID,
651304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                              count, 1, data.getID(), dataOff, 0,
652ba862d1544a06528151550be1784a926ee986580Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
653304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
654304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
655fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
656ba862d1544a06528151550be1784a926ee986580Jason Sams        if (mAdaptedAllocation != null) {
657ba862d1544a06528151550be1784a926ee986580Jason Sams
658ba862d1544a06528151550be1784a926ee986580Jason Sams        } else {
659ba862d1544a06528151550be1784a926ee986580Jason Sams
660ba862d1544a06528151550be1784a926ee986580Jason Sams            if (xoff < 0 || yoff < 0) {
661ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
662ba862d1544a06528151550be1784a926ee986580Jason Sams            }
663ba862d1544a06528151550be1784a926ee986580Jason Sams            if (h < 0 || w < 0) {
664ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
665ba862d1544a06528151550be1784a926ee986580Jason Sams            }
666ba862d1544a06528151550be1784a926ee986580Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
667ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
668ba862d1544a06528151550be1784a926ee986580Jason Sams            }
669fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
670fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
671768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
672f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
673304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy a rectangular region from the array into the allocation.
674304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * The incoming array is assumed to be tightly packed.
675f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
676f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param xoff X offset of the region to update
677f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param yoff Y offset of the region to update
678f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param w Width of the incoming region to update
679f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param h Height of the incoming region to update
680f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param data to be placed into the allocation
681f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
682f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
683fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
684fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
68548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
686ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length);
687fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
688fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
689f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
690fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
691fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
69248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
693ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length * 2);
694fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
695fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
696f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
697771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
698fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
69948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
700ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length * 4);
701b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
702b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
703f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
704771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
705fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
70648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
707ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length * 4);
708b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
709b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
710f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
711304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy a rectangular region into the allocation from another
712304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * allocation.
713304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
714304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param xoff X offset of the region to update.
715304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param yoff Y offset of the region to update.
716304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param w Width of the incoming region to update.
717304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param h Height of the incoming region to update.
718304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param data source allocation.
719304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataXoff X offset in data of the region to update.
720304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataYoff Y offset in data of the region to update.
721304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
722304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
723304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                                Allocation data, int dataXoff, int dataYoff) {
724304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        mRS.validate();
725304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        validate2DRange(xoff, yoff, w, h);
72648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
727ba862d1544a06528151550be1784a926ee986580Jason Sams                              mSelectedLOD, mSelectedFace.mID,
728304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                              w, h, data.getID(), dataXoff, dataYoff,
729ba862d1544a06528151550be1784a926ee986580Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
730304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
731304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
732304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    /**
733f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Copy a bitmap into an allocation.  The height and width of
734f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * the update will use the height and width of the incoming
735f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * bitmap.
736f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
737f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param xoff X offset of the region to update
738f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param yoff Y offset of the region to update
739f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param data the bitmap to be copied
740f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
741f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
742fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
743fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(data);
744fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
74548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
746fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
747fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
748fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
74948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
75048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a Bitmap.  The bitmap must
75148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * match the dimensions of the Allocation.
75248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
75348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param b The bitmap to be set from the Allocation.
75448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
755fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(Bitmap b) {
756fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
757fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
758fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
759fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationCopyToBitmap(getID(), b);
760fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
761fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
76248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
76348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a byte array.  The array must
76448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be at least as large as the Allocation.  The allocation must
76548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be of an 8 bit elemental type.
76648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
76748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
76848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
769fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(byte[] d) {
770b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
771fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
772fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationRead(getID(), d);
773fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
774fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
77548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
77648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a short array.  The array must
77748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be at least as large as the Allocation.  The allocation must
77848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be of an 16 bit elemental type.
77948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
78048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
78148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
782fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(short[] d) {
783b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
784fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
785fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationRead(getID(), d);
786fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
787fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
78848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
78948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a int array.  The array must be
79048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * at least as large as the Allocation.  The allocation must be
79148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * of an 32 bit elemental type.
79248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
79348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
79448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
795fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(int[] d) {
796b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
797771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
79806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
79940a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
80040a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
80148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
80248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a float array.  The array must
80348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be at least as large as the Allocation.  The allocation must
80448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be of an 32 bit float elemental type.
80548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
80648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
80748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
808fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(float[] d) {
809b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
810771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
81106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
81240a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
81340a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
814f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
815f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Resize a 1D allocation.  The contents of the allocation are
816f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * preserved.  If new elements are allocated objects are created
817f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * with null contents and the new region is otherwise undefined.
818f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
819f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * If the new region is smaller the references of any objects
820f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * outside the new region will be released.
821f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
822f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * A new type will be created with the new dimension.
823f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
824f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param dimX The new size of the allocation.
825f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
82631a7e42f4baa059352f0db119de38428e655eab2Jason Sams    public synchronized void resize(int dimX) {
827bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
82806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
8295edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
83006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize1D(getID(), dimX);
831d26297fa562d8bb203df1bb5e6ded7f62c56cdb7Jason Sams        mRS.finish();  // Necessary because resize is fifoed and update is async.
83231a7e42f4baa059352f0db119de38428e655eab2Jason Sams
83306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
83431a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType = new Type(typeID, mRS);
83531a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType.updateFromNative();
836452a7661e8b06459b75493b441d33244939c1153Jason Sams        updateCacheInfo(mType);
8375edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
8385edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams
8395edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    /*
8405edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    public void resize(int dimX, int dimY) {
8415edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
84206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
8435edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
8445edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if (mType.getY() == 0) {
84506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
8465edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
84706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize2D(getID(), dimX, dimY);
8485edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
8495edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    */
85040a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
851bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
852b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
853b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    // creation
854b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
85549a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
856b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    static {
857b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        mBitmapOptions.inScaled = false;
858b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
859b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
860623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
861623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
862623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
863623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
864623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
865623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
866623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
867623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
868623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
869771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
8705476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (type.getID() == 0) {
87106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Bad Type");
87206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        }
873623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk        int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage);
8745476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
87506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
8761bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams        }
8775476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, type, usage);
878b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
879b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
880623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
881623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with the size specified by
882623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the type and no mipmaps generated by default
883623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
884f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
885623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
886623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
887623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
888623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
889623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
890623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
891e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
892e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
893e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    }
894e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams
895623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
896623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation for use by the script with
897623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the size specified by the type and no mipmaps generated by
898623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * default
899623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
900f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
901623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
902623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
903623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
904623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
9055476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
906d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
9075476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
9081bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams
909623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
910623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with a specified number of
911623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * given elements
912623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
913f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
914623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param e describes what each element of an allocation is
915623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param count specifies the number of element in the allocation
916623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
917623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
918623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
919623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
920623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
9215476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e,
9225476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                         int count, int usage) {
923771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
924768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type.Builder b = new Type.Builder(rs, e);
925bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        b.setX(count);
926768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type t = b.create();
927768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
928d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
9295476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
93006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
931b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
9325476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
9335476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
9345476b450e50939940dcf3f15c92335cee2fc572dJason Sams
935623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
936623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with a specified number of
937623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * given elements
938623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
939f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
940623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param e describes what each element of an allocation is
941623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param count specifies the number of element in the allocation
942623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
943623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
944623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
9455476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
946d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
947b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
948b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
94949a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
9508a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        final Bitmap.Config bc = b.getConfig();
9518a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
9528a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.A_8(rs);
9538a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
9548a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
9558a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_4444(rs);
9568a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
9578a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
9588a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_8888(rs);
9598a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
9608a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.RGB_565) {
9618a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGB_565(rs);
9628a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
9634bd1a3dbcad2ae424293e276434b45ebee97248dJeff Sharkey        throw new RSInvalidStateException("Bad bitmap type: " + bc);
9648a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
9658a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
96649a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
9674ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                       MipmapControl mip) {
9688a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Element e = elementFromBitmap(rs, b);
9698a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
970bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(b.getWidth());
971bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(b.getHeight());
9724ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
9738a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        return tb.create();
9748a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
9758a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
976623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
977623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation from a bitmap
978623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
979f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
980623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap source for the allocation data
981623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
982623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
983623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
984623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
985623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
986623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing bitmap data
987623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
988623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
98967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
9904ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                              MipmapControl mips,
9915476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
992771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
9935476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Type t = typeFromBitmap(rs, b, mips);
9948a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
9955476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
9965476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
99706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Load failed.");
998718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        }
9995476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
10005476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
10015476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1002623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1003623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped renderscript allocation to use as a
1004623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture
1005623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1006f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1007623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap source for the allocation data
1008623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1009623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing bitmap data
1010623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1011623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
10126d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
10136d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
10146d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                USAGE_GRAPHICS_TEXTURE);
10158a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
10168a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
1017fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    /**
1018623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a cubemap allocation from a bitmap containing the
1019623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * horizontal list of cube faces. Each individual face must be
1020623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the same size and power of 2
1021623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1022f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1023623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap with cubemap faces layed out in the following
1024623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
1025623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
1026623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
1027623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1028623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1029623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1030623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
103167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
10324ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                     MipmapControl mips,
10335476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     int usage) {
103467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        rs.validate();
10355476b450e50939940dcf3f15c92335cee2fc572dJason Sams
103667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int height = b.getHeight();
103767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int width = b.getWidth();
103867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
1039fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width % 6 != 0) {
104067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
104167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
1042fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width / 6 != height) {
1043dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
104467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
1045fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
104667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (!isPow2) {
104767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
104867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
104967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
105067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Element e = elementFromBitmap(rs, b);
105167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
1052fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setX(height);
1053fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setY(height);
1054bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setFaces(true);
10554ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
105667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type t = tb.create();
105767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
10585476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
105967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if(id == 0) {
106067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
106167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
10625476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
10635476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
10645476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1065623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1066623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped cubemap allocation for use as a
1067623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from a bitmap containing the horizontal list
1068623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * of cube faces. Each individual face must be the same size and
1069623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1070623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1071f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1072623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap with cubemap faces layed out in the following
1073623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
1074623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1075623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1076623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1077623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1078dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs,
1079dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                     Bitmap b) {
10806d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1081fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk                                       USAGE_GRAPHICS_TEXTURE);
108267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
108367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
1084623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1085623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a cubemap allocation from 6 bitmaps containing
1086623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the cube faces. All the faces must be the same size and
1087623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1088623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1089f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1090623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1091623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1092623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1093623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1094623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1095623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1096623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
1097623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
1098623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1099623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1100623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1101623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1102dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1103dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1104dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1105dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1106dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1107dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1108dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg,
1109dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        MipmapControl mips,
1110dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        int usage) {
1111dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        int height = xpos.getHeight();
1112dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (xpos.getWidth() != height ||
1113dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            xneg.getWidth() != height || xneg.getHeight() != height ||
1114dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            ypos.getWidth() != height || ypos.getHeight() != height ||
1115dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            yneg.getWidth() != height || yneg.getHeight() != height ||
1116dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zpos.getWidth() != height || zpos.getHeight() != height ||
1117dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zneg.getWidth() != height || zneg.getHeight() != height) {
1118dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
1119dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1120dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
1121dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (!isPow2) {
1122dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1123dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1124dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1125dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Element e = elementFromBitmap(rs, xpos);
1126dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
1127dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setX(height);
1128dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setY(height);
1129dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setFaces(true);
1130dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1131dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type t = tb.create();
1132dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1133dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1134dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
113520fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_X);
1136dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xpos);
1137dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1138dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xneg);
113920fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
1140dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(ypos);
1141dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1142dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(yneg);
114320fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
1144dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zpos);
1145dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1146dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zneg);
1147dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1148dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return cubemap;
1149dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1150dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1151623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1152623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped cubemap allocation for use as a
1153623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from 6 bitmaps containing
1154623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the cube faces. All the faces must be the same size and
1155623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1156623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1157f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1158623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1159623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1160623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1161623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1162623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1163623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1164623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1165623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1166623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1167623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1168dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1169dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1170dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1171dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1172dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1173dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1174dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg) {
1175dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1176dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
1177dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          USAGE_GRAPHICS_TEXTURE);
1178dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1179dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1180623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1181623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation from the bitmap referenced
1182623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * by resource id
1183623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1184f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1185623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1186623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1187623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
1188623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
1189623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1190623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1191623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1192623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing resource data
1193623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1194623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
11955476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
11965476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
11975476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int id,
11984ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                      MipmapControl mips,
11995476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int usage) {
1200b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1201771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
12025476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Bitmap b = BitmapFactory.decodeResource(res, id);
12035476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Allocation alloc = createFromBitmap(rs, b, mips, usage);
12045476b450e50939940dcf3f15c92335cee2fc572dJason Sams        b.recycle();
12055476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return alloc;
12065476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
1207650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
1208623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1209623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped renderscript allocation to use as a
1210623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from the bitmap referenced by resource id
1211623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1212f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1213623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1214623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1215623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1216623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing resource data
1217623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1218623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
12195476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
12205476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
12216d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                                      int id) {
12226d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmapResource(rs, res, id,
12236d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        MipmapControl.MIPMAP_NONE,
12246d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        USAGE_GRAPHICS_TEXTURE);
12256d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    }
12266d8eb266dd398abf0511685fdaf98abba3396174Jason Sams
1227623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1228623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation containing string data
1229623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * encoded in UTF-8 format
1230623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1231f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1232623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param str string to create the allocation from
1233623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocaiton is
1234623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1235623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1236623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
12375476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromString(RenderScript rs,
12385476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              String str,
12395476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
12405476b450e50939940dcf3f15c92335cee2fc572dJason Sams        rs.validate();
12419b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        byte[] allocArray = null;
12429b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        try {
12439b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            allocArray = str.getBytes("UTF-8");
12445476b450e50939940dcf3f15c92335cee2fc572dJason Sams            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
1245bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            alloc.copyFrom(allocArray);
12469b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            return alloc;
12479b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
12489b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        catch (Exception e) {
124906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Could not convert string to utf-8.");
12509b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
12519b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk    }
1252b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams}
1253b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1254b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1255