egl: set the EGLDevice when creating a display
[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
40 static __DRIimage*
41 surfaceless_alloc_image(struct dri2_egl_display *dri2_dpy,
42 struct dri2_egl_surface *dri2_surf)
43 {
44 return dri2_dpy->image->createImage(
45 dri2_dpy->dri_screen,
46 dri2_surf->base.Width,
47 dri2_surf->base.Height,
48 dri2_surf->visual,
49 0,
50 NULL);
51 }
52
53 static void
54 surfaceless_free_images(struct dri2_egl_surface *dri2_surf)
55 {
56 struct dri2_egl_display *dri2_dpy =
57 dri2_egl_display(dri2_surf->base.Resource.Display);
58
59 if (dri2_surf->front) {
60 dri2_dpy->image->destroyImage(dri2_surf->front);
61 dri2_surf->front = NULL;
62 }
63 }
64
65 static int
66 surfaceless_image_get_buffers(__DRIdrawable *driDrawable,
67 unsigned int format,
68 uint32_t *stamp,
69 void *loaderPrivate,
70 uint32_t buffer_mask,
71 struct __DRIimageList *buffers)
72 {
73 struct dri2_egl_surface *dri2_surf = loaderPrivate;
74 struct dri2_egl_display *dri2_dpy =
75 dri2_egl_display(dri2_surf->base.Resource.Display);
76
77 buffers->image_mask = 0;
78 buffers->front = NULL;
79 buffers->back = NULL;
80
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.
84 *
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.
89 *
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
92 * world.
93 */
94
95 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
96
97 if (!dri2_surf->front)
98 dri2_surf->front =
99 surfaceless_alloc_image(dri2_dpy, dri2_surf);
100
101 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
102 buffers->front = dri2_surf->front;
103 }
104
105 return 1;
106 }
107
108 static _EGLSurface *
109 dri2_surfaceless_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
110 _EGLConfig *conf, const EGLint *attrib_list)
111 {
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;
116
117 /* Make sure to calloc so all pointers
118 * are originally NULL.
119 */
120 dri2_surf = calloc(1, sizeof *dri2_surf);
121
122 if (!dri2_surf) {
123 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
124 return NULL;
125 }
126
127 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, false))
128 goto cleanup_surface;
129
130 config = dri2_get_dri_config(dri2_conf, type,
131 dri2_surf->base.GLColorspace);
132
133 if (!config) {
134 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
135 goto cleanup_surface;
136 }
137
138 dri2_surf->dri_drawable =
139 dri2_dpy->image_driver->createNewDrawable(dri2_dpy->dri_screen, config,
140 dri2_surf);
141 if (dri2_surf->dri_drawable == NULL) {
142 _eglError(EGL_BAD_ALLOC, "image->createNewDrawable");
143 goto cleanup_surface;
144 }
145
146 if (conf->RedSize == 5)
147 dri2_surf->visual = __DRI_IMAGE_FORMAT_RGB565;
148 else if (conf->AlphaSize == 0)
149 dri2_surf->visual = __DRI_IMAGE_FORMAT_XRGB8888;
150 else
151 dri2_surf->visual = __DRI_IMAGE_FORMAT_ARGB8888;
152
153 return &dri2_surf->base;
154
155 cleanup_surface:
156 free(dri2_surf);
157 return NULL;
158 }
159
160 static EGLBoolean
161 surfaceless_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
162 {
163 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
164 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
165
166 surfaceless_free_images(dri2_surf);
167
168 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
169
170 dri2_fini_surface(surf);
171 free(dri2_surf);
172 return EGL_TRUE;
173 }
174
175 static _EGLSurface *
176 dri2_surfaceless_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
177 _EGLConfig *conf, const EGLint *attrib_list)
178 {
179 return dri2_surfaceless_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
180 attrib_list);
181 }
182
183 static EGLBoolean
184 surfaceless_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
185 {
186 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
187 static const struct {
188 const char *format_name;
189 unsigned int rgba_masks[4];
190 } visuals[] = {
191 { "ARGB8888", { 0xff0000, 0xff00, 0xff, 0xff000000 } },
192 { "RGB888", { 0xff0000, 0xff00, 0xff, 0x0 } },
193 { "RGB565", { 0x00f800, 0x07e0, 0x1f, 0x0 } },
194 };
195 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
196 unsigned int config_count = 0;
197
198 for (unsigned i = 0; dri2_dpy->driver_configs[i] != NULL; i++) {
199 for (unsigned j = 0; j < ARRAY_SIZE(visuals); j++) {
200 struct dri2_egl_config *dri2_conf;
201
202 dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[i],
203 config_count + 1, EGL_PBUFFER_BIT, NULL,
204 visuals[j].rgba_masks);
205
206 if (dri2_conf) {
207 if (dri2_conf->base.ConfigID == config_count + 1)
208 config_count++;
209 format_count[j]++;
210 }
211 }
212 }
213
214 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
215 if (!format_count[i]) {
216 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
217 visuals[i].format_name);
218 }
219 }
220
221 return (config_count != 0);
222 }
223
224 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
225 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
226 .create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,
227 .destroy_surface = surfaceless_destroy_surface,
228 .create_image = dri2_create_image_khr,
229 .swap_buffers_region = dri2_fallback_swap_buffers_region,
230 .set_damage_region = dri2_fallback_set_damage_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,
237 };
238
239 static void
240 surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
241 {
242 }
243
244 static const __DRIimageLoaderExtension image_loader_extension = {
245 .base = { __DRI_IMAGE_LOADER, 1 },
246 .getBuffers = surfaceless_image_get_buffers,
247 .flushFrontBuffer = surfaceless_flush_front_buffer,
248 };
249
250 static const __DRIswrastLoaderExtension swrast_loader_extension = {
251 .base = { __DRI_SWRAST_LOADER, 1 },
252 .getDrawableInfo = NULL,
253 .putImage = NULL,
254 .getImage = NULL,
255 };
256
257 #define DRM_RENDER_DEV_NAME "%s/renderD%d"
258
259 static const __DRIextension *image_loader_extensions[] = {
260 &image_loader_extension.base,
261 &image_lookup_extension.base,
262 &use_invalidate.base,
263 NULL,
264 };
265
266 static const __DRIextension *swrast_loader_extensions[] = {
267 &swrast_loader_extension.base,
268 &image_loader_extension.base,
269 &image_lookup_extension.base,
270 &use_invalidate.base,
271 NULL,
272 };
273
274 static bool
275 surfaceless_probe_device(_EGLDisplay *dpy, bool swrast)
276 {
277 struct dri2_egl_display *dri2_dpy = dpy->DriverData;
278 const int limit = 64;
279 const int base = 128;
280 int fd;
281 int i;
282
283 /* Attempt to find DRM device. */
284 for (i = 0; i < limit; ++i) {
285 char *card_path;
286 if (asprintf(&card_path, DRM_RENDER_DEV_NAME, DRM_DIR_NAME, base + i) < 0)
287 continue;
288
289 fd = loader_open_device(card_path);
290 free(card_path);
291 if (fd < 0)
292 continue;
293
294 if (swrast) {
295 dri2_dpy->driver_name = strdup("kms_swrast");
296 dri2_dpy->loader_extensions = swrast_loader_extensions;
297 } else {
298 dri2_dpy->driver_name = loader_get_driver_for_fd(fd);
299 dri2_dpy->loader_extensions = image_loader_extensions;
300 }
301 if (!dri2_dpy->driver_name) {
302 close(fd);
303 continue;
304 }
305
306 dri2_dpy->fd = fd;
307 if (dri2_load_driver_dri3(dpy)) {
308 _EGLDevice *dev = _eglAddDevice(dri2_dpy->fd, swrast);
309 if (!dev) {
310 dlclose(dri2_dpy->driver);
311 _eglLog(_EGL_WARNING, "DRI2: failed to find EGLDevice");
312 continue;
313 }
314 dpy->Device = dev;
315 return true;
316 }
317
318 close(fd);
319 dri2_dpy->fd = -1;
320 free(dri2_dpy->driver_name);
321 dri2_dpy->driver_name = NULL;
322 dri2_dpy->loader_extensions = NULL;
323 }
324
325 /* No DRM device, so attempt to fall back to software path w/o DRM. */
326 if (swrast) {
327 _eglLog(_EGL_DEBUG, "Falling back to surfaceless swrast without DRM.");
328 dri2_dpy->fd = -1;
329 dri2_dpy->driver_name = strdup("swrast");
330 if (!dri2_dpy->driver_name) {
331 return false;
332 }
333
334 if (dri2_load_driver_swrast(dpy)) {
335 dri2_dpy->loader_extensions = swrast_loader_extensions;
336 return true;
337 }
338
339 free(dri2_dpy->driver_name);
340 dri2_dpy->driver_name = NULL;
341 }
342
343 return false;
344 }
345
346 EGLBoolean
347 dri2_initialize_surfaceless(_EGLDriver *drv, _EGLDisplay *disp)
348 {
349 struct dri2_egl_display *dri2_dpy;
350 const char* err;
351 bool driver_loaded = false;
352
353 loader_set_logger(_eglLog);
354
355 dri2_dpy = calloc(1, sizeof *dri2_dpy);
356 if (!dri2_dpy)
357 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
358
359 dri2_dpy->fd = -1;
360 disp->DriverData = (void *) dri2_dpy;
361
362 if (!disp->Options.ForceSoftware) {
363 driver_loaded = surfaceless_probe_device(disp, false);
364 if (!driver_loaded)
365 _eglLog(_EGL_WARNING,
366 "No hardware driver found, falling back to software rendering");
367 }
368
369 if (!driver_loaded && !surfaceless_probe_device(disp, true)) {
370 err = "DRI2: failed to load driver";
371 goto cleanup;
372 }
373
374 if (!dri2_create_screen(disp)) {
375 err = "DRI2: failed to create screen";
376 goto cleanup;
377 }
378
379 if (!dri2_setup_extensions(disp)) {
380 err = "DRI2: failed to find required DRI extensions";
381 goto cleanup;
382 }
383
384 dri2_setup_screen(disp);
385
386 if (!surfaceless_add_configs_for_visuals(drv, disp)) {
387 err = "DRI2: failed to add configs";
388 goto cleanup;
389 }
390
391 /* Fill vtbl last to prevent accidentally calling virtual function during
392 * initialization.
393 */
394 dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
395
396 return EGL_TRUE;
397
398 cleanup:
399 dri2_display_destroy(disp);
400 return _eglError(EGL_NOT_INITIALIZED, err);
401 }