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 <dlfcn.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <xf86drm.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35
36 #include "egl_dri2.h"
37 #include "eglglobals.h"
38 #include "kopper_interface.h"
39 #include "loader.h"
40 #include "loader_dri_helper.h"
41
42 static __DRIimage *
surfaceless_alloc_image(struct dri2_egl_display * dri2_dpy,struct dri2_egl_surface * dri2_surf)43 surfaceless_alloc_image(struct dri2_egl_display *dri2_dpy,
44 struct dri2_egl_surface *dri2_surf)
45 {
46 return dri2_dpy->image->createImage(
47 dri2_dpy->dri_screen_render_gpu, dri2_surf->base.Width,
48 dri2_surf->base.Height, dri2_surf->visual, 0, NULL);
49 }
50
51 static void
surfaceless_free_images(struct dri2_egl_surface * dri2_surf)52 surfaceless_free_images(struct dri2_egl_surface *dri2_surf)
53 {
54 struct dri2_egl_display *dri2_dpy =
55 dri2_egl_display(dri2_surf->base.Resource.Display);
56
57 if (dri2_surf->front) {
58 dri2_dpy->image->destroyImage(dri2_surf->front);
59 dri2_surf->front = NULL;
60 }
61
62 free(dri2_surf->swrast_device_buffer);
63 dri2_surf->swrast_device_buffer = NULL;
64 }
65
66 static int
surfaceless_image_get_buffers(__DRIdrawable * driDrawable,unsigned int format,uint32_t * stamp,void * loaderPrivate,uint32_t buffer_mask,struct __DRIimageList * buffers)67 surfaceless_image_get_buffers(__DRIdrawable *driDrawable, unsigned int format,
68 uint32_t *stamp, void *loaderPrivate,
69 uint32_t buffer_mask,
70 struct __DRIimageList *buffers)
71 {
72 struct dri2_egl_surface *dri2_surf = loaderPrivate;
73 struct dri2_egl_display *dri2_dpy =
74 dri2_egl_display(dri2_surf->base.Resource.Display);
75
76 buffers->image_mask = 0;
77 buffers->front = NULL;
78 buffers->back = NULL;
79
80 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
81 * the spec states that they have a back buffer but no front buffer, in
82 * contrast to pixmaps, which have a front buffer but no back buffer.
83 *
84 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
85 * from the spec, following the precedent of Mesa's EGL X11 platform. The
86 * X11 platform correctly assigns pbuffers to single-buffered configs, but
87 * assigns the pbuffer a front buffer instead of a back buffer.
88 *
89 * Pbuffers in the X11 platform mostly work today, so let's just copy its
90 * behavior instead of trying to fix (and hence potentially breaking) the
91 * world.
92 */
93
94 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
95
96 if (!dri2_surf->front)
97 dri2_surf->front = surfaceless_alloc_image(dri2_dpy, dri2_surf);
98
99 buffers->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
100 buffers->front = dri2_surf->front;
101 }
102
103 return 1;
104 }
105
106 static _EGLSurface *
dri2_surfaceless_create_surface(_EGLDisplay * disp,EGLint type,_EGLConfig * conf,const EGLint * attrib_list)107 dri2_surfaceless_create_surface(_EGLDisplay *disp, EGLint type,
108 _EGLConfig *conf, const EGLint *attrib_list)
109 {
110 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
111 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
112 struct dri2_egl_surface *dri2_surf;
113 const __DRIconfig *config;
114
115 /* Make sure to calloc so all pointers
116 * are originally NULL.
117 */
118 dri2_surf = calloc(1, sizeof *dri2_surf);
119
120 if (!dri2_surf) {
121 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
122 return NULL;
123 }
124
125 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list,
126 false, NULL))
127 goto cleanup_surface;
128
129 config = dri2_get_dri_config(dri2_conf, type, dri2_surf->base.GLColorspace);
130
131 if (!config) {
132 _eglError(EGL_BAD_MATCH,
133 "Unsupported surfacetype/colorspace configuration");
134 goto cleanup_surface;
135 }
136
137 dri2_surf->visual = dri2_image_format_for_pbuffer_config(dri2_dpy, config);
138 if (dri2_surf->visual == PIPE_FORMAT_NONE)
139 goto cleanup_surface;
140
141 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
142 goto cleanup_surface;
143
144 return &dri2_surf->base;
145
146 cleanup_surface:
147 free(dri2_surf);
148 return NULL;
149 }
150
151 static EGLBoolean
surfaceless_destroy_surface(_EGLDisplay * disp,_EGLSurface * surf)152 surfaceless_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
153 {
154 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
155 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
156
157 surfaceless_free_images(dri2_surf);
158
159 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
160
161 dri2_fini_surface(surf);
162 free(dri2_surf);
163 return EGL_TRUE;
164 }
165
166 static _EGLSurface *
dri2_surfaceless_create_pbuffer_surface(_EGLDisplay * disp,_EGLConfig * conf,const EGLint * attrib_list)167 dri2_surfaceless_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
168 const EGLint *attrib_list)
169 {
170 return dri2_surfaceless_create_surface(disp, EGL_PBUFFER_BIT, conf,
171 attrib_list);
172 }
173
174 static const struct dri2_egl_display_vtbl dri2_surfaceless_display_vtbl = {
175 .create_pbuffer_surface = dri2_surfaceless_create_pbuffer_surface,
176 .destroy_surface = surfaceless_destroy_surface,
177 .create_image = dri2_create_image_khr,
178 .get_dri_drawable = dri2_surface_get_dri_drawable,
179 };
180
181 static void
surfaceless_flush_front_buffer(__DRIdrawable * driDrawable,void * loaderPrivate)182 surfaceless_flush_front_buffer(__DRIdrawable *driDrawable, void *loaderPrivate)
183 {
184 }
185
186 static unsigned
surfaceless_get_capability(void * loaderPrivate,enum dri_loader_cap cap)187 surfaceless_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
188 {
189 /* Note: loaderPrivate is _EGLDisplay* */
190 switch (cap) {
191 case DRI_LOADER_CAP_FP16:
192 return 1;
193 default:
194 return 0;
195 }
196 }
197
198 static const __DRIkopperLoaderExtension kopper_loader_extension = {
199 .base = {__DRI_KOPPER_LOADER, 1},
200
201 .SetSurfaceCreateInfo = NULL,
202 };
203
204 static const __DRIimageLoaderExtension image_loader_extension = {
205 .base = {__DRI_IMAGE_LOADER, 2},
206 .getBuffers = surfaceless_image_get_buffers,
207 .flushFrontBuffer = surfaceless_flush_front_buffer,
208 .getCapability = surfaceless_get_capability,
209 };
210
211 static const __DRIextension *image_loader_extensions[] = {
212 &image_loader_extension.base, &image_lookup_extension.base,
213 &use_invalidate.base, &background_callable_extension.base,
214 &kopper_loader_extension.base, NULL,
215 };
216
217 static const __DRIextension *swrast_loader_extensions[] = {
218 &swrast_pbuffer_loader_extension.base, &image_loader_extension.base,
219 &image_lookup_extension.base, &use_invalidate.base,
220 &kopper_loader_extension.base, NULL,
221 };
222
223 static bool
surfaceless_probe_device(_EGLDisplay * disp,bool swrast,bool zink)224 surfaceless_probe_device(_EGLDisplay *disp, bool swrast, bool zink)
225 {
226 const unsigned node_type = swrast ? DRM_NODE_PRIMARY : DRM_NODE_RENDER;
227 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
228 _EGLDevice *dev_list = _eglGlobal.DeviceList;
229 drmDevicePtr device;
230
231 while (dev_list) {
232 if (!_eglDeviceSupports(dev_list, _EGL_DEVICE_DRM))
233 goto next;
234
235 if (_eglHasAttrib(disp, EGL_DEVICE_EXT) && dev_list != disp->Device) {
236 goto next;
237 }
238
239 device = _eglDeviceDrm(dev_list);
240 assert(device);
241
242 if (!(device->available_nodes & (1 << node_type)))
243 goto next;
244
245 dri2_dpy->fd_render_gpu = loader_open_device(device->nodes[node_type]);
246 if (dri2_dpy->fd_render_gpu < 0)
247 goto next;
248
249 disp->Device = dev_list;
250
251 char *driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu);
252 if (swrast) {
253 /* Use kms swrast only with vgem / virtio_gpu.
254 * virtio-gpu fallbacks to software rendering when 3D features
255 * are unavailable since 6c5ab, and kms_swrast is more
256 * feature complete than swrast.
257 */
258 if (driver_name && (strcmp(driver_name, "vgem") == 0 ||
259 strcmp(driver_name, "virtio_gpu") == 0))
260 dri2_dpy->driver_name = strdup("kms_swrast");
261 free(driver_name);
262 } else {
263 /* Use the given hardware driver */
264 dri2_dpy->driver_name = driver_name;
265 }
266
267 if (dri2_dpy->driver_name && dri2_load_driver_dri3(disp)) {
268 if (swrast || zink)
269 dri2_dpy->loader_extensions = swrast_loader_extensions;
270 else
271 dri2_dpy->loader_extensions = image_loader_extensions;
272 break;
273 }
274
275 free(dri2_dpy->driver_name);
276 dri2_dpy->driver_name = NULL;
277 close(dri2_dpy->fd_render_gpu);
278 dri2_dpy->fd_render_gpu = -1;
279
280 next:
281 dev_list = _eglDeviceNext(dev_list);
282 }
283
284 if (!dev_list)
285 return false;
286
287 return true;
288 }
289
290 static bool
surfaceless_probe_device_sw(_EGLDisplay * disp)291 surfaceless_probe_device_sw(_EGLDisplay *disp)
292 {
293 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
294 struct _egl_device *device = _eglFindDevice(dri2_dpy->fd_render_gpu, true);
295
296 dri2_dpy->fd_render_gpu = -1;
297
298 if (_eglHasAttrib(disp, EGL_DEVICE_EXT) && disp->Device != device) {
299 return false;
300 }
301
302 disp->Device = device;
303 assert(disp->Device);
304
305 dri2_dpy->driver_name = strdup(disp->Options.Zink ? "zink" : "swrast");
306 if (!dri2_dpy->driver_name)
307 return false;
308
309 if (!dri2_load_driver_swrast(disp)) {
310 free(dri2_dpy->driver_name);
311 dri2_dpy->driver_name = NULL;
312 return false;
313 }
314
315 dri2_dpy->loader_extensions = swrast_loader_extensions;
316 return true;
317 }
318
319 EGLBoolean
dri2_initialize_surfaceless(_EGLDisplay * disp)320 dri2_initialize_surfaceless(_EGLDisplay *disp)
321 {
322 const char *err;
323 bool driver_loaded = false;
324 struct dri2_egl_display *dri2_dpy = dri2_display_create();
325 if (!dri2_dpy)
326 return EGL_FALSE;
327
328 disp->DriverData = (void *)dri2_dpy;
329
330 /* When ForceSoftware is false, we try the HW driver. When ForceSoftware
331 * is true, we try kms_swrast and swrast in order.
332 */
333 driver_loaded = surfaceless_probe_device(disp, disp->Options.ForceSoftware,
334 disp->Options.Zink);
335 if (!driver_loaded && disp->Options.ForceSoftware) {
336 _eglLog(_EGL_DEBUG, "Falling back to surfaceless swrast without DRM.");
337 driver_loaded = surfaceless_probe_device_sw(disp);
338 }
339
340 if (!driver_loaded) {
341 err = "DRI2: failed to load driver";
342 goto cleanup;
343 }
344
345 dri2_dpy->fd_display_gpu = dri2_dpy->fd_render_gpu;
346
347 if (!dri2_create_screen(disp)) {
348 err = "DRI2: failed to create screen";
349 goto cleanup;
350 }
351
352 if (!dri2_setup_extensions(disp)) {
353 err = "DRI2: failed to find required DRI extensions";
354 goto cleanup;
355 }
356
357 dri2_setup_screen(disp);
358 #ifdef HAVE_WAYLAND_PLATFORM
359 dri2_dpy->device_name =
360 loader_get_device_name_for_fd(dri2_dpy->fd_render_gpu);
361 #endif
362 dri2_set_WL_bind_wayland_display(disp);
363
364 dri2_add_pbuffer_configs_for_visuals(disp);
365
366 /* Fill vtbl last to prevent accidentally calling virtual function during
367 * initialization.
368 */
369 dri2_dpy->vtbl = &dri2_surfaceless_display_vtbl;
370
371 return EGL_TRUE;
372
373 cleanup:
374 dri2_display_destroy(disp);
375 return _eglError(EGL_NOT_INITIALIZED, err);
376 }
377