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 <boram1288.park@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"
42 #define PP_FUNC_ENTRY() \
43 tdm_private_module *private_module; \
44 tdm_func_pp *func_pp; \
45 tdm_private_display *private_display; \
46 tdm_private_pp *private_pp; \
47 tdm_error ret = TDM_ERROR_NONE; \
48 TDM_RETURN_VAL_IF_FAIL(pp != NULL, TDM_ERROR_INVALID_PARAMETER); \
49 private_pp = (tdm_private_pp*)pp; \
50 private_display = private_pp->private_display; \
51 private_module = private_pp->private_module; \
52 func_pp = &private_module->func_pp
55 _tdm_pp_print_list(struct list_head *list)
57 tdm_pp_private_buffer *b = NULL;
59 int len = sizeof(str);
61 TDM_RETURN_IF_FAIL(list != NULL);
65 LIST_FOR_EACH_ENTRY(b, list, link) {
67 tbm_bo src_bo = tbm_surface_internal_get_bo(b->src, 0);
68 tbm_bo dst_bo = tbm_surface_internal_get_bo(b->dst, 0);
69 int src_flags = tbm_bo_get_flags(src_bo);
70 int dst_flags = tbm_bo_get_flags(dst_bo);
71 int l = snprintf(p, len, " (%p[bo_flags:%x], %p[bo_flags:%x])",
72 b->src, src_flags, b->dst, dst_flags);
79 TDM_INFO("\t %s", str);
82 static tdm_pp_private_buffer *
83 _tdm_pp_find_tbm_buffers(struct list_head *list, tbm_surface_h src, tbm_surface_h dst)
85 tdm_pp_private_buffer *b = NULL, *bb = NULL;
87 LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
88 if (b->src == src && b->dst == dst)
95 static tdm_pp_private_buffer *
96 _tdm_pp_find_buffer(struct list_head *list, tdm_pp_private_buffer *pp_buffer)
98 tdm_pp_private_buffer *b = NULL, *bb = NULL;
100 LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
109 _tdm_pp_thread_cb_done(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
111 tdm_thread_cb_pp_done *pp_done = (tdm_thread_cb_pp_done *)cb_base;
112 tdm_private_pp *private_pp = object;
113 tdm_pp_private_buffer *pp_buffer = NULL, *first_entry = NULL;
117 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
119 assert(private_pp->owner_tid == syscall(SYS_gettid));
124 if (tdm_debug_dump & TDM_DUMP_FLAG_PP) {
125 /* LCOV_EXCL_START */
126 char str[TDM_PATH_LEN];
128 snprintf(str, TDM_PATH_LEN, "pp_dst_%03d", i++);
129 tdm_helper_dump_buffer_str(dst, tdm_debug_dump_dir, str);
133 if (!LIST_IS_EMPTY(&private_pp->buffer_list)) {
134 first_entry = container_of((&private_pp->buffer_list)->next, pp_buffer, link);
135 if (first_entry->src != src || first_entry->dst != dst)
136 TDM_ERR("buffer(%p,%p) is skipped", first_entry->src, first_entry->dst);
138 TDM_NEVER_GET_HERE();
141 if ((pp_buffer = _tdm_pp_find_tbm_buffers(&private_pp->buffer_list, src, dst))) {
142 LIST_DEL(&pp_buffer->link);
143 LIST_DELINIT(&pp_buffer->commit_link);
145 if (tdm_debug_module & TDM_DEBUG_BUFFER)
146 TDM_INFO("pp(%p) done: src(%p) dst(%p)", private_pp, src, dst);
148 if (tdm_ttrace_module & TDM_TTRACE_PP) {
149 tbm_bo bo = tbm_surface_internal_get_bo(dst, 0);
150 TDM_TRACE_ASYNC_END((intptr_t)private_pp, "[PP] %d", tbm_bo_export(bo));
153 _pthread_mutex_unlock(&private_display->lock);
154 if (private_pp->done_func)
155 private_pp->done_func(private_pp, src, dst, private_pp->done_user_data);
156 tdm_buffer_unref_backend(src);
157 tdm_buffer_unref_backend(dst);
158 _pthread_mutex_lock(&private_display->lock);
165 _tdm_pp_cb_done(tdm_pp *pp_module, tbm_surface_h src, tbm_surface_h dst, void *user_data)
167 tdm_thread_cb_pp_done pp_done;
168 tdm_private_pp *private_pp = user_data;
171 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
173 memset(&pp_done, 0, sizeof pp_done);
174 pp_done.base.type = TDM_THREAD_CB_PP_DONE;
175 pp_done.base.length = sizeof pp_done;
176 pp_done.base.object_stamp = private_pp->stamp;
177 pp_done.base.data = NULL;
178 pp_done.base.sync = 0;
182 ret = tdm_thread_cb_call(private_pp, &pp_done.base, 1);
183 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
187 _tdm_pp_find_object(tdm_private_display *private_display, double stamp)
189 tdm_private_module *private_module = NULL;
190 tdm_private_pp *private_pp = NULL;
192 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
194 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
195 LIST_FOR_EACH_ENTRY(private_pp, &private_module->pp_list, link) {
196 if (private_pp->stamp == stamp)
205 tdm_pp_init(tdm_private_display *private_display)
207 tdm_thread_cb_set_find_func(TDM_THREAD_CB_PP_DONE, _tdm_pp_find_object);
209 return TDM_ERROR_NONE;
212 INTERN tdm_private_pp *
213 tdm_pp_create_internal(tdm_private_module *private_module, tdm_error *error)
215 tdm_private_display *private_display;
216 tdm_func_display *func_display;
217 tdm_func_pp *func_pp;
218 tdm_private_pp *private_pp = NULL;
219 tdm_pp *pp_module = NULL;
220 tdm_error ret = TDM_ERROR_NONE;
222 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
223 TDM_RETURN_VAL_IF_FAIL(private_module != NULL, NULL);
225 private_display = private_module->private_display;
226 func_display = &private_module->func_display;
227 func_pp = &private_module->func_pp;
229 if (!(private_module->capabilities & TDM_DISPLAY_CAPABILITY_PP)) {
230 /* LCOV_EXCL_START */
231 TDM_ERR("backedn(%s) no pp capability", private_module->module_data->name);
233 *error = TDM_ERROR_NO_CAPABILITY;
238 pp_module = func_display->display_create_pp(private_module->bdata, &ret);
239 if (ret != TDM_ERROR_NONE) {
240 /* LCOV_EXCL_START */
247 private_pp = calloc(1, sizeof(tdm_private_pp));
249 /* LCOV_EXCL_START */
250 TDM_ERR("failed: alloc memory");
251 func_pp->pp_destroy(pp_module);
253 *error = TDM_ERROR_OUT_OF_MEMORY;
258 ret = tdm_thread_cb_add(private_pp, TDM_THREAD_CB_PP_DONE, NULL, _tdm_pp_thread_cb_done, NULL);
259 if (ret != TDM_ERROR_NONE) {
260 TDM_ERR("pp tdm_thread_cb_add failed");
261 func_pp->pp_destroy(pp_module);
268 ret = func_pp->pp_set_done_handler(pp_module, _tdm_pp_cb_done, private_pp);
269 if (ret != TDM_ERROR_NONE) {
270 /* LCOV_EXCL_START */
271 TDM_ERR("spp(%p) et pp_done_handler failed", private_pp);
272 func_pp->pp_destroy(pp_module);
280 private_pp->stamp = tdm_helper_get_time();
281 while (_tdm_pp_find_object(private_display, private_pp->stamp))
284 LIST_ADD(&private_pp->link, &private_module->pp_list);
285 private_pp->private_display = private_module->private_display;
286 private_pp->private_module = private_module;
287 private_pp->pp_module = pp_module;
288 private_pp->owner_tid = syscall(SYS_gettid);
290 LIST_INITHEAD(&private_pp->pending_buffer_list);
291 LIST_INITHEAD(&private_pp->buffer_list);
294 *error = TDM_ERROR_NONE;
300 tdm_pp_destroy_internal(tdm_private_pp *private_pp)
302 tdm_private_display *private_display;
303 tdm_private_module *private_module;
304 tdm_func_pp *func_pp;
305 tdm_pp_private_buffer *b = NULL, *bb = NULL;
306 struct list_head clone_list;
308 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
313 tdm_thread_cb_remove(private_pp, TDM_THREAD_CB_PP_DONE, NULL, _tdm_pp_thread_cb_done, NULL);
315 private_display = private_pp->private_display;
316 private_module = private_pp->private_module;
317 func_pp = &private_module->func_pp;
319 LIST_DEL(&private_pp->link);
321 func_pp->pp_destroy(private_pp->pp_module);
323 if (!LIST_IS_EMPTY(&private_pp->pending_buffer_list)) {
324 TDM_WRN("pp(%p) not finished:", private_pp);
325 _tdm_pp_print_list(&private_pp->pending_buffer_list);
327 LIST_INITHEAD(&clone_list);
328 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
330 LIST_ADDTAIL(&b->link, &clone_list);
333 _pthread_mutex_unlock(&private_display->lock);
334 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
337 if (tdm_ttrace_module & TDM_TTRACE_PP) {
338 tbm_bo bo = tbm_surface_internal_get_bo(b->dst, 0);
339 TDM_TRACE_ASYNC_END((intptr_t)private_pp, "[PP] %d", tbm_bo_export(bo));
342 tdm_buffer_unref_backend(b->src);
343 tdm_buffer_unref_backend(b->dst);
346 _pthread_mutex_lock(&private_display->lock);
349 if (!LIST_IS_EMPTY(&private_pp->buffer_list)) {
350 TDM_WRN("pp(%p) not finished:", private_pp);
351 _tdm_pp_print_list(&private_pp->buffer_list);
353 LIST_INITHEAD(&clone_list);
354 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->buffer_list, link) {
356 LIST_ADDTAIL(&b->link, &clone_list);
359 _pthread_mutex_unlock(&private_display->lock);
360 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
363 if (tdm_ttrace_module & TDM_TTRACE_PP) {
364 tbm_bo bo = tbm_surface_internal_get_bo(b->dst, 0);
365 TDM_TRACE_ASYNC_END((intptr_t)private_pp, "[PP] %d", tbm_bo_export(bo));
368 tdm_buffer_unref_backend(b->src);
369 tdm_buffer_unref_backend(b->dst);
372 _pthread_mutex_lock(&private_display->lock);
375 private_pp->stamp = 0;
380 tdm_pp_destroy(tdm_pp *pp)
382 tdm_private_pp *private_pp = pp;
383 tdm_private_display *private_display;
388 private_display = private_pp->private_display;
390 _pthread_mutex_lock(&private_display->lock);
391 tdm_pp_destroy_internal(private_pp);
392 _pthread_mutex_unlock(&private_display->lock);
396 tdm_pp_set_info(tdm_pp *pp, tdm_info_pp *info)
400 TDM_RETURN_VAL_IF_FAIL(info != NULL, TDM_ERROR_INVALID_PARAMETER);
402 _pthread_mutex_lock(&private_display->lock);
404 if (!func_pp->pp_set_info) {
405 /* LCOV_EXCL_START */
406 _pthread_mutex_unlock(&private_display->lock);
407 TDM_DBG("failed: not implemented!!");
408 return TDM_ERROR_NOT_IMPLEMENTED;
412 TDM_INFO("pp(%p) info: src(%ux%u %u,%u %ux%u %c%c%c%c) dst(%ux%u %u,%u %ux%u %c%c%c%c) trans(%d) sync(%d) flags(%x)",
413 private_pp, info->src_config.size.h, info->src_config.size.v,
414 info->src_config.pos.x, info->src_config.pos.y,
415 info->src_config.pos.w, info->src_config.pos.h,
416 FOURCC_STR(info->src_config.format),
417 info->dst_config.size.h, info->dst_config.size.v,
418 info->dst_config.pos.x, info->dst_config.pos.y,
419 info->dst_config.pos.w, info->dst_config.pos.h,
420 FOURCC_STR(info->dst_config.format),
421 info->transform, info->sync, info->flags);
423 ret = func_pp->pp_set_info(private_pp->pp_module, info);
424 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
426 private_pp->info = *info;
428 _pthread_mutex_unlock(&private_display->lock);
434 tdm_pp_set_done_handler(tdm_pp *pp, tdm_pp_done_handler func, void *user_data)
436 tdm_private_display *private_display;
437 tdm_private_pp *private_pp;
438 tdm_error ret = TDM_ERROR_NONE;
440 TDM_RETURN_VAL_IF_FAIL(pp != NULL, TDM_ERROR_INVALID_PARAMETER);
442 private_pp = (tdm_private_pp*)pp;
443 private_display = private_pp->private_display;
445 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
447 _pthread_mutex_lock(&private_display->lock);
449 private_pp->done_func = func;
450 private_pp->done_user_data = user_data;
452 _pthread_mutex_unlock(&private_display->lock);
458 tdm_pp_attach(tdm_pp *pp, tbm_surface_h src, tbm_surface_h dst)
460 tdm_pp_private_buffer *pp_buffer;
464 TDM_RETURN_VAL_IF_FAIL(src != NULL, TDM_ERROR_INVALID_PARAMETER);
465 TDM_RETURN_VAL_IF_FAIL(dst != NULL, TDM_ERROR_INVALID_PARAMETER);
467 _pthread_mutex_lock(&private_display->lock);
469 if (!func_pp->pp_attach) {
470 /* LCOV_EXCL_START */
471 _pthread_mutex_unlock(&private_display->lock);
472 TDM_DBG("failed: not implemented!!");
473 return TDM_ERROR_NOT_IMPLEMENTED;
477 if (tdm_module_check_abi(private_module, 1, 2) &&
478 private_module->caps_pp.max_attach_count > 0) {
479 /* LCOV_EXCL_START */
480 int length = LIST_LENGTH(&private_pp->pending_buffer_list) +
481 LIST_LENGTH(&private_pp->buffer_list);
482 if (length >= private_module->caps_pp.max_attach_count) {
483 _pthread_mutex_unlock(&private_display->lock);
484 TDM_DBG("failed: backend(%s) too many attached!! max_attach_count(%d)",
485 private_module->module_data->name, private_module->caps_pp.max_attach_count);
486 return TDM_ERROR_BAD_REQUEST;
491 if (tdm_debug_dump & TDM_DUMP_FLAG_PP) {
492 /* LCOV_EXCL_START */
493 char str[TDM_PATH_LEN];
495 snprintf(str, TDM_PATH_LEN, "pp_src_%03d", i++);
496 tdm_helper_dump_buffer_str(src, tdm_debug_dump_dir, str);
500 pp_buffer = calloc(1, sizeof * pp_buffer);
502 /* LCOV_EXCL_START */
503 _pthread_mutex_unlock(&private_display->lock);
504 TDM_ERR("alloc failed");
505 return TDM_ERROR_OUT_OF_MEMORY;
509 ret = func_pp->pp_attach(private_pp->pp_module, src, dst);
510 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
512 if (ret != TDM_ERROR_NONE) {
513 /* LCOV_EXCL_START */
515 _pthread_mutex_unlock(&private_display->lock);
516 TDM_ERR("attach failed");
521 LIST_ADDTAIL(&pp_buffer->link, &private_pp->pending_buffer_list);
522 pp_buffer->src = tdm_buffer_ref_backend(src);
523 pp_buffer->dst = tdm_buffer_ref_backend(dst);
525 if (tdm_debug_module & TDM_DEBUG_BUFFER) {
526 TDM_INFO("pp(%p) attached:", private_pp);
527 _tdm_pp_print_list(&private_pp->pending_buffer_list);
530 if (tdm_ttrace_module & TDM_TTRACE_PP) {
531 tbm_bo bo = tbm_surface_internal_get_bo(dst, 0);
532 TDM_TRACE_ASYNC_BEGIN((intptr_t)pp, "[PP] %d", tbm_bo_export(bo));
535 _pthread_mutex_unlock(&private_display->lock);
541 tdm_pp_commit(tdm_pp *pp)
543 tdm_pp_private_buffer *b = NULL, *bb = NULL;
544 struct list_head commit_buffer_list;
548 _pthread_mutex_lock(&private_display->lock);
550 if (!func_pp->pp_commit) {
551 /* LCOV_EXCL_START */
552 _pthread_mutex_unlock(&private_display->lock);
553 TDM_DBG("failed: not implemented!!");
554 return TDM_ERROR_NOT_IMPLEMENTED;
558 LIST_INITHEAD(&commit_buffer_list);
560 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
562 LIST_ADDTAIL(&b->link, &private_pp->buffer_list);
563 LIST_ADDTAIL(&b->commit_link, &commit_buffer_list);
566 ret = func_pp->pp_commit(private_pp->pp_module);
567 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
569 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &commit_buffer_list, commit_link) {
570 LIST_DELINIT(&b->commit_link);
572 if (!_tdm_pp_find_buffer(&private_pp->buffer_list, b))
575 if (ret != TDM_ERROR_NONE) {
576 /* Not to call the user release handler when failed.
577 * Do we have to call this function here really?
578 * User better use set_done_handler to know when pp is done. Using
579 * buffer_release_handler is not good.
581 tdm_buffer_remove_release_handler_internal(b->src);
582 tdm_buffer_remove_release_handler_internal(b->dst);
584 _pthread_mutex_unlock(&private_display->lock);
585 tdm_buffer_unref_backend(b->src);
586 tdm_buffer_unref_backend(b->dst);
587 _pthread_mutex_lock(&private_display->lock);
594 _pthread_mutex_unlock(&private_display->lock);