1 /*
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 
17 import java.util.HashMap;
18 
19 public class JType {
20 
21     String baseType;
22     boolean isArray;
23     boolean isClass;
24     boolean isString;
25 
26     static HashMap<CType,JType> typeMapping = new HashMap<CType,JType>();
27     static HashMap<CType,JType> arrayTypeMapping = new HashMap<CType,JType>();
28 
29     static {
30     // Primitive types
typeMapping.put(new CType("GLbitfield"), new JType("int"))31     typeMapping.put(new CType("GLbitfield"), new JType("int"));
typeMapping.put(new CType("GLboolean"), new JType("boolean"))32     typeMapping.put(new CType("GLboolean"), new JType("boolean"));
typeMapping.put(new CType("GLclampf"), new JType("float"))33     typeMapping.put(new CType("GLclampf"), new JType("float"));
typeMapping.put(new CType("GLclampx"), new JType("int"))34     typeMapping.put(new CType("GLclampx"), new JType("int"));
typeMapping.put(new CType("GLenum"), new JType("int"))35     typeMapping.put(new CType("GLenum"), new JType("int"));
typeMapping.put(new CType("GLfloat"), new JType("float"))36     typeMapping.put(new CType("GLfloat"), new JType("float"));
typeMapping.put(new CType("GLfixed"), new JType("int"))37     typeMapping.put(new CType("GLfixed"), new JType("int"));
typeMapping.put(new CType("GLint"), new JType("int"))38     typeMapping.put(new CType("GLint"), new JType("int"));
typeMapping.put(new CType("GLintptr"), new JType("int"))39     typeMapping.put(new CType("GLintptr"), new JType("int"));
typeMapping.put(new CType("GLshort"), new JType("short"))40     typeMapping.put(new CType("GLshort"), new JType("short"));
typeMapping.put(new CType("GLsizei"), new JType("int"))41     typeMapping.put(new CType("GLsizei"), new JType("int"));
typeMapping.put(new CType("GLsizeiptr"), new JType("int"))42     typeMapping.put(new CType("GLsizeiptr"), new JType("int"));
typeMapping.put(new CType("GLubyte"), new JType("byte"))43     typeMapping.put(new CType("GLubyte"), new JType("byte"));
typeMapping.put(new CType("GLuint"), new JType("int"))44     typeMapping.put(new CType("GLuint"), new JType("int"));
typeMapping.put(new CType("void"), new JType("void"))45     typeMapping.put(new CType("void"), new JType("void"));
typeMapping.put(new CType("GLubyte", true, true), new JType("String", false, false))46     typeMapping.put(new CType("GLubyte", true, true), new JType("String", false, false));
typeMapping.put(new CType("char"), new JType("byte"))47     typeMapping.put(new CType("char"), new JType("byte"));
typeMapping.put(new CType("char", true, true), new JType("String", false, false))48     typeMapping.put(new CType("char", true, true), new JType("String", false, false));
typeMapping.put(new CType("GLchar", true, true), new JType("String", false, false))49     typeMapping.put(new CType("GLchar", true, true), new JType("String", false, false));
typeMapping.put(new CType("int"), new JType("int"))50     typeMapping.put(new CType("int"), new JType("int"));
typeMapping.put(new CType("GLuint64"), new JType("long"))51     typeMapping.put(new CType("GLuint64"), new JType("long"));
typeMapping.put(new CType("GLsync"), new JType("long"))52     typeMapping.put(new CType("GLsync"), new JType("long"));
53 
54     // EGL primitive types
typeMapping.put(new CType("EGLint"), new JType("int"))55     typeMapping.put(new CType("EGLint"), new JType("int"));
typeMapping.put(new CType("EGLBoolean"), new JType("boolean"))56     typeMapping.put(new CType("EGLBoolean"), new JType("boolean"));
typeMapping.put(new CType("EGLenum"), new JType("int"))57     typeMapping.put(new CType("EGLenum"), new JType("int"));
typeMapping.put(new CType("EGLNativePixmapType"), new JType("int"))58     typeMapping.put(new CType("EGLNativePixmapType"), new JType("int"));
typeMapping.put(new CType("EGLNativeWindowType"), new JType("int"))59     typeMapping.put(new CType("EGLNativeWindowType"), new JType("int"));
typeMapping.put(new CType("EGLNativeDisplayType"), new JType("long"))60     typeMapping.put(new CType("EGLNativeDisplayType"), new JType("long"));
typeMapping.put(new CType("EGLClientBuffer"), new JType("long"))61     typeMapping.put(new CType("EGLClientBuffer"), new JType("long"));
typeMapping.put(new CType("EGLnsecsANDROID"), new JType("long"))62     typeMapping.put(new CType("EGLnsecsANDROID"), new JType("long"));
typeMapping.put(new CType("EGLAttrib"), new JType("long"))63     typeMapping.put(new CType("EGLAttrib"), new JType("long"));
typeMapping.put(new CType("EGLTime"), new JType("long"))64     typeMapping.put(new CType("EGLTime"), new JType("long"));
65 
66     // EGL nonprimitive types
typeMapping.put(new CType("EGLConfig"), new JType("EGLConfig", true, false))67     typeMapping.put(new CType("EGLConfig"), new JType("EGLConfig", true, false));
typeMapping.put(new CType("EGLContext"), new JType("EGLContext", true, false))68     typeMapping.put(new CType("EGLContext"), new JType("EGLContext", true, false));
typeMapping.put(new CType("EGLDisplay"), new JType("EGLDisplay", true, false))69     typeMapping.put(new CType("EGLDisplay"), new JType("EGLDisplay", true, false));
typeMapping.put(new CType("EGLSurface"), new JType("EGLSurface", true, false))70     typeMapping.put(new CType("EGLSurface"), new JType("EGLSurface", true, false));
typeMapping.put(new CType("EGLImage"), new JType("EGLImage", true, false))71     typeMapping.put(new CType("EGLImage"), new JType("EGLImage", true, false));
typeMapping.put(new CType("EGLSync"), new JType("EGLSync", true, false))72     typeMapping.put(new CType("EGLSync"), new JType("EGLSync", true, false));
73 
74 
75     // Untyped pointers map to untyped Buffers
typeMapping.put(new CType("GLvoid", true, true), new JType("java.nio.Buffer", true, false))76     typeMapping.put(new CType("GLvoid", true, true),
77             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("GLvoid", false, true), new JType("java.nio.Buffer", true, false))78     typeMapping.put(new CType("GLvoid", false, true),
79             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("void", false, true), new JType("java.nio.Buffer", true, false))80     typeMapping.put(new CType("void", false, true),
81             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("void", true, true), new JType("java.nio.Buffer", true, false))82     typeMapping.put(new CType("void", true, true),
83             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("GLeglImageOES", false, false), new JType("java.nio.Buffer", true, false))84     typeMapping.put(new CType("GLeglImageOES", false, false),
85             new JType("java.nio.Buffer", true, false));
86 
87     // Typed pointers map to typed Buffers
typeMapping.put(new CType("GLboolean", false, true), new JType("java.nio.IntBuffer", true, false))88     typeMapping.put(new CType("GLboolean", false, true),
89             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLenum", false, true), new JType("java.nio.IntBuffer", true, false))90     typeMapping.put(new CType("GLenum", false, true),
91             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLenum", true, true), new JType("java.nio.IntBuffer", true, false))92     typeMapping.put(new CType("GLenum", true, true),
93             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLfixed", false, true), new JType("java.nio.IntBuffer", true, false))94     typeMapping.put(new CType("GLfixed", false, true),
95             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLfixed", true, true), new JType("java.nio.IntBuffer", true, false))96     typeMapping.put(new CType("GLfixed", true, true),
97             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLfloat", false, true), new JType("java.nio.FloatBuffer", true, false))98     typeMapping.put(new CType("GLfloat", false, true),
99             new JType("java.nio.FloatBuffer", true, false));
typeMapping.put(new CType("GLfloat", true, true), new JType("java.nio.FloatBuffer", true, false))100     typeMapping.put(new CType("GLfloat", true, true),
101             new JType("java.nio.FloatBuffer", true, false));
typeMapping.put(new CType("GLint", false, true), new JType("java.nio.IntBuffer", true, false))102     typeMapping.put(new CType("GLint", false, true),
103             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLint", true, true), new JType("java.nio.IntBuffer", true, false))104     typeMapping.put(new CType("GLint", true, true),
105             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLsizei", false, true), new JType("java.nio.IntBuffer", true, false))106     typeMapping.put(new CType("GLsizei", false, true),
107             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLuint", false, true), new JType("java.nio.IntBuffer", true, false))108     typeMapping.put(new CType("GLuint", false, true),
109             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLuint", true, true), new JType("java.nio.IntBuffer", true, false))110     typeMapping.put(new CType("GLuint", true, true),
111             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLshort", true, true), new JType("java.nio.ShortBuffer", true, false))112     typeMapping.put(new CType("GLshort", true, true),
113             new JType("java.nio.ShortBuffer", true, false));
typeMapping.put(new CType("GLint64", false, true), new JType("java.nio.LongBuffer", true, false))114     typeMapping.put(new CType("GLint64", false, true),
115             new JType("java.nio.LongBuffer", true, false));
116 
117     // Typed pointers map to arrays + offsets
arrayTypeMapping.put(new CType("char", false, true), new JType("byte", false, true))118     arrayTypeMapping.put(new CType("char", false, true),
119             new JType("byte", false, true));
arrayTypeMapping.put(new CType("GLchar", false, true), new JType("byte", false, true))120     arrayTypeMapping.put(new CType("GLchar", false, true),
121             new JType("byte", false, true));
arrayTypeMapping.put(new CType("GLboolean", false, true), new JType("boolean", false, true))122     arrayTypeMapping.put(new CType("GLboolean", false, true),
123             new JType("boolean", false, true));
arrayTypeMapping.put(new CType("GLenum", false, true), new JType("int", false, true))124     arrayTypeMapping.put(new CType("GLenum", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLenum", true, true), new JType("int", false, true))125     arrayTypeMapping.put(new CType("GLenum", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLfixed", true, true), new JType("int", false, true))126     arrayTypeMapping.put(new CType("GLfixed", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLfixed", false, true), new JType("int", false, true))127     arrayTypeMapping.put(new CType("GLfixed", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLfloat", false, true), new JType("float", false, true))128     arrayTypeMapping.put(new CType("GLfloat", false, true), new JType("float", false, true));
arrayTypeMapping.put(new CType("GLfloat", true, true), new JType("float", false, true))129     arrayTypeMapping.put(new CType("GLfloat", true, true), new JType("float", false, true));
arrayTypeMapping.put(new CType("GLint", false, true), new JType("int", false, true))130     arrayTypeMapping.put(new CType("GLint", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLint", true, true), new JType("int", false, true))131     arrayTypeMapping.put(new CType("GLint", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLshort", true, true), new JType("short", false, true))132     arrayTypeMapping.put(new CType("GLshort", true, true), new JType("short", false, true));
arrayTypeMapping.put(new CType("GLsizei", false, true), new JType("int", false, true))133     arrayTypeMapping.put(new CType("GLsizei", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLsizei", true, true), new JType("int", false, true))134     arrayTypeMapping.put(new CType("GLsizei", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLuint", false, true), new JType("int", false, true))135     arrayTypeMapping.put(new CType("GLuint", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLuint", true, true), new JType("int", false, true))136     arrayTypeMapping.put(new CType("GLuint", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLintptr"), new JType("int", false, true))137     arrayTypeMapping.put(new CType("GLintptr"), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLsizeiptr"), new JType("int", false, true))138     arrayTypeMapping.put(new CType("GLsizeiptr"), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLint64", false, true), new JType("long", false, true))139     arrayTypeMapping.put(new CType("GLint64", false, true), new JType("long", false, true));
140 
141     //EGL typed pointers map to arrays + offsets
arrayTypeMapping.put(new CType("EGLint", false, true), new JType("int", false, true))142     arrayTypeMapping.put(new CType("EGLint", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("EGLint", true, true), new JType("int", false, true))143     arrayTypeMapping.put(new CType("EGLint", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("EGLConfig", false, true), new JType("EGLConfig", true, true))144     arrayTypeMapping.put(new CType("EGLConfig", false, true), new JType("EGLConfig", true, true));
arrayTypeMapping.put(new CType("EGLConfig", true, true), new JType("EGLConfig", true, true))145     arrayTypeMapping.put(new CType("EGLConfig", true, true), new JType("EGLConfig", true, true));
arrayTypeMapping.put(new CType("EGLAttrib", false, true), new JType("long", false, true))146     arrayTypeMapping.put(new CType("EGLAttrib", false, true), new JType("long", false, true));
arrayTypeMapping.put(new CType("EGLAttrib", true, true), new JType("long", false, true))147     arrayTypeMapping.put(new CType("EGLAttrib", true, true), new JType("long", false, true));
148 
149     }
150 
JType()151     public JType() {
152     }
153 
JType(String primitiveTypeName)154     public JType(String primitiveTypeName) {
155     this.baseType = primitiveTypeName;
156     this.isClass = false;
157     this.isArray = false;
158     }
159 
JType(String primitiveTypeName, boolean isClass, boolean isArray)160     public JType(String primitiveTypeName, boolean isClass, boolean isArray) {
161     this.baseType = primitiveTypeName;
162     this.isClass = isClass;
163     this.isArray = isArray;
164     }
165 
getBaseType()166     public String getBaseType() {
167     return baseType;
168     }
169 
170     @Override
toString()171     public String toString() {
172     return baseType + (isArray ? "[]" : "");
173     }
174 
isArray()175     public boolean isArray() {
176     return isArray;
177     }
178 
isClass()179     public boolean isClass() {
180     return isClass;
181     }
182 
isString()183     public boolean isString() {
184         return baseType.equals("String");
185     }
186 
isPrimitive()187     public boolean isPrimitive() {
188     return !isClass() && !isArray();
189     }
190 
isVoid()191     public boolean isVoid() {
192     return baseType.equals("void");
193     }
194 
isBuffer()195     public boolean isBuffer() {
196     return baseType.indexOf("Buffer") != -1;
197     }
198 
isTypedBuffer()199     public boolean isTypedBuffer() {
200     return !baseType.equals("java.nio.Buffer") &&
201         (baseType.indexOf("Buffer") != -1);
202     }
203 
getArrayTypeForTypedBuffer()204     public JType getArrayTypeForTypedBuffer() {
205       if (!isTypedBuffer()) {
206           throw new RuntimeException("Not typed buffer type " + this);
207       }
208       switch (baseType) {
209         case "java.nio.ByteBuffer":
210           return new JType("byte", false, true);
211         case "java.nio.BooleanBuffer":
212           return new JType("boolean", false, true);
213         case "java.nio.ShortBuffer":
214           return new JType("short", false, true);
215         case "java.nio.CharBuffer":
216           return new JType("char", false, true);
217         case "java.nio.IntBuffer":
218           return new JType("int", false, true);
219         case "java.nio.LongBuffer":
220           return new JType("long", false, true);
221         case "java.nio.FloatBuffer":
222           return new JType("float", false, true);
223         case "java.nio.DoubleBuffer":
224           return new JType("double", false, true);
225         default:
226           throw new RuntimeException("Unknown typed buffer type " + this);
227       }
228     }
229 
getArrayGetterForPrimitiveArray()230     public String getArrayGetterForPrimitiveArray() {
231       if (!isArray() || isClass()) {
232           throw new RuntimeException("Not array type " + this);
233       }
234       switch (baseType) {
235         case "byte":
236           return "GetByteArrayElements";
237         case "boolean":
238           return "GetBooleanArrayElements";
239         case "short":
240           return "GetShortArrayElements";
241         case "char":
242           return "GetCharArrayElements";
243         case "int":
244           return "GetIntArrayElements";
245         case "long":
246           return "GetLongArrayElements";
247         case "float":
248           return "GetFloatArrayElements";
249         case "double":
250           return "GetDoubleArrayElements";
251         default:
252           throw new RuntimeException("Unknown array type " + this);
253       }
254     }
255 
getArrayReleaserForPrimitiveArray()256     public String getArrayReleaserForPrimitiveArray() {
257       if (!isArray() || isClass()) {
258           throw new RuntimeException("Not array type " + this);
259       }
260       switch (baseType) {
261         case "byte":
262           return "ReleaseByteArrayElements";
263         case "boolean":
264           return "ReleaseBooleanArrayElements";
265         case "short":
266           return "ReleaseShortArrayElements";
267         case "char":
268           return "ReleaseCharArrayElements";
269         case "int":
270           return "ReleaseIntArrayElements";
271         case "long":
272           return "ReleaseLongArrayElements";
273         case "float":
274           return "ReleaseFloatArrayElements";
275         case "double":
276           return "ReleaseDoubleArrayElements";
277         default:
278           throw new RuntimeException("Unknown array type " + this);
279       }
280     }
281 
isEGLHandle()282     public boolean isEGLHandle() {
283     return !isPrimitive() &&
284         (baseType.startsWith("EGL"));
285     }
286 
convert(CType ctype, boolean useArray)287     public static JType convert(CType ctype, boolean useArray) {
288      JType javaType = null;
289      if (useArray) {
290          javaType = arrayTypeMapping.get(ctype);
291      }
292      if (javaType == null) {
293          javaType = typeMapping.get(ctype);
294      }
295      if (javaType == null) {
296          throw new RuntimeException("Unsupported C type: " + ctype);
297      }
298      return javaType;
299     }
300 }
301