blob: e978b08879d51a1ccb548ec74fba2c47a85804a4 [file] [log] [blame]
Daniel Stone7580b3c2019-06-18 11:16:53 +01001/*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
4 * Copyright © 2017, 2018 Collabora, Ltd.
5 * Copyright © 2017, 2018 General Electric Company
6 * Copyright (c) 2018 DisplayLink (UK) Ltd.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial
18 * portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30#include "config.h"
31
32#include <stdint.h>
33
34#include <xf86drm.h>
35#include <xf86drmMode.h>
36#include <drm_fourcc.h>
37
Daniel Stone7580b3c2019-06-18 11:16:53 +010038#include <libweston/libweston.h>
39#include <libweston/backend-drm.h>
40#include <libweston/pixel-formats.h>
41#include <libweston/linux-dmabuf.h>
42#include "shared/helpers.h"
43#include "drm-internal.h"
44#include "linux-dmabuf.h"
45
46static void
47drm_fb_destroy(struct drm_fb *fb)
48{
49 if (fb->fb_id != 0)
50 drmModeRmFB(fb->fd, fb->fb_id);
51 weston_buffer_reference(&fb->buffer_ref, NULL);
52 weston_buffer_release_reference(&fb->buffer_release_ref, NULL);
53 free(fb);
54}
55
56static void
57drm_fb_destroy_dumb(struct drm_fb *fb)
58{
59 struct drm_mode_destroy_dumb destroy_arg;
60
61 assert(fb->type == BUFFER_PIXMAN_DUMB);
62
63 if (fb->map && fb->size > 0)
64 munmap(fb->map, fb->size);
65
66 memset(&destroy_arg, 0, sizeof(destroy_arg));
67 destroy_arg.handle = fb->handles[0];
68 drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
69
70 drm_fb_destroy(fb);
71}
72
Daniel Stone7580b3c2019-06-18 11:16:53 +010073static int
74drm_fb_addfb(struct drm_backend *b, struct drm_fb *fb)
75{
76 int ret = -EINVAL;
77#ifdef HAVE_DRM_ADDFB2_MODIFIERS
78 uint64_t mods[4] = { };
79 size_t i;
80#endif
81
82 /* If we have a modifier set, we must only use the WithModifiers
83 * entrypoint; we cannot import it through legacy ioctls. */
84 if (b->fb_modifiers && fb->modifier != DRM_FORMAT_MOD_INVALID) {
85 /* KMS demands that if a modifier is set, it must be the same
86 * for all planes. */
87#ifdef HAVE_DRM_ADDFB2_MODIFIERS
88 for (i = 0; i < ARRAY_LENGTH(mods) && fb->handles[i]; i++)
89 mods[i] = fb->modifier;
90 ret = drmModeAddFB2WithModifiers(fb->fd, fb->width, fb->height,
91 fb->format->format,
92 fb->handles, fb->strides,
93 fb->offsets, mods, &fb->fb_id,
94 DRM_MODE_FB_MODIFIERS);
95#endif
96 return ret;
97 }
98
99 ret = drmModeAddFB2(fb->fd, fb->width, fb->height, fb->format->format,
100 fb->handles, fb->strides, fb->offsets, &fb->fb_id,
101 0);
102 if (ret == 0)
103 return 0;
104
105 /* Legacy AddFB can't always infer the format from depth/bpp alone, so
106 * check if our format is one of the lucky ones. */
107 if (!fb->format->depth || !fb->format->bpp)
108 return ret;
109
110 /* Cannot fall back to AddFB for multi-planar formats either. */
111 if (fb->handles[1] || fb->handles[2] || fb->handles[3])
112 return ret;
113
114 ret = drmModeAddFB(fb->fd, fb->width, fb->height,
115 fb->format->depth, fb->format->bpp,
116 fb->strides[0], fb->handles[0], &fb->fb_id);
117 return ret;
118}
119
120struct drm_fb *
121drm_fb_create_dumb(struct drm_backend *b, int width, int height,
122 uint32_t format)
123{
124 struct drm_fb *fb;
125 int ret;
126
127 struct drm_mode_create_dumb create_arg;
128 struct drm_mode_destroy_dumb destroy_arg;
129 struct drm_mode_map_dumb map_arg;
130
131 fb = zalloc(sizeof *fb);
132 if (!fb)
133 return NULL;
134 fb->refcnt = 1;
135
136 fb->format = pixel_format_get_info(format);
137 if (!fb->format) {
138 weston_log("failed to look up format 0x%lx\n",
139 (unsigned long) format);
140 goto err_fb;
141 }
142
143 if (!fb->format->depth || !fb->format->bpp) {
144 weston_log("format 0x%lx is not compatible with dumb buffers\n",
145 (unsigned long) format);
146 goto err_fb;
147 }
148
149 memset(&create_arg, 0, sizeof create_arg);
150 create_arg.bpp = fb->format->bpp;
151 create_arg.width = width;
152 create_arg.height = height;
153
154 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
155 if (ret)
156 goto err_fb;
157
158 fb->type = BUFFER_PIXMAN_DUMB;
159 fb->modifier = DRM_FORMAT_MOD_INVALID;
160 fb->handles[0] = create_arg.handle;
161 fb->strides[0] = create_arg.pitch;
162 fb->num_planes = 1;
163 fb->size = create_arg.size;
164 fb->width = width;
165 fb->height = height;
166 fb->fd = b->drm.fd;
167
168 if (drm_fb_addfb(b, fb) != 0) {
169 weston_log("failed to create kms fb: %s\n", strerror(errno));
170 goto err_bo;
171 }
172
173 memset(&map_arg, 0, sizeof map_arg);
174 map_arg.handle = fb->handles[0];
175 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
176 if (ret)
177 goto err_add_fb;
178
179 fb->map = mmap(NULL, fb->size, PROT_WRITE,
180 MAP_SHARED, b->drm.fd, map_arg.offset);
181 if (fb->map == MAP_FAILED)
182 goto err_add_fb;
183
184 return fb;
185
186err_add_fb:
187 drmModeRmFB(b->drm.fd, fb->fb_id);
188err_bo:
189 memset(&destroy_arg, 0, sizeof(destroy_arg));
190 destroy_arg.handle = create_arg.handle;
191 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
192err_fb:
193 free(fb);
194 return NULL;
195}
196
197struct drm_fb *
198drm_fb_ref(struct drm_fb *fb)
199{
200 fb->refcnt++;
201 return fb;
202}
203
Stefan Agnerccf24072019-07-09 22:02:00 +0200204#ifdef BUILD_DRM_GBM
205static void
206drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
207{
208 struct drm_fb *fb = data;
209
210 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
211 fb->type == BUFFER_CURSOR);
212 drm_fb_destroy(fb);
213}
214
Daniel Stone7580b3c2019-06-18 11:16:53 +0100215static void
216drm_fb_destroy_dmabuf(struct drm_fb *fb)
217{
218 /* We deliberately do not close the GEM handles here; GBM manages
219 * their lifetime through the BO. */
220 if (fb->bo)
221 gbm_bo_destroy(fb->bo);
222 drm_fb_destroy(fb);
223}
224
225static struct drm_fb *
226drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf,
227 struct drm_backend *backend, bool is_opaque)
228{
Daniel Stone7580b3c2019-06-18 11:16:53 +0100229 struct drm_fb *fb;
230 struct gbm_import_fd_data import_legacy = {
231 .width = dmabuf->attributes.width,
232 .height = dmabuf->attributes.height,
233 .format = dmabuf->attributes.format,
234 .stride = dmabuf->attributes.stride[0],
235 .fd = dmabuf->attributes.fd[0],
236 };
Nicholas Niro7aab7462019-10-29 22:13:09 -0400237#ifdef HAVE_GBM_FD_IMPORT
Daniel Stone7580b3c2019-06-18 11:16:53 +0100238 struct gbm_import_fd_modifier_data import_mod = {
239 .width = dmabuf->attributes.width,
240 .height = dmabuf->attributes.height,
241 .format = dmabuf->attributes.format,
242 .num_fds = dmabuf->attributes.n_planes,
243 .modifier = dmabuf->attributes.modifier[0],
244 };
Nicholas Niro7aab7462019-10-29 22:13:09 -0400245#endif /* HAVE_GBM_FD_IMPORT */
246
Daniel Stone7580b3c2019-06-18 11:16:53 +0100247 int i;
248
249 /* XXX: TODO:
250 *
251 * Currently the buffer is rejected if any dmabuf attribute
252 * flag is set. This keeps us from passing an inverted /
253 * interlaced / bottom-first buffer (or any other type that may
254 * be added in the future) through to an overlay. Ultimately,
255 * these types of buffers should be handled through buffer
256 * transforms and not as spot-checks requiring specific
257 * knowledge. */
258 if (dmabuf->attributes.flags)
259 return NULL;
260
261 fb = zalloc(sizeof *fb);
262 if (fb == NULL)
263 return NULL;
264
265 fb->refcnt = 1;
266 fb->type = BUFFER_DMABUF;
267
Nicholas Niro7aab7462019-10-29 22:13:09 -0400268#ifdef HAVE_GBM_FD_IMPORT
Daniel Stone7580b3c2019-06-18 11:16:53 +0100269 static_assert(ARRAY_LENGTH(import_mod.fds) ==
270 ARRAY_LENGTH(dmabuf->attributes.fd),
271 "GBM and linux_dmabuf FD size must match");
272 static_assert(sizeof(import_mod.fds) == sizeof(dmabuf->attributes.fd),
273 "GBM and linux_dmabuf FD size must match");
274 memcpy(import_mod.fds, dmabuf->attributes.fd, sizeof(import_mod.fds));
275
276 static_assert(ARRAY_LENGTH(import_mod.strides) ==
277 ARRAY_LENGTH(dmabuf->attributes.stride),
278 "GBM and linux_dmabuf stride size must match");
279 static_assert(sizeof(import_mod.strides) ==
280 sizeof(dmabuf->attributes.stride),
281 "GBM and linux_dmabuf stride size must match");
282 memcpy(import_mod.strides, dmabuf->attributes.stride,
283 sizeof(import_mod.strides));
284
285 static_assert(ARRAY_LENGTH(import_mod.offsets) ==
286 ARRAY_LENGTH(dmabuf->attributes.offset),
287 "GBM and linux_dmabuf offset size must match");
288 static_assert(sizeof(import_mod.offsets) ==
289 sizeof(dmabuf->attributes.offset),
290 "GBM and linux_dmabuf offset size must match");
291 memcpy(import_mod.offsets, dmabuf->attributes.offset,
292 sizeof(import_mod.offsets));
Nicholas Niro7aab7462019-10-29 22:13:09 -0400293#endif /* NOT HAVE_GBM_FD_IMPORT */
Daniel Stone7580b3c2019-06-18 11:16:53 +0100294
295 /* The legacy FD-import path does not allow us to supply modifiers,
296 * multiple planes, or buffer offsets. */
297 if (dmabuf->attributes.modifier[0] != DRM_FORMAT_MOD_INVALID ||
Nicholas Niro7aab7462019-10-29 22:13:09 -0400298 dmabuf->attributes.n_planes > 1 ||
299 dmabuf->attributes.offset[0] > 0) {
300#ifdef HAVE_GBM_FD_IMPORT
Daniel Stone7580b3c2019-06-18 11:16:53 +0100301 fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER,
302 &import_mod,
303 GBM_BO_USE_SCANOUT);
Nicholas Niro7aab7462019-10-29 22:13:09 -0400304#else /* NOT HAVE_GBM_FD_IMPORT */
305 drm_debug(backend, "\t\t\t[dmabuf] Unsupported use of modifiers.\n");
306 goto err_free;
307#endif /* NOT HAVE_GBM_FD_IMPORT */
Daniel Stone7580b3c2019-06-18 11:16:53 +0100308 } else {
309 fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD,
310 &import_legacy,
311 GBM_BO_USE_SCANOUT);
312 }
313
314 if (!fb->bo)
315 goto err_free;
316
317 fb->width = dmabuf->attributes.width;
318 fb->height = dmabuf->attributes.height;
319 fb->modifier = dmabuf->attributes.modifier[0];
320 fb->size = 0;
321 fb->fd = backend->drm.fd;
322
323 static_assert(ARRAY_LENGTH(fb->strides) ==
324 ARRAY_LENGTH(dmabuf->attributes.stride),
325 "drm_fb and dmabuf stride size must match");
326 static_assert(sizeof(fb->strides) == sizeof(dmabuf->attributes.stride),
327 "drm_fb and dmabuf stride size must match");
328 memcpy(fb->strides, dmabuf->attributes.stride, sizeof(fb->strides));
329 static_assert(ARRAY_LENGTH(fb->offsets) ==
330 ARRAY_LENGTH(dmabuf->attributes.offset),
331 "drm_fb and dmabuf offset size must match");
332 static_assert(sizeof(fb->offsets) == sizeof(dmabuf->attributes.offset),
333 "drm_fb and dmabuf offset size must match");
334 memcpy(fb->offsets, dmabuf->attributes.offset, sizeof(fb->offsets));
335
336 fb->format = pixel_format_get_info(dmabuf->attributes.format);
337 if (!fb->format) {
338 weston_log("couldn't look up format info for 0x%lx\n",
339 (unsigned long) dmabuf->attributes.format);
340 goto err_free;
341 }
342
343 if (is_opaque)
344 fb->format = pixel_format_get_opaque_substitute(fb->format);
345
346 if (backend->min_width > fb->width ||
347 fb->width > backend->max_width ||
348 backend->min_height > fb->height ||
349 fb->height > backend->max_height) {
350 weston_log("bo geometry out of bounds\n");
351 goto err_free;
352 }
353
Nicholas Niro56d1f4e2019-10-29 21:50:54 -0400354#ifdef HAVE_GBM_MODIFIERS
Daniel Stone7580b3c2019-06-18 11:16:53 +0100355 fb->num_planes = dmabuf->attributes.n_planes;
356 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
357 union gbm_bo_handle handle;
358
359 handle = gbm_bo_get_handle_for_plane(fb->bo, i);
360 if (handle.s32 == -1)
361 goto err_free;
362 fb->handles[i] = handle.u32;
363 }
Nicholas Niro56d1f4e2019-10-29 21:50:54 -0400364#else /* NOT HAVE_GBM_MODIFIERS */
365 {
366 union gbm_bo_handle handle;
367
368 fb->num_planes = 1;
369
370 handle = gbm_bo_get_handle(fb->bo);
371
372 if (handle.s32 == -1)
373 goto err_free;
374 fb->handles[0] = handle.u32;
375 }
376#endif /* NOT HAVE_GBM_MODIFIERS */
377
Daniel Stone7580b3c2019-06-18 11:16:53 +0100378
379 if (drm_fb_addfb(backend, fb) != 0)
380 goto err_free;
381
382 return fb;
383
384err_free:
385 drm_fb_destroy_dmabuf(fb);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100386 return NULL;
387}
388
389struct drm_fb *
390drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
391 bool is_opaque, enum drm_fb_type type)
392{
393 struct drm_fb *fb = gbm_bo_get_user_data(bo);
394#ifdef HAVE_GBM_MODIFIERS
395 int i;
396#endif
397
398 if (fb) {
399 assert(fb->type == type);
400 return drm_fb_ref(fb);
401 }
402
403 fb = zalloc(sizeof *fb);
404 if (fb == NULL)
405 return NULL;
406
407 fb->type = type;
408 fb->refcnt = 1;
409 fb->bo = bo;
410 fb->fd = backend->drm.fd;
411
412 fb->width = gbm_bo_get_width(bo);
413 fb->height = gbm_bo_get_height(bo);
414 fb->format = pixel_format_get_info(gbm_bo_get_format(bo));
415 fb->size = 0;
416
417#ifdef HAVE_GBM_MODIFIERS
418 fb->modifier = gbm_bo_get_modifier(bo);
419 fb->num_planes = gbm_bo_get_plane_count(bo);
420 for (i = 0; i < fb->num_planes; i++) {
421 fb->strides[i] = gbm_bo_get_stride_for_plane(bo, i);
422 fb->handles[i] = gbm_bo_get_handle_for_plane(bo, i).u32;
423 fb->offsets[i] = gbm_bo_get_offset(bo, i);
424 }
425#else
426 fb->num_planes = 1;
427 fb->strides[0] = gbm_bo_get_stride(bo);
428 fb->handles[0] = gbm_bo_get_handle(bo).u32;
429 fb->modifier = DRM_FORMAT_MOD_INVALID;
430#endif
431
432 if (!fb->format) {
433 weston_log("couldn't look up format 0x%lx\n",
434 (unsigned long) gbm_bo_get_format(bo));
435 goto err_free;
436 }
437
438 /* We can scanout an ARGB buffer if the surface's opaque region covers
439 * the whole output, but we have to use XRGB as the KMS format code. */
440 if (is_opaque)
441 fb->format = pixel_format_get_opaque_substitute(fb->format);
442
443 if (backend->min_width > fb->width ||
444 fb->width > backend->max_width ||
445 backend->min_height > fb->height ||
446 fb->height > backend->max_height) {
447 weston_log("bo geometry out of bounds\n");
448 goto err_free;
449 }
450
451 if (drm_fb_addfb(backend, fb) != 0) {
452 if (type == BUFFER_GBM_SURFACE)
453 weston_log("failed to create kms fb: %s\n",
454 strerror(errno));
455 goto err_free;
456 }
457
458 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
459
460 return fb;
461
462err_free:
463 free(fb);
464 return NULL;
465}
466
467static void
468drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer,
469 struct weston_buffer_release *buffer_release)
470{
471 assert(fb->buffer_ref.buffer == NULL);
472 assert(fb->type == BUFFER_CLIENT || fb->type == BUFFER_DMABUF);
473 weston_buffer_reference(&fb->buffer_ref, buffer);
474 weston_buffer_release_reference(&fb->buffer_release_ref,
475 buffer_release);
476}
Stefan Agnerccf24072019-07-09 22:02:00 +0200477#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100478
479void
480drm_fb_unref(struct drm_fb *fb)
481{
482 if (!fb)
483 return;
484
485 assert(fb->refcnt > 0);
486 if (--fb->refcnt > 0)
487 return;
488
489 switch (fb->type) {
490 case BUFFER_PIXMAN_DUMB:
491 drm_fb_destroy_dumb(fb);
492 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200493#ifdef BUILD_DRM_GBM
Daniel Stone7580b3c2019-06-18 11:16:53 +0100494 case BUFFER_CURSOR:
495 case BUFFER_CLIENT:
496 gbm_bo_destroy(fb->bo);
497 break;
498 case BUFFER_GBM_SURFACE:
499 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
500 break;
501 case BUFFER_DMABUF:
502 drm_fb_destroy_dmabuf(fb);
503 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200504#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100505 default:
506 assert(NULL);
507 break;
508 }
509}
510
Stefan Agnerccf24072019-07-09 22:02:00 +0200511#ifdef BUILD_DRM_GBM
Daniel Stone7580b3c2019-06-18 11:16:53 +0100512struct drm_fb *
513drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev)
514{
515 struct drm_output *output = state->output;
516 struct drm_backend *b = to_drm_backend(output->base.compositor);
517 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
518 bool is_opaque = weston_view_is_opaque(ev, &ev->transform.boundingbox);
519 struct linux_dmabuf_buffer *dmabuf;
520 struct drm_fb *fb;
521
522 if (ev->alpha != 1.0f)
523 return NULL;
524
525 if (!drm_view_transform_supported(ev, &output->base))
526 return NULL;
527
Daniel Stoned32dfcf2019-08-19 16:53:40 +0100528 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
529 ev->surface->desired_protection > output->base.current_protection)
530 return NULL;
531
Daniel Stone7580b3c2019-06-18 11:16:53 +0100532 if (!buffer)
533 return NULL;
534
535 if (wl_shm_buffer_get(buffer->resource))
536 return NULL;
537
538 /* GBM is used for dmabuf import as well as from client wl_buffer. */
539 if (!b->gbm)
540 return NULL;
541
542 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
543 if (dmabuf) {
544 fb = drm_fb_get_from_dmabuf(dmabuf, b, is_opaque);
545 if (!fb)
546 return NULL;
547 } else {
548 struct gbm_bo *bo;
549
550 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
551 buffer->resource, GBM_BO_USE_SCANOUT);
552 if (!bo)
553 return NULL;
554
555 fb = drm_fb_get_from_bo(bo, b, is_opaque, BUFFER_CLIENT);
556 if (!fb) {
557 gbm_bo_destroy(bo);
558 return NULL;
559 }
560 }
561
562 drm_debug(b, "\t\t\t[view] view %p format: %s\n",
563 ev, fb->format->drm_format_name);
564 drm_fb_set_buffer(fb, buffer,
565 ev->surface->buffer_release_ref.buffer_release);
566 return fb;
567}
Stefan Agnerccf24072019-07-09 22:02:00 +0200568#endif