Home | History | Annotate | Download | only in com.android.ide.eclipse.gldebugger
      1 #!/usr/bin/python
      2 # -*- coding: utf-8 -*-
      3 
      4 #
      5 # Copyright 2011, The Android Open Source Project
      6 #
      7 # Licensed under the Apache License, Version 2.0 (the "License");
      8 # you may not use this file except in compliance with the License.
      9 # You may obtain a copy of the License at
     10 #
     11 #     http://www.apache.org/licenses/LICENSE-2.0
     12 #
     13 # Unless required by applicable law or agreed to in writing, software
     14 # distributed under the License is distributed on an "AS IS" BASIS,
     15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 # See the License for the specific language governing permissions and
     17 # limitations under the License.
     18 #
     19 
     20 import os
     21 import sys
     22 
     23 def RemoveAnnotation(line):
     24     if line.find(":") >= 0:
     25         annotation = line[line.find(":"): line.find(" ", line.find(":"))]
     26         return line.replace(annotation, "*")
     27     else:
     28         return line
     29         
     30 if __name__ == "__main__":
     31     externs = []
     32     lines = open("../../../frameworks/base/opengl/libs/GLES2_dbg/gl2_api_annotated.in").readlines()
     33     output = open("src/com/android/glesv2debugger/MessageFormatter.java", "w")
     34     
     35     i = 0
     36     output.write(
     37 """/*
     38  ** Copyright 2011, The Android Open Source Project
     39  **
     40  ** Licensed under the Apache License, Version 2.0 (the "License");
     41  ** you may not use this file except in compliance with the License.
     42  ** You may obtain a copy of the License at
     43  **
     44  **     http://www.apache.org/licenses/LICENSE-2.0
     45  **
     46  ** Unless required by applicable law or agreed to in writing, software
     47  ** distributed under the License is distributed on an "AS IS" BASIS,
     48  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     49  ** See the License for the specific language governing permissions and
     50  ** limitations under the License.
     51  */
     52 
     53 // auto generated by generate_MessageFormatter_java.py"
     54 
     55 package com.android.glesv2debugger;
     56 
     57 import java.nio.ByteBuffer;
     58 
     59 public class MessageFormatter {
     60 
     61     static String formatFloats(int count, final ByteBuffer data) {
     62         if (data.remaining() == 0)
     63             return "{}";
     64         data.order(SampleView.targetByteOrder);
     65         String ret = "{";
     66         for (int i = 0; i < count; i++) {
     67             ret += Float.intBitsToFloat(data.getInt());
     68             if (i < count - 1)
     69                 ret += ", ";
     70         }
     71         return ret + "}";
     72     }
     73 
     74     static String formatInts(int count, final ByteBuffer data) {
     75         if (data.remaining() == 0)
     76             return "{}";
     77         data.order(SampleView.targetByteOrder);
     78         String ret = "{";
     79         for (int i = 0; i < count; i++) {
     80             ret += data.getInt();
     81             if (i < count - 1)
     82                 ret += ", ";
     83         }
     84         return ret + "}";
     85     }
     86 
     87     static String formatUInts(int count, final ByteBuffer data) {
     88         if (data.remaining() == 0)
     89             return "{}";
     90         data.order(SampleView.targetByteOrder);
     91         String ret = "{";
     92         for (int i = 0; i < count; i++) {
     93             long bits = data.getInt() & 0xffffffff;
     94             ret += bits;
     95             if (i < count - 1)
     96                 ret += ", ";
     97         }
     98         return ret + "}";
     99     }
    100 
    101     static String formatMatrix(int columns, int count, final ByteBuffer data) {
    102         if (data.remaining() == 0)
    103             return "{}";
    104         data.order(SampleView.targetByteOrder);
    105         String ret = "{";
    106         for (int i = 0; i < count; i++) {
    107             ret += Float.intBitsToFloat(data.getInt());
    108             if (i < count - 1)
    109                 ret += ", ";
    110             if (i % columns == columns - 1)
    111                 ret += "\\n                                             ";
    112         }
    113         return ret + "}";
    114     }
    115 
    116     public static String format(final DebuggerMessage.Message msg,
    117                                 final boolean code) {
    118         String str;
    119         switch (msg.getFunction()) {
    120 """)
    121     #in source code these turn into program_%d etc.
    122     nameReplaces = ["program", "shader", "texture", "buffer", "framebuffer", "renderbuffer"]
    123     for line in lines:
    124         if line.find("API_ENTRY(") >= 0: # a function prototype
    125             returnType = line[0: line.find(" API_ENTRY(")].replace("const ", "")
    126             functionName = line[line.find("(") + 1: line.find(")")] #extract GL function name
    127             parameterList = line[line.find(")(") + 2: line.find(") {")]
    128 
    129             parameters = parameterList.split(',')
    130             paramIndex = 0
    131 
    132             formatString = "%s"
    133             formatArgs = ""
    134             if returnType != "void":
    135                 if returnType == "GLenum":
    136                     formatArgs += '\
    137                     (code ? "%s" : GLEnum.valueOf(msg.getRet()))\n' % (functionName)
    138                 elif returnType.find("*") >= 0:
    139                     formatArgs += '\
    140                     (code ? "%s" : "0x" + Integer.toHexString(msg.getRet()))\n' % (functionName)
    141                 else:
    142                     formatArgs += '\
    143                     (code ? "%s" : msg.getRet())\n' % (functionName)
    144             else:
    145                 formatArgs += '\
    146                     (code ? "%s" : "void")\n' % (functionName)
    147 
    148             formatString += "("
    149 
    150             if parameterList == "void":
    151                 parameters = []
    152             inout = ""
    153 
    154             paramNames = []
    155 
    156             for parameter in parameters:
    157                 parameter = parameter.replace("const","")
    158                 parameter = parameter.strip()
    159                 paramType = parameter.split(' ')[0]
    160                 paramName = parameter.split(' ')[1]
    161                 annotation = ""
    162 
    163                 formatString += "%s%s"
    164                 formatArgs += '\
    165                     , (code ? "/*%s*/ " : "%s=")\n' % (paramName, paramName)
    166                 if parameter.find(":") >= 0:
    167                     assert inout == "" # only one parameter should be annotated
    168                     inout = paramType.split(":")[2]
    169                     annotation = paramType.split(":")[1]
    170                     paramType = paramType.split(":")[0]
    171                     count = 1
    172                     countArg = ""
    173                     if annotation.find("*") >= 0: # [1,n] * param
    174                         count = int(annotation.split("*")[0])
    175                         countArg = annotation.split("*")[1]
    176                         assert countArg in paramNames
    177                     elif annotation in paramNames:
    178                         count = 1
    179                         countArg = annotation
    180                     elif annotation == "GLstring":
    181                         annotation = annotation
    182                     else:
    183                         count = int(annotation)
    184                     dataFormatter = ""
    185                     if paramType == "GLfloat":
    186                         dataFormatter = "formatFloats"
    187                     elif paramType == "GLint":
    188                         dataFormatter = "formatInts"
    189                     elif paramType == "GLuint":
    190                         dataFormatter = "formatUInts"
    191                     elif annotation == "GLstring":
    192                         assert paramType == "GLchar"
    193                     elif paramType.find("void") >= 0:
    194                         assert 1
    195                     else:
    196                         assert 0
    197                     if functionName.find("Matrix") >= 0:
    198                         columns = int(functionName[functionName.find("fv") - 1: functionName.find("fv")])
    199                         assert columns * columns == count
    200                         assert countArg != ""
    201                         assert paramType == "GLfloat"
    202                         formatArgs += '\
    203                     , (code ? "(GLfloat [])" : "") + formatMatrix(%d, %d * msg.getArg%d(), msg.getData().asReadOnlyByteBuffer())' % (
    204                         columns, count, paramNames.index(countArg))
    205                     elif annotation == "GLstring":
    206                         formatArgs += '\
    207                     , (code ? "\\"" : "") + msg.getData().toStringUtf8() + (code ? "\\"" : "")'
    208                     elif paramType.find("void") >= 0:
    209                         formatArgs += '\
    210                     , (code ? "arg%d" : "0x" + Integer.toHexString(msg.getArg%d()))' % (paramIndex, paramIndex)
    211                     elif countArg == "":
    212                         formatArgs += '\
    213                     , (code ? "(%s [])" : "") + %s(%d, msg.getData().asReadOnlyByteBuffer())' % (
    214                         paramType, dataFormatter, count)
    215                     else:
    216                         formatArgs += '\
    217                     , (code ? "(%s [])" : "") +  %s(%d * msg.getArg%d(), msg.getData().asReadOnlyByteBuffer())' % (
    218                         paramType, dataFormatter, count, paramNames.index(countArg))
    219                 else:
    220                     if paramType == "GLfloat" or paramType == "GLclampf":
    221                         formatArgs += "\
    222                     , Float.intBitsToFloat(msg.getArg%d())" % (paramIndex)
    223                     elif paramType == "GLenum": 
    224                         formatArgs += "\
    225                     , GLEnum.valueOf(msg.getArg%d())" % (paramIndex)
    226                     elif paramType.find("*") >= 0:
    227                         formatArgs += '\
    228                     , (code ? "arg%d" : "0x" + Integer.toHexString(msg.getArg%d()))' % (paramIndex, paramIndex)
    229                     elif paramName in nameReplaces:
    230                         formatArgs += '\
    231                     , (code ? "%s_" : "") + msg.getArg%d()' % (paramName, paramIndex)
    232                     else:
    233                         formatArgs += "\
    234                     , msg.getArg%d()" % (paramIndex)
    235                 if paramIndex < len(parameters) - 1:
    236                     formatString += ", "
    237                     formatArgs += '\n'
    238                 paramNames.append(paramName)
    239                 paramIndex += 1  
    240 
    241                 
    242             formatString += ")"
    243              
    244             output.write("            case %s:\n" % (functionName))
    245             if line.find("*") >= 0 and (line.find("*") < line.find(":") or line.find("*") > line.rfind(":")):
    246                 sys.stderr.write(line)
    247                 output.write("                // FIXME: this function uses pointers, debugger may send data in msg.data\n")
    248             output.write('\
    249                 str = String.format("%s",\n%s);\n\
    250                 break;\n' % (formatString, formatArgs))
    251 
    252 
    253     output.write("""            default:
    254                 str = msg.toString();
    255         }
    256         return str;
    257     }
    258 }""")
    259 
    260 '''    print """/*
    261 package GLESv2Debugger;
    262 
    263 public class MessageFormatterCustom {
    264 
    265     public static String format(final DebuggerMessage.Message msg) {
    266         String str;
    267         switch (msg.getFunction()) {"""
    268 
    269     for extern in externs:
    270         print "        case %s" % (extern)
    271         print "            // TODO:"
    272 
    273 print """        default:
    274             str = msg.toString();
    275         }
    276         return str;
    277     }
    278 }
    279 */"""    '''
    280         
    281         
    282