1 /**************************************************************************
5 * Copyright 2018 Samsung Electronics co., Ltd. All Rights Reserved.
7 * Contact: SooChan Lim <sc1.lim@samsung.com>,
8 * Boram Park <boram1288.park@samsung.com>,
9 * Changyeon Lee <cyeon.lee@samsung.com>,
10 * Sangjin Lee <lsj119@samsung.com>
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the
14 * "Software"), to deal in the Software without restriction, including
15 * without limitation the rights to use, copy, modify, merge, publish,
16 * distribute, sub license, and/or sell copies of the Software, and to
17 * permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
27 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
29 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
30 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 **************************************************************************/
38 #include "tdm_private.h"
40 #define HWC_FUNC_ENTRY() \
41 tdm_private_display *private_display; \
42 tdm_private_output *private_output; \
43 tdm_private_hwc *private_hwc; \
44 tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
45 TDM_RETURN_VAL_IF_FAIL(hwc != NULL, TDM_ERROR_INVALID_PARAMETER); \
46 private_hwc = (tdm_private_hwc*)hwc; \
47 private_output = private_hwc->private_output; \
48 TDM_RETURN_VAL_IF_FAIL(private_output != NULL, TDM_ERROR_INVALID_PARAMETER); \
49 private_display = private_output->private_display
51 #define HWC_FUNC_ENTRY_ERROR() \
52 tdm_private_display *private_display; \
53 tdm_private_output *private_output; \
54 tdm_private_hwc *private_hwc; \
55 tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
56 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(hwc != NULL, TDM_ERROR_INVALID_PARAMETER, NULL); \
57 private_hwc = (tdm_private_hwc*)hwc; \
58 private_output = private_hwc->private_output; \
59 TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(private_output != NULL, TDM_ERROR_INVALID_PARAMETER, NULL); \
60 private_display = private_output->private_display
62 #define HWC_FUNC_ENTRY_VOID_RETURN() \
63 tdm_private_display *private_display; \
64 tdm_private_output *private_output; \
65 tdm_private_hwc *private_hwc; \
66 tdm_error ret = TDM_ERROR_NONE; /* default TDM_ERROR_NONE */\
67 TDM_RETURN_IF_FAIL(hwc != NULL); \
68 private_hwc = (tdm_private_hwc*)hwc; \
69 private_output = private_hwc->private_output; \
70 TDM_RETURN_IF_FAIL(private_output != NULL); \
71 private_display = private_output->private_display
74 static tdm_private_hwc_window *
75 _tdm_hwc_find_private_hwc_window(tdm_private_hwc *private_hwc, tdm_hwc_window *hwc_window_backend)
77 tdm_private_hwc_window *private_hwc_window = NULL;
79 LIST_FOR_EACH_ENTRY(private_hwc_window, &private_hwc->hwc_window_list, link) {
80 if (private_hwc_window->hwc_window_backend == hwc_window_backend)
81 return private_hwc_window;
88 _tdm_hwc_thread_cb_commit(tdm_private_display *private_display, void *object,
89 tdm_thread_cb_base *cb_base, void *user_data)
91 tdm_thread_cb_hwc_commit *hwc_commit = (tdm_thread_cb_hwc_commit *)cb_base;
92 tdm_private_hwc_commit_handler *hwc_commit_handler = hwc_commit->base.data;
93 tdm_private_hwc *private_hwc = object;
95 TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
97 if (!hwc_commit_handler)
100 assert(hwc_commit_handler->owner_tid == syscall(SYS_gettid));
102 tdm_thread_cb_remove(private_hwc, TDM_THREAD_CB_HWC_COMMIT, hwc_commit_handler,
103 _tdm_hwc_thread_cb_commit, NULL);
105 LIST_DEL(&hwc_commit_handler->link);
107 if (tdm_debug_module & TDM_DEBUG_COMMIT) {
108 TDM_INFO("----------------------------------------- hwc(%d) committed", private_hwc->index);
109 TDM_INFO("handler(%p)", hwc_commit_handler);
112 if (hwc_commit_handler->func) {
113 _pthread_mutex_unlock(&private_display->lock);
114 hwc_commit_handler->func(private_hwc,
115 hwc_commit->sequence,
118 hwc_commit_handler->user_data);
119 _pthread_mutex_lock(&private_display->lock);
122 free(hwc_commit_handler);
124 if (tdm_debug_module & TDM_DEBUG_COMMIT)
125 TDM_INFO("-----------------------------------------...");
129 _tdm_hwc_cb_commit(tdm_hwc *hwc_backend, unsigned int sequence,
130 unsigned int tv_sec, unsigned int tv_usec, void *user_data)
132 tdm_private_hwc_commit_handler *hwc_commit_handler = user_data;
133 tdm_private_hwc *private_hwc;
134 tdm_thread_cb_hwc_commit hwc_commit;
137 if (hwc_commit_handler)
138 private_hwc = hwc_commit_handler->private_hwc;
140 private_hwc = tdm_display_find_private_hwc(tdm_display_get(), hwc_backend);
142 memset(&hwc_commit, 0, sizeof hwc_commit);
143 hwc_commit.base.type = TDM_THREAD_CB_HWC_COMMIT;
144 hwc_commit.base.length = sizeof hwc_commit;
145 hwc_commit.base.object_stamp = private_hwc->stamp;
146 hwc_commit.base.data = hwc_commit_handler;
147 hwc_commit.base.sync = 0;
148 hwc_commit.sequence = sequence;
149 hwc_commit.tv_sec = tv_sec;
150 hwc_commit.tv_usec = tv_usec;
152 ret = tdm_thread_cb_call(private_hwc, &hwc_commit.base, 1);
153 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
157 tdm_hwc_init(tdm_private_display *private_display)
159 tdm_thread_cb_set_find_func(TDM_THREAD_CB_HWC_COMMIT, tdm_display_find_hwc_stamp);
161 return TDM_ERROR_NONE;
164 EXTERN tdm_hwc_window *
165 tdm_hwc_create_window(tdm_hwc *hwc, tdm_error *error)
167 tdm_hwc_window *hwc_window = NULL;
169 HWC_FUNC_ENTRY_ERROR();
171 _pthread_mutex_lock(&private_display->lock);
173 hwc_window = (tdm_hwc_window *)tdm_hwc_window_create_internal(private_hwc, error);
175 _pthread_mutex_unlock(&private_display->lock);
181 tdm_hwc_get_supported_formats(tdm_hwc *hwc, const tbm_format **formats, int *count)
183 tdm_private_module *private_module;
184 tdm_func_hwc *func_hwc;
188 TDM_RETURN_VAL_IF_FAIL(formats != NULL, TDM_ERROR_INVALID_PARAMETER);
189 TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
191 _pthread_mutex_lock(&private_display->lock);
193 private_module = private_output->private_module;
194 func_hwc = &private_module->func_hwc;
196 if (!func_hwc->hwc_get_supported_formats) {
197 /* LCOV_EXCL_START */
198 _pthread_mutex_unlock(&private_display->lock);
199 TDM_WRN("not implemented!!");
200 return TDM_ERROR_NOT_IMPLEMENTED;
204 ret = func_hwc->hwc_get_supported_formats(private_hwc->hwc_backend, formats, count);
206 _pthread_mutex_unlock(&private_display->lock);
212 tdm_hwc_get_available_properties(tdm_hwc *hwc, const tdm_prop **props, int *count)
214 tdm_private_module *private_module;
215 tdm_func_hwc *func_hwc = NULL;
219 TDM_RETURN_VAL_IF_FAIL(props != NULL, TDM_ERROR_INVALID_PARAMETER);
220 TDM_RETURN_VAL_IF_FAIL(count != NULL, TDM_ERROR_INVALID_PARAMETER);
222 _pthread_mutex_lock(&private_display->lock);
224 private_module = private_output->private_module;
225 func_hwc = &private_module->func_hwc;
227 if (!func_hwc->hwc_get_available_properties) {
228 /* LCOV_EXCL_START */
229 _pthread_mutex_unlock(&private_display->lock);
230 TDM_WRN("not implemented!!");
231 return TDM_ERROR_NOT_IMPLEMENTED;
235 ret = func_hwc->hwc_get_available_properties(private_hwc->hwc_backend, props, count);
237 _pthread_mutex_unlock(&private_display->lock);
242 EXTERN tbm_surface_queue_h
243 tdm_hwc_get_client_target_buffer_queue(tdm_hwc *hwc, tdm_error *error)
245 tdm_private_module *private_module;
246 tdm_func_hwc *func_hwc = NULL;
247 tbm_surface_queue_h queue = NULL;
249 HWC_FUNC_ENTRY_ERROR();
251 _pthread_mutex_lock(&private_display->lock);
253 private_module = private_hwc->private_module;
254 func_hwc = &private_module->func_hwc;
256 if (!func_hwc->hwc_get_client_target_buffer_queue) {
257 /* LCOV_EXCL_START */
258 _pthread_mutex_unlock(&private_display->lock);
259 TDM_WRN("not implemented!!");
264 queue = func_hwc->hwc_get_client_target_buffer_queue(private_hwc->hwc_backend, error);
266 _pthread_mutex_unlock(&private_display->lock);
272 tdm_hwc_set_client_target_buffer(tdm_hwc *hwc, tbm_surface_h target_buffer, tdm_region damage)
274 tdm_private_module *private_module;
275 tdm_func_hwc *func_hwc = NULL;
279 _pthread_mutex_lock(&private_display->lock);
281 if (tdm_debug_dump & TDM_DUMP_FLAG_WINDOW) {
282 /* LCOV_EXCL_START */
283 char str[TDM_PATH_LEN];
285 snprintf(str, TDM_PATH_LEN, "target_window_%03d", i++);
286 tdm_helper_dump_buffer_str(target_buffer, tdm_debug_dump_dir, str);
290 private_module = private_hwc->private_module;
291 func_hwc = &private_module->func_hwc;
293 if (!func_hwc->hwc_set_client_target_buffer) {
294 /* LCOV_EXCL_START */
295 _pthread_mutex_unlock(&private_display->lock);
296 TDM_WRN("not implemented!!");
297 return TDM_ERROR_NOT_IMPLEMENTED;
301 ret = func_hwc->hwc_set_client_target_buffer(private_hwc->hwc_backend, target_buffer, damage);
303 _pthread_mutex_unlock(&private_display->lock);
310 tdm_hwc_validate(tdm_hwc *hwc, tdm_hwc_window **composited_wnds, uint32_t num_wnds, uint32_t *num_types)
312 tdm_private_module *private_module;
313 tdm_func_hwc *func_hwc = NULL;
314 tdm_private_hwc_window **composited_wnds_frontend = NULL;
315 tdm_hwc_window **composited_wnds_backend = NULL;
320 TDM_RETURN_VAL_IF_FAIL(num_types != NULL, TDM_ERROR_INVALID_PARAMETER);
322 _pthread_mutex_lock(&private_display->lock);
324 private_module = private_hwc->private_module;
325 func_hwc = &private_module->func_hwc;
327 if (!func_hwc->hwc_validate) {
328 /* LCOV_EXCL_START */
329 _pthread_mutex_unlock(&private_display->lock);
330 TDM_WRN("not implemented!!");
331 return TDM_ERROR_NOT_IMPLEMENTED;
336 ret = func_hwc->hwc_validate(private_hwc->hwc_backend, NULL, 0, num_types);
338 _pthread_mutex_unlock(&private_display->lock);
342 composited_wnds_backend = calloc(num_wnds, sizeof(tdm_hwc_window *));
343 if (!composited_wnds_backend) {
344 /* LCOV_EXCL_START */
345 _pthread_mutex_unlock(&private_display->lock);
346 return TDM_ERROR_OUT_OF_MEMORY;
350 composited_wnds_frontend = (tdm_private_hwc_window **)composited_wnds;
352 for (i = 0; i < num_wnds; i++)
353 composited_wnds_backend[i] = composited_wnds_frontend[i]->hwc_window_backend;
355 ret = func_hwc->hwc_validate(private_hwc->hwc_backend, composited_wnds_backend,
356 num_wnds, num_types);
358 free(composited_wnds_backend);
360 _pthread_mutex_unlock(&private_display->lock);
366 tdm_hwc_get_changed_composition_types(tdm_hwc *hwc, uint32_t *num_elements,
367 tdm_hwc_window **hwc_window,
368 tdm_hwc_window_composition *composition_types)
370 tdm_private_module *private_module;
371 tdm_func_hwc *func_hwc = NULL;
372 tdm_private_hwc_window * private_hwc_window = NULL;
377 TDM_RETURN_VAL_IF_FAIL(num_elements != NULL, TDM_ERROR_INVALID_PARAMETER);
379 _pthread_mutex_lock(&private_display->lock);
381 private_module = private_hwc->private_module;
382 func_hwc = &private_module->func_hwc;
384 if (!func_hwc->hwc_get_changed_composition_types) {
385 /* LCOV_EXCL_START */
386 _pthread_mutex_unlock(&private_display->lock);
387 TDM_WRN("not implemented!!");
388 return TDM_ERROR_NOT_IMPLEMENTED;
392 ret = func_hwc->hwc_get_changed_composition_types(private_hwc->hwc_backend,
393 num_elements, hwc_window, composition_types);
394 if (ret != TDM_ERROR_NONE) {
395 /* LCOV_EXCL_START */
396 _pthread_mutex_unlock(&private_display->lock);
401 if (hwc_window == NULL || composition_types == NULL) {
402 _pthread_mutex_unlock(&private_display->lock);
403 return TDM_ERROR_NONE;
406 for (i = 0; i < *num_elements; i++) {
407 private_hwc_window = _tdm_hwc_find_private_hwc_window(private_hwc, hwc_window[i]);
408 if (private_hwc_window == NULL) {
409 /* LCOV_EXCL_START */
410 TDM_ERR("failed! This should never happen!");
411 tdm_hwc_window_destroy_internal(private_hwc_window);
413 _pthread_mutex_unlock(&private_display->lock);
414 return TDM_ERROR_OPERATION_FAILED;
418 hwc_window[i] = (tdm_hwc_window*)private_hwc_window;
421 _pthread_mutex_unlock(&private_display->lock);
427 tdm_hwc_accept_changes(tdm_hwc *hwc)
429 tdm_private_module *private_module;
430 tdm_func_hwc *func_hwc = NULL;
434 _pthread_mutex_lock(&private_display->lock);
436 private_module = private_hwc->private_module;
437 func_hwc = &private_module->func_hwc;
439 if (!func_hwc->hwc_validate) {
440 /* LCOV_EXCL_START */
441 _pthread_mutex_unlock(&private_display->lock);
442 TDM_WRN("not implemented!!");
443 return TDM_ERROR_NOT_IMPLEMENTED;
447 ret = func_hwc->hwc_accept_changes(private_hwc->hwc_backend);
449 _pthread_mutex_unlock(&private_display->lock);
455 tdm_hwc_commit(tdm_hwc *hwc, int sync, tdm_hwc_commit_handler func, void *user_data)
457 tdm_private_module *private_module;
458 tdm_func_hwc *func_hwc = NULL;
459 tdm_private_hwc_commit_handler *hwc_commit_handler = NULL;
463 _pthread_mutex_lock(&private_display->lock);
465 private_module = private_hwc->private_module;
466 func_hwc = &private_module->func_hwc;
468 if (!func_hwc->hwc_commit) {
469 /* LCOV_EXCL_START */
470 TDM_WRN("not implemented!!");
471 _pthread_mutex_unlock(&private_display->lock);
472 return TDM_ERROR_NOT_IMPLEMENTED;
476 //TODO: I am not sure yet whether we have to check the dpms at hwc_commit.
478 /* check if the dpms is off */
479 if (TDM_OUTPUT_DPMS_VSYNC_IS_OFF(private_output->current_dpms_value)) {
480 TDM_ERR("hwc(%d) dpms: %s", private_hwc->index,
481 tdm_dpms_str(private_output->current_dpms_value));
482 _pthread_mutex_unlock(&private_display->lock);
483 return TDM_ERROR_DPMS_OFF;
487 if (tdm_debug_module & TDM_DEBUG_COMMIT)
488 TDM_INFO("hwc(%d) commit", private_hwc->index);
490 if (!private_hwc->regist_commit_cb) {
491 private_hwc->regist_commit_cb = 1;
492 ret = func_hwc->hwc_set_commit_handler(private_hwc->hwc_backend, _tdm_hwc_cb_commit);
493 /* LCOV_EXCL_START */
494 if (ret != TDM_ERROR_NONE) {
495 private_hwc->regist_commit_cb = 0;
496 TDM_ERR("hwc(%d) fail to set hwc_set_commit_handler", private_hwc->index);
497 _pthread_mutex_unlock(&private_display->lock);
503 hwc_commit_handler = calloc(1, sizeof(tdm_private_hwc_commit_handler));
504 if (!hwc_commit_handler) {
505 /* LCOV_EXCL_START */
506 TDM_ERR("failed: alloc memory");
507 _pthread_mutex_unlock(&private_display->lock);
508 return TDM_ERROR_OUT_OF_MEMORY;
512 ret = tdm_thread_cb_add(private_hwc, TDM_THREAD_CB_HWC_COMMIT, hwc_commit_handler,
513 _tdm_hwc_thread_cb_commit, NULL);
514 if (ret != TDM_ERROR_NONE) {
515 TDM_ERR("tdm_thread_cb_add failed");
516 free(hwc_commit_handler);
517 _pthread_mutex_unlock(&private_display->lock);
521 LIST_ADDTAIL(&hwc_commit_handler->link, &private_hwc->hwc_commit_handler_list);
522 hwc_commit_handler->private_hwc = private_hwc;
523 hwc_commit_handler->func = func;
524 hwc_commit_handler->user_data = user_data;
525 hwc_commit_handler->owner_tid = syscall(SYS_gettid);
527 ret = func_hwc->hwc_commit(private_hwc->hwc_backend, sync, hwc_commit_handler);
528 TDM_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, commit_failed);
530 if (tdm_debug_module & TDM_DEBUG_COMMIT)
531 TDM_INFO("hwc(%d) backend commit: handle(%p) func(%p) user_data(%p)",
532 private_hwc->index, hwc_commit_handler, func, user_data);
534 _pthread_mutex_unlock(&private_display->lock);
539 /* LCOV_EXCL_START */
540 if (hwc_commit_handler) {
541 tdm_thread_cb_remove(private_hwc, TDM_THREAD_CB_HWC_COMMIT, hwc_commit_handler,
542 _tdm_hwc_thread_cb_commit, NULL);
543 LIST_DEL(&hwc_commit_handler->link);
544 free(hwc_commit_handler);
547 _pthread_mutex_unlock(&private_display->lock);