egl: Implement getImage/putImage on pbuffer swrast.
[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 free(dri2_surf->swrast_device_buffer);
71 dri2_surf->swrast_device_buffer = NULL;
72 }
73
74 static int
75 device_image_get_buffers(__DRIdrawable *driDrawable,
76 unsigned int format,
77 uint32_t *stamp,
78 void *loaderPrivate,
79 uint32_t buffer_mask,
80 struct __DRIimageList *buffers)
81 {
82 struct dri2_egl_surface *dri2_surf = loaderPrivate;
83 struct dri2_egl_display *dri2_dpy =
84 dri2_egl_display(dri2_surf->base.Resource.Display);
85
86 buffers->image_mask = 0;
87 buffers->front = NULL;
88 buffers->back = NULL;
89
90 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
91 * the spec states that they have a back buffer but no front buffer, in
92 * contrast to pixmaps, which have a front buffer but no back buffer.
93 *
94 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
95 * from the spec, following the precedent of Mesa's EGL X11 platform. The
96 * X11 platform correctly assigns pbuffers to single-buffered configs, but
97 * assigns the pbuffer a front buffer instead of a back buffer.
98 *
99 * Pbuffers in the X11 platform mostly work today, so let's just copy its
100 * behavior instead of trying to fix (and hence potentially breaking) the
101 * world.
102 */
103
104 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
105
106 if (!dri2_surf->front)
107 dri2_surf->front =
108 device_alloc_image(dri2_dpy, dri2_surf);
109
110 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
111 buffers->front = dri2_surf->front;
112 }
113
114 return 1;
115 }
116
117 static _EGLSurface *
118 dri2_device_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
119 _EGLConfig *conf, const EGLint *attrib_list)
120 {
121 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
122 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
123 struct dri2_egl_surface *dri2_surf;
124 const __DRIconfig *config;
125
126 /* Make sure to calloc so all pointers
127 * are originally NULL.
128 */
129 dri2_surf = calloc(1, sizeof *dri2_surf);
130
131 if (!dri2_surf) {
132 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
133 return NULL;
134 }
135
136 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
137 false, NULL))
138 goto cleanup_surface;
139
140 config = dri2_get_dri_config(dri2_conf, type,
141 dri2_surf->base.GLColorspace);
142
143 if (!config) {
144 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
145 goto cleanup_surface;
146 }
147
148 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
149 goto cleanup_surface;
150
151 if (conf->RedSize == 5)
152 dri2_surf->visual = __DRI_IMAGE_FORMAT_RGB565;
153 else if (conf->AlphaSize == 0)
154 dri2_surf->visual = __DRI_IMAGE_FORMAT_XRGB8888;
155 else
156 dri2_surf->visual = __DRI_IMAGE_FORMAT_ARGB8888;
157
158 return &dri2_surf->base;
159
160 cleanup_surface:
161 free(dri2_surf);
162 return NULL;
163 }
164
165 static EGLBoolean
166 device_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
167 {
168 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
169 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
170
171 device_free_images(dri2_surf);
172
173 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
174
175 dri2_fini_surface(surf);
176 free(dri2_surf);
177 return EGL_TRUE;
178 }
179
180 static _EGLSurface *
181 dri2_device_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
182 _EGLConfig *conf, const EGLint *attrib_list)
183 {
184 return dri2_device_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
185 attrib_list);
186 }
187
188 static EGLBoolean
189 device_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
190 {
191 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
192 static const struct {
193 const char *format_name;
194 int rgba_shifts[4];
195 unsigned int rgba_sizes[4];
196 } visuals[] = {
197 { "A2RGB10", { 20, 10, 0, 30 }, { 10, 10, 10, 2 } },
198 { "X2RGB10", { 20, 10, 0, -1 }, { 10, 10, 10, 0 } },
199 { "ARGB8888", { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
200 { "RGB888", { 16, 8, 0, -1 }, { 8, 8, 8, 0 } },
201 { "RGB565", { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
202 };
203 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
204 unsigned int config_count = 0;
205
206 for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
207 for (unsigned j = 0; j < ARRAY_SIZE(visuals); j++) {
208 struct dri2_egl_config *dri2_conf;
209
210 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
211 config_count + 1, EGL_PBUFFER_BIT, NULL,
212 visuals[j].rgba_shifts, visuals[j].rgba_sizes);
213
214 if (dri2_conf) {
215 if (dri2_conf->base.ConfigID == config_count + 1)
216 config_count++;
217 format_count[j]++;
218 }
219 }
220 }
221
222 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
223 if (!format_count[i]) {
224 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
225 visuals[i].format_name);
226 }
227 }
228
229 return (config_count != 0);
230 }
231
232 static const struct dri2_egl_display_vtbl dri2_device_display_vtbl = {
233 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
234 .create_pbuffer_surface = dri2_device_create_pbuffer_surface,
235 .destroy_surface = device_destroy_surface,
236 .create_image = dri2_create_image_khr,
237 .swap_buffers_region = dri2_fallback_swap_buffers_region,
238 .post_sub_buffer = dri2_fallback_post_sub_buffer,
239 .copy_buffers = dri2_fallback_copy_buffers,
240 .query_buffer_age = dri2_fallback_query_buffer_age,
241 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
242 .get_sync_values = dri2_fallback_get_sync_values,
243 .get_dri_drawable = dri2_surface_get_dri_drawable,
244 };
245
246 static void
247 device_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
248 {
249 }
250
251 static const __DRIimageLoaderExtension image_loader_extension = {
252 .base = { __DRI_IMAGE_LOADER, 1 },
253 .getBuffers = device_image_get_buffers,
254 .flushFrontBuffer = device_flush_front_buffer,
255 };
256
257 static const __DRIextension *image_loader_extensions[] = {
258 &image_loader_extension.base,
259 &image_lookup_extension.base,
260 &use_invalidate.base,
261 NULL,
262 };
263
264 static const __DRIextension *swrast_loader_extensions[] = {
265 &swrast_pbuffer_loader_extension.base,
266 &image_lookup_extension.base,
267 &use_invalidate.base,
268 NULL,
269 };
270
271 static int
272 device_get_fd(_EGLDisplay *disp, _EGLDevice *dev)
273 {
274 #ifdef HAVE_LIBDRM
275 int fd = disp->Options.fd;
276 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
277 * and invalid one is 0.
278 */
279 if (fd) {
280 /* According to the spec - if the FD does not match the EGLDevice
281 * behaviour is undefined.
282 *
283 * Add a trivial sanity check since it doesn't cost us anything.
284 */
285 if (dev != _eglAddDevice(fd, false))
286 return -1;
287
288 /* No EGL_EXT_output* extensions are supported, hence no master perms
289 * are needed. Get the render one - otherwise drivers might error out.
290 */
291 char *node = drmGetRenderDeviceNameFromFd(fd);
292
293 /* Don't close the internal fd, get render node one based on it. */
294 fd = loader_open_device(node);
295 free(node);
296 return fd;
297 }
298 const char *node = _eglGetDRMDeviceRenderNode(dev);
299 return loader_open_device(node);
300 #else
301 _eglLog(_EGL_FATAL, "Driver bug: Built without libdrm, yet using a HW device");
302 return -1;
303 #endif
304 }
305
306 static bool
307 device_probe_device(_EGLDisplay *disp)
308 {
309 struct dri2_egl_display *dri2_dpy = disp->DriverData;
310
311 if (disp->Options.ForceSoftware)
312 _eglLog(_EGL_WARNING, "Not allowed to force software rendering when "
313 "API explicitly selects a hardware device.");
314 dri2_dpy->fd = device_get_fd(disp, disp->Device);
315 if (dri2_dpy->fd < 0)
316 return false;
317
318 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
319 if (!dri2_dpy->driver_name)
320 goto err_name;
321
322 if (!dri2_load_driver_dri3(disp))
323 goto err_load;
324
325 dri2_dpy->loader_extensions = image_loader_extensions;
326 return true;
327
328 err_load:
329 free(dri2_dpy->driver_name);
330 dri2_dpy->driver_name = NULL;
331
332 err_name:
333 close(dri2_dpy->fd);
334 dri2_dpy->fd = -1;
335 return false;
336
337 }
338
339 static bool
340 device_probe_device_sw(_EGLDisplay *disp)
341 {
342 struct dri2_egl_display *dri2_dpy = disp->DriverData;
343
344 dri2_dpy->fd = -1;
345 dri2_dpy->driver_name = strdup("swrast");
346 if (!dri2_dpy->driver_name)
347 return false;
348
349 /* HACK: should be driver_swrast_null */
350 if (!dri2_load_driver_swrast(disp)) {
351 free(dri2_dpy->driver_name);
352 dri2_dpy->driver_name = NULL;
353 return false;
354 }
355
356 dri2_dpy->loader_extensions = swrast_loader_extensions;
357 return true;
358 }
359
360 EGLBoolean
361 dri2_initialize_device(_EGLDriver *drv, _EGLDisplay *disp)
362 {
363 _EGLDevice *dev;
364 struct dri2_egl_display *dri2_dpy;
365 const char* err;
366
367 dri2_dpy = calloc(1, sizeof *dri2_dpy);
368 if (!dri2_dpy)
369 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
370
371 /* Extension requires a PlatformDisplay - the EGLDevice. */
372 dev = disp->PlatformDisplay;
373
374 dri2_dpy->fd = -1;
375 disp->Device = dev;
376 disp->DriverData = (void *) dri2_dpy;
377 err = "DRI2: failed to load driver";
378 if (_eglDeviceSupports(dev, _EGL_DEVICE_DRM)) {
379 if (!device_probe_device(disp))
380 goto cleanup;
381 } else if (_eglDeviceSupports(dev, _EGL_DEVICE_SOFTWARE)) {
382 if (!device_probe_device_sw(disp))
383 goto cleanup;
384 } else {
385 _eglLog(_EGL_FATAL, "Driver bug: exposed device is neither DRM nor SOFTWARE one");
386 return EGL_FALSE;
387 }
388
389 if (!dri2_create_screen(disp)) {
390 err = "DRI2: failed to create screen";
391 goto cleanup;
392 }
393
394 if (!dri2_setup_extensions(disp)) {
395 err = "DRI2: failed to find required DRI extensions";
396 goto cleanup;
397 }
398
399 dri2_setup_screen(disp);
400
401 if (!device_add_configs_for_visuals(drv, disp)) {
402 err = "DRI2: failed to add configs";
403 goto cleanup;
404 }
405
406 /* Fill vtbl last to prevent accidentally calling virtual function during
407 * initialization.
408 */
409 dri2_dpy->vtbl = &dri2_device_display_vtbl;
410
411 return EGL_TRUE;
412
413 cleanup:
414 dri2_display_destroy(disp);
415 return _eglError(EGL_NOT_INITIALIZED, err);
416 }