Allocation.java revision fe852e216fdfab20e7b3d3e55247f70634d267b9
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 Sams
22b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.content.res.Resources;
23650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guyimport android.content.res.AssetManager;
24b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.graphics.Bitmap;
25b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.graphics.BitmapFactory;
26b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samsimport android.util.Log;
27650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guyimport android.util.TypedValue;
28b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
29b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams/**
30a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * Memory allocation class for renderscript.  An allocation combines a Type with
31a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * memory to provide storage for user data and objects.
32a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams *
33a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * Allocations may exist in one or more memory spaces.  Currently those are
34a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * Script: accessable by RS scripts.
35a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * Graphics Texture: accessable as a graphics texture.
36a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * Graphics Vertex: accessable as graphical vertex data.
37a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * Graphics Constants: Accessable as constants in user shaders
38a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams *
39a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * By default java side updates are always applied to the script accessable
40a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * memory.  If this is not present they are then applied to the various HW
41a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * memory types.  A syncAll call is necessary after the script data is update to
42a23d4e792cb13090c540edfdd5cee03799bb9d48Jason Sams * keep the other memory spaces in sync.
43b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams *
44b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams **/
45b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samspublic class Allocation extends BaseObj {
4643ee06857bb7f99446d1d84f8789016c5d105558Jason Sams    Type mType;
478a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    Bitmap mBitmap;
485476b450e50939940dcf3f15c92335cee2fc572dJason Sams    int mUsage;
495476b450e50939940dcf3f15c92335cee2fc572dJason Sams
505476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_SCRIPT = 0x0001;
515476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
525476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
535476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
545476b450e50939940dcf3f15c92335cee2fc572dJason Sams
5543ee06857bb7f99446d1d84f8789016c5d105558Jason Sams
564ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public enum MipmapControl {
575476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_NONE(0),
585476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_FULL(1),
595476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
605476b450e50939940dcf3f15c92335cee2fc572dJason Sams
615476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int mID;
624ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl(int id) {
635476b450e50939940dcf3f15c92335cee2fc572dJason Sams            mID = id;
645476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
65b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
66b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
675476b450e50939940dcf3f15c92335cee2fc572dJason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
680de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
6949a05d7b82956009f03acbb92a064eed054eb031Jason Sams        if ((usage & ~(USAGE_SCRIPT |
7049a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_TEXTURE |
7149a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_VERTEX |
7249a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_CONSTANTS)) != 0) {
735476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
745476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
755476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mType = t;
7680a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk    }
7780a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk
78dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    @Override
79dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    void updateFromNative() {
8006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        super.updateFromNative();
8106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
82dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        if(typeID != 0) {
83dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType = new Type(typeID, mRS);
84dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType.updateFromNative();
85dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        }
86dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    }
87dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk
88ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    public Type getType() {
89ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams        return mType;
90ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    }
91ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams
925476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public void syncAll(int srcLocation) {
935476b450e50939940dcf3f15c92335cee2fc572dJason Sams        switch (srcLocation) {
945476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_SCRIPT:
955476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_CONSTANTS:
965476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_TEXTURE:
975476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_VERTEX:
985476b450e50939940dcf3f15c92335cee2fc572dJason Sams            break;
995476b450e50939940dcf3f15c92335cee2fc572dJason Sams        default:
1005476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
1015476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
1025476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.validate();
1035476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.nAllocationSyncAll(getID(), srcLocation);
1045476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
1055476b450e50939940dcf3f15c92335cee2fc572dJason Sams
106bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(BaseObj[] d) {
107bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        mRS.validate();
108bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if (d.length != mType.getCount()) {
109bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
110bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams                                                 mType.getCount() + ", array length = " + d.length);
111bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
112bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        int i[] = new int[d.length];
113bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        for (int ct=0; ct < d.length; ct++) {
114bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            i[ct] = d[ct].getID();
115bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
116fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        copy1DRangeFrom(0, mType.getCount(), i);
117bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    }
118bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams
1194ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    private void validateBitmap(Bitmap b) {
1204ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.validate();
1214ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        if(mType.getX() != b.getWidth() ||
1224ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams           mType.getY() != b.getHeight()) {
1234ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
1244ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        }
1254ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
1264ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
127bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(int[] d) {
128771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
129fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        copy1DRangeFrom(0, mType.getCount(), d);
130768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
131bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(short[] d) {
132771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
133fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        copy1DRangeFrom(0, mType.getCount(), d);
134768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
135bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(byte[] d) {
136771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
137fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        copy1DRangeFrom(0, mType.getCount(), d);
138b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
139bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(float[] d) {
140771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
141fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        copy1DRangeFrom(0, mType.getCount(), d);
142b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
143bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(Bitmap b) {
1444ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        validateBitmap(b);
1454ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.nAllocationCopyFromBitmap(getID(), b);
1464ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
14726ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
148fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    /**
149fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @hide
150fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
151fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
152fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
153fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
154fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
155fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
156fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
157fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void setOneElement(int xoff, FieldPacker fp) {
158a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int eSize = mType.mElement.getSizeBytes();
159a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        final byte[] data = fp.getData();
160a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
161a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int count = data.length / eSize;
162a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if ((eSize * count) != data.length) {
16306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer length " + data.length +
164a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               " not divisible by element size " + eSize + ".");
165a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
16649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        data1DChecks(xoff, count, data.length, data.length);
16749a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
16849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    }
16949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
17049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
171fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    /**
172fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @hide
173fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
174fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * This is only intended to be used by auto-generate code reflected from the
175fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * renderscript script files.
176fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     *
177fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param xoff
178fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param component_number
179fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     * @param fp
180fa445b9353972735d8d65e8a936786b1afe9886dJason Sams     */
181fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void setOneComponent(int xoff, int component_number, FieldPacker fp) {
18249bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (component_number >= mType.mElement.mElements.length) {
18306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
18449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
18549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if(xoff < 0) {
18606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
18749bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
18849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
18949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        final byte[] data = fp.getData();
19049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        int eSize = mType.mElement.mElements[component_number].getSizeBytes();
19149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
19249bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (data.length != eSize) {
19306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
19449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams                                               " does not match component size " + eSize + ".");
19549bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
19649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
19749a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationElementData1D(getID(), xoff, 0, component_number, data, data.length);
198a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams    }
199a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
200768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
201771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
202a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(off < 0) {
20306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
204a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
205a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(count < 1) {
20606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
207a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
208bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if((off + count) > mType.getCount()) {
209bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
210a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               ", got " + count + " at offset " + off + ".");
211768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
212768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        if((len) < dataSize) {
21306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
214768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
215b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
216b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
217fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copy1DRangeFrom(int off, int count, int[] d) {
218768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
219768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
22049a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
221768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
222fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copy1DRangeFrom(int off, int count, short[] d) {
223768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
224768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 2, dataSize);
22549a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
226768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
227fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copy1DRangeFrom(int off, int count, byte[] d) {
228768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
229768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length, dataSize);
23049a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
231768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
232fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copy1DRangeFrom(int off, int count, float[] d) {
233768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
234768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
23549a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
236b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
237b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
238768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
239fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] d) {
240fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
241fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length);
242fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
243fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
244fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] d) {
245fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
246fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 2);
247fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
248fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
249fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] d) {
250771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
25149a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
252b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
253b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
254fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] d) {
255771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
25649a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
257b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
258b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
259fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copy2DRangeFrom(int xoff, int yoff, Bitmap b) {
260fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
261fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, b);
262fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
263fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
264fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
265fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(Bitmap b) {
266fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        validateBitmap(b);
267fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationCopyToBitmap(getID(), b);
268fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
269fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
270fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(byte[] d) {
271fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
272fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationRead(getID(), d);
273fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
274fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
275fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(short[] d) {
276fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.validate();
277fa445b9353972735d8d65e8a936786b1afe9886dJason Sams        mRS.nAllocationRead(getID(), d);
278fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    }
279fa445b9353972735d8d65e8a936786b1afe9886dJason Sams
280fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(int[] d) {
281771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
28206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
28340a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
28440a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
285fa445b9353972735d8d65e8a936786b1afe9886dJason Sams    public void copyTo(float[] d) {
286771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
28706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
28840a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
28940a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
29031a7e42f4baa059352f0db119de38428e655eab2Jason Sams    public synchronized void resize(int dimX) {
291bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
29206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
2935edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
29406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize1D(getID(), dimX);
295d26297fa562d8bb203df1bb5e6ded7f62c56cdb7Jason Sams        mRS.finish();  // Necessary because resize is fifoed and update is async.
29631a7e42f4baa059352f0db119de38428e655eab2Jason Sams
29706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
29831a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType = new Type(typeID, mRS);
29931a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType.updateFromNative();
3005edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
3015edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams
3025edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    /*
3035edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    public void resize(int dimX, int dimY) {
3045edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
30506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
3065edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
3075edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if (mType.getY() == 0) {
30806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
3095edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
31006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize2D(getID(), dimX, dimY);
3115edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
3125edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    */
31340a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
314bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
315b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
316b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    // creation
317b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
31849a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
319b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    static {
320b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        mBitmapOptions.inScaled = false;
321b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
322b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
323d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) {
324771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
3255476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (type.getID() == 0) {
32606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Bad Type");
32706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        }
328d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage);
3295476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
33006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
3311bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams        }
3325476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, type, usage);
333b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
334b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
335e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
336e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
337e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    }
338e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams
3395476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
340d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
3415476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
3421bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams
3435476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e,
3445476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                         int count, int usage) {
345771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
346768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type.Builder b = new Type.Builder(rs, e);
347bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        b.setX(count);
348768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type t = b.create();
349768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
350d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
3515476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
35206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
353b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
3545476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
3555476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
3565476b450e50939940dcf3f15c92335cee2fc572dJason Sams
3575476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
358d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
359b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
360b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
36149a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
3628a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        final Bitmap.Config bc = b.getConfig();
3638a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
3648a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.A_8(rs);
3658a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
3668a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
3678a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_4444(rs);
3688a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
3698a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
3708a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_8888(rs);
3718a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
3728a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.RGB_565) {
3738a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGB_565(rs);
3748a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
3754bd1a3dbcad2ae424293e276434b45ebee97248dJeff Sharkey        throw new RSInvalidStateException("Bad bitmap type: " + bc);
3768a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
3778a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
37849a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
3794ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                       MipmapControl mip) {
3808a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Element e = elementFromBitmap(rs, b);
3818a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
382bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(b.getWidth());
383bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(b.getHeight());
3844ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
3858a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        return tb.create();
3868a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
3878a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
38867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
3894ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                              MipmapControl mips,
3905476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
391771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
3925476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Type t = typeFromBitmap(rs, b, mips);
3938a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
3945476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
3955476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
39606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Load failed.");
397718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        }
3985476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
3995476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
4005476b450e50939940dcf3f15c92335cee2fc572dJason Sams
4016d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
4026d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
4036d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                USAGE_GRAPHICS_TEXTURE);
4048a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
4058a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
406fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    /**
407fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    * Creates a cubemap allocation from a bitmap containing the
408fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    * horizontal list of cube faces. Each individual face must be
409fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    * the same size and power of 2
410fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    *
411fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    * @param rs
412fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    * @param b bitmap with cubemap faces layed out in the following
413fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    *          format: right, left, top, bottom, front, back
414fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    * @param mips specifies desired mipmap behaviour for the cubemap
415fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    * @param usage bitfield specifying how the cubemap is utilized
416fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    *
417fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    **/
41867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
4194ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                     MipmapControl mips,
4205476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     int usage) {
42167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        rs.validate();
4225476b450e50939940dcf3f15c92335cee2fc572dJason Sams
42367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int height = b.getHeight();
42467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int width = b.getWidth();
42567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
426fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width % 6 != 0) {
42767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
42867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
429fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        if (width / 6 != height) {
43067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only square cobe map faces supported");
43167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
432fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        boolean isPow2 = (height & (height - 1)) == 0;
43367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (!isPow2) {
43467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
43567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
43667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
43767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Element e = elementFromBitmap(rs, b);
43867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
439fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setX(height);
440fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk        tb.setY(height);
441bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setFaces(true);
4424ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
44367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type t = tb.create();
44467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
4455476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
44667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if(id == 0) {
44767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
44867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
4495476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
4505476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
4515476b450e50939940dcf3f15c92335cee2fc572dJason Sams
452fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b) {
4536d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
454fe852e216fdfab20e7b3d3e55247f70634d267b9Alex Sakhartchouk                                       USAGE_GRAPHICS_TEXTURE);
45567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
45667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
4575476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
4585476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
4595476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int id,
4604ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                      MipmapControl mips,
4615476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int usage) {
462b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
463771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
4645476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Bitmap b = BitmapFactory.decodeResource(res, id);
4655476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Allocation alloc = createFromBitmap(rs, b, mips, usage);
4665476b450e50939940dcf3f15c92335cee2fc572dJason Sams        b.recycle();
4675476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return alloc;
4685476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
469650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
4705476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
4715476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
4726d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                                      int id) {
4736d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmapResource(rs, res, id,
4746d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        MipmapControl.MIPMAP_NONE,
4756d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        USAGE_GRAPHICS_TEXTURE);
4766d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    }
4776d8eb266dd398abf0511685fdaf98abba3396174Jason Sams
4785476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromString(RenderScript rs,
4795476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              String str,
4805476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
4815476b450e50939940dcf3f15c92335cee2fc572dJason Sams        rs.validate();
4829b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        byte[] allocArray = null;
4839b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        try {
4849b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            allocArray = str.getBytes("UTF-8");
4855476b450e50939940dcf3f15c92335cee2fc572dJason Sams            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
486bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            alloc.copyFrom(allocArray);
4879b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            return alloc;
4889b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
4899b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        catch (Exception e) {
49006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Could not convert string to utf-8.");
4919b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
4929b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk    }
493b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams}
494b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
495b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
496