blob: b39d2e44584b81f0c6d19ec442ad695fa2169222 [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 {
49 POSITVE_X (0),
50 NEGATIVE_X (1),
51 POSITVE_Y (2),
52 NEGATIVE_Y (3),
53 POSITVE_Z (4),
54 NEGATIVE_Z (5);
55
56 int mID;
57 CubemapFace(int id) {
58 mID = id;
59 }
60 }
61
Jason Samsa1b13ed2010-11-12 14:58:37 -080062 /**
63 * Return the element associated with this Type.
64 *
65 * @return Element
66 */
Jason Samse17964e2010-01-04 16:52:27 -080067 public Element getElement() {
68 return mElement;
69 }
Jason Sams1bada8c2009-08-09 17:01:55 -070070
Jason Samsa1b13ed2010-11-12 14:58:37 -080071 /**
72 * Return the value of the X dimension.
73 *
74 * @return int
75 */
Jason Sams768bc022009-09-21 19:41:04 -070076 public int getX() {
77 return mDimX;
78 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080079
80 /**
81 * Return the value of the Y dimension or 0 for a 1D allocation.
82 *
83 * @return int
84 */
Jason Sams768bc022009-09-21 19:41:04 -070085 public int getY() {
86 return mDimY;
87 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080088
89 /**
90 * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
91 *
92 * @return int
93 */
Jason Sams768bc022009-09-21 19:41:04 -070094 public int getZ() {
95 return mDimZ;
96 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080097
98 /**
99 * Return if the Type has a mipmap chain.
100 *
101 * @return boolean
102 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800103 public boolean hasMipmaps() {
104 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700105 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800106
107 /**
108 * Return if the Type is a cube map.
109 *
110 * @return boolean
111 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800112 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700113 return mDimFaces;
114 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800115
116 /**
117 * Return the total number of accessable cells in the Type.
118 *
119 * @return int
120 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800121 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700122 return mElementCount;
123 }
124
125 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800126 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700127 int x = getX();
128 int y = getY();
129 int z = getZ();
130 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800131 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700132 faces = 6;
133 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800134 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700135 x = 1;
136 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800137 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700138 y = 1;
139 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800140 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700141 z = 1;
142 }
143
144 int count = x * y * z * faces;
Alex Sakhartchouk9ea30a62011-03-02 12:33:50 -0800145
146 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
Jason Sams768bc022009-09-21 19:41:04 -0700147 if(x > 1) {
148 x >>= 1;
149 }
150 if(y > 1) {
151 y >>= 1;
152 }
153 if(z > 1) {
154 z >>= 1;
155 }
156
157 count += x * y * z * faces;
158 }
159 mElementCount = count;
160 }
161
162
Jason Samsb8c5a842009-07-31 20:40:47 -0700163 Type(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700164 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700165 }
166
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700167 @Override
168 void updateFromNative() {
169 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
170 // mDimLOD; mDimFaces; mElement;
171 int[] dataBuffer = new int[6];
Jason Sams06d69de2010-11-09 17:11:40 -0800172 mRS.nTypeGetNativeData(getID(), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700173
174 mDimX = dataBuffer[0];
175 mDimY = dataBuffer[1];
176 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800177 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700178 mDimFaces = dataBuffer[4] == 1 ? true : false;
179
180 int elementID = dataBuffer[5];
181 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700182 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700183 mElement.updateFromNative();
184 }
185 calcElementCount();
186 }
187
Jason Samsa1b13ed2010-11-12 14:58:37 -0800188 /**
189 * Builder class for Type.
190 *
191 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700192 public static class Builder {
193 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800194 int mDimX = 1;
195 int mDimY;
196 int mDimZ;
197 boolean mDimMipmaps;
198 boolean mDimFaces;
Jason Samsb8c5a842009-07-31 20:40:47 -0700199
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800200 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700201
Jason Samsa1b13ed2010-11-12 14:58:37 -0800202 /**
203 * Create a new builder object.
204 *
205 * @param rs
206 * @param e The element for the type to be created.
207 */
Jason Sams22534172009-08-04 16:58:20 -0700208 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800209 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700210 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700211 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700212 }
213
Jason Samsa1b13ed2010-11-12 14:58:37 -0800214 /**
215 * Add a dimension to the Type.
216 *
217 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800218 * @param value
219 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800220 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700221 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800222 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700223 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800224 mDimX = value;
225 return this;
Jason Sams22534172009-08-04 16:58:20 -0700226 }
227
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800228 public Builder setY(int value) {
229 if(value < 1) {
230 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
231 }
232 mDimY = value;
233 return this;
234 }
235
236 public Builder setMipmaps(boolean value) {
237 mDimMipmaps = value;
238 return this;
239 }
240
241 public Builder setFaces(boolean value) {
242 mDimFaces = value;
243 return this;
244 }
245
246
Jason Samsa1b13ed2010-11-12 14:58:37 -0800247 /**
248 * Validate structure and create a new type.
249 *
250 * @return Type
251 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700252 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800253 if (mDimZ > 0) {
254 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800255 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
256 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800257 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800258 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
259 }
260 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800261 if (mDimY > 0) {
262 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800263 throw new RSInvalidStateException("X dimension required when Y is present.");
264 }
265 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800266 if (mDimFaces) {
267 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800268 throw new RSInvalidStateException("Cube maps require 2D Types.");
269 }
270 }
271
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800272 int id = mRS.nTypeCreate(mElement.getID(), mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces);
273 Type t = new Type(id, mRS);
274 t.mElement = mElement;
275 t.mDimX = mDimX;
276 t.mDimY = mDimY;
277 t.mDimZ = mDimZ;
278 t.mDimMipmaps = mDimMipmaps;
279 t.mDimFaces = mDimFaces;
280
Jason Sams768bc022009-09-21 19:41:04 -0700281 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700282 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700283 }
284 }
285
286}