refactoring tdm vblank
[platform/core/uifw/libtdm.git] / src / tdm_private.h
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 #ifndef _TDM_PRIVATE_H_
37 #define _TDM_PRIVATE_H_
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <stdlib.h>
43 #include <pthread.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <limits.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <dlfcn.h>
51 #include <dirent.h>
52 #include <poll.h>
53 #include <sys/syscall.h>
54 #include <sys/types.h>
55 #include <math.h>
56
57 #include <tbm_bufmgr.h>
58 #include <tbm_surface_queue.h>
59
60 #include "tdm_backend.h"
61 #include "tdm_log.h"
62 #include "tdm_list.h"
63 #include "tdm_macro.h"
64
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
69 //#define INIT_BUFMGR
70
71 /**
72  * @file tdm_private.h
73  * @brief The private header file for a frontend library
74  */
75
76 enum {
77         TDM_DEBUG_NONE,
78         TDM_DEBUG_BUFFER    = (1 << 0),
79         TDM_DEBUG_MUTEX     = (1 << 1),
80         TDM_DEBUG_THREAD    = (1 << 2),
81         TDM_DEBUG_SERVER    = (1 << 3),
82         TDM_DEBUG_VBLANK    = (1 << 4),
83 };
84
85 extern int tdm_debug_module;
86 extern int tdm_debug_dump;
87
88 #ifdef HAVE_TTRACE
89 #include <ttrace.h>
90 #define TDM_TRACE_BEGIN(NAME) traceBegin(TTRACE_TAG_GRAPHICS, "TDM:"#NAME)
91 #define TDM_TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
92 #define TDM_TRACE_COUNT(NAME, COUNT) traceCounter(TTRACE_TAG_GRAPHICS, COUNT, "TDM:"#NAME)
93 #define TDM_TRACE_MARK(NAME) traceMark(TTRACE_TAG_GRAPHICS, "TDM:"#NAME)
94 #else
95 #define TDM_TRACE_BEGIN(NAME)
96 #define TDM_TRACE_END()
97 #define TDM_TRACE_COUNT(NAME, COUNT)
98 #define TDM_TRACE_MARK(NAME)
99 #endif
100
101 typedef enum {
102         TDM_CAPTURE_TARGET_OUTPUT,
103         TDM_CAPTURE_TARGET_LAYER,
104 } tdm_capture_target;
105
106 enum {
107         TDM_DUMP_FLAG_LAYER   = (1 << 0),
108         TDM_DUMP_FLAG_PP      = (1 << 1),
109         TDM_DUMP_FLAG_CAPTURE = (1 << 2),
110 };
111
112 #define TDM_DUMP_DIR    "/tmp"
113
114 typedef struct _tdm_private_display tdm_private_display;
115 typedef struct _tdm_private_output tdm_private_output;
116 typedef struct _tdm_private_layer tdm_private_layer;
117 typedef struct _tdm_private_pp tdm_private_pp;
118 typedef struct _tdm_private_capture tdm_private_capture;
119 typedef struct _tdm_private_loop tdm_private_loop;
120 typedef struct _tdm_private_server tdm_private_server;
121 typedef struct _tdm_private_thread tdm_private_thread;
122 typedef struct _tdm_private_vblank_handler tdm_private_vblank_handler;
123 typedef struct _tdm_private_commit_handler tdm_private_commit_handler;
124 typedef struct _tdm_private_change_handler tdm_private_change_handler;
125
126 struct _tdm_private_display {
127         pthread_mutex_t lock;
128         unsigned int init_count;
129
130         /* backend module info */
131         void *module;
132         tdm_backend_module *module_data;
133         tdm_backend_data *bdata;
134
135 #ifdef INIT_BUFMGR
136         tbm_bufmgr bufmgr;
137 #endif
138
139         /* backend function */
140         tdm_display_capability capabilities;
141         tdm_func_display func_display;
142         tdm_func_output func_output;
143         tdm_func_layer func_layer;
144         tdm_func_pp func_pp;
145         tdm_func_capture func_capture;
146
147         /* backend capability */
148         tdm_caps_display caps_display;
149         tdm_caps_pp caps_pp;
150         tdm_caps_capture caps_capture;
151
152         /* output, pp list */
153         struct list_head output_list;
154         struct list_head pp_list;
155         struct list_head capture_list;
156
157         void **outputs_ptr;
158
159         /* for event handling */
160         tdm_private_loop *private_loop;
161
162         /* output order */
163         tdm_output **outputs;
164 };
165
166 struct _tdm_private_output {
167         struct list_head link;
168
169         int index;
170         unsigned long stamp;
171
172         tdm_private_display *private_display;
173
174         tdm_caps_output caps;
175         tdm_output *output_backend;
176
177         unsigned int pipe;
178         tdm_output_dpms current_dpms_value;
179         const tdm_output_mode *current_mode;
180
181         int regist_vblank_cb;
182         int regist_commit_cb;
183         int regist_change_cb;
184         int regist_dpms_cb;
185
186         struct list_head layer_list;
187         struct list_head capture_list;
188         struct list_head vblank_handler_list;
189         struct list_head commit_handler_list;
190
191         /* seperate list for multi-thread*/
192         struct list_head change_handler_list_main;
193         struct list_head change_handler_list_sub;
194
195         void **layers_ptr;
196
197         /* TODO: temp solution for handling DPMS things in sub-htread */
198         tdm_event_loop_source *dpms_changed_timer;
199 };
200
201 struct _tdm_private_layer {
202         struct list_head link;
203
204         int index;
205
206         tdm_private_display *private_display;
207         tdm_private_output *private_output;
208
209         tdm_caps_layer caps;
210         tdm_layer *layer_backend;
211
212         tbm_surface_h pending_buffer;
213         tbm_surface_h waiting_buffer;
214         tbm_surface_h showing_buffer;
215         tbm_surface_queue_h buffer_queue;
216
217         struct list_head capture_list;
218
219         unsigned int usable;
220 };
221
222 struct _tdm_private_pp {
223         struct list_head link;
224
225         unsigned long stamp;
226
227         tdm_private_display *private_display;
228
229         tdm_pp *pp_backend;
230
231         struct list_head pending_buffer_list;
232         struct list_head buffer_list;
233
234         tdm_info_pp info;
235         pid_t owner_tid;
236
237         tdm_pp_done_handler done_func;
238         void *done_user_data;
239 };
240
241 struct _tdm_private_capture {
242         struct list_head link;
243         struct list_head display_link;
244
245         unsigned long stamp;
246
247         tdm_capture_target target;
248
249         tdm_private_display *private_display;
250         tdm_private_output *private_output;
251         tdm_private_layer *private_layer;
252
253         tdm_capture *capture_backend;
254
255         struct list_head pending_buffer_list;
256         struct list_head buffer_list;
257
258         tdm_info_capture info;
259         pid_t owner_tid;
260
261         tdm_capture_done_handler done_func;
262         void *done_user_data;
263 };
264
265 /* CAUTION:
266  * Note that we don't need to (un)lock mutex to use this structure. If there is
267  * no TDM thread, all TDM resources are protected by private_display's mutex.
268  * If there is a TDM thread, this struct will be used only in a TDM thread.
269  * So, we don't need to protect this structure by mutex. Not thread-safe.
270  */
271 struct _tdm_private_loop {
272         /* TDM uses wl_event_loop to handle various event sources including the TDM
273          * backend's fd.
274          */
275         struct wl_display *wl_display;
276         struct wl_event_loop *wl_loop;
277
278         int backend_fd;
279         tdm_event_loop_source *backend_source;
280
281         /* In event loop, all resources are accessed by this dpy.
282          * CAUTION:
283          * - DO NOT include other private structure in this structure because this
284          *   struct is not protected by mutex.
285          */
286         tdm_display *dpy;
287
288         /* for handling TDM client requests */
289         tdm_private_server *private_server;
290
291         /* To have a TDM event thread. If TDM_THREAD enviroment variable is not set
292          * private_thread is NULL.
293          */
294         tdm_private_thread *private_thread;
295 };
296
297 struct _tdm_private_vblank_handler {
298         struct list_head link;
299
300         tdm_private_output *private_output;
301         tdm_output_vblank_handler func;
302         void *user_data;
303
304         pid_t owner_tid;
305 };
306
307 struct _tdm_private_commit_handler {
308         struct list_head link;
309
310         tdm_private_output *private_output;
311         tdm_output_commit_handler func;
312         void *user_data;
313
314         pid_t owner_tid;
315 };
316
317 struct _tdm_private_change_handler {
318         struct list_head link;
319
320         tdm_private_output *private_output;
321         tdm_output_change_handler func;
322         void *user_data;
323
324         pid_t owner_tid;
325 };
326
327 typedef struct _tdm_buffer_info {
328         tbm_surface_h buffer;
329
330         /* ref_count for backend */
331         int backend_ref_count;
332
333         struct list_head release_funcs;
334         struct list_head destroy_funcs;
335
336         struct list_head *list;
337         struct list_head link;
338 } tdm_buffer_info;
339
340 typedef struct _tdm_pp_private_buffer {
341         tbm_surface_h src;
342         tbm_surface_h dst;
343         struct list_head link;
344         struct list_head commit_link;
345 } tdm_pp_private_buffer;
346
347 int
348 tdm_display_check_module_abi(tdm_private_display *private_display, int abimaj, int abimin);
349
350 tdm_private_output *
351 tdm_display_find_output_stamp(tdm_private_display *private_display,
352                                                           unsigned long stamp);
353 tdm_private_pp *
354 tdm_pp_find_stamp(tdm_private_display *private_display, unsigned long stamp);
355 tdm_private_capture *
356 tdm_capture_find_stamp(tdm_private_display *private_display, unsigned long stamp);
357
358 void
359 tdm_output_cb_vblank(tdm_output *output_backend, unsigned int sequence,
360                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data);
361 void
362 tdm_output_cb_commit(tdm_output *output_backend, unsigned int sequence,
363                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data);
364 void
365 tdm_output_cb_status(tdm_output *output_backend, tdm_output_conn_status status,
366                                          void *user_data);
367 void
368 tdm_output_cb_dpms(tdm_output *output_backend, tdm_output_dpms dpms,
369                                    void *user_data);
370 void
371 tdm_pp_cb_done(tdm_pp *pp_backend, tbm_surface_h src, tbm_surface_h dst,
372                            void *user_data);
373 void
374 tdm_capture_cb_done(tdm_capture *capture_backend, tbm_surface_h buffer,
375                                         void *user_data);
376
377 void
378 tdm_output_call_change_handler_internal(tdm_private_output *private_output,
379                                                                                 struct list_head *change_handler_list,
380                                                                                 tdm_output_change_type type,
381                                                                                 tdm_value value,
382                                                                                 int no_check_thread_id);
383
384 tdm_private_pp *
385 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error);
386 void
387 tdm_pp_destroy_internal(tdm_private_pp *private_pp);
388
389 tdm_private_capture *
390 tdm_capture_create_output_internal(tdm_private_output *private_output,
391                                                                    tdm_error *error);
392 tdm_private_capture *
393 tdm_capture_create_layer_internal(tdm_private_layer *private_layer,
394                                                                   tdm_error *error);
395 void
396 tdm_capture_destroy_internal(tdm_private_capture *private_capture);
397
398 /* utility buffer functions for private */
399 tdm_buffer_info*
400 tdm_buffer_get_info(tbm_surface_h buffer);
401 tbm_surface_h
402 tdm_buffer_list_get_first_entry(struct list_head *list);
403 void
404 tdm_buffer_list_dump(struct list_head *list);
405
406 void
407 tdm_buffer_remove_release_handler_internal(tbm_surface_h buffer);
408
409 /* event functions for private */
410 tdm_error
411 tdm_event_loop_init(tdm_private_display *private_display);
412 void
413 tdm_event_loop_deinit(tdm_private_display *private_display);
414 void
415 tdm_event_loop_create_backend_source(tdm_private_display *private_display);
416 int
417 tdm_event_loop_get_fd(tdm_private_display *private_display);
418 tdm_error
419 tdm_event_loop_dispatch(tdm_private_display *private_display);
420 void
421 tdm_event_loop_flush(tdm_private_display *private_display);
422
423 typedef enum {
424         TDM_THREAD_CB_NONE,
425         TDM_THREAD_CB_OUTPUT_COMMIT,
426         TDM_THREAD_CB_OUTPUT_VBLANK,
427         TDM_THREAD_CB_OUTPUT_STATUS,
428         TDM_THREAD_CB_OUTPUT_DPMS,
429         TDM_THREAD_CB_PP_DONE,
430         TDM_THREAD_CB_CAPTURE_DONE,
431 } tdm_thread_cb_type;
432
433 typedef struct _tdm_thread_cb_base tdm_thread_cb_base;
434 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_commit;
435 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_vblank;
436 typedef struct _tdm_thread_cb_output_status tdm_thread_cb_output_status;
437 typedef struct _tdm_thread_cb_output_dpms tdm_thread_cb_output_dpms;
438 typedef struct _tdm_thread_cb_pp_done tdm_thread_cb_pp_done;
439 typedef struct _tdm_thread_cb_capture_done tdm_thread_cb_capture_done;
440
441 struct _tdm_thread_cb_base {
442         tdm_thread_cb_type type;
443         unsigned int length;
444 };
445
446 struct _tdm_thread_cb_output_vblank {
447         tdm_thread_cb_base base;
448         unsigned long output_stamp;
449         unsigned int sequence;
450         unsigned int tv_sec;
451         unsigned int tv_usec;
452         void *user_data;
453 };
454
455 struct _tdm_thread_cb_output_status {
456         tdm_thread_cb_base base;
457         unsigned long output_stamp;
458         tdm_output_conn_status status;
459         void *user_data;
460 };
461
462 struct _tdm_thread_cb_output_dpms {
463         tdm_thread_cb_base base;
464         unsigned long output_stamp;
465         tdm_output_dpms dpms;
466         void *user_data;
467 };
468
469 struct _tdm_thread_cb_pp_done {
470         tdm_thread_cb_base base;
471         unsigned long pp_stamp;
472         tbm_surface_h src;
473         tbm_surface_h dst;
474         void *user_data;
475 };
476
477 struct _tdm_thread_cb_capture_done {
478         tdm_thread_cb_base base;
479         unsigned long capture_stamp;
480         tbm_surface_h buffer;
481         void *user_data;
482 };
483
484 tdm_error
485 tdm_thread_init(tdm_private_loop *private_loop);
486 void
487 tdm_thread_deinit(tdm_private_loop *private_loop);
488 int
489 tdm_thread_get_fd(tdm_private_loop *private_loop);
490 tdm_error
491 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base);
492 tdm_error
493 tdm_thread_handle_cb(tdm_private_loop *private_loop);
494 int
495 tdm_thread_in_display_thread(pid_t tid);
496 int
497 tdm_thread_is_running(void);
498
499 tdm_error
500 tdm_server_init(tdm_private_loop *private_loop);
501 void
502 tdm_server_deinit(tdm_private_loop *private_loop);
503
504 char *
505 tdm_helper_dump_make_directory(const char *path, char *reply, int *len);
506 void
507 tdm_helper_dump_buffer_str(tbm_surface_h buffer, char *dir, char *str);
508 unsigned long
509 tdm_helper_get_time_in_millis(void);
510 unsigned long
511 tdm_helper_get_time_in_micros(void);
512
513 extern pthread_mutex_t tdm_mutex_check_lock;
514 extern int tdm_mutex_locked;
515 extern int tdm_dump_enable;
516 extern char *tdm_debug_dump_dir;
517
518 #define _pthread_mutex_unlock(l) \
519         do { \
520                 if (tdm_debug_module & TDM_DEBUG_MUTEX) \
521                         TDM_INFO("mutex unlock"); \
522                 pthread_mutex_lock(&tdm_mutex_check_lock); \
523                 tdm_mutex_locked = 0; \
524                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
525                 pthread_mutex_unlock(l); \
526         } while (0)
527 #ifdef TDM_CONFIG_MUTEX_TIMEOUT
528 #define MUTEX_TIMEOUT_SEC 5
529 #define _pthread_mutex_lock(l) \
530         do { \
531                 if (tdm_debug_module & TDM_DEBUG_MUTEX) \
532                         TDM_INFO("mutex lock"); \
533                 struct timespec rtime; \
534                 clock_gettime(CLOCK_REALTIME, &rtime); \
535                 rtime.tv_sec += MUTEX_TIMEOUT_SEC; \
536                 if (pthread_mutex_timedlock(l, &rtime)) { \
537                         TDM_ERR("Mutex lock failed PID %d", getpid()); \
538                         _pthread_mutex_unlock(l); \
539                 } \
540                 else { \
541                         pthread_mutex_lock(&tdm_mutex_check_lock); \
542                         tdm_mutex_locked = 1; \
543                         pthread_mutex_unlock(&tdm_mutex_check_lock); \
544                 } \
545         } while (0)
546 #else //TDM_CONFIG_MUTEX_TIMEOUT
547 #define _pthread_mutex_lock(l) \
548         do { \
549                 if (tdm_debug_module & TDM_DEBUG_MUTEX) \
550                         TDM_INFO("mutex lock"); \
551                 pthread_mutex_lock(l); \
552                 pthread_mutex_lock(&tdm_mutex_check_lock); \
553                 tdm_mutex_locked = 1; \
554                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
555         } while (0)
556 #endif //TDM_CONFIG_MUTEX_TIMEOUT
557 //#define TDM_MUTEX_IS_LOCKED() (tdm_mutex_locked == 1)
558 static inline int TDM_MUTEX_IS_LOCKED(void)
559 {
560         int ret;
561         /* if thread is not running, we don't need to consider mutex things. */
562         if (!tdm_thread_is_running())
563                 return 1;
564         pthread_mutex_lock(&tdm_mutex_check_lock);
565         ret = (tdm_mutex_locked == 1);
566         pthread_mutex_unlock(&tdm_mutex_check_lock);
567         return ret;
568 }
569
570 #define tdm_display_lock(dpy)   _pthread_mutex_lock(&((tdm_private_display *)dpy)->lock)
571 #define tdm_display_unlock(dpy)   _pthread_mutex_unlock(&((tdm_private_display *)dpy)->lock)
572
573 tdm_error
574 tdm_display_update_output(tdm_private_display *private_display,
575                                                   tdm_output *output_backend, int pipe);
576 tdm_error
577 tdm_display_enable_debug_module(const char*modules);
578 tdm_error
579 tdm_display_enable_dump(tdm_private_display *private_display, const char *dump_str, char *reply, int *len);
580 tdm_error
581 tdm_display_enable_path(const char *path);
582 tdm_error
583 tdm_display_enable_ttrace_vblank(tdm_display *dpy, tdm_output *output, int enable);
584
585 /**
586  * @brief The tdm vblank object
587  */
588 typedef void tdm_vblank;
589
590 typedef void (*tdm_vblank_handler)(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
591                                                                    unsigned int tv_sec, unsigned int tv_usec, void *user_data);
592
593 tdm_vblank*
594 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error);
595 void
596 tdm_vblank_destroy(tdm_vblank *vblank);
597 tdm_error
598 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps);
599 tdm_error
600 tdm_vblank_set_offset(tdm_vblank *vblank, int offset);
601 tdm_error
602 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake);
603 unsigned int
604 tdm_vblank_get_enable_fake(tdm_vblank *vblank);
605 tdm_error
606 tdm_vblank_wait(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec, unsigned int interval, tdm_vblank_handler func, void *user_data);
607
608 void
609 tdm_monitor_server_command(tdm_display *dpy, const char *options, char *reply, int *len);
610
611 #ifdef __cplusplus
612 }
613 #endif
614
615 #endif /* _TDM_PRIVATE_H_ */