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 char str[TDM_PATH_LEN];
136 snprintf(str, TDM_PATH_LEN, "pp_dst_%03d", i++);
137 tdm_helper_dump_buffer_str(dst, tdm_debug_dump_dir, str);
140 if (tdm_debug_module & TDM_DEBUG_BUFFER)
141 TDM_INFO("pp(%p) done: src(%p) dst(%p)", private_pp, src, dst);
143 if (!LIST_IS_EMPTY(&private_pp->buffer_list)) {
144 first_entry = container_of((&private_pp->buffer_list)->next, pp_buffer, link);
145 if (first_entry->src != src || first_entry->dst != dst)
146 TDM_ERR("buffer(%p,%p) is skipped", first_entry->src, first_entry->dst);
148 TDM_NEVER_GET_HERE();
151 if ((pp_buffer = _tdm_pp_find_tbm_buffers(&private_pp->buffer_list, src, dst))) {
152 LIST_DEL(&pp_buffer->link);
154 _pthread_mutex_unlock(&private_display->lock);
155 if (private_pp->done_func)
156 private_pp->done_func(private_pp, src, dst, private_pp->done_user_data);
157 tdm_buffer_unref_backend(src);
158 tdm_buffer_unref_backend(dst);
159 _pthread_mutex_lock(&private_display->lock);
165 INTERN tdm_private_pp *
166 tdm_pp_find_stamp(tdm_private_display *private_display, double stamp)
168 tdm_private_pp *private_pp = NULL;
170 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
172 LIST_FOR_EACH_ENTRY(private_pp, &private_display->pp_list, link) {
173 if (private_pp->stamp == stamp)
180 INTERN tdm_private_pp *
181 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error)
183 tdm_func_display *func_display;
184 tdm_func_pp *func_pp;
185 tdm_private_pp *private_pp = NULL;
186 tdm_pp *pp_backend = NULL;
187 tdm_error ret = TDM_ERROR_NONE;
189 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
191 func_display = &private_display->func_display;
192 func_pp = &private_display->func_pp;
194 if (!(private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP)) {
195 TDM_ERR("no pp capability");
197 *error = TDM_ERROR_NO_CAPABILITY;
201 pp_backend = func_display->display_create_pp(private_display->bdata, &ret);
202 if (ret != TDM_ERROR_NONE) {
208 private_pp = calloc(1, sizeof(tdm_private_pp));
210 TDM_ERR("failed: alloc memory");
211 func_pp->pp_destroy(pp_backend);
213 *error = TDM_ERROR_OUT_OF_MEMORY;
217 ret = func_pp->pp_set_done_handler(pp_backend, tdm_pp_cb_done, private_pp);
218 if (ret != TDM_ERROR_NONE) {
219 TDM_ERR("spp(%p) et pp_done_handler failed", private_pp);
220 func_pp->pp_destroy(pp_backend);
226 private_pp->stamp = tdm_helper_get_time();
227 while (tdm_pp_find_stamp(private_display, private_pp->stamp))
230 LIST_ADD(&private_pp->link, &private_display->pp_list);
231 private_pp->private_display = private_display;
232 private_pp->pp_backend = pp_backend;
233 private_pp->owner_tid = syscall(SYS_gettid);
235 LIST_INITHEAD(&private_pp->pending_buffer_list);
236 LIST_INITHEAD(&private_pp->buffer_list);
239 *error = TDM_ERROR_NONE;
245 tdm_pp_destroy_internal(tdm_private_pp *private_pp)
247 tdm_private_display *private_display;
248 tdm_func_pp *func_pp;
249 tdm_pp_private_buffer *b = NULL, *bb = NULL;
250 struct list_head clone_list;
252 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
257 private_display = private_pp->private_display;
258 func_pp = &private_display->func_pp;
260 LIST_DEL(&private_pp->link);
262 func_pp->pp_destroy(private_pp->pp_backend);
264 if (!LIST_IS_EMPTY(&private_pp->pending_buffer_list)) {
265 TDM_WRN("pp(%p) not finished:", private_pp);
266 _tdm_pp_print_list(&private_pp->pending_buffer_list);
268 LIST_INITHEAD(&clone_list);
269 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
271 LIST_ADDTAIL(&b->link, &clone_list);
274 _pthread_mutex_unlock(&private_display->lock);
275 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
277 tdm_buffer_unref_backend(b->src);
278 tdm_buffer_unref_backend(b->dst);
281 _pthread_mutex_lock(&private_display->lock);
284 if (!LIST_IS_EMPTY(&private_pp->buffer_list)) {
285 TDM_WRN("pp(%p) not finished:", private_pp);
286 _tdm_pp_print_list(&private_pp->buffer_list);
288 LIST_INITHEAD(&clone_list);
289 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->buffer_list, link) {
291 LIST_ADDTAIL(&b->link, &clone_list);
294 _pthread_mutex_unlock(&private_display->lock);
295 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
297 tdm_buffer_unref_backend(b->src);
298 tdm_buffer_unref_backend(b->dst);
301 _pthread_mutex_lock(&private_display->lock);
304 private_pp->stamp = 0;
309 tdm_pp_destroy(tdm_pp *pp)
311 tdm_private_pp *private_pp = pp;
312 tdm_private_display *private_display;
317 private_display = private_pp->private_display;
319 _pthread_mutex_lock(&private_display->lock);
320 tdm_pp_destroy_internal(private_pp);
321 _pthread_mutex_unlock(&private_display->lock);
325 tdm_pp_set_info(tdm_pp *pp, tdm_info_pp *info)
329 TDM_RETURN_VAL_IF_FAIL(info != NULL, TDM_ERROR_INVALID_PARAMETER);
331 _pthread_mutex_lock(&private_display->lock);
333 if (!func_pp->pp_set_info) {
334 _pthread_mutex_unlock(&private_display->lock);
335 TDM_DBG("failed: not implemented!!");
336 return TDM_ERROR_NOT_IMPLEMENTED;
339 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)",
340 private_pp, info->src_config.size.h, info->src_config.size.v,
341 info->src_config.pos.x, info->src_config.pos.y,
342 info->src_config.pos.w, info->src_config.pos.h,
343 FOURCC_STR(info->src_config.format),
344 info->dst_config.size.h, info->dst_config.size.v,
345 info->dst_config.pos.x, info->dst_config.pos.y,
346 info->dst_config.pos.w, info->dst_config.pos.h,
347 FOURCC_STR(info->dst_config.format),
348 info->transform, info->sync, info->flags);
350 ret = func_pp->pp_set_info(private_pp->pp_backend, info);
351 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
353 private_pp->info = *info;
355 _pthread_mutex_unlock(&private_display->lock);
361 tdm_pp_set_done_handler(tdm_pp *pp, tdm_pp_done_handler func, void *user_data)
363 tdm_private_display *private_display;
364 tdm_private_pp *private_pp;
365 tdm_error ret = TDM_ERROR_NONE;
367 TDM_RETURN_VAL_IF_FAIL(pp != NULL, TDM_ERROR_INVALID_PARAMETER);
369 private_pp = (tdm_private_pp*)pp;
370 private_display = private_pp->private_display;
372 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
374 _pthread_mutex_lock(&private_display->lock);
376 private_pp->done_func = func;
377 private_pp->done_user_data = user_data;
379 _pthread_mutex_unlock(&private_display->lock);
385 tdm_pp_attach(tdm_pp *pp, tbm_surface_h src, tbm_surface_h dst)
387 tdm_pp_private_buffer *pp_buffer;
391 TDM_RETURN_VAL_IF_FAIL(src != NULL, TDM_ERROR_INVALID_PARAMETER);
392 TDM_RETURN_VAL_IF_FAIL(dst != NULL, TDM_ERROR_INVALID_PARAMETER);
394 _pthread_mutex_lock(&private_display->lock);
396 if (!func_pp->pp_attach) {
397 _pthread_mutex_unlock(&private_display->lock);
398 TDM_DBG("failed: not implemented!!");
399 return TDM_ERROR_NOT_IMPLEMENTED;
402 if (tdm_display_check_module_abi(private_display, 1, 2) &&
403 private_display->caps_pp.max_attach_count > 0) {
404 int length = LIST_LENGTH(&private_pp->pending_buffer_list) +
405 LIST_LENGTH(&private_pp->buffer_list);
406 if (length >= private_display->caps_pp.max_attach_count) {
407 _pthread_mutex_unlock(&private_display->lock);
408 TDM_DBG("failed: too many attached!! max_attach_count(%d)",
409 private_display->caps_pp.max_attach_count);
410 return TDM_ERROR_BAD_REQUEST;
414 if (tdm_debug_dump & TDM_DUMP_FLAG_PP) {
415 char str[TDM_PATH_LEN];
417 snprintf(str, TDM_PATH_LEN, "pp_src_%03d", i++);
418 tdm_helper_dump_buffer_str(src, tdm_debug_dump_dir, str);
421 pp_buffer = calloc(1, sizeof * pp_buffer);
423 _pthread_mutex_unlock(&private_display->lock);
424 TDM_ERR("alloc failed");
425 return TDM_ERROR_OUT_OF_MEMORY;
428 ret = func_pp->pp_attach(private_pp->pp_backend, src, dst);
429 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
431 if (ret != TDM_ERROR_NONE) {
433 _pthread_mutex_unlock(&private_display->lock);
434 TDM_ERR("attach failed");
438 LIST_ADDTAIL(&pp_buffer->link, &private_pp->pending_buffer_list);
439 pp_buffer->src = tdm_buffer_ref_backend(src);
440 pp_buffer->dst = tdm_buffer_ref_backend(dst);
442 if (tdm_debug_module & TDM_DEBUG_BUFFER) {
443 TDM_INFO("pp(%p) attached:", private_pp);
444 _tdm_pp_print_list(&private_pp->pending_buffer_list);
447 _pthread_mutex_unlock(&private_display->lock);
453 tdm_pp_commit(tdm_pp *pp)
455 tdm_pp_private_buffer *b = NULL, *bb = NULL;
456 struct list_head commit_buffer_list;
460 _pthread_mutex_lock(&private_display->lock);
462 if (!func_pp->pp_commit) {
463 _pthread_mutex_unlock(&private_display->lock);
464 TDM_DBG("failed: not implemented!!");
465 return TDM_ERROR_NOT_IMPLEMENTED;
468 LIST_INITHEAD(&commit_buffer_list);
470 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
472 LIST_ADDTAIL(&b->link, &private_pp->buffer_list);
473 LIST_ADDTAIL(&b->commit_link, &commit_buffer_list);
476 ret = func_pp->pp_commit(private_pp->pp_backend);
477 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
479 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &commit_buffer_list, commit_link) {
480 LIST_DEL(&b->commit_link);
482 if (!_tdm_pp_find_buffer(&private_pp->buffer_list, b))
485 if (ret != TDM_ERROR_NONE) {
486 /* Not to call the user release handler when failed.
487 * Do we have to call this function here really?
488 * User better use set_done_handler to know when pp is done. Using
489 * buffer_release_handler is not good.
491 tdm_buffer_remove_release_handler_internal(b->src);
492 tdm_buffer_remove_release_handler_internal(b->dst);
494 _pthread_mutex_unlock(&private_display->lock);
495 tdm_buffer_unref_backend(b->src);
496 tdm_buffer_unref_backend(b->dst);
497 _pthread_mutex_lock(&private_display->lock);
502 _pthread_mutex_unlock(&private_display->lock);