blob: 7022bcbd88372ae602106241328945c70000831e [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>
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -07004#include <string.h>
Jason Sams326e0dd2009-05-22 14:03:28 -07005
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -08006void printFileHeader(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -07007 fprintf(f, "/*\n");
Jason Sams186e5912011-04-26 14:50:00 -07008 fprintf(f, " * Copyright (C) 2011 The Android Open Source Project\n");
Jason Sams326e0dd2009-05-22 14:03:28 -07009 fprintf(f, " *\n");
10 fprintf(f, " * Licensed under the Apache License, Version 2.0 (the \"License\");\n");
11 fprintf(f, " * you may not use this file except in compliance with the License.\n");
12 fprintf(f, " * You may obtain a copy of the License at\n");
13 fprintf(f, " *\n");
14 fprintf(f, " * http://www.apache.org/licenses/LICENSE-2.0\n");
15 fprintf(f, " *\n");
16 fprintf(f, " * Unless required by applicable law or agreed to in writing, software\n");
17 fprintf(f, " * distributed under the License is distributed on an \"AS IS\" BASIS,\n");
18 fprintf(f, " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n");
19 fprintf(f, " * See the License for the specific language governing permissions and\n");
20 fprintf(f, " * limitations under the License.\n");
21 fprintf(f, " */\n\n");
22}
23
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080024void printVarType(FILE *f, const VarType *vt) {
Jason Sams326e0dd2009-05-22 14:03:28 -070025 int ct;
26 if (vt->isConst) {
27 fprintf(f, "const ");
28 }
29
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080030 switch (vt->type) {
Jason Sams326e0dd2009-05-22 14:03:28 -070031 case 0:
32 fprintf(f, "void");
33 break;
34 case 1:
35 fprintf(f, "int%i_t", vt->bits);
36 break;
37 case 2:
38 fprintf(f, "uint%i_t", vt->bits);
39 break;
40 case 3:
41 if (vt->bits == 32)
42 fprintf(f, "float");
43 else
44 fprintf(f, "double");
45 break;
46 case 4:
Joe Onorato84614dd2009-08-10 15:01:51 -070047 fprintf(f, "%s", vt->typeName);
Jason Sams326e0dd2009-05-22 14:03:28 -070048 break;
49 }
50
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080051 if (vt->ptrLevel) {
Jason Sams326e0dd2009-05-22 14:03:28 -070052 fprintf(f, " ");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080053 for (ct=0; ct < vt->ptrLevel; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070054 fprintf(f, "*");
55 }
56 }
Jason Sams5fb1aeb2011-04-27 15:12:49 -070057}
58
59void printVarTypeAndName(FILE *f, const VarType *vt) {
60 printVarType(f, vt);
Jason Sams326e0dd2009-05-22 14:03:28 -070061
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080062 if (vt->name[0]) {
Jason Sams326e0dd2009-05-22 14:03:28 -070063 fprintf(f, " %s", vt->name);
64 }
65}
66
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080067void printArgList(FILE *f, const ApiEntry * api, int assumePrevious) {
Jason Sams326e0dd2009-05-22 14:03:28 -070068 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080069 for (ct=0; ct < api->paramCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070070 if (ct || assumePrevious) {
71 fprintf(f, ", ");
72 }
Jason Sams5fb1aeb2011-04-27 15:12:49 -070073 printVarTypeAndName(f, &api->params[ct]);
Jason Sams326e0dd2009-05-22 14:03:28 -070074 }
75}
76
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080077void printStructures(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -070078 int ct;
79 int ct2;
80
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080081 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070082 fprintf(f, "typedef struct RS_CMD_%s_rec RS_CMD_%s;\n", apis[ct].name, apis[ct].name);
83 }
84 fprintf(f, "\n");
85
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080086 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070087 const ApiEntry * api = &apis[ct];
88 fprintf(f, "#define RS_CMD_ID_%s %i\n", api->name, ct+1);
89 fprintf(f, "struct RS_CMD_%s_rec {\n", api->name);
90 //fprintf(f, " RsCommandHeader _hdr;\n");
91
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080092 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070093 fprintf(f, " ");
Jason Sams5fb1aeb2011-04-27 15:12:49 -070094 printVarTypeAndName(f, &api->params[ct2]);
Jason Sams326e0dd2009-05-22 14:03:28 -070095 fprintf(f, ";\n");
96 }
97 fprintf(f, "};\n\n");
98 }
99}
100
Jason Samsc975cf42011-04-28 18:26:48 -0700101void printFuncDecl(FILE *f, const ApiEntry *api, const char *prefix, int addContext, int isFnPtr) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700102 printVarTypeAndName(f, &api->ret);
Jason Samsc975cf42011-04-28 18:26:48 -0700103 if (isFnPtr) {
104 char t[1024];
105 strcpy(t, api->name);
106 if (strlen(prefix) == 0) {
107 if (t[0] > 'A' && t[0] < 'Z') {
108 t[0] -= 'A' - 'a';
109 }
110 }
111 fprintf(f, " (* %s%s) (", prefix, api->name);
112 } else {
113 fprintf(f, " %s%s (", prefix, api->name);
114 }
Jason Sams186e5912011-04-26 14:50:00 -0700115 if (!api->nocontext) {
116 if (addContext) {
117 fprintf(f, "Context *");
118 } else {
119 fprintf(f, "RsContext rsc");
120 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700121 }
Jason Sams186e5912011-04-26 14:50:00 -0700122 printArgList(f, api, !api->nocontext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700123 fprintf(f, ")");
124}
125
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800126void printFuncDecls(FILE *f, const char *prefix, int addContext) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700127 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128 for (ct=0; ct < apiCount; ct++) {
Jason Samsc975cf42011-04-28 18:26:48 -0700129 printFuncDecl(f, &apis[ct], prefix, addContext, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700130 fprintf(f, ";\n");
131 }
132 fprintf(f, "\n\n");
133}
134
Jason Samsc975cf42011-04-28 18:26:48 -0700135void printFuncPointers(FILE *f, int addContext) {
136 fprintf(f, "\n");
137 fprintf(f, "typedef struct RsApiEntrypoints {\n");
138 int ct;
139 for (ct=0; ct < apiCount; ct++) {
140 fprintf(f, " ");
141 printFuncDecl(f, &apis[ct], "", addContext, 1);
142 fprintf(f, ";\n");
143 }
144 fprintf(f, "} RsApiEntrypoints_t;\n\n");
145}
146
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800147void printPlaybackFuncs(FILE *f, const char *prefix) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700148 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800149 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700150 if (apis[ct].direct) {
151 continue;
152 }
153
Jason Sams326e0dd2009-05-22 14:03:28 -0700154 fprintf(f, "void %s%s (Context *, const void *);\n", prefix, apis[ct].name);
155 }
156}
157
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700158static int hasInlineDataPointers(const ApiEntry * api) {
159 int ret = 0;
160 int ct;
Jason Samsb6931122011-05-02 16:29:42 -0700161 if (api->sync || api->ret.typeName[0]) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700162 return 0;
163 }
164 for (ct=0; ct < api->paramCount; ct++) {
165 const VarType *vt = &api->params[ct];
166
167 if (!vt->isConst && vt->ptrLevel) {
168 // Non-const pointers cannot be inlined.
169 return 0;
170 }
171 if (vt->ptrLevel > 1) {
172 // not handled yet.
173 return 0;
174 }
175
176 if (vt->isConst && vt->ptrLevel) {
177 // Non-const pointers cannot be inlined.
178 ret = 1;
179 }
180 }
181 return ret;
182}
183
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800184void printApiCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700185 int ct;
186 int ct2;
187
188 fprintf(f, "#include \"rsDevice.h\"\n");
189 fprintf(f, "#include \"rsContext.h\"\n");
190 fprintf(f, "#include \"rsThreadIO.h\"\n");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800191 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700192 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700193 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700194 fprintf(f, "\n");
195 fprintf(f, "using namespace android;\n");
196 fprintf(f, "using namespace android::renderscript;\n");
197 fprintf(f, "\n");
198
Jason Samsc975cf42011-04-28 18:26:48 -0700199 printFuncPointers(f, 0);
200
201 // Generate RS funcs for local fifo
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800202 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700203 int needFlush = 0;
204 const ApiEntry * api = &apis[ct];
205
Jason Samsc975cf42011-04-28 18:26:48 -0700206 fprintf(f, "static ");
207 printFuncDecl(f, api, "LF_", 0, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700208 fprintf(f, "\n{\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700209 if (api->direct) {
210 fprintf(f, " ");
211 if (api->ret.typeName[0]) {
212 fprintf(f, "return ");
213 }
214 fprintf(f, "rsi_%s(", api->name);
215 if (!api->nocontext) {
216 fprintf(f, "(Context *)rsc");
Jason Samsc975cf42011-04-28 18:26:48 -0700217 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800218 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700219 const VarType *vt = &api->params[ct2];
Jason Samsc975cf42011-04-28 18:26:48 -0700220 if (ct2 > 0 || !api->nocontext) {
221 fprintf(f, ", ");
222 }
223 fprintf(f, "%s", vt->name);
Jason Sams9397e302009-08-27 20:23:34 -0700224 }
225 fprintf(f, ");\n");
226 } else {
Tim Murray4d252d62012-11-29 14:37:59 -0800227 // handle synchronous path
228 fprintf(f, " if (((Context *)rsc)->isSynchronous()) {\n");
229 fprintf(f, " ");
230 if (api->ret.typeName[0]) {
231 fprintf(f, "return ");
232 }
233 fprintf(f, "rsi_%s(", api->name);
234 if (!api->nocontext) {
235 fprintf(f, "(Context *)rsc");
236 }
237 for (ct2=0; ct2 < api->paramCount; ct2++) {
238 const VarType *vt = &api->params[ct2];
239 if (ct2 > 0 || !api->nocontext) {
240 fprintf(f, ", ");
241 }
242 fprintf(f, "%s", vt->name);
243 }
244 fprintf(f, ");\n");
245 if (!api->ret.typeName[0]) {
246 fprintf(f, " return;");
247 }
248 fprintf(f, " }\n\n");
249
Jason Sams9397e302009-08-27 20:23:34 -0700250 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Jason Samsb6931122011-05-02 16:29:42 -0700251 fprintf(f, " const uint32_t size = sizeof(RS_CMD_%s);\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700252 if (hasInlineDataPointers(api)) {
253 fprintf(f, " uint32_t dataSize = 0;\n");
254 for (ct2=0; ct2 < api->paramCount; ct2++) {
255 const VarType *vt = &api->params[ct2];
256 if (vt->isConst && vt->ptrLevel) {
257 fprintf(f, " dataSize += %s_length;\n", vt->name);
258 }
259 }
260 }
261
Steve Blockaf12ac62012-01-06 19:20:56 +0000262 //fprintf(f, " ALOGE(\"add command %s\\n\");\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700263 if (hasInlineDataPointers(api)) {
Jason Samsb6931122011-05-02 16:29:42 -0700264 fprintf(f, " RS_CMD_%s *cmd = NULL;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800265 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {;\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700266 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 -0700267 fprintf(f, " } else {\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700268 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 -0700269 fprintf(f, " }\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700270 fprintf(f, " uint8_t *payload = (uint8_t *)&cmd[1];\n");
Jason Sams6e58aef2011-04-29 16:23:40 -0700271 } else {
Jason Sams1a4efa32011-05-17 15:01:29 -0700272 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 -0700273 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700274
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800275 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700276 const VarType *vt = &api->params[ct2];
277 needFlush += vt->ptrLevel;
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700278 if (vt->ptrLevel && hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800279 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700280 fprintf(f, " memcpy(payload, %s, %s_length);\n", vt->name, vt->name);
281 fprintf(f, " cmd->%s = (", vt->name);
282 printVarType(f, vt);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800283 fprintf(f, ")(payload - ((uint8_t *)&cmd[1]));\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700284 fprintf(f, " payload += %s_length;\n", vt->name);
285 fprintf(f, " } else {\n");
286 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
287 fprintf(f, " }\n");
288
289 } else {
290 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
291 }
Jason Sams9397e302009-08-27 20:23:34 -0700292 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700293 if (api->ret.typeName[0] || api->sync) {
Jason Sams9397e302009-08-27 20:23:34 -0700294 needFlush = 1;
295 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700296
Jason Sams5f27d6f2012-02-07 15:32:08 -0800297 fprintf(f, " io->coreCommit();\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700298 if (hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800299 fprintf(f, " if (dataSize >= io->getMaxInlineSize()) {\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800300 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700301 fprintf(f, " }\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800302 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700303 fprintf(f, "\n ");
Jason Sams20087472011-05-06 14:14:30 -0700304 printVarType(f, &api->ret);
Jason Sams1a4efa32011-05-17 15:01:29 -0700305 fprintf(f, " ret;\n");
306 fprintf(f, " io->coreGetReturn(&ret, sizeof(ret));\n");
307 fprintf(f, " return ret;\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800308 } else if (needFlush) {
309 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams9397e302009-08-27 20:23:34 -0700310 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700311 }
312 fprintf(f, "};\n\n");
Jason Sams20087472011-05-06 14:14:30 -0700313
314
Jason Samsbda75a92012-02-16 17:21:32 -0800315 // Generate a remote sender function
316 const char * str = "core";
317 if (api->direct) {
318 str = "async";
319 }
320
Jason Sams20087472011-05-06 14:14:30 -0700321 fprintf(f, "static ");
322 printFuncDecl(f, api, "RF_", 0, 0);
323 fprintf(f, "\n{\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800324 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Jason Sams20087472011-05-06 14:14:30 -0700325 fprintf(f, " const uint32_t cmdID = RS_CMD_ID_%s;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800326 fprintf(f, " io->%sWrite(&cmdID, sizeof(cmdID));\n\n", str);
327
Jason Sams1a4efa32011-05-17 15:01:29 -0700328 for (ct2=0; ct2 < api->paramCount; ct2++) {
329 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800330 if (vt->ptrLevel == 0) {
331 fprintf(f, " io->%sWrite(& %s, sizeof(%s));\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700332 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700333 }
334 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700335
Jason Sams1a4efa32011-05-17 15:01:29 -0700336 for (ct2=0; ct2 < api->paramCount; ct2++) {
337 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800338 if ((vt->ptrLevel == 1) && (vt->isConst)) {
339 fprintf(f, " io->%sWrite(%s, %s_length);\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700340 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700341 }
342 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700343
Jason Sams1a4efa32011-05-17 15:01:29 -0700344 for (ct2=0; ct2 < api->paramCount; ct2++) {
345 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800346 if ((vt->ptrLevel == 2) && (vt->isConst)) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700347 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 -0800348 fprintf(f, " io->%sWrite(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
Jason Sams1a4efa32011-05-17 15:01:29 -0700349 fprintf(f, " }\n");
350 }
351 }
Jason Samsbda75a92012-02-16 17:21:32 -0800352 fprintf(f, "\n");
353
354 for (ct2=0; ct2 < api->paramCount; ct2++) {
355 const VarType *vt = &api->params[ct2];
356 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
357 fprintf(f, " io->%sGetReturn(%s, %s_length);\n", str, vt->name, vt->name);
358 }
359 }
360 fprintf(f, "\n");
361
362 for (ct2=0; ct2 < api->paramCount; ct2++) {
363 const VarType *vt = &api->params[ct2];
364 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
365 fprintf(f, " for (size_t ct = 0; ct < (%s_length_length / sizeof(%s_length)); ct++) {\n", vt->name, vt->name);
366 fprintf(f, " io->%sGetReturn(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
367 fprintf(f, " }\n");
368 }
369 }
370 fprintf(f, "\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700371
372 if (api->ret.typeName[0]) {
373 fprintf(f, " ");
374 printVarType(f, &api->ret);
375 fprintf(f, " retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800376 fprintf(f, " io->%sGetReturn(&retValue, sizeof(retValue));\n", str);
Jason Sams1a4efa32011-05-17 15:01:29 -0700377 fprintf(f, " return retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800378 } else /*if (api->sync)*/ {
379 fprintf(f, " io->%sGetReturn(NULL, 0);\n", str);
Jason Sams20087472011-05-06 14:14:30 -0700380 }
381 fprintf(f, "}\n\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700382 }
Jason Samsc975cf42011-04-28 18:26:48 -0700383
384 fprintf(f, "\n");
385 fprintf(f, "static RsApiEntrypoints_t s_LocalTable = {\n");
386 for (ct=0; ct < apiCount; ct++) {
387 fprintf(f, " LF_%s,\n", apis[ct].name);
388 }
389 fprintf(f, "};\n");
390
Jason Sams20087472011-05-06 14:14:30 -0700391 fprintf(f, "\n");
392 fprintf(f, "static RsApiEntrypoints_t s_RemoteTable = {\n");
393 for (ct=0; ct < apiCount; ct++) {
394 fprintf(f, " RF_%s,\n", apis[ct].name);
395 }
396 fprintf(f, "};\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700397
Jason Sams20087472011-05-06 14:14:30 -0700398 fprintf(f, "static RsApiEntrypoints_t *s_CurrentTable = &s_LocalTable;\n\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700399 for (ct=0; ct < apiCount; ct++) {
400 int needFlush = 0;
401 const ApiEntry * api = &apis[ct];
402
403 printFuncDecl(f, api, "rs", 0, 0);
404 fprintf(f, "\n{\n");
405 fprintf(f, " ");
406 if (api->ret.typeName[0]) {
407 fprintf(f, "return ");
408 }
409 fprintf(f, "s_CurrentTable->%s(", api->name);
410
411 if (!api->nocontext) {
412 fprintf(f, "(Context *)rsc");
413 }
414
415 for (ct2=0; ct2 < api->paramCount; ct2++) {
416 const VarType *vt = &api->params[ct2];
417 if (ct2 > 0 || !api->nocontext) {
418 fprintf(f, ", ");
419 }
420 fprintf(f, "%s", vt->name);
421 }
422 fprintf(f, ");\n");
423 fprintf(f, "}\n\n");
424 }
425
Jason Sams326e0dd2009-05-22 14:03:28 -0700426}
427
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800428void printPlaybackCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700429 int ct;
430 int ct2;
431
432 fprintf(f, "#include \"rsDevice.h\"\n");
433 fprintf(f, "#include \"rsContext.h\"\n");
434 fprintf(f, "#include \"rsThreadIO.h\"\n");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800435 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700436 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
437 fprintf(f, "\n");
438 fprintf(f, "namespace android {\n");
439 fprintf(f, "namespace renderscript {\n");
440 fprintf(f, "\n");
441
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800442 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700443 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800444 int needFlush = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700445
Jason Sams186e5912011-04-26 14:50:00 -0700446 if (api->direct) {
447 continue;
448 }
449
Jason Sams20087472011-05-06 14:14:30 -0700450 fprintf(f, "void rsp_%s(Context *con, const void *vp, size_t cmdSizeBytes) {\n", api->name);
Jason Sams186e5912011-04-26 14:50:00 -0700451 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 -0700452
Jason Sams5f27d6f2012-02-07 15:32:08 -0800453 if (hasInlineDataPointers(api)) {
454 fprintf(f, " const uint8_t *baseData = 0;\n");
455 fprintf(f, " if (cmdSizeBytes != sizeof(RS_CMD_%s)) {\n", api->name);
456 fprintf(f, " baseData = &((const uint8_t *)vp)[sizeof(*cmd)];\n");
457 fprintf(f, " }\n");
458 }
459
Jason Sams186e5912011-04-26 14:50:00 -0700460 fprintf(f, " ");
461 if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700462 fprintf(f, "\n ");
463 printVarType(f, &api->ret);
464 fprintf(f, " ret = ");
Jason Sams326e0dd2009-05-22 14:03:28 -0700465 }
Jason Sams186e5912011-04-26 14:50:00 -0700466 fprintf(f, "rsi_%s(con", api->name);
467 for (ct2=0; ct2 < api->paramCount; ct2++) {
468 const VarType *vt = &api->params[ct2];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800469 needFlush += vt->ptrLevel;
470
471 if (hasInlineDataPointers(api) && vt->ptrLevel) {
472 fprintf(f, ",\n (const %s *)&baseData[(intptr_t)cmd->%s]", vt->typeName, vt->name);
473 } else {
474 fprintf(f, ",\n cmd->%s", vt->name);
475 }
Jason Sams186e5912011-04-26 14:50:00 -0700476 }
477 fprintf(f, ");\n");
478
Jason Sams5f27d6f2012-02-07 15:32:08 -0800479 if (hasInlineDataPointers(api)) {
Jason Samsee5cf002012-02-07 18:54:03 -0800480 fprintf(f, " size_t totalSize = 0;\n");
481 for (ct2=0; ct2 < api->paramCount; ct2++) {
482 if (api->params[ct2].ptrLevel) {
483 fprintf(f, " totalSize += cmd->%s_length;\n", api->params[ct2].name);
484 }
485 }
486
487 fprintf(f, " if ((totalSize != 0) && (cmdSizeBytes == sizeof(RS_CMD_%s))) {\n", api->name);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800488 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
489 fprintf(f, " }\n");
490 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700491 fprintf(f, " con->mIO.coreSetReturn(&ret, sizeof(ret));\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800492 } else if (api->sync || needFlush) {
493 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700494 }
495
Jason Sams326e0dd2009-05-22 14:03:28 -0700496 fprintf(f, "};\n\n");
497 }
498
Jason Sams20087472011-05-06 14:14:30 -0700499 for (ct=0; ct < apiCount; ct++) {
500 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800501 int needFlush = 0;
Jason Sams20087472011-05-06 14:14:30 -0700502
Jason Samsbda75a92012-02-16 17:21:32 -0800503 fprintf(f, "void rspr_%s(Context *con, ThreadIO *io) {\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700504 fprintf(f, " RS_CMD_%s cmd;\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700505
506 for (ct2=0; ct2 < api->paramCount; ct2++) {
507 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800508 if (vt->ptrLevel == 0) {
509 fprintf(f, " io->coreRead(&cmd.%s, sizeof(cmd.%s));\n", vt->name, vt->name);
510 }
511 }
512 fprintf(f, "\n");
513
514 for (ct2=0; ct2 < api->paramCount; ct2++) {
515 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700516 if (vt->ptrLevel == 1) {
517 fprintf(f, " cmd.%s = (", vt->name);
518 printVarType(f, vt);
Jason Samsbda75a92012-02-16 17:21:32 -0800519 fprintf(f, ")malloc(cmd.%s_length);\n", vt->name);
520
521 if (vt->isConst) {
522 fprintf(f, " if (cmd.%s_length) io->coreRead((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name, vt->name);
523 }
Jason Sams20087472011-05-06 14:14:30 -0700524 }
Jason Samsbda75a92012-02-16 17:21:32 -0800525 }
526 fprintf(f, "\n");
527
528 for (ct2=0; ct2 < api->paramCount; ct2++) {
529 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700530 if (vt->ptrLevel == 2) {
Jason Sams20087472011-05-06 14:14:30 -0700531 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 -0800532 fprintf(f, " cmd.%s = (", vt->name);
533 printVarType(f, vt);
534 fprintf(f, ")malloc(cmd.%s_length[ct]);\n", vt->name);
535 fprintf(f, " io->coreRead(& cmd.%s, cmd.%s_length[ct]);\n", vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700536 fprintf(f, " }\n");
Jason Sams20087472011-05-06 14:14:30 -0700537 }
538 }
539 fprintf(f, "\n");
540
541 if (api->ret.typeName[0]) {
542 fprintf(f, " ");
543 printVarType(f, &api->ret);
544 fprintf(f, " ret =\n");
545 }
546
547 fprintf(f, " rsi_%s(", api->name);
548 if (!api->nocontext) {
549 fprintf(f, "con");
550 }
551 for (ct2=0; ct2 < api->paramCount; ct2++) {
552 const VarType *vt = &api->params[ct2];
553 if (ct2 > 0 || !api->nocontext) {
554 fprintf(f, ",\n");
555 }
556 fprintf(f, " cmd.%s", vt->name);
557 }
558 fprintf(f, ");\n");
559
Jason Samsbda75a92012-02-16 17:21:32 -0800560 for (ct2=0; ct2 < api->paramCount; ct2++) {
561 const VarType *vt = &api->params[ct2];
562 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
563 fprintf(f, " io->coreSetReturn((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name);
564 }
565 }
566
567 for (ct2=0; ct2 < api->paramCount; ct2++) {
568 const VarType *vt = &api->params[ct2];
569 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
570 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
571 fprintf(f, " io->coreSetReturn((void *)cmd.%s[ct], cmd.%s_length[ct]);\n", vt->name, vt->name);
572 fprintf(f, " }\n");
573 }
574 }
575 fprintf(f, "\n");
576
Jason Sams20087472011-05-06 14:14:30 -0700577 if (api->ret.typeName[0]) {
Jason Samsbda75a92012-02-16 17:21:32 -0800578 fprintf(f, " io->coreSetReturn(&ret, sizeof(ret));\n");
579 } else /*if (needFlush)*/ {
580 fprintf(f, " io->coreSetReturn(NULL, 0);\n");
581 }
582
583 for (ct2=0; ct2 < api->paramCount; ct2++) {
584 const VarType *vt = &api->params[ct2];
585 if (vt->ptrLevel == 1) {
586 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
587 }
588 }
589 for (ct2=0; ct2 < api->paramCount; ct2++) {
590 const VarType *vt = &api->params[ct2];
591 if (vt->ptrLevel == 2) {
592 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
593 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
594 fprintf(f, " }\n");
595 }
Jason Sams20087472011-05-06 14:14:30 -0700596 }
597
598 fprintf(f, "};\n\n");
599 }
600
601 fprintf(f, "RsPlaybackLocalFunc gPlaybackFuncs[%i] = {\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700602 fprintf(f, " NULL,\n");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800603 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700604 if (apis[ct].direct) {
605 fprintf(f, " NULL,\n");
606 } else {
607 fprintf(f, " %s%s,\n", "rsp_", apis[ct].name);
608 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700609 }
610 fprintf(f, "};\n");
611
Jason Sams20087472011-05-06 14:14:30 -0700612 fprintf(f, "RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i] = {\n", apiCount + 1);
613 fprintf(f, " NULL,\n");
614 for (ct=0; ct < apiCount; ct++) {
615 fprintf(f, " %s%s,\n", "rspr_", apis[ct].name);
616 }
617 fprintf(f, "};\n");
618
Jason Sams326e0dd2009-05-22 14:03:28 -0700619 fprintf(f, "};\n");
620 fprintf(f, "};\n");
621}
622
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700623void yylex();
624
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800625int main(int argc, char **argv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700626 if (argc != 3) {
627 fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]);
628 return 1;
629 }
630 const char* rsgFile = argv[1];
631 const char* outFile = argv[2];
632 FILE* input = fopen(rsgFile, "r");
633
634 char choice = fgetc(input);
635 fclose(input);
636
637 if (choice < '0' || choice > '3') {
638 fprintf(stderr, "Uknown command: \'%c\'\n", choice);
639 return -2;
640 }
641
642 yylex();
643 // printf("# of lines = %d\n", num_lines);
644
645 FILE *f = fopen(outFile, "w");
646
647 printFileHeader(f);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800648 switch (choice) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700649 case '0': // rsgApiStructs.h
650 {
651 fprintf(f, "\n");
652 fprintf(f, "#include \"rsContext.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700653 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700654 fprintf(f, "\n");
655 fprintf(f, "namespace android {\n");
656 fprintf(f, "namespace renderscript {\n");
657 printStructures(f);
658 printFuncDecls(f, "rsi_", 1);
659 printPlaybackFuncs(f, "rsp_");
Jason Sams20087472011-05-06 14:14:30 -0700660 fprintf(f, "\n\ntypedef struct RsPlaybackRemoteHeaderRec {\n");
661 fprintf(f, " uint32_t command;\n");
662 fprintf(f, " uint32_t size;\n");
663 fprintf(f, "} RsPlaybackRemoteHeader;\n\n");
664 fprintf(f, "typedef void (*RsPlaybackLocalFunc)(Context *, const void *, size_t sizeBytes);\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800665 fprintf(f, "typedef void (*RsPlaybackRemoteFunc)(Context *, ThreadIO *);\n");
Jason Sams20087472011-05-06 14:14:30 -0700666 fprintf(f, "extern RsPlaybackLocalFunc gPlaybackFuncs[%i];\n", apiCount + 1);
667 fprintf(f, "extern RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i];\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700668
669 fprintf(f, "}\n");
670 fprintf(f, "}\n");
671 }
672 break;
673
674 case '1': // rsgApiFuncDecl.h
675 {
676 printFuncDecls(f, "rs", 0);
677 }
678 break;
679
680 case '2': // rsgApi.cpp
681 {
682 printApiCpp(f);
683 }
684 break;
685
686 case '3': // rsgApiReplay.cpp
687 {
688 printFileHeader(f);
689 printPlaybackCpp(f);
690 }
691 break;
Jason Samsc975cf42011-04-28 18:26:48 -0700692
693 case '4': // rsgApiStream.cpp
694 {
695 printFileHeader(f);
696 printPlaybackCpp(f);
697 }
698
699 case '5': // rsgApiStreamReplay.cpp
700 {
701 printFileHeader(f);
702 printPlaybackCpp(f);
703 }
704 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700705 }
706 fclose(f);
707 return 0;
708}