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