blob: 17bc23421894c11fdadaa8da2c198b182b0ca12a [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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070019/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070020 * Only intended for use by generated reflected code.
Jason Sams49a05d72010-12-29 14:31:29 -080021 *
Xusong Wang8b4548c2021-01-05 10:09:52 -080022 * @deprecated Renderscript has been deprecated in API level 31. Please refer to the <a
23 * href="https://developer.android.com/guide/topics/renderscript/migration-guide">migration
24 * guide</a> for the proposed alternatives.
Jason Sams49a05d72010-12-29 14:31:29 -080025 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080026@Deprecated
Jason Sams49a05d72010-12-29 14:31:29 -080027public class AllocationAdapter extends Allocation {
Jason Sams46ba27e32015-02-06 17:45:15 -080028 Type mWindow;
29
30 AllocationAdapter(long id, RenderScript rs, Allocation alloc, Type t) {
Jason Samsba862d12011-07-07 15:24:42 -070031 super(id, rs, alloc.mType, alloc.mUsage);
32 mAdaptedAllocation = alloc;
Jason Sams46ba27e32015-02-06 17:45:15 -080033 mWindow = t;
Jason Sams49a05d72010-12-29 14:31:29 -080034 }
35
Jason Sams46ba27e32015-02-06 17:45:15 -080036 /*
Tim Murray460a0492013-11-19 12:45:54 -080037 long getID(RenderScript rs) {
Jason Sams48fe5342011-07-08 13:52:30 -070038 throw new RSInvalidStateException(
39 "This operation is not supported with adapters at this time.");
Jason Samsee2d8092011-06-21 16:42:42 -070040 }
Jason Sams46ba27e32015-02-06 17:45:15 -080041 */
Jason Sams49a05d72010-12-29 14:31:29 -080042
Jason Samsba862d12011-07-07 15:24:42 -070043 void initLOD(int lod) {
Jason Samsee2d8092011-06-21 16:42:42 -070044 if (lod < 0) {
45 throw new RSIllegalArgumentException("Attempting to set negative lod (" + lod + ").");
46 }
47
Jason Samsba862d12011-07-07 15:24:42 -070048 int tx = mAdaptedAllocation.mType.getX();
49 int ty = mAdaptedAllocation.mType.getY();
50 int tz = mAdaptedAllocation.mType.getZ();
Jason Samsee2d8092011-06-21 16:42:42 -070051
52 for (int ct=0; ct < lod; ct++) {
53 if ((tx==1) && (ty == 1) && (tz == 1)) {
54 throw new RSIllegalArgumentException("Attempting to set lod (" + lod + ") out of range.");
55 }
56
57 if (tx > 1) tx >>= 1;
58 if (ty > 1) ty >>= 1;
59 if (tz > 1) tz >>= 1;
60 }
61
Jason Samsba862d12011-07-07 15:24:42 -070062 mCurrentDimX = tx;
63 mCurrentDimY = ty;
64 mCurrentDimZ = tz;
65 mCurrentCount = mCurrentDimX;
66 if (mCurrentDimY > 1) {
67 mCurrentCount *= mCurrentDimY;
Jason Samsee2d8092011-06-21 16:42:42 -070068 }
Jason Samsba862d12011-07-07 15:24:42 -070069 if (mCurrentDimZ > 1) {
70 mCurrentCount *= mCurrentDimZ;
Jason Samsee2d8092011-06-21 16:42:42 -070071 }
Jason Samsba862d12011-07-07 15:24:42 -070072 mSelectedY = 0;
73 mSelectedZ = 0;
Jason Samsee2d8092011-06-21 16:42:42 -070074 }
75
Jason Sams46ba27e32015-02-06 17:45:15 -080076 private void updateOffsets() {
77 int a1 = 0, a2 = 0, a3 = 0, a4 = 0;
78
Jason Samsadd04be2015-02-25 16:42:00 -080079 if (mSelectedArray != null) {
80 if (mSelectedArray.length > 0) {
81 a1 = mSelectedArray[0];
82 }
83 if (mSelectedArray.length > 1) {
84 a2 = mSelectedArray[2];
85 }
86 if (mSelectedArray.length > 2) {
87 a3 = mSelectedArray[2];
88 }
89 if (mSelectedArray.length > 3) {
90 a4 = mSelectedArray[3];
91 }
Jason Sams46ba27e32015-02-06 17:45:15 -080092 }
93 mRS.nAllocationAdapterOffset(getID(mRS), mSelectedX, mSelectedY, mSelectedZ,
94 mSelectedLOD, mSelectedFace.mID, a1, a2, a3, a4);
95
96 }
97
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070098 /**
Jason Samsee2d8092011-06-21 16:42:42 -070099 * Set the active LOD. The LOD must be within the range for the
Jason Samsba862d12011-07-07 15:24:42 -0700100 * type being adapted. The base allocation must have mipmaps.
101 *
102 * Because this changes the dimensions of the adapter the
103 * current Y and Z will be reset.
Jason Samsee2d8092011-06-21 16:42:42 -0700104 *
105 * @param lod The LOD to make active.
106 */
Jason Sams49a05d72010-12-29 14:31:29 -0800107 public void setLOD(int lod) {
Jason Samsba862d12011-07-07 15:24:42 -0700108 if (!mAdaptedAllocation.getType().hasMipmaps()) {
Jason Samsee2d8092011-06-21 16:42:42 -0700109 throw new RSInvalidStateException("Cannot set LOD when the allocation type does not include mipmaps.");
110 }
Jason Sams46ba27e32015-02-06 17:45:15 -0800111 if (mWindow.hasMipmaps()) {
Jason Samsee2d8092011-06-21 16:42:42 -0700112 throw new RSInvalidStateException("Cannot set LOD when the adapter includes mipmaps.");
113 }
114
115 initLOD(lod);
Jason Sams46ba27e32015-02-06 17:45:15 -0800116 mSelectedLOD = lod;
117 updateOffsets();
Jason Sams49a05d72010-12-29 14:31:29 -0800118 }
119
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700120 /**
Jason Samsba862d12011-07-07 15:24:42 -0700121 * Set the active Face. The base allocation must be of a type
122 * that includes faces.
123 *
124 * @param cf The face to make active.
125 */
Jason Sams49a05d72010-12-29 14:31:29 -0800126 public void setFace(Type.CubemapFace cf) {
Jason Samsba862d12011-07-07 15:24:42 -0700127 if (!mAdaptedAllocation.getType().hasFaces()) {
128 throw new RSInvalidStateException("Cannot set Face when the allocation type does not include faces.");
129 }
Jason Sams46ba27e32015-02-06 17:45:15 -0800130 if (mWindow.hasFaces()) {
131 throw new RSInvalidStateException("Cannot set face when the adapter includes faces.");
Jason Samsba862d12011-07-07 15:24:42 -0700132 }
133 if (cf == null) {
134 throw new RSIllegalArgumentException("Cannot set null face.");
135 }
136
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700137 mSelectedFace = cf;
Jason Sams46ba27e32015-02-06 17:45:15 -0800138 updateOffsets();
139 }
140
141
142 /**
Jason Sams20666672015-03-20 15:08:45 -0700143 *
Jason Sams46ba27e32015-02-06 17:45:15 -0800144 * Set the active X. The x value must be within the range for
145 * the allocation being adapted.
146 *
147 * @param x The x to make active.
148 */
149 public void setX(int x) {
150 if (mAdaptedAllocation.getType().getX() <= x) {
151 throw new RSInvalidStateException("Cannot set X greater than dimension of allocation.");
152 }
153 if (mWindow.getX() == mAdaptedAllocation.getType().getX()) {
154 throw new RSInvalidStateException("Cannot set X when the adapter includes X.");
155 }
156 if ((mWindow.getX() + x) >= mAdaptedAllocation.getType().getX()) {
157 throw new RSInvalidStateException("Cannot set (X + window) which would be larger than dimension of allocation.");
158 }
159
160 mSelectedX = x;
161 updateOffsets();
Jason Sams49a05d72010-12-29 14:31:29 -0800162 }
163
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700164 /**
Jason Samsba862d12011-07-07 15:24:42 -0700165 * Set the active Y. The y value must be within the range for
166 * the allocation being adapted. The base allocation must
167 * contain the Y dimension.
168 *
169 * @param y The y to make active.
170 */
Jason Sams49a05d72010-12-29 14:31:29 -0800171 public void setY(int y) {
Jason Samsba862d12011-07-07 15:24:42 -0700172 if (mAdaptedAllocation.getType().getY() == 0) {
173 throw new RSInvalidStateException("Cannot set Y when the allocation type does not include Y dim.");
174 }
175 if (mAdaptedAllocation.getType().getY() <= y) {
176 throw new RSInvalidStateException("Cannot set Y greater than dimension of allocation.");
177 }
Jason Sams46ba27e32015-02-06 17:45:15 -0800178 if (mWindow.getY() == mAdaptedAllocation.getType().getY()) {
Jason Samsba862d12011-07-07 15:24:42 -0700179 throw new RSInvalidStateException("Cannot set Y when the adapter includes Y.");
180 }
Jason Sams46ba27e32015-02-06 17:45:15 -0800181 if ((mWindow.getY() + y) >= mAdaptedAllocation.getType().getY()) {
182 throw new RSInvalidStateException("Cannot set (Y + window) which would be larger than dimension of allocation.");
183 }
Jason Samsba862d12011-07-07 15:24:42 -0700184
185 mSelectedY = y;
Jason Sams46ba27e32015-02-06 17:45:15 -0800186 updateOffsets();
Jason Sams49a05d72010-12-29 14:31:29 -0800187 }
188
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700189 /**
Jason Samsba862d12011-07-07 15:24:42 -0700190 * Set the active Z. The z value must be within the range for
191 * the allocation being adapted. The base allocation must
192 * contain the Z dimension.
193 *
194 * @param z The z to make active.
195 */
Jason Sams49a05d72010-12-29 14:31:29 -0800196 public void setZ(int z) {
Jason Samsba862d12011-07-07 15:24:42 -0700197 if (mAdaptedAllocation.getType().getZ() == 0) {
198 throw new RSInvalidStateException("Cannot set Z when the allocation type does not include Z dim.");
199 }
200 if (mAdaptedAllocation.getType().getZ() <= z) {
201 throw new RSInvalidStateException("Cannot set Z greater than dimension of allocation.");
202 }
Jason Sams46ba27e32015-02-06 17:45:15 -0800203 if (mWindow.getZ() == mAdaptedAllocation.getType().getZ()) {
Jason Samsba862d12011-07-07 15:24:42 -0700204 throw new RSInvalidStateException("Cannot set Z when the adapter includes Z.");
205 }
Jason Sams46ba27e32015-02-06 17:45:15 -0800206 if ((mWindow.getZ() + z) >= mAdaptedAllocation.getType().getZ()) {
207 throw new RSInvalidStateException("Cannot set (Z + window) which would be larger than dimension of allocation.");
208 }
Jason Samsba862d12011-07-07 15:24:42 -0700209
210 mSelectedZ = z;
Jason Sams46ba27e32015-02-06 17:45:15 -0800211 updateOffsets();
212 }
213
214 /**
Jason Samsd0162662015-04-15 17:18:10 -0700215 * @hide
Jason Sams46ba27e32015-02-06 17:45:15 -0800216 */
217 public void setArray(int arrayNum, int arrayVal) {
218 if (mAdaptedAllocation.getType().getArray(arrayNum) == 0) {
219 throw new RSInvalidStateException("Cannot set arrayNum when the allocation type does not include arrayNum dim.");
220 }
221 if (mAdaptedAllocation.getType().getArray(arrayNum) <= arrayVal) {
222 throw new RSInvalidStateException("Cannot set arrayNum greater than dimension of allocation.");
223 }
224 if (mWindow.getArray(arrayNum) == mAdaptedAllocation.getType().getArray(arrayNum)) {
225 throw new RSInvalidStateException("Cannot set arrayNum when the adapter includes arrayNum.");
226 }
227 if ((mWindow.getArray(arrayNum) + arrayVal) >= mAdaptedAllocation.getType().getArray(arrayNum)) {
228 throw new RSInvalidStateException("Cannot set (arrayNum + window) which would be larger than dimension of allocation.");
229 }
230
231 mSelectedArray[arrayNum] = arrayVal;
232 updateOffsets();
Jason Sams49a05d72010-12-29 14:31:29 -0800233 }
234
Jason Samsba862d12011-07-07 15:24:42 -0700235 static public AllocationAdapter create1D(RenderScript rs, Allocation a) {
236 rs.validate();
Jason Sams46ba27e32015-02-06 17:45:15 -0800237 Type t = Type.createX(rs, a.getElement(), a.getType().getX());
238 return createTyped(rs, a, t);
Jason Samsba862d12011-07-07 15:24:42 -0700239 }
Jason Sams49a05d72010-12-29 14:31:29 -0800240
Jason Sams46ba27e32015-02-06 17:45:15 -0800241
Jason Sams49a05d72010-12-29 14:31:29 -0800242 static public AllocationAdapter create2D(RenderScript rs, Allocation a) {
243 rs.validate();
Jason Sams46ba27e32015-02-06 17:45:15 -0800244 Type t = Type.createXY(rs, a.getElement(), a.getType().getX(), a.getType().getY());
245 return createTyped(rs, a, t);
Jason Sams49a05d72010-12-29 14:31:29 -0800246 }
247
Jason Sams46ba27e32015-02-06 17:45:15 -0800248 /**
Jason Sams20666672015-03-20 15:08:45 -0700249 *
Jason Sams46ba27e32015-02-06 17:45:15 -0800250 *
Pirama Arumuga Nainar115b4112015-10-07 19:59:48 -0700251 * Create an arbitrary window into the base allocation.
Jason Sams46ba27e32015-02-06 17:45:15 -0800252 * The type describes the shape of the window.
253 *
254 * Any dimensions present in the type must be equal or smaller
255 * to the dimensions in the source allocation. A dimension
256 * present in the allocation that is not present in the type
Pirama Arumuga Nainar115b4112015-10-07 19:59:48 -0700257 * will be constrained away with the selectors.
Jason Sams46ba27e32015-02-06 17:45:15 -0800258 *
Pirama Arumuga Nainar115b4112015-10-07 19:59:48 -0700259 * If a dimension is present in both the type and allocation, one of
260 * two things will happen.
Jason Sams46ba27e32015-02-06 17:45:15 -0800261 *
Pirama Arumuga Nainar115b4112015-10-07 19:59:48 -0700262 * If the type is smaller than the allocation, a window will be
Jason Sams46ba27e32015-02-06 17:45:15 -0800263 * created, the selected value in the adapter for that dimension
Pirama Arumuga Nainar115b4112015-10-07 19:59:48 -0700264 * will act as the base address, and the type will describe the
Jason Sams46ba27e32015-02-06 17:45:15 -0800265 * size of the view starting at that point.
266 *
Pirama Arumuga Nainar115b4112015-10-07 19:59:48 -0700267 * If the type and allocation dimension are of the same size,
Jason Sams46ba27e32015-02-06 17:45:15 -0800268 * then setting the selector for the dimension will be an error.
269 */
270 static public AllocationAdapter createTyped(RenderScript rs, Allocation a, Type t) {
271 rs.validate();
272
273 if (a.mAdaptedAllocation != null) {
274 throw new RSInvalidStateException("Adapters cannot be nested.");
275 }
276
277 if (!a.getType().getElement().equals(t.getElement())) {
278 throw new RSInvalidStateException("Element must match Allocation type.");
279 }
280
281 if (t.hasFaces() || t.hasMipmaps()) {
282 throw new RSInvalidStateException("Adapters do not support window types with Mipmaps or Faces.");
283 }
284
285 Type at = a.getType();
286 if ((t.getX() > at.getX()) ||
287 (t.getY() > at.getY()) ||
288 (t.getZ() > at.getZ()) ||
289 (t.getArrayCount() > at.getArrayCount())) {
290
291 throw new RSInvalidStateException("Type cannot have dimension larger than the source allocation.");
292 }
293
294 if (t.getArrayCount() > 0) {
295 for (int i = 0; i < t.getArray(i); i++) {
296 if (t.getArray(i) > at.getArray(i)) {
297 throw new RSInvalidStateException("Type cannot have dimension larger than the source allocation.");
298 }
299 }
300 }
301
302 // Create the object
303 long id = rs.nAllocationAdapterCreate(a.getID(rs), t.getID(rs));
304 if (id == 0) {
305 throw new RSRuntimeException("AllocationAdapter creation failed.");
306 }
307 return new AllocationAdapter(id, rs, a, t);
308 }
Jason Sams49a05d72010-12-29 14:31:29 -0800309
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700310 /**
Jason Samsba862d12011-07-07 15:24:42 -0700311 * Override the Allocation resize. Resizing adapters is not
312 * allowed and will throw a RSInvalidStateException.
313 *
314 * @param dimX ignored.
315 */
316 public synchronized void resize(int dimX) {
317 throw new RSInvalidStateException("Resize not allowed for Adapters.");
318 }
319
Jason Sams49a05d72010-12-29 14:31:29 -0800320}
321
322