support the tdm dynamic fps and the dpms on/off event
[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 extern int tdm_debug_buffer;
77 extern int tdm_debug_mutex;
78 extern int tdm_debug_thread;
79
80 #ifdef HAVE_TTRACE
81 #include <ttrace.h>
82 #define TDM_TRACE_BEGIN(NAME) traceBegin(TTRACE_TAG_GRAPHICS, "TDM:"#NAME)
83 #define TDM_TRACE_END() traceEnd(TTRACE_TAG_GRAPHICS)
84 #else
85 #define TDM_TRACE_BEGIN(NAME)
86 #define TDM_TRACE_END()
87 #endif
88
89 typedef enum {
90         TDM_CAPTURE_TARGET_OUTPUT,
91         TDM_CAPTURE_TARGET_LAYER,
92 } tdm_capture_target;
93
94 typedef struct _tdm_private_display tdm_private_display;
95 typedef struct _tdm_private_output tdm_private_output;
96 typedef struct _tdm_private_layer tdm_private_layer;
97 typedef struct _tdm_private_pp tdm_private_pp;
98 typedef struct _tdm_private_capture tdm_private_capture;
99 typedef struct _tdm_private_loop tdm_private_loop;
100 typedef struct _tdm_private_server tdm_private_server;
101 typedef struct _tdm_private_thread tdm_private_thread;
102 typedef struct _tdm_private_vblank_handler tdm_private_vblank_handler;
103 typedef struct _tdm_private_commit_handler tdm_private_commit_handler;
104 typedef struct _tdm_private_change_handler tdm_private_change_handler;
105
106 struct _tdm_private_display {
107         pthread_mutex_t lock;
108         unsigned int init_count;
109
110         /* backend module info */
111         void *module;
112         tdm_backend_module *module_data;
113         tdm_backend_data *bdata;
114
115 #ifdef INIT_BUFMGR
116         tbm_bufmgr bufmgr;
117 #endif
118
119         /* backend function */
120         tdm_display_capability capabilities;
121         tdm_func_display func_display;
122         tdm_func_output func_output;
123         tdm_func_layer func_layer;
124         tdm_func_pp func_pp;
125         tdm_func_capture func_capture;
126
127         /* backend capability */
128         tdm_caps_display caps_display;
129         tdm_caps_pp caps_pp;
130         tdm_caps_capture caps_capture;
131
132         /* output, pp list */
133         struct list_head output_list;
134         struct list_head pp_list;
135         struct list_head capture_list;
136
137         void **outputs_ptr;
138
139         /* for event handling */
140         tdm_private_loop *private_loop;
141
142         /* output order */
143         tdm_output **outputs;
144 };
145
146 struct _tdm_private_output {
147         struct list_head link;
148
149         unsigned long stamp;
150
151         tdm_private_display *private_display;
152
153         tdm_caps_output caps;
154         tdm_output *output_backend;
155
156         unsigned int pipe;
157         tdm_output_dpms current_dpms_value;
158
159         int regist_vblank_cb;
160         int regist_commit_cb;
161         int regist_change_cb;
162
163         struct list_head layer_list;
164         struct list_head capture_list;
165         struct list_head vblank_handler_list;
166         struct list_head commit_handler_list;
167
168         /* seperate list for multi-thread*/
169         struct list_head change_handler_list_main;
170         struct list_head change_handler_list_sub;
171
172         void **layers_ptr;
173 };
174
175 struct _tdm_private_layer {
176         struct list_head link;
177
178         tdm_private_display *private_display;
179         tdm_private_output *private_output;
180
181         tdm_caps_layer caps;
182         tdm_layer *layer_backend;
183
184         tbm_surface_h pending_buffer;
185         tbm_surface_h waiting_buffer;
186         tbm_surface_h showing_buffer;
187         tbm_surface_queue_h buffer_queue;
188
189         struct list_head capture_list;
190
191         unsigned int usable;
192 };
193
194 struct _tdm_private_pp {
195         struct list_head link;
196
197         unsigned long stamp;
198
199         tdm_private_display *private_display;
200
201         tdm_pp *pp_backend;
202
203         struct list_head pending_buffer_list;
204         struct list_head buffer_list;
205
206         pid_t owner_tid;
207 };
208
209 struct _tdm_private_capture {
210         struct list_head link;
211         struct list_head display_link;
212
213         unsigned long stamp;
214
215         tdm_capture_target target;
216
217         tdm_private_display *private_display;
218         tdm_private_output *private_output;
219         tdm_private_layer *private_layer;
220
221         tdm_capture *capture_backend;
222
223         struct list_head pending_buffer_list;
224         struct list_head buffer_list;
225
226         pid_t owner_tid;
227 };
228
229 /* CAUTION:
230  * Note that we don't need to (un)lock mutex to use this structure. If there is
231  * no TDM thread, all TDM resources are protected by private_display's mutex.
232  * If there is a TDM thread, this struct will be used only in a TDM thread.
233  * So, we don't need to protect this structure by mutex. Not thread-safe.
234  */
235 struct _tdm_private_loop {
236         /* TDM uses wl_event_loop to handle various event sources including the TDM
237          * backend's fd.
238          */
239         struct wl_display *wl_display;
240         struct wl_event_loop *wl_loop;
241
242         int backend_fd;
243         tdm_event_loop_source *backend_source;
244
245         /* In event loop, all resources are accessed by this dpy.
246          * CAUTION:
247          * - DO NOT include other private structure in this structure because this
248          *   struct is not protected by mutex.
249          */
250         tdm_display *dpy;
251
252         /* for handling TDM client requests */
253         tdm_private_server *private_server;
254
255         /* To have a TDM event thread. If TDM_THREAD enviroment variable is not set
256          * private_thread is NULL.
257          */
258         tdm_private_thread *private_thread;
259 };
260
261 struct _tdm_private_vblank_handler {
262         struct list_head link;
263
264         tdm_private_output *private_output;
265         tdm_output_vblank_handler func;
266         void *user_data;
267
268         pid_t owner_tid;
269 };
270
271 struct _tdm_private_commit_handler {
272         struct list_head link;
273
274         tdm_private_output *private_output;
275         tdm_output_commit_handler func;
276         void *user_data;
277
278         pid_t owner_tid;
279 };
280
281 struct _tdm_private_change_handler {
282         struct list_head link;
283
284         tdm_private_output *private_output;
285         tdm_output_change_handler func;
286         void *user_data;
287
288         pid_t owner_tid;
289 };
290
291 typedef struct _tdm_buffer_info {
292         tbm_surface_h buffer;
293
294         /* ref_count for backend */
295         int backend_ref_count;
296
297         struct list_head release_funcs;
298         struct list_head destroy_funcs;
299
300         struct list_head *list;
301         struct list_head link;
302 } tdm_buffer_info;
303
304 int
305 tdm_display_check_module_abi(tdm_private_display *private_display, int abimaj, int abimin);
306
307 tdm_private_output *
308 tdm_display_find_output_stamp(tdm_private_display *private_display,
309                                                           unsigned long stamp);
310 tdm_private_pp *
311 tdm_pp_find_stamp(tdm_private_display *private_display, unsigned long stamp);
312 tdm_private_capture *
313 tdm_capture_find_stamp(tdm_private_display *private_display, unsigned long stamp);
314
315 void
316 tdm_output_cb_vblank(tdm_output *output_backend, unsigned int sequence,
317                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data);
318 void
319 tdm_output_cb_commit(tdm_output *output_backend, unsigned int sequence,
320                                          unsigned int tv_sec, unsigned int tv_usec, void *user_data);
321 void
322 tdm_output_cb_status(tdm_output *output_backend, tdm_output_conn_status status,
323                                          void *user_data);
324 void
325 tdm_pp_cb_done(tdm_pp *pp_backend, tbm_surface_h src, tbm_surface_h dst,
326                            void *user_data);
327 void
328 tdm_capture_cb_done(tdm_capture *capture_backend, tbm_surface_h buffer,
329                                         void *user_data);
330
331 void
332 tdm_output_call_change_handler_internal(tdm_private_output *private_output,
333                                                                                 struct list_head *change_handler_list,
334                                                                                 tdm_output_change_type type,
335                                                                                 tdm_value value,
336                                                                                 int no_check_thread_id);
337
338 tdm_private_pp *
339 tdm_pp_create_internal(tdm_private_display *private_display, tdm_error *error);
340 void
341 tdm_pp_destroy_internal(tdm_private_pp *private_pp);
342
343 tdm_private_capture *
344 tdm_capture_create_output_internal(tdm_private_output *private_output,
345                                                                    tdm_error *error);
346 tdm_private_capture *
347 tdm_capture_create_layer_internal(tdm_private_layer *private_layer,
348                                                                   tdm_error *error);
349 void
350 tdm_capture_destroy_internal(tdm_private_capture *private_capture);
351
352 /* utility buffer functions for private */
353 tdm_buffer_info*
354 tdm_buffer_get_info(tbm_surface_h buffer);
355 tbm_surface_h
356 tdm_buffer_list_get_first_entry(struct list_head *list);
357 void
358 tdm_buffer_list_dump(struct list_head *list);
359
360 /* event functions for private */
361 tdm_error
362 tdm_event_loop_init(tdm_private_display *private_display);
363 void
364 tdm_event_loop_deinit(tdm_private_display *private_display);
365 void
366 tdm_event_loop_create_backend_source(tdm_private_display *private_display);
367 int
368 tdm_event_loop_get_fd(tdm_private_display *private_display);
369 tdm_error
370 tdm_event_loop_dispatch(tdm_private_display *private_display);
371 void
372 tdm_event_loop_flush(tdm_private_display *private_display);
373
374 typedef enum {
375         TDM_THREAD_CB_NONE,
376         TDM_THREAD_CB_OUTPUT_COMMIT,
377         TDM_THREAD_CB_OUTPUT_VBLANK,
378         TDM_THREAD_CB_OUTPUT_STATUS,
379         TDM_THREAD_CB_PP_DONE,
380         TDM_THREAD_CB_CAPTURE_DONE,
381 } tdm_thread_cb_type;
382
383 typedef struct _tdm_thread_cb_base tdm_thread_cb_base;
384 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_commit;
385 typedef struct _tdm_thread_cb_output_vblank tdm_thread_cb_output_vblank;
386 typedef struct _tdm_thread_cb_output_status tdm_thread_cb_output_status;
387 typedef struct _tdm_thread_cb_pp_done tdm_thread_cb_pp_done;
388 typedef struct _tdm_thread_cb_capture_done tdm_thread_cb_capture_done;
389
390 struct _tdm_thread_cb_base {
391         tdm_thread_cb_type type;
392         unsigned int length;
393 };
394
395 struct _tdm_thread_cb_output_vblank {
396         tdm_thread_cb_base base;
397         unsigned long output_stamp;
398         unsigned int sequence;
399         unsigned int tv_sec;
400         unsigned int tv_usec;
401         void *user_data;
402 };
403
404 struct _tdm_thread_cb_output_status {
405         tdm_thread_cb_base base;
406         unsigned long output_stamp;
407         tdm_output_conn_status status;
408         void *user_data;
409 };
410
411 struct _tdm_thread_cb_pp_done {
412         tdm_thread_cb_base base;
413         unsigned long pp_stamp;
414         tbm_surface_h src;
415         tbm_surface_h dst;
416         void *user_data;
417 };
418
419 struct _tdm_thread_cb_capture_done {
420         tdm_thread_cb_base base;
421         unsigned long capture_stamp;
422         tbm_surface_h buffer;
423         void *user_data;
424 };
425
426 tdm_error
427 tdm_thread_init(tdm_private_loop *private_loop);
428 void
429 tdm_thread_deinit(tdm_private_loop *private_loop);
430 int
431 tdm_thread_get_fd(tdm_private_loop *private_loop);
432 tdm_error
433 tdm_thread_send_cb(tdm_private_loop *private_loop, tdm_thread_cb_base *base);
434 tdm_error
435 tdm_thread_handle_cb(tdm_private_loop *private_loop);
436 int
437 tdm_thread_in_display_thread(pid_t tid);
438 int
439 tdm_thread_is_running(void);
440
441 tdm_error
442 tdm_server_init(tdm_private_loop *private_loop);
443 void
444 tdm_server_deinit(tdm_private_loop *private_loop);
445
446 unsigned long
447 tdm_helper_get_time_in_millis(void);
448 unsigned long
449 tdm_helper_get_time_in_micros(void);
450
451 extern pthread_mutex_t tdm_mutex_check_lock;
452 extern int tdm_mutex_locked;
453 extern int tdm_dump_enable;
454
455 #define _pthread_mutex_unlock(l) \
456         do { \
457                 if (tdm_debug_mutex) \
458                         TDM_INFO("mutex unlock"); \
459                 pthread_mutex_lock(&tdm_mutex_check_lock); \
460                 tdm_mutex_locked = 0; \
461                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
462                 pthread_mutex_unlock(l); \
463         } while (0)
464 #ifdef TDM_CONFIG_MUTEX_TIMEOUT
465 #define MUTEX_TIMEOUT_SEC 5
466 #define _pthread_mutex_lock(l) \
467         do { \
468                 if (tdm_debug_mutex) \
469                         TDM_INFO("mutex lock"); \
470                 struct timespec rtime; \
471                 clock_gettime(CLOCK_REALTIME, &rtime); \
472                 rtime.tv_sec += MUTEX_TIMEOUT_SEC; \
473                 if (pthread_mutex_timedlock(l, &rtime)) { \
474                         TDM_ERR("Mutex lock failed PID %d", getpid()); \
475                         _pthread_mutex_unlock(l); \
476                 } \
477                 else { \
478                         pthread_mutex_lock(&tdm_mutex_check_lock); \
479                         tdm_mutex_locked = 1; \
480                         pthread_mutex_unlock(&tdm_mutex_check_lock); \
481                 } \
482         } while (0)
483 #else //TDM_CONFIG_MUTEX_TIMEOUT
484 #define _pthread_mutex_lock(l) \
485         do { \
486                 if (tdm_debug_mutex) \
487                         TDM_INFO("mutex lock"); \
488                 pthread_mutex_lock(l); \
489                 pthread_mutex_lock(&tdm_mutex_check_lock); \
490                 tdm_mutex_locked = 1; \
491                 pthread_mutex_unlock(&tdm_mutex_check_lock); \
492         } while (0)
493 #endif //TDM_CONFIG_MUTEX_TIMEOUT
494 //#define TDM_MUTEX_IS_LOCKED() (tdm_mutex_locked == 1)
495 static inline int TDM_MUTEX_IS_LOCKED(void)
496 {
497         int ret;
498         pthread_mutex_lock(&tdm_mutex_check_lock);
499         ret = (tdm_mutex_locked == 1);
500         pthread_mutex_unlock(&tdm_mutex_check_lock);
501         return ret;
502 }
503
504 tdm_error
505 _tdm_display_lock(tdm_display *dpy, const char *func);
506 void
507 _tdm_display_unlock(tdm_display *dpy, const char *func);
508
509 #define tdm_display_lock(dpy)   _tdm_display_lock(dpy, __FUNCTION__)
510 #define tdm_display_unlock(dpy)   _tdm_display_unlock(dpy, __FUNCTION__)
511
512 tdm_error
513 tdm_display_update_output(tdm_private_display *private_display,
514                                                   tdm_output *output_backend, int pipe);
515
516
517 /**
518  * @brief The tdm vblank object
519  */
520 typedef void tdm_vblank;
521
522 typedef void (*tdm_vblank_handler)(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
523                                                                    unsigned int tv_sec, unsigned int tv_usec, void *user_data);
524
525 tdm_vblank*
526 tdm_vblank_create(tdm_display *dpy, tdm_output *output, tdm_error *error);
527 void
528 tdm_vblank_destroy(tdm_vblank *vblank);
529 tdm_error
530 tdm_vblank_set_fps(tdm_vblank *vblank, unsigned int fps);
531 tdm_error
532 tdm_vblank_set_offset(tdm_vblank *vblank, int offset);
533 tdm_error
534 tdm_vblank_set_enable_fake(tdm_vblank *vblank, unsigned int enable_fake);
535 tdm_error
536 tdm_vblank_wait(tdm_vblank *vblank, unsigned int req_sec, unsigned int req_usec, unsigned int interval, tdm_vblank_handler func, void *user_data);
537
538 #ifdef __cplusplus
539 }
540 #endif
541
542 #endif /* _TDM_PRIVATE_H_ */