2 * Mesa 3-D graphics library
4 * Copyright (c) 2014 The Chromium OS Authors.
5 * Copyright © 2011 Intel Corporation
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
31 #include <sys/types.h>
37 #include "egl_dri2_fallbacks.h"
41 surfaceless_alloc_image(struct dri2_egl_display
*dri2_dpy
,
42 struct dri2_egl_surface
*dri2_surf
)
44 return dri2_dpy
->image
->createImage(
46 dri2_surf
->base
.Width
,
47 dri2_surf
->base
.Height
,
54 surfaceless_free_images(struct dri2_egl_surface
*dri2_surf
)
56 struct dri2_egl_display
*dri2_dpy
=
57 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
59 if (dri2_surf
->front
) {
60 dri2_dpy
->image
->destroyImage(dri2_surf
->front
);
61 dri2_surf
->front
= NULL
;
66 surfaceless_image_get_buffers(__DRIdrawable
*driDrawable
,
71 struct __DRIimageList
*buffers
)
73 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
74 struct dri2_egl_display
*dri2_dpy
=
75 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
77 buffers
->image_mask
= 0;
78 buffers
->front
= NULL
;
81 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
82 * the spec states that they have a back buffer but no front buffer, in
83 * contrast to pixmaps, which have a front buffer but no back buffer.
85 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
86 * from the spec, following the precedent of Mesa's EGL X11 platform. The
87 * X11 platform correctly assigns pbuffers to single-buffered configs, but
88 * assigns the pbuffer a front buffer instead of a back buffer.
90 * Pbuffers in the X11 platform mostly work today, so let's just copy its
91 * behavior instead of trying to fix (and hence potentially breaking) the
95 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
97 if (!dri2_surf
->front
)
99 surfaceless_alloc_image(dri2_dpy
, dri2_surf
);
101 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
102 buffers
->front
= dri2_surf
->front
;
109 dri2_surfaceless_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
110 _EGLConfig
*conf
, const EGLint
*attrib_list
)
112 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
113 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
114 struct dri2_egl_surface
*dri2_surf
;
115 const __DRIconfig
*config
;
117 /* Make sure to calloc so all pointers
118 * are originally NULL.
120 dri2_surf
= calloc(1, sizeof *dri2_surf
);
123 _eglError(EGL_BAD_ALLOC
, "eglCreatePbufferSurface");
127 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
,
129 goto cleanup_surface
;
131 config
= dri2_get_dri_config(dri2_conf
, type
,
132 dri2_surf
->base
.GLColorspace
);
135 _eglError(EGL_BAD_MATCH
, "Unsupported surfacetype/colorspace configuration");
136 goto cleanup_surface
;
139 if (!dri2_create_drawable(dri2_dpy
, config
, dri2_surf
, dri2_surf
))
140 goto cleanup_surface
;
142 if (conf
->RedSize
== 5)
143 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_RGB565
;
144 else if (conf
->AlphaSize
== 0)
145 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_XRGB8888
;
147 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_ARGB8888
;
149 return &dri2_surf
->base
;
157 surfaceless_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
159 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
160 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
162 surfaceless_free_images(dri2_surf
);
164 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
166 dri2_fini_surface(surf
);
172 dri2_surfaceless_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
173 _EGLConfig
*conf
, const EGLint
*attrib_list
)
175 return dri2_surfaceless_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
180 surfaceless_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*disp
)
182 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
183 static const struct {
184 const char *format_name
;
186 unsigned int rgba_sizes
[4];
188 { "ABGR16F", { 0, 16, 32, 48 }, { 16, 16, 16, 16 } },
189 { "XBGR16F", { 0, 16, 32, -1 }, { 16, 16, 16, 0 } },
190 { "A2RGB10", { 20, 10, 0, 30 }, { 10, 10, 10, 2 } },
191 { "X2RGB10", { 20, 10, 0, -1 }, { 10, 10, 10, 0 } },
192 { "ARGB8888", { 16, 8, 0, 24 }, { 8, 8, 8, 8 } },
193 { "RGB888", { 16, 8, 0, -1 }, { 8, 8, 8, 0 } },
194 { "RGB565", { 11, 5, 0, -1 }, { 5, 6, 5, 0 } },
196 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
197 unsigned int config_count
= 0;
199 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
] != NULL
; i
++) {
200 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
201 struct dri2_egl_config
*dri2_conf
;
203 dri2_conf
= dri2_add_config(disp
, dri2_dpy
->driver_configs
[i
],
204 config_count
+ 1, EGL_PBUFFER_BIT
, NULL
,
205 visuals
[j
].rgba_shifts
, visuals
[j
].rgba_sizes
);
208 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
215 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
216 if (!format_count
[i
]) {
217 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
218 visuals
[i
].format_name
);
222 return (config_count
!= 0);
225 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl
= {
226 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
227 .create_pbuffer_surface
= dri2_surfaceless_create_pbuffer_surface
,
228 .destroy_surface
= surfaceless_destroy_surface
,
229 .create_image
= dri2_create_image_khr
,
230 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
231 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
232 .copy_buffers
= dri2_fallback_copy_buffers
,
233 .query_buffer_age
= dri2_fallback_query_buffer_age
,
234 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
235 .get_sync_values
= dri2_fallback_get_sync_values
,
236 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
240 surfaceless_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
245 surfaceless_get_capability(void *loaderPrivate
, enum dri_loader_cap cap
)
247 /* Note: loaderPrivate is _EGLDisplay* */
249 case DRI_LOADER_CAP_FP16
:
256 static const __DRIimageLoaderExtension image_loader_extension
= {
257 .base
= { __DRI_IMAGE_LOADER
, 2 },
258 .getBuffers
= surfaceless_image_get_buffers
,
259 .flushFrontBuffer
= surfaceless_flush_front_buffer
,
260 .getCapability
= surfaceless_get_capability
,
263 static const __DRIextension
*image_loader_extensions
[] = {
264 &image_loader_extension
.base
,
265 &image_lookup_extension
.base
,
266 &use_invalidate
.base
,
270 static const __DRIextension
*swrast_loader_extensions
[] = {
271 &swrast_pbuffer_loader_extension
.base
,
272 &image_loader_extension
.base
,
273 &image_lookup_extension
.base
,
274 &use_invalidate
.base
,
279 surfaceless_probe_device(_EGLDisplay
*disp
, bool swrast
)
281 #define MAX_DRM_DEVICES 64
282 const unsigned node_type
= swrast
? DRM_NODE_PRIMARY
: DRM_NODE_RENDER
;
283 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
284 drmDevicePtr device
, devices
[MAX_DRM_DEVICES
] = { NULL
};
287 num_devices
= drmGetDevices2(0, devices
, ARRAY_SIZE(devices
));
291 for (i
= 0; i
< num_devices
; ++i
) {
294 if (!(device
->available_nodes
& (1 << node_type
)))
297 dri2_dpy
->fd
= loader_open_device(device
->nodes
[node_type
]);
298 if (dri2_dpy
->fd
< 0)
301 disp
->Device
= _eglAddDevice(dri2_dpy
->fd
, swrast
);
308 char *driver_name
= loader_get_driver_for_fd(dri2_dpy
->fd
);
310 /* Use kms swrast only with vgem / virtio_gpu.
311 * virtio-gpu fallbacks to software rendering when 3D features
312 * are unavailable since 6c5ab, and kms_swrast is more
313 * feature complete than swrast.
316 (strcmp(driver_name
, "vgem") == 0 ||
317 strcmp(driver_name
, "virtio_gpu") == 0))
318 dri2_dpy
->driver_name
= strdup("kms_swrast");
321 /* Use the given hardware driver */
322 dri2_dpy
->driver_name
= driver_name
;
325 if (dri2_dpy
->driver_name
&& dri2_load_driver_dri3(disp
))
328 free(dri2_dpy
->driver_name
);
329 dri2_dpy
->driver_name
= NULL
;
333 drmFreeDevices(devices
, num_devices
);
335 if (i
== num_devices
)
339 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
341 dri2_dpy
->loader_extensions
= image_loader_extensions
;
347 surfaceless_probe_device_sw(_EGLDisplay
*disp
)
349 struct dri2_egl_display
*dri2_dpy
= disp
->DriverData
;
352 disp
->Device
= _eglAddDevice(dri2_dpy
->fd
, true);
353 assert(disp
->Device
);
355 dri2_dpy
->driver_name
= strdup("swrast");
356 if (!dri2_dpy
->driver_name
)
359 if (!dri2_load_driver_swrast(disp
)) {
360 free(dri2_dpy
->driver_name
);
361 dri2_dpy
->driver_name
= NULL
;
365 dri2_dpy
->loader_extensions
= swrast_loader_extensions
;
370 dri2_initialize_surfaceless(_EGLDriver
*drv
, _EGLDisplay
*disp
)
372 struct dri2_egl_display
*dri2_dpy
;
374 bool driver_loaded
= false;
376 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
378 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
381 disp
->DriverData
= (void *) dri2_dpy
;
383 if (!disp
->Options
.ForceSoftware
) {
384 driver_loaded
= surfaceless_probe_device(disp
, false);
386 _eglLog(_EGL_WARNING
,
387 "No hardware driver found, falling back to software rendering");
391 driver_loaded
= surfaceless_probe_device(disp
, true);
393 if (!driver_loaded
) {
394 _eglLog(_EGL_DEBUG
, "Falling back to surfaceless swrast without DRM.");
395 if (!surfaceless_probe_device_sw(disp
)) {
396 err
= "DRI2: failed to load driver";
401 if (!dri2_create_screen(disp
)) {
402 err
= "DRI2: failed to create screen";
406 if (!dri2_setup_extensions(disp
)) {
407 err
= "DRI2: failed to find required DRI extensions";
411 dri2_setup_screen(disp
);
412 #ifdef HAVE_WAYLAND_PLATFORM
413 dri2_dpy
->device_name
= loader_get_device_name_for_fd(dri2_dpy
->fd
);
415 dri2_set_WL_bind_wayland_display(drv
, disp
);
417 if (!surfaceless_add_configs_for_visuals(drv
, disp
)) {
418 err
= "DRI2: failed to add configs";
422 /* Fill vtbl last to prevent accidentally calling virtual function during
425 dri2_dpy
->vtbl
= &dri2_surfaceless_display_vtbl
;
430 dri2_display_destroy(disp
);
431 return _eglError(EGL_NOT_INITIALIZED
, err
);