• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("int"), new JType("int"))49     typeMapping.put(new CType("int"), new JType("int"));
50 
51     // EGL primitive types
typeMapping.put(new CType("EGLint"), new JType("int"))52     typeMapping.put(new CType("EGLint"), new JType("int"));
typeMapping.put(new CType("EGLBoolean"), new JType("boolean"))53     typeMapping.put(new CType("EGLBoolean"), new JType("boolean"));
typeMapping.put(new CType("EGLenum"), new JType("int"))54     typeMapping.put(new CType("EGLenum"), new JType("int"));
typeMapping.put(new CType("EGLNativePixmapType"), new JType("int"))55     typeMapping.put(new CType("EGLNativePixmapType"), new JType("int"));
typeMapping.put(new CType("EGLNativeWindowType"), new JType("int"))56     typeMapping.put(new CType("EGLNativeWindowType"), new JType("int"));
typeMapping.put(new CType("EGLNativeDisplayType"), new JType("int"))57     typeMapping.put(new CType("EGLNativeDisplayType"), new JType("int"));
typeMapping.put(new CType("EGLClientBuffer"), new JType("int"))58     typeMapping.put(new CType("EGLClientBuffer"), new JType("int"));
59 
60     // EGL nonprimitive types
typeMapping.put(new CType("EGLConfig"), new JType("EGLConfig", true, false))61     typeMapping.put(new CType("EGLConfig"), new JType("EGLConfig", true, false));
typeMapping.put(new CType("EGLContext"), new JType("EGLContext", true, false))62     typeMapping.put(new CType("EGLContext"), new JType("EGLContext", true, false));
typeMapping.put(new CType("EGLDisplay"), new JType("EGLDisplay", true, false))63     typeMapping.put(new CType("EGLDisplay"), new JType("EGLDisplay", true, false));
typeMapping.put(new CType("EGLSurface"), new JType("EGLSurface", true, false))64     typeMapping.put(new CType("EGLSurface"), new JType("EGLSurface", true, false));
65 
66 
67     // Untyped pointers map to untyped Buffers
typeMapping.put(new CType("GLvoid", true, true), new JType("java.nio.Buffer", true, false))68     typeMapping.put(new CType("GLvoid", true, true),
69             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("GLvoid", false, true), new JType("java.nio.Buffer", true, false))70     typeMapping.put(new CType("GLvoid", false, true),
71             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("void", false, true), new JType("java.nio.Buffer", true, false))72     typeMapping.put(new CType("void", false, true),
73             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("GLeglImageOES", false, false), new JType("java.nio.Buffer", true, false))74     typeMapping.put(new CType("GLeglImageOES", false, false),
75             new JType("java.nio.Buffer", true, false));
76 
77     // Typed pointers map to typed Buffers
typeMapping.put(new CType("GLboolean", false, true), new JType("java.nio.IntBuffer", true, false))78     typeMapping.put(new CType("GLboolean", false, true),
79             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLenum", false, true), new JType("java.nio.IntBuffer", true, false))80     typeMapping.put(new CType("GLenum", false, true),
81             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLfixed", false, true), new JType("java.nio.IntBuffer", true, false))82     typeMapping.put(new CType("GLfixed", false, true),
83             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLfixed", true, true), new JType("java.nio.IntBuffer", true, false))84     typeMapping.put(new CType("GLfixed", true, true),
85             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLfloat", false, true), new JType("java.nio.FloatBuffer", true, false))86     typeMapping.put(new CType("GLfloat", false, true),
87             new JType("java.nio.FloatBuffer", true, false));
typeMapping.put(new CType("GLfloat", true, true), new JType("java.nio.FloatBuffer", true, false))88     typeMapping.put(new CType("GLfloat", true, true),
89             new JType("java.nio.FloatBuffer", true, false));
typeMapping.put(new CType("GLint", false, true), new JType("java.nio.IntBuffer", true, false))90     typeMapping.put(new CType("GLint", false, true),
91             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLint", true, true), new JType("java.nio.IntBuffer", true, false))92     typeMapping.put(new CType("GLint", true, true),
93             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLsizei", false, true), new JType("java.nio.IntBuffer", true, false))94     typeMapping.put(new CType("GLsizei", false, true),
95             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLuint", false, true), new JType("java.nio.IntBuffer", true, false))96     typeMapping.put(new CType("GLuint", false, true),
97             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLuint", true, true), new JType("java.nio.IntBuffer", true, false))98     typeMapping.put(new CType("GLuint", true, true),
99             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLshort", true, true), new JType("java.nio.ShortBuffer", true, false))100     typeMapping.put(new CType("GLshort", true, true),
101             new JType("java.nio.ShortBuffer", true, false));
102 
103     // Typed pointers map to arrays + offsets
arrayTypeMapping.put(new CType("char", false, true), new JType("byte", false, true))104     arrayTypeMapping.put(new CType("char", false, true),
105             new JType("byte", false, true));
arrayTypeMapping.put(new CType("GLboolean", false, true), new JType("boolean", false, true))106     arrayTypeMapping.put(new CType("GLboolean", false, true),
107             new JType("boolean", false, true));
arrayTypeMapping.put(new CType("GLenum", false, true), new JType("int", false, true))108     arrayTypeMapping.put(new CType("GLenum", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLfixed", true, true), new JType("int", false, true))109     arrayTypeMapping.put(new CType("GLfixed", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLfixed", false, true), new JType("int", false, true))110     arrayTypeMapping.put(new CType("GLfixed", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLfloat", false, true), new JType("float", false, true))111     arrayTypeMapping.put(new CType("GLfloat", false, true), new JType("float", false, true));
arrayTypeMapping.put(new CType("GLfloat", true, true), new JType("float", false, true))112     arrayTypeMapping.put(new CType("GLfloat", true, true), new JType("float", false, true));
arrayTypeMapping.put(new CType("GLint", false, true), new JType("int", false, true))113     arrayTypeMapping.put(new CType("GLint", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLint", true, true), new JType("int", false, true))114     arrayTypeMapping.put(new CType("GLint", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLshort", true, true), new JType("short", false, true))115     arrayTypeMapping.put(new CType("GLshort", true, true), new JType("short", false, true));
arrayTypeMapping.put(new CType("GLsizei", false, true), new JType("int", false, true))116     arrayTypeMapping.put(new CType("GLsizei", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLsizei", true, true), new JType("int", false, true))117     arrayTypeMapping.put(new CType("GLsizei", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLuint", false, true), new JType("int", false, true))118     arrayTypeMapping.put(new CType("GLuint", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLuint", true, true), new JType("int", false, true))119     arrayTypeMapping.put(new CType("GLuint", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLintptr"), new JType("int", false, true))120     arrayTypeMapping.put(new CType("GLintptr"), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLsizeiptr"), new JType("int", false, true))121     arrayTypeMapping.put(new CType("GLsizeiptr"), new JType("int", false, true));
122 
123     //EGL typed pointers map to arrays + offsets
arrayTypeMapping.put(new CType("EGLint", false, true), new JType("int", false, true))124     arrayTypeMapping.put(new CType("EGLint", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("EGLint", true, true), new JType("int", false, true))125     arrayTypeMapping.put(new CType("EGLint", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("EGLConfig", false, true), new JType("EGLConfig", true, true))126     arrayTypeMapping.put(new CType("EGLConfig", false, true), new JType("EGLConfig", true, true));
arrayTypeMapping.put(new CType("EGLConfig", true, true), new JType("EGLConfig", true, true))127     arrayTypeMapping.put(new CType("EGLConfig", true, true), new JType("EGLConfig", true, true));
128 
129     }
130 
JType()131     public JType() {
132     }
133 
JType(String primitiveTypeName)134     public JType(String primitiveTypeName) {
135     this.baseType = primitiveTypeName;
136     this.isClass = false;
137     this.isArray = false;
138     }
139 
JType(String primitiveTypeName, boolean isClass, boolean isArray)140     public JType(String primitiveTypeName, boolean isClass, boolean isArray) {
141     this.baseType = primitiveTypeName;
142     this.isClass = isClass;
143     this.isArray = isArray;
144     }
145 
getBaseType()146     public String getBaseType() {
147     return baseType;
148     }
149 
150     @Override
toString()151     public String toString() {
152     return baseType + (isArray ? "[]" : "");
153     }
154 
isArray()155     public boolean isArray() {
156     return isArray;
157     }
158 
isClass()159     public boolean isClass() {
160     return isClass;
161     }
162 
isString()163     public boolean isString() {
164         return baseType.equals("String");
165     }
166 
isPrimitive()167     public boolean isPrimitive() {
168     return !isClass() && !isArray();
169     }
170 
isVoid()171     public boolean isVoid() {
172     return baseType.equals("void");
173     }
174 
isBuffer()175     public boolean isBuffer() {
176     return baseType.indexOf("Buffer") != -1;
177     }
178 
isTypedBuffer()179     public boolean isTypedBuffer() {
180     return !baseType.equals("java.nio.Buffer") &&
181         (baseType.indexOf("Buffer") != -1);
182     }
183 
isEGLHandle()184     public boolean isEGLHandle() {
185     return !isPrimitive() &&
186         (baseType.startsWith("EGL"));
187     }
188 
convert(CType ctype, boolean useArray)189     public static JType convert(CType ctype, boolean useArray) {
190      JType javaType = null;
191      if (useArray) {
192          javaType = arrayTypeMapping.get(ctype);
193      }
194      if (javaType == null) {
195          javaType = typeMapping.get(ctype);
196      }
197      if (javaType == null) {
198          throw new RuntimeException("Unsupported C type: " + ctype);
199      }
200      return javaType;
201     }
202 }
203