blob: 83bf4a57fa6f7f2024828de30c763c310b1a29d5 [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 Sams768bc022009-09-21 19:41:04 -070055
Jason Sams49a05d72010-12-29 14:31:29 -080056 public enum CubemapFace {
Stephen Hines20fbd012011-06-16 17:44:53 -070057 POSITIVE_X (0),
Jason Sams49a05d72010-12-29 14:31:29 -080058 NEGATIVE_X (1),
Stephen Hines20fbd012011-06-16 17:44:53 -070059 POSITIVE_Y (2),
Jason Sams49a05d72010-12-29 14:31:29 -080060 NEGATIVE_Y (3),
Stephen Hines20fbd012011-06-16 17:44:53 -070061 POSITIVE_Z (4),
62 NEGATIVE_Z (5),
63 @Deprecated
64 POSITVE_X (0),
65 @Deprecated
66 POSITVE_Y (2),
67 @Deprecated
68 POSITVE_Z (4);
Jason Sams49a05d72010-12-29 14:31:29 -080069
70 int mID;
71 CubemapFace(int id) {
72 mID = id;
73 }
74 }
75
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070076 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080077 * Return the element associated with this Type.
78 *
79 * @return Element
80 */
Jason Samse17964e2010-01-04 16:52:27 -080081 public Element getElement() {
82 return mElement;
83 }
Jason Sams1bada8c2009-08-09 17:01:55 -070084
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070085 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080086 * Return the value of the X dimension.
87 *
88 * @return int
89 */
Jason Sams768bc022009-09-21 19:41:04 -070090 public int getX() {
91 return mDimX;
92 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080093
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070094 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080095 * Return the value of the Y dimension or 0 for a 1D allocation.
96 *
97 * @return int
98 */
Jason Sams768bc022009-09-21 19:41:04 -070099 public int getY() {
100 return mDimY;
101 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800102
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700103 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800104 * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
105 *
106 * @return int
107 */
Jason Sams768bc022009-09-21 19:41:04 -0700108 public int getZ() {
109 return mDimZ;
110 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800111
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700112 /**
Jason Sams5a722cf2013-03-26 13:27:37 -0700113 * Get the YUV format
114 *
Jason Sams02d56d92013-04-12 16:40:50 -0700115 *
Jason Sams5a722cf2013-03-26 13:27:37 -0700116 * @return int
117 */
118 public int getYuv() {
119 return mDimYuv;
120 }
121
122 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800123 * Return if the Type has a mipmap chain.
124 *
125 * @return boolean
126 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800127 public boolean hasMipmaps() {
128 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700129 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800130
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700131 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800132 * Return if the Type is a cube map.
133 *
134 * @return boolean
135 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800136 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700137 return mDimFaces;
138 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800139
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700140 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800141 * Return the total number of accessable cells in the Type.
142 *
143 * @return int
144 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800145 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700146 return mElementCount;
147 }
148
149 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800150 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700151 int x = getX();
152 int y = getY();
153 int z = getZ();
154 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800155 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700156 faces = 6;
157 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800158 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700159 x = 1;
160 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800161 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700162 y = 1;
163 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800164 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700165 z = 1;
166 }
167
168 int count = x * y * z * faces;
Alex Sakhartchouk9ea30a62011-03-02 12:33:50 -0800169
170 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
Jason Sams768bc022009-09-21 19:41:04 -0700171 if(x > 1) {
172 x >>= 1;
173 }
174 if(y > 1) {
175 y >>= 1;
176 }
177 if(z > 1) {
178 z >>= 1;
179 }
180
181 count += x * y * z * faces;
182 }
183 mElementCount = count;
184 }
185
186
Tim Murray460a0492013-11-19 12:45:54 -0800187 Type(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700188 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700189 }
190
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700191 @Override
192 void updateFromNative() {
Tim Murray460a0492013-11-19 12:45:54 -0800193 // FIXME: rsaTypeGetNativeData needs 32-bit and 64-bit paths
194
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700195 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
196 // mDimLOD; mDimFaces; mElement;
197 int[] dataBuffer = new int[6];
Tim Murray460a0492013-11-19 12:45:54 -0800198 mRS.nTypeGetNativeData((int)getID(mRS), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700199
200 mDimX = dataBuffer[0];
201 mDimY = dataBuffer[1];
202 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800203 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700204 mDimFaces = dataBuffer[4] == 1 ? true : false;
205
206 int elementID = dataBuffer[5];
207 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700208 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700209 mElement.updateFromNative();
210 }
211 calcElementCount();
212 }
213
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700214 /**
Jason Samsec44e5d2013-10-09 17:15:36 -0700215 * Utility function for creating basic 1D types. The type is
216 * created without mipmaps enabled.
217 *
218 * @param rs The RenderScript context
219 * @param e The Element for the Type
220 * @param dimX The X dimension, must be > 0
221 *
222 * @return Type
223 */
224 static public Type createX(RenderScript rs, Element e, int dimX) {
225 if (dimX < 1) {
226 throw new RSInvalidStateException("Dimension must be >= 1.");
227 }
228
Tim Murray460a0492013-11-19 12:45:54 -0800229 long id = rs.nTypeCreate(e.getID(rs), dimX, 0, 0, false, false, 0);
Jason Samsec44e5d2013-10-09 17:15:36 -0700230 Type t = new Type(id, rs);
231 t.mElement = e;
232 t.mDimX = dimX;
233 t.calcElementCount();
234 return t;
235 }
236
237 /**
238 * Utility function for creating basic 2D types. The type is
239 * created without mipmaps or cubemaps.
240 *
241 * @param rs The RenderScript context
242 * @param e The Element for the Type
243 * @param dimX The X dimension, must be > 0
244 * @param dimY The Y dimension, must be > 0
245 *
246 * @return Type
247 */
248 static public Type createXY(RenderScript rs, Element e, int dimX, int dimY) {
249 if ((dimX < 1) || (dimY < 1)) {
250 throw new RSInvalidStateException("Dimension must be >= 1.");
251 }
252
Tim Murray460a0492013-11-19 12:45:54 -0800253 long id = rs.nTypeCreate(e.getID(rs), dimX, dimY, 0, false, false, 0);
Jason Samsec44e5d2013-10-09 17:15:36 -0700254 Type t = new Type(id, rs);
255 t.mElement = e;
256 t.mDimX = dimX;
257 t.mDimY = dimY;
258 t.calcElementCount();
259 return t;
260 }
261
262 /**
263 * Utility function for creating basic 3D types. The type is
264 * created without mipmaps.
265 *
266 * @param rs The RenderScript context
267 * @param e The Element for the Type
268 * @param dimX The X dimension, must be > 0
269 * @param dimY The Y dimension, must be > 0
270 * @param dimZ The Z dimension, must be > 0
271 *
272 * @return Type
273 */
274 static public Type createXYZ(RenderScript rs, Element e, int dimX, int dimY, int dimZ) {
275 if ((dimX < 1) || (dimY < 1) || (dimZ < 1)) {
276 throw new RSInvalidStateException("Dimension must be >= 1.");
277 }
278
Tim Murray460a0492013-11-19 12:45:54 -0800279 long id = rs.nTypeCreate(e.getID(rs), dimX, dimY, dimZ, false, false, 0);
Jason Samsec44e5d2013-10-09 17:15:36 -0700280 Type t = new Type(id, rs);
281 t.mElement = e;
282 t.mDimX = dimX;
283 t.mDimY = dimY;
284 t.mDimZ = dimZ;
285 t.calcElementCount();
286 return t;
287 }
288
289 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800290 * Builder class for Type.
291 *
292 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700293 public static class Builder {
294 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800295 int mDimX = 1;
296 int mDimY;
297 int mDimZ;
298 boolean mDimMipmaps;
299 boolean mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800300 int mYuv;
Jason Samsb8c5a842009-07-31 20:40:47 -0700301
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800302 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700303
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700304 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800305 * Create a new builder object.
306 *
307 * @param rs
308 * @param e The element for the type to be created.
309 */
Jason Sams22534172009-08-04 16:58:20 -0700310 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800311 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700312 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700313 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700314 }
315
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700316 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800317 * Add a dimension to the Type.
318 *
319 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800320 * @param value
321 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800322 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700323 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800324 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700325 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800326 mDimX = value;
327 return this;
Jason Sams22534172009-08-04 16:58:20 -0700328 }
329
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800330 public Builder setY(int value) {
331 if(value < 1) {
332 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
333 }
334 mDimY = value;
335 return this;
336 }
337
Jason Samsd1c306a2012-12-27 20:26:41 -0800338 public Builder setZ(int value) {
339 if(value < 1) {
340 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid.");
341 }
342 mDimZ = value;
343 return this;
344 }
345
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800346 public Builder setMipmaps(boolean value) {
347 mDimMipmaps = value;
348 return this;
349 }
350
351 public Builder setFaces(boolean value) {
352 mDimFaces = value;
353 return this;
354 }
355
Jason Samsb109cc72013-01-07 18:20:12 -0800356 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700357 * Set the YUV layout for a Type.
Jason Samsb109cc72013-01-07 18:20:12 -0800358 *
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -0700359 * @param yuvFormat {@link android.graphics.ImageFormat#YV12}, {@link android.graphics.ImageFormat#NV21}, or
360 * {@link android.graphics.ImageFormat#YUV_420_888}.
Jason Samsb109cc72013-01-07 18:20:12 -0800361 */
362 public Builder setYuvFormat(int yuvFormat) {
363 switch (yuvFormat) {
364 case android.graphics.ImageFormat.NV21:
365 case android.graphics.ImageFormat.YV12:
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -0700366 case android.graphics.ImageFormat.YUV_420_888:
Jason Samsb109cc72013-01-07 18:20:12 -0800367 break;
368
369 default:
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -0700370 throw new RSIllegalArgumentException(
371 "Only ImageFormat.NV21, .YV12, and .YUV_420_888 are supported..");
Jason Samsb109cc72013-01-07 18:20:12 -0800372 }
373
374 mYuv = yuvFormat;
375 return this;
376 }
377
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800378
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700379 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700380 * Validate structure and create a new Type.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800381 *
382 * @return Type
383 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700384 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800385 if (mDimZ > 0) {
386 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800387 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
388 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800389 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800390 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
391 }
392 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800393 if (mDimY > 0) {
394 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800395 throw new RSInvalidStateException("X dimension required when Y is present.");
396 }
397 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800398 if (mDimFaces) {
399 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800400 throw new RSInvalidStateException("Cube maps require 2D Types.");
401 }
402 }
403
Jason Samsb109cc72013-01-07 18:20:12 -0800404 if (mYuv != 0) {
405 if ((mDimZ != 0) || mDimFaces || mDimMipmaps) {
406 throw new RSInvalidStateException("YUV only supports basic 2D.");
407 }
408 }
409
Tim Murray460a0492013-11-19 12:45:54 -0800410 long id = mRS.nTypeCreate(mElement.getID(mRS),
Jason Samsb109cc72013-01-07 18:20:12 -0800411 mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces, mYuv);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800412 Type t = new Type(id, mRS);
413 t.mElement = mElement;
414 t.mDimX = mDimX;
415 t.mDimY = mDimY;
416 t.mDimZ = mDimZ;
417 t.mDimMipmaps = mDimMipmaps;
418 t.mDimFaces = mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800419 t.mDimYuv = mYuv;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800420
Jason Sams768bc022009-09-21 19:41:04 -0700421 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700422 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700423 }
424 }
425
426}