blob: c0f82dc31453c2e64c49af68caad293150a52b88 [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");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800190 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700191 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");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800411 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700412 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
413 fprintf(f, "\n");
414 fprintf(f, "namespace android {\n");
415 fprintf(f, "namespace renderscript {\n");
416 fprintf(f, "\n");
417
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800418 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700419 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800420 int needFlush = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700421
Jason Sams186e5912011-04-26 14:50:00 -0700422 if (api->direct) {
423 continue;
424 }
425
Jason Sams20087472011-05-06 14:14:30 -0700426 fprintf(f, "void rsp_%s(Context *con, const void *vp, size_t cmdSizeBytes) {\n", api->name);
Jason Sams186e5912011-04-26 14:50:00 -0700427 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 -0700428
Jason Sams5f27d6f2012-02-07 15:32:08 -0800429 if (hasInlineDataPointers(api)) {
430 fprintf(f, " const uint8_t *baseData = 0;\n");
431 fprintf(f, " if (cmdSizeBytes != sizeof(RS_CMD_%s)) {\n", api->name);
432 fprintf(f, " baseData = &((const uint8_t *)vp)[sizeof(*cmd)];\n");
433 fprintf(f, " }\n");
434 }
435
Jason Sams186e5912011-04-26 14:50:00 -0700436 fprintf(f, " ");
437 if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700438 fprintf(f, "\n ");
439 printVarType(f, &api->ret);
440 fprintf(f, " ret = ");
Jason Sams326e0dd2009-05-22 14:03:28 -0700441 }
Jason Sams186e5912011-04-26 14:50:00 -0700442 fprintf(f, "rsi_%s(con", api->name);
443 for (ct2=0; ct2 < api->paramCount; ct2++) {
444 const VarType *vt = &api->params[ct2];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800445 needFlush += vt->ptrLevel;
446
447 if (hasInlineDataPointers(api) && vt->ptrLevel) {
448 fprintf(f, ",\n (const %s *)&baseData[(intptr_t)cmd->%s]", vt->typeName, vt->name);
449 } else {
450 fprintf(f, ",\n cmd->%s", vt->name);
451 }
Jason Sams186e5912011-04-26 14:50:00 -0700452 }
453 fprintf(f, ");\n");
454
Jason Sams5f27d6f2012-02-07 15:32:08 -0800455 if (hasInlineDataPointers(api)) {
Jason Samsee5cf002012-02-07 18:54:03 -0800456 fprintf(f, " size_t totalSize = 0;\n");
457 for (ct2=0; ct2 < api->paramCount; ct2++) {
458 if (api->params[ct2].ptrLevel) {
459 fprintf(f, " totalSize += cmd->%s_length;\n", api->params[ct2].name);
460 }
461 }
462
463 fprintf(f, " if ((totalSize != 0) && (cmdSizeBytes == sizeof(RS_CMD_%s))) {\n", api->name);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800464 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
465 fprintf(f, " }\n");
466 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700467 fprintf(f, " con->mIO.coreSetReturn(&ret, sizeof(ret));\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800468 } else if (api->sync || needFlush) {
469 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700470 }
471
Jason Sams326e0dd2009-05-22 14:03:28 -0700472 fprintf(f, "};\n\n");
473 }
474
Jason Sams20087472011-05-06 14:14:30 -0700475 for (ct=0; ct < apiCount; ct++) {
476 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800477 int needFlush = 0;
Jason Sams20087472011-05-06 14:14:30 -0700478
Jason Samsbda75a92012-02-16 17:21:32 -0800479 fprintf(f, "void rspr_%s(Context *con, ThreadIO *io) {\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700480 fprintf(f, " RS_CMD_%s cmd;\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700481
482 for (ct2=0; ct2 < api->paramCount; ct2++) {
483 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800484 if (vt->ptrLevel == 0) {
485 fprintf(f, " io->coreRead(&cmd.%s, sizeof(cmd.%s));\n", vt->name, vt->name);
486 }
487 }
488 fprintf(f, "\n");
489
490 for (ct2=0; ct2 < api->paramCount; ct2++) {
491 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700492 if (vt->ptrLevel == 1) {
493 fprintf(f, " cmd.%s = (", vt->name);
494 printVarType(f, vt);
Jason Samsbda75a92012-02-16 17:21:32 -0800495 fprintf(f, ")malloc(cmd.%s_length);\n", vt->name);
496
497 if (vt->isConst) {
498 fprintf(f, " if (cmd.%s_length) io->coreRead((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name, vt->name);
499 }
Jason Sams20087472011-05-06 14:14:30 -0700500 }
Jason Samsbda75a92012-02-16 17:21:32 -0800501 }
502 fprintf(f, "\n");
503
504 for (ct2=0; ct2 < api->paramCount; ct2++) {
505 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700506 if (vt->ptrLevel == 2) {
Jason Sams20087472011-05-06 14:14:30 -0700507 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 -0800508 fprintf(f, " cmd.%s = (", vt->name);
509 printVarType(f, vt);
510 fprintf(f, ")malloc(cmd.%s_length[ct]);\n", vt->name);
511 fprintf(f, " io->coreRead(& cmd.%s, cmd.%s_length[ct]);\n", vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700512 fprintf(f, " }\n");
Jason Sams20087472011-05-06 14:14:30 -0700513 }
514 }
515 fprintf(f, "\n");
516
517 if (api->ret.typeName[0]) {
518 fprintf(f, " ");
519 printVarType(f, &api->ret);
520 fprintf(f, " ret =\n");
521 }
522
523 fprintf(f, " rsi_%s(", api->name);
524 if (!api->nocontext) {
525 fprintf(f, "con");
526 }
527 for (ct2=0; ct2 < api->paramCount; ct2++) {
528 const VarType *vt = &api->params[ct2];
529 if (ct2 > 0 || !api->nocontext) {
530 fprintf(f, ",\n");
531 }
532 fprintf(f, " cmd.%s", vt->name);
533 }
534 fprintf(f, ");\n");
535
Jason Samsbda75a92012-02-16 17:21:32 -0800536 for (ct2=0; ct2 < api->paramCount; ct2++) {
537 const VarType *vt = &api->params[ct2];
538 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
539 fprintf(f, " io->coreSetReturn((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name);
540 }
541 }
542
543 for (ct2=0; ct2 < api->paramCount; ct2++) {
544 const VarType *vt = &api->params[ct2];
545 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
546 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
547 fprintf(f, " io->coreSetReturn((void *)cmd.%s[ct], cmd.%s_length[ct]);\n", vt->name, vt->name);
548 fprintf(f, " }\n");
549 }
550 }
551 fprintf(f, "\n");
552
Jason Sams20087472011-05-06 14:14:30 -0700553 if (api->ret.typeName[0]) {
Jason Samsbda75a92012-02-16 17:21:32 -0800554 fprintf(f, " io->coreSetReturn(&ret, sizeof(ret));\n");
555 } else /*if (needFlush)*/ {
556 fprintf(f, " io->coreSetReturn(NULL, 0);\n");
557 }
558
559 for (ct2=0; ct2 < api->paramCount; ct2++) {
560 const VarType *vt = &api->params[ct2];
561 if (vt->ptrLevel == 1) {
562 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
563 }
564 }
565 for (ct2=0; ct2 < api->paramCount; ct2++) {
566 const VarType *vt = &api->params[ct2];
567 if (vt->ptrLevel == 2) {
568 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
569 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
570 fprintf(f, " }\n");
571 }
Jason Sams20087472011-05-06 14:14:30 -0700572 }
573
574 fprintf(f, "};\n\n");
575 }
576
577 fprintf(f, "RsPlaybackLocalFunc gPlaybackFuncs[%i] = {\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700578 fprintf(f, " NULL,\n");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800579 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700580 if (apis[ct].direct) {
581 fprintf(f, " NULL,\n");
582 } else {
583 fprintf(f, " %s%s,\n", "rsp_", apis[ct].name);
584 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700585 }
586 fprintf(f, "};\n");
587
Jason Sams20087472011-05-06 14:14:30 -0700588 fprintf(f, "RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i] = {\n", apiCount + 1);
589 fprintf(f, " NULL,\n");
590 for (ct=0; ct < apiCount; ct++) {
591 fprintf(f, " %s%s,\n", "rspr_", apis[ct].name);
592 }
593 fprintf(f, "};\n");
594
Jason Sams326e0dd2009-05-22 14:03:28 -0700595 fprintf(f, "};\n");
596 fprintf(f, "};\n");
597}
598
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700599void yylex();
600
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800601int main(int argc, char **argv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700602 if (argc != 3) {
603 fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]);
604 return 1;
605 }
606 const char* rsgFile = argv[1];
607 const char* outFile = argv[2];
608 FILE* input = fopen(rsgFile, "r");
609
610 char choice = fgetc(input);
611 fclose(input);
612
613 if (choice < '0' || choice > '3') {
614 fprintf(stderr, "Uknown command: \'%c\'\n", choice);
615 return -2;
616 }
617
618 yylex();
619 // printf("# of lines = %d\n", num_lines);
620
621 FILE *f = fopen(outFile, "w");
622
623 printFileHeader(f);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800624 switch (choice) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700625 case '0': // rsgApiStructs.h
626 {
627 fprintf(f, "\n");
628 fprintf(f, "#include \"rsContext.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700629 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700630 fprintf(f, "\n");
631 fprintf(f, "namespace android {\n");
632 fprintf(f, "namespace renderscript {\n");
633 printStructures(f);
634 printFuncDecls(f, "rsi_", 1);
635 printPlaybackFuncs(f, "rsp_");
Jason Sams20087472011-05-06 14:14:30 -0700636 fprintf(f, "\n\ntypedef struct RsPlaybackRemoteHeaderRec {\n");
637 fprintf(f, " uint32_t command;\n");
638 fprintf(f, " uint32_t size;\n");
639 fprintf(f, "} RsPlaybackRemoteHeader;\n\n");
640 fprintf(f, "typedef void (*RsPlaybackLocalFunc)(Context *, const void *, size_t sizeBytes);\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800641 fprintf(f, "typedef void (*RsPlaybackRemoteFunc)(Context *, ThreadIO *);\n");
Jason Sams20087472011-05-06 14:14:30 -0700642 fprintf(f, "extern RsPlaybackLocalFunc gPlaybackFuncs[%i];\n", apiCount + 1);
643 fprintf(f, "extern RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i];\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700644
645 fprintf(f, "}\n");
646 fprintf(f, "}\n");
647 }
648 break;
649
650 case '1': // rsgApiFuncDecl.h
651 {
652 printFuncDecls(f, "rs", 0);
653 }
654 break;
655
656 case '2': // rsgApi.cpp
657 {
658 printApiCpp(f);
659 }
660 break;
661
662 case '3': // rsgApiReplay.cpp
663 {
664 printFileHeader(f);
665 printPlaybackCpp(f);
666 }
667 break;
Jason Samsc975cf42011-04-28 18:26:48 -0700668
669 case '4': // rsgApiStream.cpp
670 {
671 printFileHeader(f);
672 printPlaybackCpp(f);
673 }
674
675 case '5': // rsgApiStreamReplay.cpp
676 {
677 printFileHeader(f);
678 printPlaybackCpp(f);
679 }
680 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700681 }
682 fclose(f);
683 return 0;
684}