blob: 1eb4a9eb7c8ca44fcffb105dce7158443c70f53a [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{
229#ifdef HAVE_GBM_FD_IMPORT
230 struct drm_fb *fb;
231 struct gbm_import_fd_data import_legacy = {
232 .width = dmabuf->attributes.width,
233 .height = dmabuf->attributes.height,
234 .format = dmabuf->attributes.format,
235 .stride = dmabuf->attributes.stride[0],
236 .fd = dmabuf->attributes.fd[0],
237 };
238 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 };
245 int i;
246
247 /* XXX: TODO:
248 *
249 * Currently the buffer is rejected if any dmabuf attribute
250 * flag is set. This keeps us from passing an inverted /
251 * interlaced / bottom-first buffer (or any other type that may
252 * be added in the future) through to an overlay. Ultimately,
253 * these types of buffers should be handled through buffer
254 * transforms and not as spot-checks requiring specific
255 * knowledge. */
256 if (dmabuf->attributes.flags)
257 return NULL;
258
259 fb = zalloc(sizeof *fb);
260 if (fb == NULL)
261 return NULL;
262
263 fb->refcnt = 1;
264 fb->type = BUFFER_DMABUF;
265
266 static_assert(ARRAY_LENGTH(import_mod.fds) ==
267 ARRAY_LENGTH(dmabuf->attributes.fd),
268 "GBM and linux_dmabuf FD size must match");
269 static_assert(sizeof(import_mod.fds) == sizeof(dmabuf->attributes.fd),
270 "GBM and linux_dmabuf FD size must match");
271 memcpy(import_mod.fds, dmabuf->attributes.fd, sizeof(import_mod.fds));
272
273 static_assert(ARRAY_LENGTH(import_mod.strides) ==
274 ARRAY_LENGTH(dmabuf->attributes.stride),
275 "GBM and linux_dmabuf stride size must match");
276 static_assert(sizeof(import_mod.strides) ==
277 sizeof(dmabuf->attributes.stride),
278 "GBM and linux_dmabuf stride size must match");
279 memcpy(import_mod.strides, dmabuf->attributes.stride,
280 sizeof(import_mod.strides));
281
282 static_assert(ARRAY_LENGTH(import_mod.offsets) ==
283 ARRAY_LENGTH(dmabuf->attributes.offset),
284 "GBM and linux_dmabuf offset size must match");
285 static_assert(sizeof(import_mod.offsets) ==
286 sizeof(dmabuf->attributes.offset),
287 "GBM and linux_dmabuf offset size must match");
288 memcpy(import_mod.offsets, dmabuf->attributes.offset,
289 sizeof(import_mod.offsets));
290
291 /* The legacy FD-import path does not allow us to supply modifiers,
292 * multiple planes, or buffer offsets. */
293 if (dmabuf->attributes.modifier[0] != DRM_FORMAT_MOD_INVALID ||
294 import_mod.num_fds > 1 ||
295 import_mod.offsets[0] > 0) {
296 fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER,
297 &import_mod,
298 GBM_BO_USE_SCANOUT);
299 } else {
300 fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD,
301 &import_legacy,
302 GBM_BO_USE_SCANOUT);
303 }
304
305 if (!fb->bo)
306 goto err_free;
307
308 fb->width = dmabuf->attributes.width;
309 fb->height = dmabuf->attributes.height;
310 fb->modifier = dmabuf->attributes.modifier[0];
311 fb->size = 0;
312 fb->fd = backend->drm.fd;
313
314 static_assert(ARRAY_LENGTH(fb->strides) ==
315 ARRAY_LENGTH(dmabuf->attributes.stride),
316 "drm_fb and dmabuf stride size must match");
317 static_assert(sizeof(fb->strides) == sizeof(dmabuf->attributes.stride),
318 "drm_fb and dmabuf stride size must match");
319 memcpy(fb->strides, dmabuf->attributes.stride, sizeof(fb->strides));
320 static_assert(ARRAY_LENGTH(fb->offsets) ==
321 ARRAY_LENGTH(dmabuf->attributes.offset),
322 "drm_fb and dmabuf offset size must match");
323 static_assert(sizeof(fb->offsets) == sizeof(dmabuf->attributes.offset),
324 "drm_fb and dmabuf offset size must match");
325 memcpy(fb->offsets, dmabuf->attributes.offset, sizeof(fb->offsets));
326
327 fb->format = pixel_format_get_info(dmabuf->attributes.format);
328 if (!fb->format) {
329 weston_log("couldn't look up format info for 0x%lx\n",
330 (unsigned long) dmabuf->attributes.format);
331 goto err_free;
332 }
333
334 if (is_opaque)
335 fb->format = pixel_format_get_opaque_substitute(fb->format);
336
337 if (backend->min_width > fb->width ||
338 fb->width > backend->max_width ||
339 backend->min_height > fb->height ||
340 fb->height > backend->max_height) {
341 weston_log("bo geometry out of bounds\n");
342 goto err_free;
343 }
344
345 fb->num_planes = dmabuf->attributes.n_planes;
346 for (i = 0; i < dmabuf->attributes.n_planes; i++) {
347 union gbm_bo_handle handle;
348
349 handle = gbm_bo_get_handle_for_plane(fb->bo, i);
350 if (handle.s32 == -1)
351 goto err_free;
352 fb->handles[i] = handle.u32;
353 }
354
355 if (drm_fb_addfb(backend, fb) != 0)
356 goto err_free;
357
358 return fb;
359
360err_free:
361 drm_fb_destroy_dmabuf(fb);
362#endif
363 return NULL;
364}
365
366struct drm_fb *
367drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
368 bool is_opaque, enum drm_fb_type type)
369{
370 struct drm_fb *fb = gbm_bo_get_user_data(bo);
371#ifdef HAVE_GBM_MODIFIERS
372 int i;
373#endif
374
375 if (fb) {
376 assert(fb->type == type);
377 return drm_fb_ref(fb);
378 }
379
380 fb = zalloc(sizeof *fb);
381 if (fb == NULL)
382 return NULL;
383
384 fb->type = type;
385 fb->refcnt = 1;
386 fb->bo = bo;
387 fb->fd = backend->drm.fd;
388
389 fb->width = gbm_bo_get_width(bo);
390 fb->height = gbm_bo_get_height(bo);
391 fb->format = pixel_format_get_info(gbm_bo_get_format(bo));
392 fb->size = 0;
393
394#ifdef HAVE_GBM_MODIFIERS
395 fb->modifier = gbm_bo_get_modifier(bo);
396 fb->num_planes = gbm_bo_get_plane_count(bo);
397 for (i = 0; i < fb->num_planes; i++) {
398 fb->strides[i] = gbm_bo_get_stride_for_plane(bo, i);
399 fb->handles[i] = gbm_bo_get_handle_for_plane(bo, i).u32;
400 fb->offsets[i] = gbm_bo_get_offset(bo, i);
401 }
402#else
403 fb->num_planes = 1;
404 fb->strides[0] = gbm_bo_get_stride(bo);
405 fb->handles[0] = gbm_bo_get_handle(bo).u32;
406 fb->modifier = DRM_FORMAT_MOD_INVALID;
407#endif
408
409 if (!fb->format) {
410 weston_log("couldn't look up format 0x%lx\n",
411 (unsigned long) gbm_bo_get_format(bo));
412 goto err_free;
413 }
414
415 /* We can scanout an ARGB buffer if the surface's opaque region covers
416 * the whole output, but we have to use XRGB as the KMS format code. */
417 if (is_opaque)
418 fb->format = pixel_format_get_opaque_substitute(fb->format);
419
420 if (backend->min_width > fb->width ||
421 fb->width > backend->max_width ||
422 backend->min_height > fb->height ||
423 fb->height > backend->max_height) {
424 weston_log("bo geometry out of bounds\n");
425 goto err_free;
426 }
427
428 if (drm_fb_addfb(backend, fb) != 0) {
429 if (type == BUFFER_GBM_SURFACE)
430 weston_log("failed to create kms fb: %s\n",
431 strerror(errno));
432 goto err_free;
433 }
434
435 gbm_bo_set_user_data(bo, fb, drm_fb_destroy_gbm);
436
437 return fb;
438
439err_free:
440 free(fb);
441 return NULL;
442}
443
444static void
445drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer,
446 struct weston_buffer_release *buffer_release)
447{
448 assert(fb->buffer_ref.buffer == NULL);
449 assert(fb->type == BUFFER_CLIENT || fb->type == BUFFER_DMABUF);
450 weston_buffer_reference(&fb->buffer_ref, buffer);
451 weston_buffer_release_reference(&fb->buffer_release_ref,
452 buffer_release);
453}
Stefan Agnerccf24072019-07-09 22:02:00 +0200454#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100455
456void
457drm_fb_unref(struct drm_fb *fb)
458{
459 if (!fb)
460 return;
461
462 assert(fb->refcnt > 0);
463 if (--fb->refcnt > 0)
464 return;
465
466 switch (fb->type) {
467 case BUFFER_PIXMAN_DUMB:
468 drm_fb_destroy_dumb(fb);
469 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200470#ifdef BUILD_DRM_GBM
Daniel Stone7580b3c2019-06-18 11:16:53 +0100471 case BUFFER_CURSOR:
472 case BUFFER_CLIENT:
473 gbm_bo_destroy(fb->bo);
474 break;
475 case BUFFER_GBM_SURFACE:
476 gbm_surface_release_buffer(fb->gbm_surface, fb->bo);
477 break;
478 case BUFFER_DMABUF:
479 drm_fb_destroy_dmabuf(fb);
480 break;
Stefan Agnerccf24072019-07-09 22:02:00 +0200481#endif
Daniel Stone7580b3c2019-06-18 11:16:53 +0100482 default:
483 assert(NULL);
484 break;
485 }
486}
487
Stefan Agnerccf24072019-07-09 22:02:00 +0200488#ifdef BUILD_DRM_GBM
Daniel Stone7580b3c2019-06-18 11:16:53 +0100489struct drm_fb *
490drm_fb_get_from_view(struct drm_output_state *state, struct weston_view *ev)
491{
492 struct drm_output *output = state->output;
493 struct drm_backend *b = to_drm_backend(output->base.compositor);
494 struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
495 bool is_opaque = weston_view_is_opaque(ev, &ev->transform.boundingbox);
496 struct linux_dmabuf_buffer *dmabuf;
497 struct drm_fb *fb;
498
499 if (ev->alpha != 1.0f)
500 return NULL;
501
502 if (!drm_view_transform_supported(ev, &output->base))
503 return NULL;
504
Daniel Stoned32dfcf2019-08-19 16:53:40 +0100505 if (ev->surface->protection_mode == WESTON_SURFACE_PROTECTION_MODE_ENFORCED &&
506 ev->surface->desired_protection > output->base.current_protection)
507 return NULL;
508
Daniel Stone7580b3c2019-06-18 11:16:53 +0100509 if (!buffer)
510 return NULL;
511
512 if (wl_shm_buffer_get(buffer->resource))
513 return NULL;
514
515 /* GBM is used for dmabuf import as well as from client wl_buffer. */
516 if (!b->gbm)
517 return NULL;
518
519 dmabuf = linux_dmabuf_buffer_get(buffer->resource);
520 if (dmabuf) {
521 fb = drm_fb_get_from_dmabuf(dmabuf, b, is_opaque);
522 if (!fb)
523 return NULL;
524 } else {
525 struct gbm_bo *bo;
526
527 bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
528 buffer->resource, GBM_BO_USE_SCANOUT);
529 if (!bo)
530 return NULL;
531
532 fb = drm_fb_get_from_bo(bo, b, is_opaque, BUFFER_CLIENT);
533 if (!fb) {
534 gbm_bo_destroy(bo);
535 return NULL;
536 }
537 }
538
539 drm_debug(b, "\t\t\t[view] view %p format: %s\n",
540 ev, fb->format->drm_format_name);
541 drm_fb_set_buffer(fb, buffer,
542 ev->surface->buffer_release_ref.buffer_release);
543 return fb;
544}
Stefan Agnerccf24072019-07-09 22:02:00 +0200545#endif