blob: 70d1de43e40456fbba5cb8bf4b5128a420f9f17d [file] [log] [blame]
Jason Samsb8c5a842009-07-31 20:40:47 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
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;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -070021import android.util.Log;
Jason Sams43ee06852009-08-12 17:54:11 -070022
Jason Samsb8c5a842009-07-31 20:40:47 -070023/**
Robert Ly11518ac2011-02-09 13:57:06 -080024 * <p>Type is an allocation template. It consists of an Element and one or more
25 * dimensions. It describes only the layout of memory but does not allocate any
26 * storage for the data that is described.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080027 *
Robert Ly11518ac2011-02-09 13:57:06 -080028 * <p>A Type consists of several dimensions. Those are X, Y, Z, LOD (level of
Jason Samsa1b13ed2010-11-12 14:58:37 -080029 * detail), Faces (faces of a cube map). The X,Y,Z dimensions can be assigned
30 * any positive integral value within the constraints of available memory. A
31 * single dimension allocation would have an X dimension of greater than zero
32 * while the Y and Z dimensions would be zero to indicate not present. In this
33 * regard an allocation of x=10, y=1 would be considered 2 dimensionsal while
Robert Ly11518ac2011-02-09 13:57:06 -080034 * x=10, y=0 would be considered 1 dimensional.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080035 *
Robert Ly11518ac2011-02-09 13:57:06 -080036 * <p>The LOD and Faces dimensions are booleans to indicate present or not present.</p>
Jason Samsa1b13ed2010-11-12 14:58:37 -080037 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080038 * <div class="special reference">
39 * <h3>Developer Guides</h3>
40 * <p>For more information about creating an application that uses Renderscript, read the
41 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
42 * </div>
Jason Samsb8c5a842009-07-31 20:40:47 -070043 **/
44public class Type extends BaseObj {
Jason Sams768bc022009-09-21 19:41:04 -070045 int mDimX;
46 int mDimY;
47 int mDimZ;
Jason Samsbf6ef8d2010-12-06 15:59:59 -080048 boolean mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -070049 boolean mDimFaces;
50 int mElementCount;
Jason Sams1bada8c2009-08-09 17:01:55 -070051 Element mElement;
Jason Sams768bc022009-09-21 19:41:04 -070052
Jason Sams49a05d72010-12-29 14:31:29 -080053 public enum CubemapFace {
Stephen Hines20fbd012011-06-16 17:44:53 -070054 POSITIVE_X (0),
Jason Sams49a05d72010-12-29 14:31:29 -080055 NEGATIVE_X (1),
Stephen Hines20fbd012011-06-16 17:44:53 -070056 POSITIVE_Y (2),
Jason Sams49a05d72010-12-29 14:31:29 -080057 NEGATIVE_Y (3),
Stephen Hines20fbd012011-06-16 17:44:53 -070058 POSITIVE_Z (4),
59 NEGATIVE_Z (5),
60 @Deprecated
61 POSITVE_X (0),
62 @Deprecated
63 POSITVE_Y (2),
64 @Deprecated
65 POSITVE_Z (4);
Jason Sams49a05d72010-12-29 14:31:29 -080066
67 int mID;
68 CubemapFace(int id) {
69 mID = id;
70 }
71 }
72
Jason Samsa1b13ed2010-11-12 14:58:37 -080073 /**
74 * Return the element associated with this Type.
75 *
76 * @return Element
77 */
Jason Samse17964e2010-01-04 16:52:27 -080078 public Element getElement() {
79 return mElement;
80 }
Jason Sams1bada8c2009-08-09 17:01:55 -070081
Jason Samsa1b13ed2010-11-12 14:58:37 -080082 /**
83 * Return the value of the X dimension.
84 *
85 * @return int
86 */
Jason Sams768bc022009-09-21 19:41:04 -070087 public int getX() {
88 return mDimX;
89 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080090
91 /**
92 * Return the value of the Y dimension or 0 for a 1D allocation.
93 *
94 * @return int
95 */
Jason Sams768bc022009-09-21 19:41:04 -070096 public int getY() {
97 return mDimY;
98 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080099
100 /**
101 * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
102 *
103 * @return int
104 */
Jason Sams768bc022009-09-21 19:41:04 -0700105 public int getZ() {
106 return mDimZ;
107 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800108
109 /**
110 * Return if the Type has a mipmap chain.
111 *
112 * @return boolean
113 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800114 public boolean hasMipmaps() {
115 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700116 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800117
118 /**
119 * Return if the Type is a cube map.
120 *
121 * @return boolean
122 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800123 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700124 return mDimFaces;
125 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800126
127 /**
128 * Return the total number of accessable cells in the Type.
129 *
130 * @return int
131 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800132 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700133 return mElementCount;
134 }
135
136 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800137 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700138 int x = getX();
139 int y = getY();
140 int z = getZ();
141 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800142 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700143 faces = 6;
144 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800145 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700146 x = 1;
147 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800148 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700149 y = 1;
150 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800151 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700152 z = 1;
153 }
154
155 int count = x * y * z * faces;
Alex Sakhartchouk9ea30a62011-03-02 12:33:50 -0800156
157 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
Jason Sams768bc022009-09-21 19:41:04 -0700158 if(x > 1) {
159 x >>= 1;
160 }
161 if(y > 1) {
162 y >>= 1;
163 }
164 if(z > 1) {
165 z >>= 1;
166 }
167
168 count += x * y * z * faces;
169 }
170 mElementCount = count;
171 }
172
173
Jason Samsb8c5a842009-07-31 20:40:47 -0700174 Type(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700175 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700176 }
177
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700178 @Override
179 void updateFromNative() {
180 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
181 // mDimLOD; mDimFaces; mElement;
182 int[] dataBuffer = new int[6];
Jason Sams06d69de2010-11-09 17:11:40 -0800183 mRS.nTypeGetNativeData(getID(), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700184
185 mDimX = dataBuffer[0];
186 mDimY = dataBuffer[1];
187 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800188 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700189 mDimFaces = dataBuffer[4] == 1 ? true : false;
190
191 int elementID = dataBuffer[5];
192 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700193 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700194 mElement.updateFromNative();
195 }
196 calcElementCount();
197 }
198
Jason Samsa1b13ed2010-11-12 14:58:37 -0800199 /**
200 * Builder class for Type.
201 *
202 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700203 public static class Builder {
204 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800205 int mDimX = 1;
206 int mDimY;
207 int mDimZ;
208 boolean mDimMipmaps;
209 boolean mDimFaces;
Jason Samsb8c5a842009-07-31 20:40:47 -0700210
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800211 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700212
Jason Samsa1b13ed2010-11-12 14:58:37 -0800213 /**
214 * Create a new builder object.
215 *
216 * @param rs
217 * @param e The element for the type to be created.
218 */
Jason Sams22534172009-08-04 16:58:20 -0700219 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800220 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700221 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700222 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700223 }
224
Jason Samsa1b13ed2010-11-12 14:58:37 -0800225 /**
226 * Add a dimension to the Type.
227 *
228 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800229 * @param value
230 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800231 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700232 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800233 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700234 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800235 mDimX = value;
236 return this;
Jason Sams22534172009-08-04 16:58:20 -0700237 }
238
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800239 public Builder setY(int value) {
240 if(value < 1) {
241 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
242 }
243 mDimY = value;
244 return this;
245 }
246
247 public Builder setMipmaps(boolean value) {
248 mDimMipmaps = value;
249 return this;
250 }
251
252 public Builder setFaces(boolean value) {
253 mDimFaces = value;
254 return this;
255 }
256
257
Jason Samsa1b13ed2010-11-12 14:58:37 -0800258 /**
259 * Validate structure and create a new type.
260 *
261 * @return Type
262 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700263 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800264 if (mDimZ > 0) {
265 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800266 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
267 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800268 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800269 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
270 }
271 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800272 if (mDimY > 0) {
273 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800274 throw new RSInvalidStateException("X dimension required when Y is present.");
275 }
276 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800277 if (mDimFaces) {
278 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800279 throw new RSInvalidStateException("Cube maps require 2D Types.");
280 }
281 }
282
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800283 int id = mRS.nTypeCreate(mElement.getID(), mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces);
284 Type t = new Type(id, mRS);
285 t.mElement = mElement;
286 t.mDimX = mDimX;
287 t.mDimY = mDimY;
288 t.mDimZ = mDimZ;
289 t.mDimMipmaps = mDimMipmaps;
290 t.mDimFaces = mDimFaces;
291
Jason Sams768bc022009-09-21 19:41:04 -0700292 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700293 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700294 }
295 }
296
297}