blob: 93d8b4b0ad9c9c5079eec1cd6e33d8102dad60cc [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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -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;
Jason Sams8140d7b2012-12-13 17:01:09 -080050 int mDimYuv;
Jason Sams768bc022009-09-21 19:41:04 -070051 int mElementCount;
Jason Sams1bada8c2009-08-09 17:01:55 -070052 Element mElement;
Jason Sams768bc022009-09-21 19:41:04 -070053
Jason Sams49a05d72010-12-29 14:31:29 -080054 public enum CubemapFace {
Stephen Hines20fbd012011-06-16 17:44:53 -070055 POSITIVE_X (0),
Jason Sams49a05d72010-12-29 14:31:29 -080056 NEGATIVE_X (1),
Stephen Hines20fbd012011-06-16 17:44:53 -070057 POSITIVE_Y (2),
Jason Sams49a05d72010-12-29 14:31:29 -080058 NEGATIVE_Y (3),
Stephen Hines20fbd012011-06-16 17:44:53 -070059 POSITIVE_Z (4),
60 NEGATIVE_Z (5),
61 @Deprecated
62 POSITVE_X (0),
63 @Deprecated
64 POSITVE_Y (2),
65 @Deprecated
66 POSITVE_Z (4);
Jason Sams49a05d72010-12-29 14:31:29 -080067
68 int mID;
69 CubemapFace(int id) {
70 mID = id;
71 }
72 }
73
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070074 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080075 * Return the element associated with this Type.
76 *
77 * @return Element
78 */
Jason Samse17964e2010-01-04 16:52:27 -080079 public Element getElement() {
80 return mElement;
81 }
Jason Sams1bada8c2009-08-09 17:01:55 -070082
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070083 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080084 * Return the value of the X dimension.
85 *
86 * @return int
87 */
Jason Sams768bc022009-09-21 19:41:04 -070088 public int getX() {
89 return mDimX;
90 }
Jason Samsa1b13ed2010-11-12 14:58:37 -080091
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070092 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -080093 * Return the value of the Y dimension or 0 for a 1D allocation.
94 *
95 * @return int
96 */
Jason Sams768bc022009-09-21 19:41:04 -070097 public int getY() {
98 return mDimY;
99 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800100
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700101 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800102 * Return the value of the Z dimension or 0 for a 1D or 2D allocation.
103 *
104 * @return int
105 */
Jason Sams768bc022009-09-21 19:41:04 -0700106 public int getZ() {
107 return mDimZ;
108 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800109
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700110 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800111 * Return if the Type has a mipmap chain.
112 *
113 * @return boolean
114 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800115 public boolean hasMipmaps() {
116 return mDimMipmaps;
Jason Sams768bc022009-09-21 19:41:04 -0700117 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800118
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700119 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800120 * Return if the Type is a cube map.
121 *
122 * @return boolean
123 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800124 public boolean hasFaces() {
Jason Sams768bc022009-09-21 19:41:04 -0700125 return mDimFaces;
126 }
Jason Samsa1b13ed2010-11-12 14:58:37 -0800127
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700128 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800129 * Return the total number of accessable cells in the Type.
130 *
131 * @return int
132 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800133 public int getCount() {
Jason Sams768bc022009-09-21 19:41:04 -0700134 return mElementCount;
135 }
136
137 void calcElementCount() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800138 boolean hasLod = hasMipmaps();
Jason Sams768bc022009-09-21 19:41:04 -0700139 int x = getX();
140 int y = getY();
141 int z = getZ();
142 int faces = 1;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800143 if (hasFaces()) {
Jason Sams768bc022009-09-21 19:41:04 -0700144 faces = 6;
145 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800146 if (x == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700147 x = 1;
148 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800149 if (y == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700150 y = 1;
151 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800152 if (z == 0) {
Jason Sams768bc022009-09-21 19:41:04 -0700153 z = 1;
154 }
155
156 int count = x * y * z * faces;
Alex Sakhartchouk9ea30a62011-03-02 12:33:50 -0800157
158 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
Jason Sams768bc022009-09-21 19:41:04 -0700159 if(x > 1) {
160 x >>= 1;
161 }
162 if(y > 1) {
163 y >>= 1;
164 }
165 if(z > 1) {
166 z >>= 1;
167 }
168
169 count += x * y * z * faces;
170 }
171 mElementCount = count;
172 }
173
174
Jason Samsb8c5a842009-07-31 20:40:47 -0700175 Type(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700176 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700177 }
178
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700179 @Override
180 void updateFromNative() {
181 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
182 // mDimLOD; mDimFaces; mElement;
183 int[] dataBuffer = new int[6];
Jason Samse07694b2012-04-03 15:36:36 -0700184 mRS.nTypeGetNativeData(getID(mRS), dataBuffer);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700185
186 mDimX = dataBuffer[0];
187 mDimY = dataBuffer[1];
188 mDimZ = dataBuffer[2];
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800189 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700190 mDimFaces = dataBuffer[4] == 1 ? true : false;
191
192 int elementID = dataBuffer[5];
193 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700194 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700195 mElement.updateFromNative();
196 }
197 calcElementCount();
198 }
199
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700200 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800201 * Builder class for Type.
202 *
203 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700204 public static class Builder {
205 RenderScript mRS;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800206 int mDimX = 1;
207 int mDimY;
208 int mDimZ;
209 boolean mDimMipmaps;
210 boolean mDimFaces;
Jason Samsb8c5a842009-07-31 20:40:47 -0700211
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800212 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700213
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700214 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800215 * Create a new builder object.
216 *
217 * @param rs
218 * @param e The element for the type to be created.
219 */
Jason Sams22534172009-08-04 16:58:20 -0700220 public Builder(RenderScript rs, Element e) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800221 e.checkValid();
Jason Sams22534172009-08-04 16:58:20 -0700222 mRS = rs;
Jason Sams22534172009-08-04 16:58:20 -0700223 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700224 }
225
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700226 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800227 * Add a dimension to the Type.
228 *
229 *
Jason Samsa1b13ed2010-11-12 14:58:37 -0800230 * @param value
231 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800232 public Builder setX(int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700233 if(value < 1) {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800234 throw new RSIllegalArgumentException("Values of less than 1 for Dimension X are not valid.");
Jason Sams3c0dfba2009-09-27 17:50:38 -0700235 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800236 mDimX = value;
237 return this;
Jason Sams22534172009-08-04 16:58:20 -0700238 }
239
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800240 public Builder setY(int value) {
241 if(value < 1) {
242 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Y are not valid.");
243 }
244 mDimY = value;
245 return this;
246 }
247
Jason Samsd1c306a2012-12-27 20:26:41 -0800248 public Builder setZ(int value) {
249 if(value < 1) {
250 throw new RSIllegalArgumentException("Values of less than 1 for Dimension Z are not valid.");
251 }
252 mDimZ = value;
253 return this;
254 }
255
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800256 public Builder setMipmaps(boolean value) {
257 mDimMipmaps = value;
258 return this;
259 }
260
261 public Builder setFaces(boolean value) {
262 mDimFaces = value;
263 return this;
264 }
265
266
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700267 /**
Jason Samsa1b13ed2010-11-12 14:58:37 -0800268 * Validate structure and create a new type.
269 *
270 * @return Type
271 */
Jason Samsb8c5a842009-07-31 20:40:47 -0700272 public Type create() {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800273 if (mDimZ > 0) {
274 if ((mDimX < 1) || (mDimY < 1)) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800275 throw new RSInvalidStateException("Both X and Y dimension required when Z is present.");
276 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800277 if (mDimFaces) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800278 throw new RSInvalidStateException("Cube maps not supported with 3D types.");
279 }
280 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800281 if (mDimY > 0) {
282 if (mDimX < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800283 throw new RSInvalidStateException("X dimension required when Y is present.");
284 }
285 }
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800286 if (mDimFaces) {
287 if (mDimY < 1) {
Jason Samsa1b13ed2010-11-12 14:58:37 -0800288 throw new RSInvalidStateException("Cube maps require 2D Types.");
289 }
290 }
291
Jason Samse07694b2012-04-03 15:36:36 -0700292 int id = mRS.nTypeCreate(mElement.getID(mRS),
293 mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800294 Type t = new Type(id, mRS);
295 t.mElement = mElement;
296 t.mDimX = mDimX;
297 t.mDimY = mDimY;
298 t.mDimZ = mDimZ;
299 t.mDimMipmaps = mDimMipmaps;
300 t.mDimFaces = mDimFaces;
301
Jason Sams768bc022009-09-21 19:41:04 -0700302 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700303 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700304 }
305 }
306
307}