blob: 017422f20c6fa563df110e73e21d0e2a5a84da9d [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>
Daniel Stone7580b3c2019-06-18 11:16:53 +010036
Daniel Stone7580b3c2019-06-18 11:16:53 +010037#include <libweston/libweston.h>
38#include <libweston/backend-drm.h>
39#include <libweston/pixel-formats.h>
40#include <libweston/linux-dmabuf.h>
41#include "shared/helpers.h"
Pekka Paalanen4b301fe2021-02-04 17:39:45 +020042#include "shared/weston-drm-fourcc.h"
Daniel Stone7580b3c2019-06-18 11:16:53 +010043#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;
Daniel Stone7580b3c2019-06-18 11:16:53 +010077 uint64_t mods[4] = { };
78 size_t i;
Daniel Stone7580b3c2019-06-18 11:16:53 +010079
80 /* If we have a modifier set, we must only use the WithModifiers
81 * entrypoint; we cannot import it through legacy ioctls. */
82 if (b->fb_modifiers && fb->modifier != DRM_FORMAT_MOD_INVALID) {
83 /* KMS demands that if a modifier is set, it must be the same
84 * for all planes. */
Daniel Stone7580b3c2019-06-18 11:16:53 +010085 for (i = 0; i < ARRAY_LENGTH(mods) && fb->handles[i]; i++)
86 mods[i] = fb->modifier;
87 ret = drmModeAddFB2WithModifiers(fb->fd, fb->width, fb->height,
88 fb->format->format,
89 fb->handles, fb->strides,
90 fb->offsets, mods, &fb->fb_id,
91 DRM_MODE_FB_MODIFIERS);
Daniel Stone7580b3c2019-06-18 11:16:53 +010092 return ret;
93 }
94
95 ret = drmModeAddFB2(fb->fd, fb->width, fb->height, fb->format->format,
96 fb->handles, fb->strides, fb->offsets, &fb->fb_id,
97 0);
98 if (ret == 0)
99 return 0;
100
101 /* Legacy AddFB can't always infer the format from depth/bpp alone, so
102 * check if our format is one of the lucky ones. */
103 if (!fb->format->depth || !fb->format->bpp)
104 return ret;
105
106 /* Cannot fall back to AddFB for multi-planar formats either. */
107 if (fb->handles[1] || fb->handles[2] || fb->handles[3])
108 return ret;
109
110 ret = drmModeAddFB(fb->fd, fb->width, fb->height,
111 fb->format->depth, fb->format->bpp,
112 fb->strides[0], fb->handles[0], &fb->fb_id);
113 return ret;
114}
115
116struct drm_fb *
117drm_fb_create_dumb(struct drm_backend *b, int width, int height,
118 uint32_t format)
119{
120 struct drm_fb *fb;
121 int ret;
122
123 struct drm_mode_create_dumb create_arg;
124 struct drm_mode_destroy_dumb destroy_arg;
125 struct drm_mode_map_dumb map_arg;
126
127 fb = zalloc(sizeof *fb);
128 if (!fb)
129 return NULL;
130 fb->refcnt = 1;
131
132 fb->format = pixel_format_get_info(format);
133 if (!fb->format) {
134 weston_log("failed to look up format 0x%lx\n",
135 (unsigned long) format);
136 goto err_fb;
137 }
138
139 if (!fb->format->depth || !fb->format->bpp) {
140 weston_log("format 0x%lx is not compatible with dumb buffers\n",
141 (unsigned long) format);
142 goto err_fb;
143 }
144
145 memset(&create_arg, 0, sizeof create_arg);
146 create_arg.bpp = fb->format->bpp;
147 create_arg.width = width;
148 create_arg.height = height;
149
150 ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
151 if (ret)
152 goto err_fb;
153
154 fb->type = BUFFER_PIXMAN_DUMB;
155 fb->modifier = DRM_FORMAT_MOD_INVALID;
156 fb->handles[0] = create_arg.handle;
157 fb->strides[0] = create_arg.pitch;
158 fb->num_planes = 1;
159 fb->size = create_arg.size;
160 fb->width = width;
161 fb->height = height;
162 fb->fd = b->drm.fd;
163
164 if (drm_fb_addfb(b, fb) != 0) {
165 weston_log("failed to create kms fb: %s\n", strerror(errno));
166 goto err_bo;
167 }
168
169 memset(&map_arg, 0, sizeof map_arg);
170 map_arg.handle = fb->handles[0];
171 ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
172 if (ret)
173 goto err_add_fb;
174
175 fb->map = mmap(NULL, fb->size, PROT_WRITE,
176 MAP_SHARED, b->drm.fd, map_arg.offset);
177 if (fb->map == MAP_FAILED)
178 goto err_add_fb;
179
180 return fb;
181
182err_add_fb:
183 drmModeRmFB(b->drm.fd, fb->fb_id);
184err_bo:
185 memset(&destroy_arg, 0, sizeof(destroy_arg));
186 destroy_arg.handle = create_arg.handle;
187 drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
188err_fb:
189 free(fb);
190 return NULL;
191}
192
193struct drm_fb *
194drm_fb_ref(struct drm_fb *fb)
195{
196 fb->refcnt++;
197 return fb;
198}
199
Stefan Agnerccf24072019-07-09 22:02:00 +0200200#ifdef BUILD_DRM_GBM
201static void
202drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
203{
204 struct drm_fb *fb = data;
205
206 assert(fb->type == BUFFER_GBM_SURFACE || fb->type == BUFFER_CLIENT ||
207 fb->type == BUFFER_CURSOR);
208 drm_fb_destroy(fb);
209}
210
Daniel Stone7580b3c2019-06-18 11:16:53 +0100211static void
212drm_fb_destroy_dmabuf(struct drm_fb *fb)
213{
214 /* We deliberately do not close the GEM handles here; GBM manages
215 * their lifetime through the BO. */
216 if (fb->bo)
217 gbm_bo_destroy(fb->bo);
218 drm_fb_destroy(fb);
219}
220
221static struct drm_fb *
222drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf,
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300223 struct drm_backend *backend, bool is_opaque,
224 uint32_t *try_view_on_plane_failure_reasons)
Daniel Stone7580b3c2019-06-18 11:16:53 +0100225{
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300226#ifndef HAVE_GBM_FD_IMPORT
227 /* Importing a buffer to KMS requires explicit modifiers, so
228 * we can't continue with the legacy GBM_BO_IMPORT_FD instead
229 * of GBM_BO_IMPORT_FD_MODIFIER. */
230 return NULL;
231#else
Daniel Stone7580b3c2019-06-18 11:16:53 +0100232 struct drm_fb *fb;
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300233 int i;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100234 struct gbm_import_fd_modifier_data import_mod = {
235 .width = dmabuf->attributes.width,
236 .height = dmabuf->attributes.height,
237 .format = dmabuf->attributes.format,
238 .num_fds = dmabuf->attributes.n_planes,
239 .modifier = dmabuf->attributes.modifier[0],
240 };
Nicholas Niro7aab7462019-10-29 22:13:09 -0400241
Emmanuel Gil Peyroteff793a2021-07-31 17:25:41 +0200242 /* We should not import to KMS a buffer that has been allocated using no
243 * modifiers. Usually drivers use linear layouts to allocate with no
244 * modifiers, but this is not a rule. The driver could use, for
245 * instance, a tiling layout under the hood - and both Weston and the
246 * KMS driver can't know. So giving the buffer to KMS is not safe, as
247 * not knowing its layout can result in garbage being displayed. In
248 * short, importing a buffer to KMS requires explicit modifiers. */
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300249 if (dmabuf->attributes.modifier[0] == DRM_FORMAT_MOD_INVALID) {
250 if (try_view_on_plane_failure_reasons)
251 *try_view_on_plane_failure_reasons |=
252 FAILURE_REASONS_DMABUF_MODIFIER_INVALID;
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300253 return NULL;
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300254 }
Daniel Stone7580b3c2019-06-18 11:16:53 +0100255
256 /* XXX: TODO:
257 *
258 * Currently the buffer is rejected if any dmabuf attribute
259 * flag is set. This keeps us from passing an inverted /
260 * interlaced / bottom-first buffer (or any other type that may
261 * be added in the future) through to an overlay. Ultimately,
262 * these types of buffers should be handled through buffer
263 * transforms and not as spot-checks requiring specific
264 * knowledge. */
265 if (dmabuf->attributes.flags)
266 return NULL;
267
268 fb = zalloc(sizeof *fb);
269 if (fb == NULL)
270 return NULL;
271
272 fb->refcnt = 1;
273 fb->type = BUFFER_DMABUF;
274
Leandro Ribeiro3193ab52021-04-27 19:00:34 -0300275 ARRAY_COPY(import_mod.fds, dmabuf->attributes.fd);
276 ARRAY_COPY(import_mod.strides, dmabuf->attributes.stride);
277 ARRAY_COPY(import_mod.offsets, dmabuf->attributes.offset);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100278
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300279 fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER,
280 &import_mod, GBM_BO_USE_SCANOUT);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100281 if (!fb->bo)
282 goto err_free;
283
284 fb->width = dmabuf->attributes.width;
285 fb->height = dmabuf->attributes.height;
286 fb->modifier = dmabuf->attributes.modifier[0];
287 fb->size = 0;
288 fb->fd = backend->drm.fd;
289
Leandro Ribeiro3193ab52021-04-27 19:00:34 -0300290 ARRAY_COPY(fb->strides, dmabuf->attributes.stride);
291 ARRAY_COPY(fb->offsets, dmabuf->attributes.offset);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100292
293 fb->format = pixel_format_get_info(dmabuf->attributes.format);
294 if (!fb->format) {
295 weston_log("couldn't look up format info for 0x%lx\n",
296 (unsigned long) dmabuf->attributes.format);
297 goto err_free;
298 }
299
300 if (is_opaque)
301 fb->format = pixel_format_get_opaque_substitute(fb->format);
302
303 if (backend->min_width > fb->width ||
304 fb->width > backend->max_width ||
305 backend->min_height > fb->height ||
306 fb->height > backend->max_height) {
307 weston_log("bo geometry out of bounds\n");
308 goto err_free;
309 }
310
311 fb->num_planes = dmabuf->attributes.n_planes;
312 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
313 union gbm_bo_handle handle;
314
315 handle = gbm_bo_get_handle_for_plane(fb->bo, i);
316 if (handle.s32 == -1)
317 goto err_free;
318 fb->handles[i] = handle.u32;
319 }
320
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300321 if (drm_fb_addfb(backend, fb) != 0) {
322 if (try_view_on_plane_failure_reasons)
323 *try_view_on_plane_failure_reasons |=
324 FAILURE_REASONS_ADD_FB_FAILED;
Daniel Stone7580b3c2019-06-18 11:16:53 +0100325 goto err_free;
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300326 }
Daniel Stone7580b3c2019-06-18 11:16:53 +0100327
328 return fb;
329
330err_free:
331 drm_fb_destroy_dmabuf(fb);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100332 return NULL;
Leandro Ribeiro5aea1bc2021-04-21 10:38:58 -0300333#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100334}
335
336struct drm_fb *
337drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
338 bool is_opaque, enum drm_fb_type type)
339{
340 struct drm_fb *fb = gbm_bo_get_user_data(bo);
341#ifdef HAVE_GBM_MODIFIERS
342 int i;
343#endif
344
345 if (fb) {
346 assert(fb->type == type);
347 return drm_fb_ref(fb);
348 }
349
350 fb = zalloc(sizeof *fb);
351 if (fb == NULL)
352 return NULL;
353
354 fb->type = type;
355 fb->refcnt = 1;
356 fb->bo = bo;
357 fb->fd = backend->drm.fd;
358
359 fb->width = gbm_bo_get_width(bo);
360 fb->height = gbm_bo_get_height(bo);
361 fb->format = pixel_format_get_info(gbm_bo_get_format(bo));
362 fb->size = 0;
363
364#ifdef HAVE_GBM_MODIFIERS
365 fb->modifier = gbm_bo_get_modifier(bo);
366 fb->num_planes = gbm_bo_get_plane_count(bo);
367 for (i = 0; i < fb->num_planes; i++) {
368 fb->strides[i] = gbm_bo_get_stride_for_plane(bo, i);
369 fb->handles[i] = gbm_bo_get_handle_for_plane(bo, i).u32;
370 fb->offsets[i] = gbm_bo_get_offset(bo, i);
371 }
372#else
373 fb->num_planes = 1;
374 fb->strides[0] = gbm_bo_get_stride(bo);
375 fb->handles[0] = gbm_bo_get_handle(bo).u32;
376 fb->modifier = DRM_FORMAT_MOD_INVALID;
377#endif
378
379 if (!fb->format) {
380 weston_log("couldn't look up format 0x%lx\n",
381 (unsigned long) gbm_bo_get_format(bo));
382 goto err_free;
383 }
384
385 /* We can scanout an ARGB buffer if the surface's opaque region covers
386 * the whole output, but we have to use XRGB as the KMS format code. */
387 if (is_opaque)
388 fb->format = pixel_format_get_opaque_substitute(fb->format);
389
390 if (backend->min_width > fb->width ||
391 fb->width > backend->max_width ||
392 backend->min_height > fb->height ||
393 fb->height > backend->max_height) {
394 weston_log("bo geometry out of bounds\n");
395 goto err_free;
396 }
397
398 if (drm_fb_addfb(backend, fb) != 0) {
399 if (type == BUFFER_GBM_SURFACE)
400 weston_log("failed to create kms fb: %s\n",
401 strerror(errno));
402 goto err_free;
403 }
404
405 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
406
407 return fb;
408
409err_free:
410 free(fb);
411 return NULL;
412}
413
414static void
415drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer,
416 struct weston_buffer_release *buffer_release)
417{
418 assert(fb->buffer_ref.buffer == NULL);
419 assert(fb->type == BUFFER_CLIENT || fb->type == BUFFER_DMABUF);
420 weston_buffer_reference(&fb->buffer_ref, buffer);
421 weston_buffer_release_reference(&fb->buffer_release_ref,
422 buffer_release);
423}
Stefan Agnerccf24072019-07-09 22:02:00 +0200424#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100425
426void
427drm_fb_unref(struct drm_fb *fb)
428{
429 if (!fb)
430 return;
431
432 assert(fb->refcnt > 0);
433 if (--fb->refcnt > 0)
434 return;
435
436 switch (fb->type) {
437 case BUFFER_PIXMAN_DUMB:
438 drm_fb_destroy_dumb(fb);
439 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200440#ifdef BUILD_DRM_GBM
Daniel Stone7580b3c2019-06-18 11:16:53 +0100441 case BUFFER_CURSOR:
442 case BUFFER_CLIENT:
443 gbm_bo_destroy(fb->bo);
444 break;
445 case BUFFER_GBM_SURFACE:
446 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
447 break;
448 case BUFFER_DMABUF:
449 drm_fb_destroy_dmabuf(fb);
450 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200451#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100452 default:
453 assert(NULL);
454 break;
455 }
456}
457
Stefan Agnerccf24072019-07-09 22:02:00 +0200458#ifdef BUILD_DRM_GBM
Marius Vlad81bada52019-11-11 00:27:17 +0200459bool
460drm_can_scanout_dmabuf(struct weston_compositor *ec,
461 struct linux_dmabuf_buffer *dmabuf)
462{
463 struct drm_fb *fb;
464 struct drm_backend *b = to_drm_backend(ec);
465 bool ret = false;
466
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300467 fb = drm_fb_get_from_dmabuf(dmabuf, b, true, NULL);
Marius Vlad81bada52019-11-11 00:27:17 +0200468 if (fb)
469 ret = true;
470
471 drm_fb_unref(fb);
472 drm_debug(b, "[dmabuf] dmabuf %p, import test %s\n", dmabuf,
473 ret ? "succeeded" : "failed");
474 return ret;
475}
476
Daniel Stone7580b3c2019-06-18 11:16:53 +0100477struct drm_fb *
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300478drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev,
479 uint32_t *try_view_on_plane_failure_reasons)
Daniel Stone7580b3c2019-06-18 11:16:53 +0100480{
481 struct drm_output *output = state->output;
482 struct drm_backend *b = to_drm_backend(output->base.compositor);
483 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
484 bool is_opaque = weston_view_is_opaque(ev, &ev->transform.boundingbox);
485 struct linux_dmabuf_buffer *dmabuf;
486 struct drm_fb *fb;
487
488 if (ev->alpha != 1.0f)
489 return NULL;
490
491 if (!drm_view_transform_supported(ev, &output->base))
492 return NULL;
493
Daniel Stoned32dfcf2019-08-19 16:53:40 +0100494 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
495 ev->surface->desired_protection > output->base.current_protection)
496 return NULL;
497
Daniel Stone7580b3c2019-06-18 11:16:53 +0100498 if (!buffer)
499 return NULL;
500
501 if (wl_shm_buffer_get(buffer->resource))
502 return NULL;
503
504 /* GBM is used for dmabuf import as well as from client wl_buffer. */
505 if (!b->gbm)
506 return NULL;
507
508 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
509 if (dmabuf) {
Leandro Ribeiro0a7034c2021-09-13 14:52:53 -0300510 fb = drm_fb_get_from_dmabuf(dmabuf, b, is_opaque,
511 try_view_on_plane_failure_reasons);
Daniel Stone7580b3c2019-06-18 11:16:53 +0100512 if (!fb)
513 return NULL;
514 } else {
515 struct gbm_bo *bo;
516
517 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
518 buffer->resource, GBM_BO_USE_SCANOUT);
519 if (!bo)
520 return NULL;
521
522 fb = drm_fb_get_from_bo(bo, b, is_opaque, BUFFER_CLIENT);
523 if (!fb) {
524 gbm_bo_destroy(bo);
525 return NULL;
526 }
527 }
528
529 drm_debug(b, "\t\t\t[view] view %p format: %s\n",
530 ev, fb->format->drm_format_name);
531 drm_fb_set_buffer(fb, buffer,
532 ev->surface->buffer_release_ref.buffer_release);
533 return fb;
534}
Stefan Agnerccf24072019-07-09 22:02:00 +0200535#endif