blob: f88af8bc9a8a97dd670fbe83ec3754f03068b559 [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 *
Jason Samsb8c5a842009-07-31 20:40:47 -070038 **/
39public class Type extends BaseObj {
Jason Sams768bc022009-09-21 19:41:04 -070040 int mDimX;
41 int mDimY;
42 int mDimZ;
Jason Samsbf6ef8d2010-12-06 15:59:59 -080043 boolean mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -070044 boolean mDimFaces;
45 int mElementCount;
Jason Sams1bada8c2009-08-09 17:01:55 -070046 Element mElement;
Jason Sams768bc022009-09-21 19:41:04 -070047
Jason Sams49a05d72010-12-29 14:31:29 -080048 public enum CubemapFace {
Stephen Hines20fbd012011-06-16 17:44:53 -070049 POSITIVE_X (0),
Jason Sams49a05d72010-12-29 14:31:29 -080050 NEGATIVE_X (1),
Stephen Hines20fbd012011-06-16 17:44:53 -070051 POSITIVE_Y (2),
Jason Sams49a05d72010-12-29 14:31:29 -080052 NEGATIVE_Y (3),
Stephen Hines20fbd012011-06-16 17:44:53 -070053 POSITIVE_Z (4),
54 NEGATIVE_Z (5),
55 @Deprecated
56 POSITVE_X (0),
57 @Deprecated
58 POSITVE_Y (2),
59 @Deprecated
60 POSITVE_Z (4);
Jason Sams49a05d72010-12-29 14:31:29 -080061
62 int mID;
63 CubemapFace(int id) {
64 mID = id;
65 }
66 }
67
Jason Samsa1b13ed2010-11-12 14:58:37 -080068 /**
69 * Return the element associated with this Type.
70 *
71 * @return Element
72 */
Jason Samse17964e2010-01-04 16:52:27 -080073 public Element getElement() {
74 return mElement;
75 }
Jason Sams1bada8c2009-08-09 17:01:55 -070076
Jason Samsa1b13ed2010-11-12 14:58:37 -080077 /**
78 * Return the value of the X dimension.
79 *
80 * @return int
81 */
Jason Sams768bc022009-09-21 19:41:04 -070082 public int getX() {
83 return mDimX;
84 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080085
86 /**
87 * Return the value of the Y dimension or 0 for a 1D allocation.
88 *
89 * @return int
90 */
Jason Sams768bc022009-09-21 19:41:04 -070091 public int getY() {
92 return mDimY;
93 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080094
95 /**
96 * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
97 *
98 * @return int
99 */
Jason Sams768bc022009-09-21 19:41:04 -0700100 public int getZ() {
101 return mDimZ;
102 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800103
104 /**
105 * Return if the Type has a mipmap chain.
106 *
107 * @return boolean
108 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800109 public boolean hasMipmaps() {
110 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700111 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800112
113 /**
114 * Return if the Type is a cube map.
115 *
116 * @return boolean
117 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800118 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700119 return mDimFaces;
120 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800121
122 /**
123 * Return the total number of accessable cells in the Type.
124 *
125 * @return int
126 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800127 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700128 return mElementCount;
129 }
130
131 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800132 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700133 int x = getX();
134 int y = getY();
135 int z = getZ();
136 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800137 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700138 faces = 6;
139 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800140 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700141 x = 1;
142 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800143 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700144 y = 1;
145 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800146 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700147 z = 1;
148 }
149
150 int count = x * y * z * faces;
Alex Sakhartchouk9ea30a62011-03-02 12:33:50 -0800151
152 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
Jason Sams768bc022009-09-21 19:41:04 -0700153 if(x > 1) {
154 x >>= 1;
155 }
156 if(y > 1) {
157 y >>= 1;
158 }
159 if(z > 1) {
160 z >>= 1;
161 }
162
163 count += x * y * z * faces;
164 }
165 mElementCount = count;
166 }
167
168
Jason Samsb8c5a842009-07-31 20:40:47 -0700169 Type(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700170 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700171 }
172
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700173 @Override
174 void updateFromNative() {
175 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
176 // mDimLOD; mDimFaces; mElement;
177 int[] dataBuffer = new int[6];
Jason Sams06d69de2010-11-09 17:11:40 -0800178 mRS.nTypeGetNativeData(getID(), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700179
180 mDimX = dataBuffer[0];
181 mDimY = dataBuffer[1];
182 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800183 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700184 mDimFaces = dataBuffer[4] == 1 ? true : false;
185
186 int elementID = dataBuffer[5];
187 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700188 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700189 mElement.updateFromNative();
190 }
191 calcElementCount();
192 }
193
Jason Samsa1b13ed2010-11-12 14:58:37 -0800194 /**
195 * Builder class for Type.
196 *
197 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700198 public static class Builder {
199 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800200 int mDimX = 1;
201 int mDimY;
202 int mDimZ;
203 boolean mDimMipmaps;
204 boolean mDimFaces;
Jason Samsb8c5a842009-07-31 20:40:47 -0700205
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800206 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700207
Jason Samsa1b13ed2010-11-12 14:58:37 -0800208 /**
209 * Create a new builder object.
210 *
211 * @param rs
212 * @param e The element for the type to be created.
213 */
Jason Sams22534172009-08-04 16:58:20 -0700214 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800215 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700216 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700217 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700218 }
219
Jason Samsa1b13ed2010-11-12 14:58:37 -0800220 /**
221 * Add a dimension to the Type.
222 *
223 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800224 * @param value
225 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800226 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700227 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800228 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700229 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800230 mDimX = value;
231 return this;
Jason Sams22534172009-08-04 16:58:20 -0700232 }
233
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800234 public Builder setY(int value) {
235 if(value < 1) {
236 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
237 }
238 mDimY = value;
239 return this;
240 }
241
242 public Builder setMipmaps(boolean value) {
243 mDimMipmaps = value;
244 return this;
245 }
246
247 public Builder setFaces(boolean value) {
248 mDimFaces = value;
249 return this;
250 }
251
252
Jason Samsa1b13ed2010-11-12 14:58:37 -0800253 /**
254 * Validate structure and create a new type.
255 *
256 * @return Type
257 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700258 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800259 if (mDimZ > 0) {
260 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800261 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
262 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800263 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800264 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
265 }
266 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800267 if (mDimY > 0) {
268 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800269 throw new RSInvalidStateException("X dimension required when Y is present.");
270 }
271 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800272 if (mDimFaces) {
273 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800274 throw new RSInvalidStateException("Cube maps require 2D Types.");
275 }
276 }
277
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800278 int id = mRS.nTypeCreate(mElement.getID(), mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces);
279 Type t = new Type(id, mRS);
280 t.mElement = mElement;
281 t.mDimX = mDimX;
282 t.mDimY = mDimY;
283 t.mDimZ = mDimZ;
284 t.mDimMipmaps = mDimMipmaps;
285 t.mDimFaces = mDimFaces;
286
Jason Sams768bc022009-09-21 19:41:04 -0700287 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700288 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700289 }
290 }
291
292}