560e40d4cee81a0df75ecd4eae2f2b08317815da
[mesa.git] / src / gallium / state_trackers / egl / wayland / native_wayland.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.11
4 *
5 * Copyright (C) 2011 Benjamin Franzke <benjaminfranzke@googlemail.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "util/u_memory.h"
27 #include "util/u_inlines.h"
28
29 #include "pipe/p_compiler.h"
30 #include "pipe/p_screen.h"
31 #include "pipe/p_context.h"
32 #include "pipe/p_state.h"
33 #include "state_tracker/drm_driver.h"
34 #include "egllog.h"
35
36 #include "native_wayland.h"
37
38 static void
39 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
40 {
41 int *done = data;
42
43 *done = 1;
44 wl_callback_destroy(callback);
45 }
46
47 static const struct wl_callback_listener sync_listener = {
48 sync_callback
49 };
50
51 int
52 wayland_roundtrip(struct wayland_display *display)
53 {
54 struct wl_callback *callback;
55 int done = 0, ret = 0;
56
57 callback = wl_display_sync(display->dpy);
58 wl_callback_add_listener(callback, &sync_listener, &done);
59 wl_proxy_set_queue((struct wl_proxy *) callback, display->queue);
60 while (ret == 0 && !done)
61 ret = wl_display_dispatch_queue(display->dpy, display->queue);
62
63 return ret;
64 }
65
66 static const struct native_event_handler *wayland_event_handler;
67
68 const static struct {
69 enum pipe_format format;
70 enum wayland_format_flag flag;
71 } wayland_formats[] = {
72 { PIPE_FORMAT_B8G8R8A8_UNORM, HAS_ARGB8888 },
73 { PIPE_FORMAT_B8G8R8X8_UNORM, HAS_XRGB8888 },
74 };
75
76 static const struct native_config **
77 wayland_display_get_configs(struct native_display *ndpy, int *num_configs)
78 {
79 struct wayland_display *display = wayland_display(ndpy);
80 const struct native_config **configs;
81 int i;
82
83 if (!display->configs) {
84 struct native_config *nconf;
85
86 display->num_configs = 0;
87 display->configs = CALLOC(Elements(wayland_formats),
88 sizeof(*display->configs));
89 if (!display->configs)
90 return NULL;
91
92 for (i = 0; i < Elements(wayland_formats); ++i) {
93 if (!(display->formats & wayland_formats[i].flag))
94 continue;
95
96 nconf = &display->configs[display->num_configs].base;
97 nconf->buffer_mask =
98 (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
99 (1 << NATIVE_ATTACHMENT_BACK_LEFT);
100
101 nconf->window_bit = TRUE;
102
103 nconf->color_format = wayland_formats[i].format;
104 display->num_configs++;
105 }
106 }
107
108 configs = MALLOC(display->num_configs * sizeof(*configs));
109 if (configs) {
110 for (i = 0; i < display->num_configs; ++i)
111 configs[i] = &display->configs[i].base;
112 if (num_configs)
113 *num_configs = display->num_configs;
114 }
115
116 return configs;
117 }
118
119 static int
120 wayland_display_get_param(struct native_display *ndpy,
121 enum native_param_type param)
122 {
123 int val;
124
125 switch (param) {
126 case NATIVE_PARAM_PREMULTIPLIED_ALPHA:
127 val = 1;
128 break;
129 case NATIVE_PARAM_USE_NATIVE_BUFFER:
130 case NATIVE_PARAM_PRESERVE_BUFFER:
131 case NATIVE_PARAM_MAX_SWAP_INTERVAL:
132 default:
133 val = 0;
134 break;
135 }
136
137 return val;
138 }
139
140 static void
141 wayland_release_pending_resource(void *data,
142 struct wl_callback *callback,
143 uint32_t time)
144 {
145 struct wayland_surface *surface = data;
146
147 wl_callback_destroy(callback);
148
149 /* FIXME: print internal error */
150 if (!surface->pending_resource)
151 return;
152
153 pipe_resource_reference(&surface->pending_resource, NULL);
154 }
155
156 static const struct wl_callback_listener release_buffer_listener = {
157 wayland_release_pending_resource
158 };
159
160 static void
161 wayland_window_surface_handle_resize(struct wayland_surface *surface)
162 {
163 struct wayland_display *display = surface->display;
164 struct pipe_resource *front_resource;
165 const enum native_attachment front_natt = NATIVE_ATTACHMENT_FRONT_LEFT;
166 int i;
167
168 front_resource = resource_surface_get_single_resource(surface->rsurf,
169 front_natt);
170 if (resource_surface_set_size(surface->rsurf,
171 surface->win->width, surface->win->height)) {
172
173 if (surface->pending_resource)
174 wayland_roundtrip(display);
175
176 if (front_resource) {
177 struct wl_callback *callback;
178
179 surface->pending_resource = front_resource;
180 front_resource = NULL;
181
182 callback = wl_display_sync(display->dpy);
183 wl_callback_add_listener(callback, &release_buffer_listener, surface);
184 wl_proxy_set_queue((struct wl_proxy *) callback, display->queue);
185 }
186
187 for (i = 0; i < WL_BUFFER_COUNT; ++i) {
188 if (surface->buffer[i])
189 wl_buffer_destroy(surface->buffer[i]);
190 surface->buffer[i] = NULL;
191 }
192
193 surface->dx = surface->win->dx;
194 surface->dy = surface->win->dy;
195 }
196 pipe_resource_reference(&front_resource, NULL);
197 }
198
199 static boolean
200 wayland_surface_validate(struct native_surface *nsurf, uint attachment_mask,
201 unsigned int *seq_num, struct pipe_resource **textures,
202 int *width, int *height)
203 {
204 struct wayland_surface *surface = wayland_surface(nsurf);
205
206 if (surface->type == WL_WINDOW_SURFACE)
207 wayland_window_surface_handle_resize(surface);
208
209 if (!resource_surface_add_resources(surface->rsurf, attachment_mask |
210 surface->attachment_mask))
211 return FALSE;
212
213 if (textures)
214 resource_surface_get_resources(surface->rsurf, textures, attachment_mask);
215
216 if (seq_num)
217 *seq_num = surface->sequence_number;
218
219 resource_surface_get_size(surface->rsurf, (uint *) width, (uint *) height);
220
221 return TRUE;
222 }
223
224 static void
225 wayland_frame_callback(void *data, struct wl_callback *callback, uint32_t time)
226 {
227 struct wayland_surface *surface = data;
228
229 surface->frame_callback = NULL;
230
231 wl_callback_destroy(callback);
232 }
233
234 static const struct wl_callback_listener frame_listener = {
235 wayland_frame_callback
236 };
237
238 static INLINE void
239 wayland_buffers_swap(struct wl_buffer **buffer,
240 enum wayland_buffer_type buf1,
241 enum wayland_buffer_type buf2)
242 {
243 struct wl_buffer *tmp = buffer[buf1];
244 buffer[buf1] = buffer[buf2];
245 buffer[buf2] = tmp;
246 }
247
248 static boolean
249 wayland_surface_swap_buffers(struct native_surface *nsurf)
250 {
251 struct wayland_surface *surface = wayland_surface(nsurf);
252 struct wayland_display *display = surface->display;
253 int ret = 0;
254
255 while (surface->frame_callback && ret != -1)
256 ret = wl_display_dispatch_queue(display->dpy, display->queue);
257 if (ret == -1)
258 return EGL_FALSE;
259
260 surface->frame_callback = wl_surface_frame(surface->win->surface);
261 wl_callback_add_listener(surface->frame_callback, &frame_listener, surface);
262 wl_proxy_set_queue((struct wl_proxy *) surface->frame_callback,
263 display->queue);
264
265 if (surface->type == WL_WINDOW_SURFACE) {
266 resource_surface_swap_buffers(surface->rsurf,
267 NATIVE_ATTACHMENT_FRONT_LEFT,
268 NATIVE_ATTACHMENT_BACK_LEFT, FALSE);
269
270 wayland_buffers_swap(surface->buffer, WL_BUFFER_FRONT, WL_BUFFER_BACK);
271
272 if (surface->buffer[WL_BUFFER_FRONT] == NULL)
273 surface->buffer[WL_BUFFER_FRONT] =
274 display->create_buffer(display, surface,
275 NATIVE_ATTACHMENT_FRONT_LEFT);
276
277 wl_surface_attach(surface->win->surface, surface->buffer[WL_BUFFER_FRONT],
278 surface->dx, surface->dy);
279
280 resource_surface_get_size(surface->rsurf,
281 (uint *) &surface->win->attached_width,
282 (uint *) &surface->win->attached_height);
283 surface->dx = 0;
284 surface->dy = 0;
285 }
286
287 surface->sequence_number++;
288 wayland_event_handler->invalid_surface(&display->base,
289 &surface->base,
290 surface->sequence_number);
291
292 return TRUE;
293 }
294
295 static boolean
296 wayland_surface_present(struct native_surface *nsurf,
297 const struct native_present_control *ctrl)
298 {
299 struct wayland_surface *surface = wayland_surface(nsurf);
300 uint width, height;
301 boolean ret;
302
303 if (ctrl->preserve || ctrl->swap_interval)
304 return FALSE;
305
306 /* force buffers to be re-created if they will be presented differently */
307 if (surface->premultiplied_alpha != ctrl->premultiplied_alpha) {
308 enum wayland_buffer_type buffer;
309
310 for (buffer = 0; buffer < WL_BUFFER_COUNT; ++buffer) {
311 if (surface->buffer[buffer]) {
312 wl_buffer_destroy(surface->buffer[buffer]);
313 surface->buffer[buffer] = NULL;
314 }
315 }
316
317 surface->premultiplied_alpha = ctrl->premultiplied_alpha;
318 }
319
320 switch (ctrl->natt) {
321 case NATIVE_ATTACHMENT_FRONT_LEFT:
322 ret = TRUE;
323 break;
324 case NATIVE_ATTACHMENT_BACK_LEFT:
325 ret = wayland_surface_swap_buffers(nsurf);
326 break;
327 default:
328 ret = FALSE;
329 break;
330 }
331
332 if (surface->type == WL_WINDOW_SURFACE) {
333 resource_surface_get_size(surface->rsurf, &width, &height);
334 wl_surface_damage(surface->win->surface, 0, 0, width, height);
335 wl_surface_commit(surface->win->surface);
336 }
337
338 return ret;
339 }
340
341 static void
342 wayland_surface_wait(struct native_surface *nsurf)
343 {
344 /* no-op */
345 }
346
347 static void
348 wayland_surface_destroy(struct native_surface *nsurf)
349 {
350 struct wayland_surface *surface = wayland_surface(nsurf);
351 enum wayland_buffer_type buffer;
352
353 for (buffer = 0; buffer < WL_BUFFER_COUNT; ++buffer) {
354 if (surface->buffer[buffer])
355 wl_buffer_destroy(surface->buffer[buffer]);
356 }
357
358 if (surface->frame_callback)
359 wl_callback_destroy(surface->frame_callback);
360
361 resource_surface_destroy(surface->rsurf);
362 FREE(surface);
363 }
364
365
366 static struct native_surface *
367 wayland_create_window_surface(struct native_display *ndpy,
368 EGLNativeWindowType win,
369 const struct native_config *nconf)
370 {
371 struct wayland_display *display = wayland_display(ndpy);
372 struct wayland_config *config = wayland_config(nconf);
373 struct wayland_surface *surface;
374 uint bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW |
375 PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT;
376
377 surface = CALLOC_STRUCT(wayland_surface);
378 if (!surface)
379 return NULL;
380
381 surface->display = display;
382 surface->color_format = config->base.color_format;
383
384 surface->win = (struct wl_egl_window *) win;
385
386 surface->pending_resource = NULL;
387 surface->frame_callback = NULL;
388 surface->type = WL_WINDOW_SURFACE;
389
390 surface->buffer[WL_BUFFER_FRONT] = NULL;
391 surface->buffer[WL_BUFFER_BACK] = NULL;
392 surface->attachment_mask = (1 << NATIVE_ATTACHMENT_FRONT_LEFT) |
393 (1 << NATIVE_ATTACHMENT_BACK_LEFT);
394
395 surface->rsurf = resource_surface_create(display->base.screen,
396 surface->color_format, bind);
397
398 if (!surface->rsurf) {
399 FREE(surface);
400 return NULL;
401 }
402
403 surface->base.destroy = wayland_surface_destroy;
404 surface->base.present = wayland_surface_present;
405 surface->base.validate = wayland_surface_validate;
406 surface->base.wait = wayland_surface_wait;
407
408 return &surface->base;
409 }
410
411 static struct native_display *
412 native_create_display(void *dpy, boolean use_sw)
413 {
414 struct wayland_display *display = NULL;
415 boolean own_dpy = FALSE;
416
417 use_sw = use_sw || debug_get_bool_option("EGL_SOFTWARE", FALSE);
418
419 if (dpy == NULL) {
420 dpy = wl_display_connect(NULL);
421 if (dpy == NULL)
422 return NULL;
423 own_dpy = TRUE;
424 }
425
426 if (use_sw) {
427 _eglLog(_EGL_INFO, "use software fallback");
428 display = wayland_create_shm_display((struct wl_display *) dpy,
429 wayland_event_handler);
430 } else {
431 display = wayland_create_drm_display((struct wl_display *) dpy,
432 wayland_event_handler);
433 }
434
435 if (!display)
436 return NULL;
437
438 display->base.get_param = wayland_display_get_param;
439 display->base.get_configs = wayland_display_get_configs;
440 display->base.create_window_surface = wayland_create_window_surface;
441
442 display->own_dpy = own_dpy;
443
444 return &display->base;
445 }
446
447 static const struct native_platform wayland_platform = {
448 "wayland", /* name */
449 native_create_display
450 };
451
452 const struct native_platform *
453 native_get_wayland_platform(const struct native_event_handler *event_handler)
454 {
455 wayland_event_handler = event_handler;
456 return &wayland_platform;
457 }
458
459 /* vim: set sw=3 ts=8 sts=3 expandtab: */