anv/wsi/wl: stop using device in more places
[mesa.git] / src / intel / vulkan / anv_wsi.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_wsi.h"
25 #include "vk_format_info.h"
26 VkResult
27 anv_init_wsi(struct anv_physical_device *physical_device)
28 {
29 VkResult result;
30
31 memset(physical_device->wsi_device.wsi, 0, sizeof(physical_device->wsi_device.wsi));
32
33 #ifdef VK_USE_PLATFORM_XCB_KHR
34 result = anv_x11_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
35 if (result != VK_SUCCESS)
36 return result;
37 #endif
38
39 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
40 result = anv_wl_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc,
41 anv_physical_device_to_handle(physical_device));
42 if (result != VK_SUCCESS) {
43 #ifdef VK_USE_PLATFORM_XCB_KHR
44 anv_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
45 #endif
46 return result;
47 }
48 #endif
49
50 return VK_SUCCESS;
51 }
52
53 void
54 anv_finish_wsi(struct anv_physical_device *physical_device)
55 {
56 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
57 anv_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
58 #endif
59 #ifdef VK_USE_PLATFORM_XCB_KHR
60 anv_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
61 #endif
62 }
63
64 void anv_DestroySurfaceKHR(
65 VkInstance _instance,
66 VkSurfaceKHR _surface,
67 const VkAllocationCallbacks* pAllocator)
68 {
69 ANV_FROM_HANDLE(anv_instance, instance, _instance);
70 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
71
72 vk_free2(&instance->alloc, pAllocator, surface);
73 }
74
75 VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
76 VkPhysicalDevice physicalDevice,
77 uint32_t queueFamilyIndex,
78 VkSurfaceKHR _surface,
79 VkBool32* pSupported)
80 {
81 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
82 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
83 struct anv_wsi_interface *iface = device->wsi_device.wsi[surface->platform];
84
85 return iface->get_support(surface, &device->wsi_device,
86 &device->instance->alloc,
87 queueFamilyIndex, pSupported);
88 }
89
90 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
91 VkPhysicalDevice physicalDevice,
92 VkSurfaceKHR _surface,
93 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
94 {
95 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
96 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
97 struct anv_wsi_interface *iface = device->wsi_device.wsi[surface->platform];
98
99 return iface->get_capabilities(surface, pSurfaceCapabilities);
100 }
101
102 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
103 VkPhysicalDevice physicalDevice,
104 VkSurfaceKHR _surface,
105 uint32_t* pSurfaceFormatCount,
106 VkSurfaceFormatKHR* pSurfaceFormats)
107 {
108 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
109 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
110 struct anv_wsi_interface *iface = device->wsi_device.wsi[surface->platform];
111
112 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
113 pSurfaceFormats);
114 }
115
116 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
117 VkPhysicalDevice physicalDevice,
118 VkSurfaceKHR _surface,
119 uint32_t* pPresentModeCount,
120 VkPresentModeKHR* pPresentModes)
121 {
122 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
123 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
124 struct anv_wsi_interface *iface = device->wsi_device.wsi[surface->platform];
125
126 return iface->get_present_modes(surface, pPresentModeCount,
127 pPresentModes);
128 }
129
130
131 static VkResult
132 x11_anv_wsi_image_create(VkDevice device_h,
133 const VkSwapchainCreateInfoKHR *pCreateInfo,
134 const VkAllocationCallbacks* pAllocator,
135 VkImage *image_p,
136 VkDeviceMemory *memory_p,
137 uint32_t *size,
138 uint32_t *offset,
139 uint32_t *row_pitch, int *fd_p)
140 {
141 struct anv_device *device = anv_device_from_handle(device_h);
142 VkImage image_h;
143 struct anv_image *image;
144
145 VkResult result;
146 result = anv_image_create(anv_device_to_handle(device),
147 &(struct anv_image_create_info) {
148 .isl_tiling_flags = ISL_TILING_X_BIT,
149 .stride = 0,
150 .vk_info =
151 &(VkImageCreateInfo) {
152 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
153 .imageType = VK_IMAGE_TYPE_2D,
154 .format = pCreateInfo->imageFormat,
155 .extent = {
156 .width = pCreateInfo->imageExtent.width,
157 .height = pCreateInfo->imageExtent.height,
158 .depth = 1
159 },
160 .mipLevels = 1,
161 .arrayLayers = 1,
162 .samples = 1,
163 /* FIXME: Need a way to use X tiling to allow scanout */
164 .tiling = VK_IMAGE_TILING_OPTIMAL,
165 .usage = (pCreateInfo->imageUsage |
166 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
167 .flags = 0,
168 }},
169 NULL,
170 &image_h);
171 if (result != VK_SUCCESS)
172 return result;
173
174 image = anv_image_from_handle(image_h);
175 assert(vk_format_is_color(image->vk_format));
176
177 VkDeviceMemory memory_h;
178 struct anv_device_memory *memory;
179 result = anv_AllocateMemory(anv_device_to_handle(device),
180 &(VkMemoryAllocateInfo) {
181 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
182 .allocationSize = image->size,
183 .memoryTypeIndex = 0,
184 },
185 NULL /* XXX: pAllocator */,
186 &memory_h);
187 if (result != VK_SUCCESS)
188 goto fail_create_image;
189
190 memory = anv_device_memory_from_handle(memory_h);
191 memory->bo.is_winsys_bo = true;
192
193 anv_BindImageMemory(VK_NULL_HANDLE, image_h, memory_h, 0);
194
195 struct anv_surface *surface = &image->color_surface;
196 assert(surface->isl.tiling == ISL_TILING_X);
197
198 *row_pitch = surface->isl.row_pitch;
199 int ret = anv_gem_set_tiling(device, memory->bo.gem_handle,
200 surface->isl.row_pitch, I915_TILING_X);
201 if (ret) {
202 /* FINISHME: Choose a better error. */
203 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
204 "set_tiling failed: %m");
205 goto fail_alloc_memory;
206 }
207
208 int fd = anv_gem_handle_to_fd(device, memory->bo.gem_handle);
209 if (fd == -1) {
210 /* FINISHME: Choose a better error. */
211 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
212 "handle_to_fd failed: %m");
213 goto fail_alloc_memory;
214 }
215
216 *image_p = image_h;
217 *memory_p = memory_h;
218 *fd_p = fd;
219 *size = image->size;
220 *offset = image->offset;
221 return VK_SUCCESS;
222 fail_alloc_memory:
223 anv_FreeMemory(device_h, memory_h, pAllocator);
224
225 fail_create_image:
226 anv_DestroyImage(device_h, image_h, pAllocator);
227 return result;
228 }
229
230 static void
231 x11_anv_wsi_image_free(VkDevice device,
232 const VkAllocationCallbacks* pAllocator,
233 VkImage image_h,
234 VkDeviceMemory memory_h)
235 {
236 anv_DestroyImage(device, image_h, pAllocator);
237
238 anv_FreeMemory(device, memory_h, pAllocator);
239 }
240
241 static const struct anv_wsi_image_fns anv_wsi_image_fns = {
242 .create_wsi_image = x11_anv_wsi_image_create,
243 .free_wsi_image = x11_anv_wsi_image_free,
244 };
245
246 VkResult anv_CreateSwapchainKHR(
247 VkDevice _device,
248 const VkSwapchainCreateInfoKHR* pCreateInfo,
249 const VkAllocationCallbacks* pAllocator,
250 VkSwapchainKHR* pSwapchain)
251 {
252 ANV_FROM_HANDLE(anv_device, device, _device);
253 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, pCreateInfo->surface);
254 struct anv_wsi_interface *iface =
255 device->instance->physicalDevice.wsi_device.wsi[surface->platform];
256 struct anv_swapchain *swapchain;
257 const VkAllocationCallbacks *alloc;
258
259 if (pAllocator)
260 alloc = pAllocator;
261 else
262 alloc = &device->alloc;
263 VkResult result = iface->create_swapchain(surface, _device,
264 &device->instance->physicalDevice.wsi_device,
265 pCreateInfo,
266 alloc, &anv_wsi_image_fns,
267 &swapchain);
268 if (result != VK_SUCCESS)
269 return result;
270
271 swapchain->alloc = *alloc;
272
273 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
274 swapchain->fences[i] = VK_NULL_HANDLE;
275
276 *pSwapchain = anv_swapchain_to_handle(swapchain);
277
278 return VK_SUCCESS;
279 }
280
281 void anv_DestroySwapchainKHR(
282 VkDevice _device,
283 VkSwapchainKHR _swapchain,
284 const VkAllocationCallbacks* pAllocator)
285 {
286 ANV_FROM_HANDLE(anv_device, device, _device);
287 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
288 const VkAllocationCallbacks *alloc;
289
290 if (pAllocator)
291 alloc = pAllocator;
292 else
293 alloc = &device->alloc;
294 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
295 if (swapchain->fences[i] != VK_NULL_HANDLE)
296 anv_DestroyFence(_device, swapchain->fences[i], pAllocator);
297 }
298
299 swapchain->destroy(swapchain, alloc);
300 }
301
302 VkResult anv_GetSwapchainImagesKHR(
303 VkDevice device,
304 VkSwapchainKHR _swapchain,
305 uint32_t* pSwapchainImageCount,
306 VkImage* pSwapchainImages)
307 {
308 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
309
310 return swapchain->get_images(swapchain, pSwapchainImageCount,
311 pSwapchainImages);
312 }
313
314 VkResult anv_AcquireNextImageKHR(
315 VkDevice device,
316 VkSwapchainKHR _swapchain,
317 uint64_t timeout,
318 VkSemaphore semaphore,
319 VkFence fence,
320 uint32_t* pImageIndex)
321 {
322 ANV_FROM_HANDLE(anv_swapchain, swapchain, _swapchain);
323
324 return swapchain->acquire_next_image(swapchain, timeout, semaphore,
325 pImageIndex);
326 }
327
328 VkResult anv_QueuePresentKHR(
329 VkQueue _queue,
330 const VkPresentInfoKHR* pPresentInfo)
331 {
332 ANV_FROM_HANDLE(anv_queue, queue, _queue);
333 VkResult result;
334
335 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
336 ANV_FROM_HANDLE(anv_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
337
338 assert(anv_device_from_handle(swapchain->device) == queue->device);
339
340 if (swapchain->fences[0] == VK_NULL_HANDLE) {
341 result = anv_CreateFence(anv_device_to_handle(queue->device),
342 &(VkFenceCreateInfo) {
343 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
344 .flags = 0,
345 }, &swapchain->alloc, &swapchain->fences[0]);
346 if (result != VK_SUCCESS)
347 return result;
348 } else {
349 anv_ResetFences(anv_device_to_handle(queue->device),
350 1, &swapchain->fences[0]);
351 }
352
353 anv_QueueSubmit(_queue, 0, NULL, swapchain->fences[0]);
354
355 result = swapchain->queue_present(swapchain,
356 pPresentInfo->pImageIndices[i]);
357 /* TODO: What if one of them returns OUT_OF_DATE? */
358 if (result != VK_SUCCESS)
359 return result;
360
361 VkFence last = swapchain->fences[2];
362 swapchain->fences[2] = swapchain->fences[1];
363 swapchain->fences[1] = swapchain->fences[0];
364 swapchain->fences[0] = last;
365
366 if (last != VK_NULL_HANDLE) {
367 anv_WaitForFences(anv_device_to_handle(queue->device),
368 1, &last, true, 1);
369 }
370 }
371
372 return VK_SUCCESS;
373 }