blob: b77d087a01aad03a0b348ed0d7398628ba2fa2c5 [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/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070025 * Only intended for use by generated reflected code.
Jason Sams49a05d72010-12-29 14:31:29 -080026 *
27 **/
28public class AllocationAdapter extends Allocation {
Tim Murray7a629fa2013-11-19 12:45:54 -080029 AllocationAdapter(long id, RenderScript rs, Allocation alloc) {
Jason Samsba862d12011-07-07 15:24:42 -070030 super(id, rs, alloc.mType, alloc.mUsage);
31 mAdaptedAllocation = alloc;
Jason Sams49a05d72010-12-29 14:31:29 -080032 }
33
Tim Murray7a629fa2013-11-19 12:45:54 -080034 long getID(RenderScript rs) {
Jason Sams48fe5342011-07-08 13:52:30 -070035 throw new RSInvalidStateException(
36 "This operation is not supported with adapters at this time.");
Jason Samsee2d8092011-06-21 16:42:42 -070037 }
38
Jason Samsba862d12011-07-07 15:24:42 -070039 /**
40 * @hide
41 */
Jason Sams49a05d72010-12-29 14:31:29 -080042 public void subData(int xoff, FieldPacker fp) {
Jason Samsba862d12011-07-07 15:24:42 -070043 super.setFromFieldPacker(xoff, fp);
Jason Sams49a05d72010-12-29 14:31:29 -080044 }
Jason Samsba862d12011-07-07 15:24:42 -070045 /**
46 * @hide
47 */
Jason Sams49a05d72010-12-29 14:31:29 -080048 public void subElementData(int xoff, int component_number, FieldPacker fp) {
Jason Samsba862d12011-07-07 15:24:42 -070049 super.setFromFieldPacker(xoff, component_number, fp);
Jason Sams49a05d72010-12-29 14:31:29 -080050 }
Jason Samsba862d12011-07-07 15:24:42 -070051 /**
52 * @hide
53 */
Jason Sams49a05d72010-12-29 14:31:29 -080054 public void subData1D(int off, int count, int[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070055 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080056 }
Jason Samsba862d12011-07-07 15:24:42 -070057 /**
58 * @hide
59 */
Jason Sams49a05d72010-12-29 14:31:29 -080060 public void subData1D(int off, int count, short[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070061 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080062 }
Jason Samsba862d12011-07-07 15:24:42 -070063 /**
64 * @hide
65 */
Jason Sams49a05d72010-12-29 14:31:29 -080066 public void subData1D(int off, int count, byte[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070067 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080068 }
Jason Samsba862d12011-07-07 15:24:42 -070069 /**
70 * @hide
71 */
Jason Sams49a05d72010-12-29 14:31:29 -080072 public void subData1D(int off, int count, float[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070073 super.copy1DRangeFrom(off, count, d);
Jason Sams49a05d72010-12-29 14:31:29 -080074 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070075 /**
Jason Samsba862d12011-07-07 15:24:42 -070076 * @hide
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070077 */
Jason Sams49a05d72010-12-29 14:31:29 -080078 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070079 super.copy2DRangeFrom(xoff, yoff, w, h, d);
Jason Sams49a05d72010-12-29 14:31:29 -080080 }
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070081 /**
Jason Samsba862d12011-07-07 15:24:42 -070082 * @hide
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -070083 */
Jason Samsba862d12011-07-07 15:24:42 -070084 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
85 super.copy2DRangeFrom(xoff, yoff, w, h, d);
Jason Sams49a05d72010-12-29 14:31:29 -080086 }
Jason Samsba862d12011-07-07 15:24:42 -070087 /**
88 * @hide
89 */
Jason Sams49a05d72010-12-29 14:31:29 -080090 public void readData(int[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070091 super.copyTo(d);
Jason Sams49a05d72010-12-29 14:31:29 -080092 }
Jason Samsba862d12011-07-07 15:24:42 -070093 /**
94 * @hide
95 */
Jason Sams49a05d72010-12-29 14:31:29 -080096 public void readData(float[] d) {
Jason Samsba862d12011-07-07 15:24:42 -070097 super.copyTo(d);
Jason Sams49a05d72010-12-29 14:31:29 -080098 }
99
Jason Samsba862d12011-07-07 15:24:42 -0700100 void initLOD(int lod) {
Jason Samsee2d8092011-06-21 16:42:42 -0700101 if (lod < 0) {
102 throw new RSIllegalArgumentException("Attempting to set negative lod (" + lod + ").");
103 }
104
Jason Samsba862d12011-07-07 15:24:42 -0700105 int tx = mAdaptedAllocation.mType.getX();
106 int ty = mAdaptedAllocation.mType.getY();
107 int tz = mAdaptedAllocation.mType.getZ();
Jason Samsee2d8092011-06-21 16:42:42 -0700108
109 for (int ct=0; ct < lod; ct++) {
110 if ((tx==1) && (ty == 1) && (tz == 1)) {
111 throw new RSIllegalArgumentException("Attempting to set lod (" + lod + ") out of range.");
112 }
113
114 if (tx > 1) tx >>= 1;
115 if (ty > 1) ty >>= 1;
116 if (tz > 1) tz >>= 1;
117 }
118
Jason Samsba862d12011-07-07 15:24:42 -0700119 mCurrentDimX = tx;
120 mCurrentDimY = ty;
121 mCurrentDimZ = tz;
122 mCurrentCount = mCurrentDimX;
123 if (mCurrentDimY > 1) {
124 mCurrentCount *= mCurrentDimY;
Jason Samsee2d8092011-06-21 16:42:42 -0700125 }
Jason Samsba862d12011-07-07 15:24:42 -0700126 if (mCurrentDimZ > 1) {
127 mCurrentCount *= mCurrentDimZ;
Jason Samsee2d8092011-06-21 16:42:42 -0700128 }
Jason Samsba862d12011-07-07 15:24:42 -0700129 mSelectedY = 0;
130 mSelectedZ = 0;
Jason Samsee2d8092011-06-21 16:42:42 -0700131 }
132
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700133 /**
Jason Samsee2d8092011-06-21 16:42:42 -0700134 * Set the active LOD. The LOD must be within the range for the
Jason Samsba862d12011-07-07 15:24:42 -0700135 * type being adapted. The base allocation must have mipmaps.
136 *
137 * Because this changes the dimensions of the adapter the
138 * current Y and Z will be reset.
Jason Samsee2d8092011-06-21 16:42:42 -0700139 *
140 * @param lod The LOD to make active.
141 */
Jason Sams49a05d72010-12-29 14:31:29 -0800142 public void setLOD(int lod) {
Jason Samsba862d12011-07-07 15:24:42 -0700143 if (!mAdaptedAllocation.getType().hasMipmaps()) {
Jason Samsee2d8092011-06-21 16:42:42 -0700144 throw new RSInvalidStateException("Cannot set LOD when the allocation type does not include mipmaps.");
145 }
146 if (!mConstrainedLOD) {
147 throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
148 }
149
150 initLOD(lod);
Jason Sams49a05d72010-12-29 14:31:29 -0800151 }
152
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700153 /**
Jason Samsba862d12011-07-07 15:24:42 -0700154 * Set the active Face. The base allocation must be of a type
155 * that includes faces.
156 *
157 * @param cf The face to make active.
158 */
Jason Sams49a05d72010-12-29 14:31:29 -0800159 public void setFace(Type.CubemapFace cf) {
Jason Samsba862d12011-07-07 15:24:42 -0700160 if (!mAdaptedAllocation.getType().hasFaces()) {
161 throw new RSInvalidStateException("Cannot set Face when the allocation type does not include faces.");
162 }
163 if (!mConstrainedFace) {
164 throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
165 }
166 if (cf == null) {
167 throw new RSIllegalArgumentException("Cannot set null face.");
168 }
169
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700170 mSelectedFace = cf;
Jason Sams49a05d72010-12-29 14:31:29 -0800171 }
172
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700173 /**
Jason Samsba862d12011-07-07 15:24:42 -0700174 * Set the active Y. The y value must be within the range for
175 * the allocation being adapted. The base allocation must
176 * contain the Y dimension.
177 *
178 * @param y The y to make active.
179 */
Jason Sams49a05d72010-12-29 14:31:29 -0800180 public void setY(int y) {
Jason Samsba862d12011-07-07 15:24:42 -0700181 if (mAdaptedAllocation.getType().getY() == 0) {
182 throw new RSInvalidStateException("Cannot set Y when the allocation type does not include Y dim.");
183 }
184 if (mAdaptedAllocation.getType().getY() <= y) {
185 throw new RSInvalidStateException("Cannot set Y greater than dimension of allocation.");
186 }
187 if (!mConstrainedY) {
188 throw new RSInvalidStateException("Cannot set Y when the adapter includes Y.");
189 }
190
191 mSelectedY = y;
Jason Sams49a05d72010-12-29 14:31:29 -0800192 }
193
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700194 /**
Jason Samsba862d12011-07-07 15:24:42 -0700195 * Set the active Z. The z value must be within the range for
196 * the allocation being adapted. The base allocation must
197 * contain the Z dimension.
198 *
199 * @param z The z to make active.
200 */
Jason Sams49a05d72010-12-29 14:31:29 -0800201 public void setZ(int z) {
Jason Samsba862d12011-07-07 15:24:42 -0700202 if (mAdaptedAllocation.getType().getZ() == 0) {
203 throw new RSInvalidStateException("Cannot set Z when the allocation type does not include Z dim.");
204 }
205 if (mAdaptedAllocation.getType().getZ() <= z) {
206 throw new RSInvalidStateException("Cannot set Z greater than dimension of allocation.");
207 }
208 if (!mConstrainedZ) {
209 throw new RSInvalidStateException("Cannot set Z when the adapter includes Z.");
210 }
211
212 mSelectedZ = z;
Jason Sams49a05d72010-12-29 14:31:29 -0800213 }
214
Jason Samsba862d12011-07-07 15:24:42 -0700215 static public AllocationAdapter create1D(RenderScript rs, Allocation a) {
216 rs.validate();
217 AllocationAdapter aa = new AllocationAdapter(0, rs, a);
218 aa.mConstrainedLOD = true;
219 aa.mConstrainedFace = true;
220 aa.mConstrainedY = true;
221 aa.mConstrainedZ = true;
222 aa.initLOD(0);
223 return aa;
224 }
Jason Sams49a05d72010-12-29 14:31:29 -0800225
226 static public AllocationAdapter create2D(RenderScript rs, Allocation a) {
Jason Samsba862d12011-07-07 15:24:42 -0700227 android.util.Log.e("rs", "create2d " + a);
Jason Sams49a05d72010-12-29 14:31:29 -0800228 rs.validate();
229 AllocationAdapter aa = new AllocationAdapter(0, rs, a);
Jason Samsee2d8092011-06-21 16:42:42 -0700230 aa.mConstrainedLOD = true;
231 aa.mConstrainedFace = true;
232 aa.mConstrainedY = false;
233 aa.mConstrainedZ = true;
234 aa.initLOD(0);
Jason Sams49a05d72010-12-29 14:31:29 -0800235 return aa;
236 }
237
238
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700239 /**
Jason Samsba862d12011-07-07 15:24:42 -0700240 * Override the Allocation resize. Resizing adapters is not
241 * allowed and will throw a RSInvalidStateException.
242 *
243 * @param dimX ignored.
244 */
245 public synchronized void resize(int dimX) {
246 throw new RSInvalidStateException("Resize not allowed for Adapters.");
247 }
248
Jason Sams49a05d72010-12-29 14:31:29 -0800249}
250
251