2af4a40b1a6b87edb9d20edc66e8150f4ae03f29
[mesa.git] / src / egl / drivers / dri2 / platform_surfaceless.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (c) 2014 The Chromium OS Authors.
5 * Copyright © 2011 Intel Corporation
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
24 */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <xf86drm.h>
30 #include <dlfcn.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35
36 #include "egl_dri2.h"
37 #include "egl_dri2_fallbacks.h"
38 #include "loader.h"
39 #include "util/debug.h"
40
41 static __DRIimage*
42 surfaceless_alloc_image(struct dri2_egl_display *dri2_dpy,
43 struct dri2_egl_surface *dri2_surf)
44 {
45 return dri2_dpy->image->createImage(
46 dri2_dpy->dri_screen,
47 dri2_surf->base.Width,
48 dri2_surf->base.Height,
49 dri2_surf->visual,
50 0,
51 NULL);
52 }
53
54 static void
55 surfaceless_free_images(struct dri2_egl_surface *dri2_surf)
56 {
57 struct dri2_egl_display *dri2_dpy =
58 dri2_egl_display(dri2_surf->base.Resource.Display);
59
60 if (dri2_surf->front) {
61 dri2_dpy->image->destroyImage(dri2_surf->front);
62 dri2_surf->front = NULL;
63 }
64 }
65
66 static int
67 surfaceless_image_get_buffers(__DRIdrawable *driDrawable,
68 unsigned int format,
69 uint32_t *stamp,
70 void *loaderPrivate,
71 uint32_t buffer_mask,
72 struct __DRIimageList *buffers)
73 {
74 struct dri2_egl_surface *dri2_surf = loaderPrivate;
75 struct dri2_egl_display *dri2_dpy =
76 dri2_egl_display(dri2_surf->base.Resource.Display);
77
78 buffers->image_mask = 0;
79 buffers->front = NULL;
80 buffers->back = NULL;
81
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.
85 *
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.
90 *
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
93 * world.
94 */
95
96 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
97
98 if (!dri2_surf->front)
99 dri2_surf->front =
100 surfaceless_alloc_image(dri2_dpy, dri2_surf);
101
102 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
103 buffers->front = dri2_surf->front;
104 }
105
106 return 1;
107 }
108
109 static _EGLSurface *
110 dri2_surfaceless_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
111 _EGLConfig *conf, const EGLint *attrib_list)
112 {
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;
117
118 /* Make sure to calloc so all pointers
119 * are originally NULL.
120 */
121 dri2_surf = calloc(1, sizeof *dri2_surf);
122
123 if (!dri2_surf) {
124 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
125 return NULL;
126 }
127
128 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, false))
129 goto cleanup_surface;
130
131 config = dri2_get_dri_config(dri2_conf, type,
132 dri2_surf->base.GLColorspace);
133
134 if (!config)
135 goto cleanup_surface;
136
137 dri2_surf->dri_drawable =
138 dri2_dpy->image_driver->createNewDrawable(dri2_dpy->dri_screen, config,
139 dri2_surf);
140 if (dri2_surf->dri_drawable == NULL) {
141 _eglError(EGL_BAD_ALLOC, "image->createNewDrawable");
142 goto cleanup_surface;
143 }
144
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;
149 else
150 dri2_surf->visual = __DRI_IMAGE_FORMAT_ARGB8888;
151
152 return &dri2_surf->base;
153
154 cleanup_surface:
155 free(dri2_surf);
156 return NULL;
157 }
158
159 static EGLBoolean
160 surfaceless_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
161 {
162 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
163 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
164
165 surfaceless_free_images(dri2_surf);
166
167 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
168
169 dri2_fini_surface(surf);
170 free(dri2_surf);
171 return EGL_TRUE;
172 }
173
174 static _EGLSurface *
175 dri2_surfaceless_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
176 _EGLConfig *conf, const EGLint *attrib_list)
177 {
178 return dri2_surfaceless_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
179 attrib_list);
180 }
181
182 static EGLBoolean
183 surfaceless_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
184 {
185 assert(!surf || surf->Type == EGL_PBUFFER_BIT);
186
187 /* From the EGL 1.5 spec:
188 * If surface is a [...] pbuffer surface, eglSwapBuffers has no effect.
189 */
190 return EGL_TRUE;
191 }
192
193 static EGLBoolean
194 surfaceless_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
195 {
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];
200 } visuals[] = {
201 { "ARGB8888", { 0xff0000, 0xff00, 0xff, 0xff000000 } },
202 { "RGB888", { 0xff0000, 0xff00, 0xff, 0x0 } },
203 { "RGB565", { 0x00f800, 0x07e0, 0x1f, 0x0 } },
204 };
205 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
206 unsigned int config_count = 0;
207
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;
211
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);
215
216 if (dri2_conf) {
217 if (dri2_conf->base.ConfigID == config_count + 1)
218 config_count++;
219 format_count[j]++;
220 }
221 }
222 }
223
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);
228 }
229 }
230
231 return (config_count != 0);
232 }
233
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,
249 };
250
251 static void
252 surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
253 {
254 }
255
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,
260 };
261
262 #define DRM_RENDER_DEV_NAME "%s/renderD%d"
263
264 static const __DRIextension *image_loader_extensions[] = {
265 &image_loader_extension.base,
266 &image_lookup_extension.base,
267 &use_invalidate.base,
268 NULL,
269 };
270
271 static bool
272 surfaceless_probe_device(_EGLDisplay *dpy, bool swrast)
273 {
274 struct dri2_egl_display *dri2_dpy = dpy->DriverData;
275 const int limit = 64;
276 const int base = 128;
277 int fd;
278 int i;
279
280 for (i = 0; i < limit; ++i) {
281 char *card_path;
282 if (asprintf(&card_path, DRM_RENDER_DEV_NAME, DRM_DIR_NAME, base + i) < 0)
283 continue;
284
285 fd = loader_open_device(card_path);
286 free(card_path);
287 if (fd < 0)
288 continue;
289
290 if (swrast)
291 dri2_dpy->driver_name = strdup("kms_swrast");
292 else
293 dri2_dpy->driver_name = loader_get_driver_for_fd(fd);
294 if (!dri2_dpy->driver_name) {
295 close(fd);
296 continue;
297 }
298
299 dri2_dpy->fd = fd;
300 if (dri2_load_driver_dri3(dpy))
301 return true;
302
303 close(fd);
304 dri2_dpy->fd = -1;
305 free(dri2_dpy->driver_name);
306 dri2_dpy->driver_name = NULL;
307 }
308
309 return false;
310 }
311
312 EGLBoolean
313 dri2_initialize_surfaceless(_EGLDriver *drv, _EGLDisplay *disp)
314 {
315 struct dri2_egl_display *dri2_dpy;
316 const char* err;
317 bool driver_loaded = false;
318
319 loader_set_logger(_eglLog);
320
321 dri2_dpy = calloc(1, sizeof *dri2_dpy);
322 if (!dri2_dpy)
323 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
324
325 dri2_dpy->fd = -1;
326 disp->DriverData = (void *) dri2_dpy;
327
328 if (!env_var_as_boolean("LIBGL_ALWAYS_SOFTWARE", false)) {
329 driver_loaded = surfaceless_probe_device(disp, false);
330 if (!driver_loaded)
331 _eglLog(_EGL_WARNING,
332 "No hardware driver found, falling back to software rendering");
333 }
334
335 if (!driver_loaded && !surfaceless_probe_device(disp, true)) {
336 err = "DRI2: failed to load driver";
337 goto cleanup;
338 }
339
340 dri2_dpy->loader_extensions = image_loader_extensions;
341
342 if (!dri2_create_screen(disp)) {
343 err = "DRI2: failed to create screen";
344 goto cleanup;
345 }
346
347 if (!dri2_setup_extensions(disp)) {
348 err = "DRI2: failed to find required DRI extensions";
349 goto cleanup;
350 }
351
352 dri2_setup_screen(disp);
353
354 if (!surfaceless_add_configs_for_visuals(drv, disp)) {
355 err = "DRI2: failed to add configs";
356 goto cleanup;
357 }
358
359 /* Fill vtbl last to prevent accidentally calling virtual function during
360 * initialization.
361 */
362 dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
363
364 return EGL_TRUE;
365
366 cleanup:
367 dri2_display_destroy(disp);
368 return _eglError(EGL_NOT_INITIALIZED, err);
369 }