• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // VulkanBackend.cpp: contains the definition of symbols exported by VulkanBackend.h so that they
16 // can be compiled twice: once export (shared library), once not exported (static library)
17 
18 // Include vulkan_platform.h before VulkanBackend.h includes vulkan.h so that we use our version
19 // of the non-dispatchable handles.
20 #include "common/vulkan_platform.h"
21 
22 #include "dawn_native/VulkanBackend.h"
23 
24 #include "common/SwapChainUtils.h"
25 #include "dawn_native/vulkan/DeviceVk.h"
26 #include "dawn_native/vulkan/NativeSwapChainImplVk.h"
27 #include "dawn_native/vulkan/TextureVk.h"
28 
29 namespace dawn_native { namespace vulkan {
30 
GetInstance(WGPUDevice device)31     VkInstance GetInstance(WGPUDevice device) {
32         Device* backendDevice = ToBackend(FromAPI(device));
33         return backendDevice->GetVkInstance();
34     }
35 
GetInstanceProcAddr(WGPUDevice device,const char * pName)36     DAWN_NATIVE_EXPORT PFN_vkVoidFunction GetInstanceProcAddr(WGPUDevice device,
37                                                               const char* pName) {
38         Device* backendDevice = ToBackend(FromAPI(device));
39         return (*backendDevice->fn.GetInstanceProcAddr)(backendDevice->GetVkInstance(), pName);
40     }
41 
42     // Explicitly export this function because it uses the "native" type for surfaces while the
43     // header as seen in this file uses the wrapped type.
44     DAWN_NATIVE_EXPORT DawnSwapChainImplementation
CreateNativeSwapChainImpl(WGPUDevice device,::VkSurfaceKHR surfaceNative)45     CreateNativeSwapChainImpl(WGPUDevice device, ::VkSurfaceKHR surfaceNative) {
46         Device* backendDevice = ToBackend(FromAPI(device));
47         VkSurfaceKHR surface = VkSurfaceKHR::CreateFromHandle(surfaceNative);
48 
49         DawnSwapChainImplementation impl;
50         impl = CreateSwapChainImplementation(new NativeSwapChainImpl(backendDevice, surface));
51         impl.textureUsage = WGPUTextureUsage_Present;
52 
53         return impl;
54     }
55 
GetNativeSwapChainPreferredFormat(const DawnSwapChainImplementation * swapChain)56     WGPUTextureFormat GetNativeSwapChainPreferredFormat(
57         const DawnSwapChainImplementation* swapChain) {
58         NativeSwapChainImpl* impl = reinterpret_cast<NativeSwapChainImpl*>(swapChain->userData);
59         return static_cast<WGPUTextureFormat>(impl->GetPreferredFormat());
60     }
61 
AdapterDiscoveryOptions()62     AdapterDiscoveryOptions::AdapterDiscoveryOptions()
63         : AdapterDiscoveryOptionsBase(WGPUBackendType_Vulkan) {
64     }
65 
66 #if defined(DAWN_PLATFORM_LINUX)
ExternalImageDescriptorOpaqueFD()67     ExternalImageDescriptorOpaqueFD::ExternalImageDescriptorOpaqueFD()
68         : ExternalImageDescriptorFD(ExternalImageType::OpaqueFD) {
69     }
70 
ExternalImageDescriptorDmaBuf()71     ExternalImageDescriptorDmaBuf::ExternalImageDescriptorDmaBuf()
72         : ExternalImageDescriptorFD(ExternalImageType::DmaBuf) {
73     }
74 
ExternalImageExportInfoOpaqueFD()75     ExternalImageExportInfoOpaqueFD::ExternalImageExportInfoOpaqueFD()
76         : ExternalImageExportInfoFD(ExternalImageType::OpaqueFD) {
77     }
78 
ExternalImageExportInfoDmaBuf()79     ExternalImageExportInfoDmaBuf::ExternalImageExportInfoDmaBuf()
80         : ExternalImageExportInfoFD(ExternalImageType::DmaBuf) {
81     }
82 #endif  // DAWN_PLATFORM_LINUX
83 
WrapVulkanImage(WGPUDevice device,const ExternalImageDescriptorVk * descriptor)84     WGPUTexture WrapVulkanImage(WGPUDevice device, const ExternalImageDescriptorVk* descriptor) {
85 #if defined(DAWN_PLATFORM_LINUX)
86         switch (descriptor->type) {
87             case ExternalImageType::OpaqueFD:
88             case ExternalImageType::DmaBuf: {
89                 Device* backendDevice = ToBackend(FromAPI(device));
90                 const ExternalImageDescriptorFD* fdDescriptor =
91                     static_cast<const ExternalImageDescriptorFD*>(descriptor);
92 
93                 return ToAPI(backendDevice->CreateTextureWrappingVulkanImage(
94                     fdDescriptor, fdDescriptor->memoryFD, fdDescriptor->waitFDs));
95             }
96             default:
97                 return nullptr;
98         }
99 #else
100         return nullptr;
101 #endif  // DAWN_PLATFORM_LINUX
102     }
103 
ExportVulkanImage(WGPUTexture texture,VkImageLayout desiredLayout,ExternalImageExportInfoVk * info)104     bool ExportVulkanImage(WGPUTexture texture,
105                            VkImageLayout desiredLayout,
106                            ExternalImageExportInfoVk* info) {
107         if (texture == nullptr) {
108             return false;
109         }
110 #if defined(DAWN_PLATFORM_LINUX)
111         switch (info->type) {
112             case ExternalImageType::OpaqueFD:
113             case ExternalImageType::DmaBuf: {
114                 Texture* backendTexture = ToBackend(FromAPI(texture));
115                 Device* device = ToBackend(backendTexture->GetDevice());
116                 ExternalImageExportInfoFD* fdInfo = static_cast<ExternalImageExportInfoFD*>(info);
117 
118                 return device->SignalAndExportExternalTexture(backendTexture, desiredLayout, fdInfo,
119                                                               &fdInfo->semaphoreHandles);
120             }
121             default:
122                 return false;
123         }
124 #else
125         return false;
126 #endif  // DAWN_PLATFORM_LINUX
127     }
128 
129 }}  // namespace dawn_native::vulkan
130