blob: fd531b6d85776abf4ecd7ae8a43fb38ab3457bf2 [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
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");
229 } else {
Tim Murray4d252d62012-11-29 14:37:59 -0800230 // handle synchronous path
231 fprintf(f, " if (((Context *)rsc)->isSynchronous()) {\n");
232 fprintf(f, " ");
233 if (api->ret.typeName[0]) {
234 fprintf(f, "return ");
235 }
236 fprintf(f, "rsi_%s(", api->name);
237 if (!api->nocontext) {
238 fprintf(f, "(Context *)rsc");
239 }
240 for (ct2=0; ct2 < api->paramCount; ct2++) {
241 const VarType *vt = &api->params[ct2];
242 if (ct2 > 0 || !api->nocontext) {
243 fprintf(f, ", ");
244 }
245 fprintf(f, "%s", vt->name);
246 }
247 fprintf(f, ");\n");
248 if (!api->ret.typeName[0]) {
249 fprintf(f, " return;");
250 }
251 fprintf(f, " }\n\n");
252
Jason Sams9397e302009-08-27 20:23:34 -0700253 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Tim Murray099bc262013-03-20 16:54:03 -0700254 fprintf(f, " const size_t size = sizeof(RS_CMD_%s);\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700255 if (hasInlineDataPointers(api)) {
Tim Murray099bc262013-03-20 16:54:03 -0700256 fprintf(f, " size_t dataSize = 0;\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700257 for (ct2=0; ct2 < api->paramCount; ct2++) {
258 const VarType *vt = &api->params[ct2];
259 if (vt->isConst && vt->ptrLevel) {
260 fprintf(f, " dataSize += %s_length;\n", vt->name);
261 }
262 }
263 }
264
Steve Blockaf12ac62012-01-06 19:20:56 +0000265 //fprintf(f, " ALOGE(\"add command %s\\n\");\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700266 if (hasInlineDataPointers(api)) {
Jason Samsb6931122011-05-02 16:29:42 -0700267 fprintf(f, " RS_CMD_%s *cmd = NULL;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800268 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {;\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700269 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 -0700270 fprintf(f, " } else {\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700271 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 -0700272 fprintf(f, " }\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700273 fprintf(f, " uint8_t *payload = (uint8_t *)&cmd[1];\n");
Jason Sams6e58aef2011-04-29 16:23:40 -0700274 } else {
Jason Sams1a4efa32011-05-17 15:01:29 -0700275 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 -0700276 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700277
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800278 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700279 const VarType *vt = &api->params[ct2];
280 needFlush += vt->ptrLevel;
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700281 if (vt->ptrLevel && hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800282 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700283 fprintf(f, " memcpy(payload, %s, %s_length);\n", vt->name, vt->name);
284 fprintf(f, " cmd->%s = (", vt->name);
285 printVarType(f, vt);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800286 fprintf(f, ")(payload - ((uint8_t *)&cmd[1]));\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700287 fprintf(f, " payload += %s_length;\n", vt->name);
288 fprintf(f, " } else {\n");
289 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
290 fprintf(f, " }\n");
291
292 } else {
293 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
294 }
Jason Sams9397e302009-08-27 20:23:34 -0700295 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700296 if (api->ret.typeName[0] || api->sync) {
Jason Sams9397e302009-08-27 20:23:34 -0700297 needFlush = 1;
298 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700299
Jason Sams5f27d6f2012-02-07 15:32:08 -0800300 fprintf(f, " io->coreCommit();\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700301 if (hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800302 fprintf(f, " if (dataSize >= io->getMaxInlineSize()) {\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800303 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700304 fprintf(f, " }\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800305 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700306 fprintf(f, "\n ");
Jason Sams20087472011-05-06 14:14:30 -0700307 printVarType(f, &api->ret);
Jason Sams1a4efa32011-05-17 15:01:29 -0700308 fprintf(f, " ret;\n");
309 fprintf(f, " io->coreGetReturn(&ret, sizeof(ret));\n");
310 fprintf(f, " return ret;\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800311 } else if (needFlush) {
312 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams9397e302009-08-27 20:23:34 -0700313 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700314 }
315 fprintf(f, "};\n\n");
Jason Sams20087472011-05-06 14:14:30 -0700316
317
Jason Samsbda75a92012-02-16 17:21:32 -0800318 // Generate a remote sender function
319 const char * str = "core";
320 if (api->direct) {
321 str = "async";
322 }
323
Jason Sams20087472011-05-06 14:14:30 -0700324 fprintf(f, "static ");
325 printFuncDecl(f, api, "RF_", 0, 0);
326 fprintf(f, "\n{\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800327 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Jason Sams20087472011-05-06 14:14:30 -0700328 fprintf(f, " const uint32_t cmdID = RS_CMD_ID_%s;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800329 fprintf(f, " io->%sWrite(&cmdID, sizeof(cmdID));\n\n", str);
330
Jason Sams1a4efa32011-05-17 15:01:29 -0700331 for (ct2=0; ct2 < api->paramCount; ct2++) {
332 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800333 if (vt->ptrLevel == 0) {
334 fprintf(f, " io->%sWrite(& %s, sizeof(%s));\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700335 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700336 }
337 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700338
Jason Sams1a4efa32011-05-17 15:01:29 -0700339 for (ct2=0; ct2 < api->paramCount; ct2++) {
340 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800341 if ((vt->ptrLevel == 1) && (vt->isConst)) {
342 fprintf(f, " io->%sWrite(%s, %s_length);\n", str, vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700343 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700344 }
345 fprintf(f, "\n");
Jason Sams20087472011-05-06 14:14:30 -0700346
Jason Sams1a4efa32011-05-17 15:01:29 -0700347 for (ct2=0; ct2 < api->paramCount; ct2++) {
348 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800349 if ((vt->ptrLevel == 2) && (vt->isConst)) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700350 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 -0800351 fprintf(f, " io->%sWrite(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
Jason Sams1a4efa32011-05-17 15:01:29 -0700352 fprintf(f, " }\n");
353 }
354 }
Jason Samsbda75a92012-02-16 17:21:32 -0800355 fprintf(f, "\n");
356
357 for (ct2=0; ct2 < api->paramCount; ct2++) {
358 const VarType *vt = &api->params[ct2];
359 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
360 fprintf(f, " io->%sGetReturn(%s, %s_length);\n", str, vt->name, vt->name);
361 }
362 }
363 fprintf(f, "\n");
364
365 for (ct2=0; ct2 < api->paramCount; ct2++) {
366 const VarType *vt = &api->params[ct2];
367 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
368 fprintf(f, " for (size_t ct = 0; ct < (%s_length_length / sizeof(%s_length)); ct++) {\n", vt->name, vt->name);
369 fprintf(f, " io->%sGetReturn(%s[ct], %s_length[ct]);\n", str, vt->name, vt->name);
370 fprintf(f, " }\n");
371 }
372 }
373 fprintf(f, "\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700374
375 if (api->ret.typeName[0]) {
376 fprintf(f, " ");
377 printVarType(f, &api->ret);
378 fprintf(f, " retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800379 fprintf(f, " io->%sGetReturn(&retValue, sizeof(retValue));\n", str);
Jason Sams1a4efa32011-05-17 15:01:29 -0700380 fprintf(f, " return retValue;\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800381 } else /*if (api->sync)*/ {
382 fprintf(f, " io->%sGetReturn(NULL, 0);\n", str);
Jason Sams20087472011-05-06 14:14:30 -0700383 }
384 fprintf(f, "}\n\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700385 }
Jason Samsc975cf42011-04-28 18:26:48 -0700386
387 fprintf(f, "\n");
388 fprintf(f, "static RsApiEntrypoints_t s_LocalTable = {\n");
389 for (ct=0; ct < apiCount; ct++) {
390 fprintf(f, " LF_%s,\n", apis[ct].name);
391 }
392 fprintf(f, "};\n");
393
Jason Sams20087472011-05-06 14:14:30 -0700394 fprintf(f, "\n");
395 fprintf(f, "static RsApiEntrypoints_t s_RemoteTable = {\n");
396 for (ct=0; ct < apiCount; ct++) {
397 fprintf(f, " RF_%s,\n", apis[ct].name);
398 }
399 fprintf(f, "};\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700400
Jason Sams20087472011-05-06 14:14:30 -0700401 fprintf(f, "static RsApiEntrypoints_t *s_CurrentTable = &s_LocalTable;\n\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700402 for (ct=0; ct < apiCount; ct++) {
403 int needFlush = 0;
404 const ApiEntry * api = &apis[ct];
405
Tim Murrayf6023e42013-07-17 12:07:28 -0700406 fprintf(f, "extern \"C\" ");
407
Jason Samsc975cf42011-04-28 18:26:48 -0700408 printFuncDecl(f, api, "rs", 0, 0);
409 fprintf(f, "\n{\n");
410 fprintf(f, " ");
411 if (api->ret.typeName[0]) {
412 fprintf(f, "return ");
413 }
414 fprintf(f, "s_CurrentTable->%s(", api->name);
415
416 if (!api->nocontext) {
417 fprintf(f, "(Context *)rsc");
418 }
419
420 for (ct2=0; ct2 < api->paramCount; ct2++) {
421 const VarType *vt = &api->params[ct2];
422 if (ct2 > 0 || !api->nocontext) {
423 fprintf(f, ", ");
424 }
425 fprintf(f, "%s", vt->name);
426 }
427 fprintf(f, ");\n");
428 fprintf(f, "}\n\n");
429 }
430
Jason Sams326e0dd2009-05-22 14:03:28 -0700431}
432
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800433void printPlaybackCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700434 int ct;
435 int ct2;
436
437 fprintf(f, "#include \"rsDevice.h\"\n");
438 fprintf(f, "#include \"rsContext.h\"\n");
439 fprintf(f, "#include \"rsThreadIO.h\"\n");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800440 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700441 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
442 fprintf(f, "\n");
443 fprintf(f, "namespace android {\n");
444 fprintf(f, "namespace renderscript {\n");
445 fprintf(f, "\n");
446
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800447 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700448 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800449 int needFlush = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700450
Jason Sams186e5912011-04-26 14:50:00 -0700451 if (api->direct) {
452 continue;
453 }
454
Jason Sams20087472011-05-06 14:14:30 -0700455 fprintf(f, "void rsp_%s(Context *con, const void *vp, size_t cmdSizeBytes) {\n", api->name);
Jason Sams186e5912011-04-26 14:50:00 -0700456 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 -0700457
Jason Sams5f27d6f2012-02-07 15:32:08 -0800458 if (hasInlineDataPointers(api)) {
459 fprintf(f, " const uint8_t *baseData = 0;\n");
460 fprintf(f, " if (cmdSizeBytes != sizeof(RS_CMD_%s)) {\n", api->name);
461 fprintf(f, " baseData = &((const uint8_t *)vp)[sizeof(*cmd)];\n");
462 fprintf(f, " }\n");
463 }
464
Jason Sams186e5912011-04-26 14:50:00 -0700465 fprintf(f, " ");
466 if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700467 fprintf(f, "\n ");
468 printVarType(f, &api->ret);
469 fprintf(f, " ret = ");
Jason Sams326e0dd2009-05-22 14:03:28 -0700470 }
Jason Sams186e5912011-04-26 14:50:00 -0700471 fprintf(f, "rsi_%s(con", api->name);
472 for (ct2=0; ct2 < api->paramCount; ct2++) {
473 const VarType *vt = &api->params[ct2];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800474 needFlush += vt->ptrLevel;
475
476 if (hasInlineDataPointers(api) && vt->ptrLevel) {
477 fprintf(f, ",\n (const %s *)&baseData[(intptr_t)cmd->%s]", vt->typeName, vt->name);
478 } else {
479 fprintf(f, ",\n cmd->%s", vt->name);
480 }
Jason Sams186e5912011-04-26 14:50:00 -0700481 }
482 fprintf(f, ");\n");
483
Jason Sams5f27d6f2012-02-07 15:32:08 -0800484 if (hasInlineDataPointers(api)) {
Jason Samsee5cf002012-02-07 18:54:03 -0800485 fprintf(f, " size_t totalSize = 0;\n");
486 for (ct2=0; ct2 < api->paramCount; ct2++) {
487 if (api->params[ct2].ptrLevel) {
488 fprintf(f, " totalSize += cmd->%s_length;\n", api->params[ct2].name);
489 }
490 }
491
492 fprintf(f, " if ((totalSize != 0) && (cmdSizeBytes == sizeof(RS_CMD_%s))) {\n", api->name);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800493 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
494 fprintf(f, " }\n");
495 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700496 fprintf(f, " con->mIO.coreSetReturn(&ret, sizeof(ret));\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800497 } else if (api->sync || needFlush) {
498 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700499 }
500
Jason Sams326e0dd2009-05-22 14:03:28 -0700501 fprintf(f, "};\n\n");
502 }
503
Jason Sams20087472011-05-06 14:14:30 -0700504 for (ct=0; ct < apiCount; ct++) {
505 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800506 int needFlush = 0;
Jason Sams20087472011-05-06 14:14:30 -0700507
Jason Samsbda75a92012-02-16 17:21:32 -0800508 fprintf(f, "void rspr_%s(Context *con, ThreadIO *io) {\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700509 fprintf(f, " RS_CMD_%s cmd;\n", api->name);
Jason Sams20087472011-05-06 14:14:30 -0700510
511 for (ct2=0; ct2 < api->paramCount; ct2++) {
512 const VarType *vt = &api->params[ct2];
Jason Samsbda75a92012-02-16 17:21:32 -0800513 if (vt->ptrLevel == 0) {
514 fprintf(f, " io->coreRead(&cmd.%s, sizeof(cmd.%s));\n", vt->name, vt->name);
515 }
516 }
517 fprintf(f, "\n");
518
519 for (ct2=0; ct2 < api->paramCount; ct2++) {
520 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700521 if (vt->ptrLevel == 1) {
522 fprintf(f, " cmd.%s = (", vt->name);
523 printVarType(f, vt);
Jason Samsbda75a92012-02-16 17:21:32 -0800524 fprintf(f, ")malloc(cmd.%s_length);\n", vt->name);
525
526 if (vt->isConst) {
527 fprintf(f, " if (cmd.%s_length) io->coreRead((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name, vt->name);
528 }
Jason Sams20087472011-05-06 14:14:30 -0700529 }
Jason Samsbda75a92012-02-16 17:21:32 -0800530 }
531 fprintf(f, "\n");
532
533 for (ct2=0; ct2 < api->paramCount; ct2++) {
534 const VarType *vt = &api->params[ct2];
Jason Sams20087472011-05-06 14:14:30 -0700535 if (vt->ptrLevel == 2) {
Jason Sams20087472011-05-06 14:14:30 -0700536 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 -0800537 fprintf(f, " cmd.%s = (", vt->name);
538 printVarType(f, vt);
539 fprintf(f, ")malloc(cmd.%s_length[ct]);\n", vt->name);
540 fprintf(f, " io->coreRead(& cmd.%s, cmd.%s_length[ct]);\n", vt->name, vt->name);
Jason Sams20087472011-05-06 14:14:30 -0700541 fprintf(f, " }\n");
Jason Sams20087472011-05-06 14:14:30 -0700542 }
543 }
544 fprintf(f, "\n");
545
546 if (api->ret.typeName[0]) {
547 fprintf(f, " ");
548 printVarType(f, &api->ret);
549 fprintf(f, " ret =\n");
550 }
551
552 fprintf(f, " rsi_%s(", api->name);
553 if (!api->nocontext) {
554 fprintf(f, "con");
555 }
556 for (ct2=0; ct2 < api->paramCount; ct2++) {
557 const VarType *vt = &api->params[ct2];
558 if (ct2 > 0 || !api->nocontext) {
559 fprintf(f, ",\n");
560 }
561 fprintf(f, " cmd.%s", vt->name);
562 }
563 fprintf(f, ");\n");
564
Jason Samsbda75a92012-02-16 17:21:32 -0800565 for (ct2=0; ct2 < api->paramCount; ct2++) {
566 const VarType *vt = &api->params[ct2];
567 if ((vt->ptrLevel == 1) && (!vt->isConst)) {
568 fprintf(f, " io->coreSetReturn((void *)cmd.%s, cmd.%s_length);\n", vt->name, vt->name);
569 }
570 }
571
572 for (ct2=0; ct2 < api->paramCount; ct2++) {
573 const VarType *vt = &api->params[ct2];
574 if ((vt->ptrLevel == 2) && (!vt->isConst)) {
575 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
576 fprintf(f, " io->coreSetReturn((void *)cmd.%s[ct], cmd.%s_length[ct]);\n", vt->name, vt->name);
577 fprintf(f, " }\n");
578 }
579 }
580 fprintf(f, "\n");
581
Jason Sams20087472011-05-06 14:14:30 -0700582 if (api->ret.typeName[0]) {
Jason Samsbda75a92012-02-16 17:21:32 -0800583 fprintf(f, " io->coreSetReturn(&ret, sizeof(ret));\n");
584 } else /*if (needFlush)*/ {
585 fprintf(f, " io->coreSetReturn(NULL, 0);\n");
586 }
587
588 for (ct2=0; ct2 < api->paramCount; ct2++) {
589 const VarType *vt = &api->params[ct2];
590 if (vt->ptrLevel == 1) {
591 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
592 }
593 }
594 for (ct2=0; ct2 < api->paramCount; ct2++) {
595 const VarType *vt = &api->params[ct2];
596 if (vt->ptrLevel == 2) {
597 fprintf(f, " for (size_t ct = 0; ct < (cmd.%s_length_length / sizeof(cmd.%s_length)); ct++) {\n", vt->name, vt->name);
598 fprintf(f, " free((void *)cmd.%s);\n", vt->name);
599 fprintf(f, " }\n");
600 }
Jason Sams20087472011-05-06 14:14:30 -0700601 }
602
603 fprintf(f, "};\n\n");
604 }
605
606 fprintf(f, "RsPlaybackLocalFunc gPlaybackFuncs[%i] = {\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700607 fprintf(f, " NULL,\n");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800608 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700609 if (apis[ct].direct) {
610 fprintf(f, " NULL,\n");
611 } else {
612 fprintf(f, " %s%s,\n", "rsp_", apis[ct].name);
613 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700614 }
615 fprintf(f, "};\n");
616
Jason Sams20087472011-05-06 14:14:30 -0700617 fprintf(f, "RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i] = {\n", apiCount + 1);
618 fprintf(f, " NULL,\n");
619 for (ct=0; ct < apiCount; ct++) {
620 fprintf(f, " %s%s,\n", "rspr_", apis[ct].name);
621 }
622 fprintf(f, "};\n");
623
Jason Sams326e0dd2009-05-22 14:03:28 -0700624 fprintf(f, "};\n");
625 fprintf(f, "};\n");
626}
627
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700628void yylex();
629
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800630int main(int argc, char **argv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700631 if (argc != 3) {
632 fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]);
633 return 1;
634 }
635 const char* rsgFile = argv[1];
636 const char* outFile = argv[2];
637 FILE* input = fopen(rsgFile, "r");
638
639 char choice = fgetc(input);
640 fclose(input);
641
642 if (choice < '0' || choice > '3') {
643 fprintf(stderr, "Uknown command: \'%c\'\n", choice);
644 return -2;
645 }
646
647 yylex();
648 // printf("# of lines = %d\n", num_lines);
649
650 FILE *f = fopen(outFile, "w");
651
652 printFileHeader(f);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800653 switch (choice) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700654 case '0': // rsgApiStructs.h
655 {
656 fprintf(f, "\n");
657 fprintf(f, "#include \"rsContext.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700658 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700659 fprintf(f, "\n");
660 fprintf(f, "namespace android {\n");
661 fprintf(f, "namespace renderscript {\n");
662 printStructures(f);
Tim Murrayf6023e42013-07-17 12:07:28 -0700663 printFuncDecls(f, "rsi_", 1, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700664 printPlaybackFuncs(f, "rsp_");
Jason Sams20087472011-05-06 14:14:30 -0700665 fprintf(f, "\n\ntypedef struct RsPlaybackRemoteHeaderRec {\n");
666 fprintf(f, " uint32_t command;\n");
Tim Murray099bc262013-03-20 16:54:03 -0700667 fprintf(f, " size_t size;\n");
Jason Sams20087472011-05-06 14:14:30 -0700668 fprintf(f, "} RsPlaybackRemoteHeader;\n\n");
669 fprintf(f, "typedef void (*RsPlaybackLocalFunc)(Context *, const void *, size_t sizeBytes);\n");
Jason Samsbda75a92012-02-16 17:21:32 -0800670 fprintf(f, "typedef void (*RsPlaybackRemoteFunc)(Context *, ThreadIO *);\n");
Jason Sams20087472011-05-06 14:14:30 -0700671 fprintf(f, "extern RsPlaybackLocalFunc gPlaybackFuncs[%i];\n", apiCount + 1);
672 fprintf(f, "extern RsPlaybackRemoteFunc gPlaybackRemoteFuncs[%i];\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700673
674 fprintf(f, "}\n");
675 fprintf(f, "}\n");
676 }
677 break;
678
679 case '1': // rsgApiFuncDecl.h
680 {
Tim Murrayf6023e42013-07-17 12:07:28 -0700681 printFuncDecls(f, "rs", 0, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700682 }
683 break;
684
685 case '2': // rsgApi.cpp
686 {
687 printApiCpp(f);
688 }
689 break;
690
691 case '3': // rsgApiReplay.cpp
692 {
693 printFileHeader(f);
694 printPlaybackCpp(f);
695 }
696 break;
Jason Samsc975cf42011-04-28 18:26:48 -0700697
698 case '4': // rsgApiStream.cpp
699 {
700 printFileHeader(f);
701 printPlaybackCpp(f);
702 }
703
704 case '5': // rsgApiStreamReplay.cpp
705 {
706 printFileHeader(f);
707 printPlaybackCpp(f);
708 }
709 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700710 }
711 fclose(f);
712 return 0;
713}