e7ae3a3aa2a71a5b04004f3a552da825678a2a45
[mesa.git] / src / egl / drivers / dri2 / platform_device.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright 2018 Collabora
5 *
6 * Based on platform_surfaceless, which has:
7 *
8 * Copyright (c) 2014 The Chromium OS Authors.
9 * Copyright © 2011 Intel Corporation
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 */
29 #ifdef HAVE_LIBDRM
30 #include <xf86drm.h>
31 #endif
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <dlfcn.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41
42 #include "egl_dri2.h"
43 #include "egl_dri2_fallbacks.h"
44 #include "loader.h"
45
46 static __DRIimage*
47 device_alloc_image(struct dri2_egl_display *dri2_dpy,
48 struct dri2_egl_surface *dri2_surf)
49 {
50 return dri2_dpy->image->createImage(
51 dri2_dpy->dri_screen,
52 dri2_surf->base.Width,
53 dri2_surf->base.Height,
54 dri2_surf->visual,
55 0,
56 NULL);
57 }
58
59 static void
60 device_free_images(struct dri2_egl_surface *dri2_surf)
61 {
62 struct dri2_egl_display *dri2_dpy =
63 dri2_egl_display(dri2_surf->base.Resource.Display);
64
65 if (dri2_surf->front) {
66 dri2_dpy->image->destroyImage(dri2_surf->front);
67 dri2_surf->front = NULL;
68 }
69 }
70
71 static int
72 device_image_get_buffers(__DRIdrawable *driDrawable,
73 unsigned int format,
74 uint32_t *stamp,
75 void *loaderPrivate,
76 uint32_t buffer_mask,
77 struct __DRIimageList *buffers)
78 {
79 struct dri2_egl_surface *dri2_surf = loaderPrivate;
80 struct dri2_egl_display *dri2_dpy =
81 dri2_egl_display(dri2_surf->base.Resource.Display);
82
83 buffers->image_mask = 0;
84 buffers->front = NULL;
85 buffers->back = NULL;
86
87 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
88 * the spec states that they have a back buffer but no front buffer, in
89 * contrast to pixmaps, which have a front buffer but no back buffer.
90 *
91 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
92 * from the spec, following the precedent of Mesa's EGL X11 platform. The
93 * X11 platform correctly assigns pbuffers to single-buffered configs, but
94 * assigns the pbuffer a front buffer instead of a back buffer.
95 *
96 * Pbuffers in the X11 platform mostly work today, so let's just copy its
97 * behavior instead of trying to fix (and hence potentially breaking) the
98 * world.
99 */
100
101 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
102
103 if (!dri2_surf->front)
104 dri2_surf->front =
105 device_alloc_image(dri2_dpy, dri2_surf);
106
107 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
108 buffers->front = dri2_surf->front;
109 }
110
111 return 1;
112 }
113
114 static _EGLSurface *
115 dri2_device_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
116 _EGLConfig *conf, const EGLint *attrib_list)
117 {
118 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
119 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
120 struct dri2_egl_surface *dri2_surf;
121 const __DRIconfig *config;
122
123 /* Make sure to calloc so all pointers
124 * are originally NULL.
125 */
126 dri2_surf = calloc(1, sizeof *dri2_surf);
127
128 if (!dri2_surf) {
129 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
130 return NULL;
131 }
132
133 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
134 false, NULL))
135 goto cleanup_surface;
136
137 config = dri2_get_dri_config(dri2_conf, type,
138 dri2_surf->base.GLColorspace);
139
140 if (!config) {
141 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
142 goto cleanup_surface;
143 }
144
145 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
146 goto cleanup_surface;
147
148 if (conf->RedSize == 5)
149 dri2_surf->visual = __DRI_IMAGE_FORMAT_RGB565;
150 else if (conf->AlphaSize == 0)
151 dri2_surf->visual = __DRI_IMAGE_FORMAT_XRGB8888;
152 else
153 dri2_surf->visual = __DRI_IMAGE_FORMAT_ARGB8888;
154
155 return &dri2_surf->base;
156
157 cleanup_surface:
158 free(dri2_surf);
159 return NULL;
160 }
161
162 static EGLBoolean
163 device_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
164 {
165 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
166 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
167
168 device_free_images(dri2_surf);
169
170 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
171
172 dri2_fini_surface(surf);
173 free(dri2_surf);
174 return EGL_TRUE;
175 }
176
177 static _EGLSurface *
178 dri2_device_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
179 _EGLConfig *conf, const EGLint *attrib_list)
180 {
181 return dri2_device_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
182 attrib_list);
183 }
184
185 static EGLBoolean
186 device_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
187 {
188 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
189 static const struct {
190 const char *format_name;
191 unsigned int rgba_masks[4];
192 } visuals[] = {
193 { "ARGB8888", { 0xff0000, 0xff00, 0xff, 0xff000000 } },
194 { "RGB888", { 0xff0000, 0xff00, 0xff, 0x0 } },
195 { "RGB565", { 0x00f800, 0x07e0, 0x1f, 0x0 } },
196 };
197 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
198 unsigned int config_count = 0;
199
200 for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
201 for (unsigned j = 0; j < ARRAY_SIZE(visuals); j++) {
202 struct dri2_egl_config *dri2_conf;
203
204 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
205 config_count + 1, EGL_PBUFFER_BIT, NULL,
206 visuals[j].rgba_masks);
207
208 if (dri2_conf) {
209 if (dri2_conf->base.ConfigID == config_count + 1)
210 config_count++;
211 format_count[j]++;
212 }
213 }
214 }
215
216 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
217 if (!format_count[i]) {
218 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
219 visuals[i].format_name);
220 }
221 }
222
223 return (config_count != 0);
224 }
225
226 static const struct dri2_egl_display_vtbl dri2_device_display_vtbl = {
227 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
228 .create_pbuffer_surface = dri2_device_create_pbuffer_surface,
229 .destroy_surface = device_destroy_surface,
230 .create_image = dri2_create_image_khr,
231 .swap_buffers_region = dri2_fallback_swap_buffers_region,
232 .post_sub_buffer = dri2_fallback_post_sub_buffer,
233 .copy_buffers = dri2_fallback_copy_buffers,
234 .query_buffer_age = dri2_fallback_query_buffer_age,
235 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
236 .get_sync_values = dri2_fallback_get_sync_values,
237 .get_dri_drawable = dri2_surface_get_dri_drawable,
238 };
239
240 static void
241 device_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
242 {
243 }
244
245 static const __DRIimageLoaderExtension image_loader_extension = {
246 .base = { __DRI_IMAGE_LOADER, 1 },
247 .getBuffers = device_image_get_buffers,
248 .flushFrontBuffer = device_flush_front_buffer,
249 };
250
251 static void
252 device_get_drawable_info(__DRIdrawable * draw,
253 int *x, int *y, int *w, int *h,
254 void *loaderPrivate)
255 {
256 struct dri2_egl_surface *dri2_surf = loaderPrivate;
257
258 *x = *y = 0;
259 *w = dri2_surf->base.Width;
260 *h = dri2_surf->base.Height;
261 }
262
263 /* HACK: technically we should have swrast_null, instead of these. We
264 * get away since only pbuffers are supported, thus the callbacks are
265 * unused.
266 */
267 static const __DRIswrastLoaderExtension swrast_loader_extension = {
268 .base = { __DRI_SWRAST_LOADER, 1 },
269 .getDrawableInfo = device_get_drawable_info,
270 .putImage = NULL,
271 .getImage = NULL,
272 };
273
274 static const __DRIextension *image_loader_extensions[] = {
275 &image_loader_extension.base,
276 &image_lookup_extension.base,
277 &use_invalidate.base,
278 NULL,
279 };
280
281 /* HACK: second part of the hack above. */
282 static const __DRIextension *swrast_loader_extensions[] = {
283 &swrast_loader_extension.base,
284 &image_lookup_extension.base,
285 &use_invalidate.base,
286 NULL,
287 };
288
289 static int
290 device_get_fd(_EGLDisplay *disp, _EGLDevice *dev)
291 {
292 #ifdef HAVE_LIBDRM
293 int fd = disp->Options.fd;
294 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
295 * and invalid one is 0.
296 */
297 if (fd) {
298 /* According to the spec - if the FD does not match the EGLDevice
299 * behaviour is undefined.
300 *
301 * Add a trivial sanity check since it doesn't cost us anything.
302 */
303 if (dev != _eglAddDevice(fd, false))
304 return -1;
305
306 /* No EGL_EXT_output* extensions are supported, hence no master perms
307 * are needed. Get the render one - otherwise drivers might error out.
308 */
309 char *node = drmGetRenderDeviceNameFromFd(fd);
310
311 /* Don't close the internal fd, get render node one based on it. */
312 fd = loader_open_device(node);
313 free(node);
314 return fd;
315 }
316 const char *node = _eglGetDRMDeviceRenderNode(dev);
317 return loader_open_device(node);
318 #else
319 _eglLog(_EGL_FATAL, "Driver bug: Built without libdrm, yet using a HW device");
320 return -1;
321 #endif
322 }
323
324 static bool
325 device_probe_device(_EGLDisplay *disp)
326 {
327 struct dri2_egl_display *dri2_dpy = disp->DriverData;
328
329 if (disp->Options.ForceSoftware)
330 _eglLog(_EGL_WARNING, "Not allowed to force software rendering when "
331 "API explicitly selects a hardware device.");
332 dri2_dpy->fd = device_get_fd(disp, disp->Device);
333 if (dri2_dpy->fd < 0)
334 return false;
335
336 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
337 if (!dri2_dpy->driver_name)
338 goto err_name;
339
340 if (!dri2_load_driver_dri3(disp))
341 goto err_load;
342
343 dri2_dpy->loader_extensions = image_loader_extensions;
344 return true;
345
346 err_load:
347 free(dri2_dpy->driver_name);
348 dri2_dpy->driver_name = NULL;
349
350 err_name:
351 close(dri2_dpy->fd);
352 dri2_dpy->fd = -1;
353 return false;
354
355 }
356
357 static bool
358 device_probe_device_sw(_EGLDisplay *disp)
359 {
360 struct dri2_egl_display *dri2_dpy = disp->DriverData;
361
362 dri2_dpy->fd = -1;
363 dri2_dpy->driver_name = strdup("swrast");
364 if (!dri2_dpy->driver_name)
365 return false;
366
367 /* HACK: should be driver_swrast_null */
368 if (!dri2_load_driver_swrast(disp)) {
369 free(dri2_dpy->driver_name);
370 dri2_dpy->driver_name = NULL;
371 return false;
372 }
373
374 dri2_dpy->loader_extensions = swrast_loader_extensions;
375 return true;
376 }
377
378 EGLBoolean
379 dri2_initialize_device(_EGLDriver *drv, _EGLDisplay *disp)
380 {
381 _EGLDevice *dev;
382 struct dri2_egl_display *dri2_dpy;
383 const char* err;
384
385 dri2_dpy = calloc(1, sizeof *dri2_dpy);
386 if (!dri2_dpy)
387 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
388
389 /* Extension requires a PlatformDisplay - the EGLDevice. */
390 dev = disp->PlatformDisplay;
391
392 dri2_dpy->fd = -1;
393 disp->Device = dev;
394 disp->DriverData = (void *) dri2_dpy;
395 err = "DRI2: failed to load driver";
396 if (_eglDeviceSupports(dev, _EGL_DEVICE_DRM)) {
397 if (!device_probe_device(disp))
398 goto cleanup;
399 } else if (_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE)) {
400 if (!device_probe_device_sw(disp))
401 goto cleanup;
402 } else {
403 _eglLog(_EGL_FATAL, "Driver bug: exposed device is neither DRM nor SOFTWARE one");
404 return EGL_FALSE;
405 }
406
407 if (!dri2_create_screen(disp)) {
408 err = "DRI2: failed to create screen";
409 goto cleanup;
410 }
411
412 if (!dri2_setup_extensions(disp)) {
413 err = "DRI2: failed to find required DRI extensions";
414 goto cleanup;
415 }
416
417 dri2_setup_screen(disp);
418
419 if (!device_add_configs_for_visuals(drv, disp)) {
420 err = "DRI2: failed to add configs";
421 goto cleanup;
422 }
423
424 /* Fill vtbl last to prevent accidentally calling virtual function during
425 * initialization.
426 */
427 dri2_dpy->vtbl = &dri2_device_display_vtbl;
428
429 return EGL_TRUE;
430
431 cleanup:
432 dri2_display_destroy(disp);
433 return _eglError(EGL_NOT_INITIALIZED, err);
434 }