1# -*- coding: utf-8 -*- 2 3#------------------------------------------------------------------------- 4# drawElements Quality Program utilities 5# -------------------------------------- 6# 7# Copyright 2015 The Android Open Source Project 8# 9# Licensed under the Apache License, Version 2.0 (the "License"); 10# you may not use this file except in compliance with the License. 11# You may obtain a copy of the License at 12# 13# http://www.apache.org/licenses/LICENSE-2.0 14# 15# Unless required by applicable law or agreed to in writing, software 16# distributed under the License is distributed on an "AS IS" BASIS, 17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18# See the License for the specific language governing permissions and 19# limitations under the License. 20# 21#------------------------------------------------------------------------- 22 23import sys, logging, re 24from lxml import etree 25from collections import OrderedDict 26from functools import wraps, partial 27 28log = logging.getLogger(__name__) 29 30debug = log.debug 31info = log.info 32warning = log.warning 33 34def warnElem(elem, fmt, *args): 35 warning('%s:%d, %s %s: ' + fmt, elem.base, elem.sourceline, elem.tag, elem.get('name') or '', *args) 36 37class Object(object): 38 def __init__(self, **kwargs): 39 self.__dict__.update(kwargs) 40 41class Located(Object): 42 location = None 43 44class Group(Located): pass 45class Enum(Located): pass 46class Enums(Located): 47 name = None 48 comment = None 49 enums = None 50 51class Type(Located): 52 location = None 53 name=None 54 definition=None 55 api=None 56 requires=None 57 58def makeObject(cls, elem, **kwargs): 59 kwargs.setdefault('name', elem.get('name')) 60 kwargs.setdefault('comment', elem.get('comment')) 61 kwargs['location'] = (elem.base, elem.sourceline) 62 return cls(**kwargs) 63 64def parseEnum(eEnum): 65 return makeObject( 66 Enum, eEnum, 67 value=eEnum.get('value'), 68 type=eEnum.get('type'), 69 alias=eEnum.get('alias')) 70 71class Param(Located): pass 72 73class Command(Located): 74 name=None 75 declaration=None 76 type=None 77 ptype=None 78 group=None 79 params=None 80 alias=None 81 82class Interface(Object): pass 83 84class Index: 85 def __init__(self, items=[], **kwargs): 86 self.index = {} 87 self.items = [] 88 self.__dict__.update(kwargs) 89 self.update(items) 90 91 def append(self, item): 92 keys = self.getkeys(item) 93 for key in keys: 94 self[key] = item 95 self.items.append(item) 96 97 def update(self, items): 98 for item in items: 99 self.append(item) 100 101 def __iter__(self): 102 return iter(self.items) 103 104 def nextkey(self, key): 105 raise KeyError 106 107 def getkeys(self, item): 108 return [] 109 110 def __contains__(self, key): 111 return key in self.index 112 113 def __setitem__(self, key, item): 114 if key in self.index: 115 self.duplicateKey(key, item) 116 else: 117 self.index[key] = item 118 119 def duplicateKey(self, key, item): 120 warning("Duplicate %s: %r", type(item).__name__.lower(), key) 121 122 def __getitem__(self, key): 123 try: 124 while True: 125 try: 126 return self.index[key] 127 except KeyError: 128 pass 129 key = self.nextkey(key) 130 except KeyError: 131 item = self.missingKey(key) 132 self.append(item) 133 return item 134 135 def missingKey(self, key): 136 raise KeyError(key) 137 138 def __len__(self): 139 return len(self.items) 140 141class ElemNameIndex(Index): 142 def getkeys(self, item): 143 return [item.get('name')] 144 145 def duplicateKey(self, key, item): 146 warnElem(item, "Duplicate key: %s", key) 147 148class CommandIndex(Index): 149 def getkeys(self, item): 150 #BOZA: No reason to add alias: it has its own entry in enums in xml file 151 #return [(name, api)] + ([(alias, api)] if alias is not None else []) 152 return [item.findtext('proto/name')] 153 154class NameApiIndex(Index): 155 def getkeys(self, item): 156 return [(item.get('name'), item.get('api'))] 157 158 def nextkey(self, key): 159 if len(key) == 2 and key[1] is not None: 160 return key[0], None 161 raise KeyError 162 163 def duplicateKey(self, key, item): 164 warnElem(item, "Duplicate key: %s", key) 165 166class TypeIndex(NameApiIndex): 167 def getkeys(self, item): 168 return [(item.get('name') or item.findtext('name'), item.get('api'))] 169 170class EnumIndex(NameApiIndex): 171 def getkeys(self, item): 172 name, api, alias = (item.get(attrib) for attrib in ['name', 'api', 'alias']) 173 #BOZA: No reason to add alias: it has its own entry in enums 174 #return [(name, api)] + ([(alias, api)] if alias is not None else []) 175 return [(name, api)] 176 177 def duplicateKey(self, nameapipair, item): 178 (name, api) = nameapipair 179 if name == item.get('alias'): 180 warnElem(item, "Alias already present: %s", name) 181 else: 182 warnElem(item, "Already present") 183 184class Registry: 185 def __init__(self, eRegistry): 186 self.types = TypeIndex(eRegistry.findall('types/type')) 187 self.groups = ElemNameIndex(eRegistry.findall('groups/group')) 188 self.enums = EnumIndex(eRegistry.findall('enums/enum')) 189 for eEnum in self.enums: 190 groupName = eEnum.get('group') 191 if groupName is not None: 192 self.groups[groupName] = eEnum 193 self.commands = CommandIndex(eRegistry.findall('commands/command')) 194 self.features = ElemNameIndex(eRegistry.findall('feature')) 195 self.apis = {} 196 for eFeature in self.features: 197 self.apis.setdefault(eFeature.get('api'), []).append(eFeature) 198 for apiFeatures in self.apis.values(): 199 apiFeatures.sort(key=lambda eFeature: eFeature.get('number')) 200 self.extensions = ElemNameIndex(eRegistry.findall('extensions/extension')) 201 self.element = eRegistry 202 203 def getFeatures(self, api, checkVersion=None): 204 return [eFeature for eFeature in self.apis[api] 205 if checkVersion is None or checkVersion(eFeature.get('number'))] 206 207class NameIndex(Index): 208 createMissing = None 209 kind = "item" 210 211 def getkeys(self, item): 212 return [item.name] 213 214 def missingKey(self, key): 215 if self.createMissing: 216 warning("Reference to implicit %s: %r", self.kind, key) 217 return self.createMissing(name=key) 218 else: 219 raise KeyError 220 221def matchApi(api1, api2): 222 return api1 is None or api2 is None or api1 == api2 223 224class Interface(Object): 225 pass 226 227def extractAlias(eCommand): 228 aliases = eCommand.xpath('alias/@name') 229 return aliases[0] if aliases else None 230 231def getExtensionName(eExtension): 232 return eExtension.get('name') 233 234def extensionSupports(eExtension, api, profile=None): 235 if api == 'gl' and profile == 'core': 236 needSupport = 'glcore' 237 else: 238 needSupport = api 239 supporteds = eExtension.get('supported').split('|') 240 return needSupport in supporteds 241 242class InterfaceSpec(Object): 243 def __init__(self): 244 self.enums = set() 245 self.types = set() 246 self.commands = set() 247 self.versions = set() 248 249 def addComponent(self, eComponent): 250 if eComponent.tag == 'require': 251 def modify(items, item): items.add(item) 252 else: 253 assert eComponent.tag == 'remove' 254 def modify(items, item): 255 try: 256 items.remove(item) 257 except KeyError: 258 warning("Tried to remove absent item: %s", item) 259 for typeName in eComponent.xpath('type/@name'): 260 modify(self.types, typeName) 261 for enumName in eComponent.xpath('enum/@name'): 262 modify(self.enums, enumName) 263 for commandName in eComponent.xpath('command/@name'): 264 modify(self.commands, commandName) 265 266 def addComponents(self, elem, api, profile=None): 267 for eComponent in elem.xpath('require|remove'): 268 cApi = eComponent.get('api') 269 cProfile = eComponent.get('profile') 270 if (matchApi(api, eComponent.get('api')) and 271 matchApi(profile, eComponent.get('profile'))): 272 self.addComponent(eComponent) 273 274 def addFeature(self, eFeature, api=None, profile=None, force=False): 275 info('Feature %s', eFeature.get('name')) 276 if not matchApi(api, eFeature.get('api')): 277 if not force: return 278 warnElem(eFeature, 'API %s is not supported', api) 279 self.addComponents(eFeature, api, profile) 280 self.versions.add(eFeature.get('name')) 281 282 def addExtension(self, eExtension, api=None, profile=None, force=False): 283 if not extensionSupports(eExtension, api, profile): 284 if not force: return 285 warnElem(eExtension, '%s is not supported in API %s' % (getExtensionName(eExtension), api)) 286 self.addComponents(eExtension, api, profile) 287 288def createInterface(registry, spec, api=None): 289 def parseType(eType): 290 # todo: apientry 291 #requires = eType.get('requires') 292 #if requires is not None: 293 # types[requires] 294 return makeObject( 295 Type, eType, 296 name=eType.get('name') or eType.findtext('name'), 297 definition=''.join(eType.xpath('.//text()')), 298 api=eType.get('api'), 299 requires=eType.get('requires')) 300 301 def createType(name): 302 info('Add type %s', name) 303 try: 304 return parseType(registry.types[name, api]) 305 except KeyError: 306 return Type(name=name) 307 308 def createEnum(enumName): 309 info('Add enum %s', enumName) 310 return parseEnum(registry.enums[enumName, api]) 311 312 def extractPtype(elem): 313 ePtype = elem.find('ptype') 314 if ePtype is None: 315 return None 316 return types[ePtype.text] 317 318 def extractGroup(elem): 319 groupName = elem.get('group') 320 if groupName is None: 321 return None 322 return groups[groupName] 323 324 def parseParam(eParam): 325 return makeObject( 326 Param, eParam, 327 name=eParam.get('name') or eParam.findtext('name'), 328 declaration=''.join(eParam.xpath('.//text()')).strip(), 329 type=''.join(eParam.xpath('(.|ptype)/text()')).strip(), 330 ptype=extractPtype(eParam), 331 group=extractGroup(eParam)) 332 333 def createCommand(commandName): 334 info('Add command %s', commandName) 335 eCmd = registry.commands[commandName] 336 eProto = eCmd.find('proto') 337 return makeObject( 338 Command, eCmd, 339 name=eCmd.findtext('proto/name'), 340 declaration=''.join(eProto.xpath('.//text()')).strip(), 341 type=''.join(eProto.xpath('(.|ptype)/text()')).strip(), 342 ptype=extractPtype(eProto), 343 group=extractGroup(eProto), 344 alias=extractAlias(eCmd), 345 params=NameIndex(list(map(parseParam, eCmd.findall('param'))))) 346 347 def createGroup(name): 348 info('Add group %s', name) 349 try: 350 eGroup = registry.groups[name] 351 except KeyError: 352 return Group(name=name) 353 return makeObject( 354 Group, eGroup, 355 # Missing enums are often from exotic extensions. Don't create dummy entries, 356 # just filter them out. 357 enums=NameIndex(enums[name] for name in eGroup.xpath('enum/@name') 358 if name in enums)) 359 360 def sortedIndex(items): 361 # Some groups have no location set, due to it is absent in gl.xml file 362 # for example glGetFenceivNV uses group FenceNV which is not declared 363 # <command> 364 # <proto>void <name>glGetFenceivNV</name></proto> 365 # <param group="FenceNV"><ptype>GLuint</ptype> <name>fence</name></param> 366 # Python 2 ignores it. Avoid sorting to allow Python 3 to continue 367 368 enableSort=True 369 for item in items: 370 if item.location is None: 371 enableSort=False 372 warning("Location not found for %s: %s", type(item).__name__.lower(), item.name) 373 374 if enableSort: 375 sortedItems = sorted(items, key=lambda item: item.location) 376 else: 377 sortedItems = items 378 return NameIndex(sortedItems) 379 380 groups = NameIndex(createMissing=createGroup, kind="group") 381 types = NameIndex(list(map(createType, spec.types)), 382 createMissing=createType, kind="type") 383 enums = NameIndex(list(map(createEnum, spec.enums)), 384 createMissing=Enum, kind="enum") 385 commands = NameIndex(list(map(createCommand, spec.commands)), 386 createMissing=Command, kind="command") 387 versions = sorted(spec.versions) 388 389 # This is a mess because the registry contains alias chains whose 390 # midpoints might not be included in the interface even though 391 # endpoints are. 392 for command in commands: 393 alias = command.alias 394 aliasCommand = None 395 while alias is not None: 396 aliasCommand = registry.commands[alias] 397 alias = extractAlias(aliasCommand) 398 command.alias = None 399 if aliasCommand is not None: 400 name = aliasCommand.findtext('proto/name') 401 if name in commands: 402 command.alias = commands[name] 403 404 sortedTypes=sortedIndex(types) 405 sortedEnums=sortedIndex(enums) 406 sortedGroups=sortedIndex(groups) 407 sortedCommands=sortedIndex(commands) 408 409 ifc=Interface( 410 types=sortedTypes, 411 enums=sortedEnums, 412 groups=sortedGroups, 413 commands=sortedCommands, 414 versions=versions) 415 416 return ifc 417 418 419def spec(registry, api, version=None, profile=None, extensionNames=[], protects=[], force=False): 420 available = set(protects) 421 spec = InterfaceSpec() 422 423 if version is None or version is False: 424 def check(v): return False 425 elif version is True: 426 def check(v): return True 427 else: 428 def check(v): return v <= version 429 430# BOZA TODO: I suppose adding primitive types will remove a lot of warnings 431# spec.addComponents(registry.types, api, profile) 432 433 for eFeature in registry.getFeatures(api, check): 434 spec.addFeature(eFeature, api, profile, force) 435 436 for extName in extensionNames: 437 eExtension = registry.extensions[extName] 438 protect = eExtension.get('protect') 439 if protect is not None and protect not in available: 440 warnElem(eExtension, "Unavailable dependency %s", protect) 441 if not force: 442 continue 443 spec.addExtension(eExtension, api, profile, force) 444 available.add(extName) 445 446 return spec 447 448def interface(registry, api, **kwargs): 449 s = spec(registry, api, **kwargs) 450 return createInterface(registry, s, api) 451 452def parse(path): 453 return Registry(etree.parse(path)) 454