blob: e682e93bf66ad3290f87e482561042ed1b4c2cff [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/**
27 * @hide
28 *
29 **/
30public class AllocationAdapter extends Allocation {
31 private int mSelectedDimX;
32 private int mSelectedDimY;
33 private int mSelectedCount;
34 private Allocation mAlloc;
35
36 private int mSelectedLOD = 0;
37 private Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITVE_X;;
38
39 AllocationAdapter(int id, RenderScript rs, Allocation alloc) {
40 super(id, rs, null, alloc.mUsage);
41 Type t = alloc.getType();
42 mSelectedDimX = t.getX();
43 mSelectedDimY = t.getY();
44 mSelectedCount = t.getCount();
45 }
46
47
48 public void copyFrom(BaseObj[] d) {
49 mRS.validate();
50 if (d.length != mSelectedCount) {
51 throw new RSIllegalArgumentException("Array size mismatch, allocation size = " +
52 mSelectedCount + ", array length = " + d.length);
53 }
54 int i[] = new int[d.length];
55 for (int ct=0; ct < d.length; ct++) {
56 i[ct] = d[ct].getID();
57 }
58 subData1D(0, mType.getCount(), i);
59 }
60
61 void validateBitmap(Bitmap b) {
62 mRS.validate();
63 if(mSelectedDimX != b.getWidth() ||
64 mSelectedDimY != b.getHeight()) {
65 throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
66 }
67 }
68
69 public void copyFrom(int[] d) {
70 mRS.validate();
71 subData1D(0, mSelectedCount, d);
72 }
73 public void copyFrom(short[] d) {
74 mRS.validate();
75 subData1D(0, mSelectedCount, d);
76 }
77 public void copyFrom(byte[] d) {
78 mRS.validate();
79 subData1D(0, mSelectedCount, d);
80 }
81 public void copyFrom(float[] d) {
82 mRS.validate();
83 subData1D(0, mSelectedCount, d);
84 }
85 public void copyFrom(Bitmap b) {
86 validateBitmap(b);
87 mRS.nAllocationCopyFromBitmap(getID(), b);
88 }
89
90 public void copyTo(Bitmap b) {
91 validateBitmap(b);
92 mRS.nAllocationCopyToBitmap(getID(), b);
93 }
94
95
96 public void subData(int xoff, FieldPacker fp) {
97 int eSize = mType.mElement.getSizeBytes();
98 final byte[] data = fp.getData();
99
100 int count = data.length / eSize;
101 if ((eSize * count) != data.length) {
102 throw new RSIllegalArgumentException("Field packer length " + data.length +
103 " not divisible by element size " + eSize + ".");
104 }
105 data1DChecks(xoff, count, data.length, data.length);
106 mRS.nAllocationData1D(getID(), xoff, mSelectedLOD, count, data, data.length);
107 }
108
109
110 public void subElementData(int xoff, int component_number, FieldPacker fp) {
111 if (component_number >= mType.mElement.mElements.length) {
112 throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
113 }
114 if(xoff < 0) {
115 throw new RSIllegalArgumentException("Offset must be >= 0.");
116 }
117
118 final byte[] data = fp.getData();
119 int eSize = mType.mElement.mElements[component_number].getSizeBytes();
120
121 if (data.length != eSize) {
122 throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
123 " does not match component size " + eSize + ".");
124 }
125
126 mRS.nAllocationElementData1D(getID(), xoff, mSelectedLOD, component_number, data, data.length);
127 }
128
129 void data1DChecks(int off, int count, int len, int dataSize) {
130 mRS.validate();
131 if(off < 0) {
132 throw new RSIllegalArgumentException("Offset must be >= 0.");
133 }
134 if(count < 1) {
135 throw new RSIllegalArgumentException("Count must be >= 1.");
136 }
137 if((off + count) > mSelectedDimX * mSelectedDimY) {
138 throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
139 ", got " + count + " at offset " + off + ".");
140 }
141 if((len) < dataSize) {
142 throw new RSIllegalArgumentException("Array too small for allocation type.");
143 }
144 }
145
146 public void subData1D(int off, int count, int[] d) {
147 int dataSize = mAlloc.mType.mElement.getSizeBytes() * count;
148 data1DChecks(off, count, d.length * 4, dataSize);
149 mRS.nAllocationData1D(getID(), off, mSelectedLOD, count, d, dataSize);
150 }
151 public void subData1D(int off, int count, short[] d) {
152 int dataSize = mAlloc.mType.mElement.getSizeBytes() * count;
153 data1DChecks(off, count, d.length * 2, dataSize);
154 mRS.nAllocationData1D(getID(), off, mSelectedLOD, count, d, dataSize);
155 }
156 public void subData1D(int off, int count, byte[] d) {
157 int dataSize = mAlloc.mType.mElement.getSizeBytes() * count;
158 data1DChecks(off, count, d.length, dataSize);
159 mRS.nAllocationData1D(getID(), off, mSelectedLOD, count, d, dataSize);
160 }
161 public void subData1D(int off, int count, float[] d) {
162 int dataSize = mAlloc.mType.mElement.getSizeBytes() * count;
163 data1DChecks(off, count, d.length * 4, dataSize);
164 mRS.nAllocationData1D(getID(), off, mSelectedLOD, count, d, dataSize);
165 }
166
167
168 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
169 mRS.validate();
170 mRS.nAllocationData2D(getID(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h, d, d.length * 4);
171 }
172
173 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
174 mRS.validate();
175 mRS.nAllocationData2D(getID(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h, d, d.length * 4);
176 }
177
178 public void readData(int[] d) {
179 mRS.validate();
180 mRS.nAllocationRead(getID(), d);
181 }
182
183 public void readData(float[] d) {
184 mRS.validate();
185 mRS.nAllocationRead(getID(), d);
186 }
187
188 public void setLOD(int lod) {
189 }
190
191 public void setFace(Type.CubemapFace cf) {
192 }
193
194 public void setY(int y) {
195 }
196
197 public void setZ(int z) {
198 }
199
200 // creation
201 //static public AllocationAdapter create1D(RenderScript rs, Allocation a) {
202 //}
203
204 static public AllocationAdapter create2D(RenderScript rs, Allocation a) {
205 rs.validate();
206 AllocationAdapter aa = new AllocationAdapter(0, rs, a);
207 return aa;
208 }
209
210
211}
212
213