blob: 61f2e1fba07955fb11d474be12898fa8a902fefc [file] [log] [blame]
Jason Sams49a05d72010-12-29 14:31:29 -08001/*
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
19import android.content.res.Resources;
20import android.content.res.AssetManager;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.util.Log;
24import android.util.TypedValue;
25
26/**
Jason Sams49a05d72010-12-29 14:31:29 -080027 *
28 **/
29public class AllocationAdapter extends Allocation {
30 private int mSelectedDimX;
31 private int mSelectedDimY;
32 private int mSelectedCount;
33 private Allocation mAlloc;
34
35 private int mSelectedLOD = 0;
Stephen Hines20fbd012011-06-16 17:44:53 -070036 private Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
Jason Sams49a05d72010-12-29 14:31:29 -080037
38 AllocationAdapter(int id, RenderScript rs, Allocation alloc) {
39 super(id, rs, null, alloc.mUsage);
40 Type t = alloc.getType();
41 mSelectedDimX = t.getX();
42 mSelectedDimY = t.getY();
43 mSelectedCount = t.getCount();
44 }
45
46
47 public void copyFrom(BaseObj[] d) {
48 mRS.validate();
49 if (d.length != mSelectedCount) {
50 throw new RSIllegalArgumentException("Array size mismatch, allocation size = " +
51 mSelectedCount + ", array length = " + d.length);
52 }
53 int i[] = new int[d.length];
54 for (int ct=0; ct < d.length; ct++) {
55 i[ct] = d[ct].getID();
56 }
57 subData1D(0, mType.getCount(), i);
58 }
59
60 void validateBitmap(Bitmap b) {
61 mRS.validate();
62 if(mSelectedDimX != b.getWidth() ||
63 mSelectedDimY != b.getHeight()) {
64 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
65 }
66 }
67
68 public void copyFrom(int[] d) {
69 mRS.validate();
70 subData1D(0, mSelectedCount, d);
71 }
72 public void copyFrom(short[] d) {
73 mRS.validate();
74 subData1D(0, mSelectedCount, d);
75 }
76 public void copyFrom(byte[] d) {
77 mRS.validate();
78 subData1D(0, mSelectedCount, d);
79 }
80 public void copyFrom(float[] d) {
81 mRS.validate();
82 subData1D(0, mSelectedCount, d);
83 }
84 public void copyFrom(Bitmap b) {
85 validateBitmap(b);
86 mRS.nAllocationCopyFromBitmap(getID(), b);
87 }
88
89 public void copyTo(Bitmap b) {
90 validateBitmap(b);
91 mRS.nAllocationCopyToBitmap(getID(), b);
92 }
93
94
95 public void subData(int xoff, FieldPacker fp) {
96 int eSize = mType.mElement.getSizeBytes();
97 final byte[] data = fp.getData();
98
99 int count = data.length / eSize;
100 if ((eSize * count) != data.length) {
101 throw new RSIllegalArgumentException("Field packer length " + data.length +
102 " not divisible by element size " + eSize + ".");
103 }
104 data1DChecks(xoff, count, data.length, data.length);
105 mRS.nAllocationData1D(getID(), xoff, mSelectedLOD, count, data, data.length);
106 }
107
108
109 public void subElementData(int xoff, int component_number, FieldPacker fp) {
110 if (component_number >= mType.mElement.mElements.length) {
111 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
112 }
113 if(xoff < 0) {
114 throw new RSIllegalArgumentException("Offset must be >= 0.");
115 }
116
117 final byte[] data = fp.getData();
118 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
119
120 if (data.length != eSize) {
121 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
122 " does not match component size " + eSize + ".");
123 }
124
125 mRS.nAllocationElementData1D(getID(), xoff, mSelectedLOD, component_number, data, data.length);
126 }
127
128 void data1DChecks(int off, int count, int len, int dataSize) {
129 mRS.validate();
130 if(off < 0) {
131 throw new RSIllegalArgumentException("Offset must be >= 0.");
132 }
133 if(count < 1) {
134 throw new RSIllegalArgumentException("Count must be >= 1.");
135 }
136 if((off + count) > mSelectedDimX * mSelectedDimY) {
137 throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
138 ", got " + count + " at offset " + off + ".");
139 }
140 if((len) < dataSize) {
141 throw new RSIllegalArgumentException("Array too small for allocation type.");
142 }
143 }
144
145 public void subData1D(int off, int count, int[] d) {
146 int dataSize = mAlloc.mType.mElement.getSizeBytes() * count;
147 data1DChecks(off, count, d.length * 4, dataSize);
148 mRS.nAllocationData1D(getID(), off, mSelectedLOD, count, d, dataSize);
149 }
150 public void subData1D(int off, int count, short[] d) {
151 int dataSize = mAlloc.mType.mElement.getSizeBytes() * count;
152 data1DChecks(off, count, d.length * 2, dataSize);
153 mRS.nAllocationData1D(getID(), off, mSelectedLOD, count, d, dataSize);
154 }
155 public void subData1D(int off, int count, byte[] d) {
156 int dataSize = mAlloc.mType.mElement.getSizeBytes() * count;
157 data1DChecks(off, count, d.length, dataSize);
158 mRS.nAllocationData1D(getID(), off, mSelectedLOD, count, d, dataSize);
159 }
160 public void subData1D(int off, int count, float[] d) {
161 int dataSize = mAlloc.mType.mElement.getSizeBytes() * count;
162 data1DChecks(off, count, d.length * 4, dataSize);
163 mRS.nAllocationData1D(getID(), off, mSelectedLOD, count, d, dataSize);
164 }
165
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700166 /**
167 * Copy part of an allocation from another allocation.
168 *
169 * @param off The offset of the first element to be copied.
170 * @param count The number of elements to be copied.
171 * @param data the source data allocation.
172 * @param dataOff off The offset of the first element in data to
173 * be copied.
174 */
175 public void subData1D(int off, int count, AllocationAdapter data, int dataOff) {
176 mRS.nAllocationData2D(getID(), off, 0,
177 mSelectedLOD, mSelectedFace.mID,
178 count, 1, data.getID(), dataOff, 0,
179 data.mSelectedLOD, data.mSelectedFace.mID);
180 }
181
Jason Sams49a05d72010-12-29 14:31:29 -0800182
183 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
184 mRS.validate();
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700185 mRS.nAllocationData2D(getID(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
186 w, h, d, d.length * 4);
Jason Sams49a05d72010-12-29 14:31:29 -0800187 }
188
189 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
190 mRS.validate();
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700191 mRS.nAllocationData2D(getID(), xoff, yoff, mSelectedLOD, mSelectedFace.mID,
192 w, h, d, d.length * 4);
193 }
194
195 /**
196 * Copy a rectangular region into the allocation from another
197 * allocation.
198 *
199 * @param xoff X offset of the region to update.
200 * @param yoff Y offset of the region to update.
201 * @param w Width of the incoming region to update.
202 * @param h Height of the incoming region to update.
203 * @param data source allocation.
204 * @param dataXoff X offset in data of the region to update.
205 * @param dataYoff Y offset in data of the region to update.
206 */
207 public void subData2D(int xoff, int yoff, int w, int h,
208 AllocationAdapter data, int dataXoff, int dataYoff) {
209 mRS.validate();
210 mRS.nAllocationData2D(getID(), xoff, yoff,
211 mSelectedLOD, mSelectedFace.mID,
212 w, h, data.getID(), dataXoff, dataYoff,
213 data.mSelectedLOD, data.mSelectedFace.mID);
Jason Sams49a05d72010-12-29 14:31:29 -0800214 }
215
216 public void readData(int[] d) {
217 mRS.validate();
218 mRS.nAllocationRead(getID(), d);
219 }
220
221 public void readData(float[] d) {
222 mRS.validate();
223 mRS.nAllocationRead(getID(), d);
224 }
225
226 public void setLOD(int lod) {
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700227 mSelectedLOD = lod;
Jason Sams49a05d72010-12-29 14:31:29 -0800228 }
229
230 public void setFace(Type.CubemapFace cf) {
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700231 mSelectedFace = cf;
Jason Sams49a05d72010-12-29 14:31:29 -0800232 }
233
234 public void setY(int y) {
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700235 mSelectedDimY = y;
Jason Sams49a05d72010-12-29 14:31:29 -0800236 }
237
238 public void setZ(int z) {
239 }
240
241 // creation
242 //static public AllocationAdapter create1D(RenderScript rs, Allocation a) {
243 //}
244
245 static public AllocationAdapter create2D(RenderScript rs, Allocation a) {
246 rs.validate();
247 AllocationAdapter aa = new AllocationAdapter(0, rs, a);
248 return aa;
249 }
250
251
252}
253
254