1 /**************************************************************************
5 * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
7 * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8 * JinYoung Jeon <jy0.jeon@samsung.com>,
9 * Taeheon Kim <th908.kim@samsung.com>,
10 * YoungJun Cho <yj44.cho@samsung.com>,
11 * SooChan Lim <sc1.lim@samsung.com>,
12 * Boram Park <sc1.lim@samsung.com>
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the
16 * "Software"), to deal in the Software without restriction, including
17 * without limitation the rights to use, copy, modify, merge, publish,
18 * distribute, sub license, and/or sell copies of the Software, and to
19 * permit persons to whom the Software is furnished to do so, subject to
20 * the following conditions:
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 **************************************************************************/
40 #include "tdm_private.h"
44 #define LAYER_FUNC_ENTRY() \
45 tdm_private_display *private_display; \
46 tdm_private_output *private_output; \
47 tdm_private_layer *private_layer; \
48 tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
49 TDM_RETURN_VAL_IF_FAIL(layer != NULL, TDM_ERROR_INVALID_PARAMETER); \
50 private_layer = (tdm_private_layer*)layer; \
51 private_output = private_layer->private_output; \
52 private_display = private_output->private_display
54 #define LAYER_FUNC_ENTRY_ERROR() \
55 tdm_private_display *private_display; \
56 tdm_private_output *private_output; \
57 tdm_private_layer *private_layer; \
58 tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
59 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(layer != NULL, TDM_ERROR_INVALID_PARAMETER, NULL); \
60 private_layer = (tdm_private_layer*)layer; \
61 private_output = private_layer->private_output; \
62 private_display = private_output->private_display
64 #define LAYER_FUNC_ENTRY_VOID_RETURN() \
65 tdm_private_display *private_display; \
66 tdm_private_output *private_output; \
67 tdm_private_layer *private_layer; \
68 tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
69 TDM_RETURN_IF_FAIL(layer != NULL); \
70 private_layer = (tdm_private_layer*)layer; \
71 private_output = private_layer->private_output; \
72 private_display = private_output->private_display
74 static void _tdm_layer_free_buffer(tdm_private_layer *private_layer, tdm_private_layer_buffer *layer_buffer);
75 static void _tdm_layer_cb_wait_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
76 unsigned int tv_sec, unsigned int tv_usec, void *user_data);
77 static void _tbm_layer_queue_acquirable_cb(tbm_surface_queue_h surface_queue, void *data);
78 static void _tbm_layer_queue_destroy_cb(tbm_surface_queue_h surface_queue, void *data);
79 static void _tdm_layer_cb_output_commit(tdm_output *output, unsigned int sequence,
80 unsigned int tv_sec, unsigned int tv_usec, void *user_data);
81 static void _tdm_layer_reset_pending_data(tdm_private_layer *private_layer);
84 tdm_layer_get_capabilities(tdm_layer *layer, tdm_layer_capability *capabilities)
88 TDM_RETURN_VAL_IF_FAIL(capabilities != NULL, TDM_ERROR_INVALID_PARAMETER);
90 _pthread_mutex_lock(&private_display->lock);
92 *capabilities = private_layer->caps.capabilities;
94 _pthread_mutex_unlock(&private_display->lock);
100 tdm_layer_get_available_formats(tdm_layer *layer, const tbm_format **formats, int *count)
104 TDM_RETURN_VAL_IF_FAIL(formats != NULL, TDM_ERROR_INVALID_PARAMETER);
105 TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
107 _pthread_mutex_lock(&private_display->lock);
109 *formats = (const tbm_format *)private_layer->caps.formats;
110 *count = private_layer->caps.format_count;
112 _pthread_mutex_unlock(&private_display->lock);
118 tdm_layer_get_available_properties(tdm_layer *layer, const tdm_prop **props, int *count)
122 TDM_RETURN_VAL_IF_FAIL(props != NULL, TDM_ERROR_INVALID_PARAMETER);
123 TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
125 _pthread_mutex_lock(&private_display->lock);
127 *props = (const tdm_prop *)private_layer->caps.props;
128 *count = private_layer->caps.prop_count;
130 _pthread_mutex_unlock(&private_display->lock);
136 tdm_layer_get_zpos(tdm_layer *layer, int *zpos)
140 TDM_RETURN_VAL_IF_FAIL(zpos != NULL, TDM_ERROR_INVALID_PARAMETER);
142 _pthread_mutex_lock(&private_display->lock);
144 *zpos = private_layer->caps.zpos;
146 _pthread_mutex_unlock(&private_display->lock);
152 tdm_layer_set_property(tdm_layer *layer, unsigned int id, tdm_value value)
154 tdm_func_layer *func_layer;
157 _pthread_mutex_lock(&private_display->lock);
159 func_layer = &private_display->func_layer;
161 if (private_layer->usable)
162 TDM_INFO("layer(%d) not usable", private_layer->index);
164 private_layer->usable = 0;
166 if (!func_layer->layer_set_property) {
167 /* LCOV_EXCL_START */
168 _pthread_mutex_unlock(&private_display->lock);
169 TDM_ERR("not implemented!!");
170 return TDM_ERROR_NOT_IMPLEMENTED;
174 ret = func_layer->layer_set_property(private_layer->layer_backend, id, value);
176 _pthread_mutex_unlock(&private_display->lock);
182 tdm_layer_get_property(tdm_layer *layer, unsigned int id, tdm_value *value)
184 tdm_func_layer *func_layer;
187 TDM_RETURN_VAL_IF_FAIL(value != NULL, TDM_ERROR_INVALID_PARAMETER);
189 _pthread_mutex_lock(&private_display->lock);
191 func_layer = &private_display->func_layer;
193 if (!func_layer->layer_get_property) {
194 /* LCOV_EXCL_START */
195 _pthread_mutex_unlock(&private_display->lock);
196 TDM_ERR("not implemented!!");
197 return TDM_ERROR_NOT_IMPLEMENTED;
201 ret = func_layer->layer_get_property(private_layer->layer_backend, id, value);
203 _pthread_mutex_unlock(&private_display->lock);
209 tdm_layer_set_info(tdm_layer *layer, tdm_info_layer *info)
211 tdm_func_layer *func_layer;
216 TDM_RETURN_VAL_IF_FAIL(info != NULL, TDM_ERROR_INVALID_PARAMETER);
218 _pthread_mutex_lock(&private_display->lock);
220 func_layer = &private_display->func_layer;
222 if (private_layer->usable)
223 TDM_INFO("layer(%p) not usable", private_layer);
225 private_layer->usable = 0;
227 if (!func_layer->layer_set_info) {
228 /* LCOV_EXCL_START */
229 _pthread_mutex_unlock(&private_display->lock);
230 TDM_ERR("not implemented!!");
231 return TDM_ERROR_NOT_IMPLEMENTED;
235 if (info->src_config.format)
236 snprintf(fmtstr, 128, "%c%c%c%c", FOURCC_STR(info->src_config.format));
238 snprintf(fmtstr, 128, "NONE");
240 TDM_INFO("layer(%p) info: src(%ux%u %u,%u %ux%u %s) dst(%u,%u %ux%u) trans(%d)",
241 private_layer, info->src_config.size.h, info->src_config.size.v,
242 info->src_config.pos.x, info->src_config.pos.y,
243 info->src_config.pos.w, info->src_config.pos.h,
245 info->dst_pos.x, info->dst_pos.y,
246 info->dst_pos.w, info->dst_pos.h,
249 private_layer->pending_info_changed = 1;
250 private_layer->pending_info = *info;
252 _pthread_mutex_unlock(&private_display->lock);
258 tdm_layer_get_info(tdm_layer *layer, tdm_info_layer *info)
260 tdm_func_layer *func_layer;
263 TDM_RETURN_VAL_IF_FAIL(info != NULL, TDM_ERROR_INVALID_PARAMETER);
265 _pthread_mutex_lock(&private_display->lock);
267 func_layer = &private_display->func_layer;
269 if (!func_layer->layer_get_info) {
270 /* LCOV_EXCL_START */
271 _pthread_mutex_unlock(&private_display->lock);
272 TDM_ERR("not implemented!!");
273 return TDM_ERROR_NOT_IMPLEMENTED;
277 ret = func_layer->layer_get_info(private_layer->layer_backend, info);
279 _pthread_mutex_unlock(&private_display->lock);
284 /* LCOV_EXCL_START */
286 _tdm_layer_dump_buffer(tdm_layer *layer, tbm_surface_h buffer)
288 tdm_private_layer *private_layer = (tdm_private_layer*)layer;
289 tdm_private_output *private_output = private_layer->private_output;
291 char fname[TDM_PATH_LEN], bufs[TDM_PATH_LEN];
292 int zpos, len = TDM_PATH_LEN;
293 tdm_private_layer *l = NULL;
297 pipe = private_output->pipe;
298 zpos = private_layer->caps.zpos;
301 LIST_FOR_EACH_ENTRY(l, &private_output->layer_list, link) {
302 if (!l->showing_buffer)
304 TDM_SNPRINTF(p, remain, "_%p", l->showing_buffer->buffer);
307 snprintf(fname, sizeof(fname), "tdm_%d_lyr_%d%s", pipe, zpos, bufs);
309 tbm_surface_internal_dump_buffer(buffer, fname);
310 TDM_DBG("%s dump excute", fname);
317 _tdm_layer_free_buffer(tdm_private_layer *private_layer, tdm_private_layer_buffer *layer_buffer)
319 tdm_private_display *private_display;
324 private_display = private_layer->private_output->private_display;
326 LIST_DEL(&layer_buffer->link);
327 if (layer_buffer->buffer) {
328 _pthread_mutex_unlock(&private_display->lock);
329 tdm_buffer_unref_backend(layer_buffer->buffer);
330 if (private_layer->buffer_queue)
331 tbm_surface_queue_release(private_layer->buffer_queue, layer_buffer->buffer);
332 _pthread_mutex_lock(&private_display->lock);
338 _tdm_layer_free_all_buffers(tdm_private_layer *private_layer)
340 tdm_private_output *private_output = private_layer->private_output;
341 tdm_private_layer_commit_handler *lm = NULL, *lmm = NULL;
342 struct list_head clone_list;
344 LIST_INITHEAD(&clone_list);
346 _tdm_layer_reset_pending_data(private_layer);
348 if (private_layer->waiting_buffer) {
349 _tdm_layer_free_buffer(private_layer, private_layer->waiting_buffer);
350 private_layer->waiting_buffer = NULL;
352 if (tdm_debug_module & TDM_DEBUG_BUFFER)
353 TDM_INFO("layer(%p) waiting_buffer(%p)",
354 private_layer, private_layer->waiting_buffer);
357 if (private_layer->committed_buffer) {
358 _tdm_layer_free_buffer(private_layer, private_layer->committed_buffer);
359 private_layer->committed_buffer = NULL;
361 if (tdm_debug_module & TDM_DEBUG_BUFFER)
362 TDM_INFO("layer(%p) committed_buffer(%p)",
363 private_layer, private_layer->committed_buffer);
366 if (private_layer->showing_buffer) {
367 _tdm_layer_free_buffer(private_layer, private_layer->showing_buffer);
368 private_layer->showing_buffer = NULL;
370 if (tdm_debug_module & TDM_DEBUG_BUFFER)
371 TDM_INFO("layer(%p) showing_buffer(%p)",
372 private_layer, private_layer->showing_buffer);
375 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
376 if (lm->private_layer != private_layer)
379 LIST_ADDTAIL(&lm->link, &clone_list);
382 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &clone_list, link) {
384 tdm_output_remove_commit_handler_internal(private_output, _tdm_layer_cb_output_commit, lm);
385 _tdm_layer_free_buffer(private_layer, lm->committed_buffer);
389 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
390 if (lm->private_layer != private_layer)
393 LIST_ADDTAIL(&lm->link, &clone_list);
396 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &clone_list, link) {
398 _tdm_layer_free_buffer(private_layer, lm->committed_buffer);
402 if (private_layer->buffer_queue) {
403 tbm_surface_queue_remove_acquirable_cb(private_layer->buffer_queue, _tbm_layer_queue_acquirable_cb, private_layer);
404 tbm_surface_queue_remove_destroy_cb(private_layer->buffer_queue, _tbm_layer_queue_destroy_cb, private_layer);
405 private_layer->buffer_queue = NULL;
410 tdm_layer_set_buffer(tdm_layer *layer, tbm_surface_h buffer)
412 tdm_func_layer *func_layer;
416 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
418 _pthread_mutex_lock(&private_display->lock);
420 /* LCOV_EXCL_START */
422 if (tdm_dump_enable && !(private_layer->caps.capabilities & TDM_LAYER_CAPABILITY_VIDEO))
423 _tdm_layer_dump_buffer(private_layer, buffer);
425 if (tdm_debug_dump & TDM_DUMP_FLAG_LAYER &&
426 !(private_layer->caps.capabilities & TDM_LAYER_CAPABILITY_VIDEO)) {
427 char str[TDM_PATH_LEN];
429 snprintf(str, TDM_PATH_LEN, "layer_%d_%d_%03d",
430 private_output->index, private_layer->index, i++);
431 tdm_helper_dump_buffer_str(buffer, tdm_debug_dump_dir, str);
435 func_layer = &private_display->func_layer;
437 if (private_layer->usable)
438 TDM_INFO("layer(%p) not usable", private_layer);
440 private_layer->usable = 0;
442 if (!func_layer->layer_set_buffer) {
443 /* LCOV_EXCL_START */
444 _pthread_mutex_unlock(&private_display->lock);
445 TDM_ERR("not implemented!!");
446 return TDM_ERROR_NOT_IMPLEMENTED;
447 /* LCOV_EXCL_START */
450 private_layer->pending_buffer_changed = 1;
452 if (private_layer->pending_buffer) {
454 if (tdm_debug_module & TDM_DEBUG_BUFFER)
455 TDM_INFO("layer(%p) pending_buffer(%p) skipped",
456 private_layer, private_layer->pending_buffer);
458 if (tdm_ttrace_module & TDM_TTRACE_LAYER) {
459 tbm_bo bo = tbm_surface_internal_get_bo(private_layer->pending_buffer, 0);
460 TDM_TRACE_ASYNC_END((intptr_t)private_layer, "[LAYER] %d", tbm_bo_export(bo));
463 tbm_surface_internal_unref(private_layer->pending_buffer);
466 tbm_surface_internal_ref(buffer);
467 private_layer->pending_buffer = buffer;
469 if (tdm_debug_module & TDM_DEBUG_BUFFER)
470 TDM_INFO("layer(%p) pending_buffer(%p)",
471 private_layer, private_layer->pending_buffer);
473 if (tdm_ttrace_module & TDM_TTRACE_LAYER) {
474 tbm_bo bo = tbm_surface_internal_get_bo(private_layer->pending_buffer, 0);
475 TDM_TRACE_ASYNC_BEGIN((intptr_t)private_layer, "[LAYER] %d", tbm_bo_export(bo));
478 _pthread_mutex_unlock(&private_display->lock);
484 tdm_layer_unset_buffer(tdm_layer *layer)
486 tdm_func_layer *func_layer;
489 _pthread_mutex_lock(&private_display->lock);
491 func_layer = &private_display->func_layer;
493 _tdm_layer_free_all_buffers(private_layer);
495 private_layer->usable = 1;
497 if (private_layer->usable)
498 TDM_INFO("layer(%p) now usable", private_layer);
500 if (!func_layer->layer_unset_buffer) {
501 /* LCOV_EXCL_START */
502 _pthread_mutex_unlock(&private_display->lock);
503 TDM_ERR("not implemented!!");
504 return TDM_ERROR_NOT_IMPLEMENTED;
505 /* LCOV_EXCL_START */
508 ret = func_layer->layer_unset_buffer(private_layer->layer_backend);
509 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
511 _pthread_mutex_unlock(&private_display->lock);
517 tdm_layer_committed(tdm_private_layer *private_layer, tdm_private_layer_buffer **committed_buffer)
519 tdm_private_output *private_output = private_layer->private_output;
520 tdm_private_display *private_display = private_output->private_display;
522 /* LCOV_EXCL_START */
523 if (private_display->print_fps) {
524 double curr = tdm_helper_get_time();
525 if (private_layer->fps_stamp == 0) {
526 private_layer->fps_stamp = curr;
527 } else if ((curr - private_layer->fps_stamp) > 1.0) {
528 TDM_INFO("output(%d) layer(%d) fps: %d", private_output->index, private_layer->index, private_layer->fps_count);
529 private_layer->fps_count = 0;
530 private_layer->fps_stamp = curr;
532 private_layer->fps_count++;
533 } else if (private_layer->fps_stamp != 0) {
534 private_layer->fps_stamp = 0;
535 private_layer->fps_count = 0;
539 if (private_layer->showing_buffer) {
540 if (tdm_ttrace_module & TDM_TTRACE_LAYER) {
541 tbm_bo bo = tbm_surface_internal_get_bo(private_layer->showing_buffer->buffer, 0);
542 TDM_TRACE_ASYNC_END((intptr_t)private_layer, "[LAYER] %d", tbm_bo_export(bo));
545 _tdm_layer_free_buffer(private_layer, private_layer->showing_buffer);
548 private_layer->showing_buffer = *committed_buffer;
549 *committed_buffer = NULL;
551 if (tdm_debug_module & TDM_DEBUG_BUFFER)
552 TDM_INFO("layer(%p) committed_buffer(%p) showing_buffer(%p)",
553 private_layer, *committed_buffer,
554 (private_layer->showing_buffer) ? private_layer->showing_buffer->buffer : NULL);
558 _tdm_layer_got_output_vblank(tdm_private_output *private_output, unsigned int sequence,
559 unsigned int tv_sec, unsigned int tv_usec)
561 tdm_private_layer_commit_handler *lm = NULL, *lmm = NULL;
562 tdm_private_display *private_display;
563 struct list_head clone_list, pending_clone_list;
564 tdm_error ret = TDM_ERROR_NONE;
566 private_display = private_output->private_display;
568 private_output->layer_waiting_vblank = 0;
570 LIST_INITHEAD(&clone_list);
571 LIST_INITHEAD(&pending_clone_list);
573 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
575 lm->private_layer->committing = 0;
576 LIST_ADDTAIL(&lm->link, &clone_list);
579 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
581 lm->private_layer->committing = 0;
582 LIST_ADDTAIL(&lm->link, &pending_clone_list);
585 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &clone_list, link) {
586 if (tdm_debug_module & TDM_DEBUG_COMMIT)
587 TDM_INFO("layer(%p) committed. handle(%p) commited_buffer(%p)",
588 lm->private_layer, lm, (lm->committed_buffer) ? lm->committed_buffer->buffer : NULL);
591 tdm_layer_committed(lm->private_layer, &lm->committed_buffer);
592 _pthread_mutex_unlock(&private_display->lock);
594 lm->func(lm->private_layer, sequence, tv_sec, tv_usec, lm->user_data);
595 _pthread_mutex_lock(&private_display->lock);
596 if (lm->committed_buffer)
597 _tdm_layer_free_buffer(lm->private_layer, lm->committed_buffer);
601 if (LIST_IS_EMPTY(&pending_clone_list))
604 TDM_GOTO_IF_FAIL(private_output->vblank != NULL, wait_failed);
606 ret = tdm_output_commit_internal(private_output, 0, NULL, NULL);
607 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
609 if (tdm_debug_module & TDM_DEBUG_COMMIT)
610 TDM_INFO("layer commit: output(%d) commit", private_output->pipe);
612 if (!TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
613 /* tdm_vblank APIs is for server. it should be called in unlock status*/
614 if (!private_output->layer_waiting_vblank) {
615 _pthread_mutex_unlock(&private_display->lock);
616 ret = tdm_vblank_wait(private_output->vblank, 0, 0, 1, _tdm_layer_cb_wait_vblank, private_output);
617 _pthread_mutex_lock(&private_display->lock);
618 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, wait_failed);
619 private_output->layer_waiting_vblank = 1;
623 if (tdm_debug_module & TDM_DEBUG_COMMIT)
624 TDM_INFO("layer commit: output(%d) wait vblank", private_output->pipe);
626 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &pending_clone_list, link) {
628 LIST_ADDTAIL(&lm->link, &private_output->layer_commit_handler_list);
631 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
632 TDM_WRN("dpms %s. Directly call vblank callback.", tdm_dpms_str(private_output->current_dpms_value));
633 _pthread_mutex_unlock(&private_display->lock);
634 _tdm_layer_cb_wait_vblank(private_output->vblank, 0, 0, 0, 0, private_output);
635 _pthread_mutex_lock(&private_display->lock);
640 /* CAUTION: Once _tdm_layer_commit returns success, the layer commit handler MUST be called always.
641 * That is, even if we get error in _tdm_layer_got_output_vblank() function for some reasons,
642 * the layer commit handler MUST be called.
644 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &pending_clone_list, link) {
645 TDM_WRN("Directly call layer commit handlers: ret(%d)\n", ret);
647 _pthread_mutex_unlock(&private_display->lock);
649 lm->func(lm->private_layer, sequence, tv_sec, tv_usec, lm->user_data);
650 _pthread_mutex_lock(&private_display->lock);
651 _tdm_layer_free_buffer(lm->private_layer, lm->committed_buffer);
659 _tdm_layer_cb_output_commit(tdm_output *output, unsigned int sequence,
660 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
662 tdm_private_layer_commit_handler *layer_commit_handler = user_data;
663 tdm_private_layer_commit_handler *lm = NULL;
664 tdm_private_display *private_display;
665 tdm_private_output *private_output = output;
666 tdm_private_layer *private_layer;
669 TDM_RETURN_IF_FAIL(layer_commit_handler != NULL);
671 private_display = private_output->private_display;
673 LIST_FOR_EACH_ENTRY(lm, &private_output->layer_commit_handler_list, link) {
674 if (layer_commit_handler == lm) {
683 LIST_DEL(&layer_commit_handler->link);
685 private_layer = layer_commit_handler->private_layer;
687 if (tdm_debug_module & TDM_DEBUG_COMMIT)
688 TDM_INFO("layer(%p) commit: output(%d) committed. handle(%p)",
689 private_layer, private_output->pipe, layer_commit_handler);
691 _pthread_mutex_lock(&private_display->lock);
693 tdm_layer_committed(private_layer, &layer_commit_handler->committed_buffer);
695 if (layer_commit_handler->func) {
696 _pthread_mutex_unlock(&private_display->lock);
697 layer_commit_handler->func(private_output, sequence,
698 tv_sec, tv_usec, layer_commit_handler->user_data);
699 _pthread_mutex_lock(&private_display->lock);
702 free(layer_commit_handler);
704 _pthread_mutex_unlock(&private_display->lock);
708 _tdm_layer_cb_wait_vblank(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
709 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
711 tdm_private_output *private_output = user_data;
712 tdm_private_display *private_display;
714 TDM_RETURN_IF_FAIL(private_output != NULL);
716 private_display = private_output->private_display;
718 _pthread_mutex_lock(&private_display->lock);
720 if (tdm_debug_module & TDM_DEBUG_COMMIT)
721 TDM_INFO("layer commit: output(%d) got vblank", private_output->pipe);
723 _tdm_layer_got_output_vblank(private_output, sequence, tv_sec, tv_usec);
725 _pthread_mutex_unlock(&private_display->lock);
729 _tdm_lauer_get_output_used_layer_count(tdm_private_output *private_output)
731 tdm_private_layer *private_layer = NULL;
732 unsigned int count = 0;
734 LIST_FOR_EACH_ENTRY(private_layer, &private_output->layer_list, link) {
735 if (!private_layer->usable)
742 /* commit_per_vblank == 1: we can commit if
743 * - there is no previous commit request
744 * - only 1 layer is used
745 * commit_per_vblank == 2: we can commit if
746 * - there is no previous commit request
749 _tdm_layer_commit_possible(tdm_private_layer *private_layer)
751 tdm_private_output *private_output = private_layer->private_output;
753 TDM_RETURN_VAL_IF_FAIL(private_output->commit_per_vblank > 0, 1);
755 /* There is a previous commit request which is not done and displayed on screen yet.
756 * We can't commit at this time.
758 if (!LIST_IS_EMPTY(&private_output->layer_commit_handler_list)) {
759 if (tdm_debug_module & TDM_DEBUG_COMMIT)
760 TDM_INFO("layer(%p) commit: not possible(previous commit)", private_layer);
764 if (private_output->commit_per_vblank == 1 && _tdm_lauer_get_output_used_layer_count(private_output) > 1) {
765 if (tdm_debug_module & TDM_DEBUG_COMMIT)
766 TDM_INFO("layer(%p) commit: not possible(more than 2 layers)", private_layer);
770 if (tdm_debug_module & TDM_DEBUG_COMMIT)
771 TDM_INFO("layer(%p) commit: possible", private_layer);
777 _tdm_layer_reset_pending_data(tdm_private_layer *private_layer)
779 private_layer->pending_info_changed = 0;
780 memset(&private_layer->pending_info, 0, sizeof private_layer->pending_info);
782 private_layer->pending_buffer_changed = 0;
783 if (private_layer->pending_buffer) {
784 tbm_surface_internal_unref(private_layer->pending_buffer);
785 private_layer->pending_buffer = NULL;
790 tdm_layer_commit_pending_data(tdm_private_layer *private_layer)
792 tdm_private_output *private_output = private_layer->private_output;
793 tdm_private_display *private_display = private_output->private_display;
794 tdm_func_layer *func_layer;
795 tdm_error ret = TDM_ERROR_NONE;
797 func_layer = &private_display->func_layer;
799 if (private_layer->pending_info_changed) {
800 ret = func_layer->layer_set_info(private_layer->layer_backend, &private_layer->pending_info);
801 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, done);
804 if (private_layer->pending_buffer_changed) {
805 tdm_private_layer_buffer *layer_buffer;
807 layer_buffer = calloc(1, sizeof(tdm_private_layer_buffer));
808 TDM_GOTO_IF_FAIL(layer_buffer != NULL, done);
810 LIST_INITHEAD(&layer_buffer->link);
812 ret = func_layer->layer_set_buffer(private_layer->layer_backend, private_layer->pending_buffer);
813 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
815 if (tdm_debug_module & TDM_DEBUG_BUFFER)
816 TDM_INFO("layer(%p) pending_buffer(%p) committed",
817 private_layer, private_layer->pending_buffer);
819 if (ret == TDM_ERROR_NONE) {
820 if (private_layer->waiting_buffer)
821 _tdm_layer_free_buffer(private_layer, private_layer->waiting_buffer);
823 private_layer->waiting_buffer = layer_buffer;
824 private_layer->waiting_buffer->buffer = tdm_buffer_ref_backend(private_layer->pending_buffer);
825 if (tdm_debug_module & TDM_DEBUG_BUFFER)
826 TDM_INFO("layer(%p) waiting_buffer(%p)",
827 private_layer, private_layer->waiting_buffer->buffer);
829 _tdm_layer_free_buffer(private_layer, layer_buffer);
833 _tdm_layer_reset_pending_data(private_layer);
838 /* CAUTION: Once _tdm_layer_commit returns success, the layer commit handler MUST be called always.
839 * That is, even if we get error in _tdm_layer_got_output_vblank() function for some reasons,
840 * the layer commit handler MUST be called.
843 _tdm_layer_commit(tdm_layer *layer, tdm_layer_commit_handler func, void *user_data)
845 tdm_private_layer_commit_handler *layer_commit_handler;
848 layer_commit_handler = calloc(1, sizeof(tdm_private_layer_commit_handler));
849 if (!layer_commit_handler) {
850 /* LCOV_EXCL_START */
851 TDM_ERR("failed: alloc memory");
852 return TDM_ERROR_OUT_OF_MEMORY;
856 if (tdm_debug_module & TDM_DEBUG_COMMIT)
857 TDM_INFO("layer(%p) commit: handle(%p)", private_layer, layer_commit_handler);
859 LIST_INITHEAD(&layer_commit_handler->link);
860 layer_commit_handler->private_layer = private_layer;
861 layer_commit_handler->func = func;
862 layer_commit_handler->user_data = user_data;
864 layer_commit_handler->committed_buffer = private_layer->waiting_buffer;
865 private_layer->waiting_buffer = NULL;
867 if (tdm_debug_module & TDM_DEBUG_BUFFER)
868 TDM_INFO("layer(%p) waiting_buffer(%p) committed_buffer(%p)",
869 private_layer, private_layer->waiting_buffer,
870 (layer_commit_handler->committed_buffer) ? layer_commit_handler->committed_buffer->buffer : NULL);
872 if (!private_output->commit_per_vblank) {
873 TDM_GOTO_IF_FAIL(private_output->commit_type == TDM_COMMIT_TYPE_OUTPUT, commit_failed);
875 LIST_ADDTAIL(&layer_commit_handler->link, &private_output->layer_commit_handler_list);
876 ret = tdm_output_commit_internal(private_layer->private_output, 0, _tdm_layer_cb_output_commit, layer_commit_handler);
877 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
879 if (tdm_debug_module & TDM_DEBUG_COMMIT)
880 TDM_INFO("layer(%p) commit: no commit-per-vblank", private_layer);
882 TDM_GOTO_IF_FAIL(private_output->commit_type == TDM_COMMIT_TYPE_LAYER, commit_failed);
884 if (private_layer->committing)
885 TDM_WRN("layer(%d) too many commit", private_layer->index);
887 private_layer->committing = 1;
889 if (_tdm_layer_commit_possible(private_layer)) {
890 /* add to layer_commit_handler_list */
891 LIST_ADDTAIL(&layer_commit_handler->link, &private_output->layer_commit_handler_list);
892 ret = tdm_output_commit_internal(private_layer->private_output, 0, NULL, NULL);
893 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
895 if (tdm_debug_module & TDM_DEBUG_COMMIT)
896 TDM_INFO("layer(%p) commit: output", private_layer);
898 /* add to pending_commit_handler_list. It will be commited when a vblank occurs */
899 LIST_ADDTAIL(&layer_commit_handler->link, &private_output->pending_commit_handler_list);
901 if (tdm_debug_module & TDM_DEBUG_COMMIT)
902 TDM_INFO("layer(%p) commit: pending", private_layer);
905 if (!private_output->vblank) {
906 /* tdm_vblank APIs is for server. it should be called in unlock status*/
907 _pthread_mutex_unlock(&private_display->lock);
908 private_output->vblank = tdm_vblank_create(private_display, private_output, NULL);
909 _pthread_mutex_lock(&private_display->lock);
910 TDM_GOTO_IF_FAIL(private_output->vblank != NULL, commit_failed);
913 if (!private_output->layer_waiting_vblank) {
914 /* tdm_vblank APIs is for server. it should be called in unlock status*/
915 _pthread_mutex_unlock(&private_display->lock);
916 ret = tdm_vblank_wait(private_output->vblank, 0, 0, 1, _tdm_layer_cb_wait_vblank, private_output);
917 _pthread_mutex_lock(&private_display->lock);
918 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
919 private_output->layer_waiting_vblank = 1;
921 if (tdm_debug_module & TDM_DEBUG_COMMIT)
922 TDM_INFO("layer(%p) commit: wait vblank", private_layer);
929 if (layer_commit_handler) {
930 private_layer->waiting_buffer = layer_commit_handler->committed_buffer;
931 LIST_DEL(&layer_commit_handler->link);
932 free(layer_commit_handler);
938 tdm_layer_commit(tdm_layer *layer, tdm_layer_commit_handler func, void *user_data)
942 _pthread_mutex_lock(&private_display->lock);
944 if (private_output->commit_type == TDM_COMMIT_TYPE_NONE) {
945 if (!private_output->commit_per_vblank)
946 private_output->commit_type = TDM_COMMIT_TYPE_OUTPUT;
948 private_output->commit_type = TDM_COMMIT_TYPE_LAYER;
951 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
952 TDM_ERR("layer(%p)'s output(%d) dpms: %s", layer, private_output->pipe,
953 tdm_dpms_str(private_output->current_dpms_value));
954 _pthread_mutex_unlock(&private_display->lock);
955 return TDM_ERROR_DPMS_OFF;
958 /* don't call this inside of _tdm_layer_commit */
959 ret = tdm_layer_commit_pending_data(private_layer);
960 if (ret != TDM_ERROR_NONE) {
961 TDM_ERR("layer(%p) committing pending data failed", layer);
962 _pthread_mutex_unlock(&private_display->lock);
966 ret = _tdm_layer_commit(private_layer, func, user_data);
968 _pthread_mutex_unlock(&private_display->lock);
974 tdm_layer_is_committing(tdm_layer *layer, unsigned int *committing)
978 TDM_RETURN_VAL_IF_FAIL(committing != NULL, TDM_ERROR_INVALID_PARAMETER);
980 _pthread_mutex_lock(&private_display->lock);
982 *committing = private_layer->committing;
984 _pthread_mutex_unlock(&private_display->lock);
990 tdm_layer_remove_commit_handler_internal(tdm_layer *layer, tdm_layer_commit_handler func, void *user_data)
992 tdm_private_layer *private_layer = (tdm_private_layer*)layer;
993 tdm_private_output *private_output = private_layer->private_output;
994 tdm_private_layer_commit_handler *lm = NULL, *lmm = NULL;
996 TDM_RETURN_IF_FAIL(private_layer != NULL);
997 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
999 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->layer_commit_handler_list, link) {
1000 if (lm->func == func && lm->user_data == user_data) {
1001 LIST_DEL(&lm->link);
1002 tdm_output_remove_commit_handler_internal(private_output, _tdm_layer_cb_output_commit, lm);
1003 _tdm_layer_free_buffer(private_layer, lm->committed_buffer);
1009 LIST_FOR_EACH_ENTRY_SAFE(lm, lmm, &private_output->pending_commit_handler_list, link) {
1010 if (lm->func == func && lm->user_data == user_data) {
1011 LIST_DEL(&lm->link);
1012 tdm_output_remove_commit_handler_internal(private_output, _tdm_layer_cb_output_commit, lm);
1013 _tdm_layer_free_buffer(private_layer, lm->committed_buffer);
1021 tdm_layer_remove_commit_handler(tdm_layer *layer, tdm_layer_commit_handler func, void *user_data)
1025 _pthread_mutex_lock(&private_display->lock);
1027 tdm_layer_remove_commit_handler_internal(layer, func, user_data);
1029 _pthread_mutex_unlock(&private_display->lock);
1034 EXTERN tbm_surface_h
1035 tdm_layer_get_displaying_buffer(tdm_layer *layer, tdm_error *error)
1037 tbm_surface_h buffer;
1038 LAYER_FUNC_ENTRY_ERROR();
1040 _pthread_mutex_lock(&private_display->lock);
1043 *error = TDM_ERROR_NONE;
1045 if (private_layer->showing_buffer) {
1046 buffer = private_layer->showing_buffer->buffer;
1049 *error = TDM_ERROR_OPERATION_FAILED;
1050 _pthread_mutex_unlock(&private_display->lock);
1051 TDM_DBG("layer(%p) showing_buffer is null", private_layer);
1054 _pthread_mutex_unlock(&private_display->lock);
1060 _tbm_layer_queue_acquirable_cb(tbm_surface_queue_h surface_queue, void *data)
1062 TDM_RETURN_IF_FAIL(data != NULL);
1063 tdm_layer *layer = data;
1064 tdm_func_layer *func_layer;
1065 tbm_surface_h surface = NULL;
1066 tdm_private_layer_buffer *layer_buffer;
1067 LAYER_FUNC_ENTRY_VOID_RETURN();
1069 _pthread_mutex_lock(&private_display->lock);
1071 func_layer = &private_display->func_layer;
1072 if (!func_layer->layer_set_buffer) {
1073 /* LCOV_EXCL_START */
1074 _pthread_mutex_unlock(&private_display->lock);
1076 /* LCOV_EXCL_STOP */
1079 layer_buffer = calloc(1, sizeof(tdm_private_layer_buffer));
1080 if (!layer_buffer) {
1081 /* LCOV_EXCL_START */
1082 _pthread_mutex_unlock(&private_display->lock);
1083 TDM_ERR("alloc failed");
1085 /* LCOV_EXCL_STOP */
1087 LIST_INITHEAD(&layer_buffer->link);
1089 if (TBM_SURFACE_QUEUE_ERROR_NONE != tbm_surface_queue_acquire(private_layer->buffer_queue, &surface) ||
1091 /* LCOV_EXCL_START */
1092 TDM_ERR("layer(%p) tbm_surface_queue_acquire() failed surface:%p",
1093 private_layer, surface);
1094 _pthread_mutex_unlock(&private_display->lock);
1097 /* LCOV_EXCL_STOP */
1100 /* we don't need to handle pending data here because the changes in this function
1101 * should be applied immediately. we can't expect calling tdm_layer_commit.
1103 ret = func_layer->layer_set_buffer(private_layer->layer_backend, surface);
1104 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
1106 if (ret == TDM_ERROR_NONE) {
1107 if (private_layer->waiting_buffer) {
1108 TDM_DBG("layer(%p) drop waiting_buffer(%p)", private_layer, private_layer->waiting_buffer->buffer);
1109 _tdm_layer_free_buffer(private_layer, private_layer->waiting_buffer);
1112 private_layer->waiting_buffer = layer_buffer;
1113 private_layer->waiting_buffer->buffer = tdm_buffer_ref_backend(surface);
1115 if (tdm_debug_module & TDM_DEBUG_BUFFER)
1116 TDM_INFO("layer(%p) waiting_buffer(%p)",
1117 private_layer, private_layer->waiting_buffer->buffer);
1119 if (private_output->commit_type == TDM_COMMIT_TYPE_OUTPUT) {
1120 ret = tdm_output_commit_internal(private_layer->private_output, 0, NULL, NULL);
1121 if (ret != TDM_ERROR_NONE)
1122 TDM_ERR("tdm_output_commit_internal() is fail");
1123 } else if (private_output->commit_type == TDM_COMMIT_TYPE_LAYER) {
1124 ret = _tdm_layer_commit(private_layer, NULL, NULL);
1125 if (ret != TDM_ERROR_NONE)
1126 TDM_ERR("layer(%p) _tdm_layer_commit() is fail", private_layer);
1128 TDM_NEVER_GET_HERE();
1131 _tdm_layer_free_buffer(private_layer, layer_buffer);
1133 _pthread_mutex_unlock(&private_display->lock);
1137 _tbm_layer_queue_destroy_cb(tbm_surface_queue_h surface_queue, void *data)
1139 TDM_RETURN_IF_FAIL(data != NULL);
1140 tdm_layer *layer = data;
1141 LAYER_FUNC_ENTRY_VOID_RETURN();
1142 TDM_RETURN_IF_FAIL(ret == TDM_ERROR_NONE);
1144 _pthread_mutex_lock(&private_display->lock);
1146 private_layer->buffer_queue = NULL;
1148 _tdm_layer_free_all_buffers(private_layer);
1150 _pthread_mutex_unlock(&private_display->lock);
1154 tdm_layer_set_buffer_queue(tdm_layer *layer, tbm_surface_queue_h buffer_queue)
1156 tdm_func_layer *func_layer;
1159 TDM_RETURN_VAL_IF_FAIL(buffer_queue != NULL, TDM_ERROR_INVALID_PARAMETER);
1161 _pthread_mutex_lock(&private_display->lock);
1163 func_layer = &private_display->func_layer;
1165 if (private_layer->usable)
1166 TDM_INFO("layer(%p) not usable", private_layer);
1168 private_layer->usable = 0;
1170 if (!func_layer->layer_set_buffer) {
1171 /* LCOV_EXCL_START */
1172 _pthread_mutex_unlock(&private_display->lock);
1173 TDM_ERR("not implemented!!");
1174 return TDM_ERROR_NOT_IMPLEMENTED;
1175 /* LCOV_EXCL_STOP */
1178 if (buffer_queue == private_layer->buffer_queue) {
1179 _pthread_mutex_unlock(&private_display->lock);
1180 return TDM_ERROR_NONE;
1183 if (private_layer->waiting_buffer) {
1184 _tdm_layer_free_buffer(private_layer, private_layer->waiting_buffer);
1185 private_layer->waiting_buffer = NULL;
1187 if (tdm_debug_module & TDM_DEBUG_BUFFER)
1188 TDM_INFO("layer(%p) waiting_buffer(%p)",
1189 private_layer, private_layer->waiting_buffer);
1192 private_layer->buffer_queue = buffer_queue;
1193 tbm_surface_queue_add_acquirable_cb(private_layer->buffer_queue,
1194 _tbm_layer_queue_acquirable_cb,
1196 tbm_surface_queue_add_destroy_cb(private_layer->buffer_queue,
1197 _tbm_layer_queue_destroy_cb,
1199 _pthread_mutex_unlock(&private_display->lock);
1205 tdm_layer_unset_buffer_queue(tdm_layer *layer)
1207 return tdm_layer_unset_buffer(layer);
1211 tdm_layer_is_usable(tdm_layer *layer, unsigned int *usable)
1215 TDM_RETURN_VAL_IF_FAIL(usable != NULL, TDM_ERROR_INVALID_PARAMETER);
1217 _pthread_mutex_lock(&private_display->lock);
1219 *usable = private_layer->usable;
1221 _pthread_mutex_unlock(&private_display->lock);
1227 tdm_layer_set_video_pos(tdm_layer *layer, int zpos)
1229 tdm_func_layer *func_layer;
1232 _pthread_mutex_lock(&private_display->lock);
1234 func_layer = &private_display->func_layer;
1236 if (!(private_layer->caps.capabilities & TDM_LAYER_CAPABILITY_VIDEO)) {
1237 TDM_ERR("layer(%p) is not video layer", private_layer);
1238 _pthread_mutex_unlock(&private_display->lock);
1239 return TDM_ERROR_INVALID_PARAMETER;
1242 if (!func_layer->layer_set_video_pos) {
1243 /* LCOV_EXCL_START */
1244 _pthread_mutex_unlock(&private_display->lock);
1245 TDM_ERR("not implemented!!");
1246 return TDM_ERROR_NOT_IMPLEMENTED;
1247 /* LCOV_EXCL_STOP */
1250 ret = func_layer->layer_set_video_pos(private_layer->layer_backend, zpos);
1252 _pthread_mutex_unlock(&private_display->lock);
1257 EXTERN tdm_capture *
1258 tdm_layer_create_capture(tdm_layer *layer, tdm_error *error)
1260 tdm_capture *capture = NULL;
1262 LAYER_FUNC_ENTRY_ERROR();
1264 _pthread_mutex_lock(&private_display->lock);
1266 capture = (tdm_capture *)tdm_capture_create_layer_internal(private_layer, error);
1268 _pthread_mutex_unlock(&private_display->lock);
1274 tdm_layer_get_buffer_flags(tdm_layer *layer, unsigned int *flags)
1276 tdm_func_layer *func_layer;
1278 TDM_RETURN_VAL_IF_FAIL(flags != NULL, TDM_ERROR_INVALID_PARAMETER);
1280 _pthread_mutex_lock(&private_display->lock);
1282 func_layer = &private_display->func_layer;
1284 if (!func_layer->layer_get_buffer_flags) {
1285 /* LCOV_EXCL_START */
1286 _pthread_mutex_unlock(&private_display->lock);
1287 TDM_ERR("not implemented!!");
1288 return TDM_ERROR_NOT_IMPLEMENTED;
1289 /* LCOV_EXCL_STOP */
1292 ret = func_layer->layer_get_buffer_flags(private_layer->layer_backend, flags);
1294 _pthread_mutex_unlock(&private_display->lock);