1# -*- coding: utf-8 -*- 2 3import os 4import string 5 6from common import * 7from khr_util.format import indentLines 8 9class LogSpec: 10 def __init__ (self, argInPrints, argOutPrints = {}, returnPrint = None): 11 self.argInPrints = argInPrints 12 self.argOutPrints = argOutPrints 13 self.returnPrint = returnPrint 14 15def enum (group): 16 return lambda name: "get%sStr(%s)" % (group, name) 17 18def pointer (size): 19 return lambda name: "getPointerStr(%s, %s)" % (name, size) 20 21def enumPointer (group, size): 22 return lambda name: "getEnumPointerStr<%(nameFunc)s>(%(name)s, %(size)s)" % {"name": name, "size": size, "nameFunc": ("get%sName" % group)} 23 24def configAttrib (attribNdx): 25 return lambda name: "getConfigAttribValueStr(param%d, %s)" % (attribNdx, name) 26 27# Special rules for printing call arguments 28CALL_LOG_SPECS = { 29 "eglBindAPI": LogSpec({0: enum("API")}), 30 "eglChooseConfig": LogSpec({1: lambda n: "getConfigAttribListStr(%s)" % n}, argOutPrints = {2: pointer("(num_config && returnValue) ? deMin32(config_size, *num_config) : 0"), 4: lambda n: "(%s ? de::toString(*%s) : \"NULL\")" % (n, n)}), 31 "eglCreateContext": LogSpec({3: lambda n: "getContextAttribListStr(%s)" % n}), 32 "eglCreatePbufferSurface": LogSpec({2: lambda n: "getSurfaceAttribListStr(%s)" % n}), 33 "eglCreatePixmapSurface": LogSpec({3: lambda n: "getSurfaceAttribListStr(%s)" % n}), 34 "eglCreateWindowSurface": LogSpec({3: lambda n: "getSurfaceAttribListStr(%s)" % n}), 35 "eglGetError": LogSpec({}, returnPrint = enum("Error")), 36 "eglGetConfigAttrib": LogSpec({2: enum("ConfigAttrib")}, argOutPrints = {3: lambda n: "getConfigAttribValuePointerStr(attribute, %s)" % n}), 37 "eglGetCurrentSurface": LogSpec({0: enum("SurfaceTarget")}), 38 "eglGetProcAddress": LogSpec({}, returnPrint = lambda n: "tcu::toHex(%s)" % (n)), 39 "eglQueryAPI": LogSpec({}, returnPrint = enum("API")), 40 "eglQueryContext": LogSpec({2: enum("ContextAttrib")}, argOutPrints = {3: lambda n: "getContextAttribValuePointerStr(attribute, %s)" % n}), 41 "eglQuerySurface": LogSpec({2: enum("SurfaceAttrib")}, argOutPrints = {3: lambda n: "getSurfaceAttribValuePointerStr(attribute, %s)" % n}), 42 "eglSurfaceAttrib": LogSpec({2: enum("SurfaceAttrib"), 3: lambda n: "getSurfaceAttribValueStr(attribute, %s)" % n}), 43} 44 45def eglwPrefix (string): 46 if string[:5] == "__egl": 47 return "eglw::" + string 48 else: 49 return re.sub(r'\bEGL', 'eglw::EGL', string) 50 51def prefixedParams (command): 52 if len(command.params) > 0: 53 return ", ".join(eglwPrefix(param.declaration) for param in command.params) 54 else: 55 return "void" 56 57def commandLogWrapperMemberDecl (command): 58 return "%s\t%s\t(%s);" % (eglwPrefix(command.type), command.name, prefixedParams(command)) 59 60def getVarDefaultPrint (type, varName): 61 if re.match(r'^const +char *\*$', type): 62 return "getStringStr(%s)" % varName 63 elif re.match(r'(EGLenum|EGLConfig|EGLSurface|EGLClientBuffer|EGLNativeDisplayType|EGLNativeWindowType|EGLNativePixmapType|\*)$', type): 64 return "toHex(%s)" % varName 65 elif type == 'EGLBoolean': 66 return "getBooleanStr(%s)" % varName 67 else: 68 return varName 69 70def commandLogWrapperMemberDef (command): 71 src = "" 72 try: 73 logSpec = CALL_LOG_SPECS[command.name] 74 except KeyError: 75 logSpec = None 76 77 src += "\n" 78 src += "%s CallLogWrapper::%s (%s)\n{\n" % (eglwPrefix(command.type), command.name, ", ".join(eglwPrefix(p.declaration) for p in command.params)) 79 80 # Append paramemetrs 81 callPrintItems = ["\"%s(\"" % command.name] 82 for paramNdx, param in enumerate(command.params): 83 if paramNdx > 0: 84 callPrintItems.append("\", \"") 85 86 if logSpec and paramNdx in logSpec.argInPrints: 87 callPrintItems.append(logSpec.argInPrints[paramNdx](param.name)) 88 else: 89 callPrintItems.append(getVarDefaultPrint(param.type, param.name)) 90 91 callPrintItems += ["\");\"", "TestLog::EndMessage"] 92 93 src += "\tif (m_enableLog)\n" 94 src += "\t\tm_log << TestLog::Message << %s;\n" % " << ".join(callPrintItems) 95 96 callStr = "m_egl.%s(%s)" % (getFunctionMemberName(command.name), ", ".join([p.name for p in command.params])) 97 98 isVoid = command.type == 'void' 99 if isVoid: 100 src += "\t%s;\n" % callStr 101 else: 102 src += "\t%s returnValue = %s;\n" % (eglwPrefix(command.type), callStr) 103 104 if logSpec and len(logSpec.argOutPrints) > 0: 105 # Print values returned in pointers 106 src += "\tif (m_enableLog)\n\t{\n" 107 108 for paramNdx, param in enumerate(command.params): 109 if paramNdx in logSpec.argOutPrints: 110 src += "\t\tm_log << TestLog::Message << \"// %s = \" << %s << TestLog::EndMessage;\n" % (param.name, logSpec.argOutPrints[paramNdx](param.name)) 111 112 src += "\t}\n" 113 114 if not isVoid: 115 # Print return value 116 returnPrint = getVarDefaultPrint(command.type, "returnValue") 117 if logSpec and logSpec.returnPrint: 118 returnPrint = logSpec.returnPrint("returnValue") 119 120 src += "\tif (m_enableLog)\n" 121 src += "\t\tm_log << TestLog::Message << \"// \" << %s << \" returned\" << TestLog::EndMessage;\n" % returnPrint 122 src += "\treturn returnValue;\n" 123 124 src += "}" 125 return src 126 127def gen (iface): 128 genCommandList(iface, commandLogWrapperMemberDecl, EGL_DIR, "egluCallLogWrapperApi.inl", True) 129 genCommandList(iface, commandLogWrapperMemberDef, EGL_DIR, "egluCallLogWrapper.inl", False) 130