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 "dix-config.h"
27 #include <X11/X.h>
28 #include "scrnintstr.h"
29 #include "misc.h"
30 #include "list.h"
31 #include "windowstr.h"
32 #include "dixstruct.h"
33 #include "present.h"
34 #include <syncsdk.h>
35 #include <syncsrv.h>
36 #include <xfixes.h>
37 #include <randrstr.h>
38 #include <inttypes.h>
39 
40 #if 0
41 #define DebugPresent(x) ErrorF x
42 #else
43 #define DebugPresent(x)
44 #endif
45 
46 extern int present_request;
47 
48 extern DevPrivateKeyRec present_screen_private_key;
49 
50 typedef struct present_fence *present_fence_ptr;
51 
52 typedef struct present_notify present_notify_rec, *present_notify_ptr;
53 
54 struct present_notify {
55     struct xorg_list    window_list;
56     WindowPtr           window;
57     CARD32              serial;
58 };
59 
60 struct present_vblank {
61     struct xorg_list    window_list;
62     struct xorg_list    event_queue;
63     ScreenPtr           screen;
64     WindowPtr           window;
65     PixmapPtr           pixmap;
66     RegionPtr           valid;
67     RegionPtr           update;
68     RRCrtcPtr           crtc;
69     uint32_t            serial;
70     int16_t             x_off;
71     int16_t             y_off;
72     CARD16              kind;
73     uint64_t            event_id;
74     uint64_t            target_msc;     /* target MSC when present should complete */
75     uint64_t            exec_msc;       /* MSC at which present can be executed */
76     uint64_t            msc_offset;
77     present_fence_ptr   idle_fence;
78     present_fence_ptr   wait_fence;
79     present_notify_ptr  notifies;
80     int                 num_notifies;
81     Bool                queued;         /* on present_exec_queue */
82     Bool                flip;           /* planning on using flip */
83     Bool                flip_ready;     /* wants to flip, but waiting for previous flip or unflip */
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 typedef void (*present_priv_clear_window_flip_ptr)(WindowPtr window);
111 
112 typedef int (*present_priv_pixmap_ptr)(WindowPtr window,
113                                        PixmapPtr pixmap,
114                                        CARD32 serial,
115                                        RegionPtr valid,
116                                        RegionPtr update,
117                                        int16_t x_off,
118                                        int16_t y_off,
119                                        RRCrtcPtr target_crtc,
120                                        SyncFence *wait_fence,
121                                        SyncFence *idle_fence,
122                                        uint32_t options,
123                                        uint64_t window_msc,
124                                        uint64_t divisor,
125                                        uint64_t remainder,
126                                        present_notify_ptr notifies,
127                                        int num_notifies);
128 
129 typedef int (*present_priv_queue_vblank_ptr)(ScreenPtr screen,
130                                              WindowPtr window,
131                                              RRCrtcPtr crtc,
132                                              uint64_t event_id,
133                                              uint64_t msc);
134 typedef void (*present_priv_flush_ptr)(WindowPtr window);
135 typedef void (*present_priv_re_execute_ptr)(present_vblank_ptr vblank);
136 
137 typedef void (*present_priv_abort_vblank_ptr)(ScreenPtr screen,
138                                               WindowPtr window,
139                                               RRCrtcPtr crtc,
140                                               uint64_t event_id,
141                                               uint64_t msc);
142 typedef void (*present_priv_flip_destroy_ptr)(ScreenPtr screen);
143 
144 struct present_screen_priv {
145     CloseScreenProcPtr          CloseScreen;
146     ConfigNotifyProcPtr         ConfigNotify;
147     DestroyWindowProcPtr        DestroyWindow;
148     ClipNotifyProcPtr           ClipNotify;
149 
150     present_vblank_ptr          flip_pending;
151     uint64_t                    unflip_event_id;
152 
153     uint32_t                    fake_interval;
154 
155     /* Currently active flipped pixmap and fence */
156     RRCrtcPtr                   flip_crtc;
157     WindowPtr                   flip_window;
158     uint32_t                    flip_serial;
159     PixmapPtr                   flip_pixmap;
160     present_fence_ptr           flip_idle_fence;
161     Bool                        flip_sync;
162 
163     present_screen_info_ptr     info;
164 
165     /* Mode hooks */
166     present_priv_query_capabilities_ptr query_capabilities;
167     present_priv_get_crtc_ptr           get_crtc;
168 
169     present_priv_check_flip_ptr         check_flip;
170     present_priv_check_flip_window_ptr  check_flip_window;
171     present_priv_can_window_flip_ptr    can_window_flip;
172     present_priv_clear_window_flip_ptr  clear_window_flip;
173 
174     present_priv_pixmap_ptr             present_pixmap;
175 
176     present_priv_queue_vblank_ptr       queue_vblank;
177     present_priv_flush_ptr              flush;
178     present_priv_re_execute_ptr         re_execute;
179 
180     present_priv_abort_vblank_ptr       abort_vblank;
181     present_priv_flip_destroy_ptr       flip_destroy;
182 };
183 
184 #define wrap(priv,real,mem,func) {\
185     priv->mem = real->mem; \
186     real->mem = func; \
187 }
188 
189 #define unwrap(priv,real,mem) {\
190     real->mem = priv->mem; \
191 }
192 
193 static inline present_screen_priv_ptr
present_screen_priv(ScreenPtr screen)194 present_screen_priv(ScreenPtr screen)
195 {
196     return (present_screen_priv_ptr)dixLookupPrivate(&(screen)->devPrivates, &present_screen_private_key);
197 }
198 
199 /*
200  * Each window has a list of clients and event masks
201  */
202 typedef struct present_event *present_event_ptr;
203 
204 typedef struct present_event {
205     present_event_ptr next;
206     ClientPtr client;
207     WindowPtr window;
208     XID id;
209     int mask;
210 } present_event_rec;
211 
212 struct present_window_priv {
213     WindowPtr              window;
214     present_event_ptr      events;
215     RRCrtcPtr              crtc;        /* Last reported CRTC from get_ust_msc */
216     uint64_t               msc_offset;
217     uint64_t               msc;         /* Last reported MSC from the current crtc */
218     struct xorg_list       vblank;
219     struct xorg_list       notifies;
220 };
221 
222 #define PresentCrtcNeverSet     ((RRCrtcPtr) 1)
223 
224 extern DevPrivateKeyRec present_window_private_key;
225 
226 static inline present_window_priv_ptr
present_window_priv(WindowPtr window)227 present_window_priv(WindowPtr window)
228 {
229     return (present_window_priv_ptr)dixGetPrivate(&(window)->devPrivates, &present_window_private_key);
230 }
231 
232 present_window_priv_ptr
233 present_get_window_priv(WindowPtr window, Bool create);
234 
235 /*
236  * Returns:
237  * TRUE if the first MSC value is after the second one
238  * FALSE if the first MSC value is equal to or before the second one
239  */
240 static inline Bool
msc_is_after(uint64_t test,uint64_t reference)241 msc_is_after(uint64_t test, uint64_t reference)
242 {
243     return (int64_t)(test - reference) > 0;
244 }
245 
246 /*
247  * present.c
248  */
249 uint32_t
250 present_query_capabilities(RRCrtcPtr crtc);
251 
252 RRCrtcPtr
253 present_get_crtc(WindowPtr window);
254 
255 void
256 present_copy_region(DrawablePtr drawable,
257                     PixmapPtr pixmap,
258                     RegionPtr update,
259                     int16_t x_off,
260                     int16_t y_off);
261 
262 void
263 present_pixmap_idle(PixmapPtr pixmap, WindowPtr window, CARD32 serial, struct present_fence *present_fence);
264 
265 void
266 present_set_tree_pixmap(WindowPtr window,
267                         PixmapPtr expected,
268                         PixmapPtr pixmap);
269 
270 uint64_t
271 present_get_target_msc(uint64_t target_msc_arg,
272                        uint64_t crtc_msc,
273                        uint64_t divisor,
274                        uint64_t remainder,
275                        uint32_t options);
276 
277 int
278 present_pixmap(WindowPtr window,
279                PixmapPtr pixmap,
280                CARD32 serial,
281                RegionPtr valid,
282                RegionPtr update,
283                int16_t x_off,
284                int16_t y_off,
285                RRCrtcPtr target_crtc,
286                SyncFence *wait_fence,
287                SyncFence *idle_fence,
288                uint32_t options,
289                uint64_t target_msc,
290                uint64_t divisor,
291                uint64_t remainder,
292                present_notify_ptr notifies,
293                int num_notifies);
294 
295 int
296 present_notify_msc(WindowPtr window,
297                    CARD32 serial,
298                    uint64_t target_msc,
299                    uint64_t divisor,
300                    uint64_t remainder);
301 
302 /*
303  * present_event.c
304  */
305 
306 void
307 present_free_events(WindowPtr window);
308 
309 void
310 present_send_config_notify(WindowPtr window, int x, int y, int w, int h, int bw, WindowPtr sibling);
311 
312 void
313 present_send_complete_notify(WindowPtr window, CARD8 kind, CARD8 mode, CARD32 serial, uint64_t ust, uint64_t msc);
314 
315 void
316 present_send_idle_notify(WindowPtr window, CARD32 serial, PixmapPtr pixmap, present_fence_ptr idle_fence);
317 
318 int
319 present_select_input(ClientPtr client,
320                      CARD32 eid,
321                      WindowPtr window,
322                      CARD32 event_mask);
323 
324 Bool
325 present_event_init(void);
326 
327 /*
328  * present_execute.c
329  */
330 Bool
331 present_execute_wait(present_vblank_ptr vblank, uint64_t crtc_msc);
332 
333 void
334 present_execute_copy(present_vblank_ptr vblank, uint64_t crtc_msc);
335 
336 void
337 present_execute_post(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc);
338 
339 /*
340  * present_fake.c
341  */
342 int
343 present_fake_get_ust_msc(ScreenPtr screen, uint64_t *ust, uint64_t *msc);
344 
345 int
346 present_fake_queue_vblank(ScreenPtr screen, uint64_t event_id, uint64_t msc);
347 
348 void
349 present_fake_abort_vblank(ScreenPtr screen, uint64_t event_id, uint64_t msc);
350 
351 void
352 present_fake_screen_init(ScreenPtr screen);
353 
354 void
355 present_fake_queue_init(void);
356 
357 /*
358  * present_fence.c
359  */
360 struct present_fence *
361 present_fence_create(SyncFence *sync_fence);
362 
363 void
364 present_fence_destroy(struct present_fence *present_fence);
365 
366 void
367 present_fence_set_triggered(struct present_fence *present_fence);
368 
369 Bool
370 present_fence_check_triggered(struct present_fence *present_fence);
371 
372 void
373 present_fence_set_callback(struct present_fence *present_fence,
374                            void (*callback)(void *param),
375                            void *param);
376 
377 XID
378 present_fence_id(struct present_fence *present_fence);
379 
380 /*
381  * present_notify.c
382  */
383 void
384 present_clear_window_notifies(WindowPtr window);
385 
386 void
387 present_free_window_notify(present_notify_ptr notify);
388 
389 int
390 present_add_window_notify(present_notify_ptr notify);
391 
392 int
393 present_create_notifies(ClientPtr client, int num_notifies, xPresentNotify *x_notifies, present_notify_ptr *p_notifies);
394 
395 void
396 present_destroy_notifies(present_notify_ptr notifies, int num_notifies);
397 
398 /*
399  * present_redirect.c
400  */
401 
402 WindowPtr
403 present_redirect(ClientPtr client, WindowPtr target);
404 
405 /*
406  * present_request.c
407  */
408 int
409 proc_present_dispatch(ClientPtr client);
410 
411 int
412 sproc_present_dispatch(ClientPtr client);
413 
414 /*
415  * present_scmd.c
416  */
417 void
418 present_abort_vblank(ScreenPtr screen, RRCrtcPtr crtc, uint64_t event_id, uint64_t msc);
419 
420 void
421 present_flip_destroy(ScreenPtr screen);
422 
423 void
424 present_restore_screen_pixmap(ScreenPtr screen);
425 
426 void
427 present_set_abort_flip(ScreenPtr screen);
428 
429 Bool
430 present_init(void);
431 
432 void
433 present_scmd_init_mode_hooks(present_screen_priv_ptr screen_priv);
434 
435 /*
436  * present_screen.c
437  */
438 Bool
439 present_screen_register_priv_keys(void);
440 
441 present_screen_priv_ptr
442 present_screen_priv_init(ScreenPtr screen);
443 
444 /*
445  * present_vblank.c
446  */
447 void
448 present_vblank_notify(present_vblank_ptr vblank, CARD8 kind, CARD8 mode, uint64_t ust, uint64_t crtc_msc);
449 
450 Bool
451 present_vblank_init(present_vblank_ptr vblank,
452                     WindowPtr window,
453                     PixmapPtr pixmap,
454                     CARD32 serial,
455                     RegionPtr valid,
456                     RegionPtr update,
457                     int16_t x_off,
458                     int16_t y_off,
459                     RRCrtcPtr target_crtc,
460                     SyncFence *wait_fence,
461                     SyncFence *idle_fence,
462                     uint32_t options,
463                     const uint32_t capabilities,
464                     present_notify_ptr notifies,
465                     int num_notifies,
466                     uint64_t target_msc,
467                     uint64_t crtc_msc);
468 
469 present_vblank_ptr
470 present_vblank_create(WindowPtr window,
471                       PixmapPtr pixmap,
472                       CARD32 serial,
473                       RegionPtr valid,
474                       RegionPtr update,
475                       int16_t x_off,
476                       int16_t y_off,
477                       RRCrtcPtr target_crtc,
478                       SyncFence *wait_fence,
479                       SyncFence *idle_fence,
480                       uint32_t options,
481                       const uint32_t capabilities,
482                       present_notify_ptr notifies,
483                       int num_notifies,
484                       uint64_t target_msc,
485                       uint64_t crtc_msc);
486 
487 void
488 present_vblank_scrap(present_vblank_ptr vblank);
489 
490 void
491 present_vblank_destroy(present_vblank_ptr vblank);
492 
493 #endif /*  _PRESENT_PRIV_H_ */
494