Package version up to 3.0.6
[platform/adaptation/nexell/libtdm-nexell.git] / src / libtdm-nexell / tdm_nexell_types.h
1 /**************************************************************************
2
3 libtdm_nexell
4
5 Copyright 2017 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: SooChan Lim <sc1.lim@samsung.com>
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sub license, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial portions
19 of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 #ifndef _TDM_NEXELL_TYPES_H_
32 #define _TDM_NEXELL_TYPES_H_
33
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <pthread.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <limits.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/ioctl.h>
44 #include <fcntl.h>
45
46 #include <linux/fb.h>
47 #include <xf86drm.h>
48 #include <xf86drmMode.h>
49 #include <tbm_surface.h>
50 #include <tbm_surface_internal.h>
51 #include <tdm_backend.h>
52 #include <tdm_log.h>
53 #include <tdm_list.h>
54 #include <drm_fourcc.h>
55 #include <tdm_helper.h>
56
57 #if HAVE_UDEV
58 #include <libudev.h>
59 #endif
60
61 #define C(b, m)                   (((b) >> (m)) & 0xFF)
62 #define B(c, s)                   ((((unsigned int)(c)) & 0xff) << (s))
63 #define FOURCC(a, b, c, d)       (B(d, 24) | B(c, 16) | B(b, 8) | B(a, 0))
64 #define FOURCC_STR(id)    C(id, 0), C(id, 8), C(id, 16), C(id, 24)
65
66 #define IS_RGB(format)    (format == TBM_FORMAT_XRGB8888 || format == TBM_FORMAT_ARGB8888 || \
67                                                    format == TBM_FORMAT_XBGR8888 || format == TBM_FORMAT_ABGR8888)
68
69 #define CLEAR(x) memset(&(x), 0, sizeof(x))
70 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
71 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
72 #define SWAP(a, b)  ({int t; t = a; a = b; b = t; })
73 #define ROUNDUP(x)  (ceil(floor((float)(height) / 4)))
74
75 #define ALIGN_TO_16B(x) ((((x) + (1 <<  4) - 1) >>  4) <<  4)
76 #define ALIGN_TO_32B(x) ((((x) + (1 <<  5) - 1) >>  5) <<  5)
77 #define ALIGN_TO_128B(x)   ((((x) + (1 <<  7) - 1) >>  7) <<  7)
78 #define ALIGN_TO_2KB(x) ((((x) + (1 << 11) - 1) >> 11) << 11)
79 #define ALIGN_TO_8KB(x) ((((x) + (1 << 13) - 1) >> 13) << 13)
80 #define ALIGN_TO_64KB(x)   ((((x) + (1 << 16) - 1) >> 16) << 16)
81
82 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
83
84 #define NUM_BUFFERS    3
85
86 #define NUM_LAYERS     3
87 #define NUM_UI_LAYERS  2
88 #define ZPOS_MAX       2
89 #define ZPOS_CURSOR    2  // cursor
90 #define ZPOS_2         2  // ui
91 #define ZPOS_1         1  // ui, primary layer
92 #define ZPOS_0         0  // video
93 #define ZPOS_VIDEO1    0  // video
94 #define ZPOS_NONE      -999
95
96 #define RETURN_VAL_IF_FAIL(cond, val) {\
97         if (!(cond)) {\
98                 TDM_ERR("'%s' failed", #cond);\
99                 return val;\
100         } \
101 }
102
103 #define RETURN_IF_FAIL(cond) {\
104         if (!(cond)) {\
105                 TDM_ERR("'%s' failed", #cond);\
106                 return;\
107         } \
108 }
109
110 #define GOTO_IF_FAIL(cond, val) {\
111         if (!(cond)) {\
112                 TDM_ERR("'%s' failed", #cond);\
113                 goto val;\
114         } \
115 }
116
117 typedef struct _tdm_nexell_data tdm_nexell_data;
118 typedef struct _tdm_nexell_output_data tdm_nexell_output_data;
119 typedef struct _tdm_nexell_layer_data tdm_nexell_layer_data;
120 typedef struct _tdm_nexell_hwc_data tdm_nexell_hwc_data;
121 typedef struct _tdm_nexell_hwc_window_data tdm_nexell_hwc_window_data;
122 typedef struct _tdm_nexell_event_data tdm_nexell_event_data;
123 typedef struct _tdm_nexell_display_buffer tdm_nexell_display_buffer;
124
125 typedef enum {
126         TDM_NEXELL_EVENT_TYPE_WAIT,
127         TDM_NEXELL_EVENT_TYPE_COMMIT,
128         TDM_NEXELL_EVENT_TYPE_PAGEFLIP,
129 } tdm_nexell_event_type;
130
131 struct _tdm_nexell_data
132 {
133         tdm_display *dpy;
134
135         int drm_fd;
136         int scaler_fd;
137
138 #if LIBDRM_MAJOR_VERSION >= 2 && LIBDRM_MINOR_VERSION >= 4  && LIBDRM_MICRO_VERSION >= 47
139         int has_universal_plane;
140         int has_atomic;
141 #endif
142
143 #if HAVE_UDEV
144         struct udev_monitor *uevent_monitor;
145         tdm_event_loop_source *uevent_source;
146 #endif
147
148         drmModeResPtr mode_res;
149         drmModePlaneResPtr plane_res;
150
151         int hwc_mode;
152
153         struct list_head output_list;
154         struct list_head buffer_list;
155 };
156
157 struct _tdm_nexell_display_buffer {
158         struct list_head link;
159
160         unsigned int fb_id;
161         tbm_surface_h buffer;
162         int width;
163         unsigned int height;
164         unsigned int format;
165         unsigned int handles[4];
166         unsigned int fds[4];
167         unsigned int pitches[4];
168         unsigned int offsets[4];
169         unsigned int size;
170         unsigned int count;
171 };
172
173 struct _tdm_nexell_event_data {
174         tdm_nexell_event_type type;
175         tdm_nexell_output_data *output_data;
176         void *user_data;
177 };
178
179 struct display_properties_ids {
180         uint32_t connector_crtc_id;
181         uint32_t crtc_mode_id;
182         uint32_t crtc_active;
183         uint32_t plane_fb_id;
184         uint32_t plane_crtc_id;
185         uint32_t plane_src_x;
186         uint32_t plane_src_y;
187         uint32_t plane_src_w;
188         uint32_t plane_src_h;
189         uint32_t plane_crtc_x;
190         uint32_t plane_crtc_y;
191         uint32_t plane_crtc_w;
192         uint32_t plane_crtc_h;
193         uint32_t plane_zpos;
194 };
195
196 struct _tdm_nexell_output_data {
197         struct list_head link;
198
199         /* data which are fixed at initializing */
200         tdm_nexell_data *nexell_data;
201         uint32_t connector_id;
202         uint32_t encoder_id;
203         uint32_t crtc_id;
204         uint32_t pipe;
205         uint32_t dpms_prop_id;
206         int count_modes;
207         drmModeModeInfoPtr drm_modes;
208         tdm_output_mode *output_modes;
209         tdm_output_type connector_type;
210         unsigned int connector_type_id;
211         struct list_head layer_list;
212         tdm_nexell_layer_data *primary_layer;
213
214         /* not fixed data below */
215         tdm_output_vblank_handler vblank_func;
216         tdm_output_commit_handler commit_func;
217
218         tdm_output_conn_status status;
219         tdm_output_status_handler status_func;
220         void *status_user_data;
221
222         int mode_changed;
223         const tdm_output_mode *current_mode;
224
225         struct display_properties_ids props;
226
227         int crtc_enabled;
228
229         /* atomic prop ids*/
230         struct {
231                 uint32_t crtc_id;
232                 uint32_t crtc_mode_id;
233                 uint32_t crtc_active;
234         } atomic_props_ids;
235
236         /* hwc */
237         int hwc_enable;
238         tdm_nexell_hwc_data *hwc_data;
239 };
240
241 struct _tdm_nexell_layer_data {
242         struct list_head link;
243
244         /* data which are fixed at initializing */
245         tdm_nexell_data *nexell_data;
246         tdm_nexell_output_data *output_data;
247         uint32_t plane_id;
248         tdm_layer_capability capabilities;
249         int zpos;
250
251         /* not fixed data below */
252         tdm_info_layer info;
253         int info_changed;
254
255         tdm_nexell_display_buffer *display_buffer;
256         int display_buffer_changed;
257
258         /* atomic prop ids*/
259         struct {
260                 uint32_t fb_id;
261                 uint32_t crtc_id;
262                 uint32_t src_x;
263                 uint32_t src_y;
264                 uint32_t src_w;
265                 uint32_t src_h;
266                 uint32_t crtc_x;
267                 uint32_t crtc_y;
268                 uint32_t crtc_w;
269                 uint32_t crtc_h;
270                 uint32_t zpos;
271         } atomic_props_ids;
272 };
273
274 struct _tdm_nexell_hwc_data {
275         tdm_nexell_hwc_window_data *target_hwc_window;
276
277         int need_validate;
278         int need_target_window;
279         int need_set_crtc;
280
281         int target_window_zpos;
282
283         tdm_nexell_output_data *output_data;
284         struct list_head hwc_window_list;
285
286         tbm_surface_queue_h cursor_tqueue;
287         tbm_surface_h cursor_tsurface;
288
289         /* UI buffer_queue list for the reserved scanout memory */
290         struct {
291                 tbm_surface_queue_h tqueue;
292                 int ref_cnt;
293         } ui_buffer_queue[NUM_LAYERS];
294
295         tdm_hwc_commit_handler commit_func;
296 };
297
298 struct _tdm_nexell_hwc_window_data {
299         struct list_head link;
300
301         tdm_nexell_hwc_data *hwc_data;
302
303         tdm_hwc_window_info info;
304         tbm_surface_h surface;
305         tdm_hwc_window_composition client_type;
306         tdm_hwc_window_composition validated_type;
307         int lzpos;
308         int lzpos_queue;
309
310         char name[TDM_NAME_LEN];
311         struct {
312                 int width;
313                 int height;
314                 int stride;
315                 void *ptr;
316         } cursor_img;
317         int cursor_img_surface;
318         int cursor_img_refresh;
319
320         int constraints;
321 };
322
323 #endif /* _TDM_NEXELL_TYPES_H_ */