1 /*
2  * Copyright © 2013 Keith Packard
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #ifndef _PRESENT_PRIV_H_
24 #define _PRESENT_PRIV_H_
25 
26 #include <X11/X.h>
27 #include "scrnintstr.h"
28 #include "misc.h"
29 #include "list.h"
30 #include "windowstr.h"
31 #include "dixstruct.h"
32 #include "present.h"
33 #include <syncsdk.h>
34 #include <syncsrv.h>
35 #include <xfixes.h>
36 #include <randrstr.h>
37 #include <inttypes.h>
38 
39 #if 0
40 #define DebugPresent(x) ErrorF x
41 #else
42 #define DebugPresent(x)
43 #endif
44 
45 extern int present_request;
46 
47 extern DevPrivateKeyRec present_screen_private_key;
48 
49 typedef struct present_fence *present_fence_ptr;
50 
51 typedef struct present_notify present_notify_rec, *present_notify_ptr;
52 
53 struct present_notify {
54     struct xorg_list    window_list;
55     WindowPtr           window;
56     CARD32              serial;
57 };
58 
59 struct present_vblank {
60     struct xorg_list    window_list;
61     struct xorg_list    event_queue;
62     ScreenPtr           screen;
63     WindowPtr           window;
64     PixmapPtr           pixmap;
65     RegionPtr           valid;
66     RegionPtr           update;
67     RRCrtcPtr           crtc;
68     uint32_t            serial;
69     int16_t             x_off;
70     int16_t             y_off;
71     CARD16              kind;
72     uint64_t            event_id;
73     uint64_t            target_msc;     /* target MSC when present should complete */
74     uint64_t            exec_msc;       /* MSC at which present can be executed */
75     uint64_t            msc_offset;
76     present_fence_ptr   idle_fence;
77     present_fence_ptr   wait_fence;
78     present_notify_ptr  notifies;
79     int                 num_notifies;
80     Bool                queued;         /* on present_exec_queue */
81     Bool                flip;           /* planning on using flip */
82     Bool                flip_ready;     /* wants to flip, but waiting for previous flip or unflip */
83     Bool                flip_idler;     /* driver explicitly permitted idling */
84     Bool                sync_flip;      /* do flip synchronous to vblank */
85     Bool                abort_flip;     /* aborting this flip */
86     PresentFlipReason   reason;         /* reason for which flip is not possible */
87     Bool                has_suboptimal; /* whether client can support SuboptimalCopy mode */
88 };
89 
90 typedef struct present_screen_priv present_screen_priv_rec, *present_screen_priv_ptr;
91 typedef struct present_window_priv present_window_priv_rec, *present_window_priv_ptr;
92 
93 /*
94  * Mode hooks
95  */
96 typedef uint32_t (*present_priv_query_capabilities_ptr)(present_screen_priv_ptr screen_priv);
97 typedef RRCrtcPtr (*present_priv_get_crtc_ptr)(present_screen_priv_ptr screen_priv,
98                                                WindowPtr window);
99 
100 typedef Bool (*present_priv_check_flip_ptr)(RRCrtcPtr crtc,
101                                             WindowPtr window,
102                                             PixmapPtr pixmap,
103                                             Bool sync_flip,
104                                             RegionPtr valid,
105                                             int16_t x_off,
106                                             int16_t y_off,
107                                             PresentFlipReason *reason);
108 typedef void (*present_priv_check_flip_window_ptr)(WindowPtr window);
109 typedef Bool (*present_priv_can_window_flip_ptr)(WindowPtr window);
110 
111 typedef int (*present_priv_pixmap_ptr)(WindowPtr window,
112                                        PixmapPtr pixmap,
113                                        CARD32 serial,
114                                        RegionPtr valid,
115                                        RegionPtr update,
116                                        int16_t x_off,
117                                        int16_t y_off,
118                                        RRCrtcPtr target_crtc,
119                                        SyncFence *wait_fence,
120                                        SyncFence *idle_fence,
121                                        uint32_t options,
122                                        uint64_t window_msc,
123                                        uint64_t divisor,
124                                        uint64_t remainder,
125                                        present_notify_ptr notifies,
126                                        int num_notifies);
127 
128 typedef void (*present_priv_create_event_id_ptr)(present_window_priv_ptr window_priv,
129                                                  present_vblank_ptr vblank);
130 
131 typedef int (*present_priv_queue_vblank_ptr)(ScreenPtr screen,
132                                              WindowPtr window,
133                                              RRCrtcPtr crtc,
134                                              uint64_t event_id,
135                                              uint64_t msc);
136 typedef void (*present_priv_flush_ptr)(WindowPtr window);
137 typedef void (*present_priv_re_execute_ptr)(present_vblank_ptr vblank);
138 
139 typedef void (*present_priv_abort_vblank_ptr)(ScreenPtr screen,
140                                               WindowPtr window,
141                                               RRCrtcPtr crtc,
142                                               uint64_t event_id,
143                                               uint64_t msc);
144 typedef void (*present_priv_flip_destroy_ptr)(ScreenPtr screen);
145 
146 struct present_screen_priv {
147     CloseScreenProcPtr          CloseScreen;
148     ConfigNotifyProcPtr         ConfigNotify;
149     DestroyWindowProcPtr        DestroyWindow;
150     ClipNotifyProcPtr           ClipNotify;
151 
152     present_vblank_ptr          flip_pending;
153     uint64_t                    unflip_event_id;
154 
155     uint32_t                    fake_interval;
156 
157     /* Currently active flipped pixmap and fence */
158     RRCrtcPtr                   flip_crtc;
159     WindowPtr                   flip_window;
160     uint32_t                    flip_serial;
161     PixmapPtr                   flip_pixmap;
162     present_fence_ptr           flip_idle_fence;
163     Bool                        flip_sync;
164 
165     present_screen_info_ptr     info;
166     present_wnmd_info_ptr       wnmd_info;
167 
168     /* Mode hooks */
169     present_priv_query_capabilities_ptr query_capabilities;
170     present_priv_get_crtc_ptr           get_crtc;
171 
172     present_priv_check_flip_ptr         check_flip;
173     present_priv_check_flip_window_ptr  check_flip_window;
174     present_priv_can_window_flip_ptr    can_window_flip;
175 
176     present_priv_pixmap_ptr             present_pixmap;
177     present_priv_create_event_id_ptr    create_event_id;
178 
179     present_priv_queue_vblank_ptr       queue_vblank;
180     present_priv_flush_ptr              flush;
181     present_priv_re_execute_ptr         re_execute;
182 
183     present_priv_abort_vblank_ptr       abort_vblank;
184     present_priv_flip_destroy_ptr       flip_destroy;
185 };
186 
187 #define wrap(priv,real,mem,func) {\
188     priv->mem = real->mem; \
189     real->mem = func; \
190 }
191 
192 #define unwrap(priv,real,mem) {\
193     real->mem = priv->mem; \
194 }
195 
196 static inline present_screen_priv_ptr
present_screen_priv(ScreenPtr screen)197 present_screen_priv(ScreenPtr screen)
198 {
199     return (present_screen_priv_ptr)dixLookupPrivate(&(screen)->devPrivates, &present_screen_private_key);
200 }
201 
202 /*
203  * Each window has a list of clients and event masks
204  */
205 typedef struct present_event *present_event_ptr;
206 
207 typedef struct present_event {
208     present_event_ptr next;
209     ClientPtr client;
210     WindowPtr window;
211     XID id;
212     int mask;
213 } present_event_rec;
214 
215 struct present_window_priv {
216     WindowPtr              window;
217     present_event_ptr      events;
218     RRCrtcPtr              crtc;        /* Last reported CRTC from get_ust_msc */
219     uint64_t               msc_offset;
220     uint64_t               msc;         /* Last reported MSC from the current crtc */
221     struct xorg_list       vblank;
222     struct xorg_list       notifies;
223 
224     /* Used for window flips */
225     uint64_t               event_id;
226     struct xorg_list       exec_queue;
227     struct xorg_list       flip_queue;
228     struct xorg_list       idle_queue;
229 
230     present_vblank_ptr     flip_pending;
231     present_vblank_ptr     flip_active;
232 };
233 
234 #define PresentCrtcNeverSet     ((RRCrtcPtr) 1)
235 
236 extern DevPrivateKeyRec present_window_private_key;
237 
238 static inline present_window_priv_ptr
present_window_priv(WindowPtr window)239 present_window_priv(WindowPtr window)
240 {
241     return (present_window_priv_ptr)dixGetPrivate(&(window)->devPrivates, &present_window_private_key);
242 }
243 
244 present_window_priv_ptr
245 present_get_window_priv(WindowPtr window, Bool create);
246 
247 /*
248  * Returns:
249  * TRUE if the first MSC value is after the second one
250  * FALSE if the first MSC value is equal to or before the second one
251  */
252 static inline Bool
msc_is_after(uint64_t test,uint64_t reference)253 msc_is_after(uint64_t test, uint64_t reference)
254 {
255     return (int64_t)(test - reference) > 0;
256 }
257 
258 /*
259  * present.c
260  */
261 uint32_t
262 present_query_capabilities(RRCrtcPtr crtc);
263 
264 RRCrtcPtr
265 present_get_crtc(WindowPtr window);
266 
267 void
268 present_copy_region(DrawablePtr drawable,
269                     PixmapPtr pixmap,
270                     RegionPtr update,
271                     int16_t x_off,
272                     int16_t y_off);
273 
274 void
275 present_pixmap_idle(PixmapPtr pixmap, WindowPtr window, CARD32 serial, struct present_fence *present_fence);
276 
277 void
278 present_set_tree_pixmap(WindowPtr window,
279                         PixmapPtr expected,
280                         PixmapPtr pixmap);
281 
282 void
283 present_adjust_timings(uint32_t options,
284                        uint64_t *crtc_msc,
285                        uint64_t *target_msc,
286                        uint64_t divisor,
287                        uint64_t remainder);
288 
289 int
290 present_pixmap(WindowPtr window,
291                PixmapPtr pixmap,
292                CARD32 serial,
293                RegionPtr valid,
294                RegionPtr update,
295                int16_t x_off,
296                int16_t y_off,
297                RRCrtcPtr target_crtc,
298                SyncFence *wait_fence,
299                SyncFence *idle_fence,
300                uint32_t options,
301                uint64_t target_msc,
302                uint64_t divisor,
303                uint64_t remainder,
304                present_notify_ptr notifies,
305                int num_notifies);
306 
307 int
308 present_notify_msc(WindowPtr window,
309                    CARD32 serial,
310                    uint64_t target_msc,
311                    uint64_t divisor,
312                    uint64_t remainder);
313 
314 /*
315  * present_event.c
316  */
317 
318 void
319 present_free_events(WindowPtr window);
320 
321 void
322 present_send_config_notify(WindowPtr window, int x, int y, int w, int h, int bw, WindowPtr sibling);
323 
324 void
325 present_send_complete_notify(WindowPtr window, CARD8 kind, CARD8 mode, CARD32 serial, uint64_t ust, uint64_t msc);
326 
327 void
328 present_send_idle_notify(WindowPtr window, CARD32 serial, PixmapPtr pixmap, present_fence_ptr idle_fence);
329 
330 int
331 present_select_input(ClientPtr client,
332                      CARD32 eid,
333                      WindowPtr window,
334                      CARD32 event_mask);
335 
336 Bool
337 present_event_init(void);
338 
339 /*
340  * present_execute.c
341  */
342 Bool
343 present_execute_wait(present_vblank_ptr vblank, uint64_t crtc_msc);
344 
345 void
346 present_execute_copy(present_vblank_ptr vblank, uint64_t crtc_msc);
347 
348 void
349 present_execute_post(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc);
350 
351 /*
352  * present_fake.c
353  */
354 int
355 present_fake_get_ust_msc(ScreenPtr screen, uint64_t *ust, uint64_t *msc);
356 
357 int
358 present_fake_queue_vblank(ScreenPtr screen, uint64_t event_id, uint64_t msc);
359 
360 void
361 present_fake_abort_vblank(ScreenPtr screen, uint64_t event_id, uint64_t msc);
362 
363 void
364 present_fake_screen_init(ScreenPtr screen);
365 
366 void
367 present_fake_queue_init(void);
368 
369 /*
370  * present_fence.c
371  */
372 struct present_fence *
373 present_fence_create(SyncFence *sync_fence);
374 
375 void
376 present_fence_destroy(struct present_fence *present_fence);
377 
378 void
379 present_fence_set_triggered(struct present_fence *present_fence);
380 
381 Bool
382 present_fence_check_triggered(struct present_fence *present_fence);
383 
384 void
385 present_fence_set_callback(struct present_fence *present_fence,
386                            void (*callback)(void *param),
387                            void *param);
388 
389 XID
390 present_fence_id(struct present_fence *present_fence);
391 
392 /*
393  * present_notify.c
394  */
395 void
396 present_clear_window_notifies(WindowPtr window);
397 
398 void
399 present_free_window_notify(present_notify_ptr notify);
400 
401 int
402 present_add_window_notify(present_notify_ptr notify);
403 
404 int
405 present_create_notifies(ClientPtr client, int num_notifies, xPresentNotify *x_notifies, present_notify_ptr *p_notifies);
406 
407 void
408 present_destroy_notifies(present_notify_ptr notifies, int num_notifies);
409 
410 /*
411  * present_redirect.c
412  */
413 
414 WindowPtr
415 present_redirect(ClientPtr client, WindowPtr target);
416 
417 /*
418  * present_request.c
419  */
420 int
421 proc_present_dispatch(ClientPtr client);
422 
423 int
424 sproc_present_dispatch(ClientPtr client);
425 
426 /*
427  * present_scmd.c
428  */
429 void
430 present_abort_vblank(ScreenPtr screen, RRCrtcPtr crtc, uint64_t event_id, uint64_t msc);
431 
432 void
433 present_flip_destroy(ScreenPtr screen);
434 
435 void
436 present_restore_screen_pixmap(ScreenPtr screen);
437 
438 void
439 present_set_abort_flip(ScreenPtr screen);
440 
441 Bool
442 present_init(void);
443 
444 void
445 present_scmd_init_mode_hooks(present_screen_priv_ptr screen_priv);
446 
447 /*
448  * present_screen.c
449  */
450 
451 /*
452  * present_vblank.c
453  */
454 void
455 present_vblank_notify(present_vblank_ptr vblank, CARD8 kind, CARD8 mode, uint64_t ust, uint64_t crtc_msc);
456 
457 present_vblank_ptr
458 present_vblank_create(WindowPtr window,
459                       PixmapPtr pixmap,
460                       CARD32 serial,
461                       RegionPtr valid,
462                       RegionPtr update,
463                       int16_t x_off,
464                       int16_t y_off,
465                       RRCrtcPtr target_crtc,
466                       SyncFence *wait_fence,
467                       SyncFence *idle_fence,
468                       uint32_t options,
469                       const uint32_t *capabilities,
470                       present_notify_ptr notifies,
471                       int num_notifies,
472                       uint64_t target_msc,
473                       uint64_t crtc_msc);
474 
475 void
476 present_vblank_scrap(present_vblank_ptr vblank);
477 
478 void
479 present_vblank_destroy(present_vblank_ptr vblank);
480 
481 /*
482  * present_wnmd.c
483  */
484 void
485 present_wnmd_set_abort_flip(WindowPtr window);
486 
487 void
488 present_wnmd_init_mode_hooks(present_screen_priv_ptr screen_priv);
489 
490 #endif /*  _PRESENT_PRIV_H_ */
491