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 CAPTURE_FUNC_ENTRY() \
46 tdm_func_capture *func_capture; \
47 tdm_private_display *private_display; \
48 tdm_private_capture *private_capture; \
49 tdm_error ret = TDM_ERROR_NONE; \
50 TDM_RETURN_VAL_IF_FAIL(capture != NULL, TDM_ERROR_INVALID_PARAMETER); \
51 private_capture = (tdm_private_capture*)capture; \
52 private_display = private_capture->private_display; \
53 func_capture = &private_display->func_capture
56 _tdm_capture_print_list(struct list_head *list)
58 tdm_capture_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)", b->buffer);
74 TDM_INFO("\t %s", str);
77 static tdm_capture_private_buffer *
78 _tdm_capture_find_tbm_buffer(struct list_head *list, tbm_surface_h buffer)
80 tdm_capture_private_buffer *b = NULL, *bb = NULL;
82 LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
83 if (b->buffer == buffer)
90 static tdm_capture_private_buffer *
91 _tdm_capture_find_buffer(struct list_head *list, tdm_capture_private_buffer *capture_buffer)
93 tdm_capture_private_buffer *b = NULL, *bb = NULL;
95 LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
96 if (b == capture_buffer)
104 tdm_capture_cb_done(tdm_capture *capture_backend, tbm_surface_h buffer,
107 tdm_private_capture *private_capture = user_data;
108 tdm_private_display *private_display = private_capture->private_display;
109 tdm_capture_private_buffer *capture_buffer = NULL, *first_entry = NULL;
111 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
113 if (private_capture->owner_tid != syscall(SYS_gettid)) {
114 tdm_thread_cb_capture_done capture_done;
117 capture_done.base.type = TDM_THREAD_CB_CAPTURE_DONE;
118 capture_done.base.length = sizeof capture_done;
119 capture_done.capture_stamp = private_capture->stamp;
120 capture_done.buffer = buffer;
121 capture_done.user_data = user_data;
123 ret = tdm_thread_send_cb(private_display->private_loop, &capture_done.base);
124 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
129 if (private_capture->owner_tid != syscall(SYS_gettid))
130 TDM_NEVER_GET_HERE();
132 if (tdm_debug_dump & TDM_DUMP_FLAG_CAPTURE) {
133 /* LCOV_EXCL_START */
134 char str[TDM_PATH_LEN];
136 snprintf(str, TDM_PATH_LEN, "capture_%03d", i++);
137 tdm_helper_dump_buffer_str(buffer, tdm_debug_dump_dir, str);
141 if (tdm_debug_module & TDM_DEBUG_BUFFER)
142 TDM_INFO("capture(%p) done: %p", private_capture, buffer);
144 if (!LIST_IS_EMPTY(&private_capture->buffer_list)) {
145 first_entry = container_of((&private_capture->buffer_list)->next, capture_buffer, link);
146 if (first_entry->buffer != buffer)
147 TDM_ERR("buffer(%p) is skipped", first_entry->buffer);
149 TDM_NEVER_GET_HERE();
152 if ((capture_buffer = _tdm_capture_find_tbm_buffer(&private_capture->buffer_list, buffer))) {
153 LIST_DEL(&capture_buffer->link);
154 LIST_DELINIT(&capture_buffer->commit_link);
156 _pthread_mutex_unlock(&private_display->lock);
157 if (private_capture->done_func)
158 private_capture->done_func(capture_buffer, buffer, private_capture->done_user_data);
159 tdm_buffer_unref_backend(buffer);
160 _pthread_mutex_lock(&private_display->lock);
162 free(capture_buffer);
166 INTERN tdm_private_capture *
167 tdm_capture_find_stamp(tdm_private_display *private_display, double stamp)
169 tdm_private_capture *private_capture = NULL;
171 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
173 LIST_FOR_EACH_ENTRY(private_capture, &private_display->capture_list, display_link) {
174 if (private_capture->stamp == stamp)
175 return private_capture;
181 INTERN tdm_private_capture *
182 tdm_capture_create_output_internal(tdm_private_output *private_output,
185 tdm_private_display *private_display = private_output->private_display;
186 tdm_func_output *func_output = &private_display->func_output;
187 tdm_func_capture *func_capture = &private_display->func_capture;
188 tdm_private_capture *private_capture = NULL;
189 tdm_capture *capture_backend = NULL;
190 tdm_error ret = TDM_ERROR_NONE;
192 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
194 if (!(private_display->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE)) {
195 /* LCOV_EXCL_START */
196 TDM_ERR("no capture capability");
198 *error = TDM_ERROR_NO_CAPABILITY;
203 if (!(private_display->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_OUTPUT)) {
204 /* LCOV_EXCL_START */
205 TDM_ERR("no output capture capability");
207 *error = TDM_ERROR_NO_CAPABILITY;
212 capture_backend = func_output->output_create_capture(
213 private_output->output_backend, &ret);
214 if (ret != TDM_ERROR_NONE) {
215 /* LCOV_EXCL_START */
222 private_capture = calloc(1, sizeof(tdm_private_capture));
223 if (!private_capture) {
224 /* LCOV_EXCL_START */
225 TDM_ERR("failed: alloc memory");
226 func_capture->capture_destroy(capture_backend);
228 *error = TDM_ERROR_OUT_OF_MEMORY;
233 ret = func_capture->capture_set_done_handler(capture_backend,
234 tdm_capture_cb_done, private_capture);
235 if (ret != TDM_ERROR_NONE) {
236 /* LCOV_EXCL_START */
237 TDM_ERR("capture(%p) set capture_done_handler failed", private_capture);
238 func_capture->capture_destroy(capture_backend);
245 private_capture->stamp = tdm_helper_get_time();
246 while (tdm_capture_find_stamp(private_display, private_capture->stamp))
247 private_capture->stamp++;
249 LIST_ADD(&private_capture->link, &private_output->capture_list);
250 LIST_ADD(&private_capture->display_link, &private_display->capture_list);
252 private_capture->target = TDM_CAPTURE_TARGET_OUTPUT;
253 private_capture->private_display = private_display;
254 private_capture->private_output = private_output;
255 private_capture->private_layer = NULL;
256 private_capture->capture_backend = capture_backend;
257 private_capture->owner_tid = syscall(SYS_gettid);
259 LIST_INITHEAD(&private_capture->pending_buffer_list);
260 LIST_INITHEAD(&private_capture->buffer_list);
262 TDM_DBG("capture(%p) create", private_capture);
265 *error = TDM_ERROR_NONE;
267 return private_capture;
270 /* LCOV_EXCL_START */
271 INTERN tdm_private_capture *
272 tdm_capture_create_layer_internal(tdm_private_layer *private_layer,
275 tdm_private_output *private_output = private_layer->private_output;
276 tdm_private_display *private_display = private_output->private_display;
277 tdm_func_layer *func_layer = &private_display->func_layer;
278 tdm_func_capture *func_capture = &private_display->func_capture;
279 tdm_private_capture *private_capture = NULL;
280 tdm_capture *capture_backend = NULL;
281 tdm_error ret = TDM_ERROR_NONE;
283 TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
285 if (!(private_display->capabilities & TDM_DISPLAY_CAPABILITY_CAPTURE)) {
286 TDM_ERR("no capture capability");
288 *error = TDM_ERROR_NO_CAPABILITY;
292 if (!(private_display->caps_capture.capabilities & TDM_CAPTURE_CAPABILITY_LAYER)) {
293 TDM_ERR("no layer capture capability");
295 *error = TDM_ERROR_NO_CAPABILITY;
299 capture_backend = func_layer->layer_create_capture(private_layer->layer_backend,
301 if (ret != TDM_ERROR_NONE)
304 private_capture = calloc(1, sizeof(tdm_private_capture));
305 if (!private_capture) {
306 TDM_ERR("failed: alloc memory");
307 func_capture->capture_destroy(capture_backend);
309 *error = TDM_ERROR_OUT_OF_MEMORY;
313 private_capture->stamp = tdm_helper_get_time();
314 while (tdm_capture_find_stamp(private_display, private_capture->stamp))
315 private_capture->stamp++;
317 LIST_ADD(&private_capture->link, &private_layer->capture_list);
318 LIST_ADD(&private_capture->display_link, &private_display->capture_list);
320 private_capture->target = TDM_CAPTURE_TARGET_LAYER;
321 private_capture->private_display = private_display;
322 private_capture->private_output = private_output;
323 private_capture->private_layer = private_layer;
324 private_capture->capture_backend = capture_backend;
325 private_capture->owner_tid = syscall(SYS_gettid);
327 LIST_INITHEAD(&private_capture->pending_buffer_list);
328 LIST_INITHEAD(&private_capture->buffer_list);
330 TDM_DBG("capture(%p) create", private_capture);
333 *error = TDM_ERROR_NONE;
335 return private_capture;
340 tdm_capture_destroy_internal(tdm_private_capture *private_capture)
342 tdm_private_display *private_display;
343 tdm_func_capture *func_capture;
344 tdm_capture_private_buffer *b = NULL, *bb = NULL;
345 struct list_head clone_list;
347 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
349 if (!private_capture)
352 private_display = private_capture->private_display;
354 LIST_DEL(&private_capture->link);
355 LIST_DEL(&private_capture->display_link);
357 func_capture = &private_capture->private_display->func_capture;
358 func_capture->capture_destroy(private_capture->capture_backend);
360 if (!LIST_IS_EMPTY(&private_capture->pending_buffer_list)) {
361 TDM_WRN("capture(%p) not finished:", private_capture);
362 _tdm_capture_print_list(&private_capture->pending_buffer_list);
364 LIST_INITHEAD(&clone_list);
365 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_capture->pending_buffer_list, link) {
367 LIST_ADDTAIL(&b->link, &clone_list);
370 _pthread_mutex_unlock(&private_display->lock);
371 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
373 tdm_buffer_unref_backend(b->buffer);
376 _pthread_mutex_lock(&private_display->lock);
379 if (!LIST_IS_EMPTY(&private_capture->buffer_list)) {
380 TDM_WRN("capture(%p) not finished:", private_capture);
381 _tdm_capture_print_list(&private_capture->buffer_list);
383 LIST_INITHEAD(&clone_list);
384 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_capture->buffer_list, link) {
386 LIST_ADDTAIL(&b->link, &clone_list);
389 _pthread_mutex_unlock(&private_display->lock);
390 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
392 tdm_buffer_unref_backend(b->buffer);
395 _pthread_mutex_lock(&private_display->lock);
398 private_capture->stamp = 0;
399 free(private_capture);
403 tdm_capture_destroy(tdm_capture *capture)
405 tdm_private_capture *private_capture = capture;
406 tdm_private_display *private_display;
408 if (!private_capture)
411 TDM_DBG("capture(%p) destroy", private_capture);
413 private_display = private_capture->private_display;
415 _pthread_mutex_lock(&private_display->lock);
416 tdm_capture_destroy_internal(private_capture);
417 _pthread_mutex_unlock(&private_display->lock);
421 tdm_capture_set_info(tdm_capture *capture, tdm_info_capture *info)
423 CAPTURE_FUNC_ENTRY();
425 TDM_RETURN_VAL_IF_FAIL(info != NULL, TDM_ERROR_INVALID_PARAMETER);
426 TDM_RETURN_VAL_IF_FAIL(info->type != 0, TDM_ERROR_INVALID_PARAMETER);
428 _pthread_mutex_lock(&private_display->lock);
430 if (!func_capture->capture_set_info) {
431 /* LCOV_EXCL_START */
432 _pthread_mutex_unlock(&private_display->lock);
433 TDM_DBG("failed: not implemented!!");
434 return TDM_ERROR_NOT_IMPLEMENTED;
438 if (info->type == TDM_CAPTURE_TYPE_STREAM && info->frequency == 0) {
439 tdm_private_output *private_output = private_capture->private_output;
440 info->frequency = private_output->current_mode->vrefresh;
443 TDM_INFO("capture(%p) info: dst(%dx%d %d,%d %dx%d %c%c%c%c) trans(%d) type(%d) freq(%d) flags(%x)",
445 info->dst_config.size.h, info->dst_config.size.v,
446 info->dst_config.pos.x, info->dst_config.pos.y,
447 info->dst_config.pos.w, info->dst_config.pos.h,
448 FOURCC_STR(info->dst_config.format),
449 info->transform, info->type, info->frequency, info->flags);
451 ret = func_capture->capture_set_info(private_capture->capture_backend, info);
452 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
454 private_capture->info = *info;
456 _pthread_mutex_unlock(&private_display->lock);
462 tdm_capture_set_done_handler(tdm_capture *capture, tdm_capture_done_handler func, void *user_data)
464 tdm_private_display *private_display;
465 tdm_private_capture *private_capture;
466 tdm_error ret = TDM_ERROR_NONE;
468 TDM_RETURN_VAL_IF_FAIL(capture != NULL, TDM_ERROR_INVALID_PARAMETER);
470 private_capture = (tdm_private_capture*)capture;
471 private_display = private_capture->private_display;
473 TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
475 _pthread_mutex_lock(&private_display->lock);
477 private_capture->done_func = func;
478 private_capture->done_user_data = user_data;
480 _pthread_mutex_unlock(&private_display->lock);
486 tdm_capture_attach(tdm_capture *capture, tbm_surface_h buffer)
488 tdm_capture_private_buffer *capture_buffer;
490 CAPTURE_FUNC_ENTRY();
492 TDM_RETURN_VAL_IF_FAIL(buffer != NULL, TDM_ERROR_INVALID_PARAMETER);
494 _pthread_mutex_lock(&private_display->lock);
496 if (!func_capture->capture_attach) {
497 /* LCOV_EXCL_START */
498 _pthread_mutex_unlock(&private_display->lock);
499 TDM_DBG("failed: not implemented!!");
500 return TDM_ERROR_NOT_IMPLEMENTED;
504 if (tdm_display_check_module_abi(private_display, 1, 2) &&
505 private_display->caps_capture.max_attach_count > 0) {
506 /* LCOV_EXCL_START */
507 int length = LIST_LENGTH(&private_capture->pending_buffer_list) +
508 LIST_LENGTH(&private_capture->buffer_list);
509 if (length >= private_display->caps_capture.max_attach_count) {
510 _pthread_mutex_unlock(&private_display->lock);
511 TDM_DBG("failed: too many attached!! max_attach_count(%d)",
512 private_display->caps_capture.max_attach_count);
513 return TDM_ERROR_BAD_REQUEST;
518 capture_buffer = calloc(1, sizeof * capture_buffer);
519 if (!capture_buffer) {
520 /* LCOV_EXCL_START */
521 _pthread_mutex_unlock(&private_display->lock);
522 TDM_ERR("alloc failed");
523 return TDM_ERROR_OUT_OF_MEMORY;
527 ret = func_capture->capture_attach(private_capture->capture_backend, buffer);
528 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
530 if (ret != TDM_ERROR_NONE) {
531 /* LCOV_EXCL_START */
532 free(capture_buffer);
533 _pthread_mutex_unlock(&private_display->lock);
534 TDM_ERR("attach failed");
539 LIST_ADDTAIL(&capture_buffer->link, &private_capture->pending_buffer_list);
540 capture_buffer->buffer = tdm_buffer_ref_backend(buffer);
542 if (tdm_debug_module & TDM_DEBUG_BUFFER) {
543 TDM_INFO("capture(%p) attached:", private_capture);
544 tdm_buffer_list_dump(&private_capture->buffer_list);
547 _pthread_mutex_unlock(&private_display->lock);
553 tdm_capture_commit(tdm_capture *capture)
555 tdm_capture_private_buffer *b = NULL, *bb = NULL;
556 tdm_private_output *private_output;
557 struct list_head commit_buffer_list;
559 CAPTURE_FUNC_ENTRY();
561 _pthread_mutex_lock(&private_display->lock);
563 private_output = private_capture->private_output;
564 if (private_output->current_dpms_value > TDM_OUTPUT_DPMS_ON) {
565 TDM_ERR("output(%d) dpms: %s", private_output->pipe,
566 tdm_dpms_str(private_output->current_dpms_value));
567 _pthread_mutex_unlock(&private_display->lock);
568 return TDM_ERROR_BAD_REQUEST;
571 if (!func_capture->capture_commit) {
572 /* LCOV_EXCL_START */
573 _pthread_mutex_unlock(&private_display->lock);
574 TDM_DBG("failed: not implemented!!");
575 return TDM_ERROR_NOT_IMPLEMENTED;
579 LIST_INITHEAD(&commit_buffer_list);
581 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_capture->pending_buffer_list, link) {
583 LIST_ADDTAIL(&b->link, &private_capture->buffer_list);
584 LIST_ADDTAIL(&b->commit_link, &commit_buffer_list);
587 ret = func_capture->capture_commit(private_capture->capture_backend);
588 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
590 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &commit_buffer_list, commit_link) {
591 LIST_DELINIT(&b->commit_link);
593 if (!_tdm_capture_find_buffer(&private_capture->buffer_list, b))
596 if (ret != TDM_ERROR_NONE) {
597 /* LCOV_EXCL_START */
598 /* Not to call the user release handler when failed.
599 * Do we have to call this function here really?
600 * User better use set_done_handler to know when pp is done. Using
601 * buffer_release_handler is not good.
603 tdm_buffer_remove_release_handler_internal(b->buffer);
605 _pthread_mutex_unlock(&private_display->lock);
606 tdm_buffer_unref_backend(b->buffer);
607 _pthread_mutex_lock(&private_display->lock);
615 _pthread_mutex_unlock(&private_display->lock);