blob: 8e45f2b5927cba071cd3932911b7b1ad6a438f99 [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 *
26 **/
27public class Type extends BaseObj {
Jason Sams768bc022009-09-21 19:41:04 -070028 int mDimX;
29 int mDimY;
30 int mDimZ;
31 boolean mDimLOD;
32 boolean mDimFaces;
33 int mElementCount;
Jason Sams1bada8c2009-08-09 17:01:55 -070034 Element mElement;
Jason Sams768bc022009-09-21 19:41:04 -070035
Jason Sams43ee06852009-08-12 17:54:11 -070036 private int mNativeCache;
37 Class mJavaClass;
Jason Sams1bada8c2009-08-09 17:01:55 -070038
Jason Samse17964e2010-01-04 16:52:27 -080039 public Element getElement() {
40 return mElement;
41 }
Jason Sams1bada8c2009-08-09 17:01:55 -070042
Jason Sams768bc022009-09-21 19:41:04 -070043 public int getX() {
44 return mDimX;
45 }
46 public int getY() {
47 return mDimY;
48 }
49 public int getZ() {
50 return mDimZ;
51 }
52 public boolean getLOD() {
53 return mDimLOD;
54 }
55 public boolean getFaces() {
56 return mDimFaces;
57 }
58 public int getElementCount() {
59 return mElementCount;
60 }
61
62 void calcElementCount() {
63 boolean hasLod = getLOD();
64 int x = getX();
65 int y = getY();
66 int z = getZ();
67 int faces = 1;
68 if(getFaces()) {
69 faces = 6;
70 }
71 if(x == 0) {
72 x = 1;
73 }
74 if(y == 0) {
75 y = 1;
76 }
77 if(z == 0) {
78 z = 1;
79 }
80
81 int count = x * y * z * faces;
82 if(hasLod && (x > 1) && (y > 1) && (z > 1)) {
83 if(x > 1) {
84 x >>= 1;
85 }
86 if(y > 1) {
87 y >>= 1;
88 }
89 if(z > 1) {
90 z >>= 1;
91 }
92
93 count += x * y * z * faces;
94 }
95 mElementCount = count;
96 }
97
98
Jason Samsb8c5a842009-07-31 20:40:47 -070099 Type(int id, RenderScript rs) {
100 super(rs);
101 mID = id;
Jason Sams43ee06852009-08-12 17:54:11 -0700102 mNativeCache = 0;
103 }
104
105 protected void finalize() throws Throwable {
Jason Sams1b52aae2009-08-12 18:50:44 -0700106 if(mNativeCache != 0) {
Jason Sams43ee06852009-08-12 17:54:11 -0700107 mRS.nTypeFinalDestroy(this);
108 mNativeCache = 0;
109 }
110 super.finalize();
Jason Samsb8c5a842009-07-31 20:40:47 -0700111 }
112
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700113 @Override
114 void updateFromNative() {
115 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
116 // mDimLOD; mDimFaces; mElement;
117 int[] dataBuffer = new int[6];
118 mRS.nTypeGetNativeData(mID, dataBuffer);
119
120 mDimX = dataBuffer[0];
121 mDimY = dataBuffer[1];
122 mDimZ = dataBuffer[2];
123 mDimLOD = dataBuffer[3] == 1 ? true : false;
124 mDimFaces = dataBuffer[4] == 1 ? true : false;
125
126 int elementID = dataBuffer[5];
127 if(elementID != 0) {
128 mElement = new Element(mRS, elementID);
129 mElement.updateFromNative();
130 }
131 calcElementCount();
132 }
133
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700134 public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
Jason Samsb42315d2010-05-14 16:29:20 -0700135 android.util.Log.e("RenderScript", "Calling depricated createFromClass");
136 return null;
Jason Sams43ee06852009-08-12 17:54:11 -0700137 }
138
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700139
Jason Samsb8c5a842009-07-31 20:40:47 -0700140 public static class Builder {
141 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700142 Entry[] mEntries;
143 int mEntryCount;
144 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700145
Jason Sams22534172009-08-04 16:58:20 -0700146 class Entry {
147 Dimension mDim;
148 int mValue;
Jason Samsb8c5a842009-07-31 20:40:47 -0700149 }
150
Jason Sams22534172009-08-04 16:58:20 -0700151 public Builder(RenderScript rs, Element e) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700152 if(e.mID == 0) {
153 throw new IllegalArgumentException("Invalid element.");
154 }
155
Jason Sams22534172009-08-04 16:58:20 -0700156 mRS = rs;
157 mEntries = new Entry[4];
158 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700159 }
160
161 public void add(Dimension d, int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700162 if(value < 1) {
163 throw new IllegalArgumentException("Values of less than 1 for Dimensions are not valid.");
164 }
Jason Sams22534172009-08-04 16:58:20 -0700165 if(mEntries.length >= mEntryCount) {
166 Entry[] en = new Entry[mEntryCount + 8];
Romain Guy484d57f2009-08-19 12:10:03 -0700167 System.arraycopy(mEntries, 0, en, 0, mEntries.length);
Jason Sams22534172009-08-04 16:58:20 -0700168 mEntries = en;
169 }
170 mEntries[mEntryCount] = new Entry();
171 mEntries[mEntryCount].mDim = d;
172 mEntries[mEntryCount].mValue = value;
173 mEntryCount++;
174 }
175
176 static synchronized Type internalCreate(RenderScript rs, Builder b) {
177 rs.nTypeBegin(b.mElement.mID);
178 for (int ct=0; ct < b.mEntryCount; ct++) {
179 Entry en = b.mEntries[ct];
180 rs.nTypeAdd(en.mDim.mID, en.mValue);
181 }
182 int id = rs.nTypeCreate();
183 return new Type(id, rs);
Jason Samsb8c5a842009-07-31 20:40:47 -0700184 }
185
186 public Type create() {
Jason Sams1bada8c2009-08-09 17:01:55 -0700187 Type t = internalCreate(mRS, this);
188 t.mElement = mElement;
Jason Sams768bc022009-09-21 19:41:04 -0700189
Jason Sams1bada8c2009-08-09 17:01:55 -0700190 for(int ct=0; ct < mEntryCount; ct++) {
Jason Sams768bc022009-09-21 19:41:04 -0700191 if(mEntries[ct].mDim == Dimension.X) {
192 t.mDimX = mEntries[ct].mValue;
193 }
194 if(mEntries[ct].mDim == Dimension.Y) {
195 t.mDimY = mEntries[ct].mValue;
196 }
197 if(mEntries[ct].mDim == Dimension.Z) {
198 t.mDimZ = mEntries[ct].mValue;
199 }
200 if(mEntries[ct].mDim == Dimension.LOD) {
201 t.mDimLOD = mEntries[ct].mValue != 0;
202 }
203 if(mEntries[ct].mDim == Dimension.FACE) {
204 t.mDimFaces = mEntries[ct].mValue != 0;
205 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700206 }
Jason Sams768bc022009-09-21 19:41:04 -0700207 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700208 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700209 }
210 }
211
212}