Element.java revision 36e612a488511940b61f09803b270aa1c61b68e0
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
20/**
21 * @hide
22 *
23 **/
24public class Element extends BaseObj {
25    final int mPredefinedID;
26    final boolean mIsPredefined;
27
28    public static final Element USER_U8 = new Element(0);
29    public static final Element USER_I8 = new Element(1);
30    public static final Element USER_U16 = new Element(2);
31    public static final Element USER_I16 = new Element(3);
32    public static final Element USER_U32 = new Element(4);
33    public static final Element USER_I32 = new Element(5);
34    public static final Element USER_FLOAT = new Element(6);
35
36    public static final Element A_8 = new Element(7);
37    public static final Element RGB_565 = new Element(8);
38    public static final Element RGB_888 = new Element(11);
39    public static final Element RGBA_5551 = new Element(9);
40    public static final Element RGBA_4444 = new Element(10);
41    public static final Element RGBA_8888 = new Element(12);
42
43    public static final Element INDEX_16 = new Element(13);
44    public static final Element INDEX_32 = new Element(14);
45    public static final Element XY_F32 = new Element(15);
46    public static final Element XYZ_F32 = new Element(16);
47    public static final Element ST_XY_F32 = new Element(17);
48    public static final Element ST_XYZ_F32 = new Element(18);
49    public static final Element NORM_XYZ_F32 = new Element(19);
50    public static final Element NORM_ST_XYZ_F32 = new Element(20);
51
52    void initPredef(RenderScript rs) {
53        mID = rs.nElementGetPredefined(mPredefinedID);
54    }
55
56    static void init(RenderScript rs) {
57        USER_U8.initPredef(rs);
58        USER_I8.initPredef(rs);
59        USER_U16.initPredef(rs);
60        USER_I16.initPredef(rs);
61        USER_U32.initPredef(rs);
62        USER_I32.initPredef(rs);
63        USER_FLOAT.initPredef(rs);
64
65        A_8.initPredef(rs);
66        RGB_565.initPredef(rs);
67        RGB_888.initPredef(rs);
68        RGBA_5551.initPredef(rs);
69        RGBA_4444.initPredef(rs);
70        RGBA_8888.initPredef(rs);
71
72        INDEX_16.initPredef(rs);
73        INDEX_32.initPredef(rs);
74        XY_F32.initPredef(rs);
75        XYZ_F32.initPredef(rs);
76        ST_XY_F32.initPredef(rs);
77        ST_XYZ_F32.initPredef(rs);
78        NORM_XYZ_F32.initPredef(rs);
79        NORM_ST_XYZ_F32.initPredef(rs);
80    }
81
82
83    public enum DataType {
84        FLOAT (0),
85        UNSIGNED (1),
86        SIGNED (2);
87
88        int mID;
89        DataType(int id) {
90            mID = id;
91        }
92    }
93
94    public enum DataKind {
95        USER (0),
96        RED (1),
97        GREEN (2),
98        BLUE (3),
99        ALPHA (4),
100        LUMINANCE (5),
101        INTENSITY (6),
102        X (7),
103        Y (8),
104        Z (9),
105        W (10),
106        S (11),
107        T (12),
108        Q (13),
109        R (14),
110        NX (15),
111        NY (16),
112        NZ (17),
113        INDEX (18);
114
115        int mID;
116        DataKind(int id) {
117            mID = id;
118        }
119    }
120
121
122    Element(int predef) {
123        super(null);
124        mID = 0;
125        mPredefinedID = predef;
126        mIsPredefined = true;
127    }
128
129    Element(int id, RenderScript rs) {
130        super(rs);
131        mID = id;
132        mPredefinedID = 0;
133        mIsPredefined = false;
134    }
135
136    public void destroy() throws IllegalStateException {
137        if(mIsPredefined) {
138            throw new IllegalStateException("Attempting to destroy a predefined Element.");
139        }
140        mRS.nElementDestroy(mID);
141        mID = 0;
142    }
143
144
145
146
147    public static class Builder {
148        RenderScript mRS;
149        boolean mActive = true;
150
151        Builder(RenderScript rs) {
152            mRS = rs;
153        }
154
155        void begin() throws IllegalStateException {
156            if (mActive) {
157                throw new IllegalStateException("Element builder already active.");
158            }
159            mRS.nElementBegin();
160            mActive = true;
161        }
162
163        public Builder add(Element e) throws IllegalArgumentException, IllegalStateException {
164            if(!mActive) {
165                throw new IllegalStateException("Element builder not active.");
166            }
167            if(!e.mIsPredefined) {
168                throw new IllegalArgumentException("add requires a predefined Element.");
169            }
170            mRS.nElementAddPredefined(e.mID);
171            return this;
172        }
173
174        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits)
175            throws IllegalStateException {
176            if(!mActive) {
177                throw new IllegalStateException("Element builder not active.");
178            }
179            int norm = 0;
180            if (isNormalized) {
181                norm = 1;
182            }
183            mRS.nElementAdd(dt.mID, dk.mID, norm, bits);
184            return this;
185        }
186
187        public void abort() throws IllegalStateException {
188            if(!mActive) {
189                throw new IllegalStateException("Element builder not active.");
190            }
191            mActive = false;
192        }
193
194        public Element create() throws IllegalStateException {
195            if(!mActive) {
196                throw new IllegalStateException("Element builder not active.");
197            }
198            int id = mRS.nElementCreate();
199            mActive = false;
200            return new Element(id, mRS);
201        }
202    }
203
204}
205
206