blob: 669718969523eb1167f86d3c798879e3874ceda9 [file] [log] [blame]
Kenny Rootbd393b72010-03-11 18:20:12 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017import java.io.PrintStream;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018import java.util.ArrayList;
19import java.util.HashSet;
20import java.util.Iterator;
21import java.util.List;
22
Jack Palevichffac1ef2009-04-14 19:00:09 -070023public class JniCodeEmitter {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025 static final boolean mUseCPlusPlus = true;
Jack Palevichffac1ef2009-04-14 19:00:09 -070026 protected boolean mUseContextPointer = true;
Jack Palevich427f5852009-04-15 19:13:17 -070027 protected boolean mUseStaticMethods = false;
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -070028 protected boolean mUseSimpleMethodNames = false;
29 protected boolean mUseHideCommentForAPI = false;
Jack Palevichffac1ef2009-04-14 19:00:09 -070030 protected String mClassPathName;
31 protected ParameterChecker mChecker;
32 protected List<String> nativeRegistrations = new ArrayList<String>();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033 boolean needsExit;
Jack Palevichffac1ef2009-04-14 19:00:09 -070034 protected static String indent = " ";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035 HashSet<String> mFunctionsEmitted = new HashSet<String>();
36
Jack Palevichffac1ef2009-04-14 19:00:09 -070037 public static String getJniName(JType jType) {
38 String jniName = "";
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -070039 if (jType.isEGLHandle()) {
40 return (jType.isArray() ? "[" : "" ) + "Landroid/opengl/" + jType.getBaseType() + ";";
41 } else if (jType.isClass()) {
Jack Palevichffac1ef2009-04-14 19:00:09 -070042 return "L" + jType.getBaseType() + ";";
43 } else if (jType.isArray()) {
44 jniName = "[";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045 }
Jack Palevichffac1ef2009-04-14 19:00:09 -070046
47 String baseType = jType.getBaseType();
48 if (baseType.equals("int")) {
49 jniName += "I";
50 } else if (baseType.equals("float")) {
51 jniName += "F";
52 } else if (baseType.equals("boolean")) {
53 jniName += "Z";
54 } else if (baseType.equals("short")) {
55 jniName += "S";
56 } else if (baseType.equals("long")) {
Andy McFadden72841452013-03-01 16:25:32 -080057 jniName += "J";
Jack Palevichffac1ef2009-04-14 19:00:09 -070058 } else if (baseType.equals("byte")) {
59 jniName += "B";
Jack Palevich50d0b142009-11-19 16:34:55 +080060 } else if (baseType.equals("String")) {
61 jniName += "Ljava/lang/String;";
62 } else if (baseType.equals("void")) {
63 // nothing.
64 } else {
Elliott Hughes98757502011-04-08 20:01:01 -070065 throw new RuntimeException("Unknown primitive basetype " + baseType);
Jack Palevichffac1ef2009-04-14 19:00:09 -070066 }
67 return jniName;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068 }
69
Jack Palevichffac1ef2009-04-14 19:00:09 -070070 public void emitCode(CFunc cfunc, String original,
71 PrintStream javaInterfaceStream,
72 PrintStream javaImplStream,
73 PrintStream cStream) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 JFunc jfunc;
75 String signature;
76 boolean duplicate;
Jack Palevich6cbca502009-04-13 16:22:25 -070077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 if (cfunc.hasTypedPointerArg()) {
79 jfunc = JFunc.convert(cfunc, true);
80
81 // Don't emit duplicate functions
82 // These may appear because they are defined in multiple
83 // Java interfaces (e.g., GL11/GL11ExtensionPack)
84 signature = jfunc.toString();
85 duplicate = false;
86 if (mFunctionsEmitted.contains(signature)) {
87 duplicate = true;
88 } else {
89 mFunctionsEmitted.add(signature);
90 }
91
92 if (!duplicate) {
Jack Palevichffac1ef2009-04-14 19:00:09 -070093 emitNativeDeclaration(jfunc, javaImplStream);
94 emitJavaCode(jfunc, javaImplStream);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 }
Jack Palevich427f5852009-04-15 19:13:17 -070096 if (javaInterfaceStream != null) {
97 emitJavaInterfaceCode(jfunc, javaInterfaceStream);
98 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 if (!duplicate) {
Jack Palevichffac1ef2009-04-14 19:00:09 -0700100 emitJniCode(jfunc, cStream);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 }
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700102 // Don't create IOBuffer versions of the EGL functions
103 if (cfunc.hasEGLHandleArg()) {
104 return;
105 }
Courtney Goeltzenleuchterb28648c2018-07-18 09:38:47 -0600106 // eglGetPlatformDisplay does not have any EGLHandleArgs
107 // but we do not want to create IOBuffers of this, so
108 // exit
109 if (cfunc.getName().equals("eglGetPlatformDisplay")) {
110 return;
111 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 }
113
114 jfunc = JFunc.convert(cfunc, false);
115
116 signature = jfunc.toString();
117 duplicate = false;
118 if (mFunctionsEmitted.contains(signature)) {
119 duplicate = true;
120 } else {
121 mFunctionsEmitted.add(signature);
122 }
123
124 if (!duplicate) {
Jack Palevichffac1ef2009-04-14 19:00:09 -0700125 emitNativeDeclaration(jfunc, javaImplStream);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126 }
Jack Palevich427f5852009-04-15 19:13:17 -0700127 if (javaInterfaceStream != null) {
128 emitJavaInterfaceCode(jfunc, javaInterfaceStream);
129 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 if (!duplicate) {
Jack Palevichffac1ef2009-04-14 19:00:09 -0700131 emitJavaCode(jfunc, javaImplStream);
132 emitJniCode(jfunc, cStream);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 }
134 }
135
136 public void emitNativeDeclaration(JFunc jfunc, PrintStream out) {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700137 if (mUseHideCommentForAPI) {
138 out.println(" /* @hide C function " + jfunc.getCFunc().getOriginal() + " */");
139 out.println();
140 } else {
141 out.println(" // C function " + jfunc.getCFunc().getOriginal());
142 out.println();
143 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144
145 emitFunction(jfunc, out, true, false);
146 }
147
148 public void emitJavaInterfaceCode(JFunc jfunc, PrintStream out) {
149 emitFunction(jfunc, out, false, true);
150 }
151
152 public void emitJavaCode(JFunc jfunc, PrintStream out) {
153 emitFunction(jfunc, out, false, false);
154 }
Jack Palevich6cbca502009-04-13 16:22:25 -0700155
Jack Palevich66089a32009-12-08 15:43:51 +0800156 boolean isPointerFunc(JFunc jfunc) {
157 String name = jfunc.getName();
158 return (name.endsWith("Pointer") || name.endsWith("PointerOES"))
159 && jfunc.getCFunc().hasPointerArg();
160 }
161
Jack Palevichffac1ef2009-04-14 19:00:09 -0700162 void emitFunctionCall(JFunc jfunc, PrintStream out, String iii, boolean grabArray) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163 boolean isVoid = jfunc.getType().isVoid();
Jack Palevich66089a32009-12-08 15:43:51 +0800164 boolean isPointerFunc = isPointerFunc(jfunc);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165
166 if (!isVoid) {
167 out.println(iii +
168 jfunc.getType() + " _returnValue;");
169 }
170 out.println(iii +
171 (isVoid ? "" : "_returnValue = ") +
172 jfunc.getName() +
173 (isPointerFunc ? "Bounds" : "" ) +
174 "(");
Jack Palevich6cbca502009-04-13 16:22:25 -0700175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 int numArgs = jfunc.getNumArgs();
177 for (int i = 0; i < numArgs; i++) {
178 String argName = jfunc.getArgName(i);
179 JType argType = jfunc.getArgType(i);
180
181 if (grabArray && argType.isTypedBuffer()) {
182 String typeName = argType.getBaseType();
183 typeName = typeName.substring(9, typeName.length() - 6);
184 out.println(iii + indent + "get" + typeName + "Array(" + argName + "),");
Jack Palevich6cbca502009-04-13 16:22:25 -0700185 out.print(iii + indent + "getOffset(" + argName + ")");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 } else {
187 out.print(iii + indent + argName);
188 }
189 if (i == numArgs - 1) {
190 if (isPointerFunc) {
191 out.println(",");
192 out.println(iii + indent + argName + ".remaining()");
193 } else {
194 out.println();
195 }
196 } else {
197 out.println(",");
198 }
199 }
Jack Palevich6cbca502009-04-13 16:22:25 -0700200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 out.println(iii + ");");
202 }
203
Jack Palevichffac1ef2009-04-14 19:00:09 -0700204 void printIfcheckPostamble(PrintStream out, boolean isBuffer, boolean emitExceptionCheck,
205 String iii) {
Mathias Agopianbf13ba52013-02-22 19:34:06 -0800206 printIfcheckPostamble(out, isBuffer, emitExceptionCheck,
207 "offset", "_remaining", iii);
208 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209
Jack Palevichffac1ef2009-04-14 19:00:09 -0700210 void printIfcheckPostamble(PrintStream out, boolean isBuffer, boolean emitExceptionCheck,
211 String offset, String remaining, String iii) {
Mathias Agopianbf13ba52013-02-22 19:34:06 -0800212 out.println(iii + " default:");
213 out.println(iii + " _needed = 1;");
214 out.println(iii + " break;");
215 out.println(iii + "}");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216
Mathias Agopianbf13ba52013-02-22 19:34:06 -0800217 out.println(iii + "if (" + remaining + " < _needed) {");
218 out.println(iii + indent + "_exception = 1;");
219 out.println(iii + indent +
220 "_exceptionType = \"java/lang/IllegalArgumentException\";");
221 out.println(iii + indent +
222 "_exceptionMessage = \"" +
223 (isBuffer ? "remaining()" : "length - " + offset) +
224 " < needed\";");
225 out.println(iii + indent + "goto exit;");
226 out.println(iii + "}");
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700227
Mathias Agopianbf13ba52013-02-22 19:34:06 -0800228 needsExit = true;
229 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230
Pablo Ceballosb62e2422015-10-01 18:25:56 -0700231 boolean isNullAllowed(CFunc cfunc, String cname) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 String[] checks = mChecker.getChecks(cfunc.getName());
233 int index = 1;
234 if (checks != null) {
235 while (index < checks.length) {
Pablo Ceballosb62e2422015-10-01 18:25:56 -0700236 if (checks[index].equals("nullAllowed") &&
237 checks[index + 1].equals(cname)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 return true;
239 } else {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700240 index = skipOneCheck(checks, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241 }
242 }
243 }
244 return false;
245 }
246
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700247 boolean hasCheckTest(CFunc cfunc) {
248 String[] checks = mChecker.getChecks(cfunc.getName());
249 int index = 1;
250 if (checks != null) {
251 while (index < checks.length) {
252 if (checks[index].startsWith("check")) {
253 return true;
254 } else {
255 index = skipOneCheck(checks, index);
256 }
257 }
258 }
259 return false;
260 }
261
Pablo Ceballosb62e2422015-10-01 18:25:56 -0700262 boolean hasCheckTest(CFunc cfunc, String cname) {
263 String[] checks = mChecker.getChecks(cfunc.getName());
264 int index = 1;
265 if (checks != null) {
266 while (index < checks.length) {
267 if (checks[index].startsWith("check") &&
268 cname != null && cname.equals(checks[index + 1])) {
269 return true;
270 } else {
271 index = skipOneCheck(checks, index);
272 }
273 }
274 }
275 return false;
276 }
277
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700278 boolean hasIfTest(CFunc cfunc) {
279 String[] checks = mChecker.getChecks(cfunc.getName());
280 int index = 1;
281 if (checks != null) {
282 while (index < checks.length) {
283 if (checks[index].startsWith("ifcheck")) {
284 return true;
285 } else {
286 index = skipOneCheck(checks, index);
287 }
288 }
289 }
290 return false;
291 }
292
293 int skipOneCheck(String[] checks, int index) {
294 if (checks[index].equals("return")) {
295 index += 2;
296 } else if (checks[index].startsWith("check")) {
297 index += 3;
298 } else if (checks[index].startsWith("sentinel")) {
299 index += 3;
300 } else if (checks[index].equals("ifcheck")) {
301 index += 5;
302 } else if (checks[index].equals("unsupported")) {
303 index += 1;
304 } else if (checks[index].equals("requires")) {
305 index += 2;
306 } else if (checks[index].equals("nullAllowed")) {
Pablo Ceballosb62e2422015-10-01 18:25:56 -0700307 index += 2;
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700308 } else {
309 System.out.println("Error: unknown keyword \"" +
310 checks[index] + "\"");
311 System.exit(0);
312 }
313
314 return index;
315 }
316
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 String getErrorReturnValue(CFunc cfunc) {
318 CType returnType = cfunc.getType();
319 boolean isVoid = returnType.isVoid();
320 if (isVoid) {
321 return null;
322 }
323
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700324 if (returnType.getBaseType().startsWith("EGL")) {
325 return "(" + returnType.getDeclaration() + ") 0";
326 }
327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800328 String[] checks = mChecker.getChecks(cfunc.getName());
329
330 int index = 1;
331 if (checks != null) {
332 while (index < checks.length) {
333 if (checks[index].equals("return")) {
334 return checks[index + 1];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800335 } else {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700336 index = skipOneCheck(checks, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800337 }
338 }
339 }
340
341 return null;
342 }
343
344 boolean isUnsupportedFunc(CFunc cfunc) {
345 String[] checks = mChecker.getChecks(cfunc.getName());
346 int index = 1;
347 if (checks != null) {
348 while (index < checks.length) {
349 if (checks[index].equals("unsupported")) {
350 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351 } else {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700352 index = skipOneCheck(checks, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800353 }
354 }
355 }
356 return false;
357 }
Elliott Hughes98757502011-04-08 20:01:01 -0700358
Jack Paleviche44e45c2010-01-28 20:28:32 +0800359 String isRequiresFunc(CFunc cfunc) {
360 String[] checks = mChecker.getChecks(cfunc.getName());
361 int index = 1;
362 if (checks != null) {
363 while (index < checks.length) {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700364 if (checks[index].equals("requires")) {
Jack Paleviche44e45c2010-01-28 20:28:32 +0800365 return checks[index+1];
Jack Paleviche44e45c2010-01-28 20:28:32 +0800366 } else {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700367 index = skipOneCheck(checks, index);
Jack Paleviche44e45c2010-01-28 20:28:32 +0800368 }
369 }
370 }
371 return null;
372 }
Elliott Hughes98757502011-04-08 20:01:01 -0700373
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800374 void emitNativeBoundsChecks(CFunc cfunc, String cname, PrintStream out,
Jack Palevichffac1ef2009-04-14 19:00:09 -0700375 boolean isBuffer, boolean emitExceptionCheck, String offset, String remaining, String iii) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800376
Elliott Hughes98757502011-04-08 20:01:01 -0700377 String[] checks = mChecker.getChecks(cfunc.getName());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800378
Elliott Hughes98757502011-04-08 20:01:01 -0700379 boolean lastWasIfcheck = false;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800380
Elliott Hughes98757502011-04-08 20:01:01 -0700381 int index = 1;
382 if (checks != null) {
383 while (index < checks.length) {
384 if (checks[index].startsWith("check")) {
385 if (lastWasIfcheck) {
386 printIfcheckPostamble(out, isBuffer, emitExceptionCheck,
387 offset, remaining, iii);
388 }
389 lastWasIfcheck = false;
390 if (cname != null && !cname.equals(checks[index + 1])) {
391 index += 3;
392 continue;
393 }
394 out.println(iii + "if (" + remaining + " < " + checks[index + 2] + ") {");
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700395 out.println(iii + indent + "_exception = 1;");
Elliott Hughes98757502011-04-08 20:01:01 -0700396 String exceptionClassName = "java/lang/IllegalArgumentException";
Jack Palevichffac1ef2009-04-14 19:00:09 -0700397 // If the "check" keyword was of the form
398 // "check_<class name>", use the class name in the
399 // exception to be thrown
400 int underscore = checks[index].indexOf('_');
401 if (underscore >= 0) {
Elliott Hughes98757502011-04-08 20:01:01 -0700402 String abbr = checks[index].substring(underscore + 1);
403 if (abbr.equals("AIOOBE")) {
404 exceptionClassName = "java/lang/ArrayIndexOutOfBoundsException";
Jack Palevichffac1ef2009-04-14 19:00:09 -0700405 } else {
Elliott Hughes98757502011-04-08 20:01:01 -0700406 throw new RuntimeException("unknown exception abbreviation: " + abbr);
Jack Palevichffac1ef2009-04-14 19:00:09 -0700407 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800408 }
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700409 out.println(iii + indent +
410 "_exceptionType = \""+exceptionClassName+"\";");
411 out.println(iii + indent +
412 "_exceptionMessage = \"" +
413 (isBuffer ? "remaining()" : "length - " +
414 offset) + " < " + checks[index + 2] +
415 " < needed\";");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416
Elliott Hughes98757502011-04-08 20:01:01 -0700417 out.println(iii + indent + "goto exit;");
Elliott Hughes98757502011-04-08 20:01:01 -0700418 out.println(iii + "}");
419
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700420 needsExit = true;
421
Elliott Hughes98757502011-04-08 20:01:01 -0700422 index += 3;
423 } else if (checks[index].equals("ifcheck")) {
424 String[] matches = checks[index + 4].split(",");
425
426 if (!lastWasIfcheck) {
427 out.println(iii + "int _needed;");
428 out.println(iii + "switch (" + checks[index + 3] + ") {");
429 }
430
431 for (int i = 0; i < matches.length; i++) {
432 out.println("#if defined(" + matches[i] + ")");
433 out.println(iii + " case " + matches[i] + ":");
434 out.println("#endif // defined(" + matches[i] + ")");
435 }
436 out.println(iii + " _needed = " + checks[index + 2] + ";");
437 out.println(iii + " break;");
438
439 lastWasIfcheck = true;
440 index += 5;
Elliott Hughes98757502011-04-08 20:01:01 -0700441 } else {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700442 index = skipOneCheck(checks, index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443 }
444 }
Elliott Hughes98757502011-04-08 20:01:01 -0700445 }
446
447 if (lastWasIfcheck) {
448 printIfcheckPostamble(out, isBuffer, emitExceptionCheck, iii);
449 }
450 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800451
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700452 void emitSentinelCheck(CFunc cfunc, String cname, PrintStream out,
453 boolean isBuffer, boolean emitExceptionCheck, String offset, String remaining, String iii) {
454
455 String[] checks = mChecker.getChecks(cfunc.getName());
456
457 int index = 1;
458 if (checks != null) {
459 while (index < checks.length) {
460 if (checks[index].startsWith("sentinel")) {
461 if (cname != null && !cname.equals(checks[index + 1])) {
462 index += 3;
463 continue;
464 }
465
466 out.println(iii + cname + "_sentinel = false;");
467 out.println(iii + "for (int i = " + remaining +
468 " - 1; i >= 0; i--) {");
469 out.println(iii + indent + "if (" + cname +
470 "[i] == " + checks[index + 2] + "){");
471 out.println(iii + indent + indent +
472 cname + "_sentinel = true;");
473 out.println(iii + indent + indent + "break;");
474 out.println(iii + indent + "}");
475 out.println(iii + "}");
476 out.println(iii +
477 "if (" + cname + "_sentinel == false) {");
478 out.println(iii + indent + "_exception = 1;");
479 out.println(iii + indent +
480 "_exceptionType = \"java/lang/IllegalArgumentException\";");
481 out.println(iii + indent + "_exceptionMessage = \"" + cname +
482 " must contain " + checks[index + 2] + "!\";");
483 out.println(iii + indent + "goto exit;");
484 out.println(iii + "}");
485
486 needsExit = true;
487 index += 3;
488 } else {
489 index = skipOneCheck(checks, index);
490 }
491 }
492 }
493 }
494
Pablo Ceballosb62e2422015-10-01 18:25:56 -0700495 void emitStringCheck(CFunc cfunc, String cname, PrintStream out, String iii) {
496
497 String[] checks = mChecker.getChecks(cfunc.getName());
498
499 int index = 1;
500 if (checks != null) {
501 while (index < checks.length) {
502 if (checks[index].startsWith("check")) {
503 if (cname != null && !cname.equals(checks[index + 1])) {
504 index += 3;
505 continue;
506 }
507 out.println(iii + "_stringlen = _env->GetStringUTFLength(" + cname + ");");
508 out.println(iii + "if (" + checks[index + 2] + " > _stringlen) {");
509 out.println(iii + indent + "_exception = 1;");
510 out.println(iii + indent +
511 "_exceptionType = \"java/lang/ArrayIndexOutOfBoundsException\";");
512 out.println(iii + indent +
513 "_exceptionMessage = \"length of " + cname + " is shorter than " +
514 checks[index + 2] + " argument\";");
515 out.println(iii + indent + "goto exit;");
516 out.println(iii + "}");
517 index += 3;
518 needsExit = true;
519 } else {
520 index = skipOneCheck(checks, index);
521 }
522 }
523 }
524 }
525
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700526 void emitLocalVariablesForSentinel(CFunc cfunc, PrintStream out) {
527
528 String[] checks = mChecker.getChecks(cfunc.getName());
529
530 int index = 1;
531 if (checks != null) {
532 while (index < checks.length) {
533 if (checks[index].startsWith("sentinel")) {
534 String cname = checks[index + 1];
535 out.println(indent + "bool " + cname + "_sentinel = false;");
536
537 index += 3;
538
539 } else {
540 index = skipOneCheck(checks, index);
541 }
542 }
543 }
544 }
545
Jack Palevichffac1ef2009-04-14 19:00:09 -0700546 boolean hasNonConstArg(JFunc jfunc, CFunc cfunc, List<Integer> nonPrimitiveArgs) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547 if (nonPrimitiveArgs.size() > 0) {
548 for (int i = nonPrimitiveArgs.size() - 1; i >= 0; i--) {
549 int idx = nonPrimitiveArgs.get(i).intValue();
550 int cIndex = jfunc.getArgCIndex(idx);
551 if (jfunc.getArgType(idx).isArray()) {
552 if (!cfunc.getArgType(cIndex).isConst()) {
553 return true;
554 }
555 } else if (jfunc.getArgType(idx).isBuffer()) {
556 if (!cfunc.getArgType(cIndex).isConst()) {
557 return true;
558 }
559 }
560 }
561 }
562
563 return false;
564 }
Jack Palevich6cbca502009-04-13 16:22:25 -0700565
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800566 /**
567 * Emit a function in several variants:
568 *
569 * if nativeDecl: public native <returntype> func(args);
570 *
571 * if !nativeDecl:
572 * if interfaceDecl: public <returntype> func(args);
573 * if !interfaceDecl: public <returntype> func(args) { body }
574 */
Jack Palevichffac1ef2009-04-14 19:00:09 -0700575 void emitFunction(JFunc jfunc, PrintStream out, boolean nativeDecl, boolean interfaceDecl) {
Jack Palevich66089a32009-12-08 15:43:51 +0800576 boolean isPointerFunc = isPointerFunc(jfunc);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577
578 if (!nativeDecl && !interfaceDecl && !isPointerFunc) {
579 // If it's not a pointer function, we've already emitted it
580 // with nativeDecl == true
581 return;
582 }
583
Jack Palevich427f5852009-04-15 19:13:17 -0700584 String maybeStatic = mUseStaticMethods ? "static " : "";
585
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800586 if (isPointerFunc) {
587 out.println(indent +
Jack Palevich427f5852009-04-15 19:13:17 -0700588 (nativeDecl ? "private " + maybeStatic +"native " :
589 (interfaceDecl ? "" : "public ") + maybeStatic) +
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800590 jfunc.getType() + " " +
591 jfunc.getName() +
592 (nativeDecl ? "Bounds" : "") +
593 "(");
594 } else {
595 out.println(indent +
Jack Palevich427f5852009-04-15 19:13:17 -0700596 (nativeDecl ? "public " + maybeStatic +"native " :
597 (interfaceDecl ? "" : "public ") + maybeStatic) +
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800598 jfunc.getType() + " " +
599 jfunc.getName() +
600 "(");
601 }
Jack Palevich6cbca502009-04-13 16:22:25 -0700602
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800603 int numArgs = jfunc.getNumArgs();
604 for (int i = 0; i < numArgs; i++) {
605 String argName = jfunc.getArgName(i);
606 JType argType = jfunc.getArgType(i);
Jack Palevich6cbca502009-04-13 16:22:25 -0700607
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608 out.print(indent + indent + argType + " " + argName);
609 if (i == numArgs - 1) {
610 if (isPointerFunc && nativeDecl) {
611 out.println(",");
612 out.println(indent + indent + "int remaining");
613 } else {
614 out.println();
615 }
616 } else {
617 out.println(",");
618 }
619 }
620
621 if (nativeDecl || interfaceDecl) {
622 out.println(indent + ");");
623 } else {
624 out.println(indent + ") {");
625
626 String iii = indent + indent;
627
Jack Palevich46d25a32009-05-07 18:28:29 -0700628 // emitBoundsChecks(jfunc, out, iii);
629 emitFunctionCall(jfunc, out, iii, false);
630
631 // Set the pointer after we call the native code, so that if
632 // the native code throws an exception we don't modify the
633 // pointer. We assume that the native code is written so that
634 // if an exception is thrown, then the underlying glXXXPointer
635 // function will not have been called.
636
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637 String fname = jfunc.getName();
638 if (isPointerFunc) {
639 // TODO - deal with VBO variants
640 if (fname.equals("glColorPointer")) {
641 out.println(iii + "if ((size == 4) &&");
642 out.println(iii + " ((type == GL_FLOAT) ||");
643 out.println(iii + " (type == GL_UNSIGNED_BYTE) ||");
644 out.println(iii + " (type == GL_FIXED)) &&");
645 out.println(iii + " (stride >= 0)) {");
646 out.println(iii + indent + "_colorPointer = pointer;");
647 out.println(iii + "}");
648 } else if (fname.equals("glNormalPointer")) {
649 out.println(iii + "if (((type == GL_FLOAT) ||");
650 out.println(iii + " (type == GL_BYTE) ||");
651 out.println(iii + " (type == GL_SHORT) ||");
652 out.println(iii + " (type == GL_FIXED)) &&");
653 out.println(iii + " (stride >= 0)) {");
654 out.println(iii + indent + "_normalPointer = pointer;");
655 out.println(iii + "}");
656 } else if (fname.equals("glTexCoordPointer")) {
657 out.println(iii + "if (((size == 2) ||");
658 out.println(iii + " (size == 3) ||");
659 out.println(iii + " (size == 4)) &&");
660 out.println(iii + " ((type == GL_FLOAT) ||");
661 out.println(iii + " (type == GL_BYTE) ||");
662 out.println(iii + " (type == GL_SHORT) ||");
663 out.println(iii + " (type == GL_FIXED)) &&");
664 out.println(iii + " (stride >= 0)) {");
665 out.println(iii + indent + "_texCoordPointer = pointer;");
666 out.println(iii + "}");
667 } else if (fname.equals("glVertexPointer")) {
668 out.println(iii + "if (((size == 2) ||");
669 out.println(iii + " (size == 3) ||");
670 out.println(iii + " (size == 4)) &&");
671 out.println(iii + " ((type == GL_FLOAT) ||");
672 out.println(iii + " (type == GL_BYTE) ||");
673 out.println(iii + " (type == GL_SHORT) ||");
674 out.println(iii + " (type == GL_FIXED)) &&");
675 out.println(iii + " (stride >= 0)) {");
676 out.println(iii + indent + "_vertexPointer = pointer;");
677 out.println(iii + "}");
Jack Palevich66089a32009-12-08 15:43:51 +0800678 } else if (fname.equals("glPointSizePointerOES")) {
679 out.println(iii + "if (((type == GL_FLOAT) ||");
680 out.println(iii + " (type == GL_FIXED)) &&");
681 out.println(iii + " (stride >= 0)) {");
682 out.println(iii + indent + "_pointSizePointerOES = pointer;");
683 out.println(iii + "}");
684 } else if (fname.equals("glMatrixIndexPointerOES")) {
685 out.println(iii + "if (((size == 2) ||");
686 out.println(iii + " (size == 3) ||");
687 out.println(iii + " (size == 4)) &&");
688 out.println(iii + " ((type == GL_FLOAT) ||");
689 out.println(iii + " (type == GL_BYTE) ||");
690 out.println(iii + " (type == GL_SHORT) ||");
691 out.println(iii + " (type == GL_FIXED)) &&");
692 out.println(iii + " (stride >= 0)) {");
693 out.println(iii + indent + "_matrixIndexPointerOES = pointer;");
694 out.println(iii + "}");
695 } else if (fname.equals("glWeightPointer")) {
696 out.println(iii + "if (((size == 2) ||");
697 out.println(iii + " (size == 3) ||");
698 out.println(iii + " (size == 4)) &&");
699 out.println(iii + " ((type == GL_FLOAT) ||");
700 out.println(iii + " (type == GL_BYTE) ||");
701 out.println(iii + " (type == GL_SHORT) ||");
702 out.println(iii + " (type == GL_FIXED)) &&");
703 out.println(iii + " (stride >= 0)) {");
704 out.println(iii + indent + "_weightPointerOES = pointer;");
705 out.println(iii + "}");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800706 }
707 }
708
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800709 boolean isVoid = jfunc.getType().isVoid();
710
711 if (!isVoid) {
712 out.println(indent + indent + "return _returnValue;");
713 }
714 out.println(indent + "}");
715 }
716 out.println();
717 }
718
Jack Palevichffac1ef2009-04-14 19:00:09 -0700719 public void addNativeRegistration(String s) {
720 nativeRegistrations.add(s);
721 }
722
Jack Palevich427f5852009-04-15 19:13:17 -0700723 public void emitNativeRegistration(String registrationFunctionName,
724 PrintStream cStream) {
Jack Palevichffac1ef2009-04-14 19:00:09 -0700725 cStream.println("static const char *classPathName = \"" +
726 mClassPathName +
727 "\";");
728 cStream.println();
729
Daniel Micayc4e95a32015-09-21 13:17:57 -0400730 cStream.println("static const JNINativeMethod methods[] = {");
Jack Palevichffac1ef2009-04-14 19:00:09 -0700731
732 cStream.println("{\"_nativeClassInit\", \"()V\", (void*)nativeClassInit },");
733
734 Iterator<String> i = nativeRegistrations.iterator();
735 while (i.hasNext()) {
736 cStream.println(i.next());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737 }
Jack Palevich6cbca502009-04-13 16:22:25 -0700738
Jack Palevichffac1ef2009-04-14 19:00:09 -0700739 cStream.println("};");
740 cStream.println();
741
742
Jack Palevich427f5852009-04-15 19:13:17 -0700743 cStream.println("int " + registrationFunctionName + "(JNIEnv *_env)");
Jack Palevichffac1ef2009-04-14 19:00:09 -0700744 cStream.println("{");
745 cStream.println(indent +
746 "int err;");
747
748 cStream.println(indent +
749 "err = android::AndroidRuntime::registerNativeMethods(_env, classPathName, methods, NELEM(methods));");
750
751 cStream.println(indent + "return err;");
752 cStream.println("}");
753 }
754
755 public JniCodeEmitter() {
756 super();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757 }
758
759 String getJniType(JType jType) {
760 if (jType.isVoid()) {
761 return "void";
762 }
763
764 String baseType = jType.getBaseType();
765 if (jType.isPrimitive()) {
766 if (baseType.equals("String")) {
767 return "jstring";
768 } else {
769 return "j" + baseType;
770 }
771 } else if (jType.isArray()) {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700772 return jType.isClass() ? "jobjectArray" : "j" + baseType + "Array";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773 } else {
774 return "jobject";
775 }
776 }
Jack Palevich6cbca502009-04-13 16:22:25 -0700777
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800778 String getJniMangledName(String name) {
779 name = name.replaceAll("_", "_1");
780 name = name.replaceAll(";", "_2");
781 name = name.replaceAll("\\[", "_3");
782 return name;
783 }
784
785 public void emitJniCode(JFunc jfunc, PrintStream out) {
786 CFunc cfunc = jfunc.getCFunc();
Jack Palevich6cbca502009-04-13 16:22:25 -0700787
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788 // Emit comment identifying original C function
789 //
790 // Example:
791 //
792 // /* void glClipPlanef ( GLenum plane, const GLfloat *equation ) */
793 //
794 out.println("/* " + cfunc.getOriginal() + " */");
795
796 // Emit JNI signature (name)
797 //
798 // Example:
799 //
800 // void
801 // android_glClipPlanef__I_3FI
802 //
803
804 String outName = "android_" + jfunc.getName();
Jack Palevich66089a32009-12-08 15:43:51 +0800805 boolean isPointerFunc = isPointerFunc(jfunc);
Jesse Hall071fc662013-04-10 01:17:34 -0700806 boolean isPointerOffsetFunc =
807 (outName.endsWith("Pointer") || outName.endsWith("PointerOES") ||
808 outName.endsWith("glDrawElements") ||
809 outName.endsWith("glDrawRangeElements") ||
810 outName.endsWith("glTexImage2D") ||
811 outName.endsWith("glTexSubImage2D") ||
812 outName.endsWith("glCompressedTexImage2D") ||
813 outName.endsWith("glCompressedTexSubImage2D") ||
814 outName.endsWith("glTexImage3D") ||
815 outName.endsWith("glTexSubImage3D") ||
816 outName.endsWith("glCompressedTexImage3D") ||
817 outName.endsWith("glCompressedTexSubImage3D") ||
818 outName.endsWith("glReadPixels"))
819 && !jfunc.getCFunc().hasPointerArg();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800820 if (isPointerFunc) {
821 outName += "Bounds";
822 }
823
824 out.print("static ");
825 out.println(getJniType(jfunc.getType()));
826 out.print(outName);
827
828 String rsignature = getJniName(jfunc.getType());
829
830 String signature = "";
831 int numArgs = jfunc.getNumArgs();
832 for (int i = 0; i < numArgs; i++) {
833 JType argType = jfunc.getArgType(i);
834 signature += getJniName(argType);
835 }
836 if (isPointerFunc) {
837 signature += "I";
838 }
839
840 // Append signature to function name
Jack Palevich50d0b142009-11-19 16:34:55 +0800841 String sig = getJniMangledName(signature).replace('.', '_').replace('/', '_');
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700842 if (!mUseSimpleMethodNames) {
843 out.print("__" + sig);
844 outName += "__" + sig;
845 }
Jack Palevich6cbca502009-04-13 16:22:25 -0700846
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800847 signature = signature.replace('.', '/');
848 rsignature = rsignature.replace('.', '/');
Jack Palevich6cbca502009-04-13 16:22:25 -0700849
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850 out.println();
851 if (rsignature.length() == 0) {
852 rsignature = "V";
853 }
854
855 String s = "{\"" +
856 jfunc.getName() +
857 (isPointerFunc ? "Bounds" : "") +
858 "\", \"(" + signature +")" +
859 rsignature +
860 "\", (void *) " +
861 outName +
862 " },";
863 nativeRegistrations.add(s);
864
865 List<Integer> nonPrimitiveArgs = new ArrayList<Integer>();
Jack Palevich50d0b142009-11-19 16:34:55 +0800866 List<Integer> stringArgs = new ArrayList<Integer>();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800867 int numBufferArgs = 0;
868 List<String> bufferArgNames = new ArrayList<String>();
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -0700869 List<JType> bufferArgTypes = new ArrayList<JType>();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800870
871 // Emit JNI signature (arguments)
872 //
873 // Example:
874 //
875 // (JNIEnv *_env, jobject this, jint plane, jfloatArray equation_ref, jint offset) {
876 //
877 out.print(" (JNIEnv *_env, jobject _this");
878 for (int i = 0; i < numArgs; i++) {
879 out.print(", ");
880 JType argType = jfunc.getArgType(i);
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700881 String suffix = "";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800882 if (!argType.isPrimitive()) {
883 if (argType.isArray()) {
884 suffix = "_ref";
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700885 } else if (argType.isBuffer()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886 suffix = "_buf";
887 }
888 nonPrimitiveArgs.add(new Integer(i));
889 if (jfunc.getArgType(i).isBuffer()) {
890 int cIndex = jfunc.getArgCIndex(i);
891 String cname = cfunc.getArgName(cIndex);
892 bufferArgNames.add(cname);
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -0700893 bufferArgTypes.add(jfunc.getArgType(i));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800894 numBufferArgs++;
895 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800896 }
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700897
Jack Palevich50d0b142009-11-19 16:34:55 +0800898 if (argType.isString()) {
899 stringArgs.add(new Integer(i));
900 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800901
902 out.print(getJniType(argType) + " " + jfunc.getArgName(i) + suffix);
903 }
904 if (isPointerFunc) {
905 out.print(", jint remaining");
906 }
907 out.println(") {");
Jack Palevich6cbca502009-04-13 16:22:25 -0700908
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800909 int numArrays = 0;
910 int numBuffers = 0;
Jack Palevich50d0b142009-11-19 16:34:55 +0800911 int numStrings = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800912 for (int i = 0; i < nonPrimitiveArgs.size(); i++) {
913 int idx = nonPrimitiveArgs.get(i).intValue();
Jack Palevich50d0b142009-11-19 16:34:55 +0800914 JType argType = jfunc.getArgType(idx);
915 if (argType.isArray()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800916 ++numArrays;
917 }
Jack Palevich50d0b142009-11-19 16:34:55 +0800918 if (argType.isBuffer()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800919 ++numBuffers;
920 }
Jack Palevich50d0b142009-11-19 16:34:55 +0800921 if (argType.isString()) {
922 ++numStrings;
923 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800924 }
925
926 // Emit method body
927
928 // Emit local variable declarations for _exception and _returnValue
929 //
930 // Example:
931 //
932 // android::gl::ogles_context_t *ctx;
Jack Palevich6cbca502009-04-13 16:22:25 -0700933 //
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934 // jint _exception;
935 // GLenum _returnValue;
936 //
937 CType returnType = cfunc.getType();
938 boolean isVoid = returnType.isVoid();
939
940 boolean isUnsupported = isUnsupportedFunc(cfunc);
941 if (isUnsupported) {
942 out.println(indent +
Elliott Hughes98757502011-04-08 20:01:01 -0700943 "jniThrowException(_env, \"java/lang/UnsupportedOperationException\",");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800944 out.println(indent +
945 " \"" + cfunc.getName() + "\");");
946 if (!isVoid) {
947 String retval = getErrorReturnValue(cfunc);
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700948 if (cfunc.getType().isEGLHandle()) {
949 String baseType = cfunc.getType().getBaseType().toLowerCase();
950 out.println(indent +
951 "return toEGLHandle(_env, " + baseType + "Class, " +
952 baseType + "Constructor, " + retval + ");");
953 } else {
954 out.println(indent + "return " + retval + ";");
955 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800956 }
957 out.println("}");
958 out.println();
959 return;
960 }
Elliott Hughes98757502011-04-08 20:01:01 -0700961
Jack Paleviche44e45c2010-01-28 20:28:32 +0800962 String requiresExtension = isRequiresFunc(cfunc);
963 if (requiresExtension != null) {
964 out.println(indent +
965 "if (! supportsExtension(_env, _this, have_" + requiresExtension + "ID)) {");
966 out.println(indent + indent +
Elliott Hughes98757502011-04-08 20:01:01 -0700967 "jniThrowException(_env, \"java/lang/UnsupportedOperationException\",");
Jack Paleviche44e45c2010-01-28 20:28:32 +0800968 out.println(indent + indent +
969 " \"" + cfunc.getName() + "\");");
970 if (isVoid) {
971 out.println(indent + indent + " return;");
972 } else {
973 String retval = getErrorReturnValue(cfunc);
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700974 if (cfunc.getType().isEGLHandle()) {
975 String baseType = cfunc.getType().getBaseType().toLowerCase();
976 out.println(indent +
977 "return toEGLHandle(_env, " + baseType + "Class, " +
978 baseType + "Constructor, " + retval + ");");
979 } else {
980 out.println(indent + "return " + retval + ";");
981 }
Jack Paleviche44e45c2010-01-28 20:28:32 +0800982 }
983 out.println(indent + "}");
984 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800985 if (mUseContextPointer) {
986 out.println(indent +
987 "android::gl::ogles_context_t *ctx = getContext(_env, _this);");
988 }
989
Jack Palevich50d0b142009-11-19 16:34:55 +0800990 boolean initializeReturnValue = stringArgs.size() > 0;
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700991 boolean emitExceptionCheck = ((numArrays > 0 || numStrings > 0)
992 && (hasNonConstArg(jfunc, cfunc, nonPrimitiveArgs)
993 || (cfunc.hasPointerArg() && numArrays > 0))
Romain Guy79699992016-12-05 12:26:42 -0800994 || (numBufferArgs > 0)
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -0700995 || hasCheckTest(cfunc)
996 || hasIfTest(cfunc))
997 || (stringArgs.size() > 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998 // mChecker.getChecks(cfunc.getName()) != null
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999 // Emit an _exeption variable if there will be error checks
1000 if (emitExceptionCheck) {
1001 out.println(indent + "jint _exception = 0;");
Mathias Agopianbf13ba52013-02-22 19:34:06 -08001002 out.println(indent + "const char * _exceptionType = NULL;");
1003 out.println(indent + "const char * _exceptionMessage = NULL;");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001004 }
1005
1006 // Emit a single _array or multiple _XXXArray variables
1007 if (numBufferArgs == 1) {
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001008 JType bufferType = bufferArgTypes.get(0);
1009 if (bufferType.isTypedBuffer()) {
1010 String typedArrayType = getJniType(bufferType.getArrayTypeForTypedBuffer());
1011 out.println(indent + typedArrayType + " _array = (" + typedArrayType + ") 0;");
1012 } else {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013 out.println(indent + "jarray _array = (jarray) 0;");
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001014 }
1015 out.println(indent + "jint _bufferOffset = (jint) 0;");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 } else {
1017 for (int i = 0; i < numBufferArgs; i++) {
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001018 JType bufferType = bufferArgTypes.get(0);
1019 if (bufferType.isTypedBuffer()) {
1020 String typedArrayType = getJniType(bufferType.getArrayTypeForTypedBuffer());
1021 out.println(indent + typedArrayType + " _" + bufferArgNames.get(i) +
1022 "Array = (" + typedArrayType + ") 0;");
1023 } else {
1024 out.println(indent + "jarray _" + bufferArgNames.get(i) +
1025 "Array = (jarray) 0;");
1026 }
Thomas Tafertshofere58a97b2012-07-12 11:16:20 -07001027 out.println(indent + "jint _" + bufferArgNames.get(i) +
1028 "BufferOffset = (jint) 0;");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 }
1030 }
1031 if (!isVoid) {
1032 String retval = getErrorReturnValue(cfunc);
1033 if (retval != null) {
1034 out.println(indent + returnType.getDeclaration() +
1035 " _returnValue = " + retval + ";");
Jack Palevich50d0b142009-11-19 16:34:55 +08001036 } else if (initializeReturnValue) {
1037 out.println(indent + returnType.getDeclaration() +
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001038 " _returnValue = 0;");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 } else {
1040 out.println(indent + returnType.getDeclaration() +
1041 " _returnValue;");
1042 }
1043 }
1044
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001045 // Emit local variable declarations for EGL Handles
1046 //
1047 // Example:
1048 //
1049 // EGLSurface surface_native = (EGLHandle)fromEGLHandle(_env, surfaceClass, surfaceConstructor, surface);
1050 //
1051 if (nonPrimitiveArgs.size() > 0) {
1052 for (int i = 0; i < nonPrimitiveArgs.size(); i++) {
1053 int idx = nonPrimitiveArgs.get(i).intValue();
1054 int cIndex = jfunc.getArgCIndex(idx);
1055 String cname = cfunc.getArgName(cIndex);
1056
1057 if (jfunc.getArgType(idx).isBuffer()
1058 || jfunc.getArgType(idx).isArray()
1059 || !jfunc.getArgType(idx).isEGLHandle())
1060 continue;
1061
1062 CType type = cfunc.getArgType(jfunc.getArgCIndex(idx));
1063 String decl = type.getDeclaration();
1064 out.println(indent +
1065 decl + " " + cname + "_native = (" +
1066 decl + ") fromEGLHandle(_env, " +
1067 type.getBaseType().toLowerCase() +
1068 "GetHandleID, " + jfunc.getArgName(idx) +
1069 ");");
1070 }
1071 }
1072
1073 // Emit local variable declarations for element/sentinel checks
1074 //
1075 // Example:
1076 //
1077 // bool attrib_list_sentinel_found = false;
1078 //
1079 emitLocalVariablesForSentinel(cfunc, out);
1080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001081 // Emit local variable declarations for pointer arguments
1082 //
1083 // Example:
1084 //
1085 // GLfixed *eqn_base;
1086 // GLfixed *eqn;
1087 //
1088 String offset = "offset";
1089 String remaining = "_remaining";
1090 if (nonPrimitiveArgs.size() > 0) {
1091 for (int i = 0; i < nonPrimitiveArgs.size(); i++) {
1092 int idx = nonPrimitiveArgs.get(i).intValue();
1093 int cIndex = jfunc.getArgCIndex(idx);
1094 String cname = cfunc.getArgName(cIndex);
1095
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001096 if (!jfunc.getArgType(idx).isBuffer() && !jfunc.getArgType(idx).isArray())
1097 continue;
1098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099 CType type = cfunc.getArgType(jfunc.getArgCIndex(idx));
1100 String decl = type.getDeclaration();
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001101 if (jfunc.getArgType(idx).isArray() && !jfunc.getArgType(idx).isClass()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 out.println(indent +
1103 decl +
1104 (decl.endsWith("*") ? "" : " ") +
1105 jfunc.getArgName(idx) +
1106 "_base = (" + decl + ") 0;");
1107 }
Jack Palevich50d0b142009-11-19 16:34:55 +08001108 remaining = ((numArrays + numBuffers) <= 1) ? "_remaining" :
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001109 "_" + cname + "Remaining";
1110 out.println(indent +
1111 "jint " + remaining + ";");
1112 out.println(indent +
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001113 decl +
1114 (decl.endsWith("*") ? "" : " ") +
1115 jfunc.getArgName(idx) +
1116 " = (" + decl + ") 0;");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001117 }
1118
1119 out.println();
1120 }
1121
Jack Palevich50d0b142009-11-19 16:34:55 +08001122 // Emit local variable declaration for strings
1123 if (stringArgs.size() > 0) {
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001124 boolean requiresStringLengthCheck = false;
Jack Palevich50d0b142009-11-19 16:34:55 +08001125 for (int i = 0; i < stringArgs.size(); i++) {
1126 int idx = stringArgs.get(i).intValue();
1127 int cIndex = jfunc.getArgCIndex(idx);
1128 String cname = cfunc.getArgName(cIndex);
1129
1130 out.println(indent + "const char* _native" + cname + " = 0;");
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001131 if (hasCheckTest(cfunc, cname)) {
1132 requiresStringLengthCheck = true;
1133 }
1134 }
1135
1136 if (requiresStringLengthCheck) {
1137 out.println(indent + "jsize _stringlen = 0;");
Jack Palevich50d0b142009-11-19 16:34:55 +08001138 }
1139
1140 out.println();
1141 }
1142
1143 // Null pointer checks and GetStringUTFChars
1144 if (stringArgs.size() > 0) {
1145 for (int i = 0; i < stringArgs.size(); i++) {
1146 int idx = stringArgs.get(i).intValue();
1147 int cIndex = jfunc.getArgCIndex(idx);
1148 String cname = cfunc.getArgName(cIndex);
1149
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001150 boolean nullAllowed = isNullAllowed(cfunc, cname);
1151 String nullAllowedIndent = nullAllowed ? indent : "";
1152
Jack Palevich50d0b142009-11-19 16:34:55 +08001153 CType type = cfunc.getArgType(jfunc.getArgCIndex(idx));
1154 String decl = type.getDeclaration();
Jack Palevich50d0b142009-11-19 16:34:55 +08001155
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001156 if (nullAllowed) {
1157 out.println(indent + "if (" + cname + ") {");
1158 } else {
1159 needsExit = true;
1160 out.println(indent + "if (!" + cname + ") {");
1161 out.println(indent + indent + "_exception = 1;");
1162 out.println(indent + indent +
1163 "_exceptionType = \"java/lang/IllegalArgumentException\";");
1164 out.println(indent + indent +
1165 "_exceptionMessage = \"" + cname + " == null\";");
1166 out.println(indent + indent + "goto exit;");
1167 out.println(indent + "}");
1168 }
1169
1170 out.println(nullAllowedIndent + indent + "_native" + cname +
1171 " = _env->GetStringUTFChars(" + cname + ", 0);");
1172
1173 emitStringCheck(cfunc, cname, out, nullAllowedIndent + indent);
1174
1175 if (nullAllowed) {
1176 out.println(indent + "}");
1177 }
Jack Palevich50d0b142009-11-19 16:34:55 +08001178 }
1179
1180 out.println();
1181 }
1182
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001183 // Emit 'GetPrimitiveArrayCritical' for non-object arrays
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184 // Emit 'GetPointer' calls for Buffer pointers
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185 if (nonPrimitiveArgs.size() > 0) {
1186 for (int i = 0; i < nonPrimitiveArgs.size(); i++) {
1187 int idx = nonPrimitiveArgs.get(i).intValue();
1188 int cIndex = jfunc.getArgCIndex(idx);
Jack Palevich6cbca502009-04-13 16:22:25 -07001189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190 String cname = cfunc.getArgName(cIndex);
1191 offset = numArrays <= 1 ? "offset" :
1192 cname + "Offset";
Jack Palevich50d0b142009-11-19 16:34:55 +08001193 remaining = ((numArrays + numBuffers) <= 1) ? "_remaining" :
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 "_" + cname + "Remaining";
1195
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001196 boolean nullAllowed = isNullAllowed(cfunc, cname);
1197 String nullAllowedIndent = nullAllowed ? indent : "";
1198
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001199 if (jfunc.getArgType(idx).isArray()
1200 && !jfunc.getArgType(idx).isEGLHandle()) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001201 needsExit = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001203 if (nullAllowed) {
1204 out.println(indent + "if (" + cname + "_ref) {");
1205 }
1206 else
1207 {
1208 out.println(indent + "if (!" + cname + "_ref) {");
1209 out.println(indent + indent + "_exception = 1;");
1210 out.println(indent + indent +
1211 "_exceptionType = " +
1212 "\"java/lang/IllegalArgumentException\";");
1213 out.println(indent + indent +
1214 "_exceptionMessage = \"" + cname +
1215 " == null\";");
1216 out.println(indent + indent + "goto exit;");
1217 out.println(indent + "}");
1218 }
1219
1220 out.println(nullAllowedIndent + indent + "if (" + offset +
1221 " < 0) {");
1222 out.println(nullAllowedIndent + indent + indent +
1223 "_exception = 1;");
1224 out.println(nullAllowedIndent + indent + indent +
1225 "_exceptionType = " +
1226 "\"java/lang/IllegalArgumentException\";");
1227 out.println(nullAllowedIndent + indent + indent +
1228 "_exceptionMessage = \"" + offset +" < 0\";");
1229 out.println(nullAllowedIndent + indent + indent +
1230 "goto exit;");
1231 out.println(nullAllowedIndent + indent + "}");
1232
1233 out.println(nullAllowedIndent + indent + remaining + " = " +
1234 (mUseCPlusPlus ? "_env" : "(*_env)") +
1235 "->GetArrayLength(" +
1236 (mUseCPlusPlus ? "" : "_env, ") +
1237 cname + "_ref) - " + offset + ";");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001238
1239 emitNativeBoundsChecks(cfunc, cname, out, false,
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001240 emitExceptionCheck, offset, remaining,
1241 nullAllowedIndent + indent);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001243 out.println(nullAllowedIndent + indent +
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 cname +
1245 "_base = (" +
1246 cfunc.getArgType(cIndex).getDeclaration() +
1247 ")");
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001248 String arrayGetter = jfunc.getArgType(idx).getArrayGetterForPrimitiveArray();
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001249 out.println(nullAllowedIndent + indent + " " +
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001250 (mUseCPlusPlus ? "_env" : "(*_env)") +
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001251 "->" + arrayGetter + "(" +
Jack Palevich6cbca502009-04-13 16:22:25 -07001252 (mUseCPlusPlus ? "" : "_env, ") +
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 jfunc.getArgName(idx) +
1254 "_ref, (jboolean *)0);");
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001255 out.println(nullAllowedIndent + indent +
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001256 cname + " = " + cname + "_base + " + offset + ";");
1257
1258 emitSentinelCheck(cfunc, cname, out, false,
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001259 emitExceptionCheck, offset, remaining,
1260 nullAllowedIndent + indent);
1261
1262 if (nullAllowed) {
1263 out.println(indent + "}");
1264 }
1265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 out.println();
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001267 } else if (jfunc.getArgType(idx).isArray()
1268 && jfunc.getArgType(idx).isEGLHandle()) {
1269 needsExit = true;
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001270
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001271 if (nullAllowed) {
1272 out.println(indent + "if (" + cname + "_ref) {");
1273 }
1274 else
1275 {
1276 out.println(indent + "if (!" + cname + "_ref) {");
1277 out.println(indent + indent + "_exception = 1;");
1278 out.println(indent + indent + "_exceptionType = " +
1279 "\"java/lang/IllegalArgumentException\";");
1280 out.println(indent + indent + "_exceptionMessage = \"" +
1281 cname +" == null\";");
1282 out.println(indent + indent + "goto exit;");
1283 out.println(indent + "}");
1284 }
1285
1286 out.println(nullAllowedIndent + indent + "if (" + offset +
1287 " < 0) {");
1288 out.println(nullAllowedIndent + indent + indent +
1289 "_exception = 1;");
1290 out.println(nullAllowedIndent + indent + indent +
1291 "_exceptionType = " +
1292 "\"java/lang/IllegalArgumentException\";");
1293 out.println(nullAllowedIndent + indent + indent +
1294 "_exceptionMessage = \"" + offset +" < 0\";");
1295 out.println(nullAllowedIndent + indent + indent +
1296 "goto exit;");
1297 out.println(nullAllowedIndent + indent + "}");
1298
1299 out.println(nullAllowedIndent + indent + remaining + " = " +
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001300 (mUseCPlusPlus ? "_env" : "(*_env)") +
1301 "->GetArrayLength(" +
1302 (mUseCPlusPlus ? "" : "_env, ") +
1303 cname + "_ref) - " + offset + ";");
1304 emitNativeBoundsChecks(cfunc, cname, out, false,
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001305 emitExceptionCheck, offset, remaining,
1306 nullAllowedIndent + indent);
1307 out.println(nullAllowedIndent + indent +
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001308 jfunc.getArgName(idx) + " = new " +
1309 cfunc.getArgType(cIndex).getBaseType() +
1310 "["+ remaining + "];");
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001311
1312 if (nullAllowed) {
1313 out.println(indent + "}");
1314 }
1315
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001316 out.println();
1317 } else if (jfunc.getArgType(idx).isBuffer()) {
Romain Guy79699992016-12-05 12:26:42 -08001318 needsExit = needsExit || (!nullAllowed && !isPointerFunc);
1319
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320 String array = numBufferArgs <= 1 ? "_array" :
Thomas Tafertshofere58a97b2012-07-12 11:16:20 -07001321 "_" + cfunc.getArgName(cIndex) + "Array";
1322 String bufferOffset = numBufferArgs <= 1 ? "_bufferOffset" :
1323 "_" + cfunc.getArgName(cIndex) + "BufferOffset";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001325 nullAllowed = nullAllowed || isPointerFunc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326 if (nullAllowed) {
1327 out.println(indent + "if (" + cname + "_buf) {");
1328 out.print(indent);
1329 }
Romain Guy79699992016-12-05 12:26:42 -08001330 else
1331 {
1332 out.println(indent + "if (!" + cname + "_buf) {");
1333 out.println(indent + indent + "_exception = 1;");
1334 out.println(indent + indent + "_exceptionType = " +
1335 "\"java/lang/IllegalArgumentException\";");
1336 out.println(indent + indent + "_exceptionMessage = \"" +
1337 cname +" == null\";");
1338 out.println(indent + indent + "goto exit;");
1339 out.println(indent + "}");
1340 }
Jack Palevich6cbca502009-04-13 16:22:25 -07001341
Jack Palevich46d25a32009-05-07 18:28:29 -07001342 if (isPointerFunc) {
1343 out.println(indent +
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 cname +
1345 " = (" +
1346 cfunc.getArgType(cIndex).getDeclaration() +
Jack Palevich6eedc8d2009-05-15 18:13:34 -07001347 ") getDirectBufferPointer(_env, " +
Jack Palevich46d25a32009-05-07 18:28:29 -07001348 cname + "_buf);");
1349 String iii = " ";
Jack Palevich5afdc872009-10-21 11:02:44 -07001350 out.println(iii + indent + "if ( ! " + cname + " ) {");
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001351 out.println(iii + indent + indent + "return;");
Jack Palevich46d25a32009-05-07 18:28:29 -07001352 out.println(iii + indent + "}");
1353 } else {
1354 out.println(indent +
1355 cname +
1356 " = (" +
1357 cfunc.getArgType(cIndex).getDeclaration() +
1358 ")getPointer(_env, " +
1359 cname +
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001360 "_buf, (jarray*)&" + array + ", &" + remaining + ", &" + bufferOffset +
Jack Palevich46d25a32009-05-07 18:28:29 -07001361 ");");
1362 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363
Jack Palevich5afdc872009-10-21 11:02:44 -07001364 emitNativeBoundsChecks(cfunc, cname, out, true,
1365 emitExceptionCheck,
1366 offset, remaining, nullAllowed ? " " : " ");
1367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 if (nullAllowed) {
1369 out.println(indent + "}");
1370 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 }
1372 }
1373 }
1374
Thomas Tafertshofere58a97b2012-07-12 11:16:20 -07001375 // Emit 'GetPrimitiveArrayCritical' for pointers if needed
1376 if (nonPrimitiveArgs.size() > 0) {
1377 for (int i = 0; i < nonPrimitiveArgs.size(); i++) {
1378 int idx = nonPrimitiveArgs.get(i).intValue();
1379 int cIndex = jfunc.getArgCIndex(idx);
1380
1381 if(!jfunc.getArgType(idx).isBuffer() || isPointerFunc) continue;
1382
1383 String cname = cfunc.getArgName(cIndex);
1384 String bufferOffset = numBufferArgs <= 1 ? "_bufferOffset" :
1385 "_" + cname + "BufferOffset";
1386 String array = numBufferArgs <= 1 ? "_array" :
1387 "_" + cfunc.getArgName(cIndex) + "Array";
1388
Pablo Ceballosb62e2422015-10-01 18:25:56 -07001389 boolean nullAllowed = isNullAllowed(cfunc, cname) ||
1390 isPointerFunc;
Thomas Tafertshofer36b285e2012-07-23 16:52:32 -07001391 if (nullAllowed) {
1392 out.println(indent + "if (" + cname + "_buf && " + cname +" == NULL) {");
1393 } else {
1394 out.println(indent + "if (" + cname +" == NULL) {");
1395 }
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001396 JType argType = jfunc.getArgType(idx);
1397 if (argType.isTypedBuffer()) {
1398 String arrayGetter = argType.getArrayTypeForTypedBuffer().getArrayGetterForPrimitiveArray();
1399 out.println(indent + indent + "char * _" + cname + "Base = (char *)_env->" + arrayGetter + "(" + array + ", (jboolean *) 0);");
1400 out.println(indent + indent + cname + " = (" +cfunc.getArgType(cIndex).getDeclaration() +") (_" + cname + "Base + " + bufferOffset + ");");
1401 out.println(indent + "}");
1402 } else {
1403 out.println(indent + indent + "char * _" + cname + "Base = (char *)_env->GetPrimitiveArrayCritical(" + array + ", (jboolean *) 0);");
1404 out.println(indent + indent + cname + " = (" +cfunc.getArgType(cIndex).getDeclaration() +") (_" + cname + "Base + " + bufferOffset + ");");
1405 out.println(indent + "}");
1406 }
Thomas Tafertshofere58a97b2012-07-12 11:16:20 -07001407 }
1408 }
1409
1410
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 if (!isVoid) {
1412 out.print(indent + "_returnValue = ");
1413 } else {
1414 out.print(indent);
1415 }
1416 String name = cfunc.getName();
1417
1418 if (mUseContextPointer) {
1419 name = name.substring(2, name.length()); // Strip off 'gl' prefix
1420 name = name.substring(0, 1).toLowerCase() +
1421 name.substring(1, name.length());
1422 out.print("ctx->procs.");
1423 }
Jack Palevich6cbca502009-04-13 16:22:25 -07001424
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425 out.print(name + (isPointerFunc ? "Bounds" : "") + "(");
1426
Jack Palevich6cbca502009-04-13 16:22:25 -07001427 numArgs = cfunc.getNumArgs();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 if (numArgs == 0) {
1429 if (mUseContextPointer) {
1430 out.println("ctx);");
1431 } else {
1432 out.println(");");
1433 }
1434 } else {
1435 if (mUseContextPointer) {
1436 out.println("ctx,");
1437 } else {
1438 out.println();
1439 }
1440 for (int i = 0; i < numArgs; i++) {
1441 String typecast;
Jesse Hall071fc662013-04-10 01:17:34 -07001442 if (i == numArgs - 1 && isPointerOffsetFunc) {
Ashok Bhatd8f09612014-02-15 12:51:43 +00001443 typecast = "reinterpret_cast<GLvoid *>";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444 } else {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001445 typecast = "(" + cfunc.getArgType(i).getDeclaration() + ")";
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446 }
1447 out.print(indent + indent +
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001448 typecast);
1449
Jack Palevich50d0b142009-11-19 16:34:55 +08001450 if (cfunc.getArgType(i).isConstCharPointer()) {
1451 out.print("_native");
1452 }
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001453
Thomas Tafertshofere58a97b2012-07-12 11:16:20 -07001454 if (cfunc.getArgType(i).isEGLHandle() &&
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001455 !cfunc.getArgType(i).isPointer()){
1456 out.print(cfunc.getArgName(i)+"_native");
Ashok Bhatd8f09612014-02-15 12:51:43 +00001457 } else if (i == numArgs - 1 && isPointerOffsetFunc){
1458 out.print("("+cfunc.getArgName(i)+")");
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001459 } else {
1460 out.print(cfunc.getArgName(i));
1461 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462
1463 if (i == numArgs - 1) {
1464 if (isPointerFunc) {
1465 out.println(",");
1466 out.println(indent + indent + "(GLsizei)remaining");
1467 } else {
1468 out.println();
1469 }
1470 } else {
1471 out.println(",");
1472 }
1473 }
1474 out.println(indent + ");");
1475 }
1476
1477 if (needsExit) {
1478 out.println();
1479 out.println("exit:");
1480 needsExit = false;
1481 }
1482
Thomas Tafertshofere58a97b2012-07-12 11:16:20 -07001483
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 if (nonPrimitiveArgs.size() > 0) {
1485 for (int i = nonPrimitiveArgs.size() - 1; i >= 0; i--) {
1486 int idx = nonPrimitiveArgs.get(i).intValue();
1487
1488 int cIndex = jfunc.getArgCIndex(idx);
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001489 if (jfunc.getArgType(idx).isArray() && !jfunc.getArgType(idx).isClass()) {
Jack Palevich6cbca502009-04-13 16:22:25 -07001490
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 // If the argument is 'const', GL will not write to it.
1492 // In this case, we can use the 'JNI_ABORT' flag to avoid
1493 // the need to write back to the Java array
1494 out.println(indent +
1495 "if (" + jfunc.getArgName(idx) + "_base) {");
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001496 String arrayReleaser = jfunc.getArgType(idx).getArrayReleaserForPrimitiveArray();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 out.println(indent + indent +
1498 (mUseCPlusPlus ? "_env" : "(*_env)") +
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001499 "->" + arrayReleaser + "(" +
Jack Palevich6cbca502009-04-13 16:22:25 -07001500 (mUseCPlusPlus ? "" : "_env, ") +
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501 jfunc.getArgName(idx) + "_ref, " +
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001502 "(j" + jfunc.getArgType(idx).getBaseType() + "*)" + cfunc.getArgName(cIndex) +
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 "_base,");
1504 out.println(indent + indent + indent +
1505 (cfunc.getArgType(cIndex).isConst() ?
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001506 "JNI_ABORT" : "_exception ? JNI_ABORT: 0" ) +
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 ");");
1508 out.println(indent + "}");
1509 } else if (jfunc.getArgType(idx).isBuffer()) {
Jack Palevich46d25a32009-05-07 18:28:29 -07001510 if (! isPointerFunc) {
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001511 JType argType = jfunc.getArgType(idx);
Jack Palevich46d25a32009-05-07 18:28:29 -07001512 String array = numBufferArgs <= 1 ? "_array" :
Thomas Tafertshofere58a97b2012-07-12 11:16:20 -07001513 "_" + cfunc.getArgName(cIndex) + "Array";
Jack Palevich46d25a32009-05-07 18:28:29 -07001514 out.println(indent + "if (" + array + ") {");
Hiroshi Yamauchi569bc1b2015-05-13 13:11:30 -07001515 if (argType.isTypedBuffer()) {
1516 String arrayReleaser =
1517 argType.getArrayTypeForTypedBuffer().getArrayReleaserForPrimitiveArray();
1518 out.println(indent + indent +
1519 "_env->" + arrayReleaser + "(" + array + ", " +
1520 "(j" + argType.getArrayTypeForTypedBuffer().getBaseType() + "*)" +
1521 cfunc.getArgName(cIndex) +
1522 ", " +
1523 (cfunc.getArgType(cIndex).isConst() ?
1524 "JNI_ABORT" : (emitExceptionCheck ?
1525 "_exception ? JNI_ABORT : 0" : "0")) +
1526 ");");
1527 } else {
1528 out.println(indent + indent +
1529 "releasePointer(_env, " + array + ", " +
1530 cfunc.getArgName(cIndex) +
1531 ", " +
1532 (cfunc.getArgType(cIndex).isConst() ?
1533 "JNI_FALSE" : (emitExceptionCheck ?
1534 "_exception ? JNI_FALSE : JNI_TRUE" : "JNI_TRUE")) +
1535 ");");
1536 }
Jack Palevich46d25a32009-05-07 18:28:29 -07001537 out.println(indent + "}");
1538 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001539 }
1540 }
1541 }
1542
Jack Palevich50d0b142009-11-19 16:34:55 +08001543 // Emit local variable declaration for strings
1544 if (stringArgs.size() > 0) {
1545 for (int i = 0; i < stringArgs.size(); i++) {
1546 int idx = stringArgs.get(i).intValue();
1547 int cIndex = jfunc.getArgCIndex(idx);
1548 String cname = cfunc.getArgName(cIndex);
1549
1550 out.println(indent + "if (_native" + cname + ") {");
1551 out.println(indent + " _env->ReleaseStringUTFChars(" + cname + ", _native" + cname + ");");
1552 out.println(indent + "}");
1553 }
1554
1555 out.println();
1556 }
1557
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001558 // Copy results back to java arrays
1559 if (nonPrimitiveArgs.size() > 0) {
1560 for (int i = nonPrimitiveArgs.size() - 1; i >= 0; i--) {
1561 int idx = nonPrimitiveArgs.get(i).intValue();
1562 int cIndex = jfunc.getArgCIndex(idx);
1563 String baseType = cfunc.getArgType(cIndex).getBaseType().toLowerCase();
1564 if (jfunc.getArgType(idx).isArray() && jfunc.getArgType(idx).isClass()) {
1565 remaining = ((numArrays + numBuffers) <= 1) ? "_remaining" :
1566 "_" + cfunc.getArgName(cIndex) + "Remaining";
1567 offset = numArrays <= 1 ? "offset" : cfunc.getArgName(cIndex) + "Offset";
1568 out.println(indent +
1569 "if (" + jfunc.getArgName(idx) + ") {");
1570 out.println(indent + indent +
1571 "for (int i = 0; i < " + remaining + "; i++) {");
1572 out.println(indent + indent + indent +
1573 "jobject " + cfunc.getArgName(cIndex) +
1574 "_new = toEGLHandle(_env, " + baseType +
1575 "Class, " + baseType + "Constructor, " +
1576 cfunc.getArgName(cIndex) + "[i]);");
1577 out.println(indent + indent + indent +
1578 (mUseCPlusPlus ? "_env" : "(*_env)") +
1579 "->SetObjectArrayElement(" +
1580 (mUseCPlusPlus ? "" : "_env, ") +
1581 cfunc.getArgName(cIndex) +
1582 "_ref, i + " + offset + ", " +
1583 cfunc.getArgName(cIndex) + "_new);");
1584 out.println(indent + indent + "}");
1585 out.println(indent + indent +
1586 "delete[] " + jfunc.getArgName(idx) + ";");
1587 out.println(indent + "}");
1588 }
1589 }
1590 }
1591
1592
1593 // Throw exception if there is one
1594 if (emitExceptionCheck) {
1595 out.println(indent + "if (_exception) {");
1596 out.println(indent + indent +
1597 "jniThrowException(_env, _exceptionType, _exceptionMessage);");
1598 out.println(indent + "}");
1599
1600 }
1601
Jack Palevich50d0b142009-11-19 16:34:55 +08001602
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 if (!isVoid) {
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001604 if (cfunc.getType().isEGLHandle()) {
1605 String baseType = cfunc.getType().getBaseType().toLowerCase();
1606 out.println(indent +
1607 "return toEGLHandle(_env, " + baseType + "Class, " +
1608 baseType + "Constructor, _returnValue);");
1609 } else {
Jesse Hall68fc8bb2013-04-10 01:01:00 -07001610 out.println(indent + "return (" +
1611 getJniType(jfunc.getType()) + ")_returnValue;");
Thomas Tafertshofer66a42db2012-06-15 16:22:43 -07001612 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 }
1614
1615 out.println("}");
1616 out.println();
1617 }
1618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619}