#!/usr/bin/python # -*- coding: utf-8 -*- import os import sys def RemoveAnnotation(line): if line.find(":") >= 0: annotation = line[line.find(":"): line.find(" ", line.find(":"))] return line.replace(annotation, "*") else: return line if __name__ == "__main__": externs = [] lines = open("gl2_api_annotated.in").readlines() i = 0 print """// auto generated by generate_MessageFormatter_java.py" package GLESv2Debugger; public class MessageFormatter { \tstatic String FormatFloats(int count, byte [] data) \t{ \t\tString ret = "["; \t\tfor (int i = 0; i < count; i++) \t\t{ \t\t\tint bits = (data[i* 4 + 0] & 0xff) << 24; \t\t\tbits |= (data[i* 4 + 1] & 0xff) << 16; \t\t\tbits |= (data[i* 4 + 2] & 0xff) << 8; \t\t\tbits |= (data[i* 4 + 3] & 0xff) << 0; \t\t\tret += Float.intBitsToFloat(bits); \t\t\tif (i < count - 1) \t\t\t\tret += ", "; \t\t} \t\treturn ret + "]"; \t} \t \tstatic String FormatInts(int count, byte [] data) \t{ \t\tString ret = "["; \t\tfor (int i = 0; i < count; i++) \t\t{ \t\t\tint bits = (data[i* 4 + 3] & 0xff) << 24; \t\t\tbits |= (data[i* 4 + 2] & 0xff) << 16; \t\t\tbits |= (data[i* 4 + 1] & 0xff) << 8; \t\t\tbits |= (data[i* 4 + 0] & 0xff) << 0; \t\t\tret += bits; \t\t\tif (i < count - 1) \t\t\t\tret += ", "; \t\t} \t\treturn ret + "]"; \t} \t \tstatic String FormatUints(int count, byte [] data) \t{ \t\tString ret = "["; \t\tfor (int i = 0; i < count; i++) \t\t{ \t\t\tlong bits = (data[i* 4 + 3] & 0xff) << 24; \t\t\tbits |= (data[i* 4 + 2] & 0xff) << 16; \t\t\tbits |= (data[i* 4 + 1] & 0xff) << 8; \t\t\tbits |= (data[i* 4 + 0] & 0xff) << 0; \t\t\tret += bits; \t\t\tif (i < count - 1) \t\t\t\tret += ", "; \t\t} \t\treturn ret + "]"; \t} \t \tstatic String FormatMatrix(int columns, int count, byte [] data) \t{ \t\tString ret = "["; \t\tfor (int i = 0; i < count; i++) \t\t{ \t\t\tint bits = (data[i* 4 + 0] & 0xff) << 24; \t\t\tbits |= (data[i* 4 + 1] & 0xff) << 16; \t\t\tbits |= (data[i* 4 + 2] & 0xff) << 8; \t\t\tbits |= (data[i* 4 + 3] & 0xff) << 0; \t\t\tret += Float.intBitsToFloat(bits); \t\t\tif (i % columns == columns - 1) \t\t\t\tret += '\\n'; \t\t\telse if (i < count - 1) \t\t\t\tret += ", "; \t\t} \t\treturn ret + "]"; \t} \tpublic static String Format(final DebuggerMessage.Message msg) \t{ \t\tString str; \t\tswitch (msg.getFunction()) \t\t{""" for line in lines: if line.find("API_ENTRY(") >= 0: # a function prototype returnType = line[0: line.find(" API_ENTRY(")].replace("const ", "") functionName = line[line.find("(") + 1: line.find(")")] #extract GL function name parameterList = line[line.find(")(") + 2: line.find(") {")] # print "\t\tcase %s:" % (functionName) # print "\t\t\t//return MessageFormatterCustom.Format(msg);" # print "\t\t\tstr = msg.toString(); break;" # #extern = "%s Format_%s(%s);" % (returnType, functionName, parameterList) # extern = functionName # externs.append(extern) # continue parameters = parameterList.split(',') paramIndex = 0 formatString = "%s " formatArgs = "" if returnType != "void": if returnType == "GLenum": formatArgs += "GLEnum.valueOf(msg.getRet())" elif returnType.find("*") >= 0: formatArgs += '"0x" + Integer.toHexString(msg.getRet())' else: formatArgs += "msg.getRet()" else: formatArgs += '"void"' #formatString += "%s(" % (functionName) formatString += "(" if parameterList == "void": parameters = [] paramTypes = [] paramNames = [] paramAnnotations = [] for parameter in parameters: parameter = parameter.replace("const","") parameter = parameter.strip() paramType = parameter.split(' ')[0] paramName = parameter.split(' ')[1] annotation = "" formatString += paramName + "=%s" if parameter.find(":") >= 0: assert paramIndex == len(parameters) - 1 # only last parameter should be annotated inout = paramType.split(":")[2] annotation = paramType.split(":")[1] paramType = paramType.split(":")[0] assert paramType.find("void") < 0 count = 1 countArg = "" if annotation.find("*") >= 0: count = int(annotation.split("*")[0]) countArg = annotation.split("*")[1] assert countArg in paramNames elif annotation in paramNames: count = 1 countArg = annotation elif annotation == "GLstring": annotation = annotation else: count = int(annotation) dataFormatter = "" if paramType == "GLfloat": dataFormatter = "FormatFloats" elif paramType == "GLint": dataFormatter = "FormatInts" elif paramType == "GLuint": dataFormatter = "FormatUints" elif annotation == "GLstring": assert paramType == "GLchar" else: assert 0 if functionName.find("Matrix") >= 0: columns = int(functionName[functionName.find("fv") - 1: functionName.find("fv")]) assert columns * columns == count assert countArg != "" assert paramType == "GLfloat" formatArgs += ", FormatMatrix(%d, %d * msg.getArg%d(), msg.getData().toByteArray())" % (columns, count, paramNames.index(countArg)) elif annotation == "GLstring": formatArgs += ", msg.getData().toStringUtf8()" elif countArg == "": formatArgs += ", %s(%d, msg.getData().toByteArray())" % (dataFormatter, count) else: formatArgs += ", %s(%d * msg.getArg%d(), msg.getData().toByteArray())" % (dataFormatter, count, paramNames.index(countArg)) else: #formatArgs += ', "%s"' % (paramName) if paramType == "GLfloat" or paramType == "GLclampf": formatArgs += ", Float.intBitsToFloat(msg.getArg%d())" % (paramIndex) elif paramType == "GLenum": formatArgs += ", GLEnum.valueOf(msg.getArg%d())" % (paramIndex) elif paramType.find("*") >= 0: formatArgs += ', "0x" + Integer.toHexString(msg.getArg%d())' % (paramIndex) else: formatArgs += ", msg.getArg%d()" % (paramIndex) if paramIndex < len(parameters) - 1: formatString += ", " paramTypes.append(paramType) paramNames.append(paramName) paramAnnotations.append(annotation) paramIndex += 1 formatString += ")" print "\t\tcase %s:" % (functionName) if line.find("*") >= 0 and (line.find("*") < line.find(":") or line.find("*") > line.rfind(":")): sys.stderr.write(line) print "\t\t\t// FIXME: this function uses pointers, debugger may send data in msg.data" print '\t\t\tstr = String.format("%s", %s); break;' % (formatString, formatArgs) print """\t\tdefault: \t\t\tstr = msg.toString(); \t\t} \t\treturn str; \t} }""" print """/* package GLESv2Debugger; public class MessageFormatterCustom { \tpublic static String Format(final DebuggerMessage.Message msg) { \t\tString str; \t\tswitch (msg.getFunction()) {""" for extern in externs: print "\t\tcase %s" % (extern) print "\t\t\t// TODO:" print """\t\tdefault: \t\t\tstr = msg.toString(); \t\t} \t\treturn str; \t} } */"""