blob: a58e42cd97123b49a0cfc8fe96660557d4313fe3 [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
Jason Samsdd6c8b32013-02-15 17:27:24 -08002 * Copyright (C) 2013 The Android Open Source Project
Jason Samsb8c5a842009-07-31 20:40:47 -07003 *
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 * <p>A Type describes the {@link android.renderscript.Element} and dimensions used for an {@link
21 * android.renderscript.Allocation} or a parallel operation. Types are created through {@link
22 * android.renderscript.Type.Builder}.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080023 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070024 * <p>A Type always includes an {@link android.renderscript.Element} and an X
25 * dimension. A Type may be multidimensional, up to three dimensions. A nonzero
26 * value in the Y or Z dimensions indicates that the dimension is present. Note
27 * that a Type with only a given X dimension and a Type with the same X
28 * dimension but Y = 1 are not equivalent.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080029 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070030 * <p>A Type also supports inclusion of level of detail (LOD) or cube map
31 * faces. LOD and cube map faces are booleans to indicate present or not
32 * present. </p>
33 *
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -070034 * <p>A Type also supports YUV format information to support an
35 * {@link android.renderscript.Allocation} in a YUV format. The YUV formats
36 * supported are {@link android.graphics.ImageFormat#YV12},
37 * {@link android.graphics.ImageFormat#NV21}, and
38 * {@link android.graphics.ImageFormat#YUV_420_888}</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080039 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080040 * <div class="special reference">
41 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070042 * <p>For more information about creating an application that uses RenderScript, read the
43 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080044 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070045 **/
46public class Type extends BaseObj {
Jason Sams768bc022009-09-21 19:41:04 -070047 int mDimX;
48 int mDimY;
49 int mDimZ;
Jason Samsbf6ef8d2010-12-06 15:59:59 -080050 boolean mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -070051 boolean mDimFaces;
Jason Sams8140d7b2012-12-13 17:01:09 -080052 int mDimYuv;
Jason Sams768bc022009-09-21 19:41:04 -070053 int mElementCount;
Jason Sams1bada8c2009-08-09 17:01:55 -070054 Element mElement;
Jason Sams46ba27e32015-02-06 17:45:15 -080055 int mArrays[];
56
57 static final int mMaxArrays = 4;
Jason Sams768bc022009-09-21 19:41:04 -070058
Jason Sams49a05d72010-12-29 14:31:29 -080059 public enum CubemapFace {
Stephen Hines20fbd012011-06-16 17:44:53 -070060 POSITIVE_X (0),
Jason Sams49a05d72010-12-29 14:31:29 -080061 NEGATIVE_X (1),
Stephen Hines20fbd012011-06-16 17:44:53 -070062 POSITIVE_Y (2),
Jason Sams49a05d72010-12-29 14:31:29 -080063 NEGATIVE_Y (3),
Stephen Hines20fbd012011-06-16 17:44:53 -070064 POSITIVE_Z (4),
65 NEGATIVE_Z (5),
66 @Deprecated
67 POSITVE_X (0),
68 @Deprecated
69 POSITVE_Y (2),
70 @Deprecated
71 POSITVE_Z (4);
Jason Sams49a05d72010-12-29 14:31:29 -080072
73 int mID;
74 CubemapFace(int id) {
75 mID = id;
76 }
77 }
78
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070079 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080080 * Return the element associated with this Type.
81 *
82 * @return Element
83 */
Jason Samse17964e2010-01-04 16:52:27 -080084 public Element getElement() {
85 return mElement;
86 }
Jason Sams1bada8c2009-08-09 17:01:55 -070087
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070088 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080089 * Return the value of the X dimension.
90 *
91 * @return int
92 */
Jason Sams768bc022009-09-21 19:41:04 -070093 public int getX() {
94 return mDimX;
95 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080096
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070097 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080098 * Return the value of the Y dimension or 0 for a 1D allocation.
99 *
100 * @return int
101 */
Jason Sams768bc022009-09-21 19:41:04 -0700102 public int getY() {
103 return mDimY;
104 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800105
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700106 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800107 * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
108 *
109 * @return int
110 */
Jason Sams768bc022009-09-21 19:41:04 -0700111 public int getZ() {
112 return mDimZ;
113 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800114
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700115 /**
Jason Sams5a722cf2013-03-26 13:27:37 -0700116 * Get the YUV format
117 *
Jason Sams02d56d92013-04-12 16:40:50 -0700118 *
Jason Sams5a722cf2013-03-26 13:27:37 -0700119 * @return int
120 */
121 public int getYuv() {
122 return mDimYuv;
123 }
124
125 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800126 * Return if the Type has a mipmap chain.
127 *
128 * @return boolean
129 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800130 public boolean hasMipmaps() {
131 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700132 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800133
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700134 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800135 * Return if the Type is a cube map.
136 *
137 * @return boolean
138 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800139 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700140 return mDimFaces;
141 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800142
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700143 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800144 * Return the total number of accessable cells in the Type.
145 *
146 * @return int
147 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800148 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700149 return mElementCount;
150 }
151
Jason Sams46ba27e32015-02-06 17:45:15 -0800152 /**
153 * @hide
154 */
155 public int getArray(int dim) {
156 if ((dim < 0) || (dim >= mMaxArrays)) {
157 throw new RSIllegalArgumentException("Array dimension out of range.");
158 }
159
160 if (mArrays == null || dim >= mArrays.length) {
161 // Dimension in range but no array for that dimension allocated
162 return 0;
163 }
164
165 return mArrays[dim];
166 }
167
168 /**
169 * @hide
170 */
171 public int getArrayCount() {
172 if (mArrays != null) return mArrays.length;
173 return 0;
174 }
175
Jason Sams768bc022009-09-21 19:41:04 -0700176 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800177 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700178 int x = getX();
179 int y = getY();
180 int z = getZ();
181 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800182 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700183 faces = 6;
184 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800185 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700186 x = 1;
187 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800188 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700189 y = 1;
190 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800191 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700192 z = 1;
193 }
194
195 int count = x * y * z * faces;
Alex Sakhartchouk9ea30a62011-03-02 12:33:50 -0800196
197 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
Jason Sams768bc022009-09-21 19:41:04 -0700198 if(x > 1) {
199 x >>= 1;
200 }
201 if(y > 1) {
202 y >>= 1;
203 }
204 if(z > 1) {
205 z >>= 1;
206 }
207
208 count += x * y * z * faces;
209 }
Jason Sams46ba27e32015-02-06 17:45:15 -0800210
211 if (mArrays != null) {
212 for (int ct = 0; ct < mArrays.length; ct++) {
213 count *= mArrays[ct];
214 }
215 }
216
Jason Sams768bc022009-09-21 19:41:04 -0700217 mElementCount = count;
218 }
219
220
Tim Murray460a0492013-11-19 12:45:54 -0800221 Type(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700222 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700223 }
224
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700225 @Override
226 void updateFromNative() {
Ashok Bhat98071552014-02-12 09:54:43 +0000227 // We have 6 integer/long to obtain mDimX; mDimY; mDimZ;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700228 // mDimLOD; mDimFaces; mElement;
Ashok Bhat98071552014-02-12 09:54:43 +0000229 long[] dataBuffer = new long[6];
230 mRS.nTypeGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700231
Ashok Bhat98071552014-02-12 09:54:43 +0000232 mDimX = (int)dataBuffer[0];
233 mDimY = (int)dataBuffer[1];
234 mDimZ = (int)dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800235 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700236 mDimFaces = dataBuffer[4] == 1 ? true : false;
237
Ashok Bhat98071552014-02-12 09:54:43 +0000238 long elementID = dataBuffer[5];
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700239 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700240 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700241 mElement.updateFromNative();
242 }
243 calcElementCount();
244 }
245
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700246 /**
Jason Samsec44e5d2013-10-09 17:15:36 -0700247 * Utility function for creating basic 1D types. The type is
248 * created without mipmaps enabled.
249 *
250 * @param rs The RenderScript context
251 * @param e The Element for the Type
252 * @param dimX The X dimension, must be > 0
253 *
254 * @return Type
255 */
256 static public Type createX(RenderScript rs, Element e, int dimX) {
257 if (dimX < 1) {
258 throw new RSInvalidStateException("Dimension must be >= 1.");
259 }
260
Tim Murray460a0492013-11-19 12:45:54 -0800261 long id = rs.nTypeCreate(e.getID(rs), dimX, 0, 0, false, false, 0);
Jason Samsec44e5d2013-10-09 17:15:36 -0700262 Type t = new Type(id, rs);
263 t.mElement = e;
264 t.mDimX = dimX;
265 t.calcElementCount();
266 return t;
267 }
268
269 /**
270 * Utility function for creating basic 2D types. The type is
271 * created without mipmaps or cubemaps.
272 *
273 * @param rs The RenderScript context
274 * @param e The Element for the Type
275 * @param dimX The X dimension, must be > 0
276 * @param dimY The Y dimension, must be > 0
277 *
278 * @return Type
279 */
280 static public Type createXY(RenderScript rs, Element e, int dimX, int dimY) {
281 if ((dimX < 1) || (dimY < 1)) {
282 throw new RSInvalidStateException("Dimension must be >= 1.");
283 }
284
Tim Murray460a0492013-11-19 12:45:54 -0800285 long id = rs.nTypeCreate(e.getID(rs), dimX, dimY, 0, false, false, 0);
Jason Samsec44e5d2013-10-09 17:15:36 -0700286 Type t = new Type(id, rs);
287 t.mElement = e;
288 t.mDimX = dimX;
289 t.mDimY = dimY;
290 t.calcElementCount();
291 return t;
292 }
293
294 /**
295 * Utility function for creating basic 3D types. The type is
296 * created without mipmaps.
297 *
298 * @param rs The RenderScript context
299 * @param e The Element for the Type
300 * @param dimX The X dimension, must be > 0
301 * @param dimY The Y dimension, must be > 0
302 * @param dimZ The Z dimension, must be > 0
303 *
304 * @return Type
305 */
306 static public Type createXYZ(RenderScript rs, Element e, int dimX, int dimY, int dimZ) {
307 if ((dimX < 1) || (dimY < 1) || (dimZ < 1)) {
308 throw new RSInvalidStateException("Dimension must be >= 1.");
309 }
310
Tim Murray460a0492013-11-19 12:45:54 -0800311 long id = rs.nTypeCreate(e.getID(rs), dimX, dimY, dimZ, false, false, 0);
Jason Samsec44e5d2013-10-09 17:15:36 -0700312 Type t = new Type(id, rs);
313 t.mElement = e;
314 t.mDimX = dimX;
315 t.mDimY = dimY;
316 t.mDimZ = dimZ;
317 t.calcElementCount();
318 return t;
319 }
320
321 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800322 * Builder class for Type.
323 *
324 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700325 public static class Builder {
326 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800327 int mDimX = 1;
328 int mDimY;
329 int mDimZ;
330 boolean mDimMipmaps;
331 boolean mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800332 int mYuv;
Jason Sams46ba27e32015-02-06 17:45:15 -0800333 int[] mArray = new int[mMaxArrays];
Jason Samsb8c5a842009-07-31 20:40:47 -0700334
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800335 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700336
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700337 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800338 * Create a new builder object.
339 *
340 * @param rs
341 * @param e The element for the type to be created.
342 */
Jason Sams22534172009-08-04 16:58:20 -0700343 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800344 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700345 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700346 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700347 }
348
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700349 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800350 * Add a dimension to the Type.
351 *
352 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800353 * @param value
354 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800355 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700356 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800357 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700358 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800359 mDimX = value;
360 return this;
Jason Sams22534172009-08-04 16:58:20 -0700361 }
362
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800363 public Builder setY(int value) {
364 if(value < 1) {
365 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
366 }
367 mDimY = value;
368 return this;
369 }
370
Jason Samsd1c306a2012-12-27 20:26:41 -0800371 public Builder setZ(int value) {
372 if(value < 1) {
373 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid.");
374 }
375 mDimZ = value;
376 return this;
377 }
378
Jason Sams46ba27e32015-02-06 17:45:15 -0800379 /**
380 * @hide
381 *
382 * @param dim
383 * @param value
384 *
385 * @return Builder
386 */
387 public Builder setArray(int dim, int value) {
388 if(dim < 0 || dim >= mMaxArrays) {
389 throw new RSIllegalArgumentException("Array dimension out of range.");
390 }
391 mArray[dim] = value;
392 return this;
393 }
394
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800395 public Builder setMipmaps(boolean value) {
396 mDimMipmaps = value;
397 return this;
398 }
399
400 public Builder setFaces(boolean value) {
401 mDimFaces = value;
402 return this;
403 }
404
Jason Samsb109cc72013-01-07 18:20:12 -0800405 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700406 * Set the YUV layout for a Type.
Jason Samsb109cc72013-01-07 18:20:12 -0800407 *
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -0700408 * @param yuvFormat {@link android.graphics.ImageFormat#YV12}, {@link android.graphics.ImageFormat#NV21}, or
409 * {@link android.graphics.ImageFormat#YUV_420_888}.
Jason Samsb109cc72013-01-07 18:20:12 -0800410 */
411 public Builder setYuvFormat(int yuvFormat) {
412 switch (yuvFormat) {
413 case android.graphics.ImageFormat.NV21:
414 case android.graphics.ImageFormat.YV12:
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -0700415 case android.graphics.ImageFormat.YUV_420_888:
Jason Samsb109cc72013-01-07 18:20:12 -0800416 break;
417
418 default:
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -0700419 throw new RSIllegalArgumentException(
420 "Only ImageFormat.NV21, .YV12, and .YUV_420_888 are supported..");
Jason Samsb109cc72013-01-07 18:20:12 -0800421 }
422
423 mYuv = yuvFormat;
424 return this;
425 }
426
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800427
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700428 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700429 * Validate structure and create a new Type.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800430 *
431 * @return Type
432 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700433 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800434 if (mDimZ > 0) {
435 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800436 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
437 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800438 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800439 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
440 }
441 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800442 if (mDimY > 0) {
443 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800444 throw new RSInvalidStateException("X dimension required when Y is present.");
445 }
446 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800447 if (mDimFaces) {
448 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800449 throw new RSInvalidStateException("Cube maps require 2D Types.");
450 }
451 }
452
Jason Samsb109cc72013-01-07 18:20:12 -0800453 if (mYuv != 0) {
454 if ((mDimZ != 0) || mDimFaces || mDimMipmaps) {
455 throw new RSInvalidStateException("YUV only supports basic 2D.");
456 }
457 }
458
Jason Sams46ba27e32015-02-06 17:45:15 -0800459 int[] arrays = null;
460 for (int ct = mMaxArrays - 1; ct >= 0; ct--) {
461 if (mArray[ct] != 0 && arrays == null) {
462 arrays = new int[ct];
463 }
464 if ((mArray[ct] == 0) && (arrays != null)) {
465 throw new RSInvalidStateException("Array dimensions must be contigous from 0.");
466 }
467 }
468
Tim Murray460a0492013-11-19 12:45:54 -0800469 long id = mRS.nTypeCreate(mElement.getID(mRS),
Jason Samsb109cc72013-01-07 18:20:12 -0800470 mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces, mYuv);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800471 Type t = new Type(id, mRS);
472 t.mElement = mElement;
473 t.mDimX = mDimX;
474 t.mDimY = mDimY;
475 t.mDimZ = mDimZ;
476 t.mDimMipmaps = mDimMipmaps;
477 t.mDimFaces = mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800478 t.mDimYuv = mYuv;
Jason Sams46ba27e32015-02-06 17:45:15 -0800479 t.mArrays = arrays;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800480
Jason Sams768bc022009-09-21 19:41:04 -0700481 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700482 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700483 }
484 }
485
486}