de22c19a578fae7da3b967cadfe0dca174daebf2
[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 bool reallocate; /* Buffer should be reallocated and not reused */
65
66 uint32_t num_planes;
67 uint32_t size;
68 int strides[4];
69 int offsets[4];
70 uint64_t modifier;
71 uint32_t cpp;
72 uint32_t flags;
73 uint32_t width, height;
74 uint64_t last_swap;
75 };
76
77
78 #define LOADER_DRI3_MAX_BACK 4
79 #define LOADER_DRI3_BACK_ID(i) (i)
80 #define LOADER_DRI3_FRONT_ID (LOADER_DRI3_MAX_BACK)
81
82 static inline int
83 loader_dri3_pixmap_buf_id(enum loader_dri3_buffer_type buffer_type)
84 {
85 if (buffer_type == loader_dri3_buffer_back)
86 return LOADER_DRI3_BACK_ID(0);
87 else
88 return LOADER_DRI3_FRONT_ID;
89 }
90
91 struct loader_dri3_extensions {
92 const __DRIcoreExtension *core;
93 const __DRIimageDriverExtension *image_driver;
94 const __DRI2flushExtension *flush;
95 const __DRI2configQueryExtension *config;
96 const __DRItexBufferExtension *tex_buffer;
97 const __DRIimageExtension *image;
98 };
99
100 struct loader_dri3_drawable;
101
102 struct loader_dri3_vtable {
103 void (*set_drawable_size)(struct loader_dri3_drawable *, int, int);
104 bool (*in_current_context)(struct loader_dri3_drawable *);
105 __DRIcontext *(*get_dri_context)(struct loader_dri3_drawable *);
106 __DRIscreen *(*get_dri_screen)(void);
107 void (*flush_drawable)(struct loader_dri3_drawable *, unsigned);
108 void (*show_fps)(struct loader_dri3_drawable *, uint64_t);
109 };
110
111 #define LOADER_DRI3_NUM_BUFFERS (1 + LOADER_DRI3_MAX_BACK)
112
113 struct loader_dri3_drawable {
114 xcb_connection_t *conn;
115 __DRIdrawable *dri_drawable;
116 xcb_drawable_t drawable;
117 int width;
118 int height;
119 int depth;
120 uint8_t have_back;
121 uint8_t have_fake_front;
122 uint8_t is_pixmap;
123
124 /* Information about the GPU owning the buffer */
125 __DRIscreen *dri_screen;
126 bool is_different_gpu;
127 bool multiplanes_available;
128
129 /* Present extension capabilities
130 */
131 uint32_t present_capabilities;
132
133 /* SBC numbers are tracked by using the serial numbers
134 * in the present request and complete events
135 */
136 uint64_t send_sbc;
137 uint64_t recv_sbc;
138
139 /* Last received UST/MSC values for pixmap present complete */
140 uint64_t ust, msc;
141
142 /* Last received UST/MSC values from present notify msc event */
143 uint64_t notify_ust, notify_msc;
144
145 struct loader_dri3_buffer *buffers[LOADER_DRI3_NUM_BUFFERS];
146 int cur_back;
147 int num_back;
148 int cur_blit_source;
149
150 uint32_t *stamp;
151
152 xcb_present_event_t eid;
153 xcb_gcontext_t gc;
154 xcb_special_event_t *special_event;
155
156 bool first_init;
157 int swap_interval;
158
159 struct loader_dri3_extensions *ext;
160 const struct loader_dri3_vtable *vtable;
161
162 unsigned int swap_method;
163 unsigned int back_format;
164 xcb_present_complete_mode_t last_present_mode;
165
166 /* Currently protects the following fields:
167 * event_cnd, has_event_waiter,
168 * recv_sbc, ust, msc, recv_msc_serial,
169 * notify_ust, notify_msc
170 */
171 mtx_t mtx;
172 cnd_t event_cnd;
173 bool has_event_waiter;
174 };
175
176 void
177 loader_dri3_set_swap_interval(struct loader_dri3_drawable *draw,
178 int interval);
179
180 void
181 loader_dri3_drawable_fini(struct loader_dri3_drawable *draw);
182
183 int
184 loader_dri3_drawable_init(xcb_connection_t *conn,
185 xcb_drawable_t drawable,
186 __DRIscreen *dri_screen,
187 bool is_different_gpu,
188 bool is_multiplanes_available,
189 const __DRIconfig *dri_config,
190 struct loader_dri3_extensions *ext,
191 const struct loader_dri3_vtable *vtable,
192 struct loader_dri3_drawable*);
193
194 bool loader_dri3_wait_for_msc(struct loader_dri3_drawable *draw,
195 int64_t target_msc,
196 int64_t divisor, int64_t remainder,
197 int64_t *ust, int64_t *msc, int64_t *sbc);
198
199 int64_t
200 loader_dri3_swap_buffers_msc(struct loader_dri3_drawable *draw,
201 int64_t target_msc, int64_t divisor,
202 int64_t remainder, unsigned flush_flags,
203 bool force_copy);
204
205 int
206 loader_dri3_wait_for_sbc(struct loader_dri3_drawable *draw,
207 int64_t target_sbc, int64_t *ust,
208 int64_t *msc, int64_t *sbc);
209
210 int loader_dri3_query_buffer_age(struct loader_dri3_drawable *draw);
211
212 void
213 loader_dri3_flush(struct loader_dri3_drawable *draw,
214 unsigned flags,
215 enum __DRI2throttleReason throttle_reason);
216
217 void
218 loader_dri3_copy_sub_buffer(struct loader_dri3_drawable *draw,
219 int x, int y,
220 int width, int height,
221 bool flush);
222
223 void
224 loader_dri3_copy_drawable(struct loader_dri3_drawable *draw,
225 xcb_drawable_t dest,
226 xcb_drawable_t src);
227
228 void
229 loader_dri3_wait_x(struct loader_dri3_drawable *draw);
230
231 void
232 loader_dri3_wait_gl(struct loader_dri3_drawable *draw);
233
234 int loader_dri3_open(xcb_connection_t *conn,
235 xcb_window_t root,
236 uint32_t provider);
237
238 __DRIimage *
239 loader_dri3_create_image(xcb_connection_t *c,
240 xcb_dri3_buffer_from_pixmap_reply_t *bp_reply,
241 unsigned int format,
242 __DRIscreen *dri_screen,
243 const __DRIimageExtension *image,
244 void *loaderPrivate);
245
246 __DRIimage *
247 loader_dri3_create_image_from_buffers(xcb_connection_t *c,
248 xcb_dri3_buffers_from_pixmap_reply_t *bp_reply,
249 unsigned int format,
250 __DRIscreen *dri_screen,
251 const __DRIimageExtension *image,
252 void *loaderPrivate);
253
254 int
255 loader_dri3_get_buffers(__DRIdrawable *driDrawable,
256 unsigned int format,
257 uint32_t *stamp,
258 void *loaderPrivate,
259 uint32_t buffer_mask,
260 struct __DRIimageList *buffers);
261
262 void
263 loader_dri3_update_drawable_geometry(struct loader_dri3_drawable *draw);
264
265 void
266 loader_dri3_swapbuffer_barrier(struct loader_dri3_drawable *draw);
267
268 void
269 loader_dri3_close_screen(__DRIscreen *dri_screen);
270 #endif