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.ArrayList;
     18 import java.util.List;
     19 
     20 public class JFunc {
     21 
     22     String className = "com.google.android.gles_jni.GL11Impl";
     23 
     24     CFunc cfunc;
     25     JType ftype;
     26     String fname;
     27 
     28     List<String> argNames = new ArrayList<String>();
     29     List<JType> argTypes = new ArrayList<JType>();
     30     List<Integer> argCIndices = new ArrayList<Integer>();
     31 
     32     boolean hasBufferArg = false;
     33     boolean hasTypedBufferArg = false;
     34     ArrayList<String> bufferArgNames = new ArrayList<String>();
     35 
     36     public JFunc(CFunc cfunc) {
     37         this.cfunc = cfunc;
     38     }
     39 
     40     public CFunc getCFunc() {
     41         return cfunc;
     42     }
     43 
     44     public void setName(String fname) {
     45         this.fname = fname;
     46     }
     47 
     48     public String getName() {
     49         return fname;
     50     }
     51 
     52     public void setType(JType ftype) {
     53         this.ftype = ftype;
     54     }
     55 
     56     public JType getType() {
     57         return ftype;
     58     }
     59 
     60     public void setClassName(String className) {
     61         this.className = className;
     62     }
     63 
     64     public String getClassName() {
     65         return className;
     66     }
     67 
     68     public boolean hasBufferArg() {
     69         return hasBufferArg;
     70     }
     71 
     72     public boolean hasTypedBufferArg() {
     73         return hasTypedBufferArg;
     74     }
     75 
     76     public String getBufferArgName(int index) {
     77         return bufferArgNames.get(index);
     78     }
     79 
     80     public void addArgument(String argName, JType argType, int cindex) {
     81         argNames.add(argName);
     82         argTypes.add(argType);
     83         argCIndices.add(new Integer(cindex));
     84 
     85         if (argType.isBuffer()) {
     86             hasBufferArg = true;
     87             bufferArgNames.add(argName);
     88         }
     89         if (argType.isTypedBuffer()) {
     90             hasTypedBufferArg = true;
     91             bufferArgNames.add(argName);
     92         }
     93     }
     94 
     95     public int getNumArgs() {
     96         return argNames.size();
     97     }
     98 
     99     public int getArgIndex(String name) {
    100         int len = argNames.size();
    101         for (int i = 0; i < len; i++) {
    102             if (name.equals(argNames.get(i))) {
    103                 return i;
    104             }
    105         }
    106         return -1;
    107     }
    108 
    109     public String getArgName(int index) {
    110         return argNames.get(index);
    111     }
    112 
    113     public JType getArgType(int index) {
    114         return argTypes.get(index);
    115     }
    116 
    117     public int getArgCIndex(int index) {
    118         return argCIndices.get(index).intValue();
    119     }
    120 
    121     public static JFunc convert(CFunc cfunc, boolean useArray) {
    122         try {
    123             JFunc jfunc = new JFunc(cfunc);
    124             jfunc.setName(cfunc.getName());
    125             jfunc.setType(JType.convert(cfunc.getType(), false));
    126 
    127             int numArgs = cfunc.getNumArgs();
    128             int numOffsets = 0;
    129             for (int i = 0; i < numArgs; i++) {
    130                 CType cArgType = cfunc.getArgType(i);
    131                 if (cArgType.isTypedPointer() && useArray) {
    132                     ++numOffsets;
    133                 }
    134             }
    135 
    136             for (int i = 0; i < numArgs; i++) {
    137                 String cArgName = cfunc.getArgName(i);
    138                 CType cArgType = cfunc.getArgType(i);
    139 
    140                 jfunc.addArgument(cArgName, JType.convert(cArgType, useArray), i);
    141                 if (cArgType.isTypedPointer() && useArray) {
    142                     if (numOffsets > 1) {
    143                         jfunc.addArgument(cArgName + "Offset", new JType("int"), i);
    144                     } else {
    145                         jfunc.addArgument("offset", new JType("int"), i);
    146                     }
    147                 }
    148             }
    149 
    150             return jfunc;
    151         } catch (RuntimeException e) {
    152             System.err.println("Failed to convert function " + cfunc);
    153             throw e;
    154         }
    155     }
    156 
    157     @Override
    158     public String toString() {
    159         String s =  "Function " + fname + " returns " + ftype + ": ";
    160         for (int i = 0; i < argNames.size(); i++) {
    161             if (i > 0) {
    162                 s += ", ";
    163             }
    164             s += argTypes.get(i) + " " + argNames.get(i);
    165         }
    166         return s;
    167     }
    168 
    169 }
    170