| 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 | |
| 50 | public void destroy() { |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 51 | if(mDestroyed) { |
| 52 | throw new IllegalStateException("Object already destroyed."); |
| 53 | } |
| 54 | mDestroyed = true; |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 55 | mRS.nAllocationDestroy(mID); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | public void data(int[] d) { |
| 59 | mRS.nAllocationData(mID, d); |
| 60 | } |
| 61 | |
| 62 | public void data(float[] d) { |
| 63 | mRS.nAllocationData(mID, d); |
| 64 | } |
| 65 | |
| 66 | public void subData1D(int off, int count, int[] d) { |
| 67 | mRS.nAllocationSubData1D(mID, off, count, d); |
| 68 | } |
| 69 | |
| 70 | public void subData1D(int off, int count, float[] d) { |
| 71 | mRS.nAllocationSubData1D(mID, off, count, d); |
| 72 | } |
| 73 | |
| 74 | public void subData2D(int xoff, int yoff, int w, int h, int[] d) { |
| 75 | mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d); |
| 76 | } |
| 77 | |
| 78 | public void subData2D(int xoff, int yoff, int w, int h, float[] d) { |
| 79 | mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d); |
| 80 | } |
| 81 | |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 82 | public void readData(int[] d) { |
| 83 | mRS.nAllocationRead(mID, d); |
| 84 | } |
| 85 | |
| 86 | public void readData(float[] d) { |
| 87 | mRS.nAllocationRead(mID, d); |
| 88 | } |
| 89 | |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 90 | public void data(Object o) { |
| 91 | mRS.nAllocationDataFromObject(mID, mType, o); |
| 92 | } |
| 93 | |
| Jason Sams | 40a29e8 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 94 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 95 | public class Adapter1D extends BaseObj { |
| 96 | Adapter1D(int id, RenderScript rs) { |
| 97 | super(rs); |
| 98 | mID = id; |
| 99 | } |
| 100 | |
| 101 | public void destroy() { |
| 102 | mRS.nAdapter1DDestroy(mID); |
| 103 | mID = 0; |
| 104 | } |
| 105 | |
| 106 | public void setConstraint(Dimension dim, int value) { |
| 107 | mRS.nAdapter1DSetConstraint(mID, dim.mID, value); |
| 108 | } |
| 109 | |
| 110 | public void data(int[] d) { |
| 111 | mRS.nAdapter1DData(mID, d); |
| 112 | } |
| 113 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 114 | public void data(float[] d) { |
| 115 | mRS.nAdapter1DData(mID, d); |
| 116 | } |
| 117 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 118 | public void subData(int off, int count, int[] d) { |
| 119 | mRS.nAdapter1DSubData(mID, off, count, d); |
| 120 | } |
| 121 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 122 | public void subData(int off, int count, float[] d) { |
| 123 | mRS.nAdapter1DSubData(mID, off, count, d); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | public Adapter1D createAdapter1D() { |
| 128 | int id = mRS.nAdapter1DCreate(); |
| 129 | if (id != 0) { |
| 130 | mRS.nAdapter1DBindAllocation(id, mID); |
| 131 | } |
| 132 | return new Adapter1D(id, mRS); |
| 133 | } |
| 134 | |
| 135 | |
| Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 136 | public class Adapter2D extends BaseObj { |
| 137 | Adapter2D(int id, RenderScript rs) { |
| 138 | super(rs); |
| 139 | mID = id; |
| 140 | } |
| 141 | |
| 142 | public void destroy() { |
| 143 | mRS.nAdapter2DDestroy(mID); |
| 144 | mID = 0; |
| 145 | } |
| 146 | |
| 147 | public void setConstraint(Dimension dim, int value) { |
| 148 | mRS.nAdapter2DSetConstraint(mID, dim.mID, value); |
| 149 | } |
| 150 | |
| 151 | public void data(int[] d) { |
| 152 | mRS.nAdapter2DData(mID, d); |
| 153 | } |
| 154 | |
| 155 | public void data(float[] d) { |
| 156 | mRS.nAdapter2DData(mID, d); |
| 157 | } |
| 158 | |
| 159 | public void subData(int xoff, int yoff, int w, int h, int[] d) { |
| 160 | mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); |
| 161 | } |
| 162 | |
| 163 | public void subData(int xoff, int yoff, int w, int h, float[] d) { |
| 164 | mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | public Adapter2D createAdapter2D() { |
| 169 | int id = mRS.nAdapter2DCreate(); |
| 170 | if (id != 0) { |
| 171 | mRS.nAdapter2DBindAllocation(id, mID); |
| 172 | } |
| 173 | return new Adapter2D(id, mRS); |
| 174 | } |
| 175 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 176 | |
| 177 | // creation |
| 178 | |
| 179 | private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options(); |
| 180 | static { |
| 181 | mBitmapOptions.inScaled = false; |
| 182 | } |
| 183 | |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 184 | static public Allocation createTyped(RenderScript rs, Type type) |
| 185 | throws IllegalArgumentException { |
| 186 | |
| 187 | if(type.mID == 0) { |
| 188 | throw new IllegalStateException("Bad Type"); |
| 189 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 190 | int id = rs.nAllocationCreateTyped(type.mID); |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 191 | return new Allocation(id, rs, type); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 194 | static public Allocation createSized(RenderScript rs, Element e, int count) |
| 195 | throws IllegalArgumentException { |
| 196 | |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 197 | int id; |
| 198 | if(e.mIsPredefined) { |
| 199 | id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count); |
| 200 | } else { |
| 201 | id = rs.nAllocationCreateSized(e.mID, count); |
| Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 202 | if(id == 0) { |
| 203 | throw new IllegalStateException("Bad element."); |
| 204 | } |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 205 | } |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 206 | return new Allocation(id, rs, null); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) |
| 210 | throws IllegalArgumentException { |
| 211 | if(!dstFmt.mIsPredefined) { |
| 212 | throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element."); |
| 213 | } |
| 214 | |
| 215 | int id = rs.nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b); |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 216 | return new Allocation(id, rs, null); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static public Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) |
| 220 | throws IllegalArgumentException { |
| 221 | if(!dstFmt.mIsPredefined) { |
| 222 | throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element."); |
| 223 | } |
| 224 | |
| 225 | int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b); |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 226 | return new Allocation(id, rs, null); |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) |
| 230 | throws IllegalArgumentException { |
| 231 | |
| 232 | Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); |
| 233 | return createFromBitmap(rs, b, dstFmt, genMips); |
| 234 | } |
| 235 | |
| 236 | static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) |
| 237 | throws IllegalArgumentException { |
| 238 | |
| 239 | Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); |
| 240 | return createFromBitmapBoxed(rs, b, dstFmt, genMips); |
| 241 | } |
| Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 242 | /* |
| 243 | public static Allocation createFromObject(RenderScript rs, Object o) { |
| 244 | Class c = o.getClass(); |
| 245 | Type t; |
| 246 | if(c.isArray()) { |
| 247 | t = Type.createFromClass(rs, c, Array.getLength(o)); |
| 248 | } else { |
| 249 | t = Type.createFromClass(rs, c, 1); |
| 250 | } |
| 251 | Allocation alloc = createTyped(rs, t); |
| 252 | t.destroy(); |
| 253 | return alloc; |
| 254 | } |
| 255 | */ |
| Jason Sams | b8c5a84 | 2009-07-31 20:40:47 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | |