blob: 89caa600df79697c82102acc2973499c4be4efb5 [file] [log] [blame]
Joe Onorato84614dd2009-08-10 15:01:51 -07001#include "spec.h"
2#include <stdio.h>
Shih-wei Liaoda3b58d2012-08-03 04:24:33 -07003#include <string.h>
Jason Sams326e0dd2009-05-22 14:03:28 -07004
Matt Walab74514d2015-07-20 14:03:25 -07005#define LOCAL_FIFO_PREFIX "LF_"
6#define RS_PLAYBACK_PREFIX "rsp_"
7#define RS_INTERNAL_PREFIX "rsi_"
8
9#define RSG_API_CPP_DOC \
10"/*\n" \
11" * rsgApi.cpp\n" \
12" * This file implements the functions responsible for sending messages\n" \
13" * to the RS driver layer. The messages are sent through a FIFO that is\n" \
14" * shared between the process's caller threads and driver thread.\n" \
15" */\n\n"
16
17#define RSG_API_REPLAY_CPP_DOC \
18"/*\n" \
19" * rsgApiReplay.cpp\n" \
20" * This file implements the functions responsible for reading messages\n" \
21" * sent to the RS driver layer.\n" \
22" */\n\n"
23
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080024void printFileHeader(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -070025 fprintf(f, "/*\n");
Matt Walab74514d2015-07-20 14:03:25 -070026 fprintf(f, " * Copyright (C) 2015 The Android Open Source Project\n");
Jason Sams326e0dd2009-05-22 14:03:28 -070027 fprintf(f, " *\n");
28 fprintf(f, " * Licensed under the Apache License, Version 2.0 (the \"License\");\n");
29 fprintf(f, " * you may not use this file except in compliance with the License.\n");
30 fprintf(f, " * You may obtain a copy of the License at\n");
31 fprintf(f, " *\n");
32 fprintf(f, " * http://www.apache.org/licenses/LICENSE-2.0\n");
33 fprintf(f, " *\n");
34 fprintf(f, " * Unless required by applicable law or agreed to in writing, software\n");
35 fprintf(f, " * distributed under the License is distributed on an \"AS IS\" BASIS,\n");
36 fprintf(f, " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n");
37 fprintf(f, " * See the License for the specific language governing permissions and\n");
38 fprintf(f, " * limitations under the License.\n");
39 fprintf(f, " */\n\n");
40}
41
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080042void printVarType(FILE *f, const VarType *vt) {
Jason Sams326e0dd2009-05-22 14:03:28 -070043 int ct;
44 if (vt->isConst) {
45 fprintf(f, "const ");
46 }
47
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080048 switch (vt->type) {
Jason Sams326e0dd2009-05-22 14:03:28 -070049 case 0:
50 fprintf(f, "void");
51 break;
52 case 1:
53 fprintf(f, "int%i_t", vt->bits);
54 break;
55 case 2:
56 fprintf(f, "uint%i_t", vt->bits);
57 break;
58 case 3:
59 if (vt->bits == 32)
60 fprintf(f, "float");
61 else
62 fprintf(f, "double");
63 break;
64 case 4:
Joe Onorato84614dd2009-08-10 15:01:51 -070065 fprintf(f, "%s", vt->typeName);
Jason Sams326e0dd2009-05-22 14:03:28 -070066 break;
67 }
68
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080069 if (vt->ptrLevel) {
Jason Sams326e0dd2009-05-22 14:03:28 -070070 fprintf(f, " ");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080071 for (ct=0; ct < vt->ptrLevel; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070072 fprintf(f, "*");
73 }
74 }
Jason Sams5fb1aeb2011-04-27 15:12:49 -070075}
76
77void printVarTypeAndName(FILE *f, const VarType *vt) {
78 printVarType(f, vt);
Jason Sams326e0dd2009-05-22 14:03:28 -070079
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080080 if (vt->name[0]) {
Jason Sams326e0dd2009-05-22 14:03:28 -070081 fprintf(f, " %s", vt->name);
82 }
83}
84
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080085void printArgList(FILE *f, const ApiEntry * api, int assumePrevious) {
Jason Sams326e0dd2009-05-22 14:03:28 -070086 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080087 for (ct=0; ct < api->paramCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070088 if (ct || assumePrevious) {
89 fprintf(f, ", ");
90 }
Jason Sams5fb1aeb2011-04-27 15:12:49 -070091 printVarTypeAndName(f, &api->params[ct]);
Jason Sams326e0dd2009-05-22 14:03:28 -070092 }
93}
94
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080095void printStructures(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -070096 int ct;
97 int ct2;
98
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080099 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700100 fprintf(f, "typedef struct RS_CMD_%s_rec RS_CMD_%s;\n", apis[ct].name, apis[ct].name);
101 }
102 fprintf(f, "\n");
103
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800104 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700105 const ApiEntry * api = &apis[ct];
106 fprintf(f, "#define RS_CMD_ID_%s %i\n", api->name, ct+1);
Stephen Hinesf0475752014-06-25 00:06:26 -0700107 fprintf(f, "struct __attribute__((packed)) RS_CMD_%s_rec {\n", api->name);
Jason Sams326e0dd2009-05-22 14:03:28 -0700108 //fprintf(f, " RsCommandHeader _hdr;\n");
109
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800110 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700111 fprintf(f, " ");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700112 printVarTypeAndName(f, &api->params[ct2]);
Jason Sams326e0dd2009-05-22 14:03:28 -0700113 fprintf(f, ";\n");
114 }
115 fprintf(f, "};\n\n");
116 }
117}
118
Jason Samsc975cf42011-04-28 18:26:48 -0700119void printFuncDecl(FILE *f, const ApiEntry *api, const char *prefix, int addContext, int isFnPtr) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700120 printVarTypeAndName(f, &api->ret);
Jason Samsc975cf42011-04-28 18:26:48 -0700121 if (isFnPtr) {
122 char t[1024];
Michael Butlerca451c32017-04-19 18:47:55 -0700123 strncpy(t, api->name, sizeof(t)-1);
124 t[sizeof(t)-1] = '\0';
Jason Samsc975cf42011-04-28 18:26:48 -0700125 if (strlen(prefix) == 0) {
126 if (t[0] > 'A' && t[0] < 'Z') {
127 t[0] -= 'A' - 'a';
128 }
129 }
130 fprintf(f, " (* %s%s) (", prefix, api->name);
131 } else {
132 fprintf(f, " %s%s (", prefix, api->name);
133 }
Jason Sams186e5912011-04-26 14:50:00 -0700134 if (!api->nocontext) {
135 if (addContext) {
136 fprintf(f, "Context *");
137 } else {
138 fprintf(f, "RsContext rsc");
139 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700140 }
Jason Sams186e5912011-04-26 14:50:00 -0700141 printArgList(f, api, !api->nocontext);
Jason Sams326e0dd2009-05-22 14:03:28 -0700142 fprintf(f, ")");
143}
144
Tim Murrayf6023e42013-07-17 12:07:28 -0700145void printFuncDecls(FILE *f, const char *prefix, int addContext, int externC) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700146 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800147 for (ct=0; ct < apiCount; ct++) {
Tim Murrayf6023e42013-07-17 12:07:28 -0700148 if (externC) {
149 fprintf(f, "extern \"C\" ");
150 }
Jason Samsc975cf42011-04-28 18:26:48 -0700151 printFuncDecl(f, &apis[ct], prefix, addContext, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700152 fprintf(f, ";\n");
153 }
154 fprintf(f, "\n\n");
155}
156
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800157void printPlaybackFuncs(FILE *f, const char *prefix) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700158 int ct;
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800159 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700160 if (apis[ct].direct) {
161 continue;
162 }
163
Jason Sams326e0dd2009-05-22 14:03:28 -0700164 fprintf(f, "void %s%s (Context *, const void *);\n", prefix, apis[ct].name);
165 }
166}
167
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700168static int hasInlineDataPointers(const ApiEntry * api) {
169 int ret = 0;
170 int ct;
Jason Samsb6931122011-05-02 16:29:42 -0700171 if (api->sync || api->ret.typeName[0]) {
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700172 return 0;
173 }
174 for (ct=0; ct < api->paramCount; ct++) {
175 const VarType *vt = &api->params[ct];
176
177 if (!vt->isConst && vt->ptrLevel) {
178 // Non-const pointers cannot be inlined.
179 return 0;
180 }
181 if (vt->ptrLevel > 1) {
182 // not handled yet.
183 return 0;
184 }
185
186 if (vt->isConst && vt->ptrLevel) {
187 // Non-const pointers cannot be inlined.
188 ret = 1;
189 }
190 }
191 return ret;
192}
193
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800194void printApiCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700195 int ct;
196 int ct2;
197
Matt Walab74514d2015-07-20 14:03:25 -0700198 fprintf(f, RSG_API_CPP_DOC);
199
Jason Sams326e0dd2009-05-22 14:03:28 -0700200 fprintf(f, "#include \"rsDevice.h\"\n");
201 fprintf(f, "#include \"rsContext.h\"\n");
202 fprintf(f, "#include \"rsThreadIO.h\"\n");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800203 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700204 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700205 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700206 fprintf(f, "\n");
Chih-Hung Hsieh11496ac2016-11-15 15:14:05 -0800207 fprintf(f, "using namespace android; // NOLINT\n");
208 fprintf(f, "using namespace android::renderscript; // NOLINT\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700209 fprintf(f, "\n");
210
Matt Walab74514d2015-07-20 14:03:25 -0700211 // Generate RS funcs that send messages on the local FIFO.
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800212 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700213 int needFlush = 0;
214 const ApiEntry * api = &apis[ct];
215
Jason Samsc975cf42011-04-28 18:26:48 -0700216 fprintf(f, "static ");
Matt Walab74514d2015-07-20 14:03:25 -0700217 printFuncDecl(f, api, LOCAL_FIFO_PREFIX, 0, 0);
Jason Sams326e0dd2009-05-22 14:03:28 -0700218 fprintf(f, "\n{\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700219 if (api->direct) {
220 fprintf(f, " ");
221 if (api->ret.typeName[0]) {
222 fprintf(f, "return ");
223 }
Matt Walab74514d2015-07-20 14:03:25 -0700224 fprintf(f, RS_INTERNAL_PREFIX "%s(", api->name);
Jason Sams1a4efa32011-05-17 15:01:29 -0700225 if (!api->nocontext) {
226 fprintf(f, "(Context *)rsc");
Jason Samsc975cf42011-04-28 18:26:48 -0700227 }
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800228 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700229 const VarType *vt = &api->params[ct2];
Jason Samsc975cf42011-04-28 18:26:48 -0700230 if (ct2 > 0 || !api->nocontext) {
231 fprintf(f, ", ");
232 }
233 fprintf(f, "%s", vt->name);
Jason Sams9397e302009-08-27 20:23:34 -0700234 }
235 fprintf(f, ");\n");
Tim Murrayc2eca552014-01-07 15:05:47 -0800236 } else if (api->handcodeApi) {
237 // handle handcode path
Matt Walab74514d2015-07-20 14:03:25 -0700238 fprintf(f, " " LOCAL_FIFO_PREFIX "%s_handcode(", api->name);
Tim Murrayc2eca552014-01-07 15:05:47 -0800239 if (!api->nocontext) {
240 fprintf(f, "(Context *)rsc");
241 }
242 for (ct2=0; ct2 < api->paramCount; ct2++) {
243 const VarType *vt = &api->params[ct2];
244 if (ct2 > 0 || !api->nocontext) {
245 fprintf(f, ", ");
246 }
247 fprintf(f, "%s", vt->name);
248 }
249 fprintf(f, ");\n");
250
Jason Sams9397e302009-08-27 20:23:34 -0700251 } else {
Tim Murray4d252d62012-11-29 14:37:59 -0800252 // handle synchronous path
253 fprintf(f, " if (((Context *)rsc)->isSynchronous()) {\n");
254 fprintf(f, " ");
255 if (api->ret.typeName[0]) {
256 fprintf(f, "return ");
257 }
Matt Walab74514d2015-07-20 14:03:25 -0700258 fprintf(f, RS_INTERNAL_PREFIX "%s(", api->name);
Tim Murray4d252d62012-11-29 14:37:59 -0800259 if (!api->nocontext) {
260 fprintf(f, "(Context *)rsc");
261 }
262 for (ct2=0; ct2 < api->paramCount; ct2++) {
263 const VarType *vt = &api->params[ct2];
264 if (ct2 > 0 || !api->nocontext) {
265 fprintf(f, ", ");
266 }
267 fprintf(f, "%s", vt->name);
268 }
269 fprintf(f, ");\n");
270 if (!api->ret.typeName[0]) {
271 fprintf(f, " return;");
272 }
273 fprintf(f, " }\n\n");
274
Jason Sams9397e302009-08-27 20:23:34 -0700275 fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n");
Tim Murray099bc262013-03-20 16:54:03 -0700276 fprintf(f, " const size_t size = sizeof(RS_CMD_%s);\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700277 if (hasInlineDataPointers(api)) {
Tim Murray099bc262013-03-20 16:54:03 -0700278 fprintf(f, " size_t dataSize = 0;\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700279 for (ct2=0; ct2 < api->paramCount; ct2++) {
280 const VarType *vt = &api->params[ct2];
281 if (vt->isConst && vt->ptrLevel) {
282 fprintf(f, " dataSize += %s_length;\n", vt->name);
283 }
284 }
285 }
286
Steve Blockaf12ac62012-01-06 19:20:56 +0000287 //fprintf(f, " ALOGE(\"add command %s\\n\");\n", api->name);
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700288 if (hasInlineDataPointers(api)) {
Jason Samsb6931122011-05-02 16:29:42 -0700289 fprintf(f, " RS_CMD_%s *cmd = NULL;\n", api->name);
Jason Samsbda75a92012-02-16 17:21:32 -0800290 fprintf(f, " if (dataSize < io->getMaxInlineSize()) {;\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700291 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 -0700292 fprintf(f, " } else {\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700293 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 -0700294 fprintf(f, " }\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700295 fprintf(f, " uint8_t *payload = (uint8_t *)&cmd[1];\n");
Jason Sams6e58aef2011-04-29 16:23:40 -0700296 } else {
Jason Sams1a4efa32011-05-17 15:01:29 -0700297 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 -0700298 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700299
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800300 for (ct2=0; ct2 < api->paramCount; ct2++) {
Jason Sams9397e302009-08-27 20:23:34 -0700301 const VarType *vt = &api->params[ct2];
302 needFlush += vt->ptrLevel;
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700303 if (vt->ptrLevel && hasInlineDataPointers(api)) {
Chris Wailesdb896f02014-07-16 18:20:06 -0700304 fprintf(f, " if (%s_length == 0) {\n", vt->name);
305 fprintf(f, " cmd->%s = NULL;\n", vt->name);
306 fprintf(f, " } else if (dataSize < io->getMaxInlineSize()) {\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700307 fprintf(f, " memcpy(payload, %s, %s_length);\n", vt->name, vt->name);
308 fprintf(f, " cmd->%s = (", vt->name);
309 printVarType(f, vt);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800310 fprintf(f, ")(payload - ((uint8_t *)&cmd[1]));\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700311 fprintf(f, " payload += %s_length;\n", vt->name);
312 fprintf(f, " } else {\n");
313 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
314 fprintf(f, " }\n");
315
316 } else {
317 fprintf(f, " cmd->%s = %s;\n", vt->name, vt->name);
318 }
Jason Sams9397e302009-08-27 20:23:34 -0700319 }
Jason Sams1a4efa32011-05-17 15:01:29 -0700320 if (api->ret.typeName[0] || api->sync) {
Jason Sams9397e302009-08-27 20:23:34 -0700321 needFlush = 1;
322 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700323
Jason Sams5f27d6f2012-02-07 15:32:08 -0800324 fprintf(f, " io->coreCommit();\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700325 if (hasInlineDataPointers(api)) {
Jason Samsbda75a92012-02-16 17:21:32 -0800326 fprintf(f, " if (dataSize >= io->getMaxInlineSize()) {\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800327 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700328 fprintf(f, " }\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800329 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700330 fprintf(f, "\n ");
Jason Sams20087472011-05-06 14:14:30 -0700331 printVarType(f, &api->ret);
Jason Sams1a4efa32011-05-17 15:01:29 -0700332 fprintf(f, " ret;\n");
333 fprintf(f, " io->coreGetReturn(&ret, sizeof(ret));\n");
334 fprintf(f, " return ret;\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800335 } else if (needFlush) {
336 fprintf(f, " io->coreGetReturn(NULL, 0);\n");
Jason Sams9397e302009-08-27 20:23:34 -0700337 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700338 }
339 fprintf(f, "};\n\n");
340 }
Jason Samsc975cf42011-04-28 18:26:48 -0700341
342 fprintf(f, "\n");
Jason Samsc975cf42011-04-28 18:26:48 -0700343
Jason Samsc975cf42011-04-28 18:26:48 -0700344 for (ct=0; ct < apiCount; ct++) {
Jason Samsc975cf42011-04-28 18:26:48 -0700345 const ApiEntry * api = &apis[ct];
346
Tim Murrayf6023e42013-07-17 12:07:28 -0700347 fprintf(f, "extern \"C\" ");
348
Jason Samsc975cf42011-04-28 18:26:48 -0700349 printFuncDecl(f, api, "rs", 0, 0);
350 fprintf(f, "\n{\n");
351 fprintf(f, " ");
352 if (api->ret.typeName[0]) {
353 fprintf(f, "return ");
354 }
Matt Walab74514d2015-07-20 14:03:25 -0700355 fprintf(f, LOCAL_FIFO_PREFIX "%s(", api->name);
Jason Samsc975cf42011-04-28 18:26:48 -0700356
357 if (!api->nocontext) {
358 fprintf(f, "(Context *)rsc");
359 }
360
361 for (ct2=0; ct2 < api->paramCount; ct2++) {
362 const VarType *vt = &api->params[ct2];
363 if (ct2 > 0 || !api->nocontext) {
364 fprintf(f, ", ");
365 }
366 fprintf(f, "%s", vt->name);
367 }
368 fprintf(f, ");\n");
369 fprintf(f, "}\n\n");
370 }
371
Jason Sams326e0dd2009-05-22 14:03:28 -0700372}
373
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800374void printPlaybackCpp(FILE *f) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700375 int ct;
376 int ct2;
377
Matt Walab74514d2015-07-20 14:03:25 -0700378 fprintf(f, RSG_API_REPLAY_CPP_DOC);
379
Jason Sams326e0dd2009-05-22 14:03:28 -0700380 fprintf(f, "#include \"rsDevice.h\"\n");
381 fprintf(f, "#include \"rsContext.h\"\n");
382 fprintf(f, "#include \"rsThreadIO.h\"\n");
Alex Sakhartchouk4edf0302012-03-09 10:47:27 -0800383 fprintf(f, "#include \"rsgApiStructs.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700384 fprintf(f, "#include \"rsgApiFuncDecl.h\"\n");
385 fprintf(f, "\n");
386 fprintf(f, "namespace android {\n");
387 fprintf(f, "namespace renderscript {\n");
388 fprintf(f, "\n");
389
Matt Walab74514d2015-07-20 14:03:25 -0700390 // Generate functions to play back messages sent from the local FIFO.
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800391 for (ct=0; ct < apiCount; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700392 const ApiEntry * api = &apis[ct];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800393 int needFlush = 0;
Jason Sams326e0dd2009-05-22 14:03:28 -0700394
Jason Sams186e5912011-04-26 14:50:00 -0700395 if (api->direct) {
396 continue;
397 }
398
Matt Walab74514d2015-07-20 14:03:25 -0700399 fprintf(f, "void " RS_PLAYBACK_PREFIX "%s(Context *con, const void *vp, size_t cmdSizeBytes) {\n", api->name);
Jason Sams186e5912011-04-26 14:50:00 -0700400 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 -0700401
Jason Sams5f27d6f2012-02-07 15:32:08 -0800402 if (hasInlineDataPointers(api)) {
403 fprintf(f, " const uint8_t *baseData = 0;\n");
404 fprintf(f, " if (cmdSizeBytes != sizeof(RS_CMD_%s)) {\n", api->name);
405 fprintf(f, " baseData = &((const uint8_t *)vp)[sizeof(*cmd)];\n");
406 fprintf(f, " }\n");
407 }
408
Jason Sams186e5912011-04-26 14:50:00 -0700409 fprintf(f, " ");
410 if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700411 fprintf(f, "\n ");
412 printVarType(f, &api->ret);
413 fprintf(f, " ret = ");
Jason Sams326e0dd2009-05-22 14:03:28 -0700414 }
Matt Walab74514d2015-07-20 14:03:25 -0700415 fprintf(f, RS_INTERNAL_PREFIX "%s(con", api->name);
Jason Sams186e5912011-04-26 14:50:00 -0700416 for (ct2=0; ct2 < api->paramCount; ct2++) {
417 const VarType *vt = &api->params[ct2];
Jason Sams5f27d6f2012-02-07 15:32:08 -0800418 needFlush += vt->ptrLevel;
419
420 if (hasInlineDataPointers(api) && vt->ptrLevel) {
Chris Wailesdb896f02014-07-16 18:20:06 -0700421 fprintf(f, ",\n cmd->%s_length == 0 ? NULL : (const %s *)&baseData[(intptr_t)cmd->%s]",
422 vt->name, vt->typeName, vt->name);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800423 } else {
424 fprintf(f, ",\n cmd->%s", vt->name);
425 }
Jason Sams186e5912011-04-26 14:50:00 -0700426 }
427 fprintf(f, ");\n");
428
Jason Sams5f27d6f2012-02-07 15:32:08 -0800429 if (hasInlineDataPointers(api)) {
Jason Samsee5cf002012-02-07 18:54:03 -0800430 fprintf(f, " size_t totalSize = 0;\n");
431 for (ct2=0; ct2 < api->paramCount; ct2++) {
432 if (api->params[ct2].ptrLevel) {
433 fprintf(f, " totalSize += cmd->%s_length;\n", api->params[ct2].name);
434 }
435 }
436
437 fprintf(f, " if ((totalSize != 0) && (cmdSizeBytes == sizeof(RS_CMD_%s))) {\n", api->name);
Jason Sams5f27d6f2012-02-07 15:32:08 -0800438 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
439 fprintf(f, " }\n");
440 } else if (api->ret.typeName[0]) {
Jason Sams1a4efa32011-05-17 15:01:29 -0700441 fprintf(f, " con->mIO.coreSetReturn(&ret, sizeof(ret));\n");
Jason Sams5f27d6f2012-02-07 15:32:08 -0800442 } else if (api->sync || needFlush) {
443 fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n");
Jason Sams1a4efa32011-05-17 15:01:29 -0700444 }
445
Jason Sams326e0dd2009-05-22 14:03:28 -0700446 fprintf(f, "};\n\n");
447 }
448
Matt Walab74514d2015-07-20 14:03:25 -0700449 // Generate the globally accessible table of playback functions.
Jason Sams20087472011-05-06 14:14:30 -0700450 fprintf(f, "RsPlaybackLocalFunc gPlaybackFuncs[%i] = {\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700451 fprintf(f, " NULL,\n");
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800452 for (ct=0; ct < apiCount; ct++) {
Jason Sams186e5912011-04-26 14:50:00 -0700453 if (apis[ct].direct) {
454 fprintf(f, " NULL,\n");
455 } else {
Matt Walab74514d2015-07-20 14:03:25 -0700456 fprintf(f, " %s%s,\n", RS_PLAYBACK_PREFIX, apis[ct].name);
Jason Sams186e5912011-04-26 14:50:00 -0700457 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700458 }
459 fprintf(f, "};\n");
460
461 fprintf(f, "};\n");
462 fprintf(f, "};\n");
463}
464
Jason Sams5fb1aeb2011-04-27 15:12:49 -0700465void yylex();
466
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800467int main(int argc, char **argv) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700468 if (argc != 3) {
469 fprintf(stderr, "usage: %s commandFile outFile\n", argv[0]);
470 return 1;
471 }
472 const char* rsgFile = argv[1];
473 const char* outFile = argv[2];
Nick Kralevich95e17022018-12-11 14:35:57 -0800474 FILE* input = fopen(rsgFile, "re");
Jason Sams326e0dd2009-05-22 14:03:28 -0700475
476 char choice = fgetc(input);
477 fclose(input);
478
479 if (choice < '0' || choice > '3') {
480 fprintf(stderr, "Uknown command: \'%c\'\n", choice);
481 return -2;
482 }
483
484 yylex();
485 // printf("# of lines = %d\n", num_lines);
486
Nick Kralevich95e17022018-12-11 14:35:57 -0800487 FILE *f = fopen(outFile, "we");
Jason Sams326e0dd2009-05-22 14:03:28 -0700488
489 printFileHeader(f);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800490 switch (choice) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700491 case '0': // rsgApiStructs.h
492 {
493 fprintf(f, "\n");
494 fprintf(f, "#include \"rsContext.h\"\n");
Jason Sams20087472011-05-06 14:14:30 -0700495 fprintf(f, "#include \"rsFifo.h\"\n");
Jason Sams326e0dd2009-05-22 14:03:28 -0700496 fprintf(f, "\n");
497 fprintf(f, "namespace android {\n");
498 fprintf(f, "namespace renderscript {\n");
499 printStructures(f);
Matt Walab74514d2015-07-20 14:03:25 -0700500 printFuncDecls(f, RS_INTERNAL_PREFIX, 1, 0);
501 printPlaybackFuncs(f, RS_PLAYBACK_PREFIX);
Jason Sams20087472011-05-06 14:14:30 -0700502 fprintf(f, "typedef void (*RsPlaybackLocalFunc)(Context *, const void *, size_t sizeBytes);\n");
Jason Sams20087472011-05-06 14:14:30 -0700503 fprintf(f, "extern RsPlaybackLocalFunc gPlaybackFuncs[%i];\n", apiCount + 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700504
505 fprintf(f, "}\n");
506 fprintf(f, "}\n");
507 }
508 break;
509
510 case '1': // rsgApiFuncDecl.h
511 {
Tim Murrayf6023e42013-07-17 12:07:28 -0700512 printFuncDecls(f, "rs", 0, 1);
Jason Sams326e0dd2009-05-22 14:03:28 -0700513 }
514 break;
515
516 case '2': // rsgApi.cpp
517 {
518 printApiCpp(f);
519 }
520 break;
521
522 case '3': // rsgApiReplay.cpp
523 {
Jason Sams326e0dd2009-05-22 14:03:28 -0700524 printPlaybackCpp(f);
525 }
526 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700527 }
528 fclose(f);
529 return 0;
530}