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 "libANGLE/renderer/vulkan/vk_headers.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 mDedicatedMemory = dedicatedMemory;
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 // Initialize VkImage with initial layout of VK_IMAGE_LAYOUT_UNDEFINED.
188 //
189 // Binding a VkImage with an initial layout of VK_IMAGE_LAYOUT_UNDEFINED to
190 // external memory whose content has already been defined does not make the
191 // content undefined (see 11.7.1. External Resource Sharing).
192 //
193 // If the content is already defined, the ownership rules imply that the
194 // first operation on the texture must be a call to glWaitSemaphoreEXT that
195 // grants ownership of the image and informs us of the true layout.
196 //
197 // If the content is not already defined, the first operation may not be a
198 // glWaitSemaphore, but in this case undefined layout is appropriate.
199 ANGLE_TRY(image->initExternal(
200 contextVk, type, vkExtents, vkFormat, 1, imageUsageFlags, vk::kVkImageCreateFlagsNone,
201 vk::ImageLayout::Undefined, &externalMemoryImageCreateInfo, 0,
202 static_cast<uint32_t>(levels) - 1, static_cast<uint32_t>(levels), layerCount));
203
204 VkMemoryRequirements externalMemoryRequirements;
205 image->getImage().getMemoryRequirements(renderer->getDevice(), &externalMemoryRequirements);
206
207 void *importMemoryInfo = nullptr;
208 VkMemoryDedicatedAllocateInfo memoryDedicatedAllocateInfo = {};
209 if (mDedicatedMemory)
210 {
211 memoryDedicatedAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR;
212 memoryDedicatedAllocateInfo.image = image->getImage().getHandle();
213 importMemoryInfo = &memoryDedicatedAllocateInfo;
214 }
215
216 VkImportMemoryFdInfoKHR importMemoryFdInfo = {};
217 VkImportMemoryZirconHandleInfoFUCHSIA importMemoryZirconHandleInfo = {};
218 switch (mHandleType)
219 {
220 case gl::HandleType::OpaqueFd:
221 ASSERT(mFd != kInvalidFd);
222 importMemoryFdInfo.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR;
223 importMemoryFdInfo.pNext = importMemoryInfo;
224 importMemoryFdInfo.handleType = ToVulkanHandleType(mHandleType);
225 importMemoryFdInfo.fd = dup(mFd);
226 importMemoryInfo = &importMemoryFdInfo;
227 break;
228 case gl::HandleType::ZirconVmo:
229 ASSERT(mZirconHandle != ZX_HANDLE_INVALID);
230 importMemoryZirconHandleInfo.sType =
231 VK_STRUCTURE_TYPE_TEMP_IMPORT_MEMORY_ZIRCON_HANDLE_INFO_FUCHSIA;
232 importMemoryZirconHandleInfo.pNext = importMemoryInfo;
233 importMemoryZirconHandleInfo.handleType = ToVulkanHandleType(mHandleType);
234 ANGLE_TRY(
235 DuplicateZirconVmo(contextVk, mZirconHandle, &importMemoryZirconHandleInfo.handle));
236 importMemoryInfo = &importMemoryZirconHandleInfo;
237 break;
238 default:
239 UNREACHABLE();
240 }
241
242 // TODO(jmadill, spang): Memory sub-allocation. http://anglebug.com/2162
243 ASSERT(offset == 0);
244 ASSERT(externalMemoryRequirements.size == mSize);
245
246 VkMemoryPropertyFlags flags = 0;
247 ANGLE_TRY(image->initExternalMemory(contextVk, renderer->getMemoryProperties(),
248 externalMemoryRequirements, importMemoryInfo,
249 renderer->getQueueFamilyIndex(), flags));
250
251 return angle::Result::Continue;
252 }
253
254 } // namespace rx
255