blob: 99c305e0702c1e2bfaa178668bd74b02f8245806 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001
Joe Onorato84614dd2009-08-10 15:01:51 -07002#include "spec.h"
3#include <stdio.h>
Jason Sams326e0dd2009-05-22 14:03:28 -07004
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08005void printFileHeader(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -07006 fprintf(f, "/*\n");
Jason Sams186e5912011-04-26 14:50:00 -07007 fprintf(f, " * Copyright (C) 2011 The Android Open Source Project\n");
Jason Sams326e0dd2009-05-22 14:03:28 -07008 fprintf(f, " *\n");
9 fprintf(f, " * Licensed under the Apache License, Version 2.0 (the \"License\");\n");
10 fprintf(f, " * you may not use this file except in compliance with the License.\n");
11 fprintf(f, " * You may obtain a copy of the License at\n");
12 fprintf(f, " *\n");
13 fprintf(f, " * http://www.apache.org/licenses/LICENSE-2.0\n");
14 fprintf(f, " *\n");
15 fprintf(f, " * Unless required by applicable law or agreed to in writing, software\n");
16 fprintf(f, " * distributed under the License is distributed on an \"AS IS\" BASIS,\n");
17 fprintf(f, " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n");
18 fprintf(f, " * See the License for the specific language governing permissions and\n");
19 fprintf(f, " * limitations under the License.\n");
20 fprintf(f, " */\n\n");
21}
22
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080023void printVarType(FILE *f, const VarType *vt) {
Jason Sams326e0dd2009-05-22 14:03:28 -070024 int ct;
25 if (vt->isConst) {
26 fprintf(f, "const ");
27 }
28
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080029 switch (vt->type) {
Jason Sams326e0dd2009-05-22 14:03:28 -070030 case 0:
31 fprintf(f, "void");
32 break;
33 case 1:
34 fprintf(f, "int%i_t", vt->bits);
35 break;
36 case 2:
37 fprintf(f, "uint%i_t", vt->bits);
38 break;
39 case 3:
40 if (vt->bits == 32)
41 fprintf(f, "float");
42 else
43 fprintf(f, "double");
44 break;
45 case 4:
Joe Onorato84614dd2009-08-10 15:01:51 -070046 fprintf(f, "%s", vt->typeName);
Jason Sams326e0dd2009-05-22 14:03:28 -070047 break;
48 }
49
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080050 if (vt->ptrLevel) {
Jason Sams326e0dd2009-05-22 14:03:28 -070051 fprintf(f, " ");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080052 for (ct=0; ct < vt->ptrLevel; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070053 fprintf(f, "*");
54 }
55 }
Jason Sams5fb1aeb2011-04-27 15:12:49 -070056}
57
58void printVarTypeAndName(FILE *f, const VarType *vt) {
59 printVarType(f, vt);
Jason Sams326e0dd2009-05-22 14:03:28 -070060
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080061 if (vt->name[0]) {
Jason Sams326e0dd2009-05-22 14:03:28 -070062 fprintf(f, " %s", vt->name);
63 }
64}
65
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080066void printArgList(FILE *f, const ApiEntry * api, int assumePrevious) {
Jason Sams326e0dd2009-05-22 14:03:28 -070067 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080068 for (ct=0; ct < api->paramCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070069 if (ct || assumePrevious) {
70 fprintf(f, ", ");
71 }
Jason Sams5fb1aeb2011-04-27 15:12:49 -070072 printVarTypeAndName(f, &api->params[ct]);
Jason Sams326e0dd2009-05-22 14:03:28 -070073 }
74}
75
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080076void printStructures(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -070077 int ct;
78 int ct2;
79
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080080 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070081 fprintf(f, "typedef struct RS_CMD_%s_rec RS_CMD_%s;\n", apis[ct].name, apis[ct].name);
82 }
83 fprintf(f, "\n");
84
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080085 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070086 const ApiEntry * api = &apis[ct];
87 fprintf(f, "#define RS_CMD_ID_%s %i\n", api->name, ct+1);
88 fprintf(f, "struct RS_CMD_%s_rec {\n", api->name);
89 //fprintf(f, " RsCommandHeader _hdr;\n");
90
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080091 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070092 fprintf(f, " ");
Jason Sams5fb1aeb2011-04-27 15:12:49 -070093 printVarTypeAndName(f, &api->params[ct2]);
Jason Sams326e0dd2009-05-22 14:03:28 -070094 fprintf(f, ";\n");
95 }
96 fprintf(f, "};\n\n");
97 }
98}
99
Jason Samsc975cf42011-04-28 18:26:48 -0700100void printFuncDecl(FILE *f, const ApiEntry *api, const char *prefix, int addContext, int isFnPtr) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700101 printVarTypeAndName(f, &api->ret);
Jason Samsc975cf42011-04-28 18:26:48 -0700102 if (isFnPtr) {
103 char t[1024];
104 strcpy(t, api->name);
105 if (strlen(prefix) == 0) {
106 if (t[0] > 'A' && t[0] < 'Z') {
107 t[0] -= 'A' - 'a';
108 }
109 }
110 fprintf(f, " (* %s%s) (", prefix, api->name);
111 } else {
112 fprintf(f, " %s%s (", prefix, api->name);
113 }
Jason Sams186e5912011-04-26 14:50:00 -0700114 if (!api->nocontext) {
115 if (addContext) {
116 fprintf(f, "Context *");
117 } else {
118 fprintf(f, "RsContext rsc");
119 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700120 }
Jason Sams186e5912011-04-26 14:50:00 -0700121 printArgList(f, api, !api->nocontext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700122 fprintf(f, ")");
123}
124
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800125void printFuncDecls(FILE *f, const char *prefix, int addContext) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700126 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800127 for (ct=0; ct < apiCount; ct++) {
Jason Samsc975cf42011-04-28 18:26:48 -0700128 printFuncDecl(f, &apis[ct], prefix, addContext, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700129 fprintf(f, ";\n");
130 }
131 fprintf(f, "\n\n");
132}
133
Jason Samsc975cf42011-04-28 18:26:48 -0700134void printFuncPointers(FILE *f, int addContext) {
135 fprintf(f, "\n");
136 fprintf(f, "typedef struct RsApiEntrypoints {\n");
137 int ct;
138 for (ct=0; ct < apiCount; ct++) {
139 fprintf(f, " ");
140 printFuncDecl(f, &apis[ct], "", addContext, 1);
141 fprintf(f, ";\n");
142 }
143 fprintf(f, "} RsApiEntrypoints_t;\n\n");
144}
145
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800146void printPlaybackFuncs(FILE *f, const char *prefix) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700147 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800148 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700149 if (apis[ct].direct) {
150 continue;
151 }
152
Jason Sams326e0dd2009-05-22 14:03:28 -0700153 fprintf(f, "void %s%s (Context *, const void *);\n", prefix, apis[ct].name);
154 }
155}
156
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700157static int hasInlineDataPointers(const ApiEntry * api) {
158 int ret = 0;
159 int ct;
Jason Samsb6931122011-05-02 16:29:42 -0700160 if (api->sync || api->ret.typeName[0]) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700161 return 0;
162 }
163 for (ct=0; ct < api->paramCount; ct++) {
164 const VarType *vt = &api->params[ct];
165
166 if (!vt->isConst && vt->ptrLevel) {
167 // Non-const pointers cannot be inlined.
168 return 0;
169 }
170 if (vt->ptrLevel > 1) {
171 // not handled yet.
172 return 0;
173 }
174
175 if (vt->isConst && vt->ptrLevel) {
176 // Non-const pointers cannot be inlined.
177 ret = 1;
178 }
179 }
180 return ret;
181}
182
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800183void printApiCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700184 int ct;
185 int ct2;
186
187 fprintf(f, "#include \"rsDevice.h\"\n");
188 fprintf(f, "#include \"rsContext.h\"\n");
189 fprintf(f, "#include \"rsThreadIO.h\"\n");
190 //fprintf(f, "#include \"rsgApiStructs.h\"\n");
191 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700192 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700193 fprintf(f, "\n");
194 fprintf(f, "using namespace android;\n");
195 fprintf(f, "using namespace android::renderscript;\n");
196 fprintf(f, "\n");
197
Jason Samsc975cf42011-04-28 18:26:48 -0700198 printFuncPointers(f, 0);
199
200 // Generate RS funcs for local fifo
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800201 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700202 int needFlush = 0;
203 const ApiEntry * api = &apis[ct];
204
Jason Samsc975cf42011-04-28 18:26:48 -0700205 fprintf(f, "static ");
206 printFuncDecl(f, api, "LF_", 0, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700207 fprintf(f, "\n{\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700208 if (api->direct) {
209 fprintf(f, " ");
210 if (api->ret.typeName[0]) {
211 fprintf(f, "return ");
212 }
213 fprintf(f, "rsi_%s(", api->name);
214 if (!api->nocontext) {
215 fprintf(f, "(Context *)rsc");
Jason Samsc975cf42011-04-28 18:26:48 -0700216 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800217 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700218 const VarType *vt = &api->params[ct2];
Jason Samsc975cf42011-04-28 18:26:48 -0700219 if (ct2 > 0 || !api->nocontext) {
220 fprintf(f, ", ");
221 }
222 fprintf(f, "%s", vt->name);
Jason Sams9397e302009-08-27 20:23:34 -0700223 }
224 fprintf(f, ");\n");
225 } else {
226 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Jason Samsb6931122011-05-02 16:29:42 -0700227 fprintf(f, " const uint32_t size = sizeof(RS_CMD_%s);\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700228 if (hasInlineDataPointers(api)) {
229 fprintf(f, " uint32_t dataSize = 0;\n");
230 for (ct2=0; ct2 < api->paramCount; ct2++) {
231 const VarType *vt = &api->params[ct2];
232 if (vt->isConst && vt->ptrLevel) {
233 fprintf(f, " dataSize += %s_length;\n", vt->name);
234 }
235 }
236 }
237
Steve Blockaf12ac62012-01-06 19:20:56 +0000238 //fprintf(f, " ALOGE(\"add command %s\\n\");\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700239 if (hasInlineDataPointers(api)) {
Jason Samsb6931122011-05-02 16:29:42 -0700240 fprintf(f, " RS_CMD_%s *cmd = NULL;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800241 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {;\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700242 fprintf(f, " cmd = static_cast<RS_CMD_%s *>(io->coreHeader(RS_CMD_ID_%s, dataSize + size));\n", api->name, api->name);
Jason Samsb6931122011-05-02 16:29:42 -0700243 fprintf(f, " } else {\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700244 fprintf(f, " cmd = static_cast<RS_CMD_%s *>(io->coreHeader(RS_CMD_ID_%s, size));\n", api->name, api->name);
Jason Samsb6931122011-05-02 16:29:42 -0700245 fprintf(f, " }\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700246 fprintf(f, " uint8_t *payload = (uint8_t *)&cmd[1];\n");
Jason Sams6e58aef2011-04-29 16:23:40 -0700247 } else {
Jason Sams1a4efa32011-05-17 15:01:29 -0700248 fprintf(f, " RS_CMD_%s *cmd = static_cast<RS_CMD_%s *>(io->coreHeader(RS_CMD_ID_%s, size));\n", api->name, api->name, api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700249 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700250
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800251 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700252 const VarType *vt = &api->params[ct2];
253 needFlush += vt->ptrLevel;
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700254 if (vt->ptrLevel && hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800255 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700256 fprintf(f, " memcpy(payload, %s, %s_length);\n", vt->name, vt->name);
257 fprintf(f, " cmd->%s = (", vt->name);
258 printVarType(f, vt);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800259 fprintf(f, ")(payload - ((uint8_t *)&cmd[1]));\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700260 fprintf(f, " payload += %s_length;\n", vt->name);
261 fprintf(f, " } else {\n");
262 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
263 fprintf(f, " }\n");
264
265 } else {
266 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
267 }
Jason Sams9397e302009-08-27 20:23:34 -0700268 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700269 if (api->ret.typeName[0] || api->sync) {
Jason Sams9397e302009-08-27 20:23:34 -0700270 needFlush = 1;
271 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700272
Jason Sams5f27d6f2012-02-07 15:32:08 -0800273 fprintf(f, " io->coreCommit();\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700274 if (hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800275 fprintf(f, " if (dataSize >= io->getMaxInlineSize()) {\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800276 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700277 fprintf(f, " }\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800278 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700279 fprintf(f, "\n ");
Jason Sams20087472011-05-06 14:14:30 -0700280 printVarType(f, &api->ret);
Jason Sams1a4efa32011-05-17 15:01:29 -0700281 fprintf(f, " ret;\n");
282 fprintf(f, " io->coreGetReturn(&ret, sizeof(ret));\n");
283 fprintf(f, " return ret;\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800284 } else if (needFlush) {
285 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams9397e302009-08-27 20:23:34 -0700286 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700287 }
288 fprintf(f, "};\n\n");
Jason Sams20087472011-05-06 14:14:30 -0700289
290
Jason Samsbda75a92012-02-16 17:21:32 -0800291 // Generate a remote sender function
292 const char * str = "core";
293 if (api->direct) {
294 str = "async";
295 }
296
Jason Sams20087472011-05-06 14:14:30 -0700297 fprintf(f, "static ");
298 printFuncDecl(f, api, "RF_", 0, 0);
299 fprintf(f, "\n{\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800300 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Jason Sams20087472011-05-06 14:14:30 -0700301 fprintf(f, " const uint32_t cmdID = RS_CMD_ID_%s;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800302 fprintf(f, " io->%sWrite(&cmdID, sizeof(cmdID));\n\n", str);
303
Jason Sams1a4efa32011-05-17 15:01:29 -0700304 for (ct2=0; ct2 < api->paramCount; ct2++) {
305 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800306 if (vt->ptrLevel == 0) {
307 fprintf(f, " io->%sWrite(& %s, sizeof(%s));\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700308 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700309 }
310 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700311
Jason Sams1a4efa32011-05-17 15:01:29 -0700312 for (ct2=0; ct2 < api->paramCount; ct2++) {
313 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800314 if ((vt->ptrLevel == 1) && (vt->isConst)) {
315 fprintf(f, " io->%sWrite(%s, %s_length);\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700316 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700317 }
318 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700319
Jason Sams1a4efa32011-05-17 15:01:29 -0700320 for (ct2=0; ct2 < api->paramCount; ct2++) {
321 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800322 if ((vt->ptrLevel == 2) && (vt->isConst)) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700323 fprintf(f, " for (size_t ct = 0; ct < (%s_length_length / sizeof(%s_length)); ct++) {\n", vt->name, vt->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800324 fprintf(f, " io->%sWrite(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
Jason Sams1a4efa32011-05-17 15:01:29 -0700325 fprintf(f, " }\n");
326 }
327 }
Jason Samsbda75a92012-02-16 17:21:32 -0800328 fprintf(f, "\n");
329
330 for (ct2=0; ct2 < api->paramCount; ct2++) {
331 const VarType *vt = &api->params[ct2];
332 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
333 fprintf(f, " io->%sGetReturn(%s, %s_length);\n", str, vt->name, vt->name);
334 }
335 }
336 fprintf(f, "\n");
337
338 for (ct2=0; ct2 < api->paramCount; ct2++) {
339 const VarType *vt = &api->params[ct2];
340 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
341 fprintf(f, " for (size_t ct = 0; ct < (%s_length_length / sizeof(%s_length)); ct++) {\n", vt->name, vt->name);
342 fprintf(f, " io->%sGetReturn(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
343 fprintf(f, " }\n");
344 }
345 }
346 fprintf(f, "\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700347
348 if (api->ret.typeName[0]) {
349 fprintf(f, " ");
350 printVarType(f, &api->ret);
351 fprintf(f, " retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800352 fprintf(f, " io->%sGetReturn(&retValue, sizeof(retValue));\n", str);
Jason Sams1a4efa32011-05-17 15:01:29 -0700353 fprintf(f, " return retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800354 } else /*if (api->sync)*/ {
355 fprintf(f, " io->%sGetReturn(NULL, 0);\n", str);
Jason Sams20087472011-05-06 14:14:30 -0700356 }
357 fprintf(f, "}\n\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700358 }
Jason Samsc975cf42011-04-28 18:26:48 -0700359
360 fprintf(f, "\n");
361 fprintf(f, "static RsApiEntrypoints_t s_LocalTable = {\n");
362 for (ct=0; ct < apiCount; ct++) {
363 fprintf(f, " LF_%s,\n", apis[ct].name);
364 }
365 fprintf(f, "};\n");
366
Jason Sams20087472011-05-06 14:14:30 -0700367 fprintf(f, "\n");
368 fprintf(f, "static RsApiEntrypoints_t s_RemoteTable = {\n");
369 for (ct=0; ct < apiCount; ct++) {
370 fprintf(f, " RF_%s,\n", apis[ct].name);
371 }
372 fprintf(f, "};\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700373
Jason Sams20087472011-05-06 14:14:30 -0700374 fprintf(f, "static RsApiEntrypoints_t *s_CurrentTable = &s_LocalTable;\n\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700375 for (ct=0; ct < apiCount; ct++) {
376 int needFlush = 0;
377 const ApiEntry * api = &apis[ct];
378
379 printFuncDecl(f, api, "rs", 0, 0);
380 fprintf(f, "\n{\n");
381 fprintf(f, " ");
382 if (api->ret.typeName[0]) {
383 fprintf(f, "return ");
384 }
385 fprintf(f, "s_CurrentTable->%s(", api->name);
386
387 if (!api->nocontext) {
388 fprintf(f, "(Context *)rsc");
389 }
390
391 for (ct2=0; ct2 < api->paramCount; ct2++) {
392 const VarType *vt = &api->params[ct2];
393 if (ct2 > 0 || !api->nocontext) {
394 fprintf(f, ", ");
395 }
396 fprintf(f, "%s", vt->name);
397 }
398 fprintf(f, ");\n");
399 fprintf(f, "}\n\n");
400 }
401
Jason Sams326e0dd2009-05-22 14:03:28 -0700402}
403
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800404void printPlaybackCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700405 int ct;
406 int ct2;
407
408 fprintf(f, "#include \"rsDevice.h\"\n");
409 fprintf(f, "#include \"rsContext.h\"\n");
410 fprintf(f, "#include \"rsThreadIO.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700411 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
412 fprintf(f, "\n");
413 fprintf(f, "namespace android {\n");
414 fprintf(f, "namespace renderscript {\n");
415 fprintf(f, "\n");
416
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800417 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700418 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800419 int needFlush = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700420
Jason Sams186e5912011-04-26 14:50:00 -0700421 if (api->direct) {
422 continue;
423 }
424
Jason Sams20087472011-05-06 14:14:30 -0700425 fprintf(f, "void rsp_%s(Context *con, const void *vp, size_t cmdSizeBytes) {\n", api->name);
Jason Sams186e5912011-04-26 14:50:00 -0700426 fprintf(f, " const RS_CMD_%s *cmd = static_cast<const RS_CMD_%s *>(vp);\n", api->name, api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700427
Jason Sams5f27d6f2012-02-07 15:32:08 -0800428 if (hasInlineDataPointers(api)) {
429 fprintf(f, " const uint8_t *baseData = 0;\n");
430 fprintf(f, " if (cmdSizeBytes != sizeof(RS_CMD_%s)) {\n", api->name);
431 fprintf(f, " baseData = &((const uint8_t *)vp)[sizeof(*cmd)];\n");
432 fprintf(f, " }\n");
433 }
434
Jason Sams186e5912011-04-26 14:50:00 -0700435 fprintf(f, " ");
436 if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700437 fprintf(f, "\n ");
438 printVarType(f, &api->ret);
439 fprintf(f, " ret = ");
Jason Sams326e0dd2009-05-22 14:03:28 -0700440 }
Jason Sams186e5912011-04-26 14:50:00 -0700441 fprintf(f, "rsi_%s(con", api->name);
442 for (ct2=0; ct2 < api->paramCount; ct2++) {
443 const VarType *vt = &api->params[ct2];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800444 needFlush += vt->ptrLevel;
445
446 if (hasInlineDataPointers(api) && vt->ptrLevel) {
447 fprintf(f, ",\n (const %s *)&baseData[(intptr_t)cmd->%s]", vt->typeName, vt->name);
448 } else {
449 fprintf(f, ",\n cmd->%s", vt->name);
450 }
Jason Sams186e5912011-04-26 14:50:00 -0700451 }
452 fprintf(f, ");\n");
453
Jason Sams5f27d6f2012-02-07 15:32:08 -0800454 if (hasInlineDataPointers(api)) {
Jason Samsee5cf002012-02-07 18:54:03 -0800455 fprintf(f, " size_t totalSize = 0;\n");
456 for (ct2=0; ct2 < api->paramCount; ct2++) {
457 if (api->params[ct2].ptrLevel) {
458 fprintf(f, " totalSize += cmd->%s_length;\n", api->params[ct2].name);
459 }
460 }
461
462 fprintf(f, " if ((totalSize != 0) && (cmdSizeBytes == sizeof(RS_CMD_%s))) {\n", api->name);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800463 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
464 fprintf(f, " }\n");
465 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700466 fprintf(f, " con->mIO.coreSetReturn(&ret, sizeof(ret));\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800467 } else if (api->sync || needFlush) {
468 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700469 }
470
Jason Sams326e0dd2009-05-22 14:03:28 -0700471 fprintf(f, "};\n\n");
472 }
473
Jason Sams20087472011-05-06 14:14:30 -0700474 for (ct=0; ct < apiCount; ct++) {
475 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800476 int needFlush = 0;
Jason Sams20087472011-05-06 14:14:30 -0700477
Jason Samsbda75a92012-02-16 17:21:32 -0800478 fprintf(f, "void rspr_%s(Context *con, ThreadIO *io) {\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700479 fprintf(f, " RS_CMD_%s cmd;\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700480
481 for (ct2=0; ct2 < api->paramCount; ct2++) {
482 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800483 if (vt->ptrLevel == 0) {
484 fprintf(f, " io->coreRead(&cmd.%s, sizeof(cmd.%s));\n", vt->name, vt->name);
485 }
486 }
487 fprintf(f, "\n");
488
489 for (ct2=0; ct2 < api->paramCount; ct2++) {
490 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700491 if (vt->ptrLevel == 1) {
492 fprintf(f, " cmd.%s = (", vt->name);
493 printVarType(f, vt);
Jason Samsbda75a92012-02-16 17:21:32 -0800494 fprintf(f, ")malloc(cmd.%s_length);\n", vt->name);
495
496 if (vt->isConst) {
497 fprintf(f, " if (cmd.%s_length) io->coreRead((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name, vt->name);
498 }
Jason Sams20087472011-05-06 14:14:30 -0700499 }
Jason Samsbda75a92012-02-16 17:21:32 -0800500 }
501 fprintf(f, "\n");
502
503 for (ct2=0; ct2 < api->paramCount; ct2++) {
504 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700505 if (vt->ptrLevel == 2) {
Jason Sams20087472011-05-06 14:14:30 -0700506 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800507 fprintf(f, " cmd.%s = (", vt->name);
508 printVarType(f, vt);
509 fprintf(f, ")malloc(cmd.%s_length[ct]);\n", vt->name);
510 fprintf(f, " io->coreRead(& cmd.%s, cmd.%s_length[ct]);\n", vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700511 fprintf(f, " }\n");
Jason Sams20087472011-05-06 14:14:30 -0700512 }
513 }
514 fprintf(f, "\n");
515
516 if (api->ret.typeName[0]) {
517 fprintf(f, " ");
518 printVarType(f, &api->ret);
519 fprintf(f, " ret =\n");
520 }
521
522 fprintf(f, " rsi_%s(", api->name);
523 if (!api->nocontext) {
524 fprintf(f, "con");
525 }
526 for (ct2=0; ct2 < api->paramCount; ct2++) {
527 const VarType *vt = &api->params[ct2];
528 if (ct2 > 0 || !api->nocontext) {
529 fprintf(f, ",\n");
530 }
531 fprintf(f, " cmd.%s", vt->name);
532 }
533 fprintf(f, ");\n");
534
Jason Samsbda75a92012-02-16 17:21:32 -0800535 for (ct2=0; ct2 < api->paramCount; ct2++) {
536 const VarType *vt = &api->params[ct2];
537 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
538 fprintf(f, " io->coreSetReturn((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name);
539 }
540 }
541
542 for (ct2=0; ct2 < api->paramCount; ct2++) {
543 const VarType *vt = &api->params[ct2];
544 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
545 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
546 fprintf(f, " io->coreSetReturn((void *)cmd.%s[ct], cmd.%s_length[ct]);\n", vt->name, vt->name);
547 fprintf(f, " }\n");
548 }
549 }
550 fprintf(f, "\n");
551
Jason Sams20087472011-05-06 14:14:30 -0700552 if (api->ret.typeName[0]) {
Jason Samsbda75a92012-02-16 17:21:32 -0800553 fprintf(f, " io->coreSetReturn(&ret, sizeof(ret));\n");
554 } else /*if (needFlush)*/ {
555 fprintf(f, " io->coreSetReturn(NULL, 0);\n");
556 }
557
558 for (ct2=0; ct2 < api->paramCount; ct2++) {
559 const VarType *vt = &api->params[ct2];
560 if (vt->ptrLevel == 1) {
561 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
562 }
563 }
564 for (ct2=0; ct2 < api->paramCount; ct2++) {
565 const VarType *vt = &api->params[ct2];
566 if (vt->ptrLevel == 2) {
567 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
568 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
569 fprintf(f, " }\n");
570 }
Jason Sams20087472011-05-06 14:14:30 -0700571 }
572
573 fprintf(f, "};\n\n");
574 }
575
576 fprintf(f, "RsPlaybackLocalFunc gPlaybackFuncs[%i] = {\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700577 fprintf(f, " NULL,\n");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800578 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700579 if (apis[ct].direct) {
580 fprintf(f, " NULL,\n");
581 } else {
582 fprintf(f, " %s%s,\n", "rsp_", apis[ct].name);
583 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700584 }
585 fprintf(f, "};\n");
586
Jason Sams20087472011-05-06 14:14:30 -0700587 fprintf(f, "RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i] = {\n", apiCount + 1);
588 fprintf(f, " NULL,\n");
589 for (ct=0; ct < apiCount; ct++) {
590 fprintf(f, " %s%s,\n", "rspr_", apis[ct].name);
591 }
592 fprintf(f, "};\n");
593
Jason Sams326e0dd2009-05-22 14:03:28 -0700594 fprintf(f, "};\n");
595 fprintf(f, "};\n");
596}
597
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700598void yylex();
599
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800600int main(int argc, char **argv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700601 if (argc != 3) {
602 fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]);
603 return 1;
604 }
605 const char* rsgFile = argv[1];
606 const char* outFile = argv[2];
607 FILE* input = fopen(rsgFile, "r");
608
609 char choice = fgetc(input);
610 fclose(input);
611
612 if (choice < '0' || choice > '3') {
613 fprintf(stderr, "Uknown command: \'%c\'\n", choice);
614 return -2;
615 }
616
617 yylex();
618 // printf("# of lines = %d\n", num_lines);
619
620 FILE *f = fopen(outFile, "w");
621
622 printFileHeader(f);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800623 switch (choice) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700624 case '0': // rsgApiStructs.h
625 {
626 fprintf(f, "\n");
627 fprintf(f, "#include \"rsContext.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700628 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700629 fprintf(f, "\n");
630 fprintf(f, "namespace android {\n");
631 fprintf(f, "namespace renderscript {\n");
632 printStructures(f);
633 printFuncDecls(f, "rsi_", 1);
634 printPlaybackFuncs(f, "rsp_");
Jason Sams20087472011-05-06 14:14:30 -0700635 fprintf(f, "\n\ntypedef struct RsPlaybackRemoteHeaderRec {\n");
636 fprintf(f, " uint32_t command;\n");
637 fprintf(f, " uint32_t size;\n");
638 fprintf(f, "} RsPlaybackRemoteHeader;\n\n");
639 fprintf(f, "typedef void (*RsPlaybackLocalFunc)(Context *, const void *, size_t sizeBytes);\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800640 fprintf(f, "typedef void (*RsPlaybackRemoteFunc)(Context *, ThreadIO *);\n");
Jason Sams20087472011-05-06 14:14:30 -0700641 fprintf(f, "extern RsPlaybackLocalFunc gPlaybackFuncs[%i];\n", apiCount + 1);
642 fprintf(f, "extern RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i];\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700643
644 fprintf(f, "}\n");
645 fprintf(f, "}\n");
646 }
647 break;
648
649 case '1': // rsgApiFuncDecl.h
650 {
651 printFuncDecls(f, "rs", 0);
652 }
653 break;
654
655 case '2': // rsgApi.cpp
656 {
657 printApiCpp(f);
658 }
659 break;
660
661 case '3': // rsgApiReplay.cpp
662 {
663 printFileHeader(f);
664 printPlaybackCpp(f);
665 }
666 break;
Jason Samsc975cf42011-04-28 18:26:48 -0700667
668 case '4': // rsgApiStream.cpp
669 {
670 printFileHeader(f);
671 printPlaybackCpp(f);
672 }
673
674 case '5': // rsgApiStreamReplay.cpp
675 {
676 printFileHeader(f);
677 printPlaybackCpp(f);
678 }
679 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700680 }
681 fclose(f);
682 return 0;
683}