summaryrefslogtreecommitdiff
path: root/codegen/vulkan/scripts/cereal/unbox.py
blob: f18fa2717e78005869871c2be5a957f8d74e4c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright (c) 2018 The Android Open Source Project
# Copyright (c) 2018 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from .common.codegen import CodeGen
from .common.vulkantypes import \
        VulkanCompoundType, VulkanAPI, makeVulkanTypeSimple, vulkanTypeNeedsTransform, vulkanTypeGetNeededTransformTypes, VulkanTypeIterator, iterateVulkanType, vulkanTypeforEachSubType, TRANSFORMED_TYPES

from .wrapperdefs import VulkanWrapperGenerator
from .wrapperdefs import STRUCT_EXTENSION_PARAM, STRUCT_EXTENSION_PARAM_FOR_WRITE

# This is different from others; it operations solely in terms of deepcopy and handlemap
class VulkanUnbox(VulkanWrapperGenerator):
    def __init__(self, module, typeInfo):
        VulkanWrapperGenerator.__init__(self, module, typeInfo)

        self.codegen = CodeGen()

        self.unboxPrefix = "unbox"
        self.toUnboxVar = "toUnbox"
        self.poolParam = \
            makeVulkanTypeSimple(False, "BumpPool", 1, "pool")

        self.knownStructs = {}
        self.needsTransform = set([])

    def onBegin(self,):
        VulkanWrapperGenerator.onBegin(self)

    def onGenType(self, typeXml, name, alias):
        VulkanWrapperGenerator.onGenType(self, typeXml, name, alias)

        if name in self.knownStructs:
            return

        category = self.typeInfo.categoryOf(name)

        if category in ["struct", "union"] and alias:
            self.module.appendHeader(
                self.codegen.makeFuncAlias(self.unboxPrefix + "_" + name,
                                           self.unboxPrefix + "_" + alias))

        if category in ["struct", "union"] and not alias:
            structInfo = self.typeInfo.structs[name]
            self.knownStructs[name] = structInfo

            api = VulkanAPI( \
                self.unboxPrefix + "_" + name,
                makeVulkanTypeSimple(False, name, 1),
                [self.poolParam] + \
                [makeVulkanTypeSimple( \
                    True, name, 1, self.toUnboxVar)])

            def funcDefGenerator(cgen):
                cgen.stmt("BoxedHandleUnwrapMapping unboxMapping")
                cgen.stmt("%s* res = (%s*)pool->alloc(sizeof(const %s))" % (name, name, name))
                cgen.stmt("deepcopy_%s(pool, %s, %s)" % (name, self.toUnboxVar, "res"))
                cgen.stmt("handlemap_%s(%s, %s)" % (name, "&unboxMapping", "res"))
                cgen.stmt("return res")

            self.module.appendHeader(
                self.codegen.makeFuncDecl(api))
            self.module.appendImpl(
                self.codegen.makeFuncImpl(api, funcDefGenerator))

    def onGenCmd(self, cmdinfo, name, alias):
        VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias)

    def onEnd(self,):
        VulkanWrapperGenerator.onEnd(self)