44fc91bf05c55d8a7c427a8c90c24dc649c815ff
[platform/core/uifw/vulkan-wsi-tizen.git] / wsi / wsi_factory.cpp
1
2 /*
3  * Copyright (c) 2019 Arm Limited.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to
9  * deal in the Software without restriction, including without limitation the
10  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11  * sell copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 /**
27  * @file
28  * @brief Implements factory methods for obtaining the specific surface and swapchain implementations.
29  */
30
31 #include "wsi_factory.hpp"
32 #include <cassert>
33 #include <cstdlib>
34 #include <cstdio>
35 #include <new>
36 #include <vulkan/vk_icd.h>
37
38 #include "headless/surface_properties.hpp"
39 #include "headless/swapchain.hpp"
40
41 namespace wsi
42 {
43
44 surface_properties *get_surface_properties(VkSurfaceKHR surface)
45 {
46    VkIcdSurfaceBase *surface_base = reinterpret_cast<VkIcdSurfaceBase *>(surface);
47
48    switch (surface_base->platform)
49    {
50    case VK_ICD_WSI_PLATFORM_HEADLESS:
51       return &headless::surface_properties::get_instance();
52    default:
53       return nullptr;
54    }
55 }
56
57 template <typename swapchain_type>
58 static swapchain_base *allocate_swapchain(layer::device_private_data &dev_data, const VkAllocationCallbacks *pAllocator)
59 {
60    if (!pAllocator)
61    {
62       return new swapchain_type(dev_data, pAllocator);
63    }
64    void *memory = pAllocator->pfnAllocation(pAllocator->pUserData, sizeof(swapchain_type), alignof(swapchain_type),
65                                             VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
66    return new (memory) swapchain_type(dev_data, pAllocator);
67 }
68
69 swapchain_base *allocate_surface_swapchain(VkSurfaceKHR surface, layer::device_private_data &dev_data,
70                                            const VkAllocationCallbacks *pAllocator)
71 {
72    VkIcdSurfaceBase *surface_base = reinterpret_cast<VkIcdSurfaceBase *>(surface);
73
74    switch (surface_base->platform)
75    {
76    case VK_ICD_WSI_PLATFORM_HEADLESS:
77       return allocate_swapchain<wsi::headless::swapchain>(dev_data, pAllocator);
78    default:
79       return nullptr;
80    }
81 }
82
83 void destroy_surface_swapchain(swapchain_base *swapchain, const VkAllocationCallbacks *pAllocator)
84 {
85    assert(swapchain);
86
87    if (!pAllocator)
88    {
89       delete swapchain;
90    }
91    else
92    {
93       swapchain->~swapchain_base();
94       pAllocator->pfnFree(pAllocator->pUserData, reinterpret_cast<void *>(swapchain));
95    }
96 }
97
98 } // namespace wsi