blob: 85d86e5c1bf7b26081715e8760b49d820031701d [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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070024/**
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 Samse07694b2012-04-03 15:36:36 -070033 int getID(RenderScript rs) {
Jason Sams48fe5342011-07-08 13:52:30 -070034 throw new RSInvalidStateException(
35 "This operation is not supported with adapters at this time.");
Jason Samsee2d8092011-06-21 16:42:42 -070036 }
37
Jason Samsba862d12011-07-07 15:24:42 -070038 /**
39 * @hide
40 */
Jason Sams49a05d72010-12-29 14:31:29 -080041 public void subData(int xoff, FieldPacker fp) {
Jason Samsba862d12011-07-07 15:24:42 -070042 super.setFromFieldPacker(xoff, fp);
Jason Sams49a05d72010-12-29 14:31:29 -080043 }
Jason Samsba862d12011-07-07 15:24:42 -070044 /**
45 * @hide
46 */
Jason Sams49a05d72010-12-29 14:31:29 -080047 public void subElementData(int xoff, int component_number, FieldPacker fp) {
Jason Samsba862d12011-07-07 15:24:42 -070048 super.setFromFieldPacker(xoff, component_number, fp);
Jason Sams49a05d72010-12-29 14:31:29 -080049 }
Jason Samsba862d12011-07-07 15:24:42 -070050 /**
51 * @hide
52 */
Jason Sams49a05d72010-12-29 14:31:29 -080053 public void subData1D(int off, int count, int[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070054 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080055 }
Jason Samsba862d12011-07-07 15:24:42 -070056 /**
57 * @hide
58 */
Jason Sams49a05d72010-12-29 14:31:29 -080059 public void subData1D(int off, int count, short[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070060 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080061 }
Jason Samsba862d12011-07-07 15:24:42 -070062 /**
63 * @hide
64 */
Jason Sams49a05d72010-12-29 14:31:29 -080065 public void subData1D(int off, int count, byte[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070066 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080067 }
Jason Samsba862d12011-07-07 15:24:42 -070068 /**
69 * @hide
70 */
Jason Sams49a05d72010-12-29 14:31:29 -080071 public void subData1D(int off, int count, float[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070072 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080073 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070074 /**
Jason Samsba862d12011-07-07 15:24:42 -070075 * @hide
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070076 */
Jason Sams49a05d72010-12-29 14:31:29 -080077 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070078 super.copy2DRangeFrom(xoff, yoff, w, h, d);
Jason Sams49a05d72010-12-29 14:31:29 -080079 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070080 /**
Jason Samsba862d12011-07-07 15:24:42 -070081 * @hide
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070082 */
Jason Samsba862d12011-07-07 15:24:42 -070083 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
84 super.copy2DRangeFrom(xoff, yoff, w, h, d);
Jason Sams49a05d72010-12-29 14:31:29 -080085 }
Jason Samsba862d12011-07-07 15:24:42 -070086 /**
87 * @hide
88 */
Jason Sams49a05d72010-12-29 14:31:29 -080089 public void readData(int[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070090 super.copyTo(d);
Jason Sams49a05d72010-12-29 14:31:29 -080091 }
Jason Samsba862d12011-07-07 15:24:42 -070092 /**
93 * @hide
94 */
Jason Sams49a05d72010-12-29 14:31:29 -080095 public void readData(float[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070096 super.copyTo(d);
Jason Sams49a05d72010-12-29 14:31:29 -080097 }
98
Jason Samsba862d12011-07-07 15:24:42 -070099 void initLOD(int lod) {
Jason Samsee2d8092011-06-21 16:42:42 -0700100 if (lod < 0) {
101 throw new RSIllegalArgumentException("Attempting to set negative lod (" + lod + ").");
102 }
103
Jason Samsba862d12011-07-07 15:24:42 -0700104 int tx = mAdaptedAllocation.mType.getX();
105 int ty = mAdaptedAllocation.mType.getY();
106 int tz = mAdaptedAllocation.mType.getZ();
Jason Samsee2d8092011-06-21 16:42:42 -0700107
108 for (int ct=0; ct < lod; ct++) {
109 if ((tx==1) && (ty == 1) && (tz == 1)) {
110 throw new RSIllegalArgumentException("Attempting to set lod (" + lod + ") out of range.");
111 }
112
113 if (tx > 1) tx >>= 1;
114 if (ty > 1) ty >>= 1;
115 if (tz > 1) tz >>= 1;
116 }
117
Jason Samsba862d12011-07-07 15:24:42 -0700118 mCurrentDimX = tx;
119 mCurrentDimY = ty;
120 mCurrentDimZ = tz;
121 mCurrentCount = mCurrentDimX;
122 if (mCurrentDimY > 1) {
123 mCurrentCount *= mCurrentDimY;
Jason Samsee2d8092011-06-21 16:42:42 -0700124 }
Jason Samsba862d12011-07-07 15:24:42 -0700125 if (mCurrentDimZ > 1) {
126 mCurrentCount *= mCurrentDimZ;
Jason Samsee2d8092011-06-21 16:42:42 -0700127 }
Jason Samsba862d12011-07-07 15:24:42 -0700128 mSelectedY = 0;
129 mSelectedZ = 0;
Jason Samsee2d8092011-06-21 16:42:42 -0700130 }
131
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700132 /**
Jason Samsee2d8092011-06-21 16:42:42 -0700133 * Set the active LOD. The LOD must be within the range for the
Jason Samsba862d12011-07-07 15:24:42 -0700134 * type being adapted. The base allocation must have mipmaps.
135 *
136 * Because this changes the dimensions of the adapter the
137 * current Y and Z will be reset.
Jason Samsee2d8092011-06-21 16:42:42 -0700138 *
139 * @param lod The LOD to make active.
140 */
Jason Sams49a05d72010-12-29 14:31:29 -0800141 public void setLOD(int lod) {
Jason Samsba862d12011-07-07 15:24:42 -0700142 if (!mAdaptedAllocation.getType().hasMipmaps()) {
Jason Samsee2d8092011-06-21 16:42:42 -0700143 throw new RSInvalidStateException("Cannot set LOD when the allocation type does not include mipmaps.");
144 }
145 if (!mConstrainedLOD) {
146 throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
147 }
148
149 initLOD(lod);
Jason Sams49a05d72010-12-29 14:31:29 -0800150 }
151
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700152 /**
Jason Samsba862d12011-07-07 15:24:42 -0700153 * Set the active Face. The base allocation must be of a type
154 * that includes faces.
155 *
156 * @param cf The face to make active.
157 */
Jason Sams49a05d72010-12-29 14:31:29 -0800158 public void setFace(Type.CubemapFace cf) {
Jason Samsba862d12011-07-07 15:24:42 -0700159 if (!mAdaptedAllocation.getType().hasFaces()) {
160 throw new RSInvalidStateException("Cannot set Face when the allocation type does not include faces.");
161 }
162 if (!mConstrainedFace) {
163 throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
164 }
165 if (cf == null) {
166 throw new RSIllegalArgumentException("Cannot set null face.");
167 }
168
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700169 mSelectedFace = cf;
Jason Sams49a05d72010-12-29 14:31:29 -0800170 }
171
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700172 /**
Jason Samsba862d12011-07-07 15:24:42 -0700173 * Set the active Y. The y value must be within the range for
174 * the allocation being adapted. The base allocation must
175 * contain the Y dimension.
176 *
177 * @param y The y to make active.
178 */
Jason Sams49a05d72010-12-29 14:31:29 -0800179 public void setY(int y) {
Jason Samsba862d12011-07-07 15:24:42 -0700180 if (mAdaptedAllocation.getType().getY() == 0) {
181 throw new RSInvalidStateException("Cannot set Y when the allocation type does not include Y dim.");
182 }
183 if (mAdaptedAllocation.getType().getY() <= y) {
184 throw new RSInvalidStateException("Cannot set Y greater than dimension of allocation.");
185 }
186 if (!mConstrainedY) {
187 throw new RSInvalidStateException("Cannot set Y when the adapter includes Y.");
188 }
189
190 mSelectedY = y;
Jason Sams49a05d72010-12-29 14:31:29 -0800191 }
192
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700193 /**
Jason Samsba862d12011-07-07 15:24:42 -0700194 * Set the active Z. The z value must be within the range for
195 * the allocation being adapted. The base allocation must
196 * contain the Z dimension.
197 *
198 * @param z The z to make active.
199 */
Jason Sams49a05d72010-12-29 14:31:29 -0800200 public void setZ(int z) {
Jason Samsba862d12011-07-07 15:24:42 -0700201 if (mAdaptedAllocation.getType().getZ() == 0) {
202 throw new RSInvalidStateException("Cannot set Z when the allocation type does not include Z dim.");
203 }
204 if (mAdaptedAllocation.getType().getZ() <= z) {
205 throw new RSInvalidStateException("Cannot set Z greater than dimension of allocation.");
206 }
207 if (!mConstrainedZ) {
208 throw new RSInvalidStateException("Cannot set Z when the adapter includes Z.");
209 }
210
211 mSelectedZ = z;
Jason Sams49a05d72010-12-29 14:31:29 -0800212 }
213
Jason Samsba862d12011-07-07 15:24:42 -0700214 static public AllocationAdapter create1D(RenderScript rs, Allocation a) {
215 rs.validate();
216 AllocationAdapter aa = new AllocationAdapter(0, rs, a);
217 aa.mConstrainedLOD = true;
218 aa.mConstrainedFace = true;
219 aa.mConstrainedY = true;
220 aa.mConstrainedZ = true;
221 aa.initLOD(0);
222 return aa;
223 }
Jason Sams49a05d72010-12-29 14:31:29 -0800224
225 static public AllocationAdapter create2D(RenderScript rs, Allocation a) {
Jason Samsba862d12011-07-07 15:24:42 -0700226 android.util.Log.e("rs", "create2d " + a);
Jason Sams49a05d72010-12-29 14:31:29 -0800227 rs.validate();
228 AllocationAdapter aa = new AllocationAdapter(0, rs, a);
Jason Samsee2d8092011-06-21 16:42:42 -0700229 aa.mConstrainedLOD = true;
230 aa.mConstrainedFace = true;
231 aa.mConstrainedY = false;
232 aa.mConstrainedZ = true;
233 aa.initLOD(0);
Jason Sams49a05d72010-12-29 14:31:29 -0800234 return aa;
235 }
236
237
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700238 /**
Jason Samsba862d12011-07-07 15:24:42 -0700239 * Override the Allocation resize. Resizing adapters is not
240 * allowed and will throw a RSInvalidStateException.
241 *
242 * @param dimX ignored.
243 */
244 public synchronized void resize(int dimX) {
245 throw new RSInvalidStateException("Resize not allowed for Adapters.");
246 }
247
Jason Sams49a05d72010-12-29 14:31:29 -0800248}
249
250