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 CAPTURE_FUNC_ENTRY() \
43 tdm_func_capture *func_capture; \
44 tdm_private_display *private_display; \
45 tdm_private_module *private_module; \
46 tdm_private_capture *private_capture; \
47 tdm_error ret = TDM_ERROR_NONE; \
48 TDM_RETURN_VAL_IF_FAIL(capture != NULL, TDM_ERROR_INVALID_PARAMETER); \
49 private_capture = (tdm_private_capture*)capture; \
50 private_display = private_capture->private_display; \
51 private_module = private_capture->private_module; \
52 func_capture = &private_module->func_capture
55 _tdm_capture_print_list(struct list_head *list)
57 tdm_capture_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 bo = tbm_surface_internal_get_bo(b->buffer, 0);
68 int flags = tbm_bo_get_flags(bo);
69 int l = snprintf(p, len, " (%p[bo_flags:%x])", b->buffer, flags);
76 TDM_INFO("\t %s", str);
79 static tdm_capture_private_buffer *
80 _tdm_capture_find_tbm_buffer(struct list_head *list, tbm_surface_h buffer)
82 tdm_capture_private_buffer *b = NULL, *bb = NULL;
84 LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
85 if (b->buffer == buffer)
92 static tdm_capture_private_buffer *
93 _tdm_capture_find_buffer(struct list_head *list, tdm_capture_private_buffer *capture_buffer)
95 tdm_capture_private_buffer *b = NULL, *bb = NULL;
97 LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
98 if (b == capture_buffer)
106 _tdm_capture_thread_cb_done(tdm_private_display *private_display, void *object, tdm_thread_cb_base *cb_base, void *user_data)
108 tdm_thread_cb_capture_done *capture_done = (tdm_thread_cb_capture_done *)cb_base;
109 tdm_private_capture *private_capture = object;
110 tdm_capture_private_buffer *capture_buffer = NULL, *first_entry = NULL;
111 tbm_surface_h buffer;
113 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
115 assert(private_capture->owner_tid == syscall(SYS_gettid));
117 buffer = capture_done->buffer;
119 if (tdm_debug_dump & TDM_DUMP_FLAG_CAPTURE) {
120 /* LCOV_EXCL_START */
121 char str[TDM_PATH_LEN];
123 snprintf(str, TDM_PATH_LEN, "capture_%03d", i++);
124 tdm_helper_dump_buffer_str(buffer, tdm_debug_dump_dir, str);
128 if (tdm_debug_module & TDM_DEBUG_BUFFER)
129 TDM_INFO("capture(%p) done: %p", private_capture, buffer);
131 if (tdm_ttrace_module & TDM_TTRACE_CAPTURE) {
132 tbm_bo bo = tbm_surface_internal_get_bo(buffer, 0);
133 TDM_TRACE_ASYNC_END((intptr_t)private_capture, "[CAPTURE] %d", tbm_bo_export(bo));
136 if (!LIST_IS_EMPTY(&private_capture->buffer_list)) {
137 first_entry = container_of((&private_capture->buffer_list)->next, capture_buffer, link);
138 if (first_entry->buffer != buffer)
139 TDM_ERR("buffer(%p) is skipped", first_entry->buffer);
142 if ((capture_buffer = _tdm_capture_find_tbm_buffer(&private_capture->buffer_list, buffer))) {
143 LIST_DEL(&capture_buffer->link);
144 LIST_DELINIT(&capture_buffer->commit_link);
146 _pthread_mutex_unlock(&private_display->lock);
147 if (private_capture->done_func)
148 private_capture->done_func(capture_buffer, buffer, private_capture->done_user_data);
149 tdm_buffer_unref_backend(buffer);
150 _pthread_mutex_lock(&private_display->lock);
152 free(capture_buffer);
157 _tdm_capture_cb_done(tdm_capture *capture_module, tbm_surface_h buffer, void *user_data)
159 tdm_private_capture *private_capture = user_data;
160 tdm_thread_cb_capture_done capture_done;
163 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
165 memset(&capture_done, 0, sizeof capture_done);
166 capture_done.base.type = TDM_THREAD_CB_CAPTURE_DONE;
167 capture_done.base.length = sizeof capture_done;
168 capture_done.base.object_stamp = private_capture->stamp;
169 capture_done.base.data = NULL;
170 capture_done.base.sync = 0;
171 capture_done.buffer = buffer;
173 ret = tdm_thread_cb_call(private_capture, &capture_done.base, 1);
174 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
178 _tdm_capture_find_object(tdm_private_display *private_display, double stamp)
180 tdm_private_module *private_module = NULL;
181 tdm_private_capture *private_capture = NULL;
183 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
185 LIST_FOR_EACH_ENTRY(private_module, &private_display->module_list, link) {
186 LIST_FOR_EACH_ENTRY(private_capture, &private_module->capture_list, display_link) {
187 if (private_capture->stamp == stamp)
188 return private_capture;
196 tdm_capture_init(tdm_private_display *private_display)
198 tdm_thread_cb_set_find_func(TDM_THREAD_CB_CAPTURE_DONE, _tdm_capture_find_object);
200 return TDM_ERROR_NONE;
203 INTERN tdm_private_capture *
204 tdm_capture_create_output_internal(tdm_private_output *private_output,
207 tdm_private_display *private_display = private_output->private_display;
208 tdm_private_module *private_module = private_output->private_module;
209 tdm_func_output *func_output = &private_module->func_output;
210 tdm_func_capture *func_capture = &private_module->func_capture;
211 tdm_private_capture *private_capture = NULL;
212 tdm_capture *capture_module = NULL;
213 tdm_error ret = TDM_ERROR_NONE;
215 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
217 if (!(private_module->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE)) {
218 /* LCOV_EXCL_START */
219 TDM_ERR("backend(%s) no capture capability", private_module->module_data->name);
221 *error = TDM_ERROR_NO_CAPABILITY;
226 if (!(private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT)) {
227 /* LCOV_EXCL_START */
228 TDM_ERR("backend(%s) no output capture capability", private_module->module_data->name);
230 *error = TDM_ERROR_NO_CAPABILITY;
235 capture_module = func_output->output_create_capture(
236 private_output->output_backend, &ret);
237 if (ret != TDM_ERROR_NONE) {
238 /* LCOV_EXCL_START */
245 private_capture = calloc(1, sizeof(tdm_private_capture));
246 if (!private_capture) {
247 /* LCOV_EXCL_START */
248 TDM_ERR("failed: alloc memory");
249 func_capture->capture_destroy(capture_module);
251 *error = TDM_ERROR_OUT_OF_MEMORY;
256 ret = tdm_thread_cb_add(private_capture, TDM_THREAD_CB_CAPTURE_DONE, NULL, _tdm_capture_thread_cb_done, NULL);
257 if (ret != TDM_ERROR_NONE) {
258 TDM_ERR("capture tdm_thread_cb_add failed");
259 func_capture->capture_destroy(capture_module);
260 free(private_capture);
266 ret = func_capture->capture_set_done_handler(capture_module, _tdm_capture_cb_done, private_capture);
267 if (ret != TDM_ERROR_NONE) {
268 /* LCOV_EXCL_START */
269 TDM_ERR("capture(%p) set capture_done_handler failed", private_capture);
270 func_capture->capture_destroy(capture_module);
271 free(private_capture);
278 private_capture->stamp = tdm_helper_get_time();
279 while (_tdm_capture_find_object(private_display, private_capture->stamp))
280 private_capture->stamp++;
282 LIST_ADD(&private_capture->link, &private_output->capture_list);
283 LIST_ADD(&private_capture->display_link, &private_module->capture_list);
285 private_capture->private_module = private_module;
287 private_capture->target = TDM_CAPTURE_TARGET_OUTPUT;
288 private_capture->private_display = private_display;
289 private_capture->private_output = private_output;
290 private_capture->private_layer = NULL;
291 private_capture->capture_module = capture_module;
292 private_capture->owner_tid = syscall(SYS_gettid);
294 LIST_INITHEAD(&private_capture->pending_buffer_list);
295 LIST_INITHEAD(&private_capture->buffer_list);
297 TDM_DBG("capture(%p) create", private_capture);
300 *error = TDM_ERROR_NONE;
302 return private_capture;
305 /* LCOV_EXCL_START */
306 INTERN tdm_private_capture *
307 tdm_capture_create_layer_internal(tdm_private_layer *private_layer,
310 tdm_private_output *private_output = private_layer->private_output;
311 tdm_private_module *private_module = private_layer->private_module;
312 tdm_private_display *private_display = private_output->private_display;
313 tdm_func_layer *func_layer = &private_module->func_layer;
314 tdm_func_capture *func_capture = &private_module->func_capture;
315 tdm_private_capture *private_capture = NULL;
316 tdm_capture *capture_module = NULL;
317 tdm_error ret = TDM_ERROR_NONE;
319 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
321 if (!(private_module->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE)) {
322 TDM_ERR("backend(%s) no capture capability", private_module->module_data->name);
324 *error = TDM_ERROR_NO_CAPABILITY;
328 if (!(private_module->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_LAYER)) {
329 TDM_ERR("backend(%s) no layer capture capability", private_module->module_data->name);
331 *error = TDM_ERROR_NO_CAPABILITY;
335 capture_module = func_layer->layer_create_capture(private_layer->layer_backend,
337 if (ret != TDM_ERROR_NONE)
340 private_capture = calloc(1, sizeof(tdm_private_capture));
341 if (!private_capture) {
342 TDM_ERR("failed: alloc memory");
343 func_capture->capture_destroy(capture_module);
345 *error = TDM_ERROR_OUT_OF_MEMORY;
349 private_capture->stamp = tdm_helper_get_time();
350 while (_tdm_capture_find_object(private_display, private_capture->stamp))
351 private_capture->stamp++;
353 LIST_ADD(&private_capture->link, &private_layer->capture_list);
354 LIST_ADD(&private_capture->display_link, &private_module->capture_list);
356 private_capture->private_module = private_module;
358 private_capture->target = TDM_CAPTURE_TARGET_LAYER;
359 private_capture->private_display = private_display;
360 private_capture->private_output = private_output;
361 private_capture->private_layer = private_layer;
362 private_capture->capture_module = capture_module;
363 private_capture->owner_tid = syscall(SYS_gettid);
365 LIST_INITHEAD(&private_capture->pending_buffer_list);
366 LIST_INITHEAD(&private_capture->buffer_list);
368 TDM_DBG("capture(%p) create", private_capture);
371 *error = TDM_ERROR_NONE;
373 return private_capture;
378 tdm_capture_destroy_internal(tdm_private_capture *private_capture)
380 tdm_private_display *private_display;
381 tdm_private_module *private_module;
382 tdm_func_capture *func_capture;
383 tdm_capture_private_buffer *b = NULL, *bb = NULL;
384 struct list_head clone_list;
386 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
388 if (!private_capture)
391 private_display = private_capture->private_display;
393 tdm_thread_cb_remove(private_capture, TDM_THREAD_CB_CAPTURE_DONE, NULL, _tdm_capture_thread_cb_done, NULL);
395 LIST_DEL(&private_capture->link);
396 LIST_DEL(&private_capture->display_link);
398 private_module = private_capture->private_module;
399 func_capture = &private_module->func_capture;
400 func_capture->capture_destroy(private_capture->capture_module);
402 if (!LIST_IS_EMPTY(&private_capture->pending_buffer_list)) {
403 TDM_WRN("capture(%p) not finished:", private_capture);
404 _tdm_capture_print_list(&private_capture->pending_buffer_list);
406 LIST_INITHEAD(&clone_list);
407 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_capture->pending_buffer_list, link) {
409 LIST_ADDTAIL(&b->link, &clone_list);
412 _pthread_mutex_unlock(&private_display->lock);
413 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
416 if (tdm_ttrace_module & TDM_TTRACE_CAPTURE) {
417 tbm_bo bo = tbm_surface_internal_get_bo(b->buffer, 0);
418 TDM_TRACE_ASYNC_END((intptr_t)private_capture, "[CAPTURE] %d", tbm_bo_export(bo));
421 tdm_buffer_unref_backend(b->buffer);
424 _pthread_mutex_lock(&private_display->lock);
427 if (!LIST_IS_EMPTY(&private_capture->buffer_list)) {
428 TDM_WRN("capture(%p) not finished:", private_capture);
429 _tdm_capture_print_list(&private_capture->buffer_list);
431 LIST_INITHEAD(&clone_list);
432 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_capture->buffer_list, link) {
434 LIST_ADDTAIL(&b->link, &clone_list);
437 _pthread_mutex_unlock(&private_display->lock);
438 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
441 if (tdm_ttrace_module & TDM_TTRACE_CAPTURE) {
442 tbm_bo bo = tbm_surface_internal_get_bo(b->buffer, 0);
443 TDM_TRACE_ASYNC_END((intptr_t)private_capture, "[CAPTURE] %d", tbm_bo_export(bo));
446 tdm_buffer_unref_backend(b->buffer);
449 _pthread_mutex_lock(&private_display->lock);
452 private_capture->stamp = 0;
453 free(private_capture);
457 tdm_capture_destroy(tdm_capture *capture)
459 tdm_private_capture *private_capture = capture;
460 tdm_private_display *private_display;
462 if (!private_capture)
465 TDM_DBG("capture(%p) destroy", private_capture);
467 private_display = private_capture->private_display;
469 _pthread_mutex_lock(&private_display->lock);
470 tdm_capture_destroy_internal(private_capture);
471 _pthread_mutex_unlock(&private_display->lock);
475 tdm_capture_set_info(tdm_capture *capture, tdm_info_capture *info)
477 CAPTURE_FUNC_ENTRY();
479 TDM_RETURN_VAL_IF_FAIL(info != NULL, TDM_ERROR_INVALID_PARAMETER);
480 TDM_RETURN_VAL_IF_FAIL(info->type != 0, TDM_ERROR_INVALID_PARAMETER);
482 _pthread_mutex_lock(&private_display->lock);
484 if (!func_capture->capture_set_info) {
485 /* LCOV_EXCL_START */
486 _pthread_mutex_unlock(&private_display->lock);
487 TDM_DBG("failed: not implemented!!");
488 return TDM_ERROR_NOT_IMPLEMENTED;
492 if (info->type == TDM_CAPTURE_TYPE_STREAM && info->frequency == 0) {
493 tdm_private_output *private_output = private_capture->private_output;
494 info->frequency = private_output->current_mode->vrefresh;
497 TDM_INFO("capture(%p) info: dst(%ux%u %u,%u %ux%u %c%c%c%c) trans(%d) type(%d) freq(%d) flags(%x)",
499 info->dst_config.size.h, info->dst_config.size.v,
500 info->dst_config.pos.x, info->dst_config.pos.y,
501 info->dst_config.pos.w, info->dst_config.pos.h,
502 FOURCC_STR(info->dst_config.format),
503 info->transform, info->type, info->frequency, info->flags);
505 ret = func_capture->capture_set_info(private_capture->capture_module, info);
506 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
508 private_capture->info = *info;
510 _pthread_mutex_unlock(&private_display->lock);
516 tdm_capture_set_done_handler(tdm_capture *capture, tdm_capture_done_handler func, void *user_data)
518 tdm_private_display *private_display;
519 tdm_private_capture *private_capture;
520 tdm_error ret = TDM_ERROR_NONE;
522 TDM_RETURN_VAL_IF_FAIL(capture != NULL, TDM_ERROR_INVALID_PARAMETER);
524 private_capture = (tdm_private_capture*)capture;
525 private_display = private_capture->private_display;
527 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
529 _pthread_mutex_lock(&private_display->lock);
531 private_capture->done_func = func;
532 private_capture->done_user_data = user_data;
534 _pthread_mutex_unlock(&private_display->lock);
540 tdm_capture_attach(tdm_capture *capture, tbm_surface_h buffer)
542 tdm_capture_private_buffer *capture_buffer;
544 CAPTURE_FUNC_ENTRY();
546 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
548 _pthread_mutex_lock(&private_display->lock);
550 if (!func_capture->capture_attach) {
551 /* LCOV_EXCL_START */
552 _pthread_mutex_unlock(&private_display->lock);
553 TDM_DBG("failed: not implemented!!");
554 return TDM_ERROR_NOT_IMPLEMENTED;
558 if (tdm_module_check_abi(private_module, 1, 2) &&
559 private_module->caps_capture.max_attach_count > 0) {
560 /* LCOV_EXCL_START */
561 int length = LIST_LENGTH(&private_capture->pending_buffer_list) +
562 LIST_LENGTH(&private_capture->buffer_list);
563 if (length >= private_module->caps_capture.max_attach_count) {
564 _pthread_mutex_unlock(&private_display->lock);
565 TDM_DBG("failed: backend(%s) too many attached!! max_attach_count(%d)",
566 private_module->module_data->name, private_module->caps_capture.max_attach_count);
567 return TDM_ERROR_BAD_REQUEST;
572 capture_buffer = calloc(1, sizeof * capture_buffer);
573 if (!capture_buffer) {
574 /* LCOV_EXCL_START */
575 _pthread_mutex_unlock(&private_display->lock);
576 TDM_ERR("alloc failed");
577 return TDM_ERROR_OUT_OF_MEMORY;
581 ret = func_capture->capture_attach(private_capture->capture_module, buffer);
582 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
584 if (ret != TDM_ERROR_NONE) {
585 /* LCOV_EXCL_START */
586 free(capture_buffer);
587 _pthread_mutex_unlock(&private_display->lock);
588 TDM_ERR("attach failed");
593 LIST_ADDTAIL(&capture_buffer->link, &private_capture->pending_buffer_list);
594 capture_buffer->buffer = tdm_buffer_ref_backend(buffer);
596 if (tdm_debug_module & TDM_DEBUG_BUFFER) {
597 TDM_INFO("capture(%p) attached:", private_capture);
598 _tdm_capture_print_list(&private_capture->pending_buffer_list);
599 _tdm_capture_print_list(&private_capture->buffer_list);
602 if (tdm_ttrace_module & TDM_TTRACE_CAPTURE) {
603 tbm_bo bo = tbm_surface_internal_get_bo(buffer, 0);
604 TDM_TRACE_ASYNC_BEGIN((intptr_t)capture, "[CAPTURE] %d", tbm_bo_export(bo));
607 _pthread_mutex_unlock(&private_display->lock);
613 tdm_capture_commit(tdm_capture *capture)
615 tdm_capture_private_buffer *b = NULL, *bb = NULL;
616 tdm_private_output *private_output;
617 struct list_head commit_buffer_list;
619 CAPTURE_FUNC_ENTRY();
621 _pthread_mutex_lock(&private_display->lock);
623 private_output = private_capture->private_output;
625 /* TODO: possible when standby mode? can't decide it yet. no scenario. */
626 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
627 TDM_ERR("output(%d) dpms: %s", private_output->pipe,
628 tdm_dpms_str(private_output->current_dpms_value));
629 _pthread_mutex_unlock(&private_display->lock);
630 return TDM_ERROR_BAD_REQUEST;
633 if (!func_capture->capture_commit) {
634 /* LCOV_EXCL_START */
635 _pthread_mutex_unlock(&private_display->lock);
636 TDM_DBG("failed: not implemented!!");
637 return TDM_ERROR_NOT_IMPLEMENTED;
641 LIST_INITHEAD(&commit_buffer_list);
643 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_capture->pending_buffer_list, link) {
645 LIST_ADDTAIL(&b->link, &private_capture->buffer_list);
646 LIST_ADDTAIL(&b->commit_link, &commit_buffer_list);
649 if (tdm_debug_module & TDM_DEBUG_BUFFER) {
650 TDM_INFO("capture(%p) committed:", private_capture);
651 _tdm_capture_print_list(&private_capture->pending_buffer_list);
652 _tdm_capture_print_list(&private_capture->buffer_list);
655 ret = func_capture->capture_commit(private_capture->capture_module);
656 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
658 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &commit_buffer_list, commit_link) {
659 LIST_DELINIT(&b->commit_link);
661 if (!_tdm_capture_find_buffer(&private_capture->buffer_list, b))
664 if (ret != TDM_ERROR_NONE) {
665 /* LCOV_EXCL_START */
667 _pthread_mutex_unlock(&private_display->lock);
668 tdm_buffer_unref_backend(b->buffer);
669 _pthread_mutex_lock(&private_display->lock);
677 _pthread_mutex_unlock(&private_display->lock);