Allocation.java revision 3aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45
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 *
653aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
663aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
673aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about creating an application that uses Renderscript, read the
683aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
693aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
70b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams **/
71b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samspublic class Allocation extends BaseObj {
7243ee06857bb7f99446d1d84f8789016c5d105558Jason Sams    Type mType;
738a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    Bitmap mBitmap;
745476b450e50939940dcf3f15c92335cee2fc572dJason Sams    int mUsage;
75ba862d1544a06528151550be1784a926ee986580Jason Sams    Allocation mAdaptedAllocation;
76ba862d1544a06528151550be1784a926ee986580Jason Sams
77ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedLOD;
78ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedFace;
79ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedY;
80ba862d1544a06528151550be1784a926ee986580Jason Sams    boolean mConstrainedZ;
81ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedY;
82ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedZ;
83ba862d1544a06528151550be1784a926ee986580Jason Sams    int mSelectedLOD;
84ba862d1544a06528151550be1784a926ee986580Jason Sams    Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
85ba862d1544a06528151550be1784a926ee986580Jason Sams
86ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimX;
87ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimY;
88ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentDimZ;
89ba862d1544a06528151550be1784a926ee986580Jason Sams    int mCurrentCount;
90ba862d1544a06528151550be1784a926ee986580Jason Sams
915476b450e50939940dcf3f15c92335cee2fc572dJason Sams
92f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
93f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * The usage of the allocation.  These signal to renderscript
94f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * where to place the allocation in memory.
95f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
96f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * SCRIPT The allocation will be bound to and accessed by
97f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * scripts.
98f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
995476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_SCRIPT = 0x0001;
100f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
101f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
102f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_TEXTURE The allcation will be used as a texture
103836c4a58a7f03485ef433dcdb61837cbc0c39735Stephen Hines     * source by one or more graphics programs.
104f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
105f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1065476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
107f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
108f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
109f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_VERTEX The allocation will be used as a graphics
110f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * mesh.
111f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
112f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1135476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
114f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
115f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
116f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
117f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * GRAPHICS_CONSTANTS The allocation will be used as the source
118f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * of shader constants by one or more programs.
119f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
120f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1215476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
1225476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1238e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk    /**
1248e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     * USAGE_GRAPHICS_RENDER_TARGET The allcation will be used as a
1258e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     * target for offscreen rendering
1268e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     *
1278e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk     */
1288e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk    public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
1298e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk
13043ee06857bb7f99446d1d84f8789016c5d105558Jason Sams
131f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
132f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Controls mipmap behavior when using the bitmap creation and
133f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * update functions.
134f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
1354ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public enum MipmapControl {
136f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        /**
137f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * No mipmaps will be generated and the type generated from the
138f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * incoming bitmap will not contain additional LODs.
139f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1405476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_NONE(0),
141f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
142f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        /**
143f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * A Full mipmap chain will be created in script memory.  The
144f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * type of the allocation will contain a full mipmap chain.  On
145f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * upload to graphics the full chain will be transfered.
146f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1475476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_FULL(1),
148f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
149f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        /**
150f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * The type of the allocation will be the same as MIPMAP_NONE.
151f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * It will not contain mipmaps.  On upload to graphics the
152f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * graphics copy of the allocation data will contain a full
153f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         * mipmap chain generated from the top level in script memory.
154f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams         */
1555476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
1565476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1575476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int mID;
1584ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl(int id) {
1595476b450e50939940dcf3f15c92335cee2fc572dJason Sams            mID = id;
1605476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
161b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
162b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
16348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams
16448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    private int getIDSafe() {
16548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        if (mAdaptedAllocation != null) {
16648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams            return mAdaptedAllocation.getID();
16748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        }
16848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        return getID();
16948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    }
17048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams
171452a7661e8b06459b75493b441d33244939c1153Jason Sams    private void updateCacheInfo(Type t) {
172452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimX = t.getX();
173452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimY = t.getY();
174452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentDimZ = t.getZ();
175452a7661e8b06459b75493b441d33244939c1153Jason Sams        mCurrentCount = mCurrentDimX;
176452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (mCurrentDimY > 1) {
177452a7661e8b06459b75493b441d33244939c1153Jason Sams            mCurrentCount *= mCurrentDimY;
178452a7661e8b06459b75493b441d33244939c1153Jason Sams        }
179452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (mCurrentDimZ > 1) {
180452a7661e8b06459b75493b441d33244939c1153Jason Sams            mCurrentCount *= mCurrentDimZ;
181452a7661e8b06459b75493b441d33244939c1153Jason Sams        }
182452a7661e8b06459b75493b441d33244939c1153Jason Sams    }
183ba862d1544a06528151550be1784a926ee986580Jason Sams
1845476b450e50939940dcf3f15c92335cee2fc572dJason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
1850de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
18649a05d7b82956009f03acbb92a064eed054eb031Jason Sams        if ((usage & ~(USAGE_SCRIPT |
18749a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_TEXTURE |
18849a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_VERTEX |
1898e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk                       USAGE_GRAPHICS_CONSTANTS |
1908e90f2bc1fa35a2dc7bd2aab8b8241b628800218Alex Sakhartchouk                       USAGE_GRAPHICS_RENDER_TARGET)) != 0) {
1915476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
1925476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
1935476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mType = t;
194ba862d1544a06528151550be1784a926ee986580Jason Sams
195452a7661e8b06459b75493b441d33244939c1153Jason Sams        if (t != null) {
196452a7661e8b06459b75493b441d33244939c1153Jason Sams            updateCacheInfo(t);
197ba862d1544a06528151550be1784a926ee986580Jason Sams        }
19880a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk    }
19980a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk
200b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt32() {
201b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
202b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
203b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
204b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
205b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
206b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit integer source does not match allocation type " + mType.mElement.mType);
207b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
208b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
209b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt16() {
210b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
211b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
212b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
213b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
214b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
215b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "16 bit integer source does not match allocation type " + mType.mElement.mType);
216b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
217b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
218b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsInt8() {
219b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
220b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
221b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
222b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
223b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
224b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "8 bit integer source does not match allocation type " + mType.mElement.mType);
225b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
226b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
227b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsFloat32() {
228b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if (mType.mElement.mType == Element.DataType.FLOAT_32) {
229b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
230b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
231b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
232b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "32 bit float source does not match allocation type " + mType.mElement.mType);
233b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
234b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
235b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    private void validateIsObject() {
236b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
237b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_TYPE) ||
238b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
239b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
240b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
241b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_MESH) ||
242b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
243b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
244b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
245b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            (mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
246b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            return;
247b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        }
248b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        throw new RSIllegalArgumentException(
249b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams            "Object source does not match allocation type " + mType.mElement.mType);
250b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
251b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
252dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    @Override
253dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    void updateFromNative() {
25406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        super.updateFromNative();
25506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
256dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        if(typeID != 0) {
257dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType = new Type(typeID, mRS);
258dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType.updateFromNative();
259ad37cb26cd8d8a05077152ebc5b841a5629cfbbdJason Sams            updateCacheInfo(mType);
260dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        }
261dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    }
262dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk
263ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    public Type getType() {
264ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams        return mType;
265ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    }
266ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams
2675476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public void syncAll(int srcLocation) {
2685476b450e50939940dcf3f15c92335cee2fc572dJason Sams        switch (srcLocation) {
2695476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_SCRIPT:
2705476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_CONSTANTS:
2715476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_TEXTURE:
2725476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_VERTEX:
2735476b450e50939940dcf3f15c92335cee2fc572dJason Sams            break;
2745476b450e50939940dcf3f15c92335cee2fc572dJason Sams        default:
2755476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
2765476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
2775476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.validate();
27848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
2795476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
2805476b450e50939940dcf3f15c92335cee2fc572dJason Sams
281bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(BaseObj[] d) {
282bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        mRS.validate();
283b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsObject();
284ba862d1544a06528151550be1784a926ee986580Jason Sams        if (d.length != mCurrentCount) {
285bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
286ba862d1544a06528151550be1784a926ee986580Jason Sams                                                 mCurrentCount + ", array length = " + d.length);
287bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
288bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        int i[] = new int[d.length];
289bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        for (int ct=0; ct < d.length; ct++) {
290bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            i[ct] = d[ct].getID();
291bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
292ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, i);
293bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    }
294bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams
295fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapFormat(Bitmap b) {
296252c07802f7039f15f723751162e64a6621e6998Jason Sams        Bitmap.Config bc = b.getConfig();
297252c07802f7039f15f723751162e64a6621e6998Jason Sams        switch (bc) {
298252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ALPHA_8:
299252c07802f7039f15f723751162e64a6621e6998Jason Sams            if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
300252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
301252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
302252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
303252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
304252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
305252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
306252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
307252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_8888:
308252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
309252c07802f7039f15f723751162e64a6621e6998Jason Sams                (mType.getElement().getSizeBytes() != 4)) {
310252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
311252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
312252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
313252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
314252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
315252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
316252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
317252c07802f7039f15f723751162e64a6621e6998Jason Sams        case RGB_565:
318252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
319252c07802f7039f15f723751162e64a6621e6998Jason Sams                (mType.getElement().getSizeBytes() != 2)) {
320252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
321252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
322252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
323252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
324252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
325252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
326252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
327252c07802f7039f15f723751162e64a6621e6998Jason Sams        case ARGB_4444:
328252c07802f7039f15f723751162e64a6621e6998Jason Sams            if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
329252c07802f7039f15f723751162e64a6621e6998Jason Sams                (mType.getElement().getSizeBytes() != 2)) {
330252c07802f7039f15f723751162e64a6621e6998Jason Sams                throw new RSIllegalArgumentException("Allocation kind is " +
331252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mKind + ", type " +
332252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     mType.getElement().mType +
333252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " of " + mType.getElement().getSizeBytes() +
334252c07802f7039f15f723751162e64a6621e6998Jason Sams                                                     " bytes, passed bitmap was " + bc);
335252c07802f7039f15f723751162e64a6621e6998Jason Sams            }
336252c07802f7039f15f723751162e64a6621e6998Jason Sams            break;
337252c07802f7039f15f723751162e64a6621e6998Jason Sams
338252c07802f7039f15f723751162e64a6621e6998Jason Sams        }
3394ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
3404ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
341fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validateBitmapSize(Bitmap b) {
342ba862d1544a06528151550be1784a926ee986580Jason Sams        if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
343fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
344fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
345fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
346fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams
3474fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3484fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3494fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3504fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3514fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3524fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3534fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3544fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(int[] d) {
3554fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
356ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
3574fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3584fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3604fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3614fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3624fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3634fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3644fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3654fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(short[] d) {
3664fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
367ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
3684fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3694fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3704fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3714fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3724fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3734fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3744fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3754fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3764fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(byte[] d) {
3774fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
378ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
3794fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3804fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3814fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is not type
3824fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked which allows an application to fill in structured
3834fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * data from an array.
3844fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3854fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3864fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
3874fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copyFromUnchecked(float[] d) {
3884fa3eed8e03348e2629abd539b3476a86b44135eJason Sams        mRS.validate();
389ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(0, mCurrentCount, d);
3904fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    }
3914fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
3924fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
3934fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
3944fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
3954fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 32 bit integer type.
3964fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
3974fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
3984fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
399bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(int[] d) {
400771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
401ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
402768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
4034fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
4044fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
4054fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
4064fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
4074fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 16 bit integer type.
4084fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4094fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4104fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
411bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(short[] d) {
412771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
413ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
414768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
4154fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
4164fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
4174fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
4184fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
4194fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 8 bit integer type.
4204fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4214fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4224fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
423bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(byte[] d) {
424771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
425ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
426b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
4274fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
4284fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
4294fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from an array.  This variant is type
4304fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * checked and will generate exceptions if the Allocation type
4314fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * is not a 32 bit float type.
4324fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4334fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
4344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
435bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(float[] d) {
436771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
437ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFrom(0, mCurrentCount, d);
438b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
4394fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
4404fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
4414fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy an allocation from a bitmap.  The height, width, and
4424fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * format of the bitmap must match the existing allocation.
4434fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
4444fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param b the source bitmap
4454fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
446bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(Bitmap b) {
447fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
448fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
449fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
4504ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.nAllocationCopyFromBitmap(getID(), b);
4514ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
45226ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
453fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    /**
454fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
455fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
456fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
457fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
458fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
459fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
46021b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, FieldPacker fp) {
461a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int eSize = mType.mElement.getSizeBytes();
462a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        final byte[] data = fp.getData();
463a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
464a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int count = data.length / eSize;
465a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if ((eSize * count) != data.length) {
46606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer length " + data.length +
467a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               " not divisible by element size " + eSize + ".");
468a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
469ba862d1544a06528151550be1784a926ee986580Jason Sams        copy1DRangeFromUnchecked(xoff, count, data);
47049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    }
47149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
472fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    /**
473fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
474fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
475fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
476fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
477fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param component_number
478fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
479fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
48021b4103e42cb0fa004cc4a978f49f63e7668ab0bJason Sams    public void setFromFieldPacker(int xoff, int component_number, FieldPacker fp) {
48149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (component_number >= mType.mElement.mElements.length) {
48206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
48349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
48449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if(xoff < 0) {
48506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
48649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
48749bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
48849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        final byte[] data = fp.getData();
48949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        int eSize = mType.mElement.mElements[component_number].getSizeBytes();
49049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
49149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (data.length != eSize) {
49206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
49349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams                                               " does not match component size " + eSize + ".");
49449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
49549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
49648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
497ba862d1544a06528151550be1784a926ee986580Jason Sams                                     component_number, data, data.length);
498a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams    }
499a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
500768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
501771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
502a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(off < 0) {
50306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
504a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
505a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(count < 1) {
50606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
507a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
508ba862d1544a06528151550be1784a926ee986580Jason Sams        if((off + count) > mCurrentCount) {
509ba862d1544a06528151550be1784a926ee986580Jason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
510a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               ", got " + count + " at offset " + off + ".");
511768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
512ba862d1544a06528151550be1784a926ee986580Jason Sams        if(len < dataSize) {
51306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
514768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
515b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
516b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
517f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
518f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Generate a mipmap chain.  Requires the type of the allocation
519f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * include mipmaps.
520f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
521f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * This function will generate a complete set of mipmaps from
522f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * the top level lod and place them into the script memoryspace.
523f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
524f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * If the allocation is also using other memory spaces a
525f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * followup sync will be required.
526f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
527f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void generateMipmaps() {
528f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams        mRS.nAllocationGenerateMipmaps(getID());
529f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    }
530f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams
5314fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5324fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5334fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5354fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5364fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5374fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5384fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5394fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5404fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
541768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
542768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
54348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
544768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5454fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5464fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5474fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5484fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5494fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5504fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5514fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5524fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5534fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5544fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
555768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
556768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 2, dataSize);
55748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
558768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5594fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5604fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5614fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5624fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5634fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5644fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5654fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5664fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5674fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5684fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
569768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
570768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length, dataSize);
57148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
572768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
5734fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5744fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5754fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * not type checked which allows an application to fill in
5764fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * structured data from an array.
5774fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5784fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5794fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5804fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5814fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
5824fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
583768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
584768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
58548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, d, dataSize);
586b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
587b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
5884fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
5894fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
5904fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
5914fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 32 bit integer type.
5924fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
5934fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
5944fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
5954fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
5964fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
597b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
598b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
599b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
600b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
6014fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
6024fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
6034fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
6044fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
6054fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 16 bit integer type.
6064fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6074fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6084fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
6094fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6104fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
611b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
612b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
613b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
614b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
6154fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
6164fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
6174fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
6184fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
6194fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 8 bit integer type.
6204fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6214fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6224fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
6234fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param d the source data array
6244fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
625b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
626b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
627b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
628b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
6294fa3eed8e03348e2629abd539b3476a86b44135eJason Sams
6304fa3eed8e03348e2629abd539b3476a86b44135eJason Sams    /**
6314fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * Copy part of an allocation from an array.  This variant is
6324fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type checked and will generate exceptions if the Allocation
6334fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * type is not a 32 bit float type.
6344fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     *
6354fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param off The offset of the first element to be copied.
6364fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     * @param count The number of elements to be copied.
637304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param d the source data array.
6384fa3eed8e03348e2629abd539b3476a86b44135eJason Sams     */
639b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
640b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
641b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        copy1DRangeFromUnchecked(off, count, d);
642b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams    }
643b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams
644304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     /**
645304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy part of an allocation from another allocation.
646304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
647304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param off The offset of the first element to be copied.
648304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param count The number of elements to be copied.
649304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param data the source data allocation.
650304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataOff off The offset of the first element in data to
651304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *          be copied.
652304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
653304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
65448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), off, 0,
655ba862d1544a06528151550be1784a926ee986580Jason Sams                              mSelectedLOD, mSelectedFace.mID,
656304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                              count, 1, data.getID(), dataOff, 0,
657ba862d1544a06528151550be1784a926ee986580Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
658304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
659304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
660fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    private void validate2DRange(int xoff, int yoff, int w, int h) {
661ba862d1544a06528151550be1784a926ee986580Jason Sams        if (mAdaptedAllocation != null) {
662ba862d1544a06528151550be1784a926ee986580Jason Sams
663ba862d1544a06528151550be1784a926ee986580Jason Sams        } else {
664ba862d1544a06528151550be1784a926ee986580Jason Sams
665ba862d1544a06528151550be1784a926ee986580Jason Sams            if (xoff < 0 || yoff < 0) {
666ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Offset cannot be negative.");
667ba862d1544a06528151550be1784a926ee986580Jason Sams            }
668ba862d1544a06528151550be1784a926ee986580Jason Sams            if (h < 0 || w < 0) {
669ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Height or width cannot be negative.");
670ba862d1544a06528151550be1784a926ee986580Jason Sams            }
671ba862d1544a06528151550be1784a926ee986580Jason Sams            if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
672ba862d1544a06528151550be1784a926ee986580Jason Sams                throw new RSIllegalArgumentException("Updated region larger than allocation.");
673ba862d1544a06528151550be1784a926ee986580Jason Sams            }
674fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        }
675fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams    }
676768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
677f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
678304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy a rectangular region from the array into the allocation.
679304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * The incoming array is assumed to be tightly packed.
680f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
681f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param xoff X offset of the region to update
682f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param yoff Y offset of the region to update
683f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param w Width of the incoming region to update
684f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param h Height of the incoming region to update
685f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param data to be placed into the allocation
686f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
687f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
688fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
689fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
69048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
691ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length);
692fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
693fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
694f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
695fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
696fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
69748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
698ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length * 2);
699fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
700fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
701f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
702771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
703fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
70448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
705ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length * 4);
706b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
707b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
708f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
709771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
710fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, w, h);
71148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
712ba862d1544a06528151550be1784a926ee986580Jason Sams                              w, h, data, data.length * 4);
713b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
714b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
715f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
716304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * Copy a rectangular region into the allocation from another
717304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * allocation.
718304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     *
719304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param xoff X offset of the region to update.
720304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param yoff Y offset of the region to update.
721304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param w Width of the incoming region to update.
722304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param h Height of the incoming region to update.
723304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param data source allocation.
724304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataXoff X offset in data of the region to update.
725304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     * @param dataYoff Y offset in data of the region to update.
726304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk     */
727304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
728304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                                Allocation data, int dataXoff, int dataYoff) {
729304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        mRS.validate();
730304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk        validate2DRange(xoff, yoff, w, h);
73148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
732ba862d1544a06528151550be1784a926ee986580Jason Sams                              mSelectedLOD, mSelectedFace.mID,
733304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk                              w, h, data.getID(), dataXoff, dataYoff,
734ba862d1544a06528151550be1784a926ee986580Jason Sams                              data.mSelectedLOD, data.mSelectedFace.mID);
735304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    }
736304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk
737304b1f5497155bcf91e7b855cfab7a675e80bf26Alex Sakhartchouk    /**
738f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Copy a bitmap into an allocation.  The height and width of
739f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * the update will use the height and width of the incoming
740f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * bitmap.
741f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
742f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param xoff X offset of the region to update
743f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param yoff Y offset of the region to update
744f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param data the bitmap to be copied
745f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
746f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
747fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
748fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(data);
749fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
75048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams        mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
751fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
752fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
753fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
75448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
75548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a Bitmap.  The bitmap must
75648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * match the dimensions of the Allocation.
75748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
75848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param b The bitmap to be set from the Allocation.
75948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
760fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(Bitmap b) {
761fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        mRS.validate();
762fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapFormat(b);
763fb9f82ca4f11cf7e43a001f3e6fd1b381cc86210Jason Sams        validateBitmapSize(b);
764fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationCopyToBitmap(getID(), b);
765fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
766fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
76748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
76848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a byte array.  The array must
76948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be at least as large as the Allocation.  The allocation must
77048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be of an 8 bit elemental type.
77148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
77248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
77348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
774fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(byte[] d) {
775b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt8();
776fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
777fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationRead(getID(), d);
778fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
779fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
78048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
78148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a short array.  The array must
78248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be at least as large as the Allocation.  The allocation must
78348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be of an 16 bit elemental type.
78448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
78548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
78648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
787fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(short[] d) {
788b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt16();
789fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
790fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationRead(getID(), d);
791fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
792fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
79348fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
79448fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a int array.  The array must be
79548fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * at least as large as the Allocation.  The allocation must be
79648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * of an 32 bit elemental type.
79748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
79848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
79948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
800fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(int[] d) {
801b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsInt32();
802771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
80306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
80440a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
80540a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
80648fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams    /**
80748fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * Copy from the Allocation into a float array.  The array must
80848fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be at least as large as the Allocation.  The allocation must
80948fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * be of an 32 bit float elemental type.
81048fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     *
81148fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     * @param d The array to be set from the Allocation.
81248fe534838d8b9d10f98ea2662eec258320ab4e7Jason Sams     */
813fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(float[] d) {
814b97b251c26b801b26f2630e3a2e3f93e4088f2c5Jason Sams        validateIsFloat32();
815771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
81606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
81740a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
81840a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
819f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams    /**
820f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * Resize a 1D allocation.  The contents of the allocation are
821f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * preserved.  If new elements are allocated objects are created
822f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * with null contents and the new region is otherwise undefined.
823f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
824f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * If the new region is smaller the references of any objects
825f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * outside the new region will be released.
826f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
827f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * A new type will be created with the new dimension.
828f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     *
829f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     * @param dimX The new size of the allocation.
830f7086090cfc8d97b5bd3b4d7801a27af11f7c207Jason Sams     */
83131a7e42f4baa059352f0db119de38428e655eab2Jason Sams    public synchronized void resize(int dimX) {
832bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
83306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
8345edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
83506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize1D(getID(), dimX);
836d26297fa562d8bb203df1bb5e6ded7f62c56cdb7Jason Sams        mRS.finish();  // Necessary because resize is fifoed and update is async.
83731a7e42f4baa059352f0db119de38428e655eab2Jason Sams
83806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
83931a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType = new Type(typeID, mRS);
84031a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType.updateFromNative();
841452a7661e8b06459b75493b441d33244939c1153Jason Sams        updateCacheInfo(mType);
8425edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
8435edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams
8445edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    /*
8455edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    public void resize(int dimX, int dimY) {
8465edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
84706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
8485edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
8495edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if (mType.getY() == 0) {
85006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
8515edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
85206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize2D(getID(), dimX, dimY);
8535edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
8545edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    */
85540a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
856bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
857b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
858b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    // creation
859b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
86049a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
861b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    static {
862b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        mBitmapOptions.inScaled = false;
863b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
864b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
865623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
866623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
867623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
868623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
869623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
870623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
871623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
872623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
873623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
874771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
8755476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (type.getID() == 0) {
87606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Bad Type");
87706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        }
878623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk        int id = rs.nAllocationCreateTyped(type.getID(), mips.mID, usage);
8795476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
88006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
8811bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams        }
8825476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, type, usage);
883b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
884b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
885623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
886623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with the size specified by
887623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the type and no mipmaps generated by default
888623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
889f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
890623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
891623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
892623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
893623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
894623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
895623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
896e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
897e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
898e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    }
899e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams
900623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
901623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation for use by the script with
902623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the size specified by the type and no mipmaps generated by
903623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * default
904623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
905f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
906623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param type renderscript type describing data layout
907623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
908623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
909623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
9105476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
911d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
9125476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
9131bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams
914623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
915623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with a specified number of
916623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * given elements
917623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
918f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
919623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param e describes what each element of an allocation is
920623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param count specifies the number of element in the allocation
921623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
922623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
923623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
924623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
925623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
9265476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e,
9275476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                         int count, int usage) {
928771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
929768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type.Builder b = new Type.Builder(rs, e);
930bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        b.setX(count);
931768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type t = b.create();
932768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
933d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
9345476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
93506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
936b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
9375476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
9385476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
9395476b450e50939940dcf3f15c92335cee2fc572dJason Sams
940623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
941623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation with a specified number of
942623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * given elements
943623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
944f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
945623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param e describes what each element of an allocation is
946623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param count specifies the number of element in the allocation
947623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
948623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation
949623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
9505476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
951d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
952b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
953b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
95449a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
9558a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        final Bitmap.Config bc = b.getConfig();
9568a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
9578a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.A_8(rs);
9588a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
9598a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
9608a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_4444(rs);
9618a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
9628a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
9638a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_8888(rs);
9648a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
9658a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.RGB_565) {
9668a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGB_565(rs);
9678a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
9684bd1a3dbcad2ae424293e276434b45ebee97248dJeff Sharkey        throw new RSInvalidStateException("Bad bitmap type: " + bc);
9698a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
9708a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
97149a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
9724ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                       MipmapControl mip) {
9738a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Element e = elementFromBitmap(rs, b);
9748a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
975bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(b.getWidth());
976bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(b.getHeight());
9774ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
9788a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        return tb.create();
9798a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
9808a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
981623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
982623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation from a bitmap
983623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
984f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
985623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap source for the allocation data
986623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
987623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
988623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
989623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
990623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
991623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing bitmap data
992623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
993623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
99467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
9954ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                              MipmapControl mips,
9965476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
997771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
9985476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Type t = typeFromBitmap(rs, b, mips);
9998a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
10005476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
10015476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
100206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Load failed.");
1003718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        }
10045476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
10055476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
10065476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1007623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1008623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped renderscript allocation to use as a
1009623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture
1010623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1011f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1012623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap source for the allocation data
1013623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1014623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing bitmap data
1015623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1016623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
10176d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
10186d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
10196d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                USAGE_GRAPHICS_TEXTURE);
10208a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
10218a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
1022fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    /**
1023623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a cubemap allocation from a bitmap containing the
1024623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * horizontal list of cube faces. Each individual face must be
1025623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the same size and power of 2
1026623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1027f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1028623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap with cubemap faces layed out in the following
1029623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
1030623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
1031623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
1032623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1033623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1034623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1035623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
103667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
10374ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                     MipmapControl mips,
10385476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     int usage) {
103967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        rs.validate();
10405476b450e50939940dcf3f15c92335cee2fc572dJason Sams
104167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int height = b.getHeight();
104267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int width = b.getWidth();
104367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
1044fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width % 6 != 0) {
104567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
104667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
1047fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width / 6 != height) {
1048dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
104967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
1050fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
105167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (!isPow2) {
105267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
105367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
105467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
105567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Element e = elementFromBitmap(rs, b);
105667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
1057fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setX(height);
1058fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setY(height);
1059bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setFaces(true);
10604ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
106167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type t = tb.create();
106267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
10635476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
106467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if(id == 0) {
106567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
106667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
10675476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
10685476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
10695476b450e50939940dcf3f15c92335cee2fc572dJason Sams
1070623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1071623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped cubemap allocation for use as a
1072623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from a bitmap containing the horizontal list
1073623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * of cube faces. Each individual face must be the same size and
1074623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1075623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1076f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1077623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param b bitmap with cubemap faces layed out in the following
1078623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *          format: right, left, top, bottom, front, back
1079623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1080623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1081623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1082623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1083dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs,
1084dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                     Bitmap b) {
10856d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
1086fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk                                       USAGE_GRAPHICS_TEXTURE);
108767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
108867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
1089623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1090623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a cubemap allocation from 6 bitmaps containing
1091623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the cube faces. All the faces must be the same size and
1092623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1093623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1094f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1095623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1096623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1097623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1098623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1099623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1100623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1101623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the cubemap
1102623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the cubemap is utilized
1103623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1104623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1105623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1106623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1107dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1108dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1109dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1110dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1111dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1112dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1113dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg,
1114dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        MipmapControl mips,
1115dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        int usage) {
1116dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        int height = xpos.getHeight();
1117dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (xpos.getWidth() != height ||
1118dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            xneg.getWidth() != height || xneg.getHeight() != height ||
1119dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            ypos.getWidth() != height || ypos.getHeight() != height ||
1120dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            yneg.getWidth() != height || yneg.getHeight() != height ||
1121dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zpos.getWidth() != height || zpos.getHeight() != height ||
1122dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            zneg.getWidth() != height || zneg.getHeight() != height) {
1123dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only square cube map faces supported");
1124dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1125dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
1126dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        if (!isPow2) {
1127dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
1128dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        }
1129dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1130dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Element e = elementFromBitmap(rs, xpos);
1131dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
1132dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setX(height);
1133dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setY(height);
1134dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setFaces(true);
1135dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
1136dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Type t = tb.create();
1137dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
1138dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1139dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
114020fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_X);
1141dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xpos);
1142dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_X);
1143dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(xneg);
114420fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Y);
1145dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(ypos);
1146dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
1147dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(yneg);
114820fbd01335f3a41ab78e0bb9f70124665afb1e3bStephen Hines        adapter.setFace(Type.CubemapFace.POSITIVE_Z);
1149dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zpos);
1150dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
1151dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        adapter.copyFrom(zneg);
1152dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1153dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return cubemap;
1154dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1155dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1156623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1157623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped cubemap allocation for use as a
1158623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from 6 bitmaps containing
1159623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * the cube faces. All the faces must be the same size and
1160623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * power of 2
1161623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1162f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1163623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xpos cubemap face in the positive x direction
1164623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param xneg cubemap face in the negative x direction
1165623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param ypos cubemap face in the positive y direction
1166623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param yneg cubemap face in the negative y direction
1167623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zpos cubemap face in the positive z direction
1168623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param zneg cubemap face in the negative z direction
1169623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1170623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return allocation containing cubemap data
1171623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1172623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
1173dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    static public Allocation createCubemapFromCubeFaces(RenderScript rs,
1174dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xpos,
1175dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap xneg,
1176dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap ypos,
1177dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap yneg,
1178dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zpos,
1179dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                                        Bitmap zneg) {
1180dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk        return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
1181dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          zpos, zneg, MipmapControl.MIPMAP_NONE,
1182dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk                                          USAGE_GRAPHICS_TEXTURE);
1183dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk    }
1184dcc231955d81c66309ce97cca05a25f79ee7d5eaAlex Sakhartchouk
1185623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1186623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation from the bitmap referenced
1187623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * by resource id
1188623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1189f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1190623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1191623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1192623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param mips specifies desired mipmap behaviour for the
1193623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *             allocation
1194623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocation is
1195623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1196623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1197623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing resource data
1198623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1199623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
12005476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
12015476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
12025476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int id,
12034ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                      MipmapControl mips,
12045476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int usage) {
1205b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1206771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
12075476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Bitmap b = BitmapFactory.decodeResource(res, id);
12085476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Allocation alloc = createFromBitmap(rs, b, mips, usage);
12095476b450e50939940dcf3f15c92335cee2fc572dJason Sams        b.recycle();
12105476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return alloc;
12115476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
1212650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
1213623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1214623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a non-mipmapped renderscript allocation to use as a
1215623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * graphics texture from the bitmap referenced by resource id
1216623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1217f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1218623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param res application resources
1219623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param id resource id to load the data from
1220623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1221623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @return renderscript allocation containing resource data
1222623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1223623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
12245476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
12255476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
12266d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                                      int id) {
12276d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmapResource(rs, res, id,
12286d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        MipmapControl.MIPMAP_NONE,
12296d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        USAGE_GRAPHICS_TEXTURE);
12306d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    }
12316d8eb266dd398abf0511685fdaf98abba3396174Jason Sams
1232623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk    /**
1233623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * Creates a renderscript allocation containing string data
1234623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * encoded in UTF-8 format
1235623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1236f5c876e82d7cc647ba94d29eb914e64b7977c303Alex Sakhartchouk     * @param rs Context to which the allocation will belong.
1237623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param str string to create the allocation from
1238623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     * @param usage bit field specifying how the allocaiton is
1239623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *              utilized
1240623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     *
1241623c54dd1605d802bb6bfdd0d266a191d4f2d88cAlex Sakhartchouk     */
12425476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromString(RenderScript rs,
12435476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              String str,
12445476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
12455476b450e50939940dcf3f15c92335cee2fc572dJason Sams        rs.validate();
12469b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        byte[] allocArray = null;
12479b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        try {
12489b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            allocArray = str.getBytes("UTF-8");
12495476b450e50939940dcf3f15c92335cee2fc572dJason Sams            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
1250bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            alloc.copyFrom(allocArray);
12519b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            return alloc;
12529b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
12539b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        catch (Exception e) {
125406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Could not convert string to utf-8.");
12559b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
12569b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk    }
1257b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams}
1258b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1259b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
1260