2 * Copyright © 2016 S-Core Corporation
3 * Copyright © 2016-2017 Samsung Electronics co., Ltd. All Rights Reserved.
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:
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
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.
31 typedef struct vk_swapchain_tdm vk_swapchain_tdm_t;
33 struct vk_swapchain_tdm {
34 tdm_display *tdm_display;
35 tdm_output *tdm_output;
37 const tdm_output_mode *tdm_mode;
38 tdm_output_dpms tdm_dpms;
40 tbm_surface_queue_h tbm_queue;
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;
50 static int swapchain_tdm_timeline_key;
51 static int swapchain_tdm_timestamp_key;
54 swapchain_tdm_increase_timestamp(tbm_surface_h tbm_surface)
57 if (tbm_surface_internal_get_user_data(tbm_surface,
58 (unsigned long)&swapchain_tdm_timeline_key,
59 (void **)(&timeline))) {
61 if (tbm_sync_timeline_inc(timeline, 1) == 0) {
63 strerror_r(errno, buf, sizeof(buf));
64 VK_ERROR("Failed to increase TBM sync timeline: %d(%s)", errno, buf);
71 swapchain_tdm_output_commit_cb(tdm_output *output, unsigned int sequence,
72 unsigned int tv_sec, unsigned int tv_usec,
75 vk_swapchain_t *chain = user_data;
76 vk_swapchain_tdm_t *swapchain_tdm = chain->backend_data;
78 if (pthread_mutex_lock(&swapchain_tdm->front_mutex))
79 VK_ERROR("pthread_mutex_lock front buffer failed\n");
81 if (swapchain_tdm->front_buffer) {
82 swapchain_tdm_increase_timestamp(swapchain_tdm->front_buffer);
83 swapchain_tdm->front_buffer = NULL;
85 pthread_mutex_unlock(&swapchain_tdm->front_mutex);
89 swapchain_tdm_queue_present_image(VkQueue queue,
90 vk_swapchain_t *chain,
91 tbm_surface_h tbm_surface,
94 tbm_surface_queue_error_e tsq_err;
96 vk_swapchain_tdm_t *swapchain_tdm = chain->backend_data;
99 if (tbm_sync_fence_wait(sync_fd, -1) != 1) {
101 strerror_r(errno, buf, sizeof(buf));
102 VK_ERROR("Failed to wait sync. | error: %d(%s)", errno, buf);
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");
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");
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");
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");
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");
128 if (pthread_mutex_lock(&swapchain_tdm->free_queue_mutex))
129 VK_ERROR("pthread_mutex_lock free queue failed\n");
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");
135 pthread_mutex_unlock(&swapchain_tdm->free_queue_mutex);
136 pthread_cond_signal(&swapchain_tdm->free_queue_cond);
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);
149 swapchain_tdm_timeline_destroy_cb(void *user_data)
151 if (((tbm_fd)user_data) != -1)
152 close((tbm_fd)user_data);
156 swapchain_tdm_get_sync_fence(tbm_surface_h tbm_surface)
162 if (tbm_surface_internal_get_user_data(tbm_surface,
163 (unsigned long)&swapchain_tdm_timeline_key,
164 (void **)(&timeline))) {
167 tbm_surface_internal_get_user_data(tbm_surface,
168 (unsigned long)&swapchain_tdm_timestamp_key,
169 (void **)(×tamp));
171 tbm_surface_internal_set_user_data(tbm_surface,
172 (unsigned long)&swapchain_tdm_timestamp_key,
175 snprintf(name, 32, "%d",
176 tbm_bo_export(tbm_surface_internal_get_bo(tbm_surface, 0)));
177 fence = tbm_sync_fence_create(timeline,
182 strerror_r(errno, buf, sizeof(buf));
183 VK_ERROR("Failed to create TBM sync fence: %d(%s)", errno, buf);
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,
194 if (timeline == -1) {
196 strerror_r(errno, buf, sizeof(buf));
197 VK_ERROR("Failed to create TBM sync timeline: %d(%s)", errno, buf);
199 tbm_surface_internal_set_user_data(tbm_surface,
200 (unsigned long)&swapchain_tdm_timestamp_key,
208 swapchain_tdm_acquire_next_image(VkDevice device,
209 vk_swapchain_t *chain,
211 tbm_surface_h *tbm_surface,
214 tbm_surface_queue_error_e tsq_err;
215 vk_swapchain_tdm_t *swapchain_tdm = chain->backend_data;
216 struct timespec abs_time;
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);
228 if (pthread_mutex_lock(&swapchain_tdm->free_queue_mutex))
229 VK_ERROR("pthread_mutex_lock free queue failed\n");
231 while (tbm_surface_queue_can_dequeue(swapchain_tdm->tbm_queue, 0) == 0) {
232 if (timeout != UINT64_MAX) {
234 ret = pthread_cond_timedwait(&swapchain_tdm->free_queue_cond,
235 &swapchain_tdm->free_queue_mutex,
237 if (ret == ETIMEDOUT) {
239 pthread_mutex_unlock(&swapchain_tdm->free_queue_mutex);
243 pthread_cond_wait(&swapchain_tdm->free_queue_cond,
244 &swapchain_tdm->free_queue_mutex);
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);
254 *sync = swapchain_tdm_get_sync_fence(*tbm_surface);
260 swapchain_tdm_deinit(VkDevice device,
261 vk_swapchain_t *chain)
263 vk_swapchain_tdm_t *swapchain_tdm = chain->backend_data;
266 tdm_output_set_dpms(swapchain_tdm->tdm_output, swapchain_tdm->tdm_dpms);
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);
272 if (swapchain_tdm->tbm_queue)
273 tbm_surface_queue_destroy(swapchain_tdm->tbm_queue);
275 if (swapchain_tdm->buffers)
276 vk_free(&chain->allocator, swapchain_tdm->buffers);
277 vk_free(&chain->allocator, swapchain_tdm);
282 swapchain_tdm_get_buffers(VkDevice device,
283 vk_swapchain_t *chain,
284 tbm_surface_h **buffers,
285 uint32_t *buffer_count)
288 tbm_surface_queue_error_e tsq_err;
290 tbm_surface_info_s surf_info;
291 tdm_info_layer tdm_info;
292 vk_swapchain_tdm_t *swapchain_tdm = chain->backend_data;
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");
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");
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");
316 *buffers = swapchain_tdm->buffers;
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");
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");
328 tbm_surface_get_info((*buffers)[0], &surf_info);
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;
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;
339 tdm_info.src_config.format = surf_info.format;
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;
346 tdm_info.transform = TDM_TRANSFORM_NORMAL;
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");
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");*/
361 swapchain_tdm_init(VkDevice device,
362 const VkSwapchainCreateInfoKHR *info,
363 vk_swapchain_t *chain,
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;
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");
375 memset(swapchain_tdm, 0x00, sizeof(*swapchain_tdm));
376 chain->backend_data = swapchain_tdm;
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;
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);
392 VK_CHECK(swapchain_tdm->tbm_queue, return VK_ERROR_SURFACE_LOST_KHR,
393 "tbm_surface_queue_create failed.\n");
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");
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;
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;