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"
39 #include "util/debug.h"
42 surfaceless_alloc_image(struct dri2_egl_display
*dri2_dpy
,
43 struct dri2_egl_surface
*dri2_surf
)
45 return dri2_dpy
->image
->createImage(
47 dri2_surf
->base
.Width
,
48 dri2_surf
->base
.Height
,
55 surfaceless_free_images(struct dri2_egl_surface
*dri2_surf
)
57 struct dri2_egl_display
*dri2_dpy
=
58 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
60 if (dri2_surf
->front
) {
61 dri2_dpy
->image
->destroyImage(dri2_surf
->front
);
62 dri2_surf
->front
= NULL
;
67 surfaceless_image_get_buffers(__DRIdrawable
*driDrawable
,
72 struct __DRIimageList
*buffers
)
74 struct dri2_egl_surface
*dri2_surf
= loaderPrivate
;
75 struct dri2_egl_display
*dri2_dpy
=
76 dri2_egl_display(dri2_surf
->base
.Resource
.Display
);
78 buffers
->image_mask
= 0;
79 buffers
->front
= NULL
;
82 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
83 * the spec states that they have a back buffer but no front buffer, in
84 * contrast to pixmaps, which have a front buffer but no back buffer.
86 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
87 * from the spec, following the precedent of Mesa's EGL X11 platform. The
88 * X11 platform correctly assigns pbuffers to single-buffered configs, but
89 * assigns the pbuffer a front buffer instead of a back buffer.
91 * Pbuffers in the X11 platform mostly work today, so let's just copy its
92 * behavior instead of trying to fix (and hence potentially breaking) the
96 if (buffer_mask
& __DRI_IMAGE_BUFFER_FRONT
) {
98 if (!dri2_surf
->front
)
100 surfaceless_alloc_image(dri2_dpy
, dri2_surf
);
102 buffers
->image_mask
|= __DRI_IMAGE_BUFFER_FRONT
;
103 buffers
->front
= dri2_surf
->front
;
110 dri2_surfaceless_create_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, EGLint type
,
111 _EGLConfig
*conf
, const EGLint
*attrib_list
)
113 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
114 struct dri2_egl_config
*dri2_conf
= dri2_egl_config(conf
);
115 struct dri2_egl_surface
*dri2_surf
;
116 const __DRIconfig
*config
;
118 /* Make sure to calloc so all pointers
119 * are originally NULL.
121 dri2_surf
= calloc(1, sizeof *dri2_surf
);
124 _eglError(EGL_BAD_ALLOC
, "eglCreatePbufferSurface");
128 if (!dri2_init_surface(&dri2_surf
->base
, disp
, type
, conf
, attrib_list
, false))
129 goto cleanup_surface
;
131 config
= dri2_get_dri_config(dri2_conf
, type
,
132 dri2_surf
->base
.GLColorspace
);
135 goto cleanup_surface
;
137 dri2_surf
->dri_drawable
=
138 dri2_dpy
->image_driver
->createNewDrawable(dri2_dpy
->dri_screen
, config
,
140 if (dri2_surf
->dri_drawable
== NULL
) {
141 _eglError(EGL_BAD_ALLOC
, "image->createNewDrawable");
142 goto cleanup_surface
;
145 if (conf
->RedSize
== 5)
146 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_RGB565
;
147 else if (conf
->AlphaSize
== 0)
148 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_XRGB8888
;
150 dri2_surf
->visual
= __DRI_IMAGE_FORMAT_ARGB8888
;
152 return &dri2_surf
->base
;
160 surfaceless_destroy_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
162 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(disp
);
163 struct dri2_egl_surface
*dri2_surf
= dri2_egl_surface(surf
);
165 surfaceless_free_images(dri2_surf
);
167 dri2_dpy
->core
->destroyDrawable(dri2_surf
->dri_drawable
);
169 dri2_fini_surface(surf
);
175 dri2_surfaceless_create_pbuffer_surface(_EGLDriver
*drv
, _EGLDisplay
*disp
,
176 _EGLConfig
*conf
, const EGLint
*attrib_list
)
178 return dri2_surfaceless_create_surface(drv
, disp
, EGL_PBUFFER_BIT
, conf
,
183 surfaceless_swap_buffers(_EGLDriver
*drv
, _EGLDisplay
*disp
, _EGLSurface
*surf
)
185 assert(!surf
|| surf
->Type
== EGL_PBUFFER_BIT
);
187 /* From the EGL 1.5 spec:
188 * If surface is a [...] pbuffer surface, eglSwapBuffers has no effect.
194 surfaceless_add_configs_for_visuals(_EGLDriver
*drv
, _EGLDisplay
*dpy
)
196 struct dri2_egl_display
*dri2_dpy
= dri2_egl_display(dpy
);
197 static const struct {
198 const char *format_name
;
199 unsigned int rgba_masks
[4];
201 { "ARGB8888", { 0xff0000, 0xff00, 0xff, 0xff000000 } },
202 { "RGB888", { 0xff0000, 0xff00, 0xff, 0x0 } },
203 { "RGB565", { 0x00f800, 0x07e0, 0x1f, 0x0 } },
205 unsigned int format_count
[ARRAY_SIZE(visuals
)] = { 0 };
206 unsigned int config_count
= 0;
208 for (unsigned i
= 0; dri2_dpy
->driver_configs
[i
] != NULL
; i
++) {
209 for (unsigned j
= 0; j
< ARRAY_SIZE(visuals
); j
++) {
210 struct dri2_egl_config
*dri2_conf
;
212 dri2_conf
= dri2_add_config(dpy
, dri2_dpy
->driver_configs
[i
],
213 config_count
+ 1, EGL_PBUFFER_BIT
, NULL
,
214 visuals
[j
].rgba_masks
);
217 if (dri2_conf
->base
.ConfigID
== config_count
+ 1)
224 for (unsigned i
= 0; i
< ARRAY_SIZE(format_count
); i
++) {
225 if (!format_count
[i
]) {
226 _eglLog(_EGL_DEBUG
, "No DRI config supports native format %s",
227 visuals
[i
].format_name
);
231 return (config_count
!= 0);
234 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl
= {
235 .create_pixmap_surface
= dri2_fallback_create_pixmap_surface
,
236 .create_pbuffer_surface
= dri2_surfaceless_create_pbuffer_surface
,
237 .destroy_surface
= surfaceless_destroy_surface
,
238 .create_image
= dri2_create_image_khr
,
239 .swap_buffers
= surfaceless_swap_buffers
,
240 .swap_buffers_with_damage
= dri2_fallback_swap_buffers_with_damage
,
241 .swap_buffers_region
= dri2_fallback_swap_buffers_region
,
242 .set_damage_region
= dri2_fallback_set_damage_region
,
243 .post_sub_buffer
= dri2_fallback_post_sub_buffer
,
244 .copy_buffers
= dri2_fallback_copy_buffers
,
245 .query_buffer_age
= dri2_fallback_query_buffer_age
,
246 .create_wayland_buffer_from_image
= dri2_fallback_create_wayland_buffer_from_image
,
247 .get_sync_values
= dri2_fallback_get_sync_values
,
248 .get_dri_drawable
= dri2_surface_get_dri_drawable
,
252 surfaceless_flush_front_buffer(__DRIdrawable
*driDrawable
, void *loaderPrivate
)
256 static const __DRIimageLoaderExtension image_loader_extension
= {
257 .base
= { __DRI_IMAGE_LOADER
, 1 },
258 .getBuffers
= surfaceless_image_get_buffers
,
259 .flushFrontBuffer
= surfaceless_flush_front_buffer
,
262 #define DRM_RENDER_DEV_NAME "%s/renderD%d"
264 static const __DRIextension
*image_loader_extensions
[] = {
265 &image_loader_extension
.base
,
266 &image_lookup_extension
.base
,
267 &use_invalidate
.base
,
272 surfaceless_probe_device(_EGLDisplay
*dpy
, bool swrast
)
274 struct dri2_egl_display
*dri2_dpy
= dpy
->DriverData
;
275 const int limit
= 64;
276 const int base
= 128;
280 for (i
= 0; i
< limit
; ++i
) {
282 if (asprintf(&card_path
, DRM_RENDER_DEV_NAME
, DRM_DIR_NAME
, base
+ i
) < 0)
285 fd
= loader_open_device(card_path
);
291 dri2_dpy
->driver_name
= strdup("kms_swrast");
293 dri2_dpy
->driver_name
= loader_get_driver_for_fd(fd
);
294 if (!dri2_dpy
->driver_name
) {
300 if (dri2_load_driver_dri3(dpy
))
305 free(dri2_dpy
->driver_name
);
306 dri2_dpy
->driver_name
= NULL
;
313 dri2_initialize_surfaceless(_EGLDriver
*drv
, _EGLDisplay
*disp
)
315 struct dri2_egl_display
*dri2_dpy
;
317 bool driver_loaded
= false;
319 loader_set_logger(_eglLog
);
321 dri2_dpy
= calloc(1, sizeof *dri2_dpy
);
323 return _eglError(EGL_BAD_ALLOC
, "eglInitialize");
326 disp
->DriverData
= (void *) dri2_dpy
;
328 if (!env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false)) {
329 driver_loaded
= surfaceless_probe_device(disp
, false);
331 _eglLog(_EGL_WARNING
,
332 "No hardware driver found, falling back to software rendering");
335 if (!driver_loaded
&& !surfaceless_probe_device(disp
, true)) {
336 err
= "DRI2: failed to load driver";
340 dri2_dpy
->loader_extensions
= image_loader_extensions
;
342 if (!dri2_create_screen(disp
)) {
343 err
= "DRI2: failed to create screen";
347 if (!dri2_setup_extensions(disp
)) {
348 err
= "DRI2: failed to find required DRI extensions";
352 dri2_setup_screen(disp
);
354 if (!surfaceless_add_configs_for_visuals(drv
, disp
)) {
355 err
= "DRI2: failed to add configs";
359 /* Fill vtbl last to prevent accidentally calling virtual function during
362 dri2_dpy
->vtbl
= &dri2_surfaceless_display_vtbl
;
367 dri2_display_destroy(disp
);
368 return _eglError(EGL_NOT_INITIALIZED
, err
);