blob: d98842a10d44e07cb8b4b757d6d93466e26fde89 [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/**
24 * @hide
25 *
Jason Samsa1b13ed2010-11-12 14:58:37 -080026 * 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 and
28 * storage for the data thus described.
29 *
30 * A Type consists of several dimensions. Those are X, Y, Z, LOD (level of
31 * 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
36 * x=10, y=0 would be considered 1 dimensional.
37 *
38 * The LOD and Faces dimensions are booleans to indicate present or not present.
39 *
Jason Samsb8c5a842009-07-31 20:40:47 -070040 **/
41public class Type extends BaseObj {
Jason Sams768bc022009-09-21 19:41:04 -070042 int mDimX;
43 int mDimY;
44 int mDimZ;
Jason Samsbf6ef8d2010-12-06 15:59:59 -080045 boolean mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -070046 boolean mDimFaces;
47 int mElementCount;
Jason Sams1bada8c2009-08-09 17:01:55 -070048 Element mElement;
Jason Sams768bc022009-09-21 19:41:04 -070049
Jason Sams49a05d72010-12-29 14:31:29 -080050 public enum CubemapFace {
51 POSITVE_X (0),
52 NEGATIVE_X (1),
53 POSITVE_Y (2),
54 NEGATIVE_Y (3),
55 POSITVE_Z (4),
56 NEGATIVE_Z (5);
57
58 int mID;
59 CubemapFace(int id) {
60 mID = id;
61 }
62 }
63
Jason Samsa1b13ed2010-11-12 14:58:37 -080064 /**
65 * Return the element associated with this Type.
66 *
67 * @return Element
68 */
Jason Samse17964e2010-01-04 16:52:27 -080069 public Element getElement() {
70 return mElement;
71 }
Jason Sams1bada8c2009-08-09 17:01:55 -070072
Jason Samsa1b13ed2010-11-12 14:58:37 -080073 /**
74 * Return the value of the X dimension.
75 *
76 * @return int
77 */
Jason Sams768bc022009-09-21 19:41:04 -070078 public int getX() {
79 return mDimX;
80 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080081
82 /**
83 * Return the value of the Y dimension or 0 for a 1D allocation.
84 *
85 * @return int
86 */
Jason Sams768bc022009-09-21 19:41:04 -070087 public int getY() {
88 return mDimY;
89 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080090
91 /**
92 * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
93 *
94 * @return int
95 */
Jason Sams768bc022009-09-21 19:41:04 -070096 public int getZ() {
97 return mDimZ;
98 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080099
100 /**
101 * Return if the Type has a mipmap chain.
102 *
103 * @return boolean
104 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800105 public boolean hasMipmaps() {
106 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700107 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800108
109 /**
110 * Return if the Type is a cube map.
111 *
112 * @return boolean
113 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800114 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700115 return mDimFaces;
116 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800117
118 /**
119 * Return the total number of accessable cells in the Type.
120 *
121 * @return int
122 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800123 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700124 return mElementCount;
125 }
126
127 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800128 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700129 int x = getX();
130 int y = getY();
131 int z = getZ();
132 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800133 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700134 faces = 6;
135 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800136 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700137 x = 1;
138 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800139 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700140 y = 1;
141 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800142 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700143 z = 1;
144 }
145
146 int count = x * y * z * faces;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800147 if (hasLod && (x > 1) && (y > 1) && (z > 1)) {
Jason Sams768bc022009-09-21 19:41:04 -0700148 if(x > 1) {
149 x >>= 1;
150 }
151 if(y > 1) {
152 y >>= 1;
153 }
154 if(z > 1) {
155 z >>= 1;
156 }
157
158 count += x * y * z * faces;
159 }
160 mElementCount = count;
161 }
162
163
Jason Samsb8c5a842009-07-31 20:40:47 -0700164 Type(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700165 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700166 }
167
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700168 @Override
169 void updateFromNative() {
170 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
171 // mDimLOD; mDimFaces; mElement;
172 int[] dataBuffer = new int[6];
Jason Sams06d69de2010-11-09 17:11:40 -0800173 mRS.nTypeGetNativeData(getID(), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700174
175 mDimX = dataBuffer[0];
176 mDimY = dataBuffer[1];
177 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800178 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700179 mDimFaces = dataBuffer[4] == 1 ? true : false;
180
181 int elementID = dataBuffer[5];
182 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700183 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700184 mElement.updateFromNative();
185 }
186 calcElementCount();
187 }
188
Jason Samsa1b13ed2010-11-12 14:58:37 -0800189 /**
190 * Builder class for Type.
191 *
192 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700193 public static class Builder {
194 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800195 int mDimX = 1;
196 int mDimY;
197 int mDimZ;
198 boolean mDimMipmaps;
199 boolean mDimFaces;
Jason Samsb8c5a842009-07-31 20:40:47 -0700200
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800201 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700202
Jason Samsa1b13ed2010-11-12 14:58:37 -0800203 /**
204 * Create a new builder object.
205 *
206 * @param rs
207 * @param e The element for the type to be created.
208 */
Jason Sams22534172009-08-04 16:58:20 -0700209 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800210 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700211 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700212 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700213 }
214
Jason Samsa1b13ed2010-11-12 14:58:37 -0800215 /**
216 * Add a dimension to the Type.
217 *
218 *
219 * @param d
220 * @param value
221 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800222 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700223 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800224 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700225 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800226 mDimX = value;
227 return this;
Jason Sams22534172009-08-04 16:58:20 -0700228 }
229
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800230 public Builder setY(int value) {
231 if(value < 1) {
232 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
233 }
234 mDimY = value;
235 return this;
236 }
237
238 public Builder setMipmaps(boolean value) {
239 mDimMipmaps = value;
240 return this;
241 }
242
243 public Builder setFaces(boolean value) {
244 mDimFaces = value;
245 return this;
246 }
247
248
Jason Samsa1b13ed2010-11-12 14:58:37 -0800249 /**
250 * Validate structure and create a new type.
251 *
252 * @return Type
253 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700254 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800255 if (mDimZ > 0) {
256 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800257 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
258 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800259 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800260 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
261 }
262 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800263 if (mDimY > 0) {
264 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800265 throw new RSInvalidStateException("X dimension required when Y is present.");
266 }
267 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800268 if (mDimFaces) {
269 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800270 throw new RSInvalidStateException("Cube maps require 2D Types.");
271 }
272 }
273
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800274 int id = mRS.nTypeCreate(mElement.getID(), mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces);
275 Type t = new Type(id, mRS);
276 t.mElement = mElement;
277 t.mDimX = mDimX;
278 t.mDimY = mDimY;
279 t.mDimZ = mDimZ;
280 t.mDimMipmaps = mDimMipmaps;
281 t.mDimFaces = mDimFaces;
282
Jason Sams768bc022009-09-21 19:41:04 -0700283 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700284 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700285 }
286 }
287
288}