1# Copyright (c) 2018 The Android Open Source Project
2# Copyright (c) 2018 Google Inc.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from .common.codegen import CodeGen
17from .common.vulkantypes import \
18        VulkanCompoundType, VulkanAPI, makeVulkanTypeSimple, vulkanTypeNeedsTransform, vulkanTypeGetNeededTransformTypes, VulkanTypeIterator, iterateVulkanType, vulkanTypeforEachSubType, TRANSFORMED_TYPES
19
20from .wrapperdefs import VulkanWrapperGenerator
21from .wrapperdefs import STRUCT_EXTENSION_PARAM, STRUCT_EXTENSION_PARAM_FOR_WRITE
22
23# This is different from others; it operations solely in terms of deepcopy and handlemap
24class VulkanUnbox(VulkanWrapperGenerator):
25    def __init__(self, module, typeInfo):
26        VulkanWrapperGenerator.__init__(self, module, typeInfo)
27
28        self.codegen = CodeGen()
29
30        self.unboxPrefix = "unbox"
31        self.toUnboxVar = "toUnbox"
32        self.poolParam = \
33            makeVulkanTypeSimple(False, "BumpPool", 1, "pool")
34
35        self.knownStructs = {}
36        self.needsTransform = set([])
37
38    def onBegin(self,):
39        VulkanWrapperGenerator.onBegin(self)
40
41    def onGenType(self, typeXml, name, alias):
42        VulkanWrapperGenerator.onGenType(self, typeXml, name, alias)
43
44        if name in self.knownStructs:
45            return
46
47        category = self.typeInfo.categoryOf(name)
48
49        if category in ["struct", "union"] and alias:
50            self.module.appendHeader(
51                self.codegen.makeFuncAlias(self.unboxPrefix + "_" + name,
52                                           self.unboxPrefix + "_" + alias))
53
54        if category in ["struct", "union"] and not alias:
55            structInfo = self.typeInfo.structs[name]
56            self.knownStructs[name] = structInfo
57
58            api = VulkanAPI( \
59                self.unboxPrefix + "_" + name,
60                makeVulkanTypeSimple(False, name, 1),
61                [self.poolParam] + \
62                [makeVulkanTypeSimple( \
63                    True, name, 1, self.toUnboxVar)])
64
65            def funcDefGenerator(cgen):
66                cgen.stmt("BoxedHandleUnwrapMapping unboxMapping")
67                cgen.stmt("%s* res = (%s*)pool->alloc(sizeof(const %s))" % (name, name, name))
68                cgen.stmt("deepcopy_%s(pool, %s, %s)" % (name, self.toUnboxVar, "res"))
69                cgen.stmt("handlemap_%s(%s, %s)" % (name, "&unboxMapping", "res"))
70                cgen.stmt("return res")
71
72            self.module.appendHeader(
73                self.codegen.makeFuncDecl(api))
74            self.module.appendImpl(
75                self.codegen.makeFuncImpl(api, funcDefGenerator))
76
77    def onGenCmd(self, cmdinfo, name, alias):
78        VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias)
79
80    def onEnd(self,):
81        VulkanWrapperGenerator.onEnd(self)
82