3 #endif /* ifdef HAVE_CONFIG_H */
6 #include "ecore_x_private.h"
14 #include <sys/types.h>
18 #define ECORE_X_VSYNC_DRI2 1
20 #ifdef ECORE_X_VSYNC_DRI2
21 // relevant header bits of dri/drm inlined here to avoid needing external
24 typedef unsigned int drm_magic_t;
28 DRM_VBLANK_ABSOLUTE = 0x00000000,
29 DRM_VBLANK_RELATIVE = 0x00000001,
30 DRM_VBLANK_EVENT = 0x04000000,
31 DRM_VBLANK_FLIP = 0x08000000,
32 DRM_VBLANK_NEXTONMISS = 0x10000000,
33 DRM_VBLANK_SECONDARY = 0x20000000,
34 DRM_VBLANK_SIGNAL = 0x40000000
38 typedef struct _drmVBlankReq
40 drmVBlankSeqType type;
41 unsigned int sequence;
45 typedef struct _drmVBlankReply
47 drmVBlankSeqType type;
48 unsigned int sequence;
53 typedef union _drmVBlank
59 #define DRM_EVENT_CONTEXT_VERSION 2
61 typedef struct _drmEventContext
64 void (*vblank_handler)(int fd,
65 unsigned int sequence,
69 void (*page_flip_handler)(int fd,
70 unsigned int sequence,
76 static int (*sym_drmClose)(int fd) = NULL;
77 static int (*sym_drmGetMagic)(int fd,
78 drm_magic_t *magic) = NULL;
79 static int (*sym_drmWaitVBlank)(int fd,
80 drmVBlank *vbl) = NULL;
81 static int (*sym_drmHandleEvent)(int fd,
82 drmEventContext *evctx) = NULL;
86 static Bool (*sym_DRI2QueryExtension)(Display *display,
88 int *errorBase) = NULL;
89 static Bool (*sym_DRI2QueryVersion)(Display *display,
92 static Bool (*sym_DRI2Connect)(Display *display,
95 char **deviceName) = NULL;
96 static Bool (*sym_DRI2Authenticate)(Display *display,
98 drm_magic_t magic) = NULL;
100 //// dri/drm data needed
101 static int dri2_event = 0;
102 static int dri2_error = 0;
103 static int dri2_major = 0;
104 static int dri2_minor = 0;
105 static char *device_name = 0;
106 static char *driver_name = 0;
107 static drm_magic_t drm_magic;
109 static int drm_fd = -1;
110 static int drm_event_is_busy = 0;
111 static int drm_animators_interval = 1;
112 static drmEventContext drm_evctx;
113 static Ecore_Fd_Handler *dri_drm_fdh = NULL;
115 static void *dri_lib = NULL;
116 static void *drm_lib = NULL;
118 static Window dri_drm_vsync_root = 0;
121 _dri_drm_tick_schedule(void)
125 vbl.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT;
126 vbl.request.sequence = drm_animators_interval;
127 vbl.request.signal = 0;
128 sym_drmWaitVBlank(drm_fd, &vbl);
132 _dri_drm_tick_begin(void *data __UNUSED__)
134 drm_event_is_busy = 1;
135 _dri_drm_tick_schedule();
139 _dri_drm_tick_end(void *data __UNUSED__)
141 drm_event_is_busy = 0;
145 _dri_drm_vblank_handler(int fd __UNUSED__,
146 unsigned int frame __UNUSED__,
147 unsigned int sec __UNUSED__,
148 unsigned int usec __UNUSED__,
149 void *data __UNUSED__)
151 ecore_animator_custom_tick();
152 if (drm_event_is_busy) _dri_drm_tick_schedule();
156 _dri_drm_cb(void *data __UNUSED__,
157 Ecore_Fd_Handler *fd_handler __UNUSED__)
159 sym_drmHandleEvent(drm_fd, &drm_evctx);
160 return ECORE_CALLBACK_RENEW;
163 // yes. most evil. we dlopen libdrm and libGL etc. to manually find smbols
164 // so we can be as compatible as possible given the whole mess of the
165 // gl/dri/drm etc. world. and handle graceful failure at runtime not
170 const char *drm_libs[] =
178 const char *dri_libs[] =
193 #define SYM(lib, xx) \
195 sym_ ## xx = dlsym(lib, #xx); \
196 if (!(sym_ ## xx)) { \
197 fprintf(stderr, "%s\n", dlerror()); \
202 if (dri_lib) return 1;
203 for (i = 0; drm_libs[i]; i++)
205 drm_lib = dlopen(drm_libs[i], RTLD_LOCAL | RTLD_LAZY);
209 SYM(drm_lib, drmClose);
210 SYM(drm_lib, drmWaitVBlank);
211 SYM(drm_lib, drmHandleEvent);
220 if (!drm_lib) return 0;
221 for (i = 0; dri_libs[i]; i++)
223 dri_lib = dlopen(dri_libs[i], RTLD_LOCAL | RTLD_LAZY);
227 SYM(dri_lib, DRI2QueryExtension);
228 SYM(dri_lib, DRI2QueryVersion);
229 SYM(dri_lib, DRI2Connect);
230 SYM(dri_lib, DRI2Authenticate);
251 if (!sym_DRI2QueryExtension(_ecore_x_disp, &dri2_event, &dri2_error))
253 if (!sym_DRI2QueryVersion(_ecore_x_disp, &dri2_major, &dri2_minor))
257 if (!sym_DRI2Connect(_ecore_x_disp, dri_drm_vsync_root, &driver_name, &device_name))
259 drm_fd = open(device_name, O_RDWR);
262 sym_drmGetMagic(drm_fd, &drm_magic);
263 if (!sym_DRI2Authenticate(_ecore_x_disp, dri_drm_vsync_root, drm_magic))
269 memset(&drm_evctx, 0, sizeof(drm_evctx));
270 drm_evctx.version = DRM_EVENT_CONTEXT_VERSION;
271 drm_evctx.vblank_handler = _dri_drm_vblank_handler;
272 drm_evctx.page_flip_handler = NULL;
274 dri_drm_fdh = ecore_main_fd_handler_add(drm_fd, ECORE_FD_READ,
275 _dri_drm_cb, NULL, NULL, NULL);
286 _dri_drm_shutdown(void)
295 ecore_main_fd_handler_del(dri_drm_fdh);
303 ecore_x_vsync_animator_tick_source_set(Ecore_X_Window win)
305 #ifdef ECORE_X_VSYNC_DRI2
308 root = ecore_x_window_root_get(win);
309 if (root != dri_drm_vsync_root)
311 dri_drm_vsync_root = root;
312 if (dri_drm_vsync_root)
314 if (!_dri_drm_link())
316 ecore_animator_source_set(ECORE_ANIMATOR_SOURCE_TIMER);
320 if (!_dri_drm_init())
322 dri_drm_vsync_root = 0;
323 ecore_animator_source_set(ECORE_ANIMATOR_SOURCE_TIMER);
326 ecore_animator_custom_source_tick_begin_callback_set
327 (_dri_drm_tick_begin, NULL);
328 ecore_animator_custom_source_tick_end_callback_set
329 (_dri_drm_tick_end, NULL);
330 ecore_animator_source_set(ECORE_ANIMATOR_SOURCE_CUSTOM);
337 ecore_animator_custom_source_tick_begin_callback_set
339 ecore_animator_custom_source_tick_end_callback_set
341 ecore_animator_source_set(ECORE_ANIMATOR_SOURCE_TIMER);