blob: d7c8255e3e529dbbb7d3d6885f45c989d8b888ea [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/**
Robert Ly11518ac2011-02-09 13:57:06 -080026 * <p>Type is an allocation template. It consists of an Element and one or more
27 * dimensions. It describes only the layout of memory but does not allocate any
28 * storage for the data that is described.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080029 *
Robert Ly11518ac2011-02-09 13:57:06 -080030 * <p>A Type consists of several dimensions. Those are X, Y, Z, LOD (level of
Jason Samsa1b13ed2010-11-12 14:58:37 -080031 * detail), Faces (faces of a cube map). The X,Y,Z dimensions can be assigned
32 * any positive integral value within the constraints of available memory. A
33 * single dimension allocation would have an X dimension of greater than zero
34 * while the Y and Z dimensions would be zero to indicate not present. In this
35 * regard an allocation of x=10, y=1 would be considered 2 dimensionsal while
Robert Ly11518ac2011-02-09 13:57:06 -080036 * x=10, y=0 would be considered 1 dimensional.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080037 *
Robert Ly11518ac2011-02-09 13:57:06 -080038 * <p>The LOD and Faces dimensions are booleans to indicate present or not present.</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>
42 * <p>For more information about creating an application that uses Renderscript, read the
Scott Mainb47fa162013-02-05 14:23:13 -080043 * <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 *
115 * @hide
116 * @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
Jason Samsb8c5a842009-07-31 20:40:47 -0700187 Type(int 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() {
193 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
194 // mDimLOD; mDimFaces; mElement;
195 int[] dataBuffer = new int[6];
Jason Samse07694b2012-04-03 15:36:36 -0700196 mRS.nTypeGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700197
198 mDimX = dataBuffer[0];
199 mDimY = dataBuffer[1];
200 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800201 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700202 mDimFaces = dataBuffer[4] == 1 ? true : false;
203
204 int elementID = dataBuffer[5];
205 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700206 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700207 mElement.updateFromNative();
208 }
209 calcElementCount();
210 }
211
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700212 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800213 * Builder class for Type.
214 *
215 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700216 public static class Builder {
217 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800218 int mDimX = 1;
219 int mDimY;
220 int mDimZ;
221 boolean mDimMipmaps;
222 boolean mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800223 int mYuv;
Jason Samsb8c5a842009-07-31 20:40:47 -0700224
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800225 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700226
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700227 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800228 * Create a new builder object.
229 *
230 * @param rs
231 * @param e The element for the type to be created.
232 */
Jason Sams22534172009-08-04 16:58:20 -0700233 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800234 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700235 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700236 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700237 }
238
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700239 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800240 * Add a dimension to the Type.
241 *
242 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800243 * @param value
244 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800245 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700246 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800247 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700248 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800249 mDimX = value;
250 return this;
Jason Sams22534172009-08-04 16:58:20 -0700251 }
252
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800253 public Builder setY(int value) {
254 if(value < 1) {
255 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
256 }
257 mDimY = value;
258 return this;
259 }
260
Jason Samsd1c306a2012-12-27 20:26:41 -0800261 public Builder setZ(int value) {
262 if(value < 1) {
263 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid.");
264 }
265 mDimZ = value;
266 return this;
267 }
268
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800269 public Builder setMipmaps(boolean value) {
270 mDimMipmaps = value;
271 return this;
272 }
273
274 public Builder setFaces(boolean value) {
275 mDimFaces = value;
276 return this;
277 }
278
Jason Samsb109cc72013-01-07 18:20:12 -0800279 /**
280 * @hide
281 *
282 * only NV21, YV12. Enums from ImageFormat
283 */
284 public Builder setYuvFormat(int yuvFormat) {
285 switch (yuvFormat) {
286 case android.graphics.ImageFormat.NV21:
287 case android.graphics.ImageFormat.YV12:
288 break;
289
290 default:
291 throw new RSIllegalArgumentException("Only NV21 and YV12 are supported..");
292 }
293
294 mYuv = yuvFormat;
295 return this;
296 }
297
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800298
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700299 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800300 * Validate structure and create a new type.
301 *
302 * @return Type
303 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700304 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800305 if (mDimZ > 0) {
306 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800307 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
308 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800309 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800310 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
311 }
312 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800313 if (mDimY > 0) {
314 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800315 throw new RSInvalidStateException("X dimension required when Y is present.");
316 }
317 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800318 if (mDimFaces) {
319 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800320 throw new RSInvalidStateException("Cube maps require 2D Types.");
321 }
322 }
323
Jason Samsb109cc72013-01-07 18:20:12 -0800324 if (mYuv != 0) {
325 if ((mDimZ != 0) || mDimFaces || mDimMipmaps) {
326 throw new RSInvalidStateException("YUV only supports basic 2D.");
327 }
328 }
329
Jason Samse07694b2012-04-03 15:36:36 -0700330 int id = mRS.nTypeCreate(mElement.getID(mRS),
Jason Samsb109cc72013-01-07 18:20:12 -0800331 mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces, mYuv);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800332 Type t = new Type(id, mRS);
333 t.mElement = mElement;
334 t.mDimX = mDimX;
335 t.mDimY = mDimY;
336 t.mDimZ = mDimZ;
337 t.mDimMipmaps = mDimMipmaps;
338 t.mDimFaces = mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800339 t.mDimYuv = mYuv;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800340
Jason Sams768bc022009-09-21 19:41:04 -0700341 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700342 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700343 }
344 }
345
346}