Allocation.java revision 4ef6650bd05a39a09958ea1db92f120ea4949cb1
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/**
30b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams * @hide
31b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams *
32b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams **/
33b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Samspublic class Allocation extends BaseObj {
3443ee06857bb7f99446d1d84f8789016c5d105558Jason Sams    Type mType;
358a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    Bitmap mBitmap;
365476b450e50939940dcf3f15c92335cee2fc572dJason Sams    int mUsage;
375476b450e50939940dcf3f15c92335cee2fc572dJason Sams
385476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_SCRIPT = 0x0001;
395476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
405476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
415476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
425476b450e50939940dcf3f15c92335cee2fc572dJason Sams
435476b450e50939940dcf3f15c92335cee2fc572dJason Sams    private static final int USAGE_ALL = 0x000F;
445476b450e50939940dcf3f15c92335cee2fc572dJason Sams
4543ee06857bb7f99446d1d84f8789016c5d105558Jason Sams
4667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    public enum CubemapLayout {
4767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        VERTICAL_FACE_LIST (0),
4867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        HORIZONTAL_FACE_LIST (1),
4967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        VERTICAL_CROSS (2),
5067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        HORIZONTAL_CROSS (3);
5167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
5267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int mID;
5367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        CubemapLayout(int id) {
5467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            mID = id;
5567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
5667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
5767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
584ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public enum MipmapControl {
595476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_NONE(0),
605476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_FULL(1),
615476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
625476b450e50939940dcf3f15c92335cee2fc572dJason Sams
635476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int mID;
644ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl(int id) {
655476b450e50939940dcf3f15c92335cee2fc572dJason Sams            mID = id;
665476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
67b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
68b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
695476b450e50939940dcf3f15c92335cee2fc572dJason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
700de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
715476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (usage > USAGE_ALL) {
725476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
735476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
745476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mType = t;
7580a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk    }
7680a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk
77dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    @Override
78dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    void updateFromNative() {
7906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        super.updateFromNative();
8006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
81dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        if(typeID != 0) {
82dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType = new Type(typeID, mRS);
83dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType.updateFromNative();
84dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        }
85dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    }
86dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk
87ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    public Type getType() {
88ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams        return mType;
89ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    }
90ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams
915476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public void syncAll(int srcLocation) {
925476b450e50939940dcf3f15c92335cee2fc572dJason Sams        switch (srcLocation) {
935476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_SCRIPT:
945476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_CONSTANTS:
955476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_TEXTURE:
965476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_VERTEX:
975476b450e50939940dcf3f15c92335cee2fc572dJason Sams            break;
985476b450e50939940dcf3f15c92335cee2fc572dJason Sams        default:
995476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
1005476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
1015476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.validate();
1025476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.nAllocationSyncAll(getID(), srcLocation);
1035476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
1045476b450e50939940dcf3f15c92335cee2fc572dJason Sams
105b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    public void uploadToTexture(int baseMipLevel) {
106771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
10706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationUploadToTexture(getID(), false, baseMipLevel);
108c2908e60c9b021fb4bb69acff8d49981dd4dade8Jason Sams    }
109c2908e60c9b021fb4bb69acff8d49981dd4dade8Jason Sams
110c2908e60c9b021fb4bb69acff8d49981dd4dade8Jason Sams    public void uploadToTexture(boolean genMips, int baseMipLevel) {
111c2908e60c9b021fb4bb69acff8d49981dd4dade8Jason Sams        mRS.validate();
11206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationUploadToTexture(getID(), genMips, baseMipLevel);
113b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
114b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
11507ae40623737a6060b8a925fd2e6bba76780dcd4Jason Sams    public void uploadToBufferObject() {
116771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
11706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationUploadToBufferObject(getID());
11807ae40623737a6060b8a925fd2e6bba76780dcd4Jason Sams    }
11907ae40623737a6060b8a925fd2e6bba76780dcd4Jason Sams
120bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams
121bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(BaseObj[] d) {
122bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        mRS.validate();
123bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if (d.length != mType.getCount()) {
124bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
125bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams                                                 mType.getCount() + ", array length = " + d.length);
126bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
127bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        int i[] = new int[d.length];
128bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        for (int ct=0; ct < d.length; ct++) {
129bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            i[ct] = d[ct].getID();
130bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
131bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), i);
132bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    }
133bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams
1344ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    private void validateBitmap(Bitmap b) {
1354ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.validate();
1364ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        if(mType.getX() != b.getWidth() ||
1374ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams           mType.getY() != b.getHeight()) {
1384ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
1394ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        }
1404ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
1414ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
142bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(int[] d) {
143771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
144bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), d);
145768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
146bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(short[] d) {
147771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
148bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), d);
149768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
150bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(byte[] d) {
151771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
152bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), d);
153b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
154bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(float[] d) {
155771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
156bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), d);
157b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
158bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(Bitmap b) {
1594ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        validateBitmap(b);
1604ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.nAllocationCopyFromBitmap(getID(), b);
1614ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
16226ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
1634ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public void copyTo(Bitmap b) {
1644ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        validateBitmap(b);
1654ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.nAllocationCopyToBitmap(getID(), b);
16626ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk    }
16726ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
1684ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
16949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    public void subData(int xoff, FieldPacker fp) {
170a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int eSize = mType.mElement.getSizeBytes();
171a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        final byte[] data = fp.getData();
172a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
173a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int count = data.length / eSize;
174a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if ((eSize * count) != data.length) {
17506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer length " + data.length +
176a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               " not divisible by element size " + eSize + ".");
177a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
17849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        data1DChecks(xoff, count, data.length, data.length);
17906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationSubData1D(getID(), xoff, count, data, data.length);
18049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    }
18149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
18249bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
18349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    public void subElementData(int xoff, int component_number, FieldPacker fp) {
18449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (component_number >= mType.mElement.mElements.length) {
18506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
18649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
18749bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if(xoff < 0) {
18806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
18949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
19049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
19149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        final byte[] data = fp.getData();
19249bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        int eSize = mType.mElement.mElements[component_number].getSizeBytes();
19349bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
19449bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        if (data.length != eSize) {
19506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
19649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams                                               " does not match component size " + eSize + ".");
19749bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        }
19849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
19906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationSubElementData1D(getID(), xoff, component_number, data, data.length);
200a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams    }
201a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
202768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    private void data1DChecks(int off, int count, int len, int dataSize) {
203771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
204a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(off < 0) {
20506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Offset must be >= 0.");
206a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
207a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if(count < 1) {
20806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Count must be >= 1.");
209a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
210bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if((off + count) > mType.getCount()) {
211bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
212a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               ", got " + count + " at offset " + off + ".");
213768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
214768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        if((len) < dataSize) {
21506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Array too small for allocation type.");
216768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        }
217b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
218b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
219768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    public void subData1D(int off, int count, int[] d) {
220768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
221768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
22206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
223768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
224768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    public void subData1D(int off, int count, short[] d) {
225768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
226768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 2, dataSize);
22706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
228768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
229768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    public void subData1D(int off, int count, byte[] d) {
230768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
231768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length, dataSize);
23206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
233768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
234b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    public void subData1D(int off, int count, float[] d) {
235768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        int dataSize = mType.mElement.getSizeBytes() * count;
236768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        data1DChecks(off, count, d.length * 4, dataSize);
23706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
238b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
239b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
240768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
241b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
242771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
24306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationSubData2D(getID(), xoff, yoff, w, h, d, d.length * 4);
244b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
245b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
246b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
247771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
24806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationSubData2D(getID(), xoff, yoff, w, h, d, d.length * 4);
249b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
250b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
25140a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    public void readData(int[] d) {
252771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
25306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
25440a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
25540a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
25640a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    public void readData(float[] d) {
257771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
25806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
25940a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
26040a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
26131a7e42f4baa059352f0db119de38428e655eab2Jason Sams    public synchronized void resize(int dimX) {
262bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
26306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
2645edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
26506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize1D(getID(), dimX);
266d26297fa562d8bb203df1bb5e6ded7f62c56cdb7Jason Sams        mRS.finish();  // Necessary because resize is fifoed and update is async.
26731a7e42f4baa059352f0db119de38428e655eab2Jason Sams
26806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
26931a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType = new Type(typeID, mRS);
27031a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType.updateFromNative();
2715edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
2725edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams
2735edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    /*
2745edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    public void resize(int dimX, int dimY) {
2755edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
27606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
2775edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
2785edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if (mType.getY() == 0) {
27906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
2805edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
28106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize2D(getID(), dimX, dimY);
2825edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
2835edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    */
28440a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
2855476b450e50939940dcf3f15c92335cee2fc572dJason Sams    /*
286b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    public class Adapter1D extends BaseObj {
287b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        Adapter1D(int id, RenderScript rs) {
2880de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk            super(id, rs);
289b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
290b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
291b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        public void setConstraint(Dimension dim, int value) {
292771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
29306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter1DSetConstraint(getID(), dim.mID, value);
294b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
295b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
296b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        public void data(int[] d) {
297771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
29806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter1DData(getID(), d);
299b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
300b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
301b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        public void data(float[] d) {
302771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
30306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter1DData(getID(), d);
304b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
305b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
306bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        public void subData(int off, int count, int[] d) {
307771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
30806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter1DSubData(getID(), off, count, d);
309bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        }
310bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
311b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        public void subData(int off, int count, float[] d) {
312771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
31306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter1DSubData(getID(), off, count, d);
314b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
315b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
316b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
317b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    public Adapter1D createAdapter1D() {
318771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
319b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        int id = mRS.nAdapter1DCreate();
320718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        if(id == 0) {
32106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Adapter creation failed.");
322b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
32306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAdapter1DBindAllocation(id, getID());
324b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        return new Adapter1D(id, mRS);
325b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
3265476b450e50939940dcf3f15c92335cee2fc572dJason Sams    */
327b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
328b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
329bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams    public class Adapter2D extends BaseObj {
330bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        Adapter2D(int id, RenderScript rs) {
3310de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk            super(id, rs);
332bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        }
333bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
334bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        public void setConstraint(Dimension dim, int value) {
335771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
33606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter2DSetConstraint(getID(), dim.mID, value);
337bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        }
338bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
339bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        public void data(int[] d) {
340771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
34106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter2DData(getID(), d);
342bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        }
343bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
344bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        public void data(float[] d) {
345771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
34606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter2DData(getID(), d);
347bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        }
348bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
349bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        public void subData(int xoff, int yoff, int w, int h, int[] d) {
350771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
35106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
352bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        }
353bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
354bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        public void subData(int xoff, int yoff, int w, int h, float[] d) {
355771bebb94054d06f97284379c93a2620613513c3Jason Sams            mRS.validate();
35606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
357bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        }
358bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams    }
359bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
360bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams    public Adapter2D createAdapter2D() {
361771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
362bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        int id = mRS.nAdapter2DCreate();
363718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        if(id == 0) {
36406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("allocation failed.");
36506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        }
36606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAdapter2DBindAllocation(id, getID());
36706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        if(id == 0) {
36806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Adapter creation failed.");
369bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        }
370bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams        return new Adapter2D(id, mRS);
371bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams    }
372bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
373b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
374b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    // creation
375b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
376b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
377b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    static {
378b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        mBitmapOptions.inScaled = false;
379b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
380b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
3815476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
382771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
3835476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (type.getID() == 0) {
38406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Bad Type");
38506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        }
3865476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCreateTyped(type.getID(), usage);
3875476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
38806d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
3891bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams        }
3905476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, type, usage);
391b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
392b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
3935476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
3945476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return createTyped(rs, type, USAGE_ALL);
3955476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
3961bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams
3975476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e,
3985476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                         int count, int usage) {
399771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
400768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type.Builder b = new Type.Builder(rs, e);
401bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        b.setX(count);
402768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type t = b.create();
403768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
4045476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCreateTyped(t.getID(), usage);
4055476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
40606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
407b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
4085476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
4095476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
4105476b450e50939940dcf3f15c92335cee2fc572dJason Sams
4115476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
4125476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return createSized(rs, e, count, USAGE_ALL);
413b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
414b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
4158a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
4168a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        final Bitmap.Config bc = b.getConfig();
4178a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
4188a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.A_8(rs);
4198a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
4208a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
4218a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_4444(rs);
4228a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
4238a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
4248a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_8888(rs);
4258a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
4268a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.RGB_565) {
4278a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGB_565(rs);
4288a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
4294bd1a3dbcad2ae424293e276434b45ebee97248dJeff Sharkey        throw new RSInvalidStateException("Bad bitmap type: " + bc);
4308a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
4318a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
4325476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static private Type typeFromBitmap(RenderScript rs, Bitmap b,
4334ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                       MipmapControl mip) {
4348a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Element e = elementFromBitmap(rs, b);
4358a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
436bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(b.getWidth());
437bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(b.getHeight());
4384ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
4398a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        return tb.create();
4408a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
4418a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
44267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
4434ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                              MipmapControl mips,
4445476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
445771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
4465476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Type t = typeFromBitmap(rs, b, mips);
4478a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
4485476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
4495476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
45006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Load failed.");
451718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        }
4525476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
4535476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
4545476b450e50939940dcf3f15c92335cee2fc572dJason Sams
4555476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
4565476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              Element dstFmt, boolean genMips) {
4574ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl mc = MipmapControl.MIPMAP_NONE;
4585476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (genMips) {
4594ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams            mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
4605476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
4615476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return createFromBitmap(rs, b, mc, USAGE_ALL);
4628a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
4638a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
46467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
4654ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                     MipmapControl mips,
4665476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     CubemapLayout layout,
4675476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     int usage) {
46867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        rs.validate();
4695476b450e50939940dcf3f15c92335cee2fc572dJason Sams
47067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int height = b.getHeight();
47167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int width = b.getWidth();
47267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
47367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (layout != CubemapLayout.VERTICAL_FACE_LIST) {
47467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only vertical face list supported");
47567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
47667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (height % 6 != 0) {
47767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
47867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
47967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (height / 6 != width) {
48067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only square cobe map faces supported");
48167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
48267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        boolean isPow2 = (width & (width - 1)) == 0;
48367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (!isPow2) {
48467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
48567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
48667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
48767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Element e = elementFromBitmap(rs, b);
48867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
489bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(width);
490bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(width);
491bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setFaces(true);
4924ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
49367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type t = tb.create();
49467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
4955476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
49667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if(id == 0) {
49767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
49867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
4995476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
5005476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
5015476b450e50939940dcf3f15c92335cee2fc572dJason Sams
5025476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
5035476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     Element dstFmt,
5045476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     boolean genMips,
5055476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     CubemapLayout layout) {
5064ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl mc = MipmapControl.MIPMAP_NONE;
5075476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (genMips) {
5084ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams            mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
5095476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
5105476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return createCubemapFromBitmap(rs, b, mc, layout, USAGE_ALL);
51167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
51267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
5135476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
5145476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
5155476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int id,
5164ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                      MipmapControl mips,
5175476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int usage) {
518b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
519771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
5205476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Bitmap b = BitmapFactory.decodeResource(res, id);
5215476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Allocation alloc = createFromBitmap(rs, b, mips, usage);
5225476b450e50939940dcf3f15c92335cee2fc572dJason Sams        b.recycle();
5235476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return alloc;
5245476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
525650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
5265476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
5275476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
5285476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int id,
5295476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Element dstFmt,
5305476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      boolean genMips) {
5314ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl mc = MipmapControl.MIPMAP_NONE;
5325476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (genMips) {
5334ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams            mc = MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE;
534650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy        }
5355476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return createFromBitmapResource(rs, res, id, mc, USAGE_ALL);
536b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
537b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
5385476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromString(RenderScript rs,
5395476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              String str,
5405476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
5415476b450e50939940dcf3f15c92335cee2fc572dJason Sams        rs.validate();
5429b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        byte[] allocArray = null;
5439b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        try {
5449b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            allocArray = str.getBytes("UTF-8");
5455476b450e50939940dcf3f15c92335cee2fc572dJason Sams            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
546bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            alloc.copyFrom(allocArray);
5479b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            return alloc;
5489b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
5499b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        catch (Exception e) {
55006d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Could not convert string to utf-8.");
5519b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
5529b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk    }
553b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams}
554b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
555b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
556