swapchain_tdm: fixed an incorrect use of user allocator.
[platform/core/uifw/vulkan-wsi-tizen.git] / src / wsi / swapchain_tdm.c
1 /*
2  * Copyright © 2016 S-Core Corporation
3  * Copyright © 2016-2017 Samsung Electronics co., Ltd. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "wsi.h"
26 #include <pthread.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdio.h>
30
31 typedef struct vk_swapchain_tdm vk_swapchain_tdm_t;
32
33 struct vk_swapchain_tdm {
34         tdm_display                             *tdm_display;
35         tdm_output                              *tdm_output;
36         tdm_layer                               *tdm_layer;
37         const tdm_output_mode   *tdm_mode;
38         tdm_output_dpms                  tdm_dpms;
39
40         tbm_surface_queue_h              tbm_queue;
41
42         VkPresentModeKHR                 present_mode;
43         tbm_surface_h                   *buffers;
44         tbm_surface_h                    front_buffer;
45         pthread_mutex_t                  front_mutex;
46         pthread_mutex_t                  free_queue_mutex;
47         pthread_cond_t                   free_queue_cond;
48 };
49
50 static int swapchain_tdm_timeline_key;
51 static int swapchain_tdm_timestamp_key;
52
53 static void
54 swapchain_tdm_increase_timestamp(tbm_surface_h tbm_surface)
55 {
56         tbm_fd           timeline;
57         if (tbm_surface_internal_get_user_data(tbm_surface,
58                                                                                    (unsigned long)&swapchain_tdm_timeline_key,
59                                                                                    (void **)(&timeline))) {
60                 if (timeline != -1) {
61                         if (tbm_sync_timeline_inc(timeline, 1) == 0) {
62                                 char buf[1024];
63                                 strerror_r(errno, buf, sizeof(buf));
64                                 VK_ERROR("Failed to increase TBM sync timeline: %d(%s)", errno, buf);
65                         }
66                 }
67         }
68 }
69
70 static void
71 swapchain_tdm_output_commit_cb(tdm_output *output, unsigned int sequence,
72                                                            unsigned int tv_sec, unsigned int tv_usec,
73                                                            void *user_data)
74 {
75         vk_swapchain_t                  *chain = user_data;
76         vk_swapchain_tdm_t              *swapchain_tdm = chain->backend_data;
77
78         if (pthread_mutex_lock(&swapchain_tdm->front_mutex))
79                 VK_ERROR("pthread_mutex_lock front buffer failed\n");
80
81         if (swapchain_tdm->front_buffer) {
82                 swapchain_tdm_increase_timestamp(swapchain_tdm->front_buffer);
83                 swapchain_tdm->front_buffer = NULL;
84         }
85         pthread_mutex_unlock(&swapchain_tdm->front_mutex);
86 }
87
88 static VkResult
89 swapchain_tdm_queue_present_image(VkQueue                                        queue,
90                                                                   vk_swapchain_t                        *chain,
91                                                                   tbm_surface_h                          tbm_surface,
92                                                                   int                                            sync_fd)
93 {
94         tbm_surface_queue_error_e        tsq_err;
95         tdm_error                                        tdm_err;
96         vk_swapchain_tdm_t                      *swapchain_tdm = chain->backend_data;
97
98         if (sync_fd != -1) {
99                 if (tbm_sync_fence_wait(sync_fd, -1) != 1) {
100                         char buf[1024];
101                         strerror_r(errno, buf, sizeof(buf));
102                         VK_ERROR("Failed to wait sync. | error: %d(%s)", errno, buf);
103                 }
104                 close(sync_fd);
105         }
106
107         tsq_err = tbm_surface_queue_enqueue(swapchain_tdm->tbm_queue, tbm_surface);
108         VK_CHECK(tsq_err == TBM_SURFACE_QUEUE_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
109                          "tbm_surface_queue_enqueue failed.\n");
110
111         tsq_err = tbm_surface_queue_acquire(swapchain_tdm->tbm_queue, &tbm_surface);
112         VK_CHECK(tsq_err == TBM_SURFACE_QUEUE_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
113                          "tbm_surface_queue_acquire failed.\n");
114
115         tdm_err = tdm_layer_set_buffer(swapchain_tdm->tdm_layer, tbm_surface);
116         VK_CHECK(tdm_err == TDM_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
117                          "tdm_layer_set_buffer failed.\n");
118
119         tdm_err = tdm_output_commit(swapchain_tdm->tdm_output, 0,
120                                                                 swapchain_tdm_output_commit_cb, chain);
121         VK_CHECK(tdm_err == TDM_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
122                          "tdm_output_commit failed.\n");
123
124         tdm_err = tdm_display_handle_events(swapchain_tdm->tdm_display);
125         VK_CHECK(tdm_err == TDM_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
126                          "tdm_display_handle_events failed.\n");
127
128         if (pthread_mutex_lock(&swapchain_tdm->free_queue_mutex))
129                 VK_ERROR("pthread_mutex_lock free queue failed\n");
130
131         tsq_err = tbm_surface_queue_release(swapchain_tdm->tbm_queue, tbm_surface);
132         VK_CHECK(tsq_err == TBM_SURFACE_QUEUE_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
133                          "tbm_surface_queue_release failed.\n");
134
135         pthread_mutex_unlock(&swapchain_tdm->free_queue_mutex);
136         pthread_cond_signal(&swapchain_tdm->free_queue_cond);
137
138         if (pthread_mutex_lock(&swapchain_tdm->front_mutex))
139                 VK_ERROR("pthread_mutex_lock front buffer failed\n");
140         if (swapchain_tdm->front_buffer)
141                 swapchain_tdm_increase_timestamp(swapchain_tdm->front_buffer);
142         swapchain_tdm->front_buffer = tbm_surface;
143         pthread_mutex_unlock(&swapchain_tdm->front_mutex);
144
145         return VK_SUCCESS;
146 }
147
148 static void
149 swapchain_tdm_timeline_destroy_cb(void *user_data)
150 {
151         if (((tbm_fd)user_data) != -1)
152                 close((tbm_fd)user_data);
153 }
154
155 static tbm_fd
156 swapchain_tdm_get_sync_fence(tbm_surface_h tbm_surface)
157 {
158         tbm_fd           timeline;
159         uint32_t         timestamp;
160         tbm_fd           fence = -1;
161
162         if (tbm_surface_internal_get_user_data(tbm_surface,
163                                                                                    (unsigned long)&swapchain_tdm_timeline_key,
164                                                                                    (void **)(&timeline))) {
165                 char name[32];
166
167                 tbm_surface_internal_get_user_data(tbm_surface,
168                                                                                    (unsigned long)&swapchain_tdm_timestamp_key,
169                                                                                    (void **)(&timestamp));
170                 timestamp++;
171                 tbm_surface_internal_set_user_data(tbm_surface,
172                                                                                    (unsigned long)&swapchain_tdm_timestamp_key,
173                                                                                    (void *)timestamp);
174
175                 snprintf(name, 32, "%d",
176                                  tbm_bo_export(tbm_surface_internal_get_bo(tbm_surface, 0)));
177                 fence = tbm_sync_fence_create(timeline,
178                                                                           name,
179                                                                           timestamp);
180                 if (fence == -1) {
181                         char buf[1024];
182                         strerror_r(errno, buf, sizeof(buf));
183                         VK_ERROR("Failed to create TBM sync fence: %d(%s)", errno, buf);
184                 }
185         } else {
186                 /* make timeline and timestamp */
187                 timeline = tbm_sync_timeline_create();
188                 tbm_surface_internal_add_user_data(tbm_surface,
189                                                                                    (unsigned long)&swapchain_tdm_timeline_key,
190                                                                                    swapchain_tdm_timeline_destroy_cb);
191                 tbm_surface_internal_set_user_data(tbm_surface,
192                                                                                    (unsigned long)&swapchain_tdm_timeline_key,
193                                                                                    (void *)timeline);
194                 if (timeline == -1) {
195                         char buf[1024];
196                         strerror_r(errno, buf, sizeof(buf));
197                         VK_ERROR("Failed to create TBM sync timeline: %d(%s)", errno, buf);
198                 } else {
199                         tbm_surface_internal_set_user_data(tbm_surface,
200                                                                                            (unsigned long)&swapchain_tdm_timestamp_key,
201                                                                                            (void *)0);
202                 }
203         }
204         return fence;
205 };
206
207 static VkResult
208 swapchain_tdm_acquire_next_image(VkDevice                        device,
209                                                                  vk_swapchain_t         *chain,
210                                                                  uint64_t                        timeout,
211                                                                  tbm_surface_h          *tbm_surface,
212                                                                  int                            *sync)
213 {
214         tbm_surface_queue_error_e        tsq_err;
215         vk_swapchain_tdm_t                      *swapchain_tdm = chain->backend_data;
216         struct timespec                          abs_time;
217
218         if (timeout != UINT64_MAX) {
219                 clock_gettime(CLOCK_REALTIME, &abs_time);
220                 abs_time.tv_sec += (timeout / 1000000000L);
221                 abs_time.tv_nsec += (timeout % 1000000000L);
222                 if (abs_time.tv_nsec >= 1000000000L) {
223                         abs_time.tv_sec += (abs_time.tv_nsec / 1000000000L);
224                         abs_time.tv_nsec = (abs_time.tv_nsec % 1000000000L);
225                 }
226         }
227
228         if (pthread_mutex_lock(&swapchain_tdm->free_queue_mutex))
229                 VK_ERROR("pthread_mutex_lock free queue failed\n");
230
231         while (tbm_surface_queue_can_dequeue(swapchain_tdm->tbm_queue, 0) == 0) {
232                 if (timeout != UINT64_MAX) {
233                         int ret;
234                         ret = pthread_cond_timedwait(&swapchain_tdm->free_queue_cond,
235                                                                                  &swapchain_tdm->free_queue_mutex,
236                                                                                  &abs_time);
237                         if (ret == ETIMEDOUT) {
238                                 /* timeout */
239                                 pthread_mutex_unlock(&swapchain_tdm->free_queue_mutex);
240                                 return VK_TIMEOUT;
241                         }
242                 } else {
243                         pthread_cond_wait(&swapchain_tdm->free_queue_cond,
244                                                           &swapchain_tdm->free_queue_mutex);
245                 }
246         }
247
248         tsq_err = tbm_surface_queue_dequeue(swapchain_tdm->tbm_queue, tbm_surface);
249         VK_CHECK(tsq_err == TBM_SURFACE_QUEUE_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
250                          "tbm_surface_queue_dequeue failed.\n");
251         pthread_mutex_unlock(&swapchain_tdm->free_queue_mutex);
252
253         if (sync)
254                 *sync = swapchain_tdm_get_sync_fence(*tbm_surface);
255
256         return VK_SUCCESS;
257 }
258
259 static void
260 swapchain_tdm_deinit(VkDevice            device,
261                                          vk_swapchain_t *chain)
262 {
263         vk_swapchain_tdm_t                      *swapchain_tdm = chain->backend_data;
264
265         if (swapchain_tdm) {
266                 tdm_output_set_dpms(swapchain_tdm->tdm_output, swapchain_tdm->tdm_dpms);
267
268                 pthread_cond_destroy(&swapchain_tdm->free_queue_cond);
269                 pthread_mutex_destroy(&swapchain_tdm->free_queue_mutex);
270                 pthread_mutex_destroy(&swapchain_tdm->front_mutex);
271
272                 if (swapchain_tdm->tbm_queue)
273                         tbm_surface_queue_destroy(swapchain_tdm->tbm_queue);
274
275                 if (swapchain_tdm->buffers)
276                         vk_free(chain->allocator, swapchain_tdm->buffers);
277                 vk_free(chain->allocator, swapchain_tdm);
278         }
279 }
280
281 static VkResult
282 swapchain_tdm_get_buffers(VkDevice                       device,
283                                                   vk_swapchain_t        *chain,
284                                                   tbm_surface_h    **buffers,
285                                                   uint32_t                      *buffer_count)
286 {
287         uint32_t                                         i;
288         tbm_surface_queue_error_e        tsq_err;
289         tdm_error                                        tdm_err;
290         tbm_surface_info_s                       surf_info;
291         tdm_info_layer                           tdm_info;
292         vk_swapchain_tdm_t                      *swapchain_tdm = chain->backend_data;
293
294         *buffer_count = tbm_surface_queue_get_size(swapchain_tdm->tbm_queue);
295         swapchain_tdm->buffers = vk_alloc(chain->allocator,
296                                                                                            sizeof(tbm_surface_h) * *buffer_count,
297                                                                                            VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
298         VK_CHECK(swapchain_tdm->buffers, return VK_ERROR_OUT_OF_HOST_MEMORY, "vk_alloc() failed.\n");
299
300         for (i = 0; i < *buffer_count; i++) {
301                 tsq_err = tbm_surface_queue_dequeue(swapchain_tdm->tbm_queue,
302                                                                                         &swapchain_tdm->buffers[i]);
303                 VK_CHECK(tsq_err == TBM_SURFACE_QUEUE_ERROR_NONE,
304                                  return VK_ERROR_SURFACE_LOST_KHR,
305                                  "tbm_surface_queue_dequeue failed.\n");
306         }
307
308         for (i = 0; i < *buffer_count; i++) {
309                 tsq_err = tbm_surface_queue_release(swapchain_tdm->tbm_queue,
310                                                                                         swapchain_tdm->buffers[i]);
311                 VK_CHECK(tsq_err == TBM_SURFACE_QUEUE_ERROR_NONE,
312                                  return VK_ERROR_SURFACE_LOST_KHR,
313                                  "tbm_surface_queue_enqueue failed.\n");
314         }
315
316         *buffers = swapchain_tdm->buffers;
317
318         tdm_err = tdm_output_get_dpms(swapchain_tdm->tdm_output, &swapchain_tdm->tdm_dpms);
319         tdm_err = tdm_output_set_dpms(swapchain_tdm->tdm_output, TDM_OUTPUT_DPMS_ON);
320         VK_CHECK(tdm_err == TDM_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
321                          "tdm_output_set_dpms failed.\n");
322
323         tdm_err = tdm_output_set_mode(swapchain_tdm->tdm_output,
324                                                                   swapchain_tdm->tdm_mode);
325         VK_CHECK(tdm_err == TDM_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
326                          "tdm_output_set_mode failed.\n");
327
328         tbm_surface_get_info((*buffers)[0], &surf_info);
329
330         /* from enlightenment */
331         tdm_info.src_config.size.h = surf_info.planes[0].stride;
332         tdm_info.src_config.size.v = surf_info.height;
333
334         tdm_info.src_config.pos.x = 0;
335         tdm_info.src_config.pos.y = 0;
336         tdm_info.src_config.pos.w = surf_info.width;
337         tdm_info.src_config.pos.h = surf_info.height;
338
339         tdm_info.src_config.format = surf_info.format;
340
341         tdm_info.dst_pos.x = 0;
342         tdm_info.dst_pos.y = 0;
343         tdm_info.dst_pos.w = surf_info.width;
344         tdm_info.dst_pos.h = surf_info.height;
345
346         tdm_info.transform = TDM_TRANSFORM_NORMAL;
347
348         tdm_err = tdm_layer_set_info(swapchain_tdm->tdm_layer, &tdm_info);
349         VK_CHECK(tdm_err == TDM_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
350                          "tdm_layer_set_info failed.\n");
351
352 /*      tdm_err = tdm_layer_set_buffer_queue(swapchain_tdm->tdm_layer,
353                                                                                  swapchain_tdm->tbm_queue);
354         VK_CHECK(tdm_err == TDM_ERROR_NONE, return VK_ERROR_SURFACE_LOST_KHR,
355                          "tdm_layer_set_buffer_queue failed.\n");*/
356
357         return VK_SUCCESS;
358 }
359
360 VkResult
361 swapchain_tdm_init(VkDevice                                                      device,
362                                    const VkSwapchainCreateInfoKHR       *info,
363                                    vk_swapchain_t                                       *chain,
364                                    tbm_format                                            format)
365 {
366         VkIcdSurfaceDisplay     *surface = (VkIcdSurfaceDisplay *)(uintptr_t)info->surface;
367         vk_display_mode_t       *disp_mode = (vk_display_mode_t *)(uintptr_t)surface->displayMode;
368         vk_display_t            *disp = disp_mode->display;
369         vk_swapchain_tdm_t      *swapchain_tdm;
370
371         swapchain_tdm = vk_alloc(chain->allocator, sizeof(vk_swapchain_tdm_t),
372                                                          VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
373         VK_CHECK(swapchain_tdm, return VK_ERROR_OUT_OF_HOST_MEMORY, "vk_alloc() failed.\n");
374
375         memset(swapchain_tdm, 0x00, sizeof(*swapchain_tdm));
376         chain->backend_data = swapchain_tdm;
377
378         swapchain_tdm->tdm_display = disp->pdev->tdm_display;
379         swapchain_tdm->tdm_output = disp->tdm_output;
380         swapchain_tdm->tdm_layer = disp->pdev->planes[surface->planeIndex].tdm_layer;
381
382         swapchain_tdm->tbm_queue =
383         /*      tbm_surface_queue_sequence_create(info->minImageCount,
384                                                                                   info->imageExtent.width,
385                                                                                   info->imageExtent.height,
386                                                                                   format, TBM_BO_SCANOUT);*/
387                 tbm_surface_queue_create(info->minImageCount,
388                                                                  info->imageExtent.width,
389                                                                  info->imageExtent.height,
390                                                                  format, TBM_BO_SCANOUT);
391
392         VK_CHECK(swapchain_tdm->tbm_queue, return VK_ERROR_SURFACE_LOST_KHR,
393                          "tbm_surface_queue_create failed.\n");
394
395         if (pthread_mutex_init(&swapchain_tdm->front_mutex, NULL))
396                 VK_ERROR("pthread_mutex_init front buffer failed\n");
397         if (pthread_mutex_init(&swapchain_tdm->free_queue_mutex, NULL))
398                 VK_ERROR("pthread_mutex_init free queue failed\n");
399         if (pthread_cond_init(&swapchain_tdm->free_queue_cond, NULL))
400                 VK_ERROR("pthread_cond_init free queue failed\n");
401
402         swapchain_tdm->present_mode = info->presentMode;
403         swapchain_tdm->tdm_mode = disp_mode->tdm_mode;
404         swapchain_tdm->tdm_dpms = TDM_OUTPUT_DPMS_OFF;
405
406         chain->get_buffers = swapchain_tdm_get_buffers;
407         chain->deinit = swapchain_tdm_deinit;
408         chain->acquire_image = swapchain_tdm_acquire_next_image;
409         chain->present_image = swapchain_tdm_queue_present_image;
410
411         return VK_SUCCESS;
412 }
413