a58e4fc606d8b7cd9896c29d13368090abb1dc6b
[platform/core/uifw/libtdm.git] / src / tdm_pp.c
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
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>
13  *
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:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
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.
33  *
34 **************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "tdm.h"
41 #include "tdm_backend.h"
42 #include "tdm_private.h"
43 #include "tdm_helper.h"
44
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
54
55 static void
56 _tdm_pp_print_list(struct list_head *list)
57 {
58         tdm_pp_private_buffer *b = NULL;
59         char str[512], *p;
60         int len = sizeof(str);
61
62         TDM_RETURN_IF_FAIL(list != NULL);
63
64         p = str;
65         LIST_FOR_EACH_ENTRY(b, list, link) {
66                 if (len > 0) {
67                         int l = snprintf(p, len, " (%p,%p)", b->src, b->dst);
68                         p += l;
69                         len -= l;
70                 } else
71                         break;
72         }
73
74         TDM_WRN("\t %s", str);
75 }
76
77 static tdm_pp_private_buffer *
78 _tdm_pp_find_tbm_buffers(struct list_head *list, tbm_surface_h src, tbm_surface_h dst)
79 {
80         tdm_pp_private_buffer *b = NULL, *bb = NULL;
81
82         LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
83                 if (b->src == src && b->dst == dst)
84                         return b;
85         }
86
87         return NULL;
88 }
89
90 static tdm_pp_private_buffer *
91 _tdm_pp_find_buffer(struct list_head *list, tdm_pp_private_buffer *pp_buffer)
92 {
93         tdm_pp_private_buffer *b = NULL, *bb = NULL;
94
95         LIST_FOR_EACH_ENTRY_SAFE(b, bb, list, link) {
96                 if (b == pp_buffer)
97                         return b;
98         }
99
100         return NULL;
101 }
102
103 INTERN void
104 tdm_pp_cb_done(tdm_pp *pp_backend, tbm_surface_h src, tbm_surface_h dst,
105                            void *user_data)
106 {
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;
110
111         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
112
113         if (private_pp->owner_tid != syscall(SYS_gettid)) {
114                 tdm_thread_cb_pp_done pp_done;
115                 tdm_error ret;
116
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;
120                 pp_done.src = src;
121                 pp_done.dst = dst;
122                 pp_done.user_data = user_data;
123
124                 ret = tdm_thread_send_cb(private_display->private_loop, &pp_done.base);
125                 TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
126
127                 return;
128         }
129
130         if (private_pp->owner_tid != syscall(SYS_gettid))
131                 TDM_NEVER_GET_HERE();
132
133         if (tdm_debug_dump & TDM_DUMP_FLAG_PP) {
134                 /* LCOV_EXCL_START */
135                 char str[TDM_PATH_LEN];
136                 static int i;
137                 snprintf(str, TDM_PATH_LEN, "pp_dst_%03d", i++);
138                 tdm_helper_dump_buffer_str(dst, tdm_debug_dump_dir, str);
139                 /* LCOV_EXCL_STOP */
140         }
141
142         if (tdm_debug_module & TDM_DEBUG_BUFFER)
143                 TDM_INFO("pp(%p) done: src(%p) dst(%p)", private_pp, src, dst);
144
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);
149         } else {
150                 TDM_NEVER_GET_HERE();
151         }
152
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);
156
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);
163
164                 free(pp_buffer);
165         }
166 }
167
168 INTERN tdm_private_pp *
169 tdm_pp_find_stamp(tdm_private_display *private_display, double stamp)
170 {
171         tdm_private_pp *private_pp = NULL;
172
173         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
174
175         LIST_FOR_EACH_ENTRY(private_pp, &private_display->pp_list, link) {
176                 if (private_pp->stamp == stamp)
177                         return private_pp;
178         }
179
180         return NULL;
181 }
182
183 INTERN tdm_private_pp *
184 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error)
185 {
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;
191
192         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
193
194         func_display = &private_display->func_display;
195         func_pp = &private_display->func_pp;
196
197         if (!(private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP)) {
198                 /* LCOV_EXCL_START */
199                 TDM_ERR("no pp capability");
200                 if (error)
201                         *error = TDM_ERROR_NO_CAPABILITY;
202                 return NULL;
203                 /* LCOV_EXCL_STOP */
204         }
205
206         pp_backend = func_display->display_create_pp(private_display->bdata, &ret);
207         if (ret != TDM_ERROR_NONE) {
208                 /* LCOV_EXCL_START */
209                 if (error)
210                         *error = ret;
211                 return NULL;
212                 /* LCOV_EXCL_STOP */
213         }
214
215         private_pp = calloc(1, sizeof(tdm_private_pp));
216         if (!private_pp) {
217                 /* LCOV_EXCL_START */
218                 TDM_ERR("failed: alloc memory");
219                 func_pp->pp_destroy(pp_backend);
220                 if (error)
221                         *error = TDM_ERROR_OUT_OF_MEMORY;
222                 return NULL;
223                 /* LCOV_EXCL_STOP */
224         }
225
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);
231                 if (error)
232                         *error = ret;
233                 return NULL;
234                 /* LCOV_EXCL_STOP */
235         }
236
237         private_pp->stamp = tdm_helper_get_time();
238         while (tdm_pp_find_stamp(private_display, private_pp->stamp))
239                 private_pp->stamp++;
240
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);
245
246         LIST_INITHEAD(&private_pp->pending_buffer_list);
247         LIST_INITHEAD(&private_pp->buffer_list);
248
249         if (error)
250                 *error = TDM_ERROR_NONE;
251
252         return private_pp;
253 }
254
255 INTERN void
256 tdm_pp_destroy_internal(tdm_private_pp *private_pp)
257 {
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;
262
263         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
264
265         if (!private_pp)
266                 return;
267
268         private_display = private_pp->private_display;
269         func_pp = &private_display->func_pp;
270
271         LIST_DEL(&private_pp->link);
272
273         func_pp->pp_destroy(private_pp->pp_backend);
274
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);
278
279                 LIST_INITHEAD(&clone_list);
280                 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
281                         LIST_DEL(&b->link);
282                         LIST_ADDTAIL(&b->link, &clone_list);
283                 }
284
285                 _pthread_mutex_unlock(&private_display->lock);
286                 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
287                         LIST_DEL(&b->link);
288                         tdm_buffer_unref_backend(b->src);
289                         tdm_buffer_unref_backend(b->dst);
290                         free(b);
291                 }
292                 _pthread_mutex_lock(&private_display->lock);
293         }
294
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);
298
299                 LIST_INITHEAD(&clone_list);
300                 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->buffer_list, link) {
301                         LIST_DEL(&b->link);
302                         LIST_ADDTAIL(&b->link, &clone_list);
303                 }
304
305                 _pthread_mutex_unlock(&private_display->lock);
306                 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
307                         LIST_DEL(&b->link);
308                         tdm_buffer_unref_backend(b->src);
309                         tdm_buffer_unref_backend(b->dst);
310                         free(b);
311                 }
312                 _pthread_mutex_lock(&private_display->lock);
313         }
314
315         private_pp->stamp = 0;
316         free(private_pp);
317 }
318
319 EXTERN void
320 tdm_pp_destroy(tdm_pp *pp)
321 {
322         tdm_private_pp *private_pp = pp;
323         tdm_private_display *private_display;
324
325         if (!private_pp)
326                 return;
327
328         private_display = private_pp->private_display;
329
330         _pthread_mutex_lock(&private_display->lock);
331         tdm_pp_destroy_internal(private_pp);
332         _pthread_mutex_unlock(&private_display->lock);
333 }
334
335 EXTERN tdm_error
336 tdm_pp_set_info(tdm_pp *pp, tdm_info_pp *info)
337 {
338         PP_FUNC_ENTRY();
339
340         TDM_RETURN_VAL_IF_FAIL(info != NULL, TDM_ERROR_INVALID_PARAMETER);
341
342         _pthread_mutex_lock(&private_display->lock);
343
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;
349                 /* LCOV_EXCL_STOP */
350         }
351
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);
362
363         ret = func_pp->pp_set_info(private_pp->pp_backend, info);
364         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
365
366         private_pp->info = *info;
367
368         _pthread_mutex_unlock(&private_display->lock);
369
370         return ret;
371 }
372
373 EXTERN tdm_error
374 tdm_pp_set_done_handler(tdm_pp *pp, tdm_pp_done_handler func, void *user_data)
375 {
376         tdm_private_display *private_display;
377         tdm_private_pp *private_pp;
378         tdm_error ret = TDM_ERROR_NONE;
379
380         TDM_RETURN_VAL_IF_FAIL(pp != NULL, TDM_ERROR_INVALID_PARAMETER);
381
382         private_pp = (tdm_private_pp*)pp;
383         private_display = private_pp->private_display;
384
385         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
386
387         _pthread_mutex_lock(&private_display->lock);
388
389         private_pp->done_func = func;
390         private_pp->done_user_data = user_data;
391
392         _pthread_mutex_unlock(&private_display->lock);
393
394         return ret;
395 }
396
397 EXTERN tdm_error
398 tdm_pp_attach(tdm_pp *pp, tbm_surface_h src, tbm_surface_h dst)
399 {
400         tdm_pp_private_buffer *pp_buffer;
401
402         PP_FUNC_ENTRY();
403
404         TDM_RETURN_VAL_IF_FAIL(src != NULL, TDM_ERROR_INVALID_PARAMETER);
405         TDM_RETURN_VAL_IF_FAIL(dst != NULL, TDM_ERROR_INVALID_PARAMETER);
406
407         _pthread_mutex_lock(&private_display->lock);
408
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;
414                 /* LCOV_EXCL_STOP */
415         }
416
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;
427                 }
428                 /* LCOV_EXCL_STOP */
429         }
430
431         if (tdm_debug_dump & TDM_DUMP_FLAG_PP) {
432                 /* LCOV_EXCL_START */
433                 char str[TDM_PATH_LEN];
434                 static int i;
435                 snprintf(str, TDM_PATH_LEN, "pp_src_%03d", i++);
436                 tdm_helper_dump_buffer_str(src, tdm_debug_dump_dir, str);
437                 /* LCOV_EXCL_STOP */
438         }
439
440         pp_buffer = calloc(1, sizeof * pp_buffer);
441         if (!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;
446                 /* LCOV_EXCL_STOP */
447         }
448
449         ret = func_pp->pp_attach(private_pp->pp_backend, src, dst);
450         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
451
452         if (ret != TDM_ERROR_NONE) {
453                 /* LCOV_EXCL_START */
454                 free(pp_buffer);
455                 _pthread_mutex_unlock(&private_display->lock);
456                 TDM_ERR("attach failed");
457                 return ret;
458                 /* LCOV_EXCL_STOP */
459         }
460
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);
464
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);
468         }
469
470         _pthread_mutex_unlock(&private_display->lock);
471
472         return ret;
473 }
474
475 EXTERN tdm_error
476 tdm_pp_commit(tdm_pp *pp)
477 {
478         tdm_pp_private_buffer *b = NULL, *bb = NULL;
479         struct list_head commit_buffer_list;
480
481         PP_FUNC_ENTRY();
482
483         _pthread_mutex_lock(&private_display->lock);
484
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;
490                 /* LCOV_EXCL_STOP */
491         }
492
493         LIST_INITHEAD(&commit_buffer_list);
494
495         LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
496                 LIST_DEL(&b->link);
497                 LIST_ADDTAIL(&b->link, &private_pp->buffer_list);
498                 LIST_ADDTAIL(&b->commit_link, &commit_buffer_list);
499         }
500
501         ret = func_pp->pp_commit(private_pp->pp_backend);
502         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
503
504         LIST_FOR_EACH_ENTRY_SAFE(b, bb, &commit_buffer_list, commit_link) {
505                 LIST_DELINIT(&b->commit_link);
506
507                 if (!_tdm_pp_find_buffer(&private_pp->buffer_list, b))
508                         continue;
509
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.
515                          */
516                         tdm_buffer_remove_release_handler_internal(b->src);
517                         tdm_buffer_remove_release_handler_internal(b->dst);
518
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);
523                         LIST_DEL(&b->link);
524
525                         free(b);
526                 }
527         }
528
529         _pthread_mutex_unlock(&private_display->lock);
530
531         return ret;
532 }