use vblank instead of the output commit handler in case of tdm_layer_commit
[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                 char str[TDM_PATH_LEN];
135                 static int i;
136                 snprintf(str, TDM_PATH_LEN, "pp_dst_%03d", i++);
137                 tdm_helper_dump_buffer_str(dst, tdm_debug_dump_dir, str);
138         }
139
140         if (tdm_debug_module & TDM_DEBUG_BUFFER)
141                 TDM_INFO("pp(%p) done: src(%p) dst(%p)", private_pp, src, dst);
142
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);
147         } else {
148                 TDM_NEVER_GET_HERE();
149         }
150
151         if ((pp_buffer = _tdm_pp_find_tbm_buffers(&private_pp->buffer_list, src, dst))) {
152                 LIST_DEL(&pp_buffer->link);
153
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);
160
161                 free(pp_buffer);
162         }
163 }
164
165 INTERN tdm_private_pp *
166 tdm_pp_find_stamp(tdm_private_display *private_display, double stamp)
167 {
168         tdm_private_pp *private_pp = NULL;
169
170         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
171
172         LIST_FOR_EACH_ENTRY(private_pp, &private_display->pp_list, link) {
173                 if (private_pp->stamp == stamp)
174                         return private_pp;
175         }
176
177         return NULL;
178 }
179
180 INTERN tdm_private_pp *
181 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error)
182 {
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;
188
189         TDM_RETURN_VAL_IF_FAIL(TDM_MUTEX_IS_LOCKED(), NULL);
190
191         func_display = &private_display->func_display;
192         func_pp = &private_display->func_pp;
193
194         if (!(private_display->capabilities & TDM_DISPLAY_CAPABILITY_PP)) {
195                 TDM_ERR("no pp capability");
196                 if (error)
197                         *error = TDM_ERROR_NO_CAPABILITY;
198                 return NULL;
199         }
200
201         pp_backend = func_display->display_create_pp(private_display->bdata, &ret);
202         if (ret != TDM_ERROR_NONE) {
203                 if (error)
204                         *error = ret;
205                 return NULL;
206         }
207
208         private_pp = calloc(1, sizeof(tdm_private_pp));
209         if (!private_pp) {
210                 TDM_ERR("failed: alloc memory");
211                 func_pp->pp_destroy(pp_backend);
212                 if (error)
213                         *error = TDM_ERROR_OUT_OF_MEMORY;
214                 return NULL;
215         }
216
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);
221                 if (error)
222                         *error = ret;
223                 return NULL;
224         }
225
226         private_pp->stamp = tdm_helper_get_time();
227         while (tdm_pp_find_stamp(private_display, private_pp->stamp))
228                 private_pp->stamp++;
229
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);
234
235         LIST_INITHEAD(&private_pp->pending_buffer_list);
236         LIST_INITHEAD(&private_pp->buffer_list);
237
238         if (error)
239                 *error = TDM_ERROR_NONE;
240
241         return private_pp;
242 }
243
244 INTERN void
245 tdm_pp_destroy_internal(tdm_private_pp *private_pp)
246 {
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;
251
252         TDM_RETURN_IF_FAIL(TDM_MUTEX_IS_LOCKED());
253
254         if (!private_pp)
255                 return;
256
257         private_display = private_pp->private_display;
258         func_pp = &private_display->func_pp;
259
260         LIST_DEL(&private_pp->link);
261
262         func_pp->pp_destroy(private_pp->pp_backend);
263
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);
267
268                 LIST_INITHEAD(&clone_list);
269                 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
270                         LIST_DEL(&b->link);
271                         LIST_ADDTAIL(&b->link, &clone_list);
272                 }
273
274                 _pthread_mutex_unlock(&private_display->lock);
275                 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
276                         LIST_DEL(&b->link);
277                         tdm_buffer_unref_backend(b->src);
278                         tdm_buffer_unref_backend(b->dst);
279                         free(b);
280                 }
281                 _pthread_mutex_lock(&private_display->lock);
282         }
283
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);
287
288                 LIST_INITHEAD(&clone_list);
289                 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->buffer_list, link) {
290                         LIST_DEL(&b->link);
291                         LIST_ADDTAIL(&b->link, &clone_list);
292                 }
293
294                 _pthread_mutex_unlock(&private_display->lock);
295                 LIST_FOR_EACH_ENTRY_SAFE(b, bb, &clone_list, link) {
296                         LIST_DEL(&b->link);
297                         tdm_buffer_unref_backend(b->src);
298                         tdm_buffer_unref_backend(b->dst);
299                         free(b);
300                 }
301                 _pthread_mutex_lock(&private_display->lock);
302         }
303
304         private_pp->stamp = 0;
305         free(private_pp);
306 }
307
308 EXTERN void
309 tdm_pp_destroy(tdm_pp *pp)
310 {
311         tdm_private_pp *private_pp = pp;
312         tdm_private_display *private_display;
313
314         if (!private_pp)
315                 return;
316
317         private_display = private_pp->private_display;
318
319         _pthread_mutex_lock(&private_display->lock);
320         tdm_pp_destroy_internal(private_pp);
321         _pthread_mutex_unlock(&private_display->lock);
322 }
323
324 EXTERN tdm_error
325 tdm_pp_set_info(tdm_pp *pp, tdm_info_pp *info)
326 {
327         PP_FUNC_ENTRY();
328
329         TDM_RETURN_VAL_IF_FAIL(info != NULL, TDM_ERROR_INVALID_PARAMETER);
330
331         _pthread_mutex_lock(&private_display->lock);
332
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;
337         }
338
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);
349
350         ret = func_pp->pp_set_info(private_pp->pp_backend, info);
351         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
352
353         private_pp->info = *info;
354
355         _pthread_mutex_unlock(&private_display->lock);
356
357         return ret;
358 }
359
360 EXTERN tdm_error
361 tdm_pp_set_done_handler(tdm_pp *pp, tdm_pp_done_handler func, void *user_data)
362 {
363         tdm_private_display *private_display;
364         tdm_private_pp *private_pp;
365         tdm_error ret = TDM_ERROR_NONE;
366
367         TDM_RETURN_VAL_IF_FAIL(pp != NULL, TDM_ERROR_INVALID_PARAMETER);
368
369         private_pp = (tdm_private_pp*)pp;
370         private_display = private_pp->private_display;
371
372         TDM_RETURN_VAL_IF_FAIL(func != NULL, TDM_ERROR_INVALID_PARAMETER);
373
374         _pthread_mutex_lock(&private_display->lock);
375
376         private_pp->done_func = func;
377         private_pp->done_user_data = user_data;
378
379         _pthread_mutex_unlock(&private_display->lock);
380
381         return ret;
382 }
383
384 EXTERN tdm_error
385 tdm_pp_attach(tdm_pp *pp, tbm_surface_h src, tbm_surface_h dst)
386 {
387         tdm_pp_private_buffer *pp_buffer;
388
389         PP_FUNC_ENTRY();
390
391         TDM_RETURN_VAL_IF_FAIL(src != NULL, TDM_ERROR_INVALID_PARAMETER);
392         TDM_RETURN_VAL_IF_FAIL(dst != NULL, TDM_ERROR_INVALID_PARAMETER);
393
394         _pthread_mutex_lock(&private_display->lock);
395
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;
400         }
401
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;
411                 }
412         }
413
414         if (tdm_debug_dump & TDM_DUMP_FLAG_PP) {
415                 char str[TDM_PATH_LEN];
416                 static int i;
417                 snprintf(str, TDM_PATH_LEN, "pp_src_%03d", i++);
418                 tdm_helper_dump_buffer_str(src, tdm_debug_dump_dir, str);
419         }
420
421         pp_buffer = calloc(1, sizeof * pp_buffer);
422         if (!pp_buffer) {
423                 _pthread_mutex_unlock(&private_display->lock);
424                 TDM_ERR("alloc failed");
425                 return TDM_ERROR_OUT_OF_MEMORY;
426         }
427
428         ret = func_pp->pp_attach(private_pp->pp_backend, src, dst);
429         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
430
431         if (ret != TDM_ERROR_NONE) {
432                 free(pp_buffer);
433                 _pthread_mutex_unlock(&private_display->lock);
434                 TDM_ERR("attach failed");
435                 return ret;
436         }
437
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);
441
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);
445         }
446
447         _pthread_mutex_unlock(&private_display->lock);
448
449         return ret;
450 }
451
452 EXTERN tdm_error
453 tdm_pp_commit(tdm_pp *pp)
454 {
455         tdm_pp_private_buffer *b = NULL, *bb = NULL;
456         struct list_head commit_buffer_list;
457
458         PP_FUNC_ENTRY();
459
460         _pthread_mutex_lock(&private_display->lock);
461
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;
466         }
467
468         LIST_INITHEAD(&commit_buffer_list);
469
470         LIST_FOR_EACH_ENTRY_SAFE(b, bb, &private_pp->pending_buffer_list, link) {
471                 LIST_DEL(&b->link);
472                 LIST_ADDTAIL(&b->link, &private_pp->buffer_list);
473                 LIST_ADDTAIL(&b->commit_link, &commit_buffer_list);
474         }
475
476         ret = func_pp->pp_commit(private_pp->pp_backend);
477         TDM_WARNING_IF_FAIL(ret == TDM_ERROR_NONE);
478
479         LIST_FOR_EACH_ENTRY_SAFE(b, bb, &commit_buffer_list, commit_link) {
480                 LIST_DEL(&b->commit_link);
481
482                 if (!_tdm_pp_find_buffer(&private_pp->buffer_list, b))
483                         continue;
484
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.
490                          */
491                         tdm_buffer_remove_release_handler_internal(b->src);
492                         tdm_buffer_remove_release_handler_internal(b->dst);
493
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);
498                         LIST_DEL(&b->link);
499                 }
500         }
501
502         _pthread_mutex_unlock(&private_display->lock);
503
504         return ret;
505 }