blob: be2dacccce8b1d71afbf3a703c080621bc5d3668 [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);
Stephen Hinesf0475752014-06-25 00:06:26 -070089 fprintf(f, "struct __attribute__((packed)) RS_CMD_%s_rec {\n", api->name);
Jason Sams326e0dd2009-05-22 14:03:28 -070090 //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
Tim Murrayf6023e42013-07-17 12:07:28 -0700126void printFuncDecls(FILE *f, const char *prefix, int addContext, int externC) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700127 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800128 for (ct=0; ct < apiCount; ct++) {
Tim Murrayf6023e42013-07-17 12:07:28 -0700129 if (externC) {
130 fprintf(f, "extern \"C\" ");
131 }
Jason Samsc975cf42011-04-28 18:26:48 -0700132 printFuncDecl(f, &apis[ct], prefix, addContext, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700133 fprintf(f, ";\n");
134 }
135 fprintf(f, "\n\n");
136}
137
Jason Samsc975cf42011-04-28 18:26:48 -0700138void printFuncPointers(FILE *f, int addContext) {
139 fprintf(f, "\n");
140 fprintf(f, "typedef struct RsApiEntrypoints {\n");
141 int ct;
142 for (ct=0; ct < apiCount; ct++) {
143 fprintf(f, " ");
144 printFuncDecl(f, &apis[ct], "", addContext, 1);
145 fprintf(f, ";\n");
146 }
147 fprintf(f, "} RsApiEntrypoints_t;\n\n");
148}
149
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800150void printPlaybackFuncs(FILE *f, const char *prefix) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700151 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800152 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700153 if (apis[ct].direct) {
154 continue;
155 }
156
Jason Sams326e0dd2009-05-22 14:03:28 -0700157 fprintf(f, "void %s%s (Context *, const void *);\n", prefix, apis[ct].name);
158 }
159}
160
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700161static int hasInlineDataPointers(const ApiEntry * api) {
162 int ret = 0;
163 int ct;
Jason Samsb6931122011-05-02 16:29:42 -0700164 if (api->sync || api->ret.typeName[0]) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700165 return 0;
166 }
167 for (ct=0; ct < api->paramCount; ct++) {
168 const VarType *vt = &api->params[ct];
169
170 if (!vt->isConst && vt->ptrLevel) {
171 // Non-const pointers cannot be inlined.
172 return 0;
173 }
174 if (vt->ptrLevel > 1) {
175 // not handled yet.
176 return 0;
177 }
178
179 if (vt->isConst && vt->ptrLevel) {
180 // Non-const pointers cannot be inlined.
181 ret = 1;
182 }
183 }
184 return ret;
185}
186
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800187void printApiCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700188 int ct;
189 int ct2;
190
191 fprintf(f, "#include \"rsDevice.h\"\n");
192 fprintf(f, "#include \"rsContext.h\"\n");
193 fprintf(f, "#include \"rsThreadIO.h\"\n");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800194 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700195 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700196 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700197 fprintf(f, "\n");
198 fprintf(f, "using namespace android;\n");
199 fprintf(f, "using namespace android::renderscript;\n");
200 fprintf(f, "\n");
201
Jason Samsc975cf42011-04-28 18:26:48 -0700202 printFuncPointers(f, 0);
203
204 // Generate RS funcs for local fifo
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800205 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700206 int needFlush = 0;
207 const ApiEntry * api = &apis[ct];
208
Jason Samsc975cf42011-04-28 18:26:48 -0700209 fprintf(f, "static ");
210 printFuncDecl(f, api, "LF_", 0, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700211 fprintf(f, "\n{\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700212 if (api->direct) {
213 fprintf(f, " ");
214 if (api->ret.typeName[0]) {
215 fprintf(f, "return ");
216 }
217 fprintf(f, "rsi_%s(", api->name);
218 if (!api->nocontext) {
219 fprintf(f, "(Context *)rsc");
Jason Samsc975cf42011-04-28 18:26:48 -0700220 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800221 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700222 const VarType *vt = &api->params[ct2];
Jason Samsc975cf42011-04-28 18:26:48 -0700223 if (ct2 > 0 || !api->nocontext) {
224 fprintf(f, ", ");
225 }
226 fprintf(f, "%s", vt->name);
Jason Sams9397e302009-08-27 20:23:34 -0700227 }
228 fprintf(f, ");\n");
Tim Murrayc2eca552014-01-07 15:05:47 -0800229 } else if (api->handcodeApi) {
230 // handle handcode path
231 fprintf(f, " LF_%s_handcode(", api->name);
232 if (!api->nocontext) {
233 fprintf(f, "(Context *)rsc");
234 }
235 for (ct2=0; ct2 < api->paramCount; ct2++) {
236 const VarType *vt = &api->params[ct2];
237 if (ct2 > 0 || !api->nocontext) {
238 fprintf(f, ", ");
239 }
240 fprintf(f, "%s", vt->name);
241 }
242 fprintf(f, ");\n");
243
Jason Sams9397e302009-08-27 20:23:34 -0700244 } else {
Tim Murray4d252d62012-11-29 14:37:59 -0800245 // handle synchronous path
246 fprintf(f, " if (((Context *)rsc)->isSynchronous()) {\n");
247 fprintf(f, " ");
248 if (api->ret.typeName[0]) {
249 fprintf(f, "return ");
250 }
251 fprintf(f, "rsi_%s(", api->name);
252 if (!api->nocontext) {
253 fprintf(f, "(Context *)rsc");
254 }
255 for (ct2=0; ct2 < api->paramCount; ct2++) {
256 const VarType *vt = &api->params[ct2];
257 if (ct2 > 0 || !api->nocontext) {
258 fprintf(f, ", ");
259 }
260 fprintf(f, "%s", vt->name);
261 }
262 fprintf(f, ");\n");
263 if (!api->ret.typeName[0]) {
264 fprintf(f, " return;");
265 }
266 fprintf(f, " }\n\n");
267
Jason Sams9397e302009-08-27 20:23:34 -0700268 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Tim Murray099bc262013-03-20 16:54:03 -0700269 fprintf(f, " const size_t size = sizeof(RS_CMD_%s);\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700270 if (hasInlineDataPointers(api)) {
Tim Murray099bc262013-03-20 16:54:03 -0700271 fprintf(f, " size_t dataSize = 0;\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700272 for (ct2=0; ct2 < api->paramCount; ct2++) {
273 const VarType *vt = &api->params[ct2];
274 if (vt->isConst && vt->ptrLevel) {
275 fprintf(f, " dataSize += %s_length;\n", vt->name);
276 }
277 }
278 }
279
Steve Blockaf12ac62012-01-06 19:20:56 +0000280 //fprintf(f, " ALOGE(\"add command %s\\n\");\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700281 if (hasInlineDataPointers(api)) {
Jason Samsb6931122011-05-02 16:29:42 -0700282 fprintf(f, " RS_CMD_%s *cmd = NULL;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800283 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {;\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700284 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 -0700285 fprintf(f, " } else {\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700286 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 -0700287 fprintf(f, " }\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700288 fprintf(f, " uint8_t *payload = (uint8_t *)&cmd[1];\n");
Jason Sams6e58aef2011-04-29 16:23:40 -0700289 } else {
Jason Sams1a4efa32011-05-17 15:01:29 -0700290 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 -0700291 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700292
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800293 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700294 const VarType *vt = &api->params[ct2];
295 needFlush += vt->ptrLevel;
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700296 if (vt->ptrLevel && hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800297 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700298 fprintf(f, " memcpy(payload, %s, %s_length);\n", vt->name, vt->name);
299 fprintf(f, " cmd->%s = (", vt->name);
300 printVarType(f, vt);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800301 fprintf(f, ")(payload - ((uint8_t *)&cmd[1]));\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700302 fprintf(f, " payload += %s_length;\n", vt->name);
303 fprintf(f, " } else {\n");
304 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
305 fprintf(f, " }\n");
306
307 } else {
308 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
309 }
Jason Sams9397e302009-08-27 20:23:34 -0700310 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700311 if (api->ret.typeName[0] || api->sync) {
Jason Sams9397e302009-08-27 20:23:34 -0700312 needFlush = 1;
313 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700314
Jason Sams5f27d6f2012-02-07 15:32:08 -0800315 fprintf(f, " io->coreCommit();\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700316 if (hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800317 fprintf(f, " if (dataSize >= io->getMaxInlineSize()) {\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800318 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700319 fprintf(f, " }\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800320 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700321 fprintf(f, "\n ");
Jason Sams20087472011-05-06 14:14:30 -0700322 printVarType(f, &api->ret);
Jason Sams1a4efa32011-05-17 15:01:29 -0700323 fprintf(f, " ret;\n");
324 fprintf(f, " io->coreGetReturn(&ret, sizeof(ret));\n");
325 fprintf(f, " return ret;\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800326 } else if (needFlush) {
327 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams9397e302009-08-27 20:23:34 -0700328 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700329 }
330 fprintf(f, "};\n\n");
Jason Sams20087472011-05-06 14:14:30 -0700331
332
Jason Samsbda75a92012-02-16 17:21:32 -0800333 // Generate a remote sender function
334 const char * str = "core";
335 if (api->direct) {
336 str = "async";
337 }
338
Jason Sams20087472011-05-06 14:14:30 -0700339 fprintf(f, "static ");
340 printFuncDecl(f, api, "RF_", 0, 0);
341 fprintf(f, "\n{\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800342 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Jason Sams20087472011-05-06 14:14:30 -0700343 fprintf(f, " const uint32_t cmdID = RS_CMD_ID_%s;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800344 fprintf(f, " io->%sWrite(&cmdID, sizeof(cmdID));\n\n", str);
345
Jason Sams1a4efa32011-05-17 15:01:29 -0700346 for (ct2=0; ct2 < api->paramCount; ct2++) {
347 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800348 if (vt->ptrLevel == 0) {
349 fprintf(f, " io->%sWrite(& %s, sizeof(%s));\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700350 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700351 }
352 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700353
Jason Sams1a4efa32011-05-17 15:01:29 -0700354 for (ct2=0; ct2 < api->paramCount; ct2++) {
355 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800356 if ((vt->ptrLevel == 1) && (vt->isConst)) {
357 fprintf(f, " io->%sWrite(%s, %s_length);\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700358 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700359 }
360 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700361
Jason Sams1a4efa32011-05-17 15:01:29 -0700362 for (ct2=0; ct2 < api->paramCount; ct2++) {
363 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800364 if ((vt->ptrLevel == 2) && (vt->isConst)) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700365 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 -0800366 fprintf(f, " io->%sWrite(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
Jason Sams1a4efa32011-05-17 15:01:29 -0700367 fprintf(f, " }\n");
368 }
369 }
Jason Samsbda75a92012-02-16 17:21:32 -0800370 fprintf(f, "\n");
371
372 for (ct2=0; ct2 < api->paramCount; ct2++) {
373 const VarType *vt = &api->params[ct2];
374 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
375 fprintf(f, " io->%sGetReturn(%s, %s_length);\n", str, vt->name, vt->name);
376 }
377 }
378 fprintf(f, "\n");
379
380 for (ct2=0; ct2 < api->paramCount; ct2++) {
381 const VarType *vt = &api->params[ct2];
382 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
383 fprintf(f, " for (size_t ct = 0; ct < (%s_length_length / sizeof(%s_length)); ct++) {\n", vt->name, vt->name);
384 fprintf(f, " io->%sGetReturn(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
385 fprintf(f, " }\n");
386 }
387 }
388 fprintf(f, "\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700389
390 if (api->ret.typeName[0]) {
391 fprintf(f, " ");
392 printVarType(f, &api->ret);
393 fprintf(f, " retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800394 fprintf(f, " io->%sGetReturn(&retValue, sizeof(retValue));\n", str);
Jason Sams1a4efa32011-05-17 15:01:29 -0700395 fprintf(f, " return retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800396 } else /*if (api->sync)*/ {
397 fprintf(f, " io->%sGetReturn(NULL, 0);\n", str);
Jason Sams20087472011-05-06 14:14:30 -0700398 }
399 fprintf(f, "}\n\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700400 }
Jason Samsc975cf42011-04-28 18:26:48 -0700401
402 fprintf(f, "\n");
403 fprintf(f, "static RsApiEntrypoints_t s_LocalTable = {\n");
404 for (ct=0; ct < apiCount; ct++) {
405 fprintf(f, " LF_%s,\n", apis[ct].name);
406 }
407 fprintf(f, "};\n");
408
Jason Sams20087472011-05-06 14:14:30 -0700409 fprintf(f, "\n");
410 fprintf(f, "static RsApiEntrypoints_t s_RemoteTable = {\n");
411 for (ct=0; ct < apiCount; ct++) {
412 fprintf(f, " RF_%s,\n", apis[ct].name);
413 }
414 fprintf(f, "};\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700415
Jason Sams20087472011-05-06 14:14:30 -0700416 fprintf(f, "static RsApiEntrypoints_t *s_CurrentTable = &s_LocalTable;\n\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700417 for (ct=0; ct < apiCount; ct++) {
418 int needFlush = 0;
419 const ApiEntry * api = &apis[ct];
420
Tim Murrayf6023e42013-07-17 12:07:28 -0700421 fprintf(f, "extern \"C\" ");
422
Jason Samsc975cf42011-04-28 18:26:48 -0700423 printFuncDecl(f, api, "rs", 0, 0);
424 fprintf(f, "\n{\n");
425 fprintf(f, " ");
426 if (api->ret.typeName[0]) {
427 fprintf(f, "return ");
428 }
429 fprintf(f, "s_CurrentTable->%s(", api->name);
430
431 if (!api->nocontext) {
432 fprintf(f, "(Context *)rsc");
433 }
434
435 for (ct2=0; ct2 < api->paramCount; ct2++) {
436 const VarType *vt = &api->params[ct2];
437 if (ct2 > 0 || !api->nocontext) {
438 fprintf(f, ", ");
439 }
440 fprintf(f, "%s", vt->name);
441 }
442 fprintf(f, ");\n");
443 fprintf(f, "}\n\n");
444 }
445
Jason Sams326e0dd2009-05-22 14:03:28 -0700446}
447
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800448void printPlaybackCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700449 int ct;
450 int ct2;
451
452 fprintf(f, "#include \"rsDevice.h\"\n");
453 fprintf(f, "#include \"rsContext.h\"\n");
454 fprintf(f, "#include \"rsThreadIO.h\"\n");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800455 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700456 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
457 fprintf(f, "\n");
458 fprintf(f, "namespace android {\n");
459 fprintf(f, "namespace renderscript {\n");
460 fprintf(f, "\n");
461
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800462 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700463 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800464 int needFlush = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700465
Jason Sams186e5912011-04-26 14:50:00 -0700466 if (api->direct) {
467 continue;
468 }
469
Jason Sams20087472011-05-06 14:14:30 -0700470 fprintf(f, "void rsp_%s(Context *con, const void *vp, size_t cmdSizeBytes) {\n", api->name);
Jason Sams186e5912011-04-26 14:50:00 -0700471 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 -0700472
Jason Sams5f27d6f2012-02-07 15:32:08 -0800473 if (hasInlineDataPointers(api)) {
474 fprintf(f, " const uint8_t *baseData = 0;\n");
475 fprintf(f, " if (cmdSizeBytes != sizeof(RS_CMD_%s)) {\n", api->name);
476 fprintf(f, " baseData = &((const uint8_t *)vp)[sizeof(*cmd)];\n");
477 fprintf(f, " }\n");
478 }
479
Jason Sams186e5912011-04-26 14:50:00 -0700480 fprintf(f, " ");
481 if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700482 fprintf(f, "\n ");
483 printVarType(f, &api->ret);
484 fprintf(f, " ret = ");
Jason Sams326e0dd2009-05-22 14:03:28 -0700485 }
Jason Sams186e5912011-04-26 14:50:00 -0700486 fprintf(f, "rsi_%s(con", api->name);
487 for (ct2=0; ct2 < api->paramCount; ct2++) {
488 const VarType *vt = &api->params[ct2];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800489 needFlush += vt->ptrLevel;
490
491 if (hasInlineDataPointers(api) && vt->ptrLevel) {
492 fprintf(f, ",\n (const %s *)&baseData[(intptr_t)cmd->%s]", vt->typeName, vt->name);
493 } else {
494 fprintf(f, ",\n cmd->%s", vt->name);
495 }
Jason Sams186e5912011-04-26 14:50:00 -0700496 }
497 fprintf(f, ");\n");
498
Jason Sams5f27d6f2012-02-07 15:32:08 -0800499 if (hasInlineDataPointers(api)) {
Jason Samsee5cf002012-02-07 18:54:03 -0800500 fprintf(f, " size_t totalSize = 0;\n");
501 for (ct2=0; ct2 < api->paramCount; ct2++) {
502 if (api->params[ct2].ptrLevel) {
503 fprintf(f, " totalSize += cmd->%s_length;\n", api->params[ct2].name);
504 }
505 }
506
507 fprintf(f, " if ((totalSize != 0) && (cmdSizeBytes == sizeof(RS_CMD_%s))) {\n", api->name);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800508 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
509 fprintf(f, " }\n");
510 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700511 fprintf(f, " con->mIO.coreSetReturn(&ret, sizeof(ret));\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800512 } else if (api->sync || needFlush) {
513 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700514 }
515
Jason Sams326e0dd2009-05-22 14:03:28 -0700516 fprintf(f, "};\n\n");
517 }
518
Jason Sams20087472011-05-06 14:14:30 -0700519 for (ct=0; ct < apiCount; ct++) {
520 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800521 int needFlush = 0;
Jason Sams20087472011-05-06 14:14:30 -0700522
Jason Samsbda75a92012-02-16 17:21:32 -0800523 fprintf(f, "void rspr_%s(Context *con, ThreadIO *io) {\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700524 fprintf(f, " RS_CMD_%s cmd;\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700525
526 for (ct2=0; ct2 < api->paramCount; ct2++) {
527 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800528 if (vt->ptrLevel == 0) {
529 fprintf(f, " io->coreRead(&cmd.%s, sizeof(cmd.%s));\n", vt->name, vt->name);
530 }
531 }
532 fprintf(f, "\n");
533
534 for (ct2=0; ct2 < api->paramCount; ct2++) {
535 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700536 if (vt->ptrLevel == 1) {
537 fprintf(f, " cmd.%s = (", vt->name);
538 printVarType(f, vt);
Jason Samsbda75a92012-02-16 17:21:32 -0800539 fprintf(f, ")malloc(cmd.%s_length);\n", vt->name);
540
541 if (vt->isConst) {
542 fprintf(f, " if (cmd.%s_length) io->coreRead((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name, vt->name);
543 }
Jason Sams20087472011-05-06 14:14:30 -0700544 }
Jason Samsbda75a92012-02-16 17:21:32 -0800545 }
546 fprintf(f, "\n");
547
548 for (ct2=0; ct2 < api->paramCount; ct2++) {
549 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700550 if (vt->ptrLevel == 2) {
Jason Sams20087472011-05-06 14:14:30 -0700551 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 -0800552 fprintf(f, " cmd.%s = (", vt->name);
553 printVarType(f, vt);
554 fprintf(f, ")malloc(cmd.%s_length[ct]);\n", vt->name);
555 fprintf(f, " io->coreRead(& cmd.%s, cmd.%s_length[ct]);\n", vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700556 fprintf(f, " }\n");
Jason Sams20087472011-05-06 14:14:30 -0700557 }
558 }
559 fprintf(f, "\n");
560
561 if (api->ret.typeName[0]) {
562 fprintf(f, " ");
563 printVarType(f, &api->ret);
564 fprintf(f, " ret =\n");
565 }
566
567 fprintf(f, " rsi_%s(", api->name);
568 if (!api->nocontext) {
569 fprintf(f, "con");
570 }
571 for (ct2=0; ct2 < api->paramCount; ct2++) {
572 const VarType *vt = &api->params[ct2];
573 if (ct2 > 0 || !api->nocontext) {
574 fprintf(f, ",\n");
575 }
576 fprintf(f, " cmd.%s", vt->name);
577 }
578 fprintf(f, ");\n");
579
Jason Samsbda75a92012-02-16 17:21:32 -0800580 for (ct2=0; ct2 < api->paramCount; ct2++) {
581 const VarType *vt = &api->params[ct2];
582 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
583 fprintf(f, " io->coreSetReturn((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name);
584 }
585 }
586
587 for (ct2=0; ct2 < api->paramCount; ct2++) {
588 const VarType *vt = &api->params[ct2];
589 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
590 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
591 fprintf(f, " io->coreSetReturn((void *)cmd.%s[ct], cmd.%s_length[ct]);\n", vt->name, vt->name);
592 fprintf(f, " }\n");
593 }
594 }
595 fprintf(f, "\n");
596
Jason Sams20087472011-05-06 14:14:30 -0700597 if (api->ret.typeName[0]) {
Jason Samsbda75a92012-02-16 17:21:32 -0800598 fprintf(f, " io->coreSetReturn(&ret, sizeof(ret));\n");
599 } else /*if (needFlush)*/ {
600 fprintf(f, " io->coreSetReturn(NULL, 0);\n");
601 }
602
603 for (ct2=0; ct2 < api->paramCount; ct2++) {
604 const VarType *vt = &api->params[ct2];
605 if (vt->ptrLevel == 1) {
606 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
607 }
608 }
609 for (ct2=0; ct2 < api->paramCount; ct2++) {
610 const VarType *vt = &api->params[ct2];
611 if (vt->ptrLevel == 2) {
612 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
613 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
614 fprintf(f, " }\n");
615 }
Jason Sams20087472011-05-06 14:14:30 -0700616 }
617
618 fprintf(f, "};\n\n");
619 }
620
621 fprintf(f, "RsPlaybackLocalFunc gPlaybackFuncs[%i] = {\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700622 fprintf(f, " NULL,\n");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800623 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700624 if (apis[ct].direct) {
625 fprintf(f, " NULL,\n");
626 } else {
627 fprintf(f, " %s%s,\n", "rsp_", apis[ct].name);
628 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700629 }
630 fprintf(f, "};\n");
631
Jason Sams20087472011-05-06 14:14:30 -0700632 fprintf(f, "RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i] = {\n", apiCount + 1);
633 fprintf(f, " NULL,\n");
634 for (ct=0; ct < apiCount; ct++) {
635 fprintf(f, " %s%s,\n", "rspr_", apis[ct].name);
636 }
637 fprintf(f, "};\n");
638
Jason Sams326e0dd2009-05-22 14:03:28 -0700639 fprintf(f, "};\n");
640 fprintf(f, "};\n");
641}
642
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700643void yylex();
644
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800645int main(int argc, char **argv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700646 if (argc != 3) {
647 fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]);
648 return 1;
649 }
650 const char* rsgFile = argv[1];
651 const char* outFile = argv[2];
652 FILE* input = fopen(rsgFile, "r");
653
654 char choice = fgetc(input);
655 fclose(input);
656
657 if (choice < '0' || choice > '3') {
658 fprintf(stderr, "Uknown command: \'%c\'\n", choice);
659 return -2;
660 }
661
662 yylex();
663 // printf("# of lines = %d\n", num_lines);
664
665 FILE *f = fopen(outFile, "w");
666
667 printFileHeader(f);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800668 switch (choice) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700669 case '0': // rsgApiStructs.h
670 {
671 fprintf(f, "\n");
672 fprintf(f, "#include \"rsContext.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700673 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700674 fprintf(f, "\n");
675 fprintf(f, "namespace android {\n");
676 fprintf(f, "namespace renderscript {\n");
677 printStructures(f);
Tim Murrayf6023e42013-07-17 12:07:28 -0700678 printFuncDecls(f, "rsi_", 1, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700679 printPlaybackFuncs(f, "rsp_");
Jason Sams20087472011-05-06 14:14:30 -0700680 fprintf(f, "\n\ntypedef struct RsPlaybackRemoteHeaderRec {\n");
681 fprintf(f, " uint32_t command;\n");
Tim Murray099bc262013-03-20 16:54:03 -0700682 fprintf(f, " size_t size;\n");
Jason Sams20087472011-05-06 14:14:30 -0700683 fprintf(f, "} RsPlaybackRemoteHeader;\n\n");
684 fprintf(f, "typedef void (*RsPlaybackLocalFunc)(Context *, const void *, size_t sizeBytes);\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800685 fprintf(f, "typedef void (*RsPlaybackRemoteFunc)(Context *, ThreadIO *);\n");
Jason Sams20087472011-05-06 14:14:30 -0700686 fprintf(f, "extern RsPlaybackLocalFunc gPlaybackFuncs[%i];\n", apiCount + 1);
687 fprintf(f, "extern RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i];\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700688
689 fprintf(f, "}\n");
690 fprintf(f, "}\n");
691 }
692 break;
693
694 case '1': // rsgApiFuncDecl.h
695 {
Tim Murrayf6023e42013-07-17 12:07:28 -0700696 printFuncDecls(f, "rs", 0, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700697 }
698 break;
699
700 case '2': // rsgApi.cpp
701 {
702 printApiCpp(f);
703 }
704 break;
705
706 case '3': // rsgApiReplay.cpp
707 {
708 printFileHeader(f);
709 printPlaybackCpp(f);
710 }
711 break;
Jason Samsc975cf42011-04-28 18:26:48 -0700712
713 case '4': // rsgApiStream.cpp
714 {
715 printFileHeader(f);
716 printPlaybackCpp(f);
717 }
718
719 case '5': // rsgApiStreamReplay.cpp
720 {
721 printFileHeader(f);
722 printPlaybackCpp(f);
723 }
724 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700725 }
726 fclose(f);
727 return 0;
728}