xref: /aosp_15_r20/external/mesa3d/src/gfxstream/codegen/scripts/cereal/unbox.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1# Copyright 2018 Google LLC
2# SPDX-License-Identifier: MIT
3
4from .common.codegen import CodeGen
5from .common.vulkantypes import \
6        VulkanCompoundType, VulkanAPI, makeVulkanTypeSimple, vulkanTypeNeedsTransform, vulkanTypeGetNeededTransformTypes, VulkanTypeIterator, iterateVulkanType, vulkanTypeforEachSubType, TRANSFORMED_TYPES
7
8from .wrapperdefs import VulkanWrapperGenerator
9from .wrapperdefs import STRUCT_EXTENSION_PARAM, STRUCT_EXTENSION_PARAM_FOR_WRITE
10
11# This is different from others; it operations solely in terms of deepcopy and handlemap
12class VulkanUnbox(VulkanWrapperGenerator):
13    def __init__(self, module, typeInfo):
14        VulkanWrapperGenerator.__init__(self, module, typeInfo)
15
16        self.codegen = CodeGen()
17
18        self.unboxPrefix = "unbox"
19        self.toUnboxVar = "toUnbox"
20        self.poolParam = \
21            makeVulkanTypeSimple(False, "BumpPool", 1, "pool")
22
23        self.knownStructs = {}
24        self.needsTransform = set([])
25
26    def onBegin(self,):
27        VulkanWrapperGenerator.onBegin(self)
28
29    def onGenType(self, typeXml, name, alias):
30        VulkanWrapperGenerator.onGenType(self, typeXml, name, alias)
31
32        if name in self.knownStructs:
33            return
34
35        category = self.typeInfo.categoryOf(name)
36
37        if category in ["struct", "union"] and alias:
38            self.module.appendHeader(
39                self.codegen.makeFuncAlias(self.unboxPrefix + "_" + name,
40                                           self.unboxPrefix + "_" + alias))
41
42        if category in ["struct", "union"] and not alias:
43            structInfo = self.typeInfo.structs[name]
44            self.knownStructs[name] = structInfo
45
46            api = VulkanAPI( \
47                self.unboxPrefix + "_" + name,
48                makeVulkanTypeSimple(False, name, 1),
49                [self.poolParam] + \
50                [makeVulkanTypeSimple( \
51                    True, name, 1, self.toUnboxVar)])
52
53            def funcDefGenerator(cgen):
54                cgen.stmt("BoxedHandleUnwrapMapping unboxMapping")
55                cgen.stmt("%s* res = (%s*)pool->alloc(sizeof(const %s))" % (name, name, name))
56                cgen.stmt("deepcopy_%s(pool, %s, %s)" % (name, self.toUnboxVar, "res"))
57                cgen.stmt("handlemap_%s(%s, %s)" % (name, "&unboxMapping", "res"))
58                cgen.stmt("return res")
59
60            self.module.appendHeader(
61                self.codegen.makeFuncDecl(api))
62            self.module.appendImpl(
63                self.codegen.makeFuncImpl(api, funcDefGenerator))
64
65    def onGenCmd(self, cmdinfo, name, alias):
66        VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias)
67
68    def onEnd(self,):
69        VulkanWrapperGenerator.onEnd(self)
70