| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include <sys/mman.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include "config.h" |
| 21 |
|
| 22 |
#if HAVE_DRM |
| 23 |
#include <libavutil/hwcontext_drm.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#if HAVE_VAAPI |
| 27 |
#include <va/va_drmcommon.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include "gpu/hwdec.h" |
| 31 |
#include "gpu/video.h" |
| 32 |
#include "mpv_talloc.h" |
| 33 |
#include "present_sync.h" |
| 34 |
#include "sub/draw_bmp.h" |
| 35 |
#include "video/fmt-conversion.h" |
| 36 |
#include "video/mp_image.h" |
| 37 |
#include "vo.h" |
| 38 |
#include "wayland_common.h" |
| 39 |
#include "wldmabuf/ra_wldmabuf.h" |
| 40 |
|
| 41 |
#if HAVE_VAAPI |
| 42 |
#include "video/vaapi.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
|
| 46 |
#include "linux-dmabuf-unstable-v1.h" |
| 47 |
#include "viewporter.h" |
| 48 |
#include "single-pixel-buffer-v1.h" |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#define WL_BUFFERS_WANTED 15 |
| 53 |
|
| 54 |
enum hwdec_type { |
| 55 |
HWDEC_NONE, |
| 56 |
HWDEC_VAAPI, |
| 57 |
HWDEC_DRMPRIME, |
| 58 |
}; |
| 59 |
|
| 60 |
struct buffer { |
| 61 |
struct vo *vo; |
| 62 |
struct wl_buffer *buffer; |
| 63 |
struct wl_list link; |
| 64 |
struct vo_frame *frame; |
| 65 |
|
| 66 |
uint32_t drm_format; |
| 67 |
uintptr_t id; |
| 68 |
}; |
| 69 |
|
| 70 |
struct osd_buffer { |
| 71 |
struct vo *vo; |
| 72 |
struct wl_buffer *buffer; |
| 73 |
struct wl_list link; |
| 74 |
struct mp_image image; |
| 75 |
size_t size; |
| 76 |
bool attached; |
| 77 |
}; |
| 78 |
|
| 79 |
struct priv { |
| 80 |
struct mp_log *log; |
| 81 |
struct mp_rect src; |
| 82 |
struct mpv_global *global; |
| 83 |
|
| 84 |
struct ra_ctx *ctx; |
| 85 |
struct ra_hwdec_ctx hwdec_ctx; |
| 86 |
|
| 87 |
struct wl_shm_pool *solid_buffer_pool; |
| 88 |
struct wl_buffer *solid_buffer; |
| 89 |
struct wl_list buffer_list; |
| 90 |
struct wl_list osd_buffer_list; |
| 91 |
|
| 92 |
struct wl_shm_pool *osd_shm_pool; |
| 93 |
uint8_t *osd_shm_data; |
| 94 |
int osd_shm_width; |
| 95 |
int osd_shm_stride; |
| 96 |
int osd_shm_height; |
| 97 |
bool osd_surface_is_mapped; |
| 98 |
bool osd_surface_has_contents; |
| 99 |
|
| 100 |
struct osd_buffer *osd_buffer; |
| 101 |
struct mp_draw_sub_cache *osd_cache; |
| 102 |
struct mp_osd_res screen_osd_res; |
| 103 |
|
| 104 |
bool destroy_buffers; |
| 105 |
bool force_window; |
| 106 |
enum hwdec_type hwdec_type; |
| 107 |
|
| 108 |
struct mp_image_params target_params; |
| 109 |
uint32_t drm_format; |
| 110 |
uint64_t drm_modifier; |
| 111 |
}; |
| 112 |
|
| 113 |
static void buffer_handle_release(void *data, struct wl_buffer *wl_buffer) |
| 114 |
{ |
| 115 |
struct buffer *buf = data; |
| 116 |
if (buf->frame) { |
| 117 |
talloc_free(buf->frame); |
| 118 |
buf->frame = NULL; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
static const struct wl_buffer_listener buffer_listener = { |
| 123 |
buffer_handle_release, |
| 124 |
}; |
| 125 |
|
| 126 |
static void osd_buffer_handle_release(void *data, struct wl_buffer *wl_buffer) |
| 127 |
{ |
| 128 |
struct osd_buffer *osd_buf = data; |
| 129 |
osd_buf->attached = false; |
| 130 |
} |
| 131 |
|
| 132 |
static const struct wl_buffer_listener osd_buffer_listener = { |
| 133 |
osd_buffer_handle_release, |
| 134 |
}; |
| 135 |
|
| 136 |
#if HAVE_VAAPI |
| 137 |
static void close_file_descriptors(const VADRMPRIMESurfaceDescriptor *desc) |
| 138 |
{ |
| 139 |
for (int i = 0; i < desc->num_objects; i++) |
| 140 |
close(desc->objects[i].fd); |
| 141 |
} |
| 142 |
#endif |
| 143 |
|
| 144 |
static uintptr_t vaapi_surface_id(struct mp_image *src) |
| 145 |
{ |
| 146 |
uintptr_t id = 0; |
| 147 |
#if HAVE_VAAPI |
| 148 |
id = (uintptr_t)va_surface_id(src); |
| 149 |
#endif |
| 150 |
return id; |
| 151 |
} |
| 152 |
|
| 153 |
static bool vaapi_drm_format(struct vo *vo, struct mp_image *src) |
| 154 |
{ |
| 155 |
bool format = false; |
| 156 |
#if HAVE_VAAPI |
| 157 |
struct priv *p = vo->priv; |
| 158 |
VADRMPRIMESurfaceDescriptor desc = {0}; |
| 159 |
|
| 160 |
uintptr_t id = vaapi_surface_id(src); |
| 161 |
VADisplay display = ra_get_native_resource(p->ctx->ra, "VADisplay"); |
| 162 |
VAStatus status = vaExportSurfaceHandle(display, id, VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2, |
| 163 |
VA_EXPORT_SURFACE_COMPOSED_LAYERS | VA_EXPORT_SURFACE_READ_ONLY, &desc); |
| 164 |
|
| 165 |
if (!CHECK_VA_STATUS(vo, "vaExportSurfaceHandle()")) { |
| 166 |
|
| 167 |
if (status == VA_STATUS_ERROR_INVALID_SURFACE) |
| 168 |
MP_VERBOSE(vo, "vaExportSurfaceHandle: composed layers not supported.\n"); |
| 169 |
goto done; |
| 170 |
} |
| 171 |
p->drm_format = desc.layers[0].drm_format; |
| 172 |
p->drm_modifier = desc.objects[0].drm_format_modifier; |
| 173 |
format = true; |
| 174 |
done: |
| 175 |
close_file_descriptors(&desc); |
| 176 |
#endif |
| 177 |
return format; |
| 178 |
} |
| 179 |
|
| 180 |
static void vaapi_dmabuf_importer(struct buffer *buf, struct mp_image *src, |
| 181 |
struct zwp_linux_buffer_params_v1 *params) |
| 182 |
{ |
| 183 |
#if HAVE_VAAPI |
| 184 |
struct vo *vo = buf->vo; |
| 185 |
struct priv *p = vo->priv; |
| 186 |
VADRMPRIMESurfaceDescriptor desc = {0}; |
| 187 |
VADisplay display = ra_get_native_resource(p->ctx->ra, "VADisplay"); |
| 188 |
|
| 189 |
|
| 190 |
int layer_no = 0; |
| 191 |
buf->id = vaapi_surface_id(src); |
| 192 |
VAStatus status = vaExportSurfaceHandle(display, buf->id, VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2, |
| 193 |
VA_EXPORT_SURFACE_COMPOSED_LAYERS | VA_EXPORT_SURFACE_READ_ONLY, &desc); |
| 194 |
|
| 195 |
if (!CHECK_VA_STATUS(vo, "vaExportSurfaceHandle()")) { |
| 196 |
|
| 197 |
if (status == VA_STATUS_ERROR_INVALID_SURFACE) |
| 198 |
MP_VERBOSE(vo, "vaExportSurfaceHandle: composed layers not supported.\n"); |
| 199 |
goto done; |
| 200 |
} |
| 201 |
buf->drm_format = desc.layers[layer_no].drm_format; |
| 202 |
if (!ra_compatible_format(p->ctx->ra, buf->drm_format, desc.objects[0].drm_format_modifier)) { |
| 203 |
MP_VERBOSE(vo, "%s(%016" PRIx64 ") is not supported.\n", |
| 204 |
mp_tag_str(buf->drm_format), desc.objects[0].drm_format_modifier); |
| 205 |
buf->drm_format = 0; |
| 206 |
goto done; |
| 207 |
} |
| 208 |
for (int plane_no = 0; plane_no < desc.layers[layer_no].num_planes; ++plane_no) { |
| 209 |
int object = desc.layers[layer_no].object_index[plane_no]; |
| 210 |
uint64_t modifier = desc.objects[object].drm_format_modifier; |
| 211 |
zwp_linux_buffer_params_v1_add(params, desc.objects[object].fd, plane_no, desc.layers[layer_no].offset[plane_no], |
| 212 |
desc.layers[layer_no].pitch[plane_no], modifier >> 32, modifier & 0xffffffff); |
| 213 |
} |
| 214 |
|
| 215 |
done: |
| 216 |
close_file_descriptors(&desc); |
| 217 |
#endif |
| 218 |
} |
| 219 |
|
| 220 |
static uintptr_t drmprime_surface_id(struct mp_image *src) |
| 221 |
{ |
| 222 |
uintptr_t id = 0; |
| 223 |
#if HAVE_DRM |
| 224 |
struct AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)src->planes[0]; |
| 225 |
|
| 226 |
AVDRMObjectDescriptor object = desc->objects[0]; |
| 227 |
id = (uintptr_t)object.fd; |
| 228 |
#endif |
| 229 |
return id; |
| 230 |
} |
| 231 |
|
| 232 |
static bool drmprime_drm_format(struct vo *vo, struct mp_image *src) |
| 233 |
{ |
| 234 |
bool format = false; |
| 235 |
#if HAVE_DRM |
| 236 |
struct priv *p = vo->priv; |
| 237 |
struct AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)src->planes[0]; |
| 238 |
if (!desc) |
| 239 |
return format; |
| 240 |
|
| 241 |
|
| 242 |
p->drm_format = desc->layers[0].format; |
| 243 |
int object_index = desc->layers[0].planes[0].object_index; |
| 244 |
p->drm_modifier = desc->objects[object_index].format_modifier; |
| 245 |
format = true; |
| 246 |
#endif |
| 247 |
return format; |
| 248 |
} |
| 249 |
|
| 250 |
static void drmprime_dmabuf_importer(struct buffer *buf, struct mp_image *src, |
| 251 |
struct zwp_linux_buffer_params_v1 *params) |
| 252 |
{ |
| 253 |
#if HAVE_DRM |
| 254 |
int layer_no, plane_no; |
| 255 |
int max_planes = 0; |
| 256 |
const AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)src->planes[0]; |
| 257 |
if (!desc) |
| 258 |
return; |
| 259 |
|
| 260 |
buf->id = drmprime_surface_id(src); |
| 261 |
for (layer_no = 0; layer_no < desc->nb_layers; layer_no++) { |
| 262 |
AVDRMLayerDescriptor layer = desc->layers[layer_no]; |
| 263 |
|
| 264 |
buf->drm_format = layer.format; |
| 265 |
max_planes = MPMAX(max_planes, layer.nb_planes); |
| 266 |
for (plane_no = 0; plane_no < layer.nb_planes; ++plane_no) { |
| 267 |
AVDRMPlaneDescriptor plane = layer.planes[plane_no]; |
| 268 |
int object_index = plane.object_index; |
| 269 |
AVDRMObjectDescriptor object = desc->objects[object_index]; |
| 270 |
uint64_t modifier = object.format_modifier; |
| 271 |
|
| 272 |
zwp_linux_buffer_params_v1_add(params, object.fd, plane_no, plane.offset, |
| 273 |
plane.pitch, modifier >> 32, modifier & 0xffffffff); |
| 274 |
} |
| 275 |
} |
| 276 |
#endif |
| 277 |
} |
| 278 |
|
| 279 |
static intptr_t surface_id(struct vo *vo, struct mp_image *src) |
| 280 |
{ |
| 281 |
struct priv *p = vo->priv; |
| 282 |
switch(p->hwdec_type) { |
| 283 |
case HWDEC_VAAPI: |
| 284 |
return vaapi_surface_id(src); |
| 285 |
case HWDEC_DRMPRIME: |
| 286 |
return drmprime_surface_id(src); |
| 287 |
default: |
| 288 |
return 0; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
static bool drm_format_check(struct vo *vo, struct mp_image *src) |
| 293 |
{ |
| 294 |
struct priv *p = vo->priv; |
| 295 |
switch(p->hwdec_type) { |
| 296 |
case HWDEC_VAAPI: |
| 297 |
return vaapi_drm_format(vo, src); |
| 298 |
case HWDEC_DRMPRIME: |
| 299 |
return drmprime_drm_format(vo, src); |
| 300 |
} |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
static struct buffer *buffer_check(struct vo *vo, struct vo_frame *frame) |
| 305 |
{ |
| 306 |
struct priv *p = vo->priv; |
| 307 |
|
| 308 |
|
| 309 |
if (wl_list_length(&p->buffer_list) < WL_BUFFERS_WANTED) |
| 310 |
goto done; |
| 311 |
|
| 312 |
uintptr_t id = surface_id(vo, frame->current); |
| 313 |
struct buffer *buf; |
| 314 |
wl_list_for_each(buf, &p->buffer_list, link) { |
| 315 |
if (buf->id == id) { |
| 316 |
if (buf->frame) |
| 317 |
talloc_free(buf->frame); |
| 318 |
buf->frame = frame; |
| 319 |
return buf; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
done: |
| 324 |
return NULL; |
| 325 |
} |
| 326 |
|
| 327 |
static struct buffer *buffer_create(struct vo *vo, struct vo_frame *frame) |
| 328 |
{ |
| 329 |
struct vo_wayland_state *wl = vo->wl; |
| 330 |
struct priv *p = vo->priv; |
| 331 |
|
| 332 |
struct buffer *buf = talloc_zero(vo, struct buffer); |
| 333 |
buf->vo = vo; |
| 334 |
buf->frame = frame; |
| 335 |
|
| 336 |
struct mp_image *image = buf->frame->current; |
| 337 |
struct zwp_linux_buffer_params_v1 *params = zwp_linux_dmabuf_v1_create_params(wl->dmabuf); |
| 338 |
|
| 339 |
switch(p->hwdec_type) { |
| 340 |
case HWDEC_VAAPI: |
| 341 |
vaapi_dmabuf_importer(buf, image, params); |
| 342 |
break; |
| 343 |
case HWDEC_DRMPRIME: |
| 344 |
drmprime_dmabuf_importer(buf, image, params); |
| 345 |
break; |
| 346 |
} |
| 347 |
|
| 348 |
if (!buf->drm_format) { |
| 349 |
talloc_free(buf->frame); |
| 350 |
talloc_free(buf); |
| 351 |
zwp_linux_buffer_params_v1_destroy(params); |
| 352 |
return NULL; |
| 353 |
} |
| 354 |
|
| 355 |
buf->buffer = zwp_linux_buffer_params_v1_create_immed(params, image->params.w, image->params.h, |
| 356 |
buf->drm_format, 0); |
| 357 |
zwp_linux_buffer_params_v1_destroy(params); |
| 358 |
wl_buffer_add_listener(buf->buffer, &buffer_listener, buf); |
| 359 |
wl_list_insert(&p->buffer_list, &buf->link); |
| 360 |
return buf; |
| 361 |
} |
| 362 |
|
| 363 |
static struct buffer *buffer_get(struct vo *vo, struct vo_frame *frame) |
| 364 |
{ |
| 365 |
|
| 366 |
struct buffer *buf = buffer_check(vo, frame); |
| 367 |
if (buf) { |
| 368 |
return buf; |
| 369 |
} else { |
| 370 |
return buffer_create(vo, frame); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
static void destroy_buffers(struct vo *vo) |
| 375 |
{ |
| 376 |
struct priv *p = vo->priv; |
| 377 |
struct buffer *buf, *tmp; |
| 378 |
p->destroy_buffers = false; |
| 379 |
wl_list_for_each_safe(buf, tmp, &p->buffer_list, link) { |
| 380 |
wl_list_remove(&buf->link); |
| 381 |
if (buf->frame) { |
| 382 |
talloc_free(buf->frame); |
| 383 |
buf->frame = NULL; |
| 384 |
} |
| 385 |
if (buf->buffer) { |
| 386 |
wl_buffer_destroy(buf->buffer); |
| 387 |
buf->buffer = NULL; |
| 388 |
} |
| 389 |
talloc_free(buf); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
static void destroy_osd_buffers(struct vo *vo) |
| 394 |
{ |
| 395 |
if (!vo->wl) |
| 396 |
return; |
| 397 |
|
| 398 |
struct priv *p = vo->priv; |
| 399 |
struct osd_buffer *osd_buf, *tmp; |
| 400 |
wl_list_for_each_safe(osd_buf, tmp, &p->osd_buffer_list, link) { |
| 401 |
wl_list_remove(&osd_buf->link); |
| 402 |
munmap(osd_buf->image.planes[0], osd_buf->size); |
| 403 |
if (osd_buf->buffer) { |
| 404 |
wl_buffer_destroy(osd_buf->buffer); |
| 405 |
osd_buf->buffer = NULL; |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
static struct osd_buffer *osd_buffer_check(struct vo *vo) |
| 411 |
{ |
| 412 |
struct priv *p = vo->priv; |
| 413 |
struct osd_buffer *osd_buf; |
| 414 |
wl_list_for_each(osd_buf, &p->osd_buffer_list, link) { |
| 415 |
if (!osd_buf->attached) |
| 416 |
return osd_buf; |
| 417 |
} |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
|
| 421 |
static struct osd_buffer *osd_buffer_create(struct vo *vo) |
| 422 |
{ |
| 423 |
struct priv *p = vo->priv; |
| 424 |
struct osd_buffer *osd_buf = talloc_zero(vo, struct osd_buffer); |
| 425 |
|
| 426 |
osd_buf->vo = vo; |
| 427 |
osd_buf->size = p->osd_shm_height * p->osd_shm_stride; |
| 428 |
mp_image_set_size(&osd_buf->image, p->osd_shm_width, p->osd_shm_height); |
| 429 |
osd_buf->image.planes[0] = p->osd_shm_data; |
| 430 |
osd_buf->image.stride[0] = p->osd_shm_stride; |
| 431 |
osd_buf->buffer = wl_shm_pool_create_buffer(p->osd_shm_pool, 0, |
| 432 |
p->osd_shm_width, p->osd_shm_height, |
| 433 |
p->osd_shm_stride, WL_SHM_FORMAT_ARGB8888); |
| 434 |
|
| 435 |
if (!osd_buf->buffer) { |
| 436 |
talloc_free(osd_buf); |
| 437 |
return NULL; |
| 438 |
} |
| 439 |
|
| 440 |
wl_list_insert(&p->osd_buffer_list, &osd_buf->link); |
| 441 |
wl_buffer_add_listener(osd_buf->buffer, &osd_buffer_listener, osd_buf); |
| 442 |
return osd_buf; |
| 443 |
} |
| 444 |
|
| 445 |
static struct osd_buffer *osd_buffer_get(struct vo *vo) |
| 446 |
{ |
| 447 |
struct osd_buffer *osd_buf = osd_buffer_check(vo); |
| 448 |
if (osd_buf) { |
| 449 |
return osd_buf; |
| 450 |
} else { |
| 451 |
return osd_buffer_create(vo); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
static void create_shm_pool(struct vo *vo) |
| 456 |
{ |
| 457 |
struct vo_wayland_state *wl = vo->wl; |
| 458 |
struct priv *p = vo->priv; |
| 459 |
|
| 460 |
int stride = MP_ALIGN_UP(vo->dwidth * 4, MP_IMAGE_BYTE_ALIGN); |
| 461 |
size_t size = vo->dheight * stride; |
| 462 |
int fd = vo_wayland_allocate_memfd(vo, size); |
| 463 |
if (fd < 0) |
| 464 |
return; |
| 465 |
uint8_t *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 466 |
if (data == MAP_FAILED) |
| 467 |
goto error1; |
| 468 |
struct wl_shm_pool *pool = wl_shm_create_pool(wl->shm, fd, size); |
| 469 |
if (!pool) |
| 470 |
goto error2; |
| 471 |
close(fd); |
| 472 |
|
| 473 |
destroy_osd_buffers(vo); |
| 474 |
|
| 475 |
if (p->osd_shm_pool) |
| 476 |
wl_shm_pool_destroy(p->osd_shm_pool); |
| 477 |
p->osd_shm_pool = pool; |
| 478 |
p->osd_shm_width = vo->dwidth; |
| 479 |
p->osd_shm_height = vo->dheight; |
| 480 |
p->osd_shm_stride = stride; |
| 481 |
p->osd_shm_data = data; |
| 482 |
return; |
| 483 |
|
| 484 |
error2: |
| 485 |
munmap(data, size); |
| 486 |
error1: |
| 487 |
close(fd); |
| 488 |
} |
| 489 |
|
| 490 |
static void set_viewport_source(struct vo *vo, struct mp_rect src) |
| 491 |
{ |
| 492 |
struct priv *p = vo->priv; |
| 493 |
struct vo_wayland_state *wl = vo->wl; |
| 494 |
|
| 495 |
if (p->force_window) |
| 496 |
return; |
| 497 |
|
| 498 |
if (!mp_rect_equals(&p->src, &src)) { |
| 499 |
wp_viewport_set_source(wl->video_viewport, wl_fixed_from_int(src.x0), |
| 500 |
wl_fixed_from_int(src.y0), wl_fixed_from_int(mp_rect_w(src)), |
| 501 |
wl_fixed_from_int(mp_rect_h(src))); |
| 502 |
p->src = src; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
static void resize(struct vo *vo) |
| 507 |
{ |
| 508 |
struct vo_wayland_state *wl = vo->wl; |
| 509 |
struct priv *p = vo->priv; |
| 510 |
|
| 511 |
struct mp_rect src; |
| 512 |
struct mp_rect dst; |
| 513 |
struct mp_vo_opts *opts = wl->opts; |
| 514 |
|
| 515 |
const int width = mp_rect_w(wl->geometry); |
| 516 |
const int height = mp_rect_h(wl->geometry); |
| 517 |
vo_get_src_dst_rects(vo, &src, &dst, &p->screen_osd_res); |
| 518 |
|
| 519 |
if (width == 0 || height == 0 || mp_rect_w(dst) == 0 || mp_rect_h(dst) == 0) |
| 520 |
return; |
| 521 |
|
| 522 |
vo_wayland_set_opaque_region(wl, false); |
| 523 |
vo->dwidth = width; |
| 524 |
vo->dheight = height; |
| 525 |
|
| 526 |
create_shm_pool(vo); |
| 527 |
|
| 528 |
|
| 529 |
vo->opts->pan_x = 0; |
| 530 |
vo->opts->pan_y = 0; |
| 531 |
vo_get_src_dst_rects(vo, &src, &dst, &p->screen_osd_res); |
| 532 |
int window_w = MPMAX(0, p->screen_osd_res.ml + p->screen_osd_res.mr) + mp_rect_w(dst); |
| 533 |
int window_h = MPMAX(0, p->screen_osd_res.mt + p->screen_osd_res.mb) + mp_rect_h(dst); |
| 534 |
wp_viewport_set_destination(wl->viewport, lround(window_w / wl->scaling_factor), |
| 535 |
lround(window_h / wl->scaling_factor)); |
| 536 |
|
| 537 |
|
| 538 |
vo->opts->pan_x = opts->pan_x; |
| 539 |
vo->opts->pan_y = opts->pan_y; |
| 540 |
vo_get_src_dst_rects(vo, &src, &dst, &p->screen_osd_res); |
| 541 |
wp_viewport_set_destination(wl->video_viewport, lround(mp_rect_w(dst) / wl->scaling_factor), |
| 542 |
lround(mp_rect_h(dst) / wl->scaling_factor)); |
| 543 |
wl_subsurface_set_position(wl->video_subsurface, lround(dst.x0 / wl->scaling_factor), lround(dst.y0 / wl->scaling_factor)); |
| 544 |
wp_viewport_set_destination(wl->osd_viewport, lround(vo->dwidth / wl->scaling_factor), |
| 545 |
lround(vo->dheight / wl->scaling_factor)); |
| 546 |
wl_subsurface_set_position(wl->osd_subsurface, lround((0 - dst.x0) / wl->scaling_factor), lround((0 - dst.y0) / wl->scaling_factor)); |
| 547 |
set_viewport_source(vo, src); |
| 548 |
|
| 549 |
mp_mutex_lock(&vo->params_mutex); |
| 550 |
vo->target_params->w = mp_rect_w(dst); |
| 551 |
vo->target_params->h = mp_rect_h(dst); |
| 552 |
vo->target_params->rotate = (vo->params->rotate % 90) * 90; |
| 553 |
vo->target_params->vflip = vo->params->vflip; |
| 554 |
mp_mutex_unlock(&vo->params_mutex); |
| 555 |
|
| 556 |
vo->want_redraw = true; |
| 557 |
} |
| 558 |
|
| 559 |
static bool draw_osd(struct vo *vo, struct mp_image *cur, double pts) |
| 560 |
{ |
| 561 |
struct priv *p = vo->priv; |
| 562 |
struct mp_osd_res *res = &p->screen_osd_res; |
| 563 |
bool draw = false; |
| 564 |
|
| 565 |
struct sub_bitmap_list *sbs = osd_render(vo->osd, *res, pts, 0, mp_draw_sub_formats); |
| 566 |
|
| 567 |
if (!sbs) |
| 568 |
return draw; |
| 569 |
|
| 570 |
struct mp_rect act_rc[1], mod_rc[64]; |
| 571 |
int num_act_rc = 0, num_mod_rc = 0; |
| 572 |
|
| 573 |
if (!p->osd_cache) |
| 574 |
p->osd_cache = mp_draw_sub_alloc(p, vo->global); |
| 575 |
|
| 576 |
struct mp_image *osd = mp_draw_sub_overlay(p->osd_cache, sbs, act_rc, |
| 577 |
MP_ARRAY_SIZE(act_rc), &num_act_rc, |
| 578 |
mod_rc, MP_ARRAY_SIZE(mod_rc), &num_mod_rc); |
| 579 |
|
| 580 |
p->osd_surface_has_contents = num_act_rc > 0; |
| 581 |
|
| 582 |
if (!osd || !num_mod_rc) |
| 583 |
goto done; |
| 584 |
|
| 585 |
for (int n = 0; n < num_mod_rc; n++) { |
| 586 |
struct mp_rect rc = mod_rc[n]; |
| 587 |
|
| 588 |
int rw = mp_rect_w(rc); |
| 589 |
int rh = mp_rect_h(rc); |
| 590 |
|
| 591 |
void *src = mp_image_pixel_ptr(osd, 0, rc.x0, rc.y0); |
| 592 |
void *dst = cur->planes[0] + rc.x0 * 4 + rc.y0 * cur->stride[0]; |
| 593 |
|
| 594 |
memcpy_pic(dst, src, rw * 4, rh, cur->stride[0], osd->stride[0]); |
| 595 |
} |
| 596 |
|
| 597 |
draw = true; |
| 598 |
done: |
| 599 |
talloc_free(sbs); |
| 600 |
return draw; |
| 601 |
} |
| 602 |
|
| 603 |
static bool draw_frame(struct vo *vo, struct vo_frame *frame) |
| 604 |
{ |
| 605 |
struct priv *p = vo->priv; |
| 606 |
struct vo_wayland_state *wl = vo->wl; |
| 607 |
struct buffer *buf; |
| 608 |
struct osd_buffer *osd_buf; |
| 609 |
double pts; |
| 610 |
|
| 611 |
if (!vo_wayland_check_visible(vo)) { |
| 612 |
if (frame->current) |
| 613 |
talloc_free(frame); |
| 614 |
return VO_FALSE; |
| 615 |
} |
| 616 |
|
| 617 |
if (p->destroy_buffers) |
| 618 |
destroy_buffers(vo); |
| 619 |
|
| 620 |
|
| 621 |
if (p->force_window) { |
| 622 |
wp_viewport_set_source(wl->video_viewport, wl_fixed_from_int(-1), |
| 623 |
wl_fixed_from_int(-1), wl_fixed_from_int(-1), |
| 624 |
wl_fixed_from_int(-1)); |
| 625 |
wl_surface_attach(wl->video_surface, p->solid_buffer, 0, 0); |
| 626 |
wl_surface_damage_buffer(wl->video_surface, 0, 0, 1, 1); |
| 627 |
} |
| 628 |
|
| 629 |
pts = frame->current ? frame->current->pts : 0; |
| 630 |
if (frame->current) { |
| 631 |
buf = buffer_get(vo, frame); |
| 632 |
|
| 633 |
if (buf && buf->frame) { |
| 634 |
vo_wayland_handle_color(wl); |
| 635 |
struct mp_image *image = buf->frame->current; |
| 636 |
wl_surface_attach(wl->video_surface, buf->buffer, 0, 0); |
| 637 |
wl_surface_damage_buffer(wl->video_surface, 0, 0, image->w, |
| 638 |
image->h); |
| 639 |
|
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
osd_buf = osd_buffer_get(vo); |
| 644 |
if (osd_buf && osd_buf->buffer) { |
| 645 |
if (draw_osd(vo, &osd_buf->image, pts) && p->osd_surface_has_contents) { |
| 646 |
wl_surface_attach(wl->osd_surface, osd_buf->buffer, 0, 0); |
| 647 |
wl_surface_damage_buffer(wl->osd_surface, 0, 0, osd_buf->image.w, |
| 648 |
osd_buf->image.h); |
| 649 |
osd_buf->attached = true; |
| 650 |
p->osd_surface_is_mapped = true; |
| 651 |
} else if (!p->osd_surface_has_contents && p->osd_surface_is_mapped) { |
| 652 |
wl_surface_attach(wl->osd_surface, NULL, 0, 0); |
| 653 |
p->osd_surface_is_mapped = false; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
return VO_TRUE; |
| 658 |
} |
| 659 |
|
| 660 |
static void flip_page(struct vo *vo) |
| 661 |
{ |
| 662 |
struct vo_wayland_state *wl = vo->wl; |
| 663 |
|
| 664 |
wl_surface_commit(wl->osd_surface); |
| 665 |
wl_surface_commit(wl->video_surface); |
| 666 |
wl_surface_commit(wl->surface); |
| 667 |
|
| 668 |
if (wl->opts->wl_internal_vsync) |
| 669 |
vo_wayland_wait_frame(wl); |
| 670 |
|
| 671 |
if (wl->use_present) |
| 672 |
present_sync_swap(wl->present); |
| 673 |
} |
| 674 |
|
| 675 |
static void get_vsync(struct vo *vo, struct vo_vsync_info *info) |
| 676 |
{ |
| 677 |
struct vo_wayland_state *wl = vo->wl; |
| 678 |
if (wl->use_present) |
| 679 |
present_sync_get_info(wl->present, info); |
| 680 |
} |
| 681 |
|
| 682 |
static int query_format(struct vo *vo, int format) |
| 683 |
{ |
| 684 |
return format == IMGFMT_DRMPRIME || format == IMGFMT_VAAPI; |
| 685 |
} |
| 686 |
|
| 687 |
static const int32_t transform_enum_lut[4][2] = { |
| 688 |
{0, 6}, {1, 5}, {2, 4}, {3, 7}, |
| 689 |
}; |
| 690 |
|
| 691 |
static int reconfig(struct vo *vo, struct mp_image *img) |
| 692 |
{ |
| 693 |
struct priv *p = vo->priv; |
| 694 |
|
| 695 |
switch (img->imgfmt) { |
| 696 |
case IMGFMT_VAAPI: |
| 697 |
p->hwdec_type = HWDEC_VAAPI; |
| 698 |
break; |
| 699 |
case IMGFMT_DRMPRIME: |
| 700 |
p->hwdec_type = HWDEC_DRMPRIME; |
| 701 |
break; |
| 702 |
default: |
| 703 |
p->hwdec_type = HWDEC_NONE; |
| 704 |
} |
| 705 |
|
| 706 |
if (p->hwdec_type == HWDEC_NONE) { |
| 707 |
MP_ERR(vo, "Format '%s' is not a valid hardware accelerated format!\n", |
| 708 |
mp_imgfmt_to_name(img->imgfmt)); |
| 709 |
return VO_ERROR; |
| 710 |
} |
| 711 |
|
| 712 |
if (img->params.force_window) { |
| 713 |
p->force_window = true; |
| 714 |
goto done; |
| 715 |
} |
| 716 |
|
| 717 |
if (!drm_format_check(vo, img)) { |
| 718 |
MP_ERR(vo, "Unable to get drm format from hardware decoding!\n"); |
| 719 |
return VO_ERROR; |
| 720 |
} |
| 721 |
|
| 722 |
if (!ra_compatible_format(p->ctx->ra, p->drm_format, p->drm_modifier)) { |
| 723 |
MP_ERR(vo, "Format '%s' with modifier '(%016" PRIx64 ")' is not supported by" |
| 724 |
" the compositor.\n", mp_tag_str(p->drm_format), p->drm_modifier); |
| 725 |
return VO_ERROR; |
| 726 |
} |
| 727 |
|
| 728 |
p->force_window = false; |
| 729 |
done: |
| 730 |
if (!vo_wayland_reconfig(vo)) |
| 731 |
return VO_ERROR; |
| 732 |
|
| 733 |
mp_mutex_lock(&vo->params_mutex); |
| 734 |
p->target_params = img->params; |
| 735 |
|
| 736 |
mp_image_params_restore_dovi_mapping(&p->target_params); |
| 737 |
|
| 738 |
struct pl_hdr_metadata *hdr = &p->target_params.color.hdr; |
| 739 |
hdr->scene_max[0] = hdr->scene_max[1] = hdr->scene_max[2] = 0; |
| 740 |
hdr->scene_avg = hdr->max_pq_y = hdr->avg_pq_y = 0; |
| 741 |
vo->target_params = &p->target_params; |
| 742 |
mp_mutex_unlock(&vo->params_mutex); |
| 743 |
|
| 744 |
wl_surface_set_buffer_transform(vo->wl->video_surface, |
| 745 |
transform_enum_lut[img->params.rotate / 90][!!img->params.vflip]); |
| 746 |
|
| 747 |
|
| 748 |
destroy_buffers(vo); |
| 749 |
return 0; |
| 750 |
} |
| 751 |
|
| 752 |
static int control(struct vo *vo, uint32_t request, void *data) |
| 753 |
{ |
| 754 |
struct priv *p = vo->priv; |
| 755 |
int events = 0; |
| 756 |
int ret; |
| 757 |
|
| 758 |
switch (request) { |
| 759 |
case VOCTRL_RESET: |
| 760 |
p->destroy_buffers = true; |
| 761 |
return VO_TRUE; |
| 762 |
case VOCTRL_SET_PANSCAN: |
| 763 |
resize(vo); |
| 764 |
return VO_TRUE; |
| 765 |
} |
| 766 |
|
| 767 |
ret = vo_wayland_control(vo, &events, request, data); |
| 768 |
if (events & VO_EVENT_RESIZE) |
| 769 |
resize(vo); |
| 770 |
if (events & VO_EVENT_EXPOSE) |
| 771 |
vo->want_redraw = true; |
| 772 |
vo_event(vo, events); |
| 773 |
|
| 774 |
return ret; |
| 775 |
} |
| 776 |
|
| 777 |
static void uninit(struct vo *vo) |
| 778 |
{ |
| 779 |
struct priv *p = vo->priv; |
| 780 |
|
| 781 |
destroy_buffers(vo); |
| 782 |
destroy_osd_buffers(vo); |
| 783 |
if (p->osd_shm_pool) |
| 784 |
wl_shm_pool_destroy(p->osd_shm_pool); |
| 785 |
if (p->solid_buffer_pool) |
| 786 |
wl_shm_pool_destroy(p->solid_buffer_pool); |
| 787 |
if (p->solid_buffer) |
| 788 |
wl_buffer_destroy(p->solid_buffer); |
| 789 |
ra_hwdec_ctx_uninit(&p->hwdec_ctx); |
| 790 |
if (vo->hwdec_devs) { |
| 791 |
hwdec_devices_set_loader(vo->hwdec_devs, NULL, NULL); |
| 792 |
hwdec_devices_destroy(vo->hwdec_devs); |
| 793 |
} |
| 794 |
|
| 795 |
vo_wayland_uninit(vo); |
| 796 |
ra_ctx_destroy(&p->ctx); |
| 797 |
} |
| 798 |
|
| 799 |
static int preinit(struct vo *vo) |
| 800 |
{ |
| 801 |
struct priv *p = vo->priv; |
| 802 |
|
| 803 |
p->log = vo->log; |
| 804 |
p->global = vo->global; |
| 805 |
p->ctx = ra_ctx_create_by_name(vo, "wldmabuf"); |
| 806 |
wl_list_init(&p->buffer_list); |
| 807 |
wl_list_init(&p->osd_buffer_list); |
| 808 |
if (!p->ctx) |
| 809 |
goto err; |
| 810 |
|
| 811 |
mp_assert(p->ctx->ra); |
| 812 |
|
| 813 |
if (!vo->wl->dmabuf || !vo->wl->dmabuf_feedback) { |
| 814 |
MP_FATAL(vo->wl, "Compositor doesn't support the %s (ver. 4) protocol!\n", |
| 815 |
zwp_linux_dmabuf_v1_interface.name); |
| 816 |
goto err; |
| 817 |
} |
| 818 |
|
| 819 |
if (!vo->wl->shm) { |
| 820 |
MP_FATAL(vo->wl, "Compositor doesn't support the %s protocol!\n", |
| 821 |
wl_shm_interface.name); |
| 822 |
goto err; |
| 823 |
} |
| 824 |
|
| 825 |
if (!vo->wl->video_subsurface) { |
| 826 |
MP_FATAL(vo->wl, "Compositor doesn't support the %s protocol!\n", |
| 827 |
wl_subcompositor_interface.name); |
| 828 |
goto err; |
| 829 |
} |
| 830 |
|
| 831 |
if (vo->wl->single_pixel_manager) { |
| 832 |
p->solid_buffer = wp_single_pixel_buffer_manager_v1_create_u32_rgba_buffer( |
| 833 |
vo->wl->single_pixel_manager, 0, 0, 0, UINT32_MAX); |
| 834 |
} else { |
| 835 |
int width = 1; |
| 836 |
int height = 1; |
| 837 |
int stride = MP_ALIGN_UP(width * 4, MP_IMAGE_BYTE_ALIGN); |
| 838 |
int fd = vo_wayland_allocate_memfd(vo, stride); |
| 839 |
if (fd < 0) |
| 840 |
goto err; |
| 841 |
p->solid_buffer_pool = wl_shm_create_pool(vo->wl->shm, fd, height * stride); |
| 842 |
close(fd); |
| 843 |
if (!p->solid_buffer_pool) |
| 844 |
goto err; |
| 845 |
p->solid_buffer = wl_shm_pool_create_buffer( |
| 846 |
p->solid_buffer_pool, 0, width, height, stride, WL_SHM_FORMAT_XRGB8888); |
| 847 |
} |
| 848 |
if (!p->solid_buffer) |
| 849 |
goto err; |
| 850 |
|
| 851 |
wl_surface_attach(vo->wl->surface, p->solid_buffer, 0, 0); |
| 852 |
|
| 853 |
vo->hwdec_devs = hwdec_devices_create(); |
| 854 |
p->hwdec_ctx = (struct ra_hwdec_ctx) { |
| 855 |
.log = p->log, |
| 856 |
.global = p->global, |
| 857 |
.ra_ctx = p->ctx, |
| 858 |
}; |
| 859 |
|
| 860 |
|
| 861 |
ra_hwdec_ctx_init(&p->hwdec_ctx, vo->hwdec_devs, "vaapi", false); |
| 862 |
ra_hwdec_ctx_init(&p->hwdec_ctx, vo->hwdec_devs, "drmprime", false); |
| 863 |
|
| 864 |
p->src = (struct mp_rect){0, 0, 0, 0}; |
| 865 |
return 0; |
| 866 |
|
| 867 |
err: |
| 868 |
uninit(vo); |
| 869 |
return -1; |
| 870 |
} |
| 871 |
|
| 872 |
const struct vo_driver video_out_dmabuf_wayland = { |
| 873 |
.description = "Wayland dmabuf video output", |
| 874 |
.name = "dmabuf-wayland", |
| 875 |
.caps = VO_CAP_ROTATE90 | |
| 876 |
VO_CAP_FRAMEOWNER | |
| 877 |
VO_CAP_VFLIP | |
| 878 |
0x0, |
| 879 |
.preinit = preinit, |
| 880 |
.query_format = query_format, |
| 881 |
.reconfig2 = reconfig, |
| 882 |
.control = control, |
| 883 |
.draw_frame = draw_frame, |
| 884 |
.flip_page = flip_page, |
| 885 |
.get_vsync = get_vsync, |
| 886 |
.wakeup = vo_wayland_wakeup, |
| 887 |
.wait_events = vo_wayland_wait_events, |
| 888 |
.uninit = uninit, |
| 889 |
.priv_size = sizeof(struct priv), |
| 890 |
}; |