blob: ede475fabff3234411b288658b02828aa50878b0 [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
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
17package android.renderscript;
18
19
20import java.io.IOException;
21import java.io.InputStream;
22
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
26import android.os.Bundle;
27import android.renderscript.Type;
28import android.util.Config;
29import android.util.Log;
30
31/**
32 * @hide
33 *
34 **/
35public class Allocation extends BaseObj {
36 Allocation(int id, RenderScript rs) {
37 super(rs);
38 mID = id;
39 }
40
41 public void uploadToTexture(int baseMipLevel) {
42 mRS.nAllocationUploadToTexture(mID, baseMipLevel);
43 }
44
45 public void destroy() {
Jason Sams1bada8c2009-08-09 17:01:55 -070046 if(mDestroyed) {
47 throw new IllegalStateException("Object already destroyed.");
48 }
49 mDestroyed = true;
Jason Samsb8c5a842009-07-31 20:40:47 -070050 mRS.nAllocationDestroy(mID);
Jason Samsb8c5a842009-07-31 20:40:47 -070051 }
52
53 public void data(int[] d) {
54 mRS.nAllocationData(mID, d);
55 }
56
57 public void data(float[] d) {
58 mRS.nAllocationData(mID, d);
59 }
60
61 public void subData1D(int off, int count, int[] d) {
62 mRS.nAllocationSubData1D(mID, off, count, d);
63 }
64
65 public void subData1D(int off, int count, float[] d) {
66 mRS.nAllocationSubData1D(mID, off, count, d);
67 }
68
69 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
70 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d);
71 }
72
73 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
74 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d);
75 }
76
77 public class Adapter1D extends BaseObj {
78 Adapter1D(int id, RenderScript rs) {
79 super(rs);
80 mID = id;
81 }
82
83 public void destroy() {
84 mRS.nAdapter1DDestroy(mID);
85 mID = 0;
86 }
87
88 public void setConstraint(Dimension dim, int value) {
89 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
90 }
91
92 public void data(int[] d) {
93 mRS.nAdapter1DData(mID, d);
94 }
95
Jason Samsb8c5a842009-07-31 20:40:47 -070096 public void data(float[] d) {
97 mRS.nAdapter1DData(mID, d);
98 }
99
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700100 public void subData(int off, int count, int[] d) {
101 mRS.nAdapter1DSubData(mID, off, count, d);
102 }
103
Jason Samsb8c5a842009-07-31 20:40:47 -0700104 public void subData(int off, int count, float[] d) {
105 mRS.nAdapter1DSubData(mID, off, count, d);
106 }
107 }
108
109 public Adapter1D createAdapter1D() {
110 int id = mRS.nAdapter1DCreate();
111 if (id != 0) {
112 mRS.nAdapter1DBindAllocation(id, mID);
113 }
114 return new Adapter1D(id, mRS);
115 }
116
117
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700118 public class Adapter2D extends BaseObj {
119 Adapter2D(int id, RenderScript rs) {
120 super(rs);
121 mID = id;
122 }
123
124 public void destroy() {
125 mRS.nAdapter2DDestroy(mID);
126 mID = 0;
127 }
128
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 Samsb8c5a842009-07-31 20:40:47 -0700158
159 // creation
160
161 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
162 static {
163 mBitmapOptions.inScaled = false;
164 }
165
Jason Sams1bada8c2009-08-09 17:01:55 -0700166 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 Samsb8c5a842009-07-31 20:40:47 -0700172 int id = rs.nAllocationCreateTyped(type.mID);
173 return new Allocation(id, rs);
174 }
175
Jason Sams1bada8c2009-08-09 17:01:55 -0700176 static public Allocation createSized(RenderScript rs, Element e, int count)
177 throws IllegalArgumentException {
178
Jason Samsb8c5a842009-07-31 20:40:47 -0700179 int id;
180 if(e.mIsPredefined) {
181 id = rs.nAllocationCreatePredefSized(e.mPredefinedID, count);
182 } else {
183 id = rs.nAllocationCreateSized(e.mID, count);
Jason Sams1bada8c2009-08-09 17:01:55 -0700184 if(id == 0) {
185 throw new IllegalStateException("Bad element.");
186 }
Jason Samsb8c5a842009-07-31 20:40:47 -0700187 }
188 return new Allocation(id, rs);
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);
198 return new Allocation(id, rs);
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);
208 return new Allocation(id, rs);
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 }
224
225
226}
227
228