blob: ef08c295a3d3f8d8f291826a7315714cf8fb1063 [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
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070019
Jason Sams43ee06852009-08-12 17:54:11 -070020import java.lang.reflect.Field;
Jason Samsb109cc72013-01-07 18:20:12 -080021
22import android.graphics.ImageFormat;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070023import android.util.Log;
Jason Sams43ee06852009-08-12 17:54:11 -070024
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070025/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070026 * <p>A Type describes the {@link android.renderscript.Element} and dimensions used for an {@link
27 * android.renderscript.Allocation} or a parallel operation. Types are created through {@link
28 * android.renderscript.Type.Builder}.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080029 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070030 * <p>A Type always includes an {@link android.renderscript.Element} and an X
31 * dimension. A Type may be multidimensional, up to three dimensions. A nonzero
32 * value in the Y or Z dimensions indicates that the dimension is present. Note
33 * that a Type with only a given X dimension and a Type with the same X
34 * dimension but Y = 1 are not equivalent.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080035 *
Tim Murrayc11e25c2013-04-09 11:01:01 -070036 * <p>A Type also supports inclusion of level of detail (LOD) or cube map
37 * faces. LOD and cube map faces are booleans to indicate present or not
38 * present. </p>
39 *
40 * <p>A Type also supports YUV format information to support an {@link
41 * android.renderscript.Allocation} in a YUV format. The YUV formats supported
42 * are {@link android.graphics.ImageFormat#YV12} and {@link
43 * android.graphics.ImageFormat#NV21}.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080044 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080045 * <div class="special reference">
46 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070047 * <p>For more information about creating an application that uses RenderScript, read the
48 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080049 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070050 **/
51public class Type extends BaseObj {
Jason Sams768bc022009-09-21 19:41:04 -070052 int mDimX;
53 int mDimY;
54 int mDimZ;
Jason Samsbf6ef8d2010-12-06 15:59:59 -080055 boolean mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -070056 boolean mDimFaces;
Jason Sams8140d7b2012-12-13 17:01:09 -080057 int mDimYuv;
Jason Sams768bc022009-09-21 19:41:04 -070058 int mElementCount;
Jason Sams1bada8c2009-08-09 17:01:55 -070059 Element mElement;
Jason Sams768bc022009-09-21 19:41:04 -070060
Jason Sams49a05d72010-12-29 14:31:29 -080061 public enum CubemapFace {
Stephen Hines20fbd012011-06-16 17:44:53 -070062 POSITIVE_X (0),
Jason Sams49a05d72010-12-29 14:31:29 -080063 NEGATIVE_X (1),
Stephen Hines20fbd012011-06-16 17:44:53 -070064 POSITIVE_Y (2),
Jason Sams49a05d72010-12-29 14:31:29 -080065 NEGATIVE_Y (3),
Stephen Hines20fbd012011-06-16 17:44:53 -070066 POSITIVE_Z (4),
67 NEGATIVE_Z (5),
68 @Deprecated
69 POSITVE_X (0),
70 @Deprecated
71 POSITVE_Y (2),
72 @Deprecated
73 POSITVE_Z (4);
Jason Sams49a05d72010-12-29 14:31:29 -080074
75 int mID;
76 CubemapFace(int id) {
77 mID = id;
78 }
79 }
80
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070081 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080082 * Return the element associated with this Type.
83 *
84 * @return Element
85 */
Jason Samse17964e2010-01-04 16:52:27 -080086 public Element getElement() {
87 return mElement;
88 }
Jason Sams1bada8c2009-08-09 17:01:55 -070089
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070090 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080091 * Return the value of the X dimension.
92 *
93 * @return int
94 */
Jason Sams768bc022009-09-21 19:41:04 -070095 public int getX() {
96 return mDimX;
97 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080098
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070099 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800100 * Return the value of the Y dimension or 0 for a 1D allocation.
101 *
102 * @return int
103 */
Jason Sams768bc022009-09-21 19:41:04 -0700104 public int getY() {
105 return mDimY;
106 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800107
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700108 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800109 * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
110 *
111 * @return int
112 */
Jason Sams768bc022009-09-21 19:41:04 -0700113 public int getZ() {
114 return mDimZ;
115 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800116
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700117 /**
Jason Sams5a722cf2013-03-26 13:27:37 -0700118 * Get the YUV format
119 *
Jason Sams02d56d92013-04-12 16:40:50 -0700120 *
Jason Sams5a722cf2013-03-26 13:27:37 -0700121 * @return int
122 */
123 public int getYuv() {
124 return mDimYuv;
125 }
126
127 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800128 * Return if the Type has a mipmap chain.
129 *
130 * @return boolean
131 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800132 public boolean hasMipmaps() {
133 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700134 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800135
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700136 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800137 * Return if the Type is a cube map.
138 *
139 * @return boolean
140 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800141 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700142 return mDimFaces;
143 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800144
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700145 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800146 * Return the total number of accessable cells in the Type.
147 *
148 * @return int
149 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800150 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700151 return mElementCount;
152 }
153
154 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800155 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700156 int x = getX();
157 int y = getY();
158 int z = getZ();
159 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800160 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700161 faces = 6;
162 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800163 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700164 x = 1;
165 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800166 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700167 y = 1;
168 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800169 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700170 z = 1;
171 }
172
173 int count = x * y * z * faces;
Alex Sakhartchouk9ea30a62011-03-02 12:33:50 -0800174
175 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
Jason Sams768bc022009-09-21 19:41:04 -0700176 if(x > 1) {
177 x >>= 1;
178 }
179 if(y > 1) {
180 y >>= 1;
181 }
182 if(z > 1) {
183 z >>= 1;
184 }
185
186 count += x * y * z * faces;
187 }
188 mElementCount = count;
189 }
190
191
Jason Samsb8c5a842009-07-31 20:40:47 -0700192 Type(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700193 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700194 }
195
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700196 @Override
197 void updateFromNative() {
198 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
199 // mDimLOD; mDimFaces; mElement;
200 int[] dataBuffer = new int[6];
Jason Samse07694b2012-04-03 15:36:36 -0700201 mRS.nTypeGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700202
203 mDimX = dataBuffer[0];
204 mDimY = dataBuffer[1];
205 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800206 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700207 mDimFaces = dataBuffer[4] == 1 ? true : false;
208
209 int elementID = dataBuffer[5];
210 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700211 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700212 mElement.updateFromNative();
213 }
214 calcElementCount();
215 }
216
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700217 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800218 * Builder class for Type.
219 *
220 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700221 public static class Builder {
222 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800223 int mDimX = 1;
224 int mDimY;
225 int mDimZ;
226 boolean mDimMipmaps;
227 boolean mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800228 int mYuv;
Jason Samsb8c5a842009-07-31 20:40:47 -0700229
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800230 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700231
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700232 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800233 * Create a new builder object.
234 *
235 * @param rs
236 * @param e The element for the type to be created.
237 */
Jason Sams22534172009-08-04 16:58:20 -0700238 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800239 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700240 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700241 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700242 }
243
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700244 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800245 * Add a dimension to the Type.
246 *
247 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800248 * @param value
249 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800250 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700251 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800252 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700253 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800254 mDimX = value;
255 return this;
Jason Sams22534172009-08-04 16:58:20 -0700256 }
257
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800258 public Builder setY(int value) {
259 if(value < 1) {
260 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
261 }
262 mDimY = value;
263 return this;
264 }
265
Jason Samsd1c306a2012-12-27 20:26:41 -0800266 public Builder setZ(int value) {
267 if(value < 1) {
268 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid.");
269 }
270 mDimZ = value;
271 return this;
272 }
273
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800274 public Builder setMipmaps(boolean value) {
275 mDimMipmaps = value;
276 return this;
277 }
278
279 public Builder setFaces(boolean value) {
280 mDimFaces = value;
281 return this;
282 }
283
Jason Samsb109cc72013-01-07 18:20:12 -0800284 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700285 * Set the YUV layout for a Type.
Jason Samsb109cc72013-01-07 18:20:12 -0800286 *
Tim Murrayc11e25c2013-04-09 11:01:01 -0700287 * @param yuvFormat {@link android.graphics.ImageFormat#YV12} or {@link android.graphics.ImageFormat#NV21}
Jason Samsb109cc72013-01-07 18:20:12 -0800288 */
289 public Builder setYuvFormat(int yuvFormat) {
290 switch (yuvFormat) {
291 case android.graphics.ImageFormat.NV21:
292 case android.graphics.ImageFormat.YV12:
293 break;
294
295 default:
296 throw new RSIllegalArgumentException("Only NV21 and YV12 are supported..");
297 }
298
299 mYuv = yuvFormat;
300 return this;
301 }
302
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800303
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700304 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700305 * Validate structure and create a new Type.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800306 *
307 * @return Type
308 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700309 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800310 if (mDimZ > 0) {
311 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800312 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
313 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800314 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800315 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
316 }
317 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800318 if (mDimY > 0) {
319 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800320 throw new RSInvalidStateException("X dimension required when Y is present.");
321 }
322 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800323 if (mDimFaces) {
324 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800325 throw new RSInvalidStateException("Cube maps require 2D Types.");
326 }
327 }
328
Jason Samsb109cc72013-01-07 18:20:12 -0800329 if (mYuv != 0) {
330 if ((mDimZ != 0) || mDimFaces || mDimMipmaps) {
331 throw new RSInvalidStateException("YUV only supports basic 2D.");
332 }
333 }
334
Jason Samse07694b2012-04-03 15:36:36 -0700335 int id = mRS.nTypeCreate(mElement.getID(mRS),
Jason Samsb109cc72013-01-07 18:20:12 -0800336 mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces, mYuv);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800337 Type t = new Type(id, mRS);
338 t.mElement = mElement;
339 t.mDimX = mDimX;
340 t.mDimY = mDimY;
341 t.mDimZ = mDimZ;
342 t.mDimMipmaps = mDimMipmaps;
343 t.mDimFaces = mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800344 t.mDimYuv = mYuv;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800345
Jason Sams768bc022009-09-21 19:41:04 -0700346 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700347 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700348 }
349 }
350
351}