blob: 0b3db69464acc41f8118e1e36212054443591038 [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 Class mJavaClass;
Jason Sams1bada8c2009-08-09 17:01:55 -070037
Jason Samse17964e2010-01-04 16:52:27 -080038 public Element getElement() {
39 return mElement;
40 }
Jason Sams1bada8c2009-08-09 17:01:55 -070041
Jason Sams768bc022009-09-21 19:41:04 -070042 public int getX() {
43 return mDimX;
44 }
45 public int getY() {
46 return mDimY;
47 }
48 public int getZ() {
49 return mDimZ;
50 }
51 public boolean getLOD() {
52 return mDimLOD;
53 }
54 public boolean getFaces() {
55 return mDimFaces;
56 }
57 public int getElementCount() {
58 return mElementCount;
59 }
60
61 void calcElementCount() {
62 boolean hasLod = getLOD();
63 int x = getX();
64 int y = getY();
65 int z = getZ();
66 int faces = 1;
67 if(getFaces()) {
68 faces = 6;
69 }
70 if(x == 0) {
71 x = 1;
72 }
73 if(y == 0) {
74 y = 1;
75 }
76 if(z == 0) {
77 z = 1;
78 }
79
80 int count = x * y * z * faces;
81 if(hasLod && (x > 1) && (y > 1) && (z > 1)) {
82 if(x > 1) {
83 x >>= 1;
84 }
85 if(y > 1) {
86 y >>= 1;
87 }
88 if(z > 1) {
89 z >>= 1;
90 }
91
92 count += x * y * z * faces;
93 }
94 mElementCount = count;
95 }
96
97
Jason Samsb8c5a842009-07-31 20:40:47 -070098 Type(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -070099 super(id, rs);
Jason Sams43ee06852009-08-12 17:54:11 -0700100 }
101
102 protected void finalize() throws Throwable {
Jason Sams43ee06852009-08-12 17:54:11 -0700103 super.finalize();
Jason Samsb8c5a842009-07-31 20:40:47 -0700104 }
105
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700106 @Override
107 void updateFromNative() {
108 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
109 // mDimLOD; mDimFaces; mElement;
110 int[] dataBuffer = new int[6];
111 mRS.nTypeGetNativeData(mID, dataBuffer);
112
113 mDimX = dataBuffer[0];
114 mDimY = dataBuffer[1];
115 mDimZ = dataBuffer[2];
116 mDimLOD = dataBuffer[3] == 1 ? true : false;
117 mDimFaces = dataBuffer[4] == 1 ? true : false;
118
119 int elementID = dataBuffer[5];
120 if(elementID != 0) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700121 mElement = new Element(elementID, mRS);
Alex Sakhartchoukdfac8142010-07-15 11:33:03 -0700122 mElement.updateFromNative();
123 }
124 calcElementCount();
125 }
126
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700127 public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
Jason Samsb42315d2010-05-14 16:29:20 -0700128 android.util.Log.e("RenderScript", "Calling depricated createFromClass");
129 return null;
Jason Sams43ee06852009-08-12 17:54:11 -0700130 }
131
Jason Samsfbf0b9e2009-08-13 12:59:04 -0700132
Jason Samsb8c5a842009-07-31 20:40:47 -0700133 public static class Builder {
134 RenderScript mRS;
Jason Sams22534172009-08-04 16:58:20 -0700135 Entry[] mEntries;
136 int mEntryCount;
137 Element mElement;
Jason Samsb8c5a842009-07-31 20:40:47 -0700138
Jason Sams22534172009-08-04 16:58:20 -0700139 class Entry {
140 Dimension mDim;
141 int mValue;
Jason Samsb8c5a842009-07-31 20:40:47 -0700142 }
143
Jason Sams22534172009-08-04 16:58:20 -0700144 public Builder(RenderScript rs, Element e) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700145 if(e.mID == 0) {
146 throw new IllegalArgumentException("Invalid element.");
147 }
148
Jason Sams22534172009-08-04 16:58:20 -0700149 mRS = rs;
150 mEntries = new Entry[4];
151 mElement = e;
Jason Samsb8c5a842009-07-31 20:40:47 -0700152 }
153
154 public void add(Dimension d, int value) {
Jason Sams3c0dfba2009-09-27 17:50:38 -0700155 if(value < 1) {
156 throw new IllegalArgumentException("Values of less than 1 for Dimensions are not valid.");
157 }
Jason Sams22534172009-08-04 16:58:20 -0700158 if(mEntries.length >= mEntryCount) {
159 Entry[] en = new Entry[mEntryCount + 8];
Romain Guy484d57f2009-08-19 12:10:03 -0700160 System.arraycopy(mEntries, 0, en, 0, mEntries.length);
Jason Sams22534172009-08-04 16:58:20 -0700161 mEntries = en;
162 }
163 mEntries[mEntryCount] = new Entry();
164 mEntries[mEntryCount].mDim = d;
165 mEntries[mEntryCount].mValue = value;
166 mEntryCount++;
167 }
168
169 static synchronized Type internalCreate(RenderScript rs, Builder b) {
170 rs.nTypeBegin(b.mElement.mID);
171 for (int ct=0; ct < b.mEntryCount; ct++) {
172 Entry en = b.mEntries[ct];
173 rs.nTypeAdd(en.mDim.mID, en.mValue);
174 }
175 int id = rs.nTypeCreate();
176 return new Type(id, rs);
Jason Samsb8c5a842009-07-31 20:40:47 -0700177 }
178
179 public Type create() {
Jason Sams1bada8c2009-08-09 17:01:55 -0700180 Type t = internalCreate(mRS, this);
181 t.mElement = mElement;
Jason Sams768bc022009-09-21 19:41:04 -0700182
Jason Sams1bada8c2009-08-09 17:01:55 -0700183 for(int ct=0; ct < mEntryCount; ct++) {
Jason Sams768bc022009-09-21 19:41:04 -0700184 if(mEntries[ct].mDim == Dimension.X) {
185 t.mDimX = mEntries[ct].mValue;
186 }
187 if(mEntries[ct].mDim == Dimension.Y) {
188 t.mDimY = mEntries[ct].mValue;
189 }
190 if(mEntries[ct].mDim == Dimension.Z) {
191 t.mDimZ = mEntries[ct].mValue;
192 }
193 if(mEntries[ct].mDim == Dimension.LOD) {
194 t.mDimLOD = mEntries[ct].mValue != 0;
195 }
196 if(mEntries[ct].mDim == Dimension.FACE) {
197 t.mDimFaces = mEntries[ct].mValue != 0;
198 }
Jason Sams1bada8c2009-08-09 17:01:55 -0700199 }
Jason Sams768bc022009-09-21 19:41:04 -0700200 t.calcElementCount();
Jason Sams1bada8c2009-08-09 17:01:55 -0700201 return t;
Jason Samsb8c5a842009-07-31 20:40:47 -0700202 }
203 }
204
205}