blob: ca5246ac4b1d617358bc52d99d42dc0745cf3325 [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;
Jason Sams49a05d72010-12-29 14:31:29 -080020import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
Jason Sams49a05d72010-12-29 14:31:29 -080022import android.util.TypedValue;
23
24/**
Jason Sams49a05d72010-12-29 14:31:29 -080025 *
26 **/
27public class AllocationAdapter extends Allocation {
Jason Sams49a05d72010-12-29 14:31:29 -080028 AllocationAdapter(int id, RenderScript rs, Allocation alloc) {
Jason Samsba862d12011-07-07 15:24:42 -070029 super(id, rs, alloc.mType, alloc.mUsage);
30 mAdaptedAllocation = alloc;
Jason Sams49a05d72010-12-29 14:31:29 -080031 }
32
Jason Samsee2d8092011-06-21 16:42:42 -070033 int getID() {
Jason Samsba862d12011-07-07 15:24:42 -070034 return mAdaptedAllocation.getID();
Jason Samsee2d8092011-06-21 16:42:42 -070035 }
36
Jason Samsba862d12011-07-07 15:24:42 -070037 /**
38 * @hide
39 */
Jason Sams49a05d72010-12-29 14:31:29 -080040 public void subData(int xoff, FieldPacker fp) {
Jason Samsba862d12011-07-07 15:24:42 -070041 super.setFromFieldPacker(xoff, fp);
Jason Sams49a05d72010-12-29 14:31:29 -080042 }
Jason Samsba862d12011-07-07 15:24:42 -070043 /**
44 * @hide
45 */
Jason Sams49a05d72010-12-29 14:31:29 -080046 public void subElementData(int xoff, int component_number, FieldPacker fp) {
Jason Samsba862d12011-07-07 15:24:42 -070047 super.setFromFieldPacker(xoff, component_number, fp);
Jason Sams49a05d72010-12-29 14:31:29 -080048 }
Jason Samsba862d12011-07-07 15:24:42 -070049 /**
50 * @hide
51 */
Jason Sams49a05d72010-12-29 14:31:29 -080052 public void subData1D(int off, int count, int[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070053 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080054 }
Jason Samsba862d12011-07-07 15:24:42 -070055 /**
56 * @hide
57 */
Jason Sams49a05d72010-12-29 14:31:29 -080058 public void subData1D(int off, int count, short[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070059 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080060 }
Jason Samsba862d12011-07-07 15:24:42 -070061 /**
62 * @hide
63 */
Jason Sams49a05d72010-12-29 14:31:29 -080064 public void subData1D(int off, int count, byte[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070065 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080066 }
Jason Samsba862d12011-07-07 15:24:42 -070067 /**
68 * @hide
69 */
Jason Sams49a05d72010-12-29 14:31:29 -080070 public void subData1D(int off, int count, float[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070071 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080072 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070073 /**
Jason Samsba862d12011-07-07 15:24:42 -070074 * @hide
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070075 */
Jason Sams49a05d72010-12-29 14:31:29 -080076 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070077 super.copy2DRangeFrom(xoff, yoff, w, h, d);
Jason Sams49a05d72010-12-29 14:31:29 -080078 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070079 /**
Jason Samsba862d12011-07-07 15:24:42 -070080 * @hide
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070081 */
Jason Samsba862d12011-07-07 15:24:42 -070082 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
83 super.copy2DRangeFrom(xoff, yoff, w, h, d);
Jason Sams49a05d72010-12-29 14:31:29 -080084 }
Jason Samsba862d12011-07-07 15:24:42 -070085 /**
86 * @hide
87 */
Jason Sams49a05d72010-12-29 14:31:29 -080088 public void readData(int[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070089 super.copyTo(d);
Jason Sams49a05d72010-12-29 14:31:29 -080090 }
Jason Samsba862d12011-07-07 15:24:42 -070091 /**
92 * @hide
93 */
Jason Sams49a05d72010-12-29 14:31:29 -080094 public void readData(float[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070095 super.copyTo(d);
Jason Sams49a05d72010-12-29 14:31:29 -080096 }
97
Jason Samsba862d12011-07-07 15:24:42 -070098 void initLOD(int lod) {
Jason Samsee2d8092011-06-21 16:42:42 -070099 if (lod < 0) {
100 throw new RSIllegalArgumentException("Attempting to set negative lod (" + lod + ").");
101 }
102
Jason Samsba862d12011-07-07 15:24:42 -0700103 int tx = mAdaptedAllocation.mType.getX();
104 int ty = mAdaptedAllocation.mType.getY();
105 int tz = mAdaptedAllocation.mType.getZ();
Jason Samsee2d8092011-06-21 16:42:42 -0700106
107 for (int ct=0; ct < lod; ct++) {
108 if ((tx==1) && (ty == 1) && (tz == 1)) {
109 throw new RSIllegalArgumentException("Attempting to set lod (" + lod + ") out of range.");
110 }
111
112 if (tx > 1) tx >>= 1;
113 if (ty > 1) ty >>= 1;
114 if (tz > 1) tz >>= 1;
115 }
116
Jason Samsba862d12011-07-07 15:24:42 -0700117 mCurrentDimX = tx;
118 mCurrentDimY = ty;
119 mCurrentDimZ = tz;
120 mCurrentCount = mCurrentDimX;
121 if (mCurrentDimY > 1) {
122 mCurrentCount *= mCurrentDimY;
Jason Samsee2d8092011-06-21 16:42:42 -0700123 }
Jason Samsba862d12011-07-07 15:24:42 -0700124 if (mCurrentDimZ > 1) {
125 mCurrentCount *= mCurrentDimZ;
Jason Samsee2d8092011-06-21 16:42:42 -0700126 }
Jason Samsba862d12011-07-07 15:24:42 -0700127 mSelectedY = 0;
128 mSelectedZ = 0;
Jason Samsee2d8092011-06-21 16:42:42 -0700129 }
130
131 /**
132 * Set the active LOD. The LOD must be within the range for the
Jason Samsba862d12011-07-07 15:24:42 -0700133 * type being adapted. The base allocation must have mipmaps.
134 *
135 * Because this changes the dimensions of the adapter the
136 * current Y and Z will be reset.
Jason Samsee2d8092011-06-21 16:42:42 -0700137 *
138 * @param lod The LOD to make active.
139 */
Jason Sams49a05d72010-12-29 14:31:29 -0800140 public void setLOD(int lod) {
Jason Samsba862d12011-07-07 15:24:42 -0700141 if (!mAdaptedAllocation.getType().hasMipmaps()) {
Jason Samsee2d8092011-06-21 16:42:42 -0700142 throw new RSInvalidStateException("Cannot set LOD when the allocation type does not include mipmaps.");
143 }
144 if (!mConstrainedLOD) {
145 throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
146 }
147
148 initLOD(lod);
Jason Sams49a05d72010-12-29 14:31:29 -0800149 }
150
Jason Samsba862d12011-07-07 15:24:42 -0700151 /**
152 * Set the active Face. The base allocation must be of a type
153 * that includes faces.
154 *
155 * @param cf The face to make active.
156 */
Jason Sams49a05d72010-12-29 14:31:29 -0800157 public void setFace(Type.CubemapFace cf) {
Jason Samsba862d12011-07-07 15:24:42 -0700158 if (!mAdaptedAllocation.getType().hasFaces()) {
159 throw new RSInvalidStateException("Cannot set Face when the allocation type does not include faces.");
160 }
161 if (!mConstrainedFace) {
162 throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
163 }
164 if (cf == null) {
165 throw new RSIllegalArgumentException("Cannot set null face.");
166 }
167
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700168 mSelectedFace = cf;
Jason Sams49a05d72010-12-29 14:31:29 -0800169 }
170
Jason Samsba862d12011-07-07 15:24:42 -0700171 /**
172 * Set the active Y. The y value must be within the range for
173 * the allocation being adapted. The base allocation must
174 * contain the Y dimension.
175 *
176 * @param y The y to make active.
177 */
Jason Sams49a05d72010-12-29 14:31:29 -0800178 public void setY(int y) {
Jason Samsba862d12011-07-07 15:24:42 -0700179 if (mAdaptedAllocation.getType().getY() == 0) {
180 throw new RSInvalidStateException("Cannot set Y when the allocation type does not include Y dim.");
181 }
182 if (mAdaptedAllocation.getType().getY() <= y) {
183 throw new RSInvalidStateException("Cannot set Y greater than dimension of allocation.");
184 }
185 if (!mConstrainedY) {
186 throw new RSInvalidStateException("Cannot set Y when the adapter includes Y.");
187 }
188
189 mSelectedY = y;
Jason Sams49a05d72010-12-29 14:31:29 -0800190 }
191
Jason Samsba862d12011-07-07 15:24:42 -0700192 /**
193 * Set the active Z. The z value must be within the range for
194 * the allocation being adapted. The base allocation must
195 * contain the Z dimension.
196 *
197 * @param z The z to make active.
198 */
Jason Sams49a05d72010-12-29 14:31:29 -0800199 public void setZ(int z) {
Jason Samsba862d12011-07-07 15:24:42 -0700200 if (mAdaptedAllocation.getType().getZ() == 0) {
201 throw new RSInvalidStateException("Cannot set Z when the allocation type does not include Z dim.");
202 }
203 if (mAdaptedAllocation.getType().getZ() <= z) {
204 throw new RSInvalidStateException("Cannot set Z greater than dimension of allocation.");
205 }
206 if (!mConstrainedZ) {
207 throw new RSInvalidStateException("Cannot set Z when the adapter includes Z.");
208 }
209
210 mSelectedZ = z;
Jason Sams49a05d72010-12-29 14:31:29 -0800211 }
212
Jason Samsba862d12011-07-07 15:24:42 -0700213 static public AllocationAdapter create1D(RenderScript rs, Allocation a) {
214 rs.validate();
215 AllocationAdapter aa = new AllocationAdapter(0, rs, a);
216 aa.mConstrainedLOD = true;
217 aa.mConstrainedFace = true;
218 aa.mConstrainedY = true;
219 aa.mConstrainedZ = true;
220 aa.initLOD(0);
221 return aa;
222 }
Jason Sams49a05d72010-12-29 14:31:29 -0800223
224 static public AllocationAdapter create2D(RenderScript rs, Allocation a) {
Jason Samsba862d12011-07-07 15:24:42 -0700225 android.util.Log.e("rs", "create2d " + a);
Jason Sams49a05d72010-12-29 14:31:29 -0800226 rs.validate();
227 AllocationAdapter aa = new AllocationAdapter(0, rs, a);
Jason Samsee2d8092011-06-21 16:42:42 -0700228 aa.mConstrainedLOD = true;
229 aa.mConstrainedFace = true;
230 aa.mConstrainedY = false;
231 aa.mConstrainedZ = true;
232 aa.initLOD(0);
Jason Sams49a05d72010-12-29 14:31:29 -0800233 return aa;
234 }
235
236
Jason Samsba862d12011-07-07 15:24:42 -0700237 /**
238 * Override the Allocation resize. Resizing adapters is not
239 * allowed and will throw a RSInvalidStateException.
240 *
241 * @param dimX ignored.
242 */
243 public synchronized void resize(int dimX) {
244 throw new RSInvalidStateException("Resize not allowed for Adapters.");
245 }
246
Jason Sams49a05d72010-12-29 14:31:29 -0800247}
248
249