Joe Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 1 | #include "spec.h" |
| 2 | #include <stdio.h> |
Shih-wei Liao | da3b58d | 2012-08-03 04:24:33 -0700 | [diff] [blame] | 3 | #include <string.h> |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 4 | |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 5 | #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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 24 | void printFileHeader(FILE *f) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 25 | fprintf(f, "/*\n"); |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 26 | fprintf(f, " * Copyright (C) 2015 The Android Open Source Project\n"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | 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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 42 | void printVarType(FILE *f, const VarType *vt) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | int ct; |
| 44 | if (vt->isConst) { |
| 45 | fprintf(f, "const "); |
| 46 | } |
| 47 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 48 | switch (vt->type) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 49 | 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 Onorato | 84614dd | 2009-08-10 15:01:51 -0700 | [diff] [blame] | 65 | fprintf(f, "%s", vt->typeName); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 66 | break; |
| 67 | } |
| 68 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 69 | if (vt->ptrLevel) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 70 | fprintf(f, " "); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 71 | for (ct=0; ct < vt->ptrLevel; ct++) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 72 | fprintf(f, "*"); |
| 73 | } |
| 74 | } |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void printVarTypeAndName(FILE *f, const VarType *vt) { |
| 78 | printVarType(f, vt); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 79 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 80 | if (vt->name[0]) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 81 | fprintf(f, " %s", vt->name); |
| 82 | } |
| 83 | } |
| 84 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 85 | void printArgList(FILE *f, const ApiEntry * api, int assumePrevious) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 86 | int ct; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 87 | for (ct=0; ct < api->paramCount; ct++) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 88 | if (ct || assumePrevious) { |
| 89 | fprintf(f, ", "); |
| 90 | } |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 91 | printVarTypeAndName(f, &api->params[ct]); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 95 | void printStructures(FILE *f) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 96 | int ct; |
| 97 | int ct2; |
| 98 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 99 | for (ct=0; ct < apiCount; ct++) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 100 | 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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 104 | for (ct=0; ct < apiCount; ct++) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 105 | const ApiEntry * api = &apis[ct]; |
| 106 | fprintf(f, "#define RS_CMD_ID_%s %i\n", api->name, ct+1); |
Stephen Hines | f047575 | 2014-06-25 00:06:26 -0700 | [diff] [blame] | 107 | fprintf(f, "struct __attribute__((packed)) RS_CMD_%s_rec {\n", api->name); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 108 | //fprintf(f, " RsCommandHeader _hdr;\n"); |
| 109 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 110 | for (ct2=0; ct2 < api->paramCount; ct2++) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 111 | fprintf(f, " "); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 112 | printVarTypeAndName(f, &api->params[ct2]); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 113 | fprintf(f, ";\n"); |
| 114 | } |
| 115 | fprintf(f, "};\n\n"); |
| 116 | } |
| 117 | } |
| 118 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 119 | void printFuncDecl(FILE *f, const ApiEntry *api, const char *prefix, int addContext, int isFnPtr) { |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 120 | printVarTypeAndName(f, &api->ret); |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 121 | if (isFnPtr) { |
| 122 | char t[1024]; |
Michael Butler | ca451c3 | 2017-04-19 18:47:55 -0700 | [diff] [blame] | 123 | strncpy(t, api->name, sizeof(t)-1); |
| 124 | t[sizeof(t)-1] = '\0'; |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 125 | 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 Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 134 | if (!api->nocontext) { |
| 135 | if (addContext) { |
| 136 | fprintf(f, "Context *"); |
| 137 | } else { |
| 138 | fprintf(f, "RsContext rsc"); |
| 139 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 140 | } |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 141 | printArgList(f, api, !api->nocontext); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 142 | fprintf(f, ")"); |
| 143 | } |
| 144 | |
Tim Murray | f6023e4 | 2013-07-17 12:07:28 -0700 | [diff] [blame] | 145 | void printFuncDecls(FILE *f, const char *prefix, int addContext, int externC) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 146 | int ct; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 147 | for (ct=0; ct < apiCount; ct++) { |
Tim Murray | f6023e4 | 2013-07-17 12:07:28 -0700 | [diff] [blame] | 148 | if (externC) { |
| 149 | fprintf(f, "extern \"C\" "); |
| 150 | } |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 151 | printFuncDecl(f, &apis[ct], prefix, addContext, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 152 | fprintf(f, ";\n"); |
| 153 | } |
| 154 | fprintf(f, "\n\n"); |
| 155 | } |
| 156 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 157 | void printPlaybackFuncs(FILE *f, const char *prefix) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 158 | int ct; |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 159 | for (ct=0; ct < apiCount; ct++) { |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 160 | if (apis[ct].direct) { |
| 161 | continue; |
| 162 | } |
| 163 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 164 | fprintf(f, "void %s%s (Context *, const void *);\n", prefix, apis[ct].name); |
| 165 | } |
| 166 | } |
| 167 | |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 168 | static int hasInlineDataPointers(const ApiEntry * api) { |
| 169 | int ret = 0; |
| 170 | int ct; |
Jason Sams | b693112 | 2011-05-02 16:29:42 -0700 | [diff] [blame] | 171 | if (api->sync || api->ret.typeName[0]) { |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 172 | 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 Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 194 | void printApiCpp(FILE *f) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 195 | int ct; |
| 196 | int ct2; |
| 197 | |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 198 | fprintf(f, RSG_API_CPP_DOC); |
| 199 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 200 | fprintf(f, "#include \"rsDevice.h\"\n"); |
| 201 | fprintf(f, "#include \"rsContext.h\"\n"); |
| 202 | fprintf(f, "#include \"rsThreadIO.h\"\n"); |
Alex Sakhartchouk | 4edf030 | 2012-03-09 10:47:27 -0800 | [diff] [blame] | 203 | fprintf(f, "#include \"rsgApiStructs.h\"\n"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 204 | fprintf(f, "#include \"rsgApiFuncDecl.h\"\n"); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 205 | fprintf(f, "#include \"rsFifo.h\"\n"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 206 | fprintf(f, "\n"); |
Chih-Hung Hsieh | 11496ac | 2016-11-15 15:14:05 -0800 | [diff] [blame] | 207 | fprintf(f, "using namespace android; // NOLINT\n"); |
| 208 | fprintf(f, "using namespace android::renderscript; // NOLINT\n"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 209 | fprintf(f, "\n"); |
| 210 | |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 211 | // Generate RS funcs that send messages on the local FIFO. |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 212 | for (ct=0; ct < apiCount; ct++) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 213 | int needFlush = 0; |
| 214 | const ApiEntry * api = &apis[ct]; |
| 215 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 216 | fprintf(f, "static "); |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 217 | printFuncDecl(f, api, LOCAL_FIFO_PREFIX, 0, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 218 | fprintf(f, "\n{\n"); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 219 | if (api->direct) { |
| 220 | fprintf(f, " "); |
| 221 | if (api->ret.typeName[0]) { |
| 222 | fprintf(f, "return "); |
| 223 | } |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 224 | fprintf(f, RS_INTERNAL_PREFIX "%s(", api->name); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 225 | if (!api->nocontext) { |
| 226 | fprintf(f, "(Context *)rsc"); |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 227 | } |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 228 | for (ct2=0; ct2 < api->paramCount; ct2++) { |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 229 | const VarType *vt = &api->params[ct2]; |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 230 | if (ct2 > 0 || !api->nocontext) { |
| 231 | fprintf(f, ", "); |
| 232 | } |
| 233 | fprintf(f, "%s", vt->name); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 234 | } |
| 235 | fprintf(f, ");\n"); |
Tim Murray | c2eca55 | 2014-01-07 15:05:47 -0800 | [diff] [blame] | 236 | } else if (api->handcodeApi) { |
| 237 | // handle handcode path |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 238 | fprintf(f, " " LOCAL_FIFO_PREFIX "%s_handcode(", api->name); |
Tim Murray | c2eca55 | 2014-01-07 15:05:47 -0800 | [diff] [blame] | 239 | 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 Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 251 | } else { |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 252 | // 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 Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 258 | fprintf(f, RS_INTERNAL_PREFIX "%s(", api->name); |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 259 | 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 Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 275 | fprintf(f, " ThreadIO *io = &((Context *)rsc)->mIO;\n"); |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 276 | fprintf(f, " const size_t size = sizeof(RS_CMD_%s);\n", api->name); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 277 | if (hasInlineDataPointers(api)) { |
Tim Murray | 099bc26 | 2013-03-20 16:54:03 -0700 | [diff] [blame] | 278 | fprintf(f, " size_t dataSize = 0;\n"); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 279 | 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 Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 287 | //fprintf(f, " ALOGE(\"add command %s\\n\");\n", api->name); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 288 | if (hasInlineDataPointers(api)) { |
Jason Sams | b693112 | 2011-05-02 16:29:42 -0700 | [diff] [blame] | 289 | fprintf(f, " RS_CMD_%s *cmd = NULL;\n", api->name); |
Jason Sams | bda75a9 | 2012-02-16 17:21:32 -0800 | [diff] [blame] | 290 | fprintf(f, " if (dataSize < io->getMaxInlineSize()) {;\n"); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 291 | fprintf(f, " cmd = static_cast<RS_CMD_%s *>(io->coreHeader(RS_CMD_ID_%s, dataSize + size));\n", api->name, api->name); |
Jason Sams | b693112 | 2011-05-02 16:29:42 -0700 | [diff] [blame] | 292 | fprintf(f, " } else {\n"); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 293 | fprintf(f, " cmd = static_cast<RS_CMD_%s *>(io->coreHeader(RS_CMD_ID_%s, size));\n", api->name, api->name); |
Jason Sams | b693112 | 2011-05-02 16:29:42 -0700 | [diff] [blame] | 294 | fprintf(f, " }\n"); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 295 | fprintf(f, " uint8_t *payload = (uint8_t *)&cmd[1];\n"); |
Jason Sams | 6e58aef | 2011-04-29 16:23:40 -0700 | [diff] [blame] | 296 | } else { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 297 | 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 Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 298 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 299 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 300 | for (ct2=0; ct2 < api->paramCount; ct2++) { |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 301 | const VarType *vt = &api->params[ct2]; |
| 302 | needFlush += vt->ptrLevel; |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 303 | if (vt->ptrLevel && hasInlineDataPointers(api)) { |
Chris Wailes | db896f0 | 2014-07-16 18:20:06 -0700 | [diff] [blame] | 304 | 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 Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 307 | fprintf(f, " memcpy(payload, %s, %s_length);\n", vt->name, vt->name); |
| 308 | fprintf(f, " cmd->%s = (", vt->name); |
| 309 | printVarType(f, vt); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 310 | fprintf(f, ")(payload - ((uint8_t *)&cmd[1]));\n"); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 311 | 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 Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 319 | } |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 320 | if (api->ret.typeName[0] || api->sync) { |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 321 | needFlush = 1; |
| 322 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 323 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 324 | fprintf(f, " io->coreCommit();\n"); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 325 | if (hasInlineDataPointers(api)) { |
Jason Sams | bda75a9 | 2012-02-16 17:21:32 -0800 | [diff] [blame] | 326 | fprintf(f, " if (dataSize >= io->getMaxInlineSize()) {\n"); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 327 | fprintf(f, " io->coreGetReturn(NULL, 0);\n"); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 328 | fprintf(f, " }\n"); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 329 | } else if (api->ret.typeName[0]) { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 330 | fprintf(f, "\n "); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 331 | printVarType(f, &api->ret); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 332 | fprintf(f, " ret;\n"); |
| 333 | fprintf(f, " io->coreGetReturn(&ret, sizeof(ret));\n"); |
| 334 | fprintf(f, " return ret;\n"); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 335 | } else if (needFlush) { |
| 336 | fprintf(f, " io->coreGetReturn(NULL, 0);\n"); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 337 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 338 | } |
| 339 | fprintf(f, "};\n\n"); |
| 340 | } |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 341 | |
| 342 | fprintf(f, "\n"); |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 343 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 344 | for (ct=0; ct < apiCount; ct++) { |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 345 | const ApiEntry * api = &apis[ct]; |
| 346 | |
Tim Murray | f6023e4 | 2013-07-17 12:07:28 -0700 | [diff] [blame] | 347 | fprintf(f, "extern \"C\" "); |
| 348 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 349 | 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 Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 355 | fprintf(f, LOCAL_FIFO_PREFIX "%s(", api->name); |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 356 | |
| 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 Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 374 | void printPlaybackCpp(FILE *f) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 375 | int ct; |
| 376 | int ct2; |
| 377 | |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 378 | fprintf(f, RSG_API_REPLAY_CPP_DOC); |
| 379 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 380 | fprintf(f, "#include \"rsDevice.h\"\n"); |
| 381 | fprintf(f, "#include \"rsContext.h\"\n"); |
| 382 | fprintf(f, "#include \"rsThreadIO.h\"\n"); |
Alex Sakhartchouk | 4edf030 | 2012-03-09 10:47:27 -0800 | [diff] [blame] | 383 | fprintf(f, "#include \"rsgApiStructs.h\"\n"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 384 | 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 Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 390 | // Generate functions to play back messages sent from the local FIFO. |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 391 | for (ct=0; ct < apiCount; ct++) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 392 | const ApiEntry * api = &apis[ct]; |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 393 | int needFlush = 0; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 394 | |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 395 | if (api->direct) { |
| 396 | continue; |
| 397 | } |
| 398 | |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 399 | fprintf(f, "void " RS_PLAYBACK_PREFIX "%s(Context *con, const void *vp, size_t cmdSizeBytes) {\n", api->name); |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 400 | fprintf(f, " const RS_CMD_%s *cmd = static_cast<const RS_CMD_%s *>(vp);\n", api->name, api->name); |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 401 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 402 | 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 Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 409 | fprintf(f, " "); |
| 410 | if (api->ret.typeName[0]) { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 411 | fprintf(f, "\n "); |
| 412 | printVarType(f, &api->ret); |
| 413 | fprintf(f, " ret = "); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 414 | } |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 415 | fprintf(f, RS_INTERNAL_PREFIX "%s(con", api->name); |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 416 | for (ct2=0; ct2 < api->paramCount; ct2++) { |
| 417 | const VarType *vt = &api->params[ct2]; |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 418 | needFlush += vt->ptrLevel; |
| 419 | |
| 420 | if (hasInlineDataPointers(api) && vt->ptrLevel) { |
Chris Wailes | db896f0 | 2014-07-16 18:20:06 -0700 | [diff] [blame] | 421 | fprintf(f, ",\n cmd->%s_length == 0 ? NULL : (const %s *)&baseData[(intptr_t)cmd->%s]", |
| 422 | vt->name, vt->typeName, vt->name); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 423 | } else { |
| 424 | fprintf(f, ",\n cmd->%s", vt->name); |
| 425 | } |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 426 | } |
| 427 | fprintf(f, ");\n"); |
| 428 | |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 429 | if (hasInlineDataPointers(api)) { |
Jason Sams | ee5cf00 | 2012-02-07 18:54:03 -0800 | [diff] [blame] | 430 | 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 Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 438 | fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n"); |
| 439 | fprintf(f, " }\n"); |
| 440 | } else if (api->ret.typeName[0]) { |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 441 | fprintf(f, " con->mIO.coreSetReturn(&ret, sizeof(ret));\n"); |
Jason Sams | 5f27d6f | 2012-02-07 15:32:08 -0800 | [diff] [blame] | 442 | } else if (api->sync || needFlush) { |
| 443 | fprintf(f, " con->mIO.coreSetReturn(NULL, 0);\n"); |
Jason Sams | 1a4efa3 | 2011-05-17 15:01:29 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 446 | fprintf(f, "};\n\n"); |
| 447 | } |
| 448 | |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 449 | // Generate the globally accessible table of playback functions. |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 450 | fprintf(f, "RsPlaybackLocalFunc gPlaybackFuncs[%i] = {\n", apiCount + 1); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 451 | fprintf(f, " NULL,\n"); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 452 | for (ct=0; ct < apiCount; ct++) { |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 453 | if (apis[ct].direct) { |
| 454 | fprintf(f, " NULL,\n"); |
| 455 | } else { |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 456 | fprintf(f, " %s%s,\n", RS_PLAYBACK_PREFIX, apis[ct].name); |
Jason Sams | 186e591 | 2011-04-26 14:50:00 -0700 | [diff] [blame] | 457 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 458 | } |
| 459 | fprintf(f, "};\n"); |
| 460 | |
| 461 | fprintf(f, "};\n"); |
| 462 | fprintf(f, "};\n"); |
| 463 | } |
| 464 | |
Jason Sams | 5fb1aeb | 2011-04-27 15:12:49 -0700 | [diff] [blame] | 465 | void yylex(); |
| 466 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 467 | int main(int argc, char **argv) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 468 | 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 Kralevich | 95e1702 | 2018-12-11 14:35:57 -0800 | [diff] [blame] | 474 | FILE* input = fopen(rsgFile, "re"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 475 | |
| 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 Kralevich | 95e1702 | 2018-12-11 14:35:57 -0800 | [diff] [blame] | 487 | FILE *f = fopen(outFile, "we"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 488 | |
| 489 | printFileHeader(f); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 490 | switch (choice) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 491 | case '0': // rsgApiStructs.h |
| 492 | { |
| 493 | fprintf(f, "\n"); |
| 494 | fprintf(f, "#include \"rsContext.h\"\n"); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 495 | fprintf(f, "#include \"rsFifo.h\"\n"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 496 | fprintf(f, "\n"); |
| 497 | fprintf(f, "namespace android {\n"); |
| 498 | fprintf(f, "namespace renderscript {\n"); |
| 499 | printStructures(f); |
Matt Wala | b74514d | 2015-07-20 14:03:25 -0700 | [diff] [blame] | 500 | printFuncDecls(f, RS_INTERNAL_PREFIX, 1, 0); |
| 501 | printPlaybackFuncs(f, RS_PLAYBACK_PREFIX); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 502 | fprintf(f, "typedef void (*RsPlaybackLocalFunc)(Context *, const void *, size_t sizeBytes);\n"); |
Jason Sams | 2008747 | 2011-05-06 14:14:30 -0700 | [diff] [blame] | 503 | fprintf(f, "extern RsPlaybackLocalFunc gPlaybackFuncs[%i];\n", apiCount + 1); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 504 | |
| 505 | fprintf(f, "}\n"); |
| 506 | fprintf(f, "}\n"); |
| 507 | } |
| 508 | break; |
| 509 | |
| 510 | case '1': // rsgApiFuncDecl.h |
| 511 | { |
Tim Murray | f6023e4 | 2013-07-17 12:07:28 -0700 | [diff] [blame] | 512 | printFuncDecls(f, "rs", 0, 1); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 513 | } |
| 514 | break; |
| 515 | |
| 516 | case '2': // rsgApi.cpp |
| 517 | { |
| 518 | printApiCpp(f); |
| 519 | } |
| 520 | break; |
| 521 | |
| 522 | case '3': // rsgApiReplay.cpp |
| 523 | { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 524 | printPlaybackCpp(f); |
| 525 | } |
| 526 | break; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 527 | } |
| 528 | fclose(f); |
| 529 | return 0; |
| 530 | } |