loader/dri3: Improve dri3 thread-safety
[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 #include <c11/threads.h>
37
38 enum loader_dri3_buffer_type {
39 loader_dri3_buffer_back = 0,
40 loader_dri3_buffer_front = 1
41 };
42
43 struct loader_dri3_buffer {
44 __DRIimage *image;
45 __DRIimage *linear_buffer;
46 uint32_t pixmap;
47
48 /* Synchronization between the client and X server is done using an
49 * xshmfence that is mapped into an X server SyncFence. This lets the
50 * client check whether the X server is done using a buffer with a simple
51 * xshmfence call, rather than going to read X events from the wire.
52 *
53 * However, we can only wait for one xshmfence to be triggered at a time,
54 * so we need to know *which* buffer is going to be idle next. We do that
55 * by waiting for a PresentIdleNotify event. When that event arrives, the
56 * 'busy' flag gets cleared and the client knows that the fence has been
57 * triggered, and that the wait call will not block.
58 */
59
60 uint32_t sync_fence; /* XID of X SyncFence object */
61 struct xshmfence *shm_fence; /* pointer to xshmfence object */
62 bool busy; /* Set on swap, cleared on IdleNotify */
63 bool own_pixmap; /* We allocated the pixmap ID, free on destroy */
64
65 uint32_t size;
66 uint32_t pitch;
67 uint32_t cpp;
68 uint32_t flags;
69 uint32_t width, height;
70 uint64_t last_swap;
71 };
72
73
74 #define LOADER_DRI3_MAX_BACK 4
75 #define LOADER_DRI3_BACK_ID(i) (i)
76 #define LOADER_DRI3_FRONT_ID (LOADER_DRI3_MAX_BACK)
77
78 static inline int
79 loader_dri3_pixmap_buf_id(enum loader_dri3_buffer_type buffer_type)
80 {
81 if (buffer_type == loader_dri3_buffer_back)
82 return LOADER_DRI3_BACK_ID(0);
83 else
84 return LOADER_DRI3_FRONT_ID;
85 }
86
87 struct loader_dri3_extensions {
88 const __DRIcoreExtension *core;
89 const __DRIimageDriverExtension *image_driver;
90 const __DRI2flushExtension *flush;
91 const __DRI2configQueryExtension *config;
92 const __DRItexBufferExtension *tex_buffer;
93 const __DRIimageExtension *image;
94 };
95
96 struct loader_dri3_drawable;
97
98 struct loader_dri3_vtable {
99 void (*set_drawable_size)(struct loader_dri3_drawable *, int, int);
100 bool (*in_current_context)(struct loader_dri3_drawable *);
101 __DRIcontext *(*get_dri_context)(struct loader_dri3_drawable *);
102 void (*flush_drawable)(struct loader_dri3_drawable *, unsigned);
103 void (*show_fps)(struct loader_dri3_drawable *, uint64_t);
104 };
105
106 #define LOADER_DRI3_NUM_BUFFERS (1 + LOADER_DRI3_MAX_BACK)
107
108 struct loader_dri3_drawable {
109 xcb_connection_t *conn;
110 __DRIdrawable *dri_drawable;
111 xcb_drawable_t drawable;
112 int width;
113 int height;
114 int depth;
115 uint8_t have_back;
116 uint8_t have_fake_front;
117 uint8_t is_pixmap;
118 uint8_t flipping;
119
120 /* Information about the GPU owning the buffer */
121 __DRIscreen *dri_screen;
122 bool is_different_gpu;
123
124 /* Present extension capabilities
125 */
126 uint32_t present_capabilities;
127
128 /* SBC numbers are tracked by using the serial numbers
129 * in the present request and complete events
130 */
131 uint64_t send_sbc;
132 uint64_t recv_sbc;
133
134 /* Last received UST/MSC values for pixmap present complete */
135 uint64_t ust, msc;
136
137 /* Last received UST/MSC values from present notify msc event */
138 uint64_t notify_ust, notify_msc;
139
140 /* Serial numbers for tracking wait_for_msc events */
141 uint32_t send_msc_serial;
142 uint32_t recv_msc_serial;
143
144 struct loader_dri3_buffer *buffers[LOADER_DRI3_NUM_BUFFERS];
145 int cur_back;
146 int num_back;
147 int cur_blit_source;
148
149 uint32_t *stamp;
150
151 xcb_present_event_t eid;
152 xcb_gcontext_t gc;
153 xcb_special_event_t *special_event;
154
155 bool first_init;
156 int swap_interval;
157
158 struct loader_dri3_extensions *ext;
159 const struct loader_dri3_vtable *vtable;
160
161 unsigned int swap_method;
162 unsigned int back_format;
163
164 /* Currently protects the following fields:
165 * event_cnd, has_event_waiter,
166 * recv_sbc, ust, msc, recv_msc_serial,
167 * notify_ust, notify_msc
168 */
169 mtx_t mtx;
170 cnd_t event_cnd;
171 bool has_event_waiter;
172 };
173
174 void
175 loader_dri3_set_swap_interval(struct loader_dri3_drawable *draw,
176 int interval);
177
178 void
179 loader_dri3_drawable_fini(struct loader_dri3_drawable *draw);
180
181 int
182 loader_dri3_drawable_init(xcb_connection_t *conn,
183 xcb_drawable_t drawable,
184 __DRIscreen *dri_screen,
185 bool is_different_gpu,
186 const __DRIconfig *dri_config,
187 struct loader_dri3_extensions *ext,
188 const struct loader_dri3_vtable *vtable,
189 struct loader_dri3_drawable*);
190
191 bool loader_dri3_wait_for_msc(struct loader_dri3_drawable *draw,
192 int64_t target_msc,
193 int64_t divisor, int64_t remainder,
194 int64_t *ust, int64_t *msc, int64_t *sbc);
195
196 int64_t
197 loader_dri3_swap_buffers_msc(struct loader_dri3_drawable *draw,
198 int64_t target_msc, int64_t divisor,
199 int64_t remainder, unsigned flush_flags,
200 bool force_copy);
201
202 int
203 loader_dri3_wait_for_sbc(struct loader_dri3_drawable *draw,
204 int64_t target_sbc, int64_t *ust,
205 int64_t *msc, int64_t *sbc);
206
207 int loader_dri3_query_buffer_age(struct loader_dri3_drawable *draw);
208
209 void
210 loader_dri3_flush(struct loader_dri3_drawable *draw,
211 unsigned flags,
212 enum __DRI2throttleReason throttle_reason);
213
214 void
215 loader_dri3_copy_sub_buffer(struct loader_dri3_drawable *draw,
216 int x, int y,
217 int width, int height,
218 bool flush);
219
220 void
221 loader_dri3_copy_drawable(struct loader_dri3_drawable *draw,
222 xcb_drawable_t dest,
223 xcb_drawable_t src);
224
225 void
226 loader_dri3_wait_x(struct loader_dri3_drawable *draw);
227
228 void
229 loader_dri3_wait_gl(struct loader_dri3_drawable *draw);
230
231 int loader_dri3_open(xcb_connection_t *conn,
232 xcb_window_t root,
233 uint32_t provider);
234
235 __DRIimage *
236 loader_dri3_create_image(xcb_connection_t *c,
237 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply,
238 unsigned int format,
239 __DRIscreen *dri_screen,
240 const __DRIimageExtension *image,
241 void *loaderPrivate);
242
243 int
244 loader_dri3_get_buffers(__DRIdrawable *driDrawable,
245 unsigned int format,
246 uint32_t *stamp,
247 void *loaderPrivate,
248 uint32_t buffer_mask,
249 struct __DRIimageList *buffers);
250
251 void
252 loader_dri3_update_drawable_geometry(struct loader_dri3_drawable *draw);
253
254 void
255 loader_dri3_swapbuffer_barrier(struct loader_dri3_drawable *draw);
256
257 void
258 loader_dri3_close_screen(__DRIscreen *dri_screen);
259 #endif