loader: remove clamp_swap_interval()
[mesa.git] / src / loader / loader_dri3_helper.h
1 /*
2 * Copyright © 2013 Keith Packard
3 * Copyright © 2015 Boyan Ding
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
14 *
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 * OF THIS SOFTWARE.
22 */
23
24 #ifndef LOADER_DRI3_HEADER_H
25 #define LOADER_DRI3_HEADER_H
26
27 #include <stdbool.h>
28 #include <stdint.h>
29
30 #include <xcb/xcb.h>
31 #include <xcb/dri3.h>
32 #include <xcb/present.h>
33
34 #include <GL/gl.h>
35 #include <GL/internal/dri_interface.h>
36
37 enum loader_dri3_buffer_type {
38 loader_dri3_buffer_back = 0,
39 loader_dri3_buffer_front = 1
40 };
41
42 struct loader_dri3_buffer {
43 __DRIimage *image;
44 __DRIimage *linear_buffer;
45 uint32_t pixmap;
46
47 /* Synchronization between the client and X server is done using an
48 * xshmfence that is mapped into an X server SyncFence. This lets the
49 * client check whether the X server is done using a buffer with a simple
50 * xshmfence call, rather than going to read X events from the wire.
51 *
52 * However, we can only wait for one xshmfence to be triggered at a time,
53 * so we need to know *which* buffer is going to be idle next. We do that
54 * by waiting for a PresentIdleNotify event. When that event arrives, the
55 * 'busy' flag gets cleared and the client knows that the fence has been
56 * triggered, and that the wait call will not block.
57 */
58
59 uint32_t sync_fence; /* XID of X SyncFence object */
60 struct xshmfence *shm_fence; /* pointer to xshmfence object */
61 bool busy; /* Set on swap, cleared on IdleNotify */
62 bool own_pixmap; /* We allocated the pixmap ID, free on destroy */
63
64 uint32_t size;
65 uint32_t pitch;
66 uint32_t cpp;
67 uint32_t flags;
68 uint32_t width, height;
69 uint64_t last_swap;
70
71 enum loader_dri3_buffer_type buffer_type;
72 };
73
74
75 #define LOADER_DRI3_MAX_BACK 4
76 #define LOADER_DRI3_BACK_ID(i) (i)
77 #define LOADER_DRI3_FRONT_ID (LOADER_DRI3_MAX_BACK)
78
79 static inline int
80 loader_dri3_pixmap_buf_id(enum loader_dri3_buffer_type buffer_type)
81 {
82 if (buffer_type == loader_dri3_buffer_back)
83 return LOADER_DRI3_BACK_ID(0);
84 else
85 return LOADER_DRI3_FRONT_ID;
86 }
87
88 struct loader_dri3_extensions {
89 const __DRIcoreExtension *core;
90 const __DRIimageDriverExtension *image_driver;
91 const __DRI2flushExtension *flush;
92 const __DRI2configQueryExtension *config;
93 const __DRItexBufferExtension *tex_buffer;
94 const __DRIimageExtension *image;
95 };
96
97 struct loader_dri3_drawable;
98
99 struct loader_dri3_vtable {
100 int (*get_swap_interval)(struct loader_dri3_drawable *);
101 void (*set_swap_interval)(struct loader_dri3_drawable *, int);
102 void (*set_drawable_size)(struct loader_dri3_drawable *, int, int);
103 bool (*in_current_context)(struct loader_dri3_drawable *);
104 __DRIcontext *(*get_dri_context)(struct loader_dri3_drawable *);
105 __DRIscreen *(*get_dri_screen)(struct loader_dri3_drawable *);
106 void (*flush_drawable)(struct loader_dri3_drawable *, unsigned);
107 void (*show_fps)(struct loader_dri3_drawable *, uint64_t);
108 };
109
110 #define LOADER_DRI3_NUM_BUFFERS (1 + LOADER_DRI3_MAX_BACK)
111
112 struct loader_dri3_drawable {
113 xcb_connection_t *conn;
114 __DRIdrawable *dri_drawable;
115 xcb_drawable_t drawable;
116 int width;
117 int height;
118 int depth;
119 uint8_t have_back;
120 uint8_t have_fake_front;
121 uint8_t is_pixmap;
122 uint8_t flipping;
123
124 /* Information about the GPU owning the buffer */
125 __DRIscreen *dri_screen;
126 bool is_different_gpu;
127
128 /* Present extension capabilities
129 */
130 uint32_t present_capabilities;
131
132 /* SBC numbers are tracked by using the serial numbers
133 * in the present request and complete events
134 */
135 uint64_t send_sbc;
136 uint64_t recv_sbc;
137
138 /* Last received UST/MSC values for pixmap present complete */
139 uint64_t ust, msc;
140
141 /* Last received UST/MSC values from present notify msc event */
142 uint64_t notify_ust, notify_msc;
143
144 /* Serial numbers for tracking wait_for_msc events */
145 uint32_t send_msc_serial;
146 uint32_t recv_msc_serial;
147
148 struct loader_dri3_buffer *buffers[LOADER_DRI3_NUM_BUFFERS];
149 int cur_back;
150 int num_back;
151
152 uint32_t *stamp;
153
154 xcb_present_event_t eid;
155 xcb_gcontext_t gc;
156 xcb_special_event_t *special_event;
157
158 bool first_init;
159
160 struct loader_dri3_extensions *ext;
161 const struct loader_dri3_vtable *vtable;
162 };
163
164 void
165 loader_dri3_set_swap_interval(struct loader_dri3_drawable *draw,
166 int interval);
167
168 void
169 loader_dri3_drawable_fini(struct loader_dri3_drawable *draw);
170
171 int
172 loader_dri3_drawable_init(xcb_connection_t *conn,
173 xcb_drawable_t drawable,
174 __DRIscreen *dri_screen,
175 bool is_different_gpu,
176 const __DRIconfig *dri_config,
177 struct loader_dri3_extensions *ext,
178 const struct loader_dri3_vtable *vtable,
179 struct loader_dri3_drawable*);
180
181 bool loader_dri3_wait_for_msc(struct loader_dri3_drawable *draw,
182 int64_t target_msc,
183 int64_t divisor, int64_t remainder,
184 int64_t *ust, int64_t *msc, int64_t *sbc);
185
186 int64_t
187 loader_dri3_swap_buffers_msc(struct loader_dri3_drawable *draw,
188 int64_t target_msc, int64_t divisor,
189 int64_t remainder, unsigned flush_flags,
190 bool force_copy);
191
192 int
193 loader_dri3_wait_for_sbc(struct loader_dri3_drawable *draw,
194 int64_t target_sbc, int64_t *ust,
195 int64_t *msc, int64_t *sbc);
196
197 int loader_dri3_query_buffer_age(struct loader_dri3_drawable *draw);
198
199 void
200 loader_dri3_flush(struct loader_dri3_drawable *draw,
201 unsigned flags,
202 enum __DRI2throttleReason throttle_reason);
203
204 void
205 loader_dri3_copy_sub_buffer(struct loader_dri3_drawable *draw,
206 int x, int y,
207 int width, int height,
208 bool flush);
209
210 void
211 loader_dri3_copy_drawable(struct loader_dri3_drawable *draw,
212 xcb_drawable_t dest,
213 xcb_drawable_t src);
214
215 void
216 loader_dri3_wait_x(struct loader_dri3_drawable *draw);
217
218 void
219 loader_dri3_wait_gl(struct loader_dri3_drawable *draw);
220
221 int loader_dri3_open(xcb_connection_t *conn,
222 xcb_window_t root,
223 uint32_t provider);
224
225 __DRIimage *
226 loader_dri3_create_image(xcb_connection_t *c,
227 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply,
228 unsigned int format,
229 __DRIscreen *dri_screen,
230 const __DRIimageExtension *image,
231 void *loaderPrivate);
232
233 int
234 loader_dri3_get_buffers(__DRIdrawable *driDrawable,
235 unsigned int format,
236 uint32_t *stamp,
237 void *loaderPrivate,
238 uint32_t buffer_mask,
239 struct __DRIimageList *buffers);
240
241 void
242 loader_dri3_update_drawable_geometry(struct loader_dri3_drawable *draw);
243 #endif