JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
NativeSignatureEmitter.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2023 JogAmp Community. All rights reserved.
3 * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40package com.jogamp.gluegen.opengl.nativesig;
41
42import com.jogamp.gluegen.CodeUnit;
43import com.jogamp.gluegen.FunctionEmitter;
44import com.jogamp.gluegen.JavaMethodBindingEmitter;
45import com.jogamp.gluegen.JavaType;
46import com.jogamp.gluegen.MethodBinding;
47import com.jogamp.gluegen.cgram.types.FunctionSymbol;
48import com.jogamp.gluegen.opengl.GLEmitter;
49import com.jogamp.gluegen.opengl.GLJavaMethodBindingEmitter;
50
51import java.io.PrintWriter;
52import java.util.ArrayList;
53import java.util.Iterator;
54import java.util.List;
55
56/**
57 * Emitter producing NativeSignature attributes.
58 *
59 * Review: This Package/Class is not used and subject to be deleted.
60 */
61public class NativeSignatureEmitter extends GLEmitter {
62
63 @Override
64 protected List<? extends FunctionEmitter> generateMethodBindingEmitters(final FunctionSymbol sym) throws Exception {
65
66 // Allow superclass to do most of the work for us
67 final List<? extends FunctionEmitter> res = super.generateMethodBindingEmitters(sym);
68
69 // Filter out all non-JavaMethodBindingEmitters
70 for (final Iterator<? extends FunctionEmitter> iter = res.iterator(); iter.hasNext();) {
71 final FunctionEmitter emitter = iter.next();
72 if (!(emitter instanceof JavaMethodBindingEmitter)) {
73 iter.remove();
74 }
75 }
76
77 if (res.isEmpty()) {
78 return res;
79 }
80
81 final CodeUnit unit = (getConfig().allStatic() ? javaUnit() : javaImplUnit());
82
83 final List<FunctionEmitter> processed = new ArrayList<FunctionEmitter>();
84
85 // First, filter out all emitters going to the "other" (public) writer
86 for (final Iterator<? extends FunctionEmitter> iter = res.iterator(); iter.hasNext();) {
87 final FunctionEmitter emitter = iter.next();
88 if (emitter.getUnit() != unit) {
89 processed.add(emitter);
90 iter.remove();
91 }
92 }
93
94 // Now process all of the remaining emitters sorted by MethodBinding
95 while (!res.isEmpty()) {
96 final List<JavaMethodBindingEmitter> emittersForBinding = new ArrayList<JavaMethodBindingEmitter>();
97 final JavaMethodBindingEmitter emitter = (JavaMethodBindingEmitter) res.remove(0);
98 emittersForBinding.add(emitter);
99 final MethodBinding binding = emitter.getBinding();
100 for (final Iterator<? extends FunctionEmitter> iter = res.iterator(); iter.hasNext();) {
101 final JavaMethodBindingEmitter emitter2 = (JavaMethodBindingEmitter) iter.next();
102 if (emitter2.getBinding() == binding) {
103 emittersForBinding.add(emitter2);
104 iter.remove();
105 }
106 }
107 generateNativeSignatureEmitters(binding, emittersForBinding);
108 processed.addAll(emittersForBinding);
109 }
110
111 return processed;
112 }
113
114 protected void generateNativeSignatureEmitters(final MethodBinding binding, final List<JavaMethodBindingEmitter> allEmitters) {
115
116 if (allEmitters.isEmpty()) {
117 return;
118 }
119
120 final CodeUnit unit = (getConfig().allStatic() ? javaUnit() : javaImplUnit());
121
122 // Give ourselves the chance to interpose on the generation of all code to keep things simple
123 final List<JavaMethodBindingEmitter> newEmitters = new ArrayList<JavaMethodBindingEmitter>();
124 for (final JavaMethodBindingEmitter javaEmitter : allEmitters) {
126 if (javaEmitter instanceof GLJavaMethodBindingEmitter) {
128 } else {
129 newEmitter = new NativeSignatureJavaMethodBindingEmitter(javaEmitter, this);
130 }
131 newEmitters.add(newEmitter);
132 }
133 allEmitters.clear();
134 allEmitters.addAll(newEmitters);
135
136 // Detect whether we need to produce more or modify some of these emitters.
137 // Note that at this point we are assuming that generatePublicEmitters has
138 // been called with signatureOnly both true and false.
139 if (signatureContainsStrings(binding) && !haveEmitterWithBody(allEmitters)) {
140 // This basically handles glGetString but also any similar methods
141 final NativeSignatureJavaMethodBindingEmitter javaEmitter = findEmitterWithUnit(allEmitters, unit);
142
143 // First, we need to clone this emitter to produce the native
144 // entry point
146 emitter.removeModifier(JavaMethodBindingEmitter.PUBLIC);
147 emitter.addModifier(JavaMethodBindingEmitter.PRIVATE);
148 emitter.setPrivateNativeMethod(true);
149 // Note: this is chosen so we don't have to change the logic in
150 // emitReturnVariableSetupAndCall which decides which variant
151 // (direct / indirect) to call
152 emitter.setForDirectBufferImplementation(true);
153 allEmitters.add(emitter);
154
155 // Now make the original emitter non-native and cause it to emit a body
156 javaEmitter.removeModifier(JavaMethodBindingEmitter.NATIVE);
157 javaEmitter.setEmitBody(true);
158 }
159 }
160
161 protected boolean signatureContainsStrings(final MethodBinding binding) {
162 for (int i = 0; i < binding.getNumArguments(); i++) {
163 final JavaType type = binding.getJavaArgumentType(i);
164 if (type.isString() || type.isStringArray()) {
165 return true;
166 }
167 }
168 final JavaType retType = binding.getJavaReturnType();
169 if (retType.isString() || retType.isStringArray()) {
170 return true;
171 }
172 return false;
173 }
174
175 protected boolean haveEmitterWithBody(final List<JavaMethodBindingEmitter> allEmitters) {
176 for (final JavaMethodBindingEmitter emitter : allEmitters) {
177 if (!emitter.signatureOnly()) {
178 return true;
179 }
180 }
181 return false;
182 }
183
184 protected NativeSignatureJavaMethodBindingEmitter findEmitterWithUnit(final List<JavaMethodBindingEmitter> allEmitters, final CodeUnit unit) {
185 for (final JavaMethodBindingEmitter jemitter : allEmitters) {
187 if (emitter.getUnit() == unit) {
188 return emitter;
189 }
190 }
191 throw new RuntimeException("Unexpectedly failed to find an emitter with the given writer");
192 }
193}
A subclass of ProcAddressEmitter with special OpenGL-specific configuration abilities.
Definition: GLEmitter.java:74
A specialization of the proc address emitter which knows how to change argument names to take into ac...
void generateNativeSignatureEmitters(final MethodBinding binding, final List< JavaMethodBindingEmitter > allEmitters)
NativeSignatureJavaMethodBindingEmitter findEmitterWithUnit(final List< JavaMethodBindingEmitter > allEmitters, final CodeUnit unit)
List<? extends FunctionEmitter > generateMethodBindingEmitters(final FunctionSymbol sym)
boolean haveEmitterWithBody(final List< JavaMethodBindingEmitter > allEmitters)