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 **************************************************************************/
41 #include "tdm_backend.h"
42 #include "tdm_private.h"
43 #include "tdm_helper.h"
45 #define PP_FUNC_ENTRY() \
46 tdm_func_pp *func_pp; \
47 tdm_private_display *private_display; \
48 tdm_private_pp *private_pp; \
49 tdm_error ret = TDM_ERROR_NONE; \
50 TDM_RETURN_VAL_IF_FAIL(pp != NULL, TDM_ERROR_INVALID_PARAMETER); \
51 private_pp = (tdm_private_pp*)pp; \
52 private_display = private_pp->private_display; \
53 func_pp = &private_display->func_pp
56 _tdm_pp_print_list(struct list_head *list)
58 tdm_pp_private_buffer *b = NULL;
60 int len = sizeof(str);
62 TDM_RETURN_IF_FAIL(list != NULL);
65 LIST_FOR_EACH_ENTRY(b, list, link) {
67 int l = snprintf(p, len, " (%p,%p)", b->src, b->dst);
74 TDM_WRN("\t %s", str);
77 static tdm_pp_private_buffer *
78 _tdm_pp_find_tbm_buffers(struct list_head *list, tbm_surface_h src, tbm_surface_h dst)
80 tdm_pp_private_buffer *b = NULL, *bb = NULL;
82 LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
83 if (b->src == src && b->dst == dst)
90 static tdm_pp_private_buffer *
91 _tdm_pp_find_buffer(struct list_head *list, tdm_pp_private_buffer *pp_buffer)
93 tdm_pp_private_buffer *b = NULL, *bb = NULL;
95 LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
104 tdm_pp_cb_done(tdm_pp *pp_backend, tbm_surface_h src, tbm_surface_h dst,
107 tdm_private_pp *private_pp = user_data;
108 tdm_private_display *private_display = private_pp->private_display;
109 tdm_pp_private_buffer *pp_buffer = NULL, *first_entry = NULL;
111 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
113 if (private_pp->owner_tid != syscall(SYS_gettid)) {
114 tdm_thread_cb_pp_done pp_done;
117 pp_done.base.type = TDM_THREAD_CB_PP_DONE;
118 pp_done.base.length = sizeof pp_done;
119 pp_done.pp_stamp = private_pp->stamp;
122 pp_done.user_data = user_data;
124 ret = tdm_thread_send_cb(private_display->private_loop, &pp_done.base);
125 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
130 if (private_pp->owner_tid != syscall(SYS_gettid))
131 TDM_NEVER_GET_HERE();
133 if (tdm_debug_dump & TDM_DUMP_FLAG_PP) {
134 /* LCOV_EXCL_START */
135 char str[TDM_PATH_LEN];
137 snprintf(str, TDM_PATH_LEN, "pp_dst_%03d", i++);
138 tdm_helper_dump_buffer_str(dst, tdm_debug_dump_dir, str);
142 if (tdm_debug_module & TDM_DEBUG_BUFFER)
143 TDM_INFO("pp(%p) done: src(%p) dst(%p)", private_pp, src, dst);
145 if (!LIST_IS_EMPTY(&private_pp->buffer_list)) {
146 first_entry = container_of((&private_pp->buffer_list)->next, pp_buffer, link);
147 if (first_entry->src != src || first_entry->dst != dst)
148 TDM_ERR("buffer(%p,%p) is skipped", first_entry->src, first_entry->dst);
150 TDM_NEVER_GET_HERE();
153 if ((pp_buffer = _tdm_pp_find_tbm_buffers(&private_pp->buffer_list, src, dst))) {
154 LIST_DEL(&pp_buffer->link);
155 LIST_DELINIT(&pp_buffer->commit_link);
157 _pthread_mutex_unlock(&private_display->lock);
158 if (private_pp->done_func)
159 private_pp->done_func(private_pp, src, dst, private_pp->done_user_data);
160 tdm_buffer_unref_backend(src);
161 tdm_buffer_unref_backend(dst);
162 _pthread_mutex_lock(&private_display->lock);
168 INTERN tdm_private_pp *
169 tdm_pp_find_stamp(tdm_private_display *private_display, double stamp)
171 tdm_private_pp *private_pp = NULL;
173 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
175 LIST_FOR_EACH_ENTRY(private_pp, &private_display->pp_list, link) {
176 if (private_pp->stamp == stamp)
183 INTERN tdm_private_pp *
184 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error)
186 tdm_func_display *func_display;
187 tdm_func_pp *func_pp;
188 tdm_private_pp *private_pp = NULL;
189 tdm_pp *pp_backend = NULL;
190 tdm_error ret = TDM_ERROR_NONE;
192 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
194 func_display = &private_display->func_display;
195 func_pp = &private_display->func_pp;
197 if (!(private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP)) {
198 /* LCOV_EXCL_START */
199 TDM_ERR("no pp capability");
201 *error = TDM_ERROR_NO_CAPABILITY;
206 pp_backend = func_display->display_create_pp(private_display->bdata, &ret);
207 if (ret != TDM_ERROR_NONE) {
208 /* LCOV_EXCL_START */
215 private_pp = calloc(1, sizeof(tdm_private_pp));
217 /* LCOV_EXCL_START */
218 TDM_ERR("failed: alloc memory");
219 func_pp->pp_destroy(pp_backend);
221 *error = TDM_ERROR_OUT_OF_MEMORY;
226 ret = func_pp->pp_set_done_handler(pp_backend, tdm_pp_cb_done, private_pp);
227 if (ret != TDM_ERROR_NONE) {
228 /* LCOV_EXCL_START */
229 TDM_ERR("spp(%p) et pp_done_handler failed", private_pp);
230 func_pp->pp_destroy(pp_backend);
237 private_pp->stamp = tdm_helper_get_time();
238 while (tdm_pp_find_stamp(private_display, private_pp->stamp))
241 LIST_ADD(&private_pp->link, &private_display->pp_list);
242 private_pp->private_display = private_display;
243 private_pp->pp_backend = pp_backend;
244 private_pp->owner_tid = syscall(SYS_gettid);
246 LIST_INITHEAD(&private_pp->pending_buffer_list);
247 LIST_INITHEAD(&private_pp->buffer_list);
250 *error = TDM_ERROR_NONE;
256 tdm_pp_destroy_internal(tdm_private_pp *private_pp)
258 tdm_private_display *private_display;
259 tdm_func_pp *func_pp;
260 tdm_pp_private_buffer *b = NULL, *bb = NULL;
261 struct list_head clone_list;
263 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
268 private_display = private_pp->private_display;
269 func_pp = &private_display->func_pp;
271 LIST_DEL(&private_pp->link);
273 func_pp->pp_destroy(private_pp->pp_backend);
275 if (!LIST_IS_EMPTY(&private_pp->pending_buffer_list)) {
276 TDM_WRN("pp(%p) not finished:", private_pp);
277 _tdm_pp_print_list(&private_pp->pending_buffer_list);
279 LIST_INITHEAD(&clone_list);
280 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
282 LIST_ADDTAIL(&b->link, &clone_list);
285 _pthread_mutex_unlock(&private_display->lock);
286 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
288 tdm_buffer_unref_backend(b->src);
289 tdm_buffer_unref_backend(b->dst);
292 _pthread_mutex_lock(&private_display->lock);
295 if (!LIST_IS_EMPTY(&private_pp->buffer_list)) {
296 TDM_WRN("pp(%p) not finished:", private_pp);
297 _tdm_pp_print_list(&private_pp->buffer_list);
299 LIST_INITHEAD(&clone_list);
300 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->buffer_list, link) {
302 LIST_ADDTAIL(&b->link, &clone_list);
305 _pthread_mutex_unlock(&private_display->lock);
306 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
308 tdm_buffer_unref_backend(b->src);
309 tdm_buffer_unref_backend(b->dst);
312 _pthread_mutex_lock(&private_display->lock);
315 private_pp->stamp = 0;
320 tdm_pp_destroy(tdm_pp *pp)
322 tdm_private_pp *private_pp = pp;
323 tdm_private_display *private_display;
328 private_display = private_pp->private_display;
330 _pthread_mutex_lock(&private_display->lock);
331 tdm_pp_destroy_internal(private_pp);
332 _pthread_mutex_unlock(&private_display->lock);
336 tdm_pp_set_info(tdm_pp *pp, tdm_info_pp *info)
340 TDM_RETURN_VAL_IF_FAIL(info != NULL, TDM_ERROR_INVALID_PARAMETER);
342 _pthread_mutex_lock(&private_display->lock);
344 if (!func_pp->pp_set_info) {
345 /* LCOV_EXCL_START */
346 _pthread_mutex_unlock(&private_display->lock);
347 TDM_DBG("failed: not implemented!!");
348 return TDM_ERROR_NOT_IMPLEMENTED;
352 TDM_INFO("pp(%p) info: src(%dx%d %d,%d %dx%d %c%c%c%c) dst(%dx%d %d,%d %dx%d %c%c%c%c) trans(%d) sync(%d) flags(%x)",
353 private_pp, info->src_config.size.h, info->src_config.size.v,
354 info->src_config.pos.x, info->src_config.pos.y,
355 info->src_config.pos.w, info->src_config.pos.h,
356 FOURCC_STR(info->src_config.format),
357 info->dst_config.size.h, info->dst_config.size.v,
358 info->dst_config.pos.x, info->dst_config.pos.y,
359 info->dst_config.pos.w, info->dst_config.pos.h,
360 FOURCC_STR(info->dst_config.format),
361 info->transform, info->sync, info->flags);
363 ret = func_pp->pp_set_info(private_pp->pp_backend, info);
364 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
366 private_pp->info = *info;
368 _pthread_mutex_unlock(&private_display->lock);
374 tdm_pp_set_done_handler(tdm_pp *pp, tdm_pp_done_handler func, void *user_data)
376 tdm_private_display *private_display;
377 tdm_private_pp *private_pp;
378 tdm_error ret = TDM_ERROR_NONE;
380 TDM_RETURN_VAL_IF_FAIL(pp != NULL, TDM_ERROR_INVALID_PARAMETER);
382 private_pp = (tdm_private_pp*)pp;
383 private_display = private_pp->private_display;
385 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
387 _pthread_mutex_lock(&private_display->lock);
389 private_pp->done_func = func;
390 private_pp->done_user_data = user_data;
392 _pthread_mutex_unlock(&private_display->lock);
398 tdm_pp_attach(tdm_pp *pp, tbm_surface_h src, tbm_surface_h dst)
400 tdm_pp_private_buffer *pp_buffer;
404 TDM_RETURN_VAL_IF_FAIL(src != NULL, TDM_ERROR_INVALID_PARAMETER);
405 TDM_RETURN_VAL_IF_FAIL(dst != NULL, TDM_ERROR_INVALID_PARAMETER);
407 _pthread_mutex_lock(&private_display->lock);
409 if (!func_pp->pp_attach) {
410 /* LCOV_EXCL_START */
411 _pthread_mutex_unlock(&private_display->lock);
412 TDM_DBG("failed: not implemented!!");
413 return TDM_ERROR_NOT_IMPLEMENTED;
417 if (tdm_display_check_module_abi(private_display, 1, 2) &&
418 private_display->caps_pp.max_attach_count > 0) {
419 /* LCOV_EXCL_START */
420 int length = LIST_LENGTH(&private_pp->pending_buffer_list) +
421 LIST_LENGTH(&private_pp->buffer_list);
422 if (length >= private_display->caps_pp.max_attach_count) {
423 _pthread_mutex_unlock(&private_display->lock);
424 TDM_DBG("failed: too many attached!! max_attach_count(%d)",
425 private_display->caps_pp.max_attach_count);
426 return TDM_ERROR_BAD_REQUEST;
431 if (tdm_debug_dump & TDM_DUMP_FLAG_PP) {
432 /* LCOV_EXCL_START */
433 char str[TDM_PATH_LEN];
435 snprintf(str, TDM_PATH_LEN, "pp_src_%03d", i++);
436 tdm_helper_dump_buffer_str(src, tdm_debug_dump_dir, str);
440 pp_buffer = calloc(1, sizeof * pp_buffer);
442 /* LCOV_EXCL_START */
443 _pthread_mutex_unlock(&private_display->lock);
444 TDM_ERR("alloc failed");
445 return TDM_ERROR_OUT_OF_MEMORY;
449 ret = func_pp->pp_attach(private_pp->pp_backend, src, dst);
450 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
452 if (ret != TDM_ERROR_NONE) {
453 /* LCOV_EXCL_START */
455 _pthread_mutex_unlock(&private_display->lock);
456 TDM_ERR("attach failed");
461 LIST_ADDTAIL(&pp_buffer->link, &private_pp->pending_buffer_list);
462 pp_buffer->src = tdm_buffer_ref_backend(src);
463 pp_buffer->dst = tdm_buffer_ref_backend(dst);
465 if (tdm_debug_module & TDM_DEBUG_BUFFER) {
466 TDM_INFO("pp(%p) attached:", private_pp);
467 _tdm_pp_print_list(&private_pp->pending_buffer_list);
470 _pthread_mutex_unlock(&private_display->lock);
476 tdm_pp_commit(tdm_pp *pp)
478 tdm_pp_private_buffer *b = NULL, *bb = NULL;
479 struct list_head commit_buffer_list;
483 _pthread_mutex_lock(&private_display->lock);
485 if (!func_pp->pp_commit) {
486 /* LCOV_EXCL_START */
487 _pthread_mutex_unlock(&private_display->lock);
488 TDM_DBG("failed: not implemented!!");
489 return TDM_ERROR_NOT_IMPLEMENTED;
493 LIST_INITHEAD(&commit_buffer_list);
495 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
497 LIST_ADDTAIL(&b->link, &private_pp->buffer_list);
498 LIST_ADDTAIL(&b->commit_link, &commit_buffer_list);
501 ret = func_pp->pp_commit(private_pp->pp_backend);
502 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
504 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &commit_buffer_list, commit_link) {
505 LIST_DELINIT(&b->commit_link);
507 if (!_tdm_pp_find_buffer(&private_pp->buffer_list, b))
510 if (ret != TDM_ERROR_NONE) {
511 /* Not to call the user release handler when failed.
512 * Do we have to call this function here really?
513 * User better use set_done_handler to know when pp is done. Using
514 * buffer_release_handler is not good.
516 tdm_buffer_remove_release_handler_internal(b->src);
517 tdm_buffer_remove_release_handler_internal(b->dst);
519 _pthread_mutex_unlock(&private_display->lock);
520 tdm_buffer_unref_backend(b->src);
521 tdm_buffer_unref_backend(b->dst);
522 _pthread_mutex_lock(&private_display->lock);
529 _pthread_mutex_unlock(&private_display->lock);