Allocation.java revision a23d4e792cb13090c540edfdd5cee03799bb9d48
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
5667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    public enum CubemapLayout {
5767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        VERTICAL_FACE_LIST (0),
5867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        HORIZONTAL_FACE_LIST (1),
5967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        VERTICAL_CROSS (2),
6067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        HORIZONTAL_CROSS (3);
6167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
6267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int mID;
6367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        CubemapLayout(int id) {
6467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            mID = id;
6567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
6667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
6767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
6849a05d7b82956009f03acbb92a064eed054eb031Jason Sams
694ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public enum MipmapControl {
705476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_NONE(0),
715476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_FULL(1),
725476b450e50939940dcf3f15c92335cee2fc572dJason Sams        MIPMAP_ON_SYNC_TO_TEXTURE(2);
735476b450e50939940dcf3f15c92335cee2fc572dJason Sams
745476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int mID;
754ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        MipmapControl(int id) {
765476b450e50939940dcf3f15c92335cee2fc572dJason Sams            mID = id;
775476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
78b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
79b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
805476b450e50939940dcf3f15c92335cee2fc572dJason Sams    Allocation(int id, RenderScript rs, Type t, int usage) {
810de9444aa6c25d2c586e8204a6168d10e67376e0Alex Sakhartchouk        super(id, rs);
8249a05d7b82956009f03acbb92a064eed054eb031Jason Sams        if ((usage & ~(USAGE_SCRIPT |
8349a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_TEXTURE |
8449a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_VERTEX |
8549a05d7b82956009f03acbb92a064eed054eb031Jason Sams                       USAGE_GRAPHICS_CONSTANTS)) != 0) {
865476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Unknown usage specified.");
875476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
885476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mType = t;
8980a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk    }
9080a4c2cd34aedb4f1a2e5e7d1ac26a9aeebe41aeAlex Sakhartchouk
91dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    @Override
92dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    void updateFromNative() {
9306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        super.updateFromNative();
9406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
95dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        if(typeID != 0) {
96dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType = new Type(typeID, mRS);
97dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk            mType.updateFromNative();
98dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk        }
99dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk    }
100dfac814c18f73dd7289f9927edca3e3b6ec6bc00Alex Sakhartchouk
101ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    public Type getType() {
102ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams        return mType;
103ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams    }
104ea87e96959895ef94cc3aa9576f41a660d2bbf03Jason Sams
1055476b450e50939940dcf3f15c92335cee2fc572dJason Sams    public void syncAll(int srcLocation) {
1065476b450e50939940dcf3f15c92335cee2fc572dJason Sams        switch (srcLocation) {
1075476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_SCRIPT:
1085476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_CONSTANTS:
1095476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_TEXTURE:
1105476b450e50939940dcf3f15c92335cee2fc572dJason Sams        case USAGE_GRAPHICS_VERTEX:
1115476b450e50939940dcf3f15c92335cee2fc572dJason Sams            break;
1125476b450e50939940dcf3f15c92335cee2fc572dJason Sams        default:
1135476b450e50939940dcf3f15c92335cee2fc572dJason Sams            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
1145476b450e50939940dcf3f15c92335cee2fc572dJason Sams        }
1155476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.validate();
1165476b450e50939940dcf3f15c92335cee2fc572dJason Sams        mRS.nAllocationSyncAll(getID(), srcLocation);
1175476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
1185476b450e50939940dcf3f15c92335cee2fc572dJason Sams
119bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(BaseObj[] d) {
120bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        mRS.validate();
121bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if (d.length != mType.getCount()) {
122bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
123bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams                                                 mType.getCount() + ", array length = " + d.length);
124bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
125bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        int i[] = new int[d.length];
126bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        for (int ct=0; ct < d.length; ct++) {
127bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            i[ct] = d[ct].getID();
128bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        }
129bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), i);
130bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    }
131bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams
1324ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    private void validateBitmap(Bitmap b) {
1334ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.validate();
1344ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        if(mType.getX() != b.getWidth() ||
1354ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams           mType.getY() != b.getHeight()) {
1364ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
1374ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        }
1384ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
1394ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
140bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(int[] d) {
141771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
142bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), d);
143768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
144bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(short[] d) {
145771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
146bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), d);
147768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    }
148bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(byte[] d) {
149771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
150bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), d);
151b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
152bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(float[] d) {
153771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
154bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        subData1D(0, mType.getCount(), d);
155b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
156bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams    public void copyFrom(Bitmap b) {
1574ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        validateBitmap(b);
1584ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.nAllocationCopyFromBitmap(getID(), b);
1594ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    }
16026ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
1614ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams    public void copyTo(Bitmap b) {
1624ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        validateBitmap(b);
1634ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        mRS.nAllocationCopyToBitmap(getID(), b);
16426ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk    }
16526ae3904e8050eae655722caf93ee5d3f0ab195aAlex Sakhartchouk
1664ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams
16749bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    public void subData(int xoff, FieldPacker fp) {
168a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int eSize = mType.mElement.getSizeBytes();
169a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        final byte[] data = fp.getData();
170a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams
171a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        int count = data.length / eSize;
172a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        if ((eSize * count) != data.length) {
17306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalArgumentException("Field packer length " + data.length +
174a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams                                               " not divisible by element size " + eSize + ".");
175a70f416c9cf2fc6cc5e132c1d656ce07441d6b82Jason Sams        }
17649bdaf0293408159df18a1d8540360f9623c40f7Jason Sams        data1DChecks(xoff, count, data.length, data.length);
17749a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
17849bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    }
17949bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
18049bdaf0293408159df18a1d8540360f9623c40f7Jason Sams
18149bdaf0293408159df18a1d8540360f9623c40f7Jason Sams    public void subElementData(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
217768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    public void subData1D(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    }
222768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    public void subData1D(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    }
227768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams    public void subData1D(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    }
232b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    public void subData1D(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
239b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
240771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
24149a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
242b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
243b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
244b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
245771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
24649a05d7b82956009f03acbb92a064eed054eb031Jason Sams        mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
247b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
248b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
24940a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    public void readData(int[] d) {
250771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
25106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
25240a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
25340a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
25440a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    public void readData(float[] d) {
255771bebb94054d06f97284379c93a2620613513c3Jason Sams        mRS.validate();
25606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationRead(getID(), d);
25740a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams    }
25840a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
25931a7e42f4baa059352f0db119de38428e655eab2Jason Sams    public synchronized void resize(int dimX) {
260bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
26106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
2625edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
26306d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize1D(getID(), dimX);
264d26297fa562d8bb203df1bb5e6ded7f62c56cdb7Jason Sams        mRS.finish();  // Necessary because resize is fifoed and update is async.
26531a7e42f4baa059352f0db119de38428e655eab2Jason Sams
26606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        int typeID = mRS.nAllocationGetType(getID());
26731a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType = new Type(typeID, mRS);
26831a7e42f4baa059352f0db119de38428e655eab2Jason Sams        mType.updateFromNative();
2695edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
2705edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams
2715edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    /*
2725edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    public void resize(int dimX, int dimY) {
2735edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
27406d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
2755edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
2765edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        if (mType.getY() == 0) {
27706d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
2785edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams        }
27906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        mRS.nAllocationResize2D(getID(), dimX, dimY);
2805edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    }
2815edc608a0749ed4b7074b5c1243043eb722c3c31Jason Sams    */
28240a29e8e28772b37ab0f9fe9708ecdcba24abb84Jason Sams
283bd1c3ad0cdf8e60b849a009cdc0b36764cc1dacbJason Sams
284b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
285b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    // creation
286b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
28749a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
288b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    static {
289b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        mBitmapOptions.inScaled = false;
290b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
291b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
292d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams    static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) {
293771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
2945476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (type.getID() == 0) {
29506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSInvalidStateException("Bad Type");
29606d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams        }
297d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage);
2985476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
29906d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
3001bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams        }
3015476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, type, usage);
302b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
303b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
304e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
305e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
306e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams    }
307e5d3712d9eaef7ebbf142b508bd740414d930cb0Jason Sams
3085476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createTyped(RenderScript rs, Type type) {
309d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
3105476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
3111bada8cd6e4f340de93cff4a2439835fc3b1456cJason Sams
3125476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e,
3135476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                         int count, int usage) {
314771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
315768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type.Builder b = new Type.Builder(rs, e);
316bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        b.setX(count);
317768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams        Type t = b.create();
318768bc02d815a94ad29146f1ed60c847d1af118ccJason Sams
319d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
3205476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
32106d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Allocation creation failed.");
322b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams        }
3235476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
3245476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
3255476b450e50939940dcf3f15c92335cee2fc572dJason Sams
3265476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createSized(RenderScript rs, Element e, int count) {
327d4b23b54445b13dacaafad97d100999abb36ea6fJason Sams        return createSized(rs, e, count, USAGE_SCRIPT);
328b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams    }
329b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
33049a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Element elementFromBitmap(RenderScript rs, Bitmap b) {
3318a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        final Bitmap.Config bc = b.getConfig();
3328a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ALPHA_8) {
3338a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.A_8(rs);
3348a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
3358a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_4444) {
3368a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_4444(rs);
3378a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
3388a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.ARGB_8888) {
3398a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGBA_8888(rs);
3408a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
3418a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        if (bc == Bitmap.Config.RGB_565) {
3428a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams            return Element.RGB_565(rs);
3438a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        }
3444bd1a3dbcad2ae424293e276434b45ebee97248dJeff Sharkey        throw new RSInvalidStateException("Bad bitmap type: " + bc);
3458a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
3468a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
34749a05d7b82956009f03acbb92a064eed054eb031Jason Sams    static Type typeFromBitmap(RenderScript rs, Bitmap b,
3484ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                       MipmapControl mip) {
3498a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Element e = elementFromBitmap(rs, b);
3508a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        Type.Builder tb = new Type.Builder(rs, e);
351bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(b.getWidth());
352bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(b.getHeight());
3534ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
3548a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams        return tb.create();
3558a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
3568a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
35767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
3584ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                              MipmapControl mips,
3595476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
360771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
3615476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Type t = typeFromBitmap(rs, b, mips);
3628a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
3635476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
3645476b450e50939940dcf3f15c92335cee2fc572dJason Sams        if (id == 0) {
36506d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Load failed.");
366718cd1f322ee5b62b6a49cb36195bcb18a5ab711Jason Sams        }
3675476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
3685476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
3695476b450e50939940dcf3f15c92335cee2fc572dJason Sams
3706d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
3716d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
3726d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                USAGE_GRAPHICS_TEXTURE);
3738a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams    }
3748a64743f37ed35af7c2204acd18bb3d62d8f66d5Jason Sams
37567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
3764ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                     MipmapControl mips,
3775476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     CubemapLayout layout,
3785476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     int usage) {
37967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        rs.validate();
3805476b450e50939940dcf3f15c92335cee2fc572dJason Sams
38167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int height = b.getHeight();
38267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        int width = b.getWidth();
38367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
38467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (layout != CubemapLayout.VERTICAL_FACE_LIST) {
38567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only vertical face list supported");
38667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
38767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (height % 6 != 0) {
38867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
38967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
39067f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (height / 6 != width) {
39167f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only square cobe map faces supported");
39267f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
39367f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        boolean isPow2 = (width & (width - 1)) == 0;
39467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if (!isPow2) {
39567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
39667f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
39767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
39867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Element e = elementFromBitmap(rs, b);
39967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type.Builder tb = new Type.Builder(rs, e);
400bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setX(width);
401bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setY(width);
402bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams        tb.setFaces(true);
4034ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams        tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
40467f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        Type t = tb.create();
40567f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
4065476b450e50939940dcf3f15c92335cee2fc572dJason Sams        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
40767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        if(id == 0) {
40867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
40967f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk        }
4105476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return new Allocation(id, rs, t, usage);
4115476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
4125476b450e50939940dcf3f15c92335cee2fc572dJason Sams
4135476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
4145476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                     CubemapLayout layout) {
4156d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
4166d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                       layout, USAGE_GRAPHICS_TEXTURE);
41767f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk    }
41867f2e442a31b8395e3c1951f8e91139ec7f2be99Alex Sakhartchouk
4195476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
4205476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
4215476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int id,
4224ef6650bd05a39a09958ea1db92f120ea4949cb1Jason Sams                                                      MipmapControl mips,
4235476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      int usage) {
424b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
425771bebb94054d06f97284379c93a2620613513c3Jason Sams        rs.validate();
4265476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Bitmap b = BitmapFactory.decodeResource(res, id);
4275476b450e50939940dcf3f15c92335cee2fc572dJason Sams        Allocation alloc = createFromBitmap(rs, b, mips, usage);
4285476b450e50939940dcf3f15c92335cee2fc572dJason Sams        b.recycle();
4295476b450e50939940dcf3f15c92335cee2fc572dJason Sams        return alloc;
4305476b450e50939940dcf3f15c92335cee2fc572dJason Sams    }
431650a3eb7d621dc8e81573142a4498bbd07bcde27Romain Guy
4325476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromBitmapResource(RenderScript rs,
4335476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                                      Resources res,
4346d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                                      int id) {
4356d8eb266dd398abf0511685fdaf98abba3396174Jason Sams        return createFromBitmapResource(rs, res, id,
4366d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        MipmapControl.MIPMAP_NONE,
4376d8eb266dd398abf0511685fdaf98abba3396174Jason Sams                                        USAGE_GRAPHICS_TEXTURE);
4386d8eb266dd398abf0511685fdaf98abba3396174Jason Sams    }
4396d8eb266dd398abf0511685fdaf98abba3396174Jason Sams
4405476b450e50939940dcf3f15c92335cee2fc572dJason Sams    static public Allocation createFromString(RenderScript rs,
4415476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              String str,
4425476b450e50939940dcf3f15c92335cee2fc572dJason Sams                                              int usage) {
4435476b450e50939940dcf3f15c92335cee2fc572dJason Sams        rs.validate();
4449b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        byte[] allocArray = null;
4459b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        try {
4469b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            allocArray = str.getBytes("UTF-8");
4475476b450e50939940dcf3f15c92335cee2fc572dJason Sams            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
448bf6ef8d78fffbce6c1849a4a28fb3f4401ad039eJason Sams            alloc.copyFrom(allocArray);
4499b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk            return alloc;
4509b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
4519b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        catch (Exception e) {
45206d69de78845659e6904ae4964e606a7f1a6a4a8Jason Sams            throw new RSRuntimeException("Could not convert string to utf-8.");
4539b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk        }
4549b949fce39f0f39ce9275b71d7c347210775e7a8Alex Sakhartchouk    }
455b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams}
456b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
457b8c5a84e7c23746a3fc26013e0880d3d95ca6588Jason Sams
458