blob: ff0d5c802acf21824dcd3231d9e79fb0fe8bb5b8 [file] [log] [blame]
Benjamin Franzke93aea842011-02-04 12:39:40 +01001/*
Kristian Høgsberg90804e82012-12-13 23:30:45 -05002 * Copyright © 2011-2012 Intel Corporation
Axel Davycdcfe482015-05-01 11:11:20 +02003 * Copyright © 2012 Collabora, Ltd.
Benjamin Franzke93aea842011-02-04 12:39:40 +01004 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
28 */
29
Emil Velikovbf0e4d22015-02-28 17:12:40 +000030#include <stdint.h>
Benjamin Franzke93aea842011-02-04 12:39:40 +010031#include <stdlib.h>
32#include <string.h>
33#include <limits.h>
34#include <dlfcn.h>
35#include <errno.h>
36#include <unistd.h>
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -040037#include <fcntl.h>
38#include <xf86drm.h>
Axel Davycdcfe482015-05-01 11:11:20 +020039#include <sys/mman.h>
Benjamin Franzke93aea842011-02-04 12:39:40 +010040
41#include "egl_dri2.h"
Chad Versace8b9298a2014-01-28 12:34:19 -080042#include "egl_dri2_fallbacks.h"
Emil Velikov8d4357b2014-01-11 04:52:48 +000043#include "loader.h"
Benjamin Franzke93aea842011-02-04 12:39:40 +010044
Benjamin Franzke6b369c42011-02-21 16:22:34 +010045#include <wayland-client.h>
46#include "wayland-drm-client-protocol.h"
47
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -040048enum wl_drm_format_flags {
Kristian Høgsberg58dc1b22012-01-11 14:23:24 -050049 HAS_ARGB8888 = 1,
Singh, Satyeshwar2efc97d2013-10-16 01:10:12 +000050 HAS_XRGB8888 = 2,
51 HAS_RGB565 = 4,
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -040052};
53
Chad Versacea2187652014-01-27 16:42:10 -080054static EGLBoolean
55dri2_wl_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
56 EGLint interval);
57
Benjamin Franzke51f28202011-02-11 02:23:14 +010058static void
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -040059sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
60{
61 int *done = data;
62
63 *done = 1;
64 wl_callback_destroy(callback);
65}
66
67static const struct wl_callback_listener sync_listener = {
Emil Velikov55674942015-07-10 12:24:11 +010068 .done = sync_callback
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -040069};
70
71static int
72roundtrip(struct dri2_egl_display *dri2_dpy)
73{
74 struct wl_callback *callback;
75 int done = 0, ret = 0;
76
77 callback = wl_display_sync(dri2_dpy->wl_dpy);
78 wl_callback_add_listener(callback, &sync_listener, &done);
79 wl_proxy_set_queue((struct wl_proxy *) callback, dri2_dpy->wl_queue);
80 while (ret != -1 && !done)
81 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
82
Jonas Ådahl800ed952012-12-25 13:01:08 +010083 if (!done)
84 wl_callback_destroy(callback);
85
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -040086 return ret;
87}
88
89static void
Benjamin Franzke0cb356d2011-05-06 19:13:29 +020090wl_buffer_release(void *data, struct wl_buffer *buffer)
91{
92 struct dri2_egl_surface *dri2_surf = data;
93 int i;
94
Kristian Høgsberg90804e82012-12-13 23:30:45 -050095 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
96 if (dri2_surf->color_buffers[i].wl_buffer == buffer)
Benjamin Franzke0cb356d2011-05-06 19:13:29 +020097 break;
98
Kristian Høgsberg90804e82012-12-13 23:30:45 -050099 if (i == ARRAY_SIZE(dri2_surf->color_buffers)) {
100 wl_buffer_destroy(buffer);
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200101 return;
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500102 }
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200103
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500104 dri2_surf->color_buffers[i].locked = 0;
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200105}
106
Emil Velikov55674942015-07-10 12:24:11 +0100107static const struct wl_buffer_listener wl_buffer_listener = {
108 .release = wl_buffer_release
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200109};
Benjamin Franzke51f28202011-02-11 02:23:14 +0100110
Ander Conselvan de Oliveiraca3ed3e2012-11-30 17:41:02 +0200111static void
112resize_callback(struct wl_egl_window *wl_win, void *data)
113{
114 struct dri2_egl_surface *dri2_surf = data;
115 struct dri2_egl_display *dri2_dpy =
116 dri2_egl_display(dri2_surf->base.Resource.Display);
117
118 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
119}
120
Benjamin Franzke93aea842011-02-04 12:39:40 +0100121/**
122 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
123 */
124static _EGLSurface *
Axel Davycd25e522015-05-01 11:16:41 +0200125dri2_wl_create_surface(_EGLDriver *drv, _EGLDisplay *disp,
Chad Versace6d1f83e2014-01-07 14:54:51 -0800126 _EGLConfig *conf, void *native_window,
Chad Versaced019cd82014-01-28 12:47:38 -0800127 const EGLint *attrib_list)
Benjamin Franzke93aea842011-02-04 12:39:40 +0100128{
129 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
130 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
Chad Versace6d1f83e2014-01-07 14:54:51 -0800131 struct wl_egl_window *window = native_window;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100132 struct dri2_egl_surface *dri2_surf;
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200133 const __DRIconfig *config;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100134
135 (void) drv;
136
Matt Turnerf0a8bcd2014-09-21 21:15:26 -0700137 dri2_surf = calloc(1, sizeof *dri2_surf);
Benjamin Franzke93aea842011-02-04 12:39:40 +0100138 if (!dri2_surf) {
139 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
140 return NULL;
141 }
Boyan Ding052b3d42015-06-13 15:36:27 +0800142
Axel Davycd25e522015-05-01 11:16:41 +0200143 if (!_eglInitSurface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, attrib_list))
Benjamin Franzke93aea842011-02-04 12:39:40 +0100144 goto cleanup_surf;
145
Singh, Satyeshwar2efc97d2013-10-16 01:10:12 +0000146 if (conf->RedSize == 5)
147 dri2_surf->format = WL_DRM_FORMAT_RGB565;
148 else if (conf->AlphaSize == 0)
Kristian Høgsberg58dc1b22012-01-11 14:23:24 -0500149 dri2_surf->format = WL_DRM_FORMAT_XRGB8888;
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -0400150 else
Kristian Høgsberg58dc1b22012-01-11 14:23:24 -0500151 dri2_surf->format = WL_DRM_FORMAT_ARGB8888;
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -0400152
Emil Velikov0afa6332015-06-18 20:19:32 +0100153 if (!window) {
154 _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_create_surface");
155 goto cleanup_surf;
156 }
157
Axel Davycd25e522015-05-01 11:16:41 +0200158 dri2_surf->wl_win = window;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100159
Axel Davycd25e522015-05-01 11:16:41 +0200160 dri2_surf->wl_win->private = dri2_surf;
161 dri2_surf->wl_win->resize_callback = resize_callback;
Ander Conselvan de Oliveiraca3ed3e2012-11-30 17:41:02 +0200162
Axel Davycd25e522015-05-01 11:16:41 +0200163 dri2_surf->base.Width = -1;
164 dri2_surf->base.Height = -1;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100165
Marek Olšákc2c2e9a2015-06-10 02:49:29 +0200166 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
167 dri2_surf->base.GLColorspace);
168
169 dri2_surf->dri_drawable =
170 (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config,
171 dri2_surf);
Benjamin Franzke93aea842011-02-04 12:39:40 +0100172 if (dri2_surf->dri_drawable == NULL) {
173 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
Emil Velikov4ea52232015-06-18 20:22:54 +0100174 goto cleanup_surf;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100175 }
176
177 return &dri2_surf->base;
178
Benjamin Franzke93aea842011-02-04 12:39:40 +0100179 cleanup_surf:
180 free(dri2_surf);
181
182 return NULL;
183}
184
185/**
186 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
187 */
188static _EGLSurface *
Chad Versaced019cd82014-01-28 12:47:38 -0800189dri2_wl_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
Chad Versace6d1f83e2014-01-07 14:54:51 -0800190 _EGLConfig *conf, void *native_window,
Chad Versaced019cd82014-01-28 12:47:38 -0800191 const EGLint *attrib_list)
Benjamin Franzke93aea842011-02-04 12:39:40 +0100192{
Neil Roberts992a2db2013-11-15 13:50:50 +0000193 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
194 _EGLSurface *surf;
195
Axel Davycd25e522015-05-01 11:16:41 +0200196 surf = dri2_wl_create_surface(drv, disp, conf, native_window, attrib_list);
Neil Roberts992a2db2013-11-15 13:50:50 +0000197
198 if (surf != NULL)
Chad Versacea2187652014-01-27 16:42:10 -0800199 dri2_wl_swap_interval(drv, disp, surf, dri2_dpy->default_swap_interval);
Neil Roberts992a2db2013-11-15 13:50:50 +0000200
201 return surf;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100202}
203
Chad Versace9a40ee12014-02-09 09:13:27 -0800204static _EGLSurface *
205dri2_wl_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
206 _EGLConfig *conf, void *native_window,
207 const EGLint *attrib_list)
208{
209 /* From the EGL_EXT_platform_wayland spec, version 3:
210 *
211 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
212 * that belongs to Wayland. Any such call fails and generates
213 * EGL_BAD_PARAMETER.
214 */
215 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on "
216 "Wayland");
217 return NULL;
218}
219
Benjamin Franzke93aea842011-02-04 12:39:40 +0100220/**
221 * Called via eglDestroySurface(), drv->API.DestroySurface().
222 */
223static EGLBoolean
Chad Versaced019cd82014-01-28 12:47:38 -0800224dri2_wl_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
Benjamin Franzke93aea842011-02-04 12:39:40 +0100225{
226 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
227 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
228 int i;
229
230 (void) drv;
231
232 if (!_eglPutSurface(surf))
233 return EGL_TRUE;
234
235 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
236
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500237 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
238 if (dri2_surf->color_buffers[i].wl_buffer)
239 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500240 if (dri2_surf->color_buffers[i].dri_image)
241 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
Axel Davy4cd546d2015-05-01 01:16:24 +0200242 if (dri2_surf->color_buffers[i].linear_copy)
243 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
Axel Davycdcfe482015-05-01 11:11:20 +0200244 if (dri2_surf->color_buffers[i].data)
245 munmap(dri2_surf->color_buffers[i].data,
246 dri2_surf->color_buffers[i].data_size);
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500247 }
Benjamin Franzke93aea842011-02-04 12:39:40 +0100248
Axel Davycdcfe482015-05-01 11:11:20 +0200249 if (dri2_dpy->dri2) {
250 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
251 if (dri2_surf->dri_buffers[i] &&
252 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
253 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
254 dri2_surf->dri_buffers[i]);
255 }
Benjamin Franzke93aea842011-02-04 12:39:40 +0100256
Neil Roberts992a2db2013-11-15 13:50:50 +0000257 if (dri2_surf->throttle_callback)
258 wl_callback_destroy(dri2_surf->throttle_callback);
Jonas Ådahla3b6b2d2012-10-28 00:50:12 +0200259
Axel Davycd25e522015-05-01 11:16:41 +0200260 dri2_surf->wl_win->private = NULL;
261 dri2_surf->wl_win->resize_callback = NULL;
Ander Conselvan de Oliveiraca3ed3e2012-11-30 17:41:02 +0200262
Benjamin Franzke93aea842011-02-04 12:39:40 +0100263 free(surf);
264
265 return EGL_TRUE;
266}
267
Benjamin Franzke51f28202011-02-11 02:23:14 +0100268static void
Chad Versaced019cd82014-01-28 12:47:38 -0800269dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
Benjamin Franzke51f28202011-02-11 02:23:14 +0100270{
271 struct dri2_egl_display *dri2_dpy =
272 dri2_egl_display(dri2_surf->base.Resource.Display);
273 int i;
274
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500275 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
276 if (dri2_surf->color_buffers[i].wl_buffer &&
277 !dri2_surf->color_buffers[i].locked)
278 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500279 if (dri2_surf->color_buffers[i].dri_image)
280 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
Axel Davy4cd546d2015-05-01 01:16:24 +0200281 if (dri2_surf->color_buffers[i].linear_copy)
282 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
Axel Davycdcfe482015-05-01 11:11:20 +0200283 if (dri2_surf->color_buffers[i].data)
284 munmap(dri2_surf->color_buffers[i].data,
285 dri2_surf->color_buffers[i].data_size);
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500286
287 dri2_surf->color_buffers[i].wl_buffer = NULL;
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500288 dri2_surf->color_buffers[i].dri_image = NULL;
Axel Davy4cd546d2015-05-01 01:16:24 +0200289 dri2_surf->color_buffers[i].linear_copy = NULL;
Axel Davycdcfe482015-05-01 11:11:20 +0200290 dri2_surf->color_buffers[i].data = NULL;
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500291 dri2_surf->color_buffers[i].locked = 0;
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200292 }
293
Axel Davycdcfe482015-05-01 11:11:20 +0200294 if (dri2_dpy->dri2) {
295 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
296 if (dri2_surf->dri_buffers[i] &&
297 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
298 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
299 dri2_surf->dri_buffers[i]);
300 }
Benjamin Franzke51f28202011-02-11 02:23:14 +0100301}
302
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500303static int
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800304get_back_bo(struct dri2_egl_surface *dri2_surf)
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200305{
306 struct dri2_egl_display *dri2_dpy =
307 dri2_egl_display(dri2_surf->base.Resource.Display);
Axel Davyd943ac42015-10-21 12:28:00 +0200308 int i, use_flags;
Vivek Kasireddy1e96eec2015-02-10 19:15:31 -0800309 unsigned int dri_image_format;
310
311 /* currently supports three WL DRM formats,
312 * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
313 * and WL_DRM_FORMAT_RGB565
314 */
315 switch (dri2_surf->format) {
316 case WL_DRM_FORMAT_ARGB8888:
317 dri_image_format = __DRI_IMAGE_FORMAT_ARGB8888;
318 break;
319 case WL_DRM_FORMAT_XRGB8888:
320 dri_image_format = __DRI_IMAGE_FORMAT_XRGB8888;
321 break;
322 case WL_DRM_FORMAT_RGB565:
323 dri_image_format = __DRI_IMAGE_FORMAT_RGB565;
324 break;
325 default:
326 /* format is not supported */
327 return -1;
328 }
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200329
Neil Roberts992a2db2013-11-15 13:50:50 +0000330 /* We always want to throttle to some event (either a frame callback or
331 * a sync request) after the commit so that we can be sure the
332 * compositor has had a chance to handle it and send us a release event
333 * before we look for a free buffer */
334 while (dri2_surf->throttle_callback != NULL)
335 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
336 dri2_dpy->wl_queue) == -1)
Kristian Høgsberg4ed055b2013-12-09 16:13:35 -0800337 return -1;
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200338
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500339 if (dri2_surf->back == NULL) {
340 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
Neil Roberts992a2db2013-11-15 13:50:50 +0000341 /* Get an unlocked buffer, preferrably one with a dri_buffer
342 * already allocated. */
343 if (dri2_surf->color_buffers[i].locked)
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500344 continue;
345 if (dri2_surf->back == NULL)
Neil Roberts992a2db2013-11-15 13:50:50 +0000346 dri2_surf->back = &dri2_surf->color_buffers[i];
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500347 else if (dri2_surf->back->dri_image == NULL)
Neil Roberts992a2db2013-11-15 13:50:50 +0000348 dri2_surf->back = &dri2_surf->color_buffers[i];
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500349 }
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200350 }
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500351
352 if (dri2_surf->back == NULL)
353 return -1;
Axel Davy4cd546d2015-05-01 01:16:24 +0200354
Axel Davyd943ac42015-10-21 12:28:00 +0200355 use_flags = __DRI_IMAGE_USE_SHARE | __DRI_IMAGE_USE_BACKBUFFER;
356
Axel Davy4cd546d2015-05-01 01:16:24 +0200357 if (dri2_dpy->is_different_gpu &&
358 dri2_surf->back->linear_copy == NULL) {
359 dri2_surf->back->linear_copy =
360 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
361 dri2_surf->base.Width,
362 dri2_surf->base.Height,
363 dri_image_format,
Axel Davyd943ac42015-10-21 12:28:00 +0200364 use_flags |
Axel Davy4cd546d2015-05-01 01:16:24 +0200365 __DRI_IMAGE_USE_LINEAR,
366 NULL);
367 if (dri2_surf->back->linear_copy == NULL)
368 return -1;
369 }
370
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500371 if (dri2_surf->back->dri_image == NULL) {
Boyan Ding052b3d42015-06-13 15:36:27 +0800372 dri2_surf->back->dri_image =
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500373 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
374 dri2_surf->base.Width,
375 dri2_surf->base.Height,
Vivek Kasireddy1e96eec2015-02-10 19:15:31 -0800376 dri_image_format,
Axel Davy4cd546d2015-05-01 01:16:24 +0200377 dri2_dpy->is_different_gpu ?
Axel Davyd943ac42015-10-21 12:28:00 +0200378 0 : use_flags,
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500379 NULL);
Kristian Høgsberg6d4d4b02012-12-13 23:32:14 -0500380 dri2_surf->back->age = 0;
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500381 }
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500382 if (dri2_surf->back->dri_image == NULL)
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500383 return -1;
384
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800385 dri2_surf->back->locked = 1;
386
387 return 0;
388}
389
390
391static void
392back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
393{
394 struct dri2_egl_display *dri2_dpy =
395 dri2_egl_display(dri2_surf->base.Resource.Display);
396 __DRIimage *image;
397 int name, pitch;
398
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500399 image = dri2_surf->back->dri_image;
400
401 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
402 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
403
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500404 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
405 buffer->name = name;
406 buffer->pitch = pitch;
407 buffer->cpp = 4;
408 buffer->flags = 0;
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500409}
410
411static int
412get_aux_bo(struct dri2_egl_surface *dri2_surf,
413 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
414{
415 struct dri2_egl_display *dri2_dpy =
416 dri2_egl_display(dri2_surf->base.Resource.Display);
417 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
418
419 if (b == NULL) {
420 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
421 attachment, format,
422 dri2_surf->base.Width,
423 dri2_surf->base.Height);
424 dri2_surf->dri_buffers[attachment] = b;
425 }
426 if (b == NULL)
427 return -1;
428
429 memcpy(buffer, b, sizeof *buffer);
430
431 return 0;
Benjamin Franzke0cb356d2011-05-06 19:13:29 +0200432}
433
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800434static int
435update_buffers(struct dri2_egl_surface *dri2_surf)
Benjamin Franzke93aea842011-02-04 12:39:40 +0100436{
Benjamin Franzke93aea842011-02-04 12:39:40 +0100437 struct dri2_egl_display *dri2_dpy =
438 dri2_egl_display(dri2_surf->base.Resource.Display);
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800439 int i;
Ander Conselvan de Oliveira60a11e22012-11-22 15:34:49 +0200440
Axel Davycd25e522015-05-01 11:16:41 +0200441 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
442 dri2_surf->base.Height != dri2_surf->wl_win->height) {
Benjamin Franzke51f28202011-02-11 02:23:14 +0100443
Chad Versaced019cd82014-01-28 12:47:38 -0800444 dri2_wl_release_buffers(dri2_surf);
Benjamin Franzke93aea842011-02-04 12:39:40 +0100445
446 dri2_surf->base.Width = dri2_surf->wl_win->width;
447 dri2_surf->base.Height = dri2_surf->wl_win->height;
448 dri2_surf->dx = dri2_surf->wl_win->dx;
449 dri2_surf->dy = dri2_surf->wl_win->dy;
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500450 }
Benjamin Franzke93aea842011-02-04 12:39:40 +0100451
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800452 if (get_back_bo(dri2_surf) < 0) {
453 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
454 return -1;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100455 }
456
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500457 /* If we have an extra unlocked buffer at this point, we had to do triple
458 * buffering for a while, but now can go back to just double buffering.
459 * That means we can free any unlocked buffer now. */
460 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
461 if (!dri2_surf->color_buffers[i].locked &&
462 dri2_surf->color_buffers[i].wl_buffer) {
463 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500464 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
Axel Davy4cd546d2015-05-01 01:16:24 +0200465 if (dri2_dpy->is_different_gpu)
466 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500467 dri2_surf->color_buffers[i].wl_buffer = NULL;
Kristian Høgsberg664fe6d2013-02-02 07:40:51 -0500468 dri2_surf->color_buffers[i].dri_image = NULL;
Axel Davy4cd546d2015-05-01 01:16:24 +0200469 dri2_surf->color_buffers[i].linear_copy = NULL;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100470 }
Benjamin Franzke93aea842011-02-04 12:39:40 +0100471 }
472
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800473 return 0;
474}
475
476static __DRIbuffer *
Chad Versaced019cd82014-01-28 12:47:38 -0800477dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable,
478 int *width, int *height,
479 unsigned int *attachments, int count,
480 int *out_count, void *loaderPrivate)
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800481{
482 struct dri2_egl_surface *dri2_surf = loaderPrivate;
483 int i, j;
484
485 if (update_buffers(dri2_surf) < 0)
486 return NULL;
487
488 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
489 switch (attachments[i]) {
490 case __DRI_BUFFER_BACK_LEFT:
491 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
492 break;
493 default:
494 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
495 &dri2_surf->buffers[j]) < 0) {
496 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
497 return NULL;
498 }
499 break;
500 }
501 }
502
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500503 *out_count = j;
504 if (j == 0)
Benjamin Franzke93aea842011-02-04 12:39:40 +0100505 return NULL;
506
507 *width = dri2_surf->base.Width;
508 *height = dri2_surf->base.Height;
509
510 return dri2_surf->buffers;
511}
512
513static __DRIbuffer *
Chad Versaced019cd82014-01-28 12:47:38 -0800514dri2_wl_get_buffers(__DRIdrawable * driDrawable,
515 int *width, int *height,
516 unsigned int *attachments, int count,
517 int *out_count, void *loaderPrivate)
Benjamin Franzke93aea842011-02-04 12:39:40 +0100518{
Vivek Kasireddy1e96eec2015-02-10 19:15:31 -0800519 struct dri2_egl_surface *dri2_surf = loaderPrivate;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100520 unsigned int *attachments_with_format;
521 __DRIbuffer *buffer;
Vivek Kasireddy1e96eec2015-02-10 19:15:31 -0800522 unsigned int bpp;
523
Benjamin Franzke93aea842011-02-04 12:39:40 +0100524 int i;
525
Vivek Kasireddy1e96eec2015-02-10 19:15:31 -0800526 switch (dri2_surf->format) {
527 case WL_DRM_FORMAT_ARGB8888:
528 case WL_DRM_FORMAT_XRGB8888:
529 bpp = 32;
530 break;
531 case WL_DRM_FORMAT_RGB565:
532 bpp = 16;
533 break;
534 default:
535 /* format is not supported */
536 return NULL;
537 }
538
Carl Worthecc89e42014-09-03 14:33:18 -0700539 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
Benjamin Franzke93aea842011-02-04 12:39:40 +0100540 if (!attachments_with_format) {
541 *out_count = 0;
542 return NULL;
543 }
544
545 for (i = 0; i < count; ++i) {
546 attachments_with_format[2*i] = attachments[i];
Vivek Kasireddy1e96eec2015-02-10 19:15:31 -0800547 attachments_with_format[2*i + 1] = bpp;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100548 }
549
550 buffer =
Chad Versaced019cd82014-01-28 12:47:38 -0800551 dri2_wl_get_buffers_with_format(driDrawable,
552 width, height,
553 attachments_with_format, count,
554 out_count, loaderPrivate);
Benjamin Franzke93aea842011-02-04 12:39:40 +0100555
556 free(attachments_with_format);
557
558 return buffer;
559}
560
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800561static int
562image_get_buffers(__DRIdrawable *driDrawable,
563 unsigned int format,
564 uint32_t *stamp,
565 void *loaderPrivate,
566 uint32_t buffer_mask,
567 struct __DRIimageList *buffers)
568{
569 struct dri2_egl_surface *dri2_surf = loaderPrivate;
570
571 if (update_buffers(dri2_surf) < 0)
572 return 0;
573
574 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
575 buffers->back = dri2_surf->back->dri_image;
576
577 return 1;
578}
579
Benjamin Franzke93aea842011-02-04 12:39:40 +0100580static void
Chad Versaced019cd82014-01-28 12:47:38 -0800581dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
Benjamin Franzke93aea842011-02-04 12:39:40 +0100582{
583 (void) driDrawable;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100584 (void) loaderPrivate;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100585}
586
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800587static const __DRIimageLoaderExtension image_loader_extension = {
Emil Velikov9e627cc2014-02-12 18:32:59 +0000588 .base = { __DRI_IMAGE_LOADER, 1 },
589
590 .getBuffers = image_get_buffers,
Chad Versaced019cd82014-01-28 12:47:38 -0800591 .flushFrontBuffer = dri2_wl_flush_front_buffer,
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800592};
593
Benjamin Franzke93aea842011-02-04 12:39:40 +0100594static void
Neil Roberts992a2db2013-11-15 13:50:50 +0000595wayland_throttle_callback(void *data,
596 struct wl_callback *callback,
597 uint32_t time)
Benjamin Franzke93aea842011-02-04 12:39:40 +0100598{
599 struct dri2_egl_surface *dri2_surf = data;
600
Neil Roberts992a2db2013-11-15 13:50:50 +0000601 dri2_surf->throttle_callback = NULL;
Kristian Høgsberg6602bda2011-08-16 22:38:22 -0400602 wl_callback_destroy(callback);
Benjamin Franzke93aea842011-02-04 12:39:40 +0100603}
604
Neil Roberts992a2db2013-11-15 13:50:50 +0000605static const struct wl_callback_listener throttle_listener = {
Emil Velikov55674942015-07-10 12:24:11 +0100606 .done = wayland_throttle_callback
Kristian Høgsberg6602bda2011-08-16 22:38:22 -0400607};
608
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500609static void
610create_wl_buffer(struct dri2_egl_surface *dri2_surf)
611{
612 struct dri2_egl_display *dri2_dpy =
613 dri2_egl_display(dri2_surf->base.Resource.Display);
Axel Davy4cd546d2015-05-01 01:16:24 +0200614 __DRIimage *image;
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800615 int fd, stride, name;
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500616
617 if (dri2_surf->current->wl_buffer != NULL)
618 return;
619
Axel Davy4cd546d2015-05-01 01:16:24 +0200620 if (dri2_dpy->is_different_gpu) {
621 image = dri2_surf->current->linear_copy;
622 } else {
623 image = dri2_surf->current->dri_image;
624 }
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500625 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
Axel Davy4cd546d2015-05-01 01:16:24 +0200626 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
627 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500628
629 dri2_surf->current->wl_buffer =
630 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
631 fd,
632 dri2_surf->base.Width,
633 dri2_surf->base.Height,
634 dri2_surf->format,
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800635 0, stride,
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500636 0, 0,
637 0, 0);
638 close(fd);
639 } else {
Axel Davy4cd546d2015-05-01 01:16:24 +0200640 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
641 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800642
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500643 dri2_surf->current->wl_buffer =
644 wl_drm_create_buffer(dri2_dpy->wl_drm,
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800645 name,
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500646 dri2_surf->base.Width,
647 dri2_surf->base.Height,
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800648 stride,
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500649 dri2_surf->format);
650 }
651
652 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->current->wl_buffer,
653 dri2_dpy->wl_queue);
654 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
655 &wl_buffer_listener, dri2_surf);
656}
657
Derek Foremand085a5d2016-02-16 10:34:39 -0600658static EGLBoolean
659try_damage_buffer(struct dri2_egl_surface *dri2_surf,
660 const EGLint *rects,
661 EGLint n_rects)
662{
663/* The WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION macro and
664 * wl_proxy_get_version() were both introduced in wayland 1.10.
665 * Instead of bumping our wayland dependency we just make this
666 * function conditional on the required 1.10 features, falling
667 * back to old (correct but suboptimal) behaviour for older
668 * wayland.
669 */
670#ifdef WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION
671 int i;
672
673 if (wl_proxy_get_version((struct wl_proxy *) dri2_surf->wl_win->surface)
674 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
675 return EGL_FALSE;
676
677 for (i = 0; i < n_rects; i++) {
678 const int *rect = &rects[i * 4];
679
680 wl_surface_damage_buffer(dri2_surf->wl_win->surface,
681 rect[0],
682 dri2_surf->base.Height - rect[1] - rect[3],
683 rect[2], rect[3]);
684 }
685 return EGL_TRUE;
686#endif
687 return EGL_FALSE;
688}
Benjamin Franzke93aea842011-02-04 12:39:40 +0100689/**
690 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
691 */
692static EGLBoolean
Chad Versaced019cd82014-01-28 12:47:38 -0800693dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
694 _EGLDisplay *disp,
695 _EGLSurface *draw,
696 const EGLint *rects,
697 EGLint n_rects)
Benjamin Franzke93aea842011-02-04 12:39:40 +0100698{
699 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
700 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
Neil Roberts25cc8892013-11-15 13:50:49 +0000701 int i;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100702
Kristian Høgsberg6d4d4b02012-12-13 23:32:14 -0500703 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
704 if (dri2_surf->color_buffers[i].age > 0)
705 dri2_surf->color_buffers[i].age++;
706
Kristian Høgsberg1fe00732013-02-06 15:41:54 -0500707 /* Make sure we have a back buffer in case we're swapping without ever
708 * rendering. */
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800709 if (get_back_bo(dri2_surf) < 0) {
Kristian Høgsberg1fe00732013-02-06 15:41:54 -0500710 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
711 return EGL_FALSE;
712 }
713
Neil Roberts992a2db2013-11-15 13:50:50 +0000714 if (draw->SwapInterval > 0) {
715 dri2_surf->throttle_callback =
716 wl_surface_frame(dri2_surf->wl_win->surface);
717 wl_callback_add_listener(dri2_surf->throttle_callback,
718 &throttle_listener, dri2_surf);
719 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
720 dri2_dpy->wl_queue);
721 }
Neil Roberts25cc8892013-11-15 13:50:49 +0000722
Kristian Høgsberg6d4d4b02012-12-13 23:32:14 -0500723 dri2_surf->back->age = 1;
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500724 dri2_surf->current = dri2_surf->back;
725 dri2_surf->back = NULL;
Benjamin Franzke93aea842011-02-04 12:39:40 +0100726
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500727 create_wl_buffer(dri2_surf);
Benjamin Franzke93aea842011-02-04 12:39:40 +0100728
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500729 wl_surface_attach(dri2_surf->wl_win->surface,
730 dri2_surf->current->wl_buffer,
731 dri2_surf->dx, dri2_surf->dy);
732
733 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
734 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
735 /* reset resize growing parameters */
736 dri2_surf->dx = 0;
737 dri2_surf->dy = 0;
738
Derek Foremand085a5d2016-02-16 10:34:39 -0600739 /* If the compositor doesn't support damage_buffer, we deliberately
740 * ignore the damage region and post maximum damage, due to
Daniel Stoned1314de2015-11-07 18:25:31 +0000741 * https://bugs.freedesktop.org/78190 */
Derek Foremand085a5d2016-02-16 10:34:39 -0600742 if (!n_rects || !try_damage_buffer(dri2_surf, rects, n_rects))
743 wl_surface_damage(dri2_surf->wl_win->surface,
744 0, 0, INT32_MAX, INT32_MAX);
Kristian Høgsberg90804e82012-12-13 23:30:45 -0500745
Axel Davy4cd546d2015-05-01 01:16:24 +0200746 if (dri2_dpy->is_different_gpu) {
747 _EGLContext *ctx = _eglGetCurrentContext();
748 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
749 dri2_dpy->image->blitImage(dri2_ctx->dri_context,
750 dri2_surf->current->linear_copy,
751 dri2_surf->current->dri_image,
752 0, 0, dri2_surf->base.Width,
753 dri2_surf->base.Height,
754 0, 0, dri2_surf->base.Width,
755 dri2_surf->base.Height, 0);
756 }
757
Eric Anholt70e8ccc2014-12-21 11:51:33 -0800758 dri2_flush_drawable_for_swapbuffers(disp, draw);
Kristian Høgsberg89ba4362012-11-30 13:29:17 -0500759 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
Benjamin Franzke93aea842011-02-04 12:39:40 +0100760
Kristian Høgsberg33eb5ea2013-12-04 12:08:35 -0800761 wl_surface_commit(dri2_surf->wl_win->surface);
Neil Roberts992a2db2013-11-15 13:50:50 +0000762
763 /* If we're not waiting for a frame callback then we'll at least throttle
764 * to a sync callback so that we always give a chance for the compositor to
765 * handle the commit and send a release event before checking for a free
766 * buffer */
767 if (dri2_surf->throttle_callback == NULL) {
768 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy);
769 wl_callback_add_listener(dri2_surf->throttle_callback,
770 &throttle_listener, dri2_surf);
771 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
772 dri2_dpy->wl_queue);
773 }
774
Axel Davy402bf6e2013-12-03 17:38:09 +0100775 wl_display_flush(dri2_dpy->wl_dpy);
776
Benjamin Franzke93aea842011-02-04 12:39:40 +0100777 return EGL_TRUE;
778}
779
Kristian Høgsberg6d4d4b02012-12-13 23:32:14 -0500780static EGLint
Chad Versaced019cd82014-01-28 12:47:38 -0800781dri2_wl_query_buffer_age(_EGLDriver *drv,
782 _EGLDisplay *disp, _EGLSurface *surface)
Kristian Høgsberg6d4d4b02012-12-13 23:32:14 -0500783{
784 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
Kristian Høgsberg6d4d4b02012-12-13 23:32:14 -0500785
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -0800786 if (get_back_bo(dri2_surf) < 0) {
Kristian Høgsberg6d4d4b02012-12-13 23:32:14 -0500787 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
788 return 0;
789 }
790
791 return dri2_surf->back->age;
792}
793
Robert Braggf8c32422012-02-10 16:59:31 +0000794static EGLBoolean
Chad Versaced019cd82014-01-28 12:47:38 -0800795dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
Robert Braggf8c32422012-02-10 16:59:31 +0000796{
Chad Versaced019cd82014-01-28 12:47:38 -0800797 return dri2_wl_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
Robert Braggf8c32422012-02-10 16:59:31 +0000798}
799
Neil Roberts5cddb1c2013-10-28 15:07:03 +0000800static struct wl_buffer *
Chad Versaceeadd5e02014-01-28 17:03:03 -0800801dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
802 _EGLDisplay *disp,
803 _EGLImage *img)
Neil Roberts5cddb1c2013-10-28 15:07:03 +0000804{
805 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
806 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
807 __DRIimage *image = dri2_img->dri_image;
808 struct wl_buffer *buffer;
809 int width, height, format, pitch;
810 enum wl_drm_format wl_format;
811
812 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
813
814 switch (format) {
815 case __DRI_IMAGE_FORMAT_ARGB8888:
816 if (!(dri2_dpy->formats & HAS_ARGB8888))
817 goto bad_format;
818 wl_format = WL_DRM_FORMAT_ARGB8888;
819 break;
820 case __DRI_IMAGE_FORMAT_XRGB8888:
821 if (!(dri2_dpy->formats & HAS_XRGB8888))
822 goto bad_format;
823 wl_format = WL_DRM_FORMAT_XRGB8888;
824 break;
825 default:
826 goto bad_format;
827 }
828
829 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
830 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
831 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
832
833 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
834 int fd;
835
836 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
837
838 buffer =
839 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
840 fd,
841 width, height,
842 wl_format,
843 0, pitch,
844 0, 0,
845 0, 0);
846
847 close(fd);
848 } else {
849 int name;
850
851 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
852
853 buffer =
854 wl_drm_create_buffer(dri2_dpy->wl_drm,
855 name,
856 width, height,
857 pitch,
858 wl_format);
859 }
860
861 /* The buffer object will have been created with our internal event queue
862 * because it is using the wl_drm object as a proxy factory. We want the
863 * buffer to be used by the application so we'll reset it to the display's
864 * default event queue */
865 if (buffer)
866 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
867
868 return buffer;
869
870bad_format:
871 _eglError(EGL_BAD_MATCH, "unsupported image format");
872 return NULL;
873}
874
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100875static int
Chad Versaced019cd82014-01-28 12:47:38 -0800876dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100877{
878 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
879 int ret = 0;
880
Axel Davyfb0960a2015-05-01 01:30:10 +0200881 if (dri2_dpy->is_render_node) {
882 _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
883 "authenticate for render-nodes");
884 return 0;
885 }
Benjamin Franzkeaa87a932011-05-31 11:45:51 +0200886 dri2_dpy->authenticated = 0;
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100887
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -0400888 wl_drm_authenticate(dri2_dpy->wl_drm, id);
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -0400889 if (roundtrip(dri2_dpy) < 0)
890 ret = -1;
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100891
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -0400892 if (!dri2_dpy->authenticated)
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100893 ret = -1;
894
895 /* reset authenticated */
Benjamin Franzkeaa87a932011-05-31 11:45:51 +0200896 dri2_dpy->authenticated = 1;
Benjamin Franzke6b369c42011-02-21 16:22:34 +0100897
898 return ret;
899}
900
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -0400901static void
902drm_handle_device(void *data, struct wl_drm *drm, const char *device)
903{
904 struct dri2_egl_display *dri2_dpy = data;
905 drm_magic_t magic;
906
907 dri2_dpy->device_name = strdup(device);
908 if (!dri2_dpy->device_name)
909 return;
910
Derek Foreman4f8f7902015-06-17 11:28:51 -0500911 dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -0400912 if (dri2_dpy->fd == -1) {
913 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
914 dri2_dpy->device_name, strerror(errno));
915 return;
916 }
917
Emil Velikov175d9752015-07-10 12:27:06 +0100918 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) {
Axel Davyfb0960a2015-05-01 01:30:10 +0200919 dri2_dpy->authenticated = 1;
920 } else {
921 drmGetMagic(dri2_dpy->fd, &magic);
922 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
923 }
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -0400924}
925
926static void
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -0400927drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
928{
929 struct dri2_egl_display *dri2_dpy = data;
930
931 switch (format) {
Kristian Høgsberg58dc1b22012-01-11 14:23:24 -0500932 case WL_DRM_FORMAT_ARGB8888:
933 dri2_dpy->formats |= HAS_ARGB8888;
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -0400934 break;
Kristian Høgsberg58dc1b22012-01-11 14:23:24 -0500935 case WL_DRM_FORMAT_XRGB8888:
936 dri2_dpy->formats |= HAS_XRGB8888;
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -0400937 break;
Singh, Satyeshwar2efc97d2013-10-16 01:10:12 +0000938 case WL_DRM_FORMAT_RGB565:
939 dri2_dpy->formats |= HAS_RGB565;
940 break;
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -0400941 }
942}
943
944static void
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500945drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
946{
947 struct dri2_egl_display *dri2_dpy = data;
948
949 dri2_dpy->capabilities = value;
950}
951
952static void
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -0400953drm_handle_authenticated(void *data, struct wl_drm *drm)
954{
955 struct dri2_egl_display *dri2_dpy = data;
956
Benjamin Franzkeaa87a932011-05-31 11:45:51 +0200957 dri2_dpy->authenticated = 1;
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -0400958}
959
960static const struct wl_drm_listener drm_listener = {
Emil Velikov55674942015-07-10 12:24:11 +0100961 .device = drm_handle_device,
962 .format = drm_handle_format,
963 .authenticated = drm_handle_authenticated,
964 .capabilities = drm_handle_capabilities
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -0400965};
966
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -0400967static void
Axel Davycdcfe482015-05-01 11:11:20 +0200968registry_handle_global_drm(void *data, struct wl_registry *registry, uint32_t name,
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -0400969 const char *interface, uint32_t version)
970{
971 struct dri2_egl_display *dri2_dpy = data;
972
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500973 if (version > 1)
974 version = 2;
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -0400975 if (strcmp(interface, "wl_drm") == 0) {
976 dri2_dpy->wl_drm =
Kristian Høgsbergde315f72013-02-02 12:26:12 -0500977 wl_registry_bind(registry, name, &wl_drm_interface, version);
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -0400978 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
979 }
980}
981
Kristian Høgsberg712269d2013-06-18 16:53:46 -0400982static void
983registry_handle_global_remove(void *data, struct wl_registry *registry,
984 uint32_t name)
985{
986}
987
Axel Davycdcfe482015-05-01 11:11:20 +0200988static const struct wl_registry_listener registry_listener_drm = {
Emil Velikov55674942015-07-10 12:24:11 +0100989 .global = registry_handle_global_drm,
990 .global_remove = registry_handle_global_remove
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -0400991};
992
Neil Roberts992a2db2013-11-15 13:50:50 +0000993static EGLBoolean
Chad Versaced019cd82014-01-28 12:47:38 -0800994dri2_wl_swap_interval(_EGLDriver *drv,
Neil Roberts992a2db2013-11-15 13:50:50 +0000995 _EGLDisplay *disp,
996 _EGLSurface *surf,
997 EGLint interval)
998{
999 if (interval > surf->Config->MaxSwapInterval)
1000 interval = surf->Config->MaxSwapInterval;
1001 else if (interval < surf->Config->MinSwapInterval)
1002 interval = surf->Config->MinSwapInterval;
1003
1004 surf->SwapInterval = interval;
1005
1006 return EGL_TRUE;
1007}
1008
1009static void
Chad Versaced019cd82014-01-28 12:47:38 -08001010dri2_wl_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
Neil Roberts992a2db2013-11-15 13:50:50 +00001011{
1012 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1013
1014 /* We can't use values greater than 1 on Wayland because we are using the
1015 * frame callback to synchronise the frame and the only way we be sure to
1016 * get a frame callback is to attach a new buffer. Therefore we can't just
1017 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1018
1019 if (dri2_dpy->config)
1020 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1021 "vblank_mode", &vblank_mode);
1022 switch (vblank_mode) {
1023 case DRI_CONF_VBLANK_NEVER:
1024 dri2_dpy->min_swap_interval = 0;
1025 dri2_dpy->max_swap_interval = 0;
1026 dri2_dpy->default_swap_interval = 0;
1027 break;
1028 case DRI_CONF_VBLANK_ALWAYS_SYNC:
1029 dri2_dpy->min_swap_interval = 1;
1030 dri2_dpy->max_swap_interval = 1;
1031 dri2_dpy->default_swap_interval = 1;
1032 break;
1033 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1034 dri2_dpy->min_swap_interval = 0;
1035 dri2_dpy->max_swap_interval = 1;
1036 dri2_dpy->default_swap_interval = 0;
1037 break;
1038 default:
1039 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1040 dri2_dpy->min_swap_interval = 0;
1041 dri2_dpy->max_swap_interval = 1;
1042 dri2_dpy->default_swap_interval = 1;
1043 break;
1044 }
1045}
1046
Chad Versace90502b12014-01-28 11:41:46 -08001047static struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
Chad Versaced019cd82014-01-28 12:47:38 -08001048 .authenticate = dri2_wl_authenticate,
Chad Versace0a0c8812014-01-28 16:39:09 -08001049 .create_window_surface = dri2_wl_create_window_surface,
Chad Versace9a40ee12014-02-09 09:13:27 -08001050 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
Chad Versacebf200762014-01-28 17:03:03 -08001051 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
Chad Versace958dd802014-01-28 17:03:03 -08001052 .destroy_surface = dri2_wl_destroy_surface,
Chad Versaceeef68a92014-01-28 17:03:03 -08001053 .create_image = dri2_create_image_khr,
Chad Versace8b9298a2014-01-28 12:34:19 -08001054 .swap_interval = dri2_wl_swap_interval,
Chad Versacead173bc2014-01-28 16:21:21 -08001055 .swap_buffers = dri2_wl_swap_buffers,
Chad Versaced03948a2014-01-28 16:26:44 -08001056 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
Chad Versace75d398e2014-01-28 17:03:03 -08001057 .swap_buffers_region = dri2_fallback_swap_buffers_region,
Chad Versace688a0e82014-01-28 17:03:03 -08001058 .post_sub_buffer = dri2_fallback_post_sub_buffer,
Chad Versacebc2cbc02014-01-28 17:03:03 -08001059 .copy_buffers = dri2_fallback_copy_buffers,
Chad Versace3fdfbd22014-01-28 17:03:03 -08001060 .query_buffer_age = dri2_wl_query_buffer_age,
Chad Versaceeadd5e02014-01-28 17:03:03 -08001061 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
Sarah Sharpc524f3e2014-05-06 12:10:57 -07001062 .get_sync_values = dri2_fallback_get_sync_values,
Boyan Dinga25df542015-07-21 23:43:59 +08001063 .get_dri_drawable = dri2_surface_get_dri_drawable,
Chad Versace90502b12014-01-28 11:41:46 -08001064};
1065
Axel Davycdcfe482015-05-01 11:11:20 +02001066static EGLBoolean
1067dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
Benjamin Franzke93aea842011-02-04 12:39:40 +01001068{
1069 struct dri2_egl_display *dri2_dpy;
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -04001070 const __DRIconfig *config;
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -04001071 uint32_t types;
Benjamin Franzke93aea842011-02-04 12:39:40 +01001072 int i;
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -04001073 static const unsigned int argb_masks[4] =
1074 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1075 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
Singh, Satyeshwar2efc97d2013-10-16 01:10:12 +00001076 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
Benjamin Franzke93aea842011-02-04 12:39:40 +01001077
Emil Velikov8d4357b2014-01-11 04:52:48 +00001078 loader_set_logger(_eglLog);
1079
Matt Turner6bda0272012-09-04 23:09:22 -07001080 dri2_dpy = calloc(1, sizeof *dri2_dpy);
Benjamin Franzke93aea842011-02-04 12:39:40 +01001081 if (!dri2_dpy)
1082 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1083
1084 disp->DriverData = (void *) dri2_dpy;
Benjamin Franzkeb8325fd2011-06-11 22:07:02 +02001085 if (disp->PlatformDisplay == NULL) {
1086 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1087 if (dri2_dpy->wl_dpy == NULL)
1088 goto cleanup_dpy;
Benjamin Franzke2a584532011-12-13 14:43:48 +01001089 dri2_dpy->own_device = 1;
Benjamin Franzkeb8325fd2011-06-11 22:07:02 +02001090 } else {
1091 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1092 }
Benjamin Franzke93aea842011-02-04 12:39:40 +01001093
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -04001094 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
Kristian Høgsberg112ccfa2013-02-26 12:49:40 -05001095
1096 if (dri2_dpy->own_device)
1097 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1098
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -04001099 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
1100 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
1101 dri2_dpy->wl_queue);
1102 wl_registry_add_listener(dri2_dpy->wl_registry,
Axel Davycdcfe482015-05-01 11:11:20 +02001103 &registry_listener_drm, dri2_dpy);
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -04001104 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
Axel Davy6aaf09b2015-05-01 00:03:32 +02001105 goto cleanup_registry;
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -04001106
1107 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -04001108 goto cleanup_drm;
Kristian Høgsberg56758c82011-02-04 15:37:51 -05001109
Kristian Høgsberg0229e3a2012-10-10 22:10:42 -04001110 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -04001111 goto cleanup_fd;
Benjamin Franzke93aea842011-02-04 12:39:40 +01001112
Axel Davy4cd546d2015-05-01 01:16:24 +02001113 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1114 &dri2_dpy->is_different_gpu);
1115 if (dri2_dpy->is_different_gpu) {
1116 free(dri2_dpy->device_name);
1117 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1118 if (!dri2_dpy->device_name) {
1119 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1120 "for requested GPU");
1121 goto cleanup_fd;
1122 }
1123 }
1124
1125 /* we have to do the check now, because loader_get_user_preferred_fd
1126 * will return a render-node when the requested gpu is different
1127 * to the server, but also if the client asks for the same gpu than
1128 * the server by requesting its pci-id */
Emil Velikov175d9752015-07-10 12:27:06 +01001129 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
Axel Davy4cd546d2015-05-01 01:16:24 +02001130
Emil Velikov8d4357b2014-01-11 04:52:48 +00001131 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
Benjamin Franzke93aea842011-02-04 12:39:40 +01001132 if (dri2_dpy->driver_name == NULL) {
1133 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1134 goto cleanup_fd;
1135 }
1136
Benjamin Franzke6b369c42011-02-21 16:22:34 +01001137 if (!dri2_load_driver(disp))
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -04001138 goto cleanup_driver_name;
Benjamin Franzke93aea842011-02-04 12:39:40 +01001139
Axel Davyfb0960a2015-05-01 01:30:10 +02001140 dri2_dpy->extensions[0] = &image_loader_extension.base;
1141 dri2_dpy->extensions[1] = &image_lookup_extension.base;
1142 dri2_dpy->extensions[2] = &use_invalidate.base;
Kristian Høgsberg68bb26be2013-11-08 22:10:36 -08001143
Axel Davyfb0960a2015-05-01 01:30:10 +02001144 /* render nodes cannot use Gem names, and thus do not support
1145 * the __DRI_DRI2_LOADER extension */
1146 if (!dri2_dpy->is_render_node) {
1147 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1148 dri2_dpy->dri2_loader_extension.base.version = 3;
1149 dri2_dpy->dri2_loader_extension.getBuffers = dri2_wl_get_buffers;
1150 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_wl_flush_front_buffer;
1151 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1152 dri2_wl_get_buffers_with_format;
1153 dri2_dpy->extensions[3] = &dri2_dpy->dri2_loader_extension.base;
1154 dri2_dpy->extensions[4] = NULL;
1155 } else
1156 dri2_dpy->extensions[3] = NULL;
Benjamin Franzke93aea842011-02-04 12:39:40 +01001157
Neil Roberts992a2db2013-11-15 13:50:50 +00001158 dri2_dpy->swap_available = EGL_TRUE;
1159
Benjamin Franzke93aea842011-02-04 12:39:40 +01001160 if (!dri2_create_screen(disp))
1161 goto cleanup_driver;
1162
Chad Versaced019cd82014-01-28 12:47:38 -08001163 dri2_wl_setup_swap_interval(dri2_dpy);
Neil Roberts992a2db2013-11-15 13:50:50 +00001164
Axel Davy4cd546d2015-05-01 01:16:24 +02001165 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1166 * createImageFromFds support indicates that Prime export/import
1167 * is supported by the driver. Fall back to
1168 * gem names if we don't have Prime support. */
Kristian Høgsberg16707372013-03-19 13:20:36 -04001169
1170 if (dri2_dpy->image->base.version < 7 ||
1171 dri2_dpy->image->createImageFromFds == NULL)
Neil Roberts63d46612014-04-08 23:28:40 +03001172 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
Kristian Høgsberg16707372013-03-19 13:20:36 -04001173
Axel Davyfb0960a2015-05-01 01:30:10 +02001174 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1175 * The server needs to accept them */
1176 if (dri2_dpy->is_render_node &&
1177 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1178 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1179 goto cleanup_screen;
1180 }
1181
Axel Davy4cd546d2015-05-01 01:16:24 +02001182 if (dri2_dpy->is_different_gpu &&
1183 (dri2_dpy->image->base.version < 9 ||
1184 dri2_dpy->image->blitImage == NULL)) {
1185 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1186 "Image extension in the driver is not "
1187 "compatible. Version 9 or later and blitImage() "
1188 "are required");
1189 goto cleanup_screen;
1190 }
1191
Kristian Høgsberge20a0f12012-10-16 14:30:53 -04001192 types = EGL_WINDOW_BIT;
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -04001193 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1194 config = dri2_dpy->driver_configs[i];
Kristian Høgsberg58dc1b22012-01-11 14:23:24 -05001195 if (dri2_dpy->formats & HAS_XRGB8888)
Kristian Høgsberg44e584a2013-09-14 23:13:22 -07001196 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
Kristian Høgsberg58dc1b22012-01-11 14:23:24 -05001197 if (dri2_dpy->formats & HAS_ARGB8888)
Kristian Høgsberg44e584a2013-09-14 23:13:22 -07001198 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
Singh, Satyeshwar2efc97d2013-10-16 01:10:12 +00001199 if (dri2_dpy->formats & HAS_RGB565)
Kristian Høgsberg44e584a2013-09-14 23:13:22 -07001200 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
Kristian Høgsberg7b1d94e2011-08-31 16:45:04 -04001201 }
Benjamin Franzke93aea842011-02-04 12:39:40 +01001202
Benjamin Franzke6b369c42011-02-21 16:22:34 +01001203 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
Axel Davy4cd546d2015-05-01 01:16:24 +02001204 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1205 * because the buffer of the EGLImage has likely a tiling mode the server
1206 * gpu won't support. These is no way to check for now. Thus do not support the
1207 * extension */
1208 if (!dri2_dpy->is_different_gpu) {
1209 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1210 } else {
1211 dri2_wl_display_vtbl.create_wayland_buffer_from_image =
1212 dri2_fallback_create_wayland_buffer_from_image;
1213 }
Kristian Høgsberg6d4d4b02012-12-13 23:32:14 -05001214 disp->Extensions.EXT_buffer_age = EGL_TRUE;
Benjamin Franzke6b369c42011-02-21 16:22:34 +01001215
Robert Braggf8c32422012-02-10 16:59:31 +00001216 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1217
Chad Versace90502b12014-01-28 11:41:46 -08001218 /* Fill vtbl last to prevent accidentally calling virtual function during
1219 * initialization.
1220 */
1221 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1222
Benjamin Franzke93aea842011-02-04 12:39:40 +01001223 return EGL_TRUE;
1224
Axel Davyfb0960a2015-05-01 01:30:10 +02001225 cleanup_screen:
1226 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
Benjamin Franzke93aea842011-02-04 12:39:40 +01001227 cleanup_driver:
1228 dlclose(dri2_dpy->driver);
Benjamin Franzke93aea842011-02-04 12:39:40 +01001229 cleanup_driver_name:
1230 free(dri2_dpy->driver_name);
1231 cleanup_fd:
1232 close(dri2_dpy->fd);
Kristian Høgsbergc0f8c992011-04-14 10:42:41 -04001233 cleanup_drm:
1234 free(dri2_dpy->device_name);
1235 wl_drm_destroy(dri2_dpy->wl_drm);
Axel Davy6aaf09b2015-05-01 00:03:32 +02001236 cleanup_registry:
1237 wl_registry_destroy(dri2_dpy->wl_registry);
1238 wl_event_queue_destroy(dri2_dpy->wl_queue);
Benjamin Franzke93aea842011-02-04 12:39:40 +01001239 cleanup_dpy:
1240 free(dri2_dpy);
Boyan Ding052b3d42015-06-13 15:36:27 +08001241
Benjamin Franzke93aea842011-02-04 12:39:40 +01001242 return EGL_FALSE;
1243}
Axel Davycdcfe482015-05-01 11:11:20 +02001244
1245static int
1246dri2_wl_swrast_get_stride_for_format(int format, int w)
1247{
1248 if (format == WL_SHM_FORMAT_RGB565)
1249 return 2 * w;
1250 else /* ARGB8888 || XRGB8888 */
1251 return 4 * w;
1252}
1253
1254/*
1255 * Taken from weston shared/os-compatibility.c
1256 */
1257
Boyan Dingd7bafca2015-08-21 21:44:36 +08001258#ifndef HAVE_MKOSTEMP
1259
Axel Davycdcfe482015-05-01 11:11:20 +02001260static int
1261set_cloexec_or_close(int fd)
1262{
1263 long flags;
1264
1265 if (fd == -1)
1266 return -1;
1267
1268 flags = fcntl(fd, F_GETFD);
1269 if (flags == -1)
1270 goto err;
1271
1272 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
1273 goto err;
1274
1275 return fd;
1276
1277err:
1278 close(fd);
1279 return -1;
1280}
1281
Boyan Dingd7bafca2015-08-21 21:44:36 +08001282#endif
1283
Axel Davycdcfe482015-05-01 11:11:20 +02001284/*
1285 * Taken from weston shared/os-compatibility.c
1286 */
1287
1288static int
1289create_tmpfile_cloexec(char *tmpname)
1290{
1291 int fd;
1292
1293#ifdef HAVE_MKOSTEMP
1294 fd = mkostemp(tmpname, O_CLOEXEC);
1295 if (fd >= 0)
1296 unlink(tmpname);
1297#else
1298 fd = mkstemp(tmpname);
1299 if (fd >= 0) {
1300 fd = set_cloexec_or_close(fd);
1301 unlink(tmpname);
1302 }
1303#endif
1304
1305 return fd;
1306}
1307
1308/*
1309 * Taken from weston shared/os-compatibility.c
1310 *
1311 * Create a new, unique, anonymous file of the given size, and
1312 * return the file descriptor for it. The file descriptor is set
1313 * CLOEXEC. The file is immediately suitable for mmap()'ing
1314 * the given size at offset zero.
1315 *
1316 * The file should not have a permanent backing store like a disk,
1317 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1318 *
1319 * The file name is deleted from the file system.
1320 *
1321 * The file is suitable for buffer sharing between processes by
1322 * transmitting the file descriptor over Unix sockets using the
1323 * SCM_RIGHTS methods.
1324 *
1325 * If the C library implements posix_fallocate(), it is used to
1326 * guarantee that disk space is available for the file at the
1327 * given size. If disk space is insufficent, errno is set to ENOSPC.
1328 * If posix_fallocate() is not supported, program may receive
1329 * SIGBUS on accessing mmap()'ed file contents instead.
1330 */
1331static int
1332os_create_anonymous_file(off_t size)
1333{
1334 static const char template[] = "/mesa-shared-XXXXXX";
1335 const char *path;
1336 char *name;
1337 int fd;
1338 int ret;
1339
1340 path = getenv("XDG_RUNTIME_DIR");
1341 if (!path) {
1342 errno = ENOENT;
1343 return -1;
1344 }
1345
1346 name = malloc(strlen(path) + sizeof(template));
1347 if (!name)
1348 return -1;
1349
1350 strcpy(name, path);
1351 strcat(name, template);
1352
1353 fd = create_tmpfile_cloexec(name);
1354
1355 free(name);
1356
1357 if (fd < 0)
1358 return -1;
1359
1360 ret = ftruncate(fd, size);
1361 if (ret < 0) {
1362 close(fd);
1363 return -1;
1364 }
1365
1366 return fd;
1367}
1368
1369
1370static EGLBoolean
1371dri2_wl_swrast_allocate_buffer(struct dri2_egl_display *dri2_dpy,
1372 int format, int w, int h,
1373 void **data, int *size,
1374 struct wl_buffer **buffer)
1375{
1376 struct wl_shm_pool *pool;
1377 int fd, stride, size_map;
1378 void *data_map;
1379
1380 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1381 size_map = h * stride;
1382
1383 /* Create a sharable buffer */
1384 fd = os_create_anonymous_file(size_map);
1385 if (fd < 0)
1386 return EGL_FALSE;
1387
1388 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1389 if (data_map == MAP_FAILED) {
1390 close(fd);
1391 return EGL_FALSE;
1392 }
1393
1394 /* Share it in a wl_buffer */
1395 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1396 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1397 wl_shm_pool_destroy(pool);
1398 close(fd);
1399
1400 *data = data_map;
1401 *size = size_map;
1402 return EGL_TRUE;
1403}
1404
1405static int
1406swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1407{
1408 struct dri2_egl_display *dri2_dpy =
1409 dri2_egl_display(dri2_surf->base.Resource.Display);
1410 int i;
1411
1412 /* we need to do the following operations only once per frame */
1413 if (dri2_surf->back)
1414 return 0;
1415
1416 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1417 dri2_surf->base.Height != dri2_surf->wl_win->height) {
1418
1419 dri2_wl_release_buffers(dri2_surf);
1420
1421 dri2_surf->base.Width = dri2_surf->wl_win->width;
1422 dri2_surf->base.Height = dri2_surf->wl_win->height;
1423 dri2_surf->dx = dri2_surf->wl_win->dx;
1424 dri2_surf->dy = dri2_surf->wl_win->dy;
1425 dri2_surf->current = NULL;
1426 }
1427
1428 /* find back buffer */
1429
1430 /* We always want to throttle to some event (either a frame callback or
1431 * a sync request) after the commit so that we can be sure the
1432 * compositor has had a chance to handle it and send us a release event
1433 * before we look for a free buffer */
1434 while (dri2_surf->throttle_callback != NULL)
1435 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1436 dri2_dpy->wl_queue) == -1)
1437 return -1;
1438
1439 /* try get free buffer already created */
1440 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1441 if (!dri2_surf->color_buffers[i].locked &&
1442 dri2_surf->color_buffers[i].wl_buffer) {
1443 dri2_surf->back = &dri2_surf->color_buffers[i];
1444 break;
1445 }
1446 }
1447
1448 /* else choose any another free location */
1449 if (!dri2_surf->back) {
1450 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1451 if (!dri2_surf->color_buffers[i].locked) {
1452 dri2_surf->back = &dri2_surf->color_buffers[i];
1453 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy,
1454 dri2_surf->format,
1455 dri2_surf->base.Width,
1456 dri2_surf->base.Height,
1457 &dri2_surf->back->data,
1458 &dri2_surf->back->data_size,
1459 &dri2_surf->back->wl_buffer)) {
1460 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1461 return -1;
1462 }
1463 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->back->wl_buffer,
1464 dri2_dpy->wl_queue);
1465 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1466 &wl_buffer_listener, dri2_surf);
1467 break;
1468 }
1469 }
1470 }
1471
1472 if (!dri2_surf->back) {
1473 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1474 return -1;
1475 }
1476
1477 dri2_surf->back->locked = 1;
1478
1479 /* If we have an extra unlocked buffer at this point, we had to do triple
1480 * buffering for a while, but now can go back to just double buffering.
1481 * That means we can free any unlocked buffer now. */
1482 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1483 if (!dri2_surf->color_buffers[i].locked &&
1484 dri2_surf->color_buffers[i].wl_buffer) {
1485 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1486 munmap(dri2_surf->color_buffers[i].data,
1487 dri2_surf->color_buffers[i].data_size);
1488 dri2_surf->color_buffers[i].wl_buffer = NULL;
1489 dri2_surf->color_buffers[i].data = NULL;
1490 }
1491 }
1492
1493 return 0;
1494}
1495
1496static void*
1497dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1498{
1499 /* if there has been a resize: */
1500 if (!dri2_surf->current)
1501 return NULL;
1502
1503 return dri2_surf->current->data;
1504}
1505
1506static void*
1507dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1508{
1509 assert(dri2_surf->back);
1510 return dri2_surf->back->data;
1511}
1512
1513static void
1514dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1515{
1516 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1517
1518 if (dri2_surf->base.SwapInterval > 0) {
1519 dri2_surf->throttle_callback =
1520 wl_surface_frame(dri2_surf->wl_win->surface);
1521 wl_callback_add_listener(dri2_surf->throttle_callback,
1522 &throttle_listener, dri2_surf);
1523 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
1524 dri2_dpy->wl_queue);
1525 }
1526
1527 dri2_surf->current = dri2_surf->back;
1528 dri2_surf->back = NULL;
1529
1530 wl_surface_attach(dri2_surf->wl_win->surface,
1531 dri2_surf->current->wl_buffer,
1532 dri2_surf->dx, dri2_surf->dy);
1533
1534 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1535 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1536 /* reset resize growing parameters */
1537 dri2_surf->dx = 0;
1538 dri2_surf->dy = 0;
1539
1540 wl_surface_damage(dri2_surf->wl_win->surface,
1541 0, 0, INT32_MAX, INT32_MAX);
1542 wl_surface_commit(dri2_surf->wl_win->surface);
1543
1544 /* If we're not waiting for a frame callback then we'll at least throttle
1545 * to a sync callback so that we always give a chance for the compositor to
1546 * handle the commit and send a release event before checking for a free
1547 * buffer */
1548 if (dri2_surf->throttle_callback == NULL) {
1549 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy);
1550 wl_callback_add_listener(dri2_surf->throttle_callback,
1551 &throttle_listener, dri2_surf);
1552 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
1553 dri2_dpy->wl_queue);
1554 }
1555
1556 wl_display_flush(dri2_dpy->wl_dpy);
1557}
1558
1559static void
1560dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1561 int *x, int *y, int *w, int *h,
1562 void *loaderPrivate)
1563{
1564 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1565
1566 (void) swrast_update_buffers(dri2_surf);
1567 *x = 0;
1568 *y = 0;
1569 *w = dri2_surf->base.Width;
1570 *h = dri2_surf->base.Height;
1571}
1572
1573static void
1574dri2_wl_swrast_get_image(__DRIdrawable * read,
1575 int x, int y, int w, int h,
1576 char *data, void *loaderPrivate)
1577{
1578 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1579 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1580 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1581 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1582 int dst_stride = copy_width;
1583 char *src, *dst;
1584
1585 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1586 if (!src) {
1587 memset(data, 0, copy_width * h);
1588 return;
1589 }
1590
1591 assert(data != src);
1592 assert(copy_width <= src_stride);
1593
1594 src += x_offset;
1595 src += y * src_stride;
1596 dst = data;
1597
1598 if (copy_width > src_stride-x_offset)
1599 copy_width = src_stride-x_offset;
1600 if (h > dri2_surf->base.Height-y)
1601 h = dri2_surf->base.Height-y;
1602
1603 for (; h>0; h--) {
1604 memcpy(dst, src, copy_width);
1605 src += src_stride;
1606 dst += dst_stride;
1607 }
1608}
1609
1610static void
1611dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1612 int x, int y, int w, int h, int stride,
1613 char *data, void *loaderPrivate)
1614{
1615 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1616 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1617 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1618 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1619 char *src, *dst;
1620
1621 assert(copy_width <= stride);
1622
1623 (void) swrast_update_buffers(dri2_surf);
1624 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1625
1626 /* partial copy, copy old content */
1627 if (copy_width < dst_stride)
1628 dri2_wl_swrast_get_image(draw, 0, 0,
1629 dri2_surf->base.Width, dri2_surf->base.Height,
1630 dst, loaderPrivate);
1631
1632 dst += x_offset;
1633 dst += y * dst_stride;
1634
1635 src = data;
1636
1637 /* drivers expect we do these checks (and some rely on it) */
1638 if (copy_width > dst_stride-x_offset)
1639 copy_width = dst_stride-x_offset;
1640 if (h > dri2_surf->base.Height-y)
1641 h = dri2_surf->base.Height-y;
1642
1643 for (; h>0; h--) {
1644 memcpy(dst, src, copy_width);
1645 src += stride;
1646 dst += dst_stride;
1647 }
1648 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1649}
1650
1651static void
1652dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1653 int x, int y, int w, int h,
1654 char *data, void *loaderPrivate)
1655{
1656 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1657 int stride;
1658
1659 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1660 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1661 stride, data, loaderPrivate);
1662}
1663
1664/**
1665 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1666 */
1667static _EGLSurface *
1668dri2_wl_swrast_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
1669 _EGLConfig *conf, void *native_window,
1670 const EGLint *attrib_list)
1671{
1672 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1673 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
1674 struct wl_egl_window *window = native_window;
1675 struct dri2_egl_surface *dri2_surf;
Emil Velikov4ea5ed92015-09-13 12:36:54 +01001676 const __DRIconfig *config;
Axel Davycdcfe482015-05-01 11:11:20 +02001677
1678 (void) drv;
1679
1680 dri2_surf = calloc(1, sizeof *dri2_surf);
1681 if (!dri2_surf) {
1682 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
1683 return NULL;
1684 }
1685
1686 if (!_eglInitSurface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, attrib_list))
1687 goto cleanup_surf;
1688
1689 if (conf->RedSize == 5)
1690 dri2_surf->format = WL_SHM_FORMAT_RGB565;
1691 else if (conf->AlphaSize == 0)
1692 dri2_surf->format = WL_SHM_FORMAT_XRGB8888;
1693 else
1694 dri2_surf->format = WL_SHM_FORMAT_ARGB8888;
1695
1696 dri2_surf->wl_win = window;
1697
1698 dri2_surf->base.Width = -1;
1699 dri2_surf->base.Height = -1;
1700
Emil Velikov4ea5ed92015-09-13 12:36:54 +01001701 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
1702 dri2_surf->base.GLColorspace);
1703
Axel Davycdcfe482015-05-01 11:11:20 +02001704 dri2_surf->dri_drawable =
Emil Velikov4ea5ed92015-09-13 12:36:54 +01001705 (*dri2_dpy->swrast->createNewDrawable)(dri2_dpy->dri_screen,
1706 config, dri2_surf);
Axel Davycdcfe482015-05-01 11:11:20 +02001707 if (dri2_surf->dri_drawable == NULL) {
1708 _eglError(EGL_BAD_ALLOC, "swrast->createNewDrawable");
1709 goto cleanup_dri_drawable;
1710 }
1711
1712 dri2_wl_swap_interval(drv, disp, &dri2_surf->base,
1713 dri2_dpy->default_swap_interval);
1714
1715 return &dri2_surf->base;
1716
1717 cleanup_dri_drawable:
1718 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
1719 cleanup_surf:
1720 free(dri2_surf);
1721
1722 return NULL;
1723}
1724
1725static EGLBoolean
1726dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1727{
1728 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1729 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1730
1731 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1732 return EGL_TRUE;
1733}
1734
1735static void
1736shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1737{
1738 struct dri2_egl_display *dri2_dpy = data;
1739
1740 switch (format) {
1741 case WL_SHM_FORMAT_ARGB8888:
1742 dri2_dpy->formats |= HAS_ARGB8888;
1743 break;
1744 case WL_SHM_FORMAT_XRGB8888:
1745 dri2_dpy->formats |= HAS_XRGB8888;
1746 break;
1747 case WL_SHM_FORMAT_RGB565:
1748 dri2_dpy->formats |= HAS_RGB565;
1749 break;
1750 }
1751}
1752
1753static const struct wl_shm_listener shm_listener = {
Emil Velikov55674942015-07-10 12:24:11 +01001754 .format = shm_handle_format
Axel Davycdcfe482015-05-01 11:11:20 +02001755};
1756
1757static void
1758registry_handle_global_swrast(void *data, struct wl_registry *registry, uint32_t name,
1759 const char *interface, uint32_t version)
1760{
1761 struct dri2_egl_display *dri2_dpy = data;
1762
1763 if (strcmp(interface, "wl_shm") == 0) {
1764 dri2_dpy->wl_shm =
1765 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1766 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1767 }
1768}
1769
1770static const struct wl_registry_listener registry_listener_swrast = {
Emil Velikov55674942015-07-10 12:24:11 +01001771 .global = registry_handle_global_swrast,
1772 .global_remove = registry_handle_global_remove
Axel Davycdcfe482015-05-01 11:11:20 +02001773};
1774
1775static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1776 .authenticate = NULL,
1777 .create_window_surface = dri2_wl_swrast_create_window_surface,
1778 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1779 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1780 .destroy_surface = dri2_wl_destroy_surface,
1781 .create_image = dri2_fallback_create_image_khr,
1782 .swap_interval = dri2_wl_swap_interval,
1783 .swap_buffers = dri2_wl_swrast_swap_buffers,
1784 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1785 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1786 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1787 .copy_buffers = dri2_fallback_copy_buffers,
1788 .query_buffer_age = dri2_fallback_query_buffer_age,
1789 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1790 .get_sync_values = dri2_fallback_get_sync_values,
Boyan Dinga25df542015-07-21 23:43:59 +08001791 .get_dri_drawable = dri2_surface_get_dri_drawable,
Axel Davycdcfe482015-05-01 11:11:20 +02001792};
1793
1794static EGLBoolean
1795dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1796{
1797 struct dri2_egl_display *dri2_dpy;
1798 const __DRIconfig *config;
1799 uint32_t types;
1800 int i;
1801 static const unsigned int argb_masks[4] =
1802 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1803 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
1804 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1805
1806 loader_set_logger(_eglLog);
1807
1808 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1809 if (!dri2_dpy)
1810 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1811
1812 disp->DriverData = (void *) dri2_dpy;
1813 if (disp->PlatformDisplay == NULL) {
1814 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1815 if (dri2_dpy->wl_dpy == NULL)
1816 goto cleanup_dpy;
1817 dri2_dpy->own_device = 1;
1818 } else {
1819 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1820 }
1821
1822 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1823
1824 if (dri2_dpy->own_device)
1825 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1826
1827 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
1828 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
1829 dri2_dpy->wl_queue);
1830 wl_registry_add_listener(dri2_dpy->wl_registry,
1831 &registry_listener_swrast, dri2_dpy);
1832
1833 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
1834 goto cleanup_registry;
1835
1836 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
1837 goto cleanup_shm;
1838
Emil Velikova1ac7422015-09-10 14:41:38 +01001839 dri2_dpy->fd = -1;
Axel Davycdcfe482015-05-01 11:11:20 +02001840 dri2_dpy->driver_name = strdup("swrast");
1841 if (!dri2_load_driver_swrast(disp))
1842 goto cleanup_shm;
1843
1844 dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
1845 dri2_dpy->swrast_loader_extension.base.version = 2;
1846 dri2_dpy->swrast_loader_extension.getDrawableInfo = dri2_wl_swrast_get_drawable_info;
1847 dri2_dpy->swrast_loader_extension.putImage = dri2_wl_swrast_put_image;
1848 dri2_dpy->swrast_loader_extension.getImage = dri2_wl_swrast_get_image;
1849 dri2_dpy->swrast_loader_extension.putImage2 = dri2_wl_swrast_put_image2;
1850
1851 dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
1852 dri2_dpy->extensions[1] = NULL;
1853
1854 if (!dri2_create_screen(disp))
1855 goto cleanup_driver;
1856
1857 dri2_wl_setup_swap_interval(dri2_dpy);
1858
1859 types = EGL_WINDOW_BIT;
1860 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1861 config = dri2_dpy->driver_configs[i];
1862 if (dri2_dpy->formats & HAS_XRGB8888)
1863 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
1864 if (dri2_dpy->formats & HAS_ARGB8888)
1865 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
1866 if (dri2_dpy->formats & HAS_RGB565)
1867 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
1868 }
1869
Axel Davycdcfe482015-05-01 11:11:20 +02001870 /* Fill vtbl last to prevent accidentally calling virtual function during
1871 * initialization.
1872 */
1873 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
1874
1875 return EGL_TRUE;
1876
1877 cleanup_driver:
1878 dlclose(dri2_dpy->driver);
1879 cleanup_shm:
1880 wl_shm_destroy(dri2_dpy->wl_shm);
1881 cleanup_registry:
1882 wl_registry_destroy(dri2_dpy->wl_registry);
1883 wl_event_queue_destroy(dri2_dpy->wl_queue);
1884 cleanup_dpy:
1885 free(dri2_dpy);
1886
1887 return EGL_FALSE;
1888}
1889
1890EGLBoolean
1891dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
1892{
1893 EGLBoolean initialized = EGL_TRUE;
1894
1895 int hw_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1896
1897 if (hw_accel) {
1898 if (!dri2_initialize_wayland_drm(drv, disp)) {
1899 initialized = dri2_initialize_wayland_swrast(drv, disp);
1900 }
1901 } else {
1902 initialized = dri2_initialize_wayland_swrast(drv, disp);
1903 }
1904
1905 return initialized;
1906
1907}