| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.renderscript; |
| 18 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 19 | import java.lang.reflect.Field; |
| 20 | import java.lang.reflect.Array; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | |
| 25 | import android.content.res.Resources; |
| 26 | import android.graphics.Bitmap; |
| 27 | import android.graphics.BitmapFactory; |
| 28 | import android.os.Bundle; |
| 29 | import android.renderscript.Type; |
| 30 | import android.util.Config; |
| 31 | import android.util.Log; |
| 32 | |
| 33 | /** |
| 34 | * @hide |
| 35 | * |
| 36 | **/ |
| 37 | public class Allocation extends BaseObj { |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 38 | Type mType; |
| 39 | |
| 40 | Allocation(int id, RenderScript rs, Type t) { |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 41 | super(rs); |
| 42 | mID = id; |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 43 | mType = t; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | public void uploadToTexture(int baseMipLevel) { |
| 47 | mRS.nAllocationUploadToTexture(mID, baseMipLevel); |
| 48 | } |
| 49 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 50 | public void data(int[] d) { |
| 51 | mRS.nAllocationData(mID, d); |
| 52 | } |
| 53 | |
| 54 | public void data(float[] d) { |
| 55 | mRS.nAllocationData(mID, d); |
| 56 | } |
| 57 | |
| 58 | public void subData1D(int off, int count, int[] d) { |
| 59 | mRS.nAllocationSubData1D(mID, off, count, d); |
| 60 | } |
| 61 | |
| 62 | public void subData1D(int off, int count, float[] d) { |
| 63 | mRS.nAllocationSubData1D(mID, off, count, d); |
| 64 | } |
| 65 | |
| 66 | public void subData2D(int xoff, int yoff, int w, int h, int[] d) { |
| 67 | mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d); |
| 68 | } |
| 69 | |
| 70 | public void subData2D(int xoff, int yoff, int w, int h, float[] d) { |
| 71 | mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d); |
| 72 | } |
| 73 | |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 74 | public void readData(int[] d) { |
| 75 | mRS.nAllocationRead(mID, d); |
| 76 | } |
| 77 | |
| 78 | public void readData(float[] d) { |
| 79 | mRS.nAllocationRead(mID, d); |
| 80 | } |
| 81 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 82 | public void data(Object o) { |
| 83 | mRS.nAllocationDataFromObject(mID, mType, o); |
| 84 | } |
| 85 | |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 86 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 87 | public class Adapter1D extends BaseObj { |
| 88 | Adapter1D(int id, RenderScript rs) { |
| 89 | super(rs); |
| 90 | mID = id; |
| 91 | } |
| 92 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 93 | public void setConstraint(Dimension dim, int value) { |
| 94 | mRS.nAdapter1DSetConstraint(mID, dim.mID, value); |
| 95 | } |
| 96 | |
| 97 | public void data(int[] d) { |
| 98 | mRS.nAdapter1DData(mID, d); |
| 99 | } |
| 100 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 101 | public void data(float[] d) { |
| 102 | mRS.nAdapter1DData(mID, d); |
| 103 | } |
| 104 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 105 | public void subData(int off, int count, int[] d) { |
| 106 | mRS.nAdapter1DSubData(mID, off, count, d); |
| 107 | } |
| 108 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 109 | public void subData(int off, int count, float[] d) { |
| 110 | mRS.nAdapter1DSubData(mID, off, count, d); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public Adapter1D createAdapter1D() { |
| 115 | int id = mRS.nAdapter1DCreate(); |
| 116 | if (id != 0) { |
| 117 | mRS.nAdapter1DBindAllocation(id, mID); |
| 118 | } |
| 119 | return new Adapter1D(id, mRS); |
| 120 | } |
| 121 | |
| 122 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 123 | public class Adapter2D extends BaseObj { |
| 124 | Adapter2D(int id, RenderScript rs) { |
| 125 | super(rs); |
| 126 | mID = id; |
| 127 | } |
| 128 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 129 | public void setConstraint(Dimension dim, int value) { |
| 130 | mRS.nAdapter2DSetConstraint(mID, dim.mID, value); |
| 131 | } |
| 132 | |
| 133 | public void data(int[] d) { |
| 134 | mRS.nAdapter2DData(mID, d); |
| 135 | } |
| 136 | |
| 137 | public void data(float[] d) { |
| 138 | mRS.nAdapter2DData(mID, d); |
| 139 | } |
| 140 | |
| 141 | public void subData(int xoff, int yoff, int w, int h, int[] d) { |
| 142 | mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); |
| 143 | } |
| 144 | |
| 145 | public void subData(int xoff, int yoff, int w, int h, float[] d) { |
| 146 | mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public Adapter2D createAdapter2D() { |
| 151 | int id = mRS.nAdapter2DCreate(); |
| 152 | if (id != 0) { |
| 153 | mRS.nAdapter2DBindAllocation(id, mID); |
| 154 | } |
| 155 | return new Adapter2D(id, mRS); |
| 156 | } |
| 157 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 158 | |
| 159 | // creation |
| 160 | |
| 161 | private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); |
| 162 | static { |
| 163 | mBitmapOptions.inScaled = false; |
| 164 | } |
| 165 | |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 166 | static public Allocation createTyped(RenderScript rs, Type type) |
| 167 | throws IllegalArgumentException { |
| 168 | |
| 169 | if(type.mID == 0) { |
| 170 | throw new IllegalStateException("Bad Type"); |
| 171 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 172 | int id = rs.nAllocationCreateTyped(type.mID); |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 173 | return new Allocation(id, rs, type); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 176 | static public Allocation createSized(RenderScript rs, Element e, int count) |
| 177 | throws IllegalArgumentException { |
| 178 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 179 | int id; |
| 180 | if(e.mIsPredefined) { |
| 181 | id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count); |
| 182 | } else { |
| 183 | id = rs.nAllocationCreateSized(e.mID, count); |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 184 | if(id == 0) { |
| 185 | throw new IllegalStateException("Bad element."); |
| 186 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 187 | } |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 188 | return new Allocation(id, rs, null); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) |
| 192 | throws IllegalArgumentException { |
| 193 | if(!dstFmt.mIsPredefined) { |
| 194 | throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element."); |
| 195 | } |
| 196 | |
| 197 | int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b); |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 198 | return new Allocation(id, rs, null); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) |
| 202 | throws IllegalArgumentException { |
| 203 | if(!dstFmt.mIsPredefined) { |
| 204 | throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element."); |
| 205 | } |
| 206 | |
| 207 | int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b); |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 208 | return new Allocation(id, rs, null); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) |
| 212 | throws IllegalArgumentException { |
| 213 | |
| 214 | Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); |
| 215 | return createFromBitmap(rs, b, dstFmt, genMips); |
| 216 | } |
| 217 | |
| 218 | static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) |
| 219 | throws IllegalArgumentException { |
| 220 | |
| 221 | Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); |
| 222 | return createFromBitmapBoxed(rs, b, dstFmt, genMips); |
| 223 | } |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 224 | /* |
| 225 | public static Allocation createFromObject(RenderScript rs, Object o) { |
| 226 | Class c = o.getClass(); |
| 227 | Type t; |
| 228 | if(c.isArray()) { |
| 229 | t = Type.createFromClass(rs, c, Array.getLength(o)); |
| 230 | } else { |
| 231 | t = Type.createFromClass(rs, c, 1); |
| 232 | } |
| 233 | Allocation alloc = createTyped(rs, t); |
| 234 | t.destroy(); |
| 235 | return alloc; |
| Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame^] | 236 | } |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 237 | */ |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |