2 * Mesa 3-D graphics library
4 * Copyright 2018 Collabora
6 * Based on platform_surfaceless, which has:
8 * Copyright (c) 2014 The Chromium OS Authors.
9 * Copyright © 2011 Intel Corporation
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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.
37 #include <sys/types.h>
43 #include "egl_dri2_fallbacks.h"
47 device_alloc_image(struct dri2_egl_display
*dri2_dpy
,
48 struct dri2_egl_surface
*dri2_surf
)
50 return dri2_dpy
->image
->createImage(
52 dri2_surf
->base
.Width
,
53 dri2_surf
->base
.Height
,
60 device_free_images(struct dri2_egl_surface
*dri2_surf
)
62 struct dri2_egl_display
*dri2_dpy
=
63 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
65 if (dri2_surf
->front
) {
66 dri2_dpy
->image
->destroyImage(dri2_surf
->front
);
67 dri2_surf
->front
= NULL
;
72 device_image_get_buffers(__DRIdrawable
*driDrawable
,
77 struct __DRIimageList
*buffers
)
79 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
80 struct dri2_egl_display
*dri2_dpy
=
81 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
83 buffers
->image_mask
= 0;
84 buffers
->front
= NULL
;
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.
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.
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
101 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
103 if (!dri2_surf
->front
)
105 device_alloc_image(dri2_dpy
, dri2_surf
);
107 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
108 buffers
->front
= dri2_surf
->front
;
115 dri2_device_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
116 _EGLConfig
*conf
, const EGLint
*attrib_list
)
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
;
123 /* Make sure to calloc so all pointers
124 * are originally NULL.
126 dri2_surf
= calloc(1, sizeof *dri2_surf
);
129 _eglError(EGL_BAD_ALLOC
, "eglCreatePbufferSurface");
133 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
135 goto cleanup_surface
;
137 config
= dri2_get_dri_config(dri2_conf
, type
,
138 dri2_surf
->base
.GLColorspace
);
141 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
142 goto cleanup_surface
;
145 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
))
146 goto cleanup_surface
;
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
;
153 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_ARGB8888
;
155 return &dri2_surf
->base
;
163 device_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
165 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
166 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
168 device_free_images(dri2_surf
);
170 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
172 dri2_fini_surface(surf
);
178 dri2_device_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
179 _EGLConfig
*conf
, const EGLint
*attrib_list
)
181 return dri2_device_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
186 device_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
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];
193 { "ARGB8888", { 0xff0000, 0xff00, 0xff, 0xff000000 } },
194 { "RGB888", { 0xff0000, 0xff00, 0xff, 0x0 } },
195 { "RGB565", { 0x00f800, 0x07e0, 0x1f, 0x0 } },
197 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
198 unsigned int config_count
= 0;
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
;
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
);
209 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
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
);
223 return (config_count
!= 0);
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 .set_damage_region
= dri2_fallback_set_damage_region
,
233 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
234 .copy_buffers
= dri2_fallback_copy_buffers
,
235 .query_buffer_age
= dri2_fallback_query_buffer_age
,
236 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
237 .get_sync_values
= dri2_fallback_get_sync_values
,
238 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
242 device_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
246 static const __DRIimageLoaderExtension image_loader_extension
= {
247 .base
= { __DRI_IMAGE_LOADER
, 1 },
248 .getBuffers
= device_image_get_buffers
,
249 .flushFrontBuffer
= device_flush_front_buffer
,
253 device_get_drawable_info(__DRIdrawable
* draw
,
254 int *x
, int *y
, int *w
, int *h
,
257 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
260 *w
= dri2_surf
->base
.Width
;
261 *h
= dri2_surf
->base
.Height
;
264 /* HACK: technically we should have swrast_null, instead of these. We
265 * get away since only pbuffers are supported, thus the callbacks are
268 static const __DRIswrastLoaderExtension swrast_loader_extension
= {
269 .base
= { __DRI_SWRAST_LOADER
, 1 },
270 .getDrawableInfo
= device_get_drawable_info
,
275 static const __DRIextension
*image_loader_extensions
[] = {
276 &image_loader_extension
.base
,
277 &image_lookup_extension
.base
,
278 &use_invalidate
.base
,
282 /* HACK: second part of the hack above. */
283 static const __DRIextension
*swrast_loader_extensions
[] = {
284 &swrast_loader_extension
.base
,
285 &image_lookup_extension
.base
,
286 &use_invalidate
.base
,
291 device_get_fd(_EGLDisplay
*disp
, _EGLDevice
*dev
)
294 int fd
= disp
->Options
.fd
;
295 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
296 * and invalid one is 0.
299 /* According to the spec - if the FD does not match the EGLDevice
300 * behaviour is undefined.
302 * Add a trivial sanity check since it doesn't cost us anything.
304 if (dev
!= _eglAddDevice(fd
, false))
307 /* No EGL_EXT_output* extensions are supported, hence no master perms
308 * are needed. Get the render one - otherwise drivers might error out.
310 char *node
= drmGetRenderDeviceNameFromFd(fd
);
312 /* Don't close the internal fd, get render node one based on it. */
313 fd
= loader_open_device(node
);
317 const char *node
= _eglGetDRMDeviceRenderNode(dev
);
318 return loader_open_device(node
);
320 _eglLog(_EGL_FATAL
, "Driver bug: Built without libdrm, yet using a HW device");
326 device_probe_device(_EGLDisplay
*disp
)
328 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
330 if (disp
->Options
.ForceSoftware
)
331 _eglLog(_EGL_WARNING
, "Not allowed to force software rendering when "
332 "API explicitly selects a hardware device.");
333 dri2_dpy
->fd
= device_get_fd(disp
, disp
->Device
);
334 if (dri2_dpy
->fd
< 0)
337 dri2_dpy
->driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
338 if (!dri2_dpy
->driver_name
)
341 if (!dri2_load_driver_dri3(disp
))
344 dri2_dpy
->loader_extensions
= image_loader_extensions
;
348 free(dri2_dpy
->driver_name
);
349 dri2_dpy
->driver_name
= NULL
;
359 device_probe_device_sw(_EGLDisplay
*disp
)
361 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
364 dri2_dpy
->driver_name
= strdup("swrast");
365 if (!dri2_dpy
->driver_name
)
368 /* HACK: should be driver_swrast_null */
369 if (!dri2_load_driver_swrast(disp
)) {
370 free(dri2_dpy
->driver_name
);
371 dri2_dpy
->driver_name
= NULL
;
375 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
380 dri2_initialize_device(_EGLDriver
*drv
, _EGLDisplay
*disp
)
383 struct dri2_egl_display
*dri2_dpy
;
386 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
388 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
390 /* Extension requires a PlatformDisplay - the EGLDevice. */
391 dev
= disp
->PlatformDisplay
;
395 disp
->DriverData
= (void *) dri2_dpy
;
396 err
= "DRI2: failed to load driver";
397 if (_eglDeviceSupports(dev
, _EGL_DEVICE_DRM
)) {
398 if (!device_probe_device(disp
))
400 } else if (_eglDeviceSupports(dev
, _EGL_DEVICE_SOFTWARE
)) {
401 if (!device_probe_device_sw(disp
))
404 _eglLog(_EGL_FATAL
, "Driver bug: exposed device is neither DRM nor SOFTWARE one");
408 if (!dri2_create_screen(disp
)) {
409 err
= "DRI2: failed to create screen";
413 if (!dri2_setup_extensions(disp
)) {
414 err
= "DRI2: failed to find required DRI extensions";
418 dri2_setup_screen(disp
);
420 if (!device_add_configs_for_visuals(drv
, disp
)) {
421 err
= "DRI2: failed to add configs";
425 /* Fill vtbl last to prevent accidentally calling virtual function during
428 dri2_dpy
->vtbl
= &dri2_device_display_vtbl
;
433 dri2_display_destroy(disp
);
434 return _eglError(EGL_NOT_INITIALIZED
, err
);