At this stage after adding few more stubs driver fails at
[mesa.git] / src / libre-soc / vulkan / libresoc_wsi.c
1 /*
2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26 #include "libresoc_private.h"
27 //#include "libresoc_meta.h"
28 #include "wsi_common.h"
29 #include "vk_util.h"
30 #include "util/macros.h"
31
32 static PFN_vkVoidFunction
33 libresoc_wsi_proc_addr(VkPhysicalDevice physicalDevice, const char *pName)
34 {
35 return libresoc_lookup_entrypoint(pName);
36 }
37
38 static void
39 libresoc_wsi_set_memory_ownership(VkDevice _device,
40 VkDeviceMemory _mem,
41 VkBool32 ownership)
42 {
43 //TODO: enable when required
44 //LIBRESOC_FROM_HANDLE(libresoc_device, device, _device);
45 //LIBRESOC_FROM_HANDLE(libresoc_device_memory, mem, _mem);
46
47 //if (ownership)
48 // libresoc_bo_list_add(device, mem->bo);
49 //else
50 // libresoc_bo_list_remove(device, mem->bo);
51 }
52
53 VkResult
54 libresoc_init_wsi(struct libresoc_physical_device *physical_device)
55 {
56 //TODO: stub
57 //return VK_SUCCESS;
58 VkResult result = wsi_device_init(&physical_device->wsi_device,
59 libresoc_physical_device_to_handle(physical_device),
60 libresoc_wsi_proc_addr,
61 &physical_device->instance->alloc,
62 physical_device->master_fd,
63 &physical_device->instance->dri_options,
64 true); //TODO: for now this is sw_device
65 if (result != VK_SUCCESS)
66 return result;
67
68 physical_device->wsi_device.set_memory_ownership = libresoc_wsi_set_memory_ownership;
69 return VK_SUCCESS;
70 }
71
72 void
73 libresoc_finish_wsi(struct libresoc_physical_device *physical_device)
74 {
75 wsi_device_finish(&physical_device->wsi_device,
76 &physical_device->instance->alloc);
77 }
78
79 void libresoc_DestroySurfaceKHR(
80 VkInstance _instance,
81 VkSurfaceKHR _surface,
82 const VkAllocationCallbacks* pAllocator)
83 {
84 LIBRESOC_FROM_HANDLE(libresoc_instance, instance, _instance);
85 ICD_FROM_HANDLE(VkIcdSurfaceBase, surface, _surface);
86
87 vk_free2(&instance->alloc, pAllocator, surface);
88 }
89
90 VkResult libresoc_GetPhysicalDeviceSurfaceSupportKHR(
91 VkPhysicalDevice physicalDevice,
92 uint32_t queueFamilyIndex,
93 VkSurfaceKHR surface,
94 VkBool32* pSupported)
95 {
96 LIBRESOC_FROM_HANDLE(libresoc_physical_device, device, physicalDevice);
97
98 return wsi_common_get_surface_support(&device->wsi_device,
99 queueFamilyIndex,
100 surface,
101 pSupported);
102 }
103
104 VkResult libresoc_GetPhysicalDeviceSurfaceCapabilitiesKHR(
105 VkPhysicalDevice physicalDevice,
106 VkSurfaceKHR surface,
107 VkSurfaceCapabilitiesKHR* pSurfaceCapabilities)
108 {
109 LIBRESOC_FROM_HANDLE(libresoc_physical_device, device, physicalDevice);
110
111 return wsi_common_get_surface_capabilities(&device->wsi_device,
112 surface,
113 pSurfaceCapabilities);
114 }
115
116 // VkResult libresoc_GetPhysicalDeviceSurfaceCapabilities2KHR(
117 // VkPhysicalDevice physicalDevice,
118 // const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
119 // VkSurfaceCapabilities2KHR* pSurfaceCapabilities)
120 // {
121 // LIBRESOC_FROM_HANDLE(libresoc_physical_device, device, physicalDevice);
122
123 // return wsi_common_get_surface_capabilities2(&device->wsi_device,
124 // pSurfaceInfo,
125 // pSurfaceCapabilities);
126 // }
127
128 // VkResult libresoc_GetPhysicalDeviceSurfaceCapabilities2EXT(
129 // VkPhysicalDevice physicalDevice,
130 // VkSurfaceKHR surface,
131 // VkSurfaceCapabilities2EXT* pSurfaceCapabilities)
132 // {
133 // LIBRESOC_FROM_HANDLE(libresoc_physical_device, device, physicalDevice);
134
135 // return wsi_common_get_surface_capabilities2ext(&device->wsi_device,
136 // surface,
137 // pSurfaceCapabilities);
138 // }
139
140 VkResult libresoc_GetPhysicalDeviceSurfaceFormatsKHR(
141 VkPhysicalDevice physicalDevice,
142 VkSurfaceKHR surface,
143 uint32_t* pSurfaceFormatCount,
144 VkSurfaceFormatKHR* pSurfaceFormats)
145 {
146 LIBRESOC_FROM_HANDLE(libresoc_physical_device, device, physicalDevice);
147
148 return wsi_common_get_surface_formats(&device->wsi_device,
149 surface,
150 pSurfaceFormatCount,
151 pSurfaceFormats);
152 }
153
154 // VkResult libresoc_GetPhysicalDeviceSurfaceFormats2KHR(
155 // VkPhysicalDevice physicalDevice,
156 // const VkPhysicalDeviceSurfaceInfo2KHR* pSurfaceInfo,
157 // uint32_t* pSurfaceFormatCount,
158 // VkSurfaceFormat2KHR* pSurfaceFormats)
159 // {
160 // LIBRESOC_FROM_HANDLE(libresoc_physical_device, device, physicalDevice);
161
162 // return wsi_common_get_surface_formats2(&device->wsi_device,
163 // pSurfaceInfo,
164 // pSurfaceFormatCount,
165 // pSurfaceFormats);
166 // }
167
168 VkResult libresoc_GetPhysicalDeviceSurfacePresentModesKHR(
169 VkPhysicalDevice physicalDevice,
170 VkSurfaceKHR surface,
171 uint32_t* pPresentModeCount,
172 VkPresentModeKHR* pPresentModes)
173 {
174 LIBRESOC_FROM_HANDLE(libresoc_physical_device, device, physicalDevice);
175
176 return wsi_common_get_surface_present_modes(&device->wsi_device,
177 surface,
178 pPresentModeCount,
179 pPresentModes);
180 }
181
182 VkResult libresoc_CreateSwapchainKHR(
183 VkDevice _device,
184 const VkSwapchainCreateInfoKHR* pCreateInfo,
185 const VkAllocationCallbacks* pAllocator,
186 VkSwapchainKHR* pSwapchain)
187 {
188 LIBRESOC_FROM_HANDLE(libresoc_device, device, _device);
189 const VkAllocationCallbacks *alloc;
190 if (pAllocator)
191 alloc = pAllocator;
192 else
193 alloc = &device->vk.alloc;
194
195 return wsi_common_create_swapchain(&device->physical_device->wsi_device,
196 libresoc_device_to_handle(device),
197 pCreateInfo,
198 alloc,
199 pSwapchain);
200 }
201
202 void libresoc_DestroySwapchainKHR(
203 VkDevice _device,
204 VkSwapchainKHR swapchain,
205 const VkAllocationCallbacks* pAllocator)
206 {
207 LIBRESOC_FROM_HANDLE(libresoc_device, device, _device);
208 const VkAllocationCallbacks *alloc;
209
210 if (pAllocator)
211 alloc = pAllocator;
212 else
213 alloc = &device->vk.alloc;
214
215 wsi_common_destroy_swapchain(_device, swapchain, alloc);
216 }
217
218 VkResult libresoc_GetSwapchainImagesKHR(
219 VkDevice device,
220 VkSwapchainKHR swapchain,
221 uint32_t* pSwapchainImageCount,
222 VkImage* pSwapchainImages)
223 {
224 return wsi_common_get_images(swapchain,
225 pSwapchainImageCount,
226 pSwapchainImages);
227 }
228
229 VkResult libresoc_AcquireNextImageKHR(
230 VkDevice device,
231 VkSwapchainKHR swapchain,
232 uint64_t timeout,
233 VkSemaphore semaphore,
234 VkFence fence,
235 uint32_t* pImageIndex)
236 {
237 VkAcquireNextImageInfoKHR acquire_info = {
238 .sType = VK_STRUCTURE_TYPE_ACQUIRE_NEXT_IMAGE_INFO_KHR,
239 .swapchain = swapchain,
240 .timeout = timeout,
241 .semaphore = semaphore,
242 .fence = fence,
243 .deviceMask = 0,
244 };
245
246 return libresoc_AcquireNextImage2KHR(device, &acquire_info, pImageIndex);
247 }
248
249 VkResult libresoc_AcquireNextImage2KHR(
250 VkDevice _device,
251 const VkAcquireNextImageInfoKHR* pAcquireInfo,
252 uint32_t* pImageIndex)
253 {
254 LIBRESOC_FROM_HANDLE(libresoc_device, device, _device);
255 struct libresoc_physical_device *pdevice = device->physical_device;
256 //LIBRESOC_FROM_HANDLE(libresoc_fence, fence, pAcquireInfo->fence);
257 //LIBRESOC_FROM_HANDLE(libresoc_semaphore, semaphore, pAcquireInfo->semaphore);
258
259 VkResult result = wsi_common_acquire_next_image2(&pdevice->wsi_device,
260 _device,
261 pAcquireInfo,
262 pImageIndex);
263 //TODO: enable if required
264 // if (result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR) {
265 // if (fence) {
266 // struct libresoc_fence_part *part =
267 // fence->temporary.kind != LIBRESOC_FENCE_NONE ?
268 // &fence->temporary : &fence->permanent;
269
270 // switch (part->kind) {
271 // case LIBRESOC_FENCE_NONE:
272 // break;
273 // case LIBRESOC_FENCE_WINSYS:
274 // device->ws->signal_fence(part->fence);
275 // break;
276 // case LIBRESOC_FENCE_SYNCOBJ:
277 // device->ws->signal_syncobj(device->ws, part->syncobj, 0);
278 // break;
279 // default:
280 // unreachable("Invalid WSI fence type");
281 // }
282 // }
283 // if (semaphore) {
284 // struct libresoc_semaphore_part *part =
285 // semaphore->temporary.kind != LIBRESOC_SEMAPHORE_NONE ?
286 // &semaphore->temporary : &semaphore->permanent;
287
288 // switch (part->kind) {
289 // case LIBRESOC_SEMAPHORE_NONE:
290 // case LIBRESOC_SEMAPHORE_WINSYS:
291 // /* Do not need to do anything. */
292 // break;
293 // case LIBRESOC_SEMAPHORE_TIMELINE:
294 // case LIBRESOC_SEMAPHORE_TIMELINE_SYNCOBJ:
295 // unreachable("WSI only allows binary semaphores.");
296 // case LIBRESOC_SEMAPHORE_SYNCOBJ:
297 // device->ws->signal_syncobj(device->ws, part->syncobj, 0);
298 // break;
299 // }
300 // }
301 // }
302 return result;
303 }
304
305 VkResult libresoc_QueuePresentKHR(
306 VkQueue _queue,
307 const VkPresentInfoKHR* pPresentInfo)
308 {
309 LIBRESOC_FROM_HANDLE(libresoc_queue, queue, _queue);
310 return wsi_common_queue_present(&queue->device->physical_device->wsi_device,
311 libresoc_device_to_handle(queue->device),
312 _queue,
313 queue->queue_family_index,
314 pPresentInfo);
315 }
316
317
318 VkResult libresoc_GetDeviceGroupPresentCapabilitiesKHR(
319 VkDevice device,
320 VkDeviceGroupPresentCapabilitiesKHR* pCapabilities)
321 {
322 memset(pCapabilities->presentMask, 0,
323 sizeof(pCapabilities->presentMask));
324 pCapabilities->presentMask[0] = 0x1;
325 pCapabilities->modes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
326
327 return VK_SUCCESS;
328 }
329
330 VkResult libresoc_GetDeviceGroupSurfacePresentModesKHR(
331 VkDevice device,
332 VkSurfaceKHR surface,
333 VkDeviceGroupPresentModeFlagsKHR* pModes)
334 {
335 *pModes = VK_DEVICE_GROUP_PRESENT_MODE_LOCAL_BIT_KHR;
336
337 return VK_SUCCESS;
338 }
339
340 VkResult libresoc_GetPhysicalDevicePresentRectanglesKHR(
341 VkPhysicalDevice physicalDevice,
342 VkSurfaceKHR surface,
343 uint32_t* pRectCount,
344 VkRect2D* pRects)
345 {
346 LIBRESOC_FROM_HANDLE(libresoc_physical_device, device, physicalDevice);
347
348 return wsi_common_get_present_rectangles(&device->wsi_device,
349 surface,
350 pRectCount, pRects);
351 }