Type.java revision 3b9c52ab8c1ab240d2299358d01a8efbe392d111
1/*
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
19
20import java.lang.reflect.Field;
21import android.util.Log;
22
23/**
24 * @hide
25 *
26 **/
27public class Type extends BaseObj {
28    int mDimX;
29    int mDimY;
30    int mDimZ;
31    boolean mDimLOD;
32    boolean mDimFaces;
33    int mElementCount;
34    Element mElement;
35
36    Class mJavaClass;
37
38    public Element getElement() {
39        return mElement;
40    }
41
42    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
98    Type(int id, RenderScript rs) {
99        super(id, rs);
100    }
101
102    protected void finalize() throws Throwable {
103        super.finalize();
104    }
105
106    @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) {
121            mElement = new Element(elementID, mRS);
122            mElement.updateFromNative();
123        }
124        calcElementCount();
125    }
126
127    public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
128        android.util.Log.e("RenderScript", "Calling depricated createFromClass");
129        return null;
130    }
131
132
133    public static class Builder {
134        RenderScript mRS;
135        Dimension[] mDimensions;
136        int[] mDimensionValues;
137        int mEntryCount;
138        Element mElement;
139
140        class Entry {
141            Dimension mDim;
142            int mValue;
143        }
144
145        public Builder(RenderScript rs, Element e) {
146            if(e.mID == 0) {
147                throw new IllegalArgumentException("Invalid element.");
148            }
149
150            mRS = rs;
151            mDimensions = new Dimension[4];
152            mDimensionValues = new int[4];
153            mElement = e;
154        }
155
156        public void add(Dimension d, int value) {
157            if(value < 1) {
158                throw new IllegalArgumentException("Values of less than 1 for Dimensions are not valid.");
159            }
160            if(mDimensions.length >= mEntryCount) {
161                Dimension[] dn = new Dimension[mEntryCount + 8];
162                System.arraycopy(mDimensions, 0, dn, 0, mEntryCount);
163                mDimensions = dn;
164
165                int[] in = new int[mEntryCount + 8];
166                System.arraycopy(mDimensionValues, 0, in, 0, mEntryCount);
167                mDimensionValues = in;
168            }
169            mDimensions[mEntryCount] = d;
170            mDimensionValues[mEntryCount] = value;
171            mEntryCount++;
172        }
173
174        public Type create() {
175            int dims[] = new int[mEntryCount];
176            for (int ct=0; ct < mEntryCount; ct++) {
177                dims[ct] = mDimensions[ct].mID;
178            }
179
180            int id = mRS.nTypeCreate(mElement.getID(), dims, mDimensionValues);
181            Type t = new Type(id, mRS);
182            t.mElement = mElement;
183
184            for(int ct=0; ct < mEntryCount; ct++) {
185                if(mDimensions[ct] == Dimension.X) {
186                    t.mDimX = mDimensionValues[ct];
187                }
188                if(mDimensions[ct] == Dimension.Y) {
189                    t.mDimY = mDimensionValues[ct];
190                }
191                if(mDimensions[ct] == Dimension.Z) {
192                    t.mDimZ = mDimensionValues[ct];
193                }
194                if(mDimensions[ct] == Dimension.LOD) {
195                    t.mDimLOD = mDimensionValues[ct] != 0;
196                }
197                if(mDimensions[ct] == Dimension.FACE) {
198                    t.mDimFaces = mDimensionValues[ct] != 0;
199                }
200            }
201            t.calcElementCount();
202            return t;
203        }
204    }
205
206}
207