1 // Copyright 2019 The ANGLE Project Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // MemoryObjectVk.cpp: Defines the class interface for MemoryObjectVk, implementing
6 // MemoryObjectImpl.
7
8 #include "libANGLE/renderer/vulkan/MemoryObjectVk.h"
9
10 #include "common/debug.h"
11 #include "libANGLE/Context.h"
12 #include "libANGLE/renderer/vulkan/ContextVk.h"
13 #include "libANGLE/renderer/vulkan/RendererVk.h"
14 #include "volk.h"
15 #include "vulkan/vulkan_fuchsia_ext.h"
16
17 #if !defined(ANGLE_PLATFORM_WINDOWS)
18 # include <unistd.h>
19 #else
20 # include <io.h>
21 #endif
22
23 #if defined(ANGLE_PLATFORM_FUCHSIA)
24 # include <zircon/status.h>
25 # include <zircon/syscalls.h>
26 #endif
27
28 namespace rx
29 {
30
31 namespace
32 {
33
34 #if defined(ANGLE_PLATFORM_WINDOWS)
close(int fd)35 int close(int fd)
36 {
37 return _close(fd);
38 }
39 #endif
40
CloseZirconVmo(zx_handle_t handle)41 void CloseZirconVmo(zx_handle_t handle)
42 {
43 #if defined(ANGLE_PLATFORM_FUCHSIA)
44 zx_handle_close(handle);
45 #else
46 UNREACHABLE();
47 #endif
48 }
49
DuplicateZirconVmo(ContextVk * contextVk,zx_handle_t handle,zx_handle_t * duplicate)50 angle::Result DuplicateZirconVmo(ContextVk *contextVk, zx_handle_t handle, zx_handle_t *duplicate)
51 {
52 #if defined(ANGLE_PLATFORM_FUCHSIA)
53 zx_status_t status = zx_handle_duplicate(handle, ZX_RIGHT_SAME_RIGHTS, duplicate);
54 ANGLE_VK_CHECK(contextVk, status == ZX_OK, VK_ERROR_INVALID_EXTERNAL_HANDLE);
55 return angle::Result::Continue;
56 #else
57 UNREACHABLE();
58 return angle::Result::Stop;
59 #endif
60 }
61
ToVulkanHandleType(gl::HandleType handleType)62 VkExternalMemoryHandleTypeFlagBits ToVulkanHandleType(gl::HandleType handleType)
63 {
64 switch (handleType)
65 {
66 case gl::HandleType::OpaqueFd:
67 return VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
68 case gl::HandleType::ZirconVmo:
69 return VK_EXTERNAL_MEMORY_HANDLE_TYPE_TEMP_ZIRCON_VMO_BIT_FUCHSIA;
70 default:
71 // Not a memory handle type.
72 UNREACHABLE();
73 return VK_EXTERNAL_MEMORY_HANDLE_TYPE_FLAG_BITS_MAX_ENUM;
74 }
75 }
76
77 } // namespace
78
MemoryObjectVk()79 MemoryObjectVk::MemoryObjectVk() {}
80
81 MemoryObjectVk::~MemoryObjectVk() = default;
82
onDestroy(const gl::Context * context)83 void MemoryObjectVk::onDestroy(const gl::Context *context)
84 {
85 if (mFd != kInvalidFd)
86 {
87 close(mFd);
88 mFd = kInvalidFd;
89 }
90
91 if (mZirconHandle != ZX_HANDLE_INVALID)
92 {
93 CloseZirconVmo(mZirconHandle);
94 mZirconHandle = ZX_HANDLE_INVALID;
95 }
96 }
97
setDedicatedMemory(const gl::Context * context,bool dedicatedMemory)98 angle::Result MemoryObjectVk::setDedicatedMemory(const gl::Context *context, bool dedicatedMemory)
99 {
100 UNIMPLEMENTED();
101 return angle::Result::Continue;
102 }
103
importFd(gl::Context * context,GLuint64 size,gl::HandleType handleType,GLint fd)104 angle::Result MemoryObjectVk::importFd(gl::Context *context,
105 GLuint64 size,
106 gl::HandleType handleType,
107 GLint fd)
108 {
109 ContextVk *contextVk = vk::GetImpl(context);
110
111 switch (handleType)
112 {
113 case gl::HandleType::OpaqueFd:
114 return importOpaqueFd(contextVk, size, fd);
115
116 default:
117 UNREACHABLE();
118 return angle::Result::Stop;
119 }
120 }
121
importZirconHandle(gl::Context * context,GLuint64 size,gl::HandleType handleType,GLuint handle)122 angle::Result MemoryObjectVk::importZirconHandle(gl::Context *context,
123 GLuint64 size,
124 gl::HandleType handleType,
125 GLuint handle)
126 {
127 ContextVk *contextVk = vk::GetImpl(context);
128
129 switch (handleType)
130 {
131 case gl::HandleType::ZirconVmo:
132 return importZirconVmo(contextVk, size, handle);
133
134 default:
135 UNREACHABLE();
136 return angle::Result::Stop;
137 }
138 }
139
importOpaqueFd(ContextVk * contextVk,GLuint64 size,GLint fd)140 angle::Result MemoryObjectVk::importOpaqueFd(ContextVk *contextVk, GLuint64 size, GLint fd)
141 {
142 ASSERT(mHandleType == gl::HandleType::InvalidEnum);
143 ASSERT(mFd == kInvalidFd);
144 ASSERT(fd != kInvalidFd);
145 mHandleType = gl::HandleType::OpaqueFd;
146 mFd = fd;
147 mSize = size;
148 return angle::Result::Continue;
149 }
150
importZirconVmo(ContextVk * contextVk,GLuint64 size,GLuint handle)151 angle::Result MemoryObjectVk::importZirconVmo(ContextVk *contextVk, GLuint64 size, GLuint handle)
152 {
153 ASSERT(mHandleType == gl::HandleType::InvalidEnum);
154 ASSERT(mZirconHandle == ZX_HANDLE_INVALID);
155 ASSERT(handle != ZX_HANDLE_INVALID);
156 mHandleType = gl::HandleType::ZirconVmo;
157 mZirconHandle = handle;
158 mSize = size;
159 return angle::Result::Continue;
160 }
161
createImage(ContextVk * contextVk,gl::TextureType type,size_t levels,GLenum internalFormat,const gl::Extents & size,GLuint64 offset,vk::ImageHelper * image)162 angle::Result MemoryObjectVk::createImage(ContextVk *contextVk,
163 gl::TextureType type,
164 size_t levels,
165 GLenum internalFormat,
166 const gl::Extents &size,
167 GLuint64 offset,
168 vk::ImageHelper *image)
169 {
170 RendererVk *renderer = contextVk->getRenderer();
171
172 const vk::Format &vkFormat = renderer->getFormat(internalFormat);
173
174 // All supported usage flags must be specified.
175 // See EXT_external_objects issue 13.
176 VkImageUsageFlags imageUsageFlags =
177 vk::GetMaximalImageUsageFlags(renderer, vkFormat.vkImageFormat);
178
179 VkExternalMemoryImageCreateInfo externalMemoryImageCreateInfo = {};
180 externalMemoryImageCreateInfo.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO;
181 externalMemoryImageCreateInfo.handleTypes = ToVulkanHandleType(mHandleType);
182
183 VkExtent3D vkExtents;
184 uint32_t layerCount;
185 gl_vk::GetExtentsAndLayerCount(type, size, &vkExtents, &layerCount);
186
187 ANGLE_TRY(image->initExternal(
188 contextVk, type, vkExtents, vkFormat, 1, imageUsageFlags,
189 vk::ImageLayout::ExternalPreInitialized, &externalMemoryImageCreateInfo, 0,
190 static_cast<uint32_t>(levels) - 1, static_cast<uint32_t>(levels), layerCount));
191
192 VkMemoryRequirements externalMemoryRequirements;
193 image->getImage().getMemoryRequirements(renderer->getDevice(), &externalMemoryRequirements);
194
195 void *importMemoryInfo = nullptr;
196 VkImportMemoryFdInfoKHR importMemoryFdInfo = {};
197 VkImportMemoryZirconHandleInfoFUCHSIA importMemoryZirconHandleInfo = {};
198 switch (mHandleType)
199 {
200 case gl::HandleType::OpaqueFd:
201 ASSERT(mFd != kInvalidFd);
202 importMemoryFdInfo.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR;
203 importMemoryFdInfo.handleType = ToVulkanHandleType(mHandleType);
204 importMemoryFdInfo.fd = dup(mFd);
205 importMemoryInfo = &importMemoryFdInfo;
206 break;
207 case gl::HandleType::ZirconVmo:
208 ASSERT(mZirconHandle != ZX_HANDLE_INVALID);
209 importMemoryZirconHandleInfo.sType =
210 VK_STRUCTURE_TYPE_TEMP_IMPORT_MEMORY_ZIRCON_HANDLE_INFO_FUCHSIA;
211 importMemoryZirconHandleInfo.handleType = ToVulkanHandleType(mHandleType);
212 ANGLE_TRY(
213 DuplicateZirconVmo(contextVk, mZirconHandle, &importMemoryZirconHandleInfo.handle));
214 importMemoryInfo = &importMemoryZirconHandleInfo;
215 break;
216 default:
217 UNREACHABLE();
218 }
219
220 // TODO(jmadill, spang): Memory sub-allocation. http://anglebug.com/2162
221 ASSERT(offset == 0);
222 ASSERT(externalMemoryRequirements.size == mSize);
223
224 VkMemoryPropertyFlags flags = 0;
225 ANGLE_TRY(image->initExternalMemory(contextVk, renderer->getMemoryProperties(),
226 externalMemoryRequirements, importMemoryInfo,
227 VK_QUEUE_FAMILY_EXTERNAL, flags));
228
229 return angle::Result::Continue;
230 }
231
232 } // namespace rx
233