blob: c404c9cab609c00deaee5b888982bb48152a9dfc [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 {
227 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Jason Samsb6931122011-05-02 16:29:42 -0700228 fprintf(f, " const uint32_t size = sizeof(RS_CMD_%s);\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700229 if (hasInlineDataPointers(api)) {
230 fprintf(f, " uint32_t dataSize = 0;\n");
231 for (ct2=0; ct2 < api->paramCount; ct2++) {
232 const VarType *vt = &api->params[ct2];
233 if (vt->isConst && vt->ptrLevel) {
234 fprintf(f, " dataSize += %s_length;\n", vt->name);
235 }
236 }
237 }
238
Steve Blockaf12ac62012-01-06 19:20:56 +0000239 //fprintf(f, " ALOGE(\"add command %s\\n\");\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700240 if (hasInlineDataPointers(api)) {
Jason Samsb6931122011-05-02 16:29:42 -0700241 fprintf(f, " RS_CMD_%s *cmd = NULL;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800242 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {;\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700243 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 -0700244 fprintf(f, " } else {\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700245 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 -0700246 fprintf(f, " }\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700247 fprintf(f, " uint8_t *payload = (uint8_t *)&cmd[1];\n");
Jason Sams6e58aef2011-04-29 16:23:40 -0700248 } else {
Jason Sams1a4efa32011-05-17 15:01:29 -0700249 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 -0700250 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700251
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800252 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700253 const VarType *vt = &api->params[ct2];
254 needFlush += vt->ptrLevel;
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700255 if (vt->ptrLevel && hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800256 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700257 fprintf(f, " memcpy(payload, %s, %s_length);\n", vt->name, vt->name);
258 fprintf(f, " cmd->%s = (", vt->name);
259 printVarType(f, vt);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800260 fprintf(f, ")(payload - ((uint8_t *)&cmd[1]));\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700261 fprintf(f, " payload += %s_length;\n", vt->name);
262 fprintf(f, " } else {\n");
263 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
264 fprintf(f, " }\n");
265
266 } else {
267 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
268 }
Jason Sams9397e302009-08-27 20:23:34 -0700269 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700270 if (api->ret.typeName[0] || api->sync) {
Jason Sams9397e302009-08-27 20:23:34 -0700271 needFlush = 1;
272 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700273
Jason Sams5f27d6f2012-02-07 15:32:08 -0800274 fprintf(f, " io->coreCommit();\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700275 if (hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800276 fprintf(f, " if (dataSize >= io->getMaxInlineSize()) {\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800277 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700278 fprintf(f, " }\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800279 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700280 fprintf(f, "\n ");
Jason Sams20087472011-05-06 14:14:30 -0700281 printVarType(f, &api->ret);
Jason Sams1a4efa32011-05-17 15:01:29 -0700282 fprintf(f, " ret;\n");
283 fprintf(f, " io->coreGetReturn(&ret, sizeof(ret));\n");
284 fprintf(f, " return ret;\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800285 } else if (needFlush) {
286 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams9397e302009-08-27 20:23:34 -0700287 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700288 }
289 fprintf(f, "};\n\n");
Jason Sams20087472011-05-06 14:14:30 -0700290
291
Jason Samsbda75a92012-02-16 17:21:32 -0800292 // Generate a remote sender function
293 const char * str = "core";
294 if (api->direct) {
295 str = "async";
296 }
297
Jason Sams20087472011-05-06 14:14:30 -0700298 fprintf(f, "static ");
299 printFuncDecl(f, api, "RF_", 0, 0);
300 fprintf(f, "\n{\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800301 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Jason Sams20087472011-05-06 14:14:30 -0700302 fprintf(f, " const uint32_t cmdID = RS_CMD_ID_%s;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800303 fprintf(f, " io->%sWrite(&cmdID, sizeof(cmdID));\n\n", str);
304
Jason Sams1a4efa32011-05-17 15:01:29 -0700305 for (ct2=0; ct2 < api->paramCount; ct2++) {
306 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800307 if (vt->ptrLevel == 0) {
308 fprintf(f, " io->%sWrite(& %s, sizeof(%s));\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700309 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700310 }
311 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700312
Jason Sams1a4efa32011-05-17 15:01:29 -0700313 for (ct2=0; ct2 < api->paramCount; ct2++) {
314 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800315 if ((vt->ptrLevel == 1) && (vt->isConst)) {
316 fprintf(f, " io->%sWrite(%s, %s_length);\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700317 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700318 }
319 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700320
Jason Sams1a4efa32011-05-17 15:01:29 -0700321 for (ct2=0; ct2 < api->paramCount; ct2++) {
322 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800323 if ((vt->ptrLevel == 2) && (vt->isConst)) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700324 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 -0800325 fprintf(f, " io->%sWrite(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
Jason Sams1a4efa32011-05-17 15:01:29 -0700326 fprintf(f, " }\n");
327 }
328 }
Jason Samsbda75a92012-02-16 17:21:32 -0800329 fprintf(f, "\n");
330
331 for (ct2=0; ct2 < api->paramCount; ct2++) {
332 const VarType *vt = &api->params[ct2];
333 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
334 fprintf(f, " io->%sGetReturn(%s, %s_length);\n", str, vt->name, vt->name);
335 }
336 }
337 fprintf(f, "\n");
338
339 for (ct2=0; ct2 < api->paramCount; ct2++) {
340 const VarType *vt = &api->params[ct2];
341 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
342 fprintf(f, " for (size_t ct = 0; ct < (%s_length_length / sizeof(%s_length)); ct++) {\n", vt->name, vt->name);
343 fprintf(f, " io->%sGetReturn(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
344 fprintf(f, " }\n");
345 }
346 }
347 fprintf(f, "\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700348
349 if (api->ret.typeName[0]) {
350 fprintf(f, " ");
351 printVarType(f, &api->ret);
352 fprintf(f, " retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800353 fprintf(f, " io->%sGetReturn(&retValue, sizeof(retValue));\n", str);
Jason Sams1a4efa32011-05-17 15:01:29 -0700354 fprintf(f, " return retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800355 } else /*if (api->sync)*/ {
356 fprintf(f, " io->%sGetReturn(NULL, 0);\n", str);
Jason Sams20087472011-05-06 14:14:30 -0700357 }
358 fprintf(f, "}\n\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700359 }
Jason Samsc975cf42011-04-28 18:26:48 -0700360
361 fprintf(f, "\n");
362 fprintf(f, "static RsApiEntrypoints_t s_LocalTable = {\n");
363 for (ct=0; ct < apiCount; ct++) {
364 fprintf(f, " LF_%s,\n", apis[ct].name);
365 }
366 fprintf(f, "};\n");
367
Jason Sams20087472011-05-06 14:14:30 -0700368 fprintf(f, "\n");
369 fprintf(f, "static RsApiEntrypoints_t s_RemoteTable = {\n");
370 for (ct=0; ct < apiCount; ct++) {
371 fprintf(f, " RF_%s,\n", apis[ct].name);
372 }
373 fprintf(f, "};\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700374
Jason Sams20087472011-05-06 14:14:30 -0700375 fprintf(f, "static RsApiEntrypoints_t *s_CurrentTable = &s_LocalTable;\n\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700376 for (ct=0; ct < apiCount; ct++) {
377 int needFlush = 0;
378 const ApiEntry * api = &apis[ct];
379
380 printFuncDecl(f, api, "rs", 0, 0);
381 fprintf(f, "\n{\n");
382 fprintf(f, " ");
383 if (api->ret.typeName[0]) {
384 fprintf(f, "return ");
385 }
386 fprintf(f, "s_CurrentTable->%s(", api->name);
387
388 if (!api->nocontext) {
389 fprintf(f, "(Context *)rsc");
390 }
391
392 for (ct2=0; ct2 < api->paramCount; ct2++) {
393 const VarType *vt = &api->params[ct2];
394 if (ct2 > 0 || !api->nocontext) {
395 fprintf(f, ", ");
396 }
397 fprintf(f, "%s", vt->name);
398 }
399 fprintf(f, ");\n");
400 fprintf(f, "}\n\n");
401 }
402
Jason Sams326e0dd2009-05-22 14:03:28 -0700403}
404
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800405void printPlaybackCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700406 int ct;
407 int ct2;
408
409 fprintf(f, "#include \"rsDevice.h\"\n");
410 fprintf(f, "#include \"rsContext.h\"\n");
411 fprintf(f, "#include \"rsThreadIO.h\"\n");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800412 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700413 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
414 fprintf(f, "\n");
415 fprintf(f, "namespace android {\n");
416 fprintf(f, "namespace renderscript {\n");
417 fprintf(f, "\n");
418
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800419 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700420 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800421 int needFlush = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700422
Jason Sams186e5912011-04-26 14:50:00 -0700423 if (api->direct) {
424 continue;
425 }
426
Jason Sams20087472011-05-06 14:14:30 -0700427 fprintf(f, "void rsp_%s(Context *con, const void *vp, size_t cmdSizeBytes) {\n", api->name);
Jason Sams186e5912011-04-26 14:50:00 -0700428 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 -0700429
Jason Sams5f27d6f2012-02-07 15:32:08 -0800430 if (hasInlineDataPointers(api)) {
431 fprintf(f, " const uint8_t *baseData = 0;\n");
432 fprintf(f, " if (cmdSizeBytes != sizeof(RS_CMD_%s)) {\n", api->name);
433 fprintf(f, " baseData = &((const uint8_t *)vp)[sizeof(*cmd)];\n");
434 fprintf(f, " }\n");
435 }
436
Jason Sams186e5912011-04-26 14:50:00 -0700437 fprintf(f, " ");
438 if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700439 fprintf(f, "\n ");
440 printVarType(f, &api->ret);
441 fprintf(f, " ret = ");
Jason Sams326e0dd2009-05-22 14:03:28 -0700442 }
Jason Sams186e5912011-04-26 14:50:00 -0700443 fprintf(f, "rsi_%s(con", api->name);
444 for (ct2=0; ct2 < api->paramCount; ct2++) {
445 const VarType *vt = &api->params[ct2];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800446 needFlush += vt->ptrLevel;
447
448 if (hasInlineDataPointers(api) && vt->ptrLevel) {
449 fprintf(f, ",\n (const %s *)&baseData[(intptr_t)cmd->%s]", vt->typeName, vt->name);
450 } else {
451 fprintf(f, ",\n cmd->%s", vt->name);
452 }
Jason Sams186e5912011-04-26 14:50:00 -0700453 }
454 fprintf(f, ");\n");
455
Jason Sams5f27d6f2012-02-07 15:32:08 -0800456 if (hasInlineDataPointers(api)) {
Jason Samsee5cf002012-02-07 18:54:03 -0800457 fprintf(f, " size_t totalSize = 0;\n");
458 for (ct2=0; ct2 < api->paramCount; ct2++) {
459 if (api->params[ct2].ptrLevel) {
460 fprintf(f, " totalSize += cmd->%s_length;\n", api->params[ct2].name);
461 }
462 }
463
464 fprintf(f, " if ((totalSize != 0) && (cmdSizeBytes == sizeof(RS_CMD_%s))) {\n", api->name);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800465 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
466 fprintf(f, " }\n");
467 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700468 fprintf(f, " con->mIO.coreSetReturn(&ret, sizeof(ret));\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800469 } else if (api->sync || needFlush) {
470 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700471 }
472
Jason Sams326e0dd2009-05-22 14:03:28 -0700473 fprintf(f, "};\n\n");
474 }
475
Jason Sams20087472011-05-06 14:14:30 -0700476 for (ct=0; ct < apiCount; ct++) {
477 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800478 int needFlush = 0;
Jason Sams20087472011-05-06 14:14:30 -0700479
Jason Samsbda75a92012-02-16 17:21:32 -0800480 fprintf(f, "void rspr_%s(Context *con, ThreadIO *io) {\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700481 fprintf(f, " RS_CMD_%s cmd;\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700482
483 for (ct2=0; ct2 < api->paramCount; ct2++) {
484 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800485 if (vt->ptrLevel == 0) {
486 fprintf(f, " io->coreRead(&cmd.%s, sizeof(cmd.%s));\n", vt->name, vt->name);
487 }
488 }
489 fprintf(f, "\n");
490
491 for (ct2=0; ct2 < api->paramCount; ct2++) {
492 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700493 if (vt->ptrLevel == 1) {
494 fprintf(f, " cmd.%s = (", vt->name);
495 printVarType(f, vt);
Jason Samsbda75a92012-02-16 17:21:32 -0800496 fprintf(f, ")malloc(cmd.%s_length);\n", vt->name);
497
498 if (vt->isConst) {
499 fprintf(f, " if (cmd.%s_length) io->coreRead((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name, vt->name);
500 }
Jason Sams20087472011-05-06 14:14:30 -0700501 }
Jason Samsbda75a92012-02-16 17:21:32 -0800502 }
503 fprintf(f, "\n");
504
505 for (ct2=0; ct2 < api->paramCount; ct2++) {
506 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700507 if (vt->ptrLevel == 2) {
Jason Sams20087472011-05-06 14:14:30 -0700508 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 -0800509 fprintf(f, " cmd.%s = (", vt->name);
510 printVarType(f, vt);
511 fprintf(f, ")malloc(cmd.%s_length[ct]);\n", vt->name);
512 fprintf(f, " io->coreRead(& cmd.%s, cmd.%s_length[ct]);\n", vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700513 fprintf(f, " }\n");
Jason Sams20087472011-05-06 14:14:30 -0700514 }
515 }
516 fprintf(f, "\n");
517
518 if (api->ret.typeName[0]) {
519 fprintf(f, " ");
520 printVarType(f, &api->ret);
521 fprintf(f, " ret =\n");
522 }
523
524 fprintf(f, " rsi_%s(", api->name);
525 if (!api->nocontext) {
526 fprintf(f, "con");
527 }
528 for (ct2=0; ct2 < api->paramCount; ct2++) {
529 const VarType *vt = &api->params[ct2];
530 if (ct2 > 0 || !api->nocontext) {
531 fprintf(f, ",\n");
532 }
533 fprintf(f, " cmd.%s", vt->name);
534 }
535 fprintf(f, ");\n");
536
Jason Samsbda75a92012-02-16 17:21:32 -0800537 for (ct2=0; ct2 < api->paramCount; ct2++) {
538 const VarType *vt = &api->params[ct2];
539 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
540 fprintf(f, " io->coreSetReturn((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name);
541 }
542 }
543
544 for (ct2=0; ct2 < api->paramCount; ct2++) {
545 const VarType *vt = &api->params[ct2];
546 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
547 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
548 fprintf(f, " io->coreSetReturn((void *)cmd.%s[ct], cmd.%s_length[ct]);\n", vt->name, vt->name);
549 fprintf(f, " }\n");
550 }
551 }
552 fprintf(f, "\n");
553
Jason Sams20087472011-05-06 14:14:30 -0700554 if (api->ret.typeName[0]) {
Jason Samsbda75a92012-02-16 17:21:32 -0800555 fprintf(f, " io->coreSetReturn(&ret, sizeof(ret));\n");
556 } else /*if (needFlush)*/ {
557 fprintf(f, " io->coreSetReturn(NULL, 0);\n");
558 }
559
560 for (ct2=0; ct2 < api->paramCount; ct2++) {
561 const VarType *vt = &api->params[ct2];
562 if (vt->ptrLevel == 1) {
563 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
564 }
565 }
566 for (ct2=0; ct2 < api->paramCount; ct2++) {
567 const VarType *vt = &api->params[ct2];
568 if (vt->ptrLevel == 2) {
569 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
570 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
571 fprintf(f, " }\n");
572 }
Jason Sams20087472011-05-06 14:14:30 -0700573 }
574
575 fprintf(f, "};\n\n");
576 }
577
578 fprintf(f, "RsPlaybackLocalFunc gPlaybackFuncs[%i] = {\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700579 fprintf(f, " NULL,\n");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800580 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700581 if (apis[ct].direct) {
582 fprintf(f, " NULL,\n");
583 } else {
584 fprintf(f, " %s%s,\n", "rsp_", apis[ct].name);
585 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700586 }
587 fprintf(f, "};\n");
588
Jason Sams20087472011-05-06 14:14:30 -0700589 fprintf(f, "RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i] = {\n", apiCount + 1);
590 fprintf(f, " NULL,\n");
591 for (ct=0; ct < apiCount; ct++) {
592 fprintf(f, " %s%s,\n", "rspr_", apis[ct].name);
593 }
594 fprintf(f, "};\n");
595
Jason Sams326e0dd2009-05-22 14:03:28 -0700596 fprintf(f, "};\n");
597 fprintf(f, "};\n");
598}
599
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700600void yylex();
601
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800602int main(int argc, char **argv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700603 if (argc != 3) {
604 fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]);
605 return 1;
606 }
607 const char* rsgFile = argv[1];
608 const char* outFile = argv[2];
609 FILE* input = fopen(rsgFile, "r");
610
611 char choice = fgetc(input);
612 fclose(input);
613
614 if (choice < '0' || choice > '3') {
615 fprintf(stderr, "Uknown command: \'%c\'\n", choice);
616 return -2;
617 }
618
619 yylex();
620 // printf("# of lines = %d\n", num_lines);
621
622 FILE *f = fopen(outFile, "w");
623
624 printFileHeader(f);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800625 switch (choice) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700626 case '0': // rsgApiStructs.h
627 {
628 fprintf(f, "\n");
629 fprintf(f, "#include \"rsContext.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700630 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700631 fprintf(f, "\n");
632 fprintf(f, "namespace android {\n");
633 fprintf(f, "namespace renderscript {\n");
634 printStructures(f);
635 printFuncDecls(f, "rsi_", 1);
636 printPlaybackFuncs(f, "rsp_");
Jason Sams20087472011-05-06 14:14:30 -0700637 fprintf(f, "\n\ntypedef struct RsPlaybackRemoteHeaderRec {\n");
638 fprintf(f, " uint32_t command;\n");
639 fprintf(f, " uint32_t size;\n");
640 fprintf(f, "} RsPlaybackRemoteHeader;\n\n");
641 fprintf(f, "typedef void (*RsPlaybackLocalFunc)(Context *, const void *, size_t sizeBytes);\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800642 fprintf(f, "typedef void (*RsPlaybackRemoteFunc)(Context *, ThreadIO *);\n");
Jason Sams20087472011-05-06 14:14:30 -0700643 fprintf(f, "extern RsPlaybackLocalFunc gPlaybackFuncs[%i];\n", apiCount + 1);
644 fprintf(f, "extern RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i];\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700645
646 fprintf(f, "}\n");
647 fprintf(f, "}\n");
648 }
649 break;
650
651 case '1': // rsgApiFuncDecl.h
652 {
653 printFuncDecls(f, "rs", 0);
654 }
655 break;
656
657 case '2': // rsgApi.cpp
658 {
659 printApiCpp(f);
660 }
661 break;
662
663 case '3': // rsgApiReplay.cpp
664 {
665 printFileHeader(f);
666 printPlaybackCpp(f);
667 }
668 break;
Jason Samsc975cf42011-04-28 18:26:48 -0700669
670 case '4': // rsgApiStream.cpp
671 {
672 printFileHeader(f);
673 printPlaybackCpp(f);
674 }
675
676 case '5': // rsgApiStreamReplay.cpp
677 {
678 printFileHeader(f);
679 printPlaybackCpp(f);
680 }
681 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700682 }
683 fclose(f);
684 return 0;
685}