1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright 2015 The Android Open Source Project
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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.
21 #-------------------------------------------------------------------------
23 from itertools import chain
24 from khr_util.format import indentLines
26 def isValueDefined (definitions, value):
27 return value in definitions
29 def allValuesUndefined (definitions, values):
31 if isValueDefined(definitions, value):
35 def anyValueDefined (definitions, values):
36 return not allValuesUndefined(definitions, values)
38 def makeDefSet (iface):
39 return set(enum.name for enum in iface.enums) | \
40 set(enum.alias for enum in iface.enums if enum.alias != None)
42 def genStrUtilProtos (iface, enumGroups, bitfieldGroups):
43 definitions = makeDefSet(iface)
46 for groupName, values in enumGroups:
47 if anyValueDefined(definitions, values):
48 yield "const char*\tget%sName\t(int value);" % groupName
50 print("Warning: Empty value set for %s, skipping" % groupName)
52 def genBitfieldProtos ():
53 for groupName, values in bitfieldGroups:
54 if anyValueDefined(definitions, values):
55 yield "tcu::Format::Bitfield<16>\tget%sStr\t(int value);" % groupName
57 print("Warning: Empty value set for %s, skipping" % groupName)
60 for groupName, values in enumGroups:
61 if anyValueDefined(definitions, values):
62 yield "inline tcu::Format::Enum<int, 2>\tget%(name)sStr\t(int value)\t{ return tcu::Format::Enum<int, 2>(get%(name)sName, value); }" % {"name": groupName}
64 # booleans can be stored in enums or in byte-sized arrays. For clarity add special case
65 if anyValueDefined(definitions, ["GL_TRUE", "GL_FALSE"]):
66 yield "inline tcu::Format::Enum<int, 1>\tgetBooleanStr\t(deUint8 value)\t{ return tcu::Format::Enum<int, 1>(getBooleanName, (int)value); }"
68 return chain(genNameProtos(), genBitfieldProtos(), genStrImpl())
70 def genEnumStrImpl (groupName, values, definitions):
71 if allValuesUndefined(definitions, values):
75 yield "const char* get%sName (int value)" % groupName
77 yield "\tswitch (value)"
82 if isValueDefined(definitions, value):
83 yield "case %s:\treturn \"%s\";" % (value, value)
85 print("Warning: %s not defined, skipping" % value)
86 yield "default:\treturn DE_NULL;"
88 for caseLine in indentLines(genCases()):
89 yield "\t\t" + caseLine
94 def genBitfieldStrImpl (groupName, values, definitions):
95 if allValuesUndefined(definitions, values):
99 yield "tcu::Format::Bitfield<16> get%sStr (int value)" % groupName
101 yield "\tstatic const tcu::Format::BitDesc s_desc[] ="
106 if isValueDefined(definitions, value):
107 yield "tcu::Format::BitDesc(%s,\t\"%s\")," % (value, value)
109 print("Warning: %s not defined, skipping" % value)
111 for fieldLine in indentLines(genFields()):
112 yield "\t\t" + fieldLine
115 yield "\treturn tcu::Format::Bitfield<16>(value, &s_desc[0], &s_desc[DE_LENGTH_OF_ARRAY(s_desc)]);"
118 def genStrUtilImpls (iface, enumGroups, bitfieldGroups):
119 definitions = makeDefSet(iface)
121 for groupName, values in enumGroups:
122 for line in genEnumStrImpl(groupName, values, definitions):
124 for groupName, values in bitfieldGroups:
125 for line in genBitfieldStrImpl(groupName, values, definitions):
128 def genQueryEnumUtilImpl (groupName, groupQueries, allEnums):
130 yield "int get%sQueryNumArgsOut (int pname)" % groupName
132 yield "\tswitch(pname)"
136 for enumName, enumQueryNumOutputs in groupQueries:
137 if enumName in allEnums:
138 yield "case %s:\treturn %s;" % (enumName, enumQueryNumOutputs)
140 print("Warning: %s not defined, skipping" % enumName)
141 yield "default:\treturn 1;"
143 for caseLine in indentLines(genCases()):
144 yield "\t\t" + caseLine
149 def genQueryEnumUtilImpls (iface, queryGroups):
150 allEnums = makeDefSet(iface)
152 for groupName, groupQueries in queryGroups:
153 for line in genQueryEnumUtilImpl(groupName, groupQueries, allEnums):
156 def genSetEnumUtilImpl (groupName, groupQueries, allEnums):
158 yield "int get%sNumArgs (int pname)" % groupName
160 yield "\tswitch(pname)"
164 for enumName, enumQueryNumOutputs in groupQueries:
165 if enumName in allEnums:
166 yield "case %s:\treturn %s;" % (enumName, enumQueryNumOutputs)
168 print("Warning: %s not defined, skipping" % enumName)
169 yield "default:\treturn 1;"
171 for caseLine in indentLines(genCases()):
172 yield "\t\t" + caseLine
177 def genSetEnumUtilImpls (iface, queryGroups):
178 allEnums = makeDefSet(iface)
180 for groupName, groupQueries in queryGroups:
181 for line in genSetEnumUtilImpl(groupName, groupQueries, allEnums):
184 def addValuePrefix (groups, prefix):
185 return [(groupName, [prefix + value for value in values]) for groupName, values in groups]