Type.java revision d5f06300341df0990be3e0b7a26fa49b13c6fc19
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    public Element getElement() {
37        return mElement;
38    }
39
40    public int getX() {
41        return mDimX;
42    }
43    public int getY() {
44        return mDimY;
45    }
46    public int getZ() {
47        return mDimZ;
48    }
49    public boolean getLOD() {
50        return mDimLOD;
51    }
52    public boolean getFaces() {
53        return mDimFaces;
54    }
55    public int getElementCount() {
56        return mElementCount;
57    }
58
59    void calcElementCount() {
60        boolean hasLod = getLOD();
61        int x = getX();
62        int y = getY();
63        int z = getZ();
64        int faces = 1;
65        if(getFaces()) {
66            faces = 6;
67        }
68        if(x == 0) {
69            x = 1;
70        }
71        if(y == 0) {
72            y = 1;
73        }
74        if(z == 0) {
75            z = 1;
76        }
77
78        int count = x * y * z * faces;
79        if(hasLod && (x > 1) && (y > 1) && (z > 1)) {
80            if(x > 1) {
81                x >>= 1;
82            }
83            if(y > 1) {
84                y >>= 1;
85            }
86            if(z > 1) {
87                z >>= 1;
88            }
89
90            count += x * y * z * faces;
91        }
92        mElementCount = count;
93    }
94
95
96    Type(int id, RenderScript rs) {
97        super(id, rs);
98    }
99
100    protected void finalize() throws Throwable {
101        super.finalize();
102    }
103
104    @Override
105    void updateFromNative() {
106        // We have 6 integer to obtain mDimX; mDimY; mDimZ;
107        // mDimLOD; mDimFaces; mElement;
108        int[] dataBuffer = new int[6];
109        mRS.nTypeGetNativeData(mID, dataBuffer);
110
111        mDimX = dataBuffer[0];
112        mDimY = dataBuffer[1];
113        mDimZ = dataBuffer[2];
114        mDimLOD = dataBuffer[3] == 1 ? true : false;
115        mDimFaces = dataBuffer[4] == 1 ? true : false;
116
117        int elementID = dataBuffer[5];
118        if(elementID != 0) {
119            mElement = new Element(elementID, mRS);
120            mElement.updateFromNative();
121        }
122        calcElementCount();
123    }
124
125    public static class Builder {
126        RenderScript mRS;
127        Dimension[] mDimensions;
128        int[] mDimensionValues;
129        int mEntryCount;
130        Element mElement;
131
132        class Entry {
133            Dimension mDim;
134            int mValue;
135        }
136
137        public Builder(RenderScript rs, Element e) {
138            if(e.mID == 0) {
139                throw new IllegalArgumentException("Invalid element.");
140            }
141
142            mRS = rs;
143            mDimensions = new Dimension[4];
144            mDimensionValues = new int[4];
145            mElement = e;
146        }
147
148        public void add(Dimension d, int value) {
149            if(value < 1) {
150                throw new IllegalArgumentException("Values of less than 1 for Dimensions are not valid.");
151            }
152            if(mDimensions.length >= mEntryCount) {
153                Dimension[] dn = new Dimension[mEntryCount + 8];
154                System.arraycopy(mDimensions, 0, dn, 0, mEntryCount);
155                mDimensions = dn;
156
157                int[] in = new int[mEntryCount + 8];
158                System.arraycopy(mDimensionValues, 0, in, 0, mEntryCount);
159                mDimensionValues = in;
160            }
161            mDimensions[mEntryCount] = d;
162            mDimensionValues[mEntryCount] = value;
163            mEntryCount++;
164        }
165
166        public Type create() {
167            int dims[] = new int[mEntryCount];
168            for (int ct=0; ct < mEntryCount; ct++) {
169                dims[ct] = mDimensions[ct].mID;
170            }
171
172            int id = mRS.nTypeCreate(mElement.getID(), dims, mDimensionValues);
173            Type t = new Type(id, mRS);
174            t.mElement = mElement;
175
176            for(int ct=0; ct < mEntryCount; ct++) {
177                if(mDimensions[ct] == Dimension.X) {
178                    t.mDimX = mDimensionValues[ct];
179                }
180                if(mDimensions[ct] == Dimension.Y) {
181                    t.mDimY = mDimensionValues[ct];
182                }
183                if(mDimensions[ct] == Dimension.Z) {
184                    t.mDimZ = mDimensionValues[ct];
185                }
186                if(mDimensions[ct] == Dimension.LOD) {
187                    t.mDimLOD = mDimensionValues[ct] != 0;
188                }
189                if(mDimensions[ct] == Dimension.FACE) {
190                    t.mDimFaces = mDimensionValues[ct] != 0;
191                }
192            }
193            t.calcElementCount();
194            return t;
195        }
196    }
197
198}
199