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