blob: 95070308b0e3894d6b91f3d90b00fae5713c64a4 [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 Samsa1b13ed2010-11-12 14:58:37 -0800113 * Return if the Type has a mipmap chain.
114 *
115 * @return boolean
116 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800117 public boolean hasMipmaps() {
118 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700119 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800120
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700121 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800122 * Return if the Type is a cube map.
123 *
124 * @return boolean
125 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800126 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700127 return mDimFaces;
128 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800129
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700130 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800131 * Return the total number of accessable cells in the Type.
132 *
133 * @return int
134 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800135 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700136 return mElementCount;
137 }
138
139 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800140 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700141 int x = getX();
142 int y = getY();
143 int z = getZ();
144 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800145 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700146 faces = 6;
147 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800148 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700149 x = 1;
150 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800151 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700152 y = 1;
153 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800154 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700155 z = 1;
156 }
157
158 int count = x * y * z * faces;
Alex Sakhartchouk9ea30a62011-03-02 12:33:50 -0800159
160 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
Jason Sams768bc022009-09-21 19:41:04 -0700161 if(x > 1) {
162 x >>= 1;
163 }
164 if(y > 1) {
165 y >>= 1;
166 }
167 if(z > 1) {
168 z >>= 1;
169 }
170
171 count += x * y * z * faces;
172 }
173 mElementCount = count;
174 }
175
176
Jason Samsb8c5a842009-07-31 20:40:47 -0700177 Type(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700178 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700179 }
180
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700181 @Override
182 void updateFromNative() {
183 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
184 // mDimLOD; mDimFaces; mElement;
185 int[] dataBuffer = new int[6];
Jason Samse07694b2012-04-03 15:36:36 -0700186 mRS.nTypeGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700187
188 mDimX = dataBuffer[0];
189 mDimY = dataBuffer[1];
190 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800191 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700192 mDimFaces = dataBuffer[4] == 1 ? true : false;
193
194 int elementID = dataBuffer[5];
195 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700196 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700197 mElement.updateFromNative();
198 }
199 calcElementCount();
200 }
201
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700202 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800203 * Builder class for Type.
204 *
205 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700206 public static class Builder {
207 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800208 int mDimX = 1;
209 int mDimY;
210 int mDimZ;
211 boolean mDimMipmaps;
212 boolean mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800213 int mYuv;
Jason Samsb8c5a842009-07-31 20:40:47 -0700214
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800215 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700216
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700217 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800218 * Create a new builder object.
219 *
220 * @param rs
221 * @param e The element for the type to be created.
222 */
Jason Sams22534172009-08-04 16:58:20 -0700223 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800224 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700225 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700226 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700227 }
228
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700229 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800230 * Add a dimension to the Type.
231 *
232 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800233 * @param value
234 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800235 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700236 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800237 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700238 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800239 mDimX = value;
240 return this;
Jason Sams22534172009-08-04 16:58:20 -0700241 }
242
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800243 public Builder setY(int value) {
244 if(value < 1) {
245 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
246 }
247 mDimY = value;
248 return this;
249 }
250
Jason Samsd1c306a2012-12-27 20:26:41 -0800251 public Builder setZ(int value) {
252 if(value < 1) {
253 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid.");
254 }
255 mDimZ = value;
256 return this;
257 }
258
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800259 public Builder setMipmaps(boolean value) {
260 mDimMipmaps = value;
261 return this;
262 }
263
264 public Builder setFaces(boolean value) {
265 mDimFaces = value;
266 return this;
267 }
268
Jason Samsb109cc72013-01-07 18:20:12 -0800269 /**
270 * @hide
271 *
272 * only NV21, YV12. Enums from ImageFormat
273 */
274 public Builder setYuvFormat(int yuvFormat) {
275 switch (yuvFormat) {
276 case android.graphics.ImageFormat.NV21:
277 case android.graphics.ImageFormat.YV12:
278 break;
279
280 default:
281 throw new RSIllegalArgumentException("Only NV21 and YV12 are supported..");
282 }
283
284 mYuv = yuvFormat;
285 return this;
286 }
287
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800288
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700289 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800290 * Validate structure and create a new type.
291 *
292 * @return Type
293 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700294 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800295 if (mDimZ > 0) {
296 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800297 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
298 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800299 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800300 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
301 }
302 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800303 if (mDimY > 0) {
304 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800305 throw new RSInvalidStateException("X dimension required when Y is present.");
306 }
307 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800308 if (mDimFaces) {
309 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800310 throw new RSInvalidStateException("Cube maps require 2D Types.");
311 }
312 }
313
Jason Samsb109cc72013-01-07 18:20:12 -0800314 if (mYuv != 0) {
315 if ((mDimZ != 0) || mDimFaces || mDimMipmaps) {
316 throw new RSInvalidStateException("YUV only supports basic 2D.");
317 }
318 }
319
Jason Samse07694b2012-04-03 15:36:36 -0700320 int id = mRS.nTypeCreate(mElement.getID(mRS),
Jason Samsb109cc72013-01-07 18:20:12 -0800321 mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces, mYuv);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800322 Type t = new Type(id, mRS);
323 t.mElement = mElement;
324 t.mDimX = mDimX;
325 t.mDimY = mDimY;
326 t.mDimZ = mDimZ;
327 t.mDimMipmaps = mDimMipmaps;
328 t.mDimFaces = mDimFaces;
Jason Samsb109cc72013-01-07 18:20:12 -0800329 t.mDimYuv = mYuv;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800330
Jason Sams768bc022009-09-21 19:41:04 -0700331 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700332 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700333 }
334 }
335
336}