anv/wsi: remove all anv references from WSI common code
[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_private.h"
25 #include "wsi_common.h"
26 #include "vk_format_info.h"
27
28 static const struct wsi_callbacks wsi_cbs = {
29 .get_phys_device_format_properties = anv_GetPhysicalDeviceFormatProperties,
30 };
31
32 VkResult
33 anv_init_wsi(struct anv_physical_device *physical_device)
34 {
35 VkResult result;
36
37 memset(physical_device->wsi_device.wsi, 0, sizeof(physical_device->wsi_device.wsi));
38
39 #ifdef VK_USE_PLATFORM_XCB_KHR
40 result = wsi_x11_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
41 if (result != VK_SUCCESS)
42 return result;
43 #endif
44
45 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
46 result = wsi_wl_init_wsi(&physical_device->wsi_device, &physical_device->instance->alloc,
47 anv_physical_device_to_handle(physical_device),
48 &wsi_cbs);
49 if (result != VK_SUCCESS) {
50 #ifdef VK_USE_PLATFORM_XCB_KHR
51 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
52 #endif
53 return result;
54 }
55 #endif
56
57 return VK_SUCCESS;
58 }
59
60 void
61 anv_finish_wsi(struct anv_physical_device *physical_device)
62 {
63 #ifdef VK_USE_PLATFORM_WAYLAND_KHR
64 wsi_wl_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
65 #endif
66 #ifdef VK_USE_PLATFORM_XCB_KHR
67 wsi_x11_finish_wsi(&physical_device->wsi_device, &physical_device->instance->alloc);
68 #endif
69 }
70
71 void anv_DestroySurfaceKHR(
72 VkInstance _instance,
73 VkSurfaceKHR _surface,
74 const VkAllocationCallbacks* pAllocator)
75 {
76 ANV_FROM_HANDLE(anv_instance, instance, _instance);
77 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
78
79 vk_free2(&instance->alloc, pAllocator, surface);
80 }
81
82 VkResult anv_GetPhysicalDeviceSurfaceSupportKHR(
83 VkPhysicalDevice physicalDevice,
84 uint32_t queueFamilyIndex,
85 VkSurfaceKHR _surface,
86 VkBool32* pSupported)
87 {
88 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
89 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
90 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
91
92 return iface->get_support(surface, &device->wsi_device,
93 &device->instance->alloc,
94 queueFamilyIndex, pSupported);
95 }
96
97 VkResult anv_GetPhysicalDeviceSurfaceCapabilitiesKHR(
98 VkPhysicalDevice physicalDevice,
99 VkSurfaceKHR _surface,
100 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
101 {
102 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
103 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
104 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
105
106 return iface->get_capabilities(surface, pSurfaceCapabilities);
107 }
108
109 VkResult anv_GetPhysicalDeviceSurfaceFormatsKHR(
110 VkPhysicalDevice physicalDevice,
111 VkSurfaceKHR _surface,
112 uint32_t* pSurfaceFormatCount,
113 VkSurfaceFormatKHR* pSurfaceFormats)
114 {
115 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
116 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
117 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
118
119 return iface->get_formats(surface, &device->wsi_device, pSurfaceFormatCount,
120 pSurfaceFormats);
121 }
122
123 VkResult anv_GetPhysicalDeviceSurfacePresentModesKHR(
124 VkPhysicalDevice physicalDevice,
125 VkSurfaceKHR _surface,
126 uint32_t* pPresentModeCount,
127 VkPresentModeKHR* pPresentModes)
128 {
129 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
130 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, _surface);
131 struct wsi_interface *iface = device->wsi_device.wsi[surface->platform];
132
133 return iface->get_present_modes(surface, pPresentModeCount,
134 pPresentModes);
135 }
136
137
138 static VkResult
139 x11_anv_wsi_image_create(VkDevice device_h,
140 const VkSwapchainCreateInfoKHR *pCreateInfo,
141 const VkAllocationCallbacks* pAllocator,
142 VkImage *image_p,
143 VkDeviceMemory *memory_p,
144 uint32_t *size,
145 uint32_t *offset,
146 uint32_t *row_pitch, int *fd_p)
147 {
148 struct anv_device *device = anv_device_from_handle(device_h);
149 VkImage image_h;
150 struct anv_image *image;
151
152 VkResult result;
153 result = anv_image_create(anv_device_to_handle(device),
154 &(struct anv_image_create_info) {
155 .isl_tiling_flags = ISL_TILING_X_BIT,
156 .stride = 0,
157 .vk_info =
158 &(VkImageCreateInfo) {
159 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
160 .imageType = VK_IMAGE_TYPE_2D,
161 .format = pCreateInfo->imageFormat,
162 .extent = {
163 .width = pCreateInfo->imageExtent.width,
164 .height = pCreateInfo->imageExtent.height,
165 .depth = 1
166 },
167 .mipLevels = 1,
168 .arrayLayers = 1,
169 .samples = 1,
170 /* FIXME: Need a way to use X tiling to allow scanout */
171 .tiling = VK_IMAGE_TILING_OPTIMAL,
172 .usage = (pCreateInfo->imageUsage |
173 VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT),
174 .flags = 0,
175 }},
176 NULL,
177 &image_h);
178 if (result != VK_SUCCESS)
179 return result;
180
181 image = anv_image_from_handle(image_h);
182 assert(vk_format_is_color(image->vk_format));
183
184 VkDeviceMemory memory_h;
185 struct anv_device_memory *memory;
186 result = anv_AllocateMemory(anv_device_to_handle(device),
187 &(VkMemoryAllocateInfo) {
188 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
189 .allocationSize = image->size,
190 .memoryTypeIndex = 0,
191 },
192 NULL /* XXX: pAllocator */,
193 &memory_h);
194 if (result != VK_SUCCESS)
195 goto fail_create_image;
196
197 memory = anv_device_memory_from_handle(memory_h);
198 memory->bo.is_winsys_bo = true;
199
200 anv_BindImageMemory(VK_NULL_HANDLE, image_h, memory_h, 0);
201
202 struct anv_surface *surface = &image->color_surface;
203 assert(surface->isl.tiling == ISL_TILING_X);
204
205 *row_pitch = surface->isl.row_pitch;
206 int ret = anv_gem_set_tiling(device, memory->bo.gem_handle,
207 surface->isl.row_pitch, I915_TILING_X);
208 if (ret) {
209 /* FINISHME: Choose a better error. */
210 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
211 "set_tiling failed: %m");
212 goto fail_alloc_memory;
213 }
214
215 int fd = anv_gem_handle_to_fd(device, memory->bo.gem_handle);
216 if (fd == -1) {
217 /* FINISHME: Choose a better error. */
218 result = vk_errorf(VK_ERROR_OUT_OF_DEVICE_MEMORY,
219 "handle_to_fd failed: %m");
220 goto fail_alloc_memory;
221 }
222
223 *image_p = image_h;
224 *memory_p = memory_h;
225 *fd_p = fd;
226 *size = image->size;
227 *offset = image->offset;
228 return VK_SUCCESS;
229 fail_alloc_memory:
230 anv_FreeMemory(device_h, memory_h, pAllocator);
231
232 fail_create_image:
233 anv_DestroyImage(device_h, image_h, pAllocator);
234 return result;
235 }
236
237 static void
238 x11_anv_wsi_image_free(VkDevice device,
239 const VkAllocationCallbacks* pAllocator,
240 VkImage image_h,
241 VkDeviceMemory memory_h)
242 {
243 anv_DestroyImage(device, image_h, pAllocator);
244
245 anv_FreeMemory(device, memory_h, pAllocator);
246 }
247
248 static const struct wsi_image_fns anv_wsi_image_fns = {
249 .create_wsi_image = x11_anv_wsi_image_create,
250 .free_wsi_image = x11_anv_wsi_image_free,
251 };
252
253 VkResult anv_CreateSwapchainKHR(
254 VkDevice _device,
255 const VkSwapchainCreateInfoKHR* pCreateInfo,
256 const VkAllocationCallbacks* pAllocator,
257 VkSwapchainKHR* pSwapchain)
258 {
259 ANV_FROM_HANDLE(anv_device, device, _device);
260 ANV_FROM_HANDLE(_VkIcdSurfaceBase, surface, pCreateInfo->surface);
261 struct wsi_interface *iface =
262 device->instance->physicalDevice.wsi_device.wsi[surface->platform];
263 struct wsi_swapchain *swapchain;
264 const VkAllocationCallbacks *alloc;
265
266 if (pAllocator)
267 alloc = pAllocator;
268 else
269 alloc = &device->alloc;
270 VkResult result = iface->create_swapchain(surface, _device,
271 &device->instance->physicalDevice.wsi_device,
272 pCreateInfo,
273 alloc, &anv_wsi_image_fns,
274 &swapchain);
275 if (result != VK_SUCCESS)
276 return result;
277
278 swapchain->alloc = *alloc;
279
280 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++)
281 swapchain->fences[i] = VK_NULL_HANDLE;
282
283 *pSwapchain = wsi_swapchain_to_handle(swapchain);
284
285 return VK_SUCCESS;
286 }
287
288 void anv_DestroySwapchainKHR(
289 VkDevice _device,
290 VkSwapchainKHR _swapchain,
291 const VkAllocationCallbacks* pAllocator)
292 {
293 ANV_FROM_HANDLE(anv_device, device, _device);
294 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
295 const VkAllocationCallbacks *alloc;
296
297 if (pAllocator)
298 alloc = pAllocator;
299 else
300 alloc = &device->alloc;
301 for (unsigned i = 0; i < ARRAY_SIZE(swapchain->fences); i++) {
302 if (swapchain->fences[i] != VK_NULL_HANDLE)
303 anv_DestroyFence(_device, swapchain->fences[i], pAllocator);
304 }
305
306 swapchain->destroy(swapchain, alloc);
307 }
308
309 VkResult anv_GetSwapchainImagesKHR(
310 VkDevice device,
311 VkSwapchainKHR _swapchain,
312 uint32_t* pSwapchainImageCount,
313 VkImage* pSwapchainImages)
314 {
315 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
316
317 return swapchain->get_images(swapchain, pSwapchainImageCount,
318 pSwapchainImages);
319 }
320
321 VkResult anv_AcquireNextImageKHR(
322 VkDevice device,
323 VkSwapchainKHR _swapchain,
324 uint64_t timeout,
325 VkSemaphore semaphore,
326 VkFence fence,
327 uint32_t* pImageIndex)
328 {
329 ANV_FROM_HANDLE(wsi_swapchain, swapchain, _swapchain);
330
331 return swapchain->acquire_next_image(swapchain, timeout, semaphore,
332 pImageIndex);
333 }
334
335 VkResult anv_QueuePresentKHR(
336 VkQueue _queue,
337 const VkPresentInfoKHR* pPresentInfo)
338 {
339 ANV_FROM_HANDLE(anv_queue, queue, _queue);
340 VkResult result;
341
342 for (uint32_t i = 0; i < pPresentInfo->swapchainCount; i++) {
343 ANV_FROM_HANDLE(wsi_swapchain, swapchain, pPresentInfo->pSwapchains[i]);
344
345 assert(anv_device_from_handle(swapchain->device) == queue->device);
346
347 if (swapchain->fences[0] == VK_NULL_HANDLE) {
348 result = anv_CreateFence(anv_device_to_handle(queue->device),
349 &(VkFenceCreateInfo) {
350 .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
351 .flags = 0,
352 }, &swapchain->alloc, &swapchain->fences[0]);
353 if (result != VK_SUCCESS)
354 return result;
355 } else {
356 anv_ResetFences(anv_device_to_handle(queue->device),
357 1, &swapchain->fences[0]);
358 }
359
360 anv_QueueSubmit(_queue, 0, NULL, swapchain->fences[0]);
361
362 result = swapchain->queue_present(swapchain,
363 pPresentInfo->pImageIndices[i]);
364 /* TODO: What if one of them returns OUT_OF_DATE? */
365 if (result != VK_SUCCESS)
366 return result;
367
368 VkFence last = swapchain->fences[2];
369 swapchain->fences[2] = swapchain->fences[1];
370 swapchain->fences[1] = swapchain->fences[0];
371 swapchain->fences[0] = last;
372
373 if (last != VK_NULL_HANDLE) {
374 anv_WaitForFences(anv_device_to_handle(queue->device),
375 1, &last, true, 1);
376 }
377 }
378
379 return VK_SUCCESS;
380 }