blob: e0237390afc446477d604876eb4f71c8061b78d3 [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 *
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -070040 * <p>A Type also supports YUV format information to support an
41 * {@link android.renderscript.Allocation} in a YUV format. The YUV formats
42 * supported are {@link android.graphics.ImageFormat#YV12},
43 * {@link android.graphics.ImageFormat#NV21}, and
44 * {@link android.graphics.ImageFormat#YUV_420_888}</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080045 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080046 * <div class="special reference">
47 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070048 * <p>For more information about creating an application that uses RenderScript, read the
49 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080050 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070051 **/
52public class Type extends BaseObj {
Jason Sams768bc022009-09-21 19:41:04 -070053 int mDimX;
54 int mDimY;
55 int mDimZ;
Jason Samsbf6ef8d2010-12-06 15:59:59 -080056 boolean mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -070057 boolean mDimFaces;
Jason Sams8140d7b2012-12-13 17:01:09 -080058 int mDimYuv;
Jason Sams768bc022009-09-21 19:41:04 -070059 int mElementCount;
Jason Sams1bada8c2009-08-09 17:01:55 -070060 Element mElement;
Jason Sams768bc022009-09-21 19:41:04 -070061
Jason Sams49a05d72010-12-29 14:31:29 -080062 public enum CubemapFace {
Stephen Hines20fbd012011-06-16 17:44:53 -070063 POSITIVE_X (0),
Jason Sams49a05d72010-12-29 14:31:29 -080064 NEGATIVE_X (1),
Stephen Hines20fbd012011-06-16 17:44:53 -070065 POSITIVE_Y (2),
Jason Sams49a05d72010-12-29 14:31:29 -080066 NEGATIVE_Y (3),
Stephen Hines20fbd012011-06-16 17:44:53 -070067 POSITIVE_Z (4),
68 NEGATIVE_Z (5),
69 @Deprecated
70 POSITVE_X (0),
71 @Deprecated
72 POSITVE_Y (2),
73 @Deprecated
74 POSITVE_Z (4);
Jason Sams49a05d72010-12-29 14:31:29 -080075
76 int mID;
77 CubemapFace(int id) {
78 mID = id;
79 }
80 }
81
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070082 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080083 * Return the element associated with this Type.
84 *
85 * @return Element
86 */
Jason Samse17964e2010-01-04 16:52:27 -080087 public Element getElement() {
88 return mElement;
89 }
Jason Sams1bada8c2009-08-09 17:01:55 -070090
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070091 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080092 * Return the value of the X dimension.
93 *
94 * @return int
95 */
Jason Sams768bc022009-09-21 19:41:04 -070096 public int getX() {
97 return mDimX;
98 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080099
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700100 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800101 * Return the value of the Y dimension or 0 for a 1D allocation.
102 *
103 * @return int
104 */
Jason Sams768bc022009-09-21 19:41:04 -0700105 public int getY() {
106 return mDimY;
107 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800108
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700109 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800110 * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
111 *
112 * @return int
113 */
Jason Sams768bc022009-09-21 19:41:04 -0700114 public int getZ() {
115 return mDimZ;
116 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800117
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700118 /**
Jason Sams5a722cf2013-03-26 13:27:37 -0700119 * Get the YUV format
120 *
Jason Sams02d56d92013-04-12 16:40:50 -0700121 *
Jason Sams5a722cf2013-03-26 13:27:37 -0700122 * @return int
123 */
124 public int getYuv() {
125 return mDimYuv;
126 }
127
128 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800129 * Return if the Type has a mipmap chain.
130 *
131 * @return boolean
132 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800133 public boolean hasMipmaps() {
134 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700135 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800136
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700137 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800138 * Return if the Type is a cube map.
139 *
140 * @return boolean
141 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800142 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700143 return mDimFaces;
144 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800145
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700146 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800147 * Return the total number of accessable cells in the Type.
148 *
149 * @return int
150 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800151 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700152 return mElementCount;
153 }
154
155 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800156 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700157 int x = getX();
158 int y = getY();
159 int z = getZ();
160 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800161 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700162 faces = 6;
163 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800164 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700165 x = 1;
166 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800167 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700168 y = 1;
169 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800170 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700171 z = 1;
172 }
173
174 int count = x * y * z * faces;
Alex Sakhartchouk9ea30a62011-03-02 12:33:50 -0800175
176 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
Jason Sams768bc022009-09-21 19:41:04 -0700177 if(x > 1) {
178 x >>= 1;
179 }
180 if(y > 1) {
181 y >>= 1;
182 }
183 if(z > 1) {
184 z >>= 1;
185 }
186
187 count += x * y * z * faces;
188 }
189 mElementCount = count;
190 }
191
192
Jason Samsb8c5a842009-07-31 20:40:47 -0700193 Type(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700194 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700195 }
196
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700197 @Override
198 void updateFromNative() {
199 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
200 // mDimLOD; mDimFaces; mElement;
201 int[] dataBuffer = new int[6];
Jason Samse07694b2012-04-03 15:36:36 -0700202 mRS.nTypeGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700203
204 mDimX = dataBuffer[0];
205 mDimY = dataBuffer[1];
206 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800207 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700208 mDimFaces = dataBuffer[4] == 1 ? true : false;
209
210 int elementID = dataBuffer[5];
211 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700212 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700213 mElement.updateFromNative();
214 }
215 calcElementCount();
216 }
217
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700218 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800219 * Builder class for Type.
220 *
221 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700222 public static class Builder {
223 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800224 int mDimX = 1;
225 int mDimY;
226 int mDimZ;
227 boolean mDimMipmaps;
228 boolean mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800229 int mYuv;
Jason Samsb8c5a842009-07-31 20:40:47 -0700230
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800231 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700232
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700233 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800234 * Create a new builder object.
235 *
236 * @param rs
237 * @param e The element for the type to be created.
238 */
Jason Sams22534172009-08-04 16:58:20 -0700239 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800240 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700241 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700242 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700243 }
244
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700245 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800246 * Add a dimension to the Type.
247 *
248 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800249 * @param value
250 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800251 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700252 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800253 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700254 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800255 mDimX = value;
256 return this;
Jason Sams22534172009-08-04 16:58:20 -0700257 }
258
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800259 public Builder setY(int value) {
260 if(value < 1) {
261 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
262 }
263 mDimY = value;
264 return this;
265 }
266
Jason Samsd1c306a2012-12-27 20:26:41 -0800267 public Builder setZ(int value) {
268 if(value < 1) {
269 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid.");
270 }
271 mDimZ = value;
272 return this;
273 }
274
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800275 public Builder setMipmaps(boolean value) {
276 mDimMipmaps = value;
277 return this;
278 }
279
280 public Builder setFaces(boolean value) {
281 mDimFaces = value;
282 return this;
283 }
284
Jason Samsb109cc72013-01-07 18:20:12 -0800285 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700286 * Set the YUV layout for a Type.
Jason Samsb109cc72013-01-07 18:20:12 -0800287 *
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -0700288 * @param yuvFormat {@link android.graphics.ImageFormat#YV12}, {@link android.graphics.ImageFormat#NV21}, or
289 * {@link android.graphics.ImageFormat#YUV_420_888}.
Jason Samsb109cc72013-01-07 18:20:12 -0800290 */
291 public Builder setYuvFormat(int yuvFormat) {
292 switch (yuvFormat) {
293 case android.graphics.ImageFormat.NV21:
294 case android.graphics.ImageFormat.YV12:
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -0700295 case android.graphics.ImageFormat.YUV_420_888:
Jason Samsb109cc72013-01-07 18:20:12 -0800296 break;
297
298 default:
Eino-Ville Talvalaccadaf12013-08-14 10:36:30 -0700299 throw new RSIllegalArgumentException(
300 "Only ImageFormat.NV21, .YV12, and .YUV_420_888 are supported..");
Jason Samsb109cc72013-01-07 18:20:12 -0800301 }
302
303 mYuv = yuvFormat;
304 return this;
305 }
306
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800307
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700308 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700309 * Validate structure and create a new Type.
Jason Samsa1b13ed2010-11-12 14:58:37 -0800310 *
311 * @return Type
312 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700313 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800314 if (mDimZ > 0) {
315 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800316 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
317 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800318 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800319 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
320 }
321 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800322 if (mDimY > 0) {
323 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800324 throw new RSInvalidStateException("X dimension required when Y is present.");
325 }
326 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800327 if (mDimFaces) {
328 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800329 throw new RSInvalidStateException("Cube maps require 2D Types.");
330 }
331 }
332
Jason Samsb109cc72013-01-07 18:20:12 -0800333 if (mYuv != 0) {
334 if ((mDimZ != 0) || mDimFaces || mDimMipmaps) {
335 throw new RSInvalidStateException("YUV only supports basic 2D.");
336 }
337 }
338
Jason Samse07694b2012-04-03 15:36:36 -0700339 int id = mRS.nTypeCreate(mElement.getID(mRS),
Jason Samsb109cc72013-01-07 18:20:12 -0800340 mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces, mYuv);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800341 Type t = new Type(id, mRS);
342 t.mElement = mElement;
343 t.mDimX = mDimX;
344 t.mDimY = mDimY;
345 t.mDimZ = mDimZ;
346 t.mDimMipmaps = mDimMipmaps;
347 t.mDimFaces = mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800348 t.mDimYuv = mYuv;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800349
Jason Sams768bc022009-09-21 19:41:04 -0700350 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700351 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700352 }
353 }
354
355}