blob: 0169b982ca88955bae5655efcd15ecb336a221a5 [file] [log] [blame]
Jason Sams4e17e0e2010-02-23 15:27:51 -08001#define NO_RS_FUNCS 1
2
3#include "stdio.h"
4#include "RenderScript.h"
5#include <vector>
6
7struct Element;
8
9struct ElementField {
Jason Sams12b14ae2010-03-18 11:39:44 -070010 // An Element Field is a combination of an Element with a name assigned.
11
Jason Sams4e17e0e2010-02-23 15:27:51 -080012 const char *name;
13 Element *e;
Jason Sams12b14ae2010-03-18 11:39:44 -070014
15
Jason Sams4e17e0e2010-02-23 15:27:51 -080016 ElementField(const char *n, Element *_e) {
17 name = n;
18 e = _e;
19 }
20 ElementField() {
21 name = NULL;
22 e = NULL;
23 }
24};
25
26struct Element {
Jason Sams12b14ae2010-03-18 11:39:44 -070027 // An Element can take one of two forms.
28 // 1: Basic. It contains a single basic type and vector size.
29 // 2: Complex. It contains a list of fields with names. Each field
30 // will in turn be another element.
31
Jason Sams4e17e0e2010-02-23 15:27:51 -080032 ElementField *fields;
Jason Sams12b14ae2010-03-18 11:39:44 -070033 size_t fieldCount; // If field count is 0, the element is a Basic type.
Jason Sams4e17e0e2010-02-23 15:27:51 -080034 const char *name;
35 bool generated;
36
Jason Sams12b14ae2010-03-18 11:39:44 -070037 // The basic data type from RenderScript.h
Jason Sams4e17e0e2010-02-23 15:27:51 -080038 RsDataType compType;
Jason Sams12b14ae2010-03-18 11:39:44 -070039
40 // The vector size of the data type for float2, float3, ....
41 // Allowed sizes are 2,3,4,8,16
Jason Sams4e17e0e2010-02-23 15:27:51 -080042 uint32_t compVectorSize;
43
44 Element() {
45 fields = NULL;
46 fieldCount = 0;
47 name = NULL;
48 generated = false;
49 compType = RS_TYPE_ELEMENT;
50 compVectorSize = 0;
51 }
52
53 Element(uint32_t _fieldCount, const char *_name) {
54 fields = new ElementField[_fieldCount];
55 fieldCount = _fieldCount;
56 name = _name;
57 generated = false;
58 compType = RS_TYPE_ELEMENT;
59 compVectorSize = 0;
60 }
61
62 Element(RsDataType t, uint32_t s) {
63 fields = NULL;
64 fieldCount = 0;
65 name = NULL;
66 generated = false;
67 compType = t;
68 compVectorSize = s;
69 }
70
71};
72
73
74static void genHeader(FILE *f, const char *packageName)
75{
76 fprintf(f, "package %s;\n", packageName);
77 fprintf(f, "\n");
78 fprintf(f, "import android.renderscript.*;\n");
79 fprintf(f, "\n");
80 fprintf(f, "\n");
81}
82
83static const char * RSTypeToJava(RsDataType dt)
84{
85 switch(dt) {
86 //case RS_TYPE_FLOAT_16: return "float";
87 case RS_TYPE_FLOAT_32: return "float";
88 //case RS_TYPE_FLOAT_64: return "double";
89
90 case RS_TYPE_SIGNED_8: return "byte";
91 case RS_TYPE_SIGNED_16: return "short";
92 case RS_TYPE_SIGNED_32: return "int";
93 //case RS_TYPE_SIGNED_64: return "long";
94
95 case RS_TYPE_UNSIGNED_8: return "short";
96 case RS_TYPE_UNSIGNED_16: return "int";
97 case RS_TYPE_UNSIGNED_32: return "long";
98 //case RS_TYPE_UNSIGNED_64: return NULL;
99
100 //case RS_TYPE_ELEMENT: return "android.renderscript.Element";
101 //case RS_TYPE_TYPE: return "android.renderscript.Type";
102 //case RS_TYPE_ALLOCATION: return "android.renderscript.Allocation";
103 //case RS_TYPE_SAMPLER: return "android.renderscript.Sampler";
104 //case RS_TYPE_SCRIPT: return "android.renderscript.Script";
105 //case RS_TYPE_MESH: return "android.renderscript.Mesh";
106 //case RS_TYPE_PROGRAM_FRAGMENT: return "android.renderscript.ProgramFragment";
107 //case RS_TYPE_PROGRAM_VERTEX: return "android.renderscript.ProgramVertex";
108 //case RS_TYPE_PROGRAM_RASTER: return "android.renderscript.ProgramRaster";
109 //case RS_TYPE_PROGRAM_STORE: return "android.renderscript.ProgramStore";
110 default: return NULL;
111 }
112 return NULL;
113}
114
115static const char * RSTypeToString(RsDataType dt)
116{
117 switch(dt) {
118 case RS_TYPE_FLOAT_16: return "F16";
119 case RS_TYPE_FLOAT_32: return "F32";
120 case RS_TYPE_FLOAT_64: return "F64";
121
122 case RS_TYPE_SIGNED_8: return "I8";
123 case RS_TYPE_SIGNED_16: return "I16";
124 case RS_TYPE_SIGNED_32: return "I32";
125 case RS_TYPE_SIGNED_64: return "I64";
126
127 case RS_TYPE_UNSIGNED_8: return "U8";
128 case RS_TYPE_UNSIGNED_16: return "U16";
129 case RS_TYPE_UNSIGNED_32: return "U32";
130 case RS_TYPE_UNSIGNED_64: return "U64";
131
132 //case RS_TYPE_ELEMENT: return "android.renderscript.Element";
133 //case RS_TYPE_TYPE: return "android.renderscript.Type";
134 //case RS_TYPE_ALLOCATION: return "android.renderscript.Allocation";
135 //case RS_TYPE_SAMPLER: return "android.renderscript.Sampler";
136 //case RS_TYPE_SCRIPT: return "android.renderscript.Script";
137 //case RS_TYPE_MESH: return "android.renderscript.Mesh";
138 //case RS_TYPE_PROGRAM_FRAGMENT: return "android.renderscript.ProgramFragment";
139 //case RS_TYPE_PROGRAM_VERTEX: return "android.renderscript.ProgramVertex";
140 //case RS_TYPE_PROGRAM_RASTER: return "android.renderscript.ProgramRaster";
141 //case RS_TYPE_PROGRAM_STORE: return "android.renderscript.ProgramStore";
142 default: return NULL;
143 }
144 return NULL;
145}
146
147bool rsGenerateElementClass(const Element *e, const char *packageName, FILE *f)
148{
149 genHeader(f, packageName);
150
151 fprintf(f, "class Element_%s {\n", e->name);
152
153 for (size_t ct=0; ct < e->fieldCount; ct++) {
154 const char *ts = RSTypeToJava(e->fields[ct].e->compType);
155 if (ts == NULL) {
156 return false;
157 }
158 fprintf(f, " public %s %s;\n", ts, e->fields[ct].name);
159 }
160
161 fprintf(f, "\n");
162 fprintf(f, " static Element getElement(RenderScript rs) {\n");
163 fprintf(f, " Element.Builder eb = new Element.Builder(rs);\n");
164 for (size_t ct=0; ct < e->fieldCount; ct++) {
165 const char *ts = RSTypeToString(e->fields[ct].e->compType);
166 fprintf(f, " eb.add(Element.USER_%s(rs), \"%s\");\n", ts, e->fields[ct].name);
167 }
168 fprintf(f, " return eb.create();\n");
169 fprintf(f, " }\n");
170
171 fprintf(f, " static Allocation createAllocation(RenderScript rs) {\n");
172 fprintf(f, " Element e = getElement(rs);\n");
173 fprintf(f, " Allocation a = Allocation.createSized(rs, e, 1);\n");
174 fprintf(f, " return a;\n");
175 fprintf(f, " }\n");
176
177
178 fprintf(f, " void copyToAllocation(Allocation a) {\n");
179 fprintf(f, " mIOBuffer.reset();\n");
180 for (size_t ct=0; ct < e->fieldCount; ct++) {
181 const char *ts = RSTypeToString(e->fields[ct].e->compType);
182 fprintf(f, " mIOBuffer.add%s(%s);\n", ts, e->fields[ct].name);
183 }
184 fprintf(f, " a.data(mIOBuffer.getData());\n");
185 fprintf(f, " }\n");
186
187
188
189 fprintf(f, " private FieldPacker mIOBuffer[];\n");
190 fprintf(f, " public Element_%s() {\n", e->name);
191 fprintf(f, " mIOBuffer = new FieldPacker(%i);\n", 100/*element->getSizeBytes()*/);
192 fprintf(f, " }\n");
193
194
195 fprintf(f, "}\n");
196
197 return true;
198}
199
200bool rsGenerateElementClassFile(Element *e, const char *packageName)
201{
202 char buf[1024];
203 sprintf(buf, "Element_%s.java", e->name);
204 printf("Creating file %s \n", buf);
205 FILE *f = fopen(buf, "w");
206 bool ret = rsGenerateElementClass(e, packageName, f);
207 fclose(f);
208 return ret;
209}
210
211
212
213
214/*
215bool rsGenerateScriptClass(const ScriptC *script, const char *packageName, FILE *f)
216{
217 genHeader(f, packageName);
218
219 fprintf(f, "class ScriptC_%s {\n", script->getName());
220
221
222
223 ObjectBaseRef<const Type> mTypes[MAX_SCRIPT_BANKS];
224 String8 mSlotNames[MAX_SCRIPT_BANKS];
225 bool mSlotWritable[MAX_SCRIPT_BANKS];
226
227
228}
229*/
230
231
232
233int main(int argc, const char *argv)
234{
235 Element *u8 = new Element(RS_TYPE_UNSIGNED_8, 1);
236 Element *i32 = new Element(RS_TYPE_SIGNED_32, 1);
237 Element *f32 = new Element(RS_TYPE_FLOAT_32, 1);
238
239 Element *e_Pixel = new Element(4, "Pixel");
240 e_Pixel->fields[0].e = u8;
241 e_Pixel->fields[0].name = "a";
242 e_Pixel->fields[1].e = u8;
243 e_Pixel->fields[1].name = "b";
244 e_Pixel->fields[2].e = u8;
245 e_Pixel->fields[2].name = "g";
246 e_Pixel->fields[3].e = u8;
247 e_Pixel->fields[3].name = "r";
248
249 Element *e_Params = new Element(5, "Params");
250 e_Params->fields[0].e = i32;
251 e_Params->fields[0].name = "inHeight";
252 e_Params->fields[1].e = i32;
253 e_Params->fields[1].name = "inWidth";
254 e_Params->fields[2].e = i32;
255 e_Params->fields[2].name = "outHeight";
256 e_Params->fields[3].e = i32;
257 e_Params->fields[3].name = "outWidth";
258 e_Params->fields[4].e = f32;
259 e_Params->fields[4].name = "threshold";
260
261
262 printf("1\n");
263 rsGenerateElementClassFile(e_Pixel, "android");
264 rsGenerateElementClassFile(e_Params, "android");
265
266}
267