blob: f2fedea3a5308d60da2d6eead6cc29f105c7e89e [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;
36 private Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITVE_X;;
37
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
166
167 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
168 mRS.validate();
169 mRS.nAllocationData2D(getID(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h, d, d.length * 4);
170 }
171
172 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
173 mRS.validate();
174 mRS.nAllocationData2D(getID(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h, d, d.length * 4);
175 }
176
177 public void readData(int[] d) {
178 mRS.validate();
179 mRS.nAllocationRead(getID(), d);
180 }
181
182 public void readData(float[] d) {
183 mRS.validate();
184 mRS.nAllocationRead(getID(), d);
185 }
186
187 public void setLOD(int lod) {
188 }
189
190 public void setFace(Type.CubemapFace cf) {
191 }
192
193 public void setY(int y) {
194 }
195
196 public void setZ(int z) {
197 }
198
199 // creation
200 //static public AllocationAdapter create1D(RenderScript rs, Allocation a) {
201 //}
202
203 static public AllocationAdapter create2D(RenderScript rs, Allocation a) {
204 rs.validate();
205 AllocationAdapter aa = new AllocationAdapter(0, rs, a);
206 return aa;
207 }
208
209
210}
211
212