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