2017-10-23 15:51:49 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-04-28 17:32:25 +00:00
|
|
|
#include <errno.h>
|
2017-10-23 15:51:49 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <xf86drm.h>
|
|
|
|
#include <xf86drmMode.h>
|
2020-03-02 15:22:10 +00:00
|
|
|
#include <drm_mode.h>
|
2017-10-23 15:51:49 +00:00
|
|
|
|
2020-04-28 17:32:25 +00:00
|
|
|
#include "common/common.h"
|
2017-10-23 15:51:49 +00:00
|
|
|
#include "common/msg.h"
|
|
|
|
#include "drm_common.h"
|
|
|
|
#include "drm_prime.h"
|
|
|
|
|
2020-03-04 11:48:40 +00:00
|
|
|
int drm_prime_create_framebuffer(struct mp_log *log, int fd,
|
|
|
|
AVDRMFrameDescriptor *descriptor, int width,
|
|
|
|
int height, struct drm_prime_framebuffer *framebuffer,
|
|
|
|
struct drm_prime_handle_refs *handle_refs)
|
2017-10-23 15:51:49 +00:00
|
|
|
{
|
|
|
|
AVDRMLayerDescriptor *layer = NULL;
|
2020-05-04 18:46:47 +00:00
|
|
|
uint32_t pitches[4] = { 0 };
|
|
|
|
uint32_t offsets[4] = { 0 };
|
|
|
|
uint32_t handles[4] = { 0 };
|
|
|
|
uint64_t modifiers[4] = { 0 };
|
2017-10-23 15:51:49 +00:00
|
|
|
int ret, layer_fd;
|
|
|
|
|
|
|
|
if (descriptor && descriptor->nb_layers) {
|
|
|
|
*framebuffer = (struct drm_prime_framebuffer){0};
|
|
|
|
|
|
|
|
for (int object = 0; object < descriptor->nb_objects; object++) {
|
2020-03-04 11:48:40 +00:00
|
|
|
ret = drmPrimeFDToHandle(fd, descriptor->objects[object].fd,
|
|
|
|
&framebuffer->gem_handles[object]);
|
2017-10-23 15:51:49 +00:00
|
|
|
if (ret < 0) {
|
2020-03-04 11:48:40 +00:00
|
|
|
mp_err(log, "Failed to retrieve the Prime Handle from handle %d (%d).\n",
|
|
|
|
object, descriptor->objects[object].fd);
|
2017-10-23 15:51:49 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
2020-05-04 18:46:47 +00:00
|
|
|
modifiers[object] = descriptor->objects[object].format_modifier;
|
2017-10-23 15:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
layer = &descriptor->layers[0];
|
|
|
|
|
|
|
|
for (int plane = 0; plane < AV_DRM_MAX_PLANES; plane++) {
|
|
|
|
layer_fd = framebuffer->gem_handles[layer->planes[plane].object_index];
|
|
|
|
if (layer_fd && layer->planes[plane].pitch) {
|
|
|
|
pitches[plane] = layer->planes[plane].pitch;
|
|
|
|
offsets[plane] = layer->planes[plane].offset;
|
|
|
|
handles[plane] = layer_fd;
|
|
|
|
} else {
|
|
|
|
pitches[plane] = 0;
|
|
|
|
offsets[plane] = 0;
|
|
|
|
handles[plane] = 0;
|
2020-03-02 15:22:10 +00:00
|
|
|
modifiers[plane] = 0;
|
2017-10-23 15:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 15:22:10 +00:00
|
|
|
ret = drmModeAddFB2WithModifiers(fd, width, height, layer->format,
|
2020-03-04 11:48:40 +00:00
|
|
|
handles, pitches, offsets,
|
|
|
|
modifiers, &framebuffer->fb_id,
|
|
|
|
DRM_MODE_FB_MODIFIERS);
|
2017-10-23 15:51:49 +00:00
|
|
|
if (ret < 0) {
|
2020-05-04 18:47:38 +00:00
|
|
|
ret = drmModeAddFB2(fd, width, height, layer->format,
|
|
|
|
handles, pitches, offsets,
|
|
|
|
&framebuffer->fb_id, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
mp_err(log, "Failed to create framebuffer with drmModeAddFB2 on layer %d: %s\n",
|
|
|
|
0, mp_strerror(errno));
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-10-23 15:51:49 +00:00
|
|
|
}
|
2020-05-04 18:47:38 +00:00
|
|
|
|
2020-03-04 11:48:40 +00:00
|
|
|
for (int plane = 0; plane < AV_DRM_MAX_PLANES; plane++) {
|
|
|
|
drm_prime_add_handle_ref(handle_refs, framebuffer->gem_handles[plane]);
|
|
|
|
}
|
2024-03-20 02:23:23 +00:00
|
|
|
}
|
2017-10-23 15:51:49 +00:00
|
|
|
|
2024-03-20 02:23:23 +00:00
|
|
|
return 0;
|
2017-10-23 15:51:49 +00:00
|
|
|
|
|
|
|
fail:
|
2024-03-20 02:23:23 +00:00
|
|
|
memset(framebuffer, 0, sizeof(*framebuffer));
|
|
|
|
return -1;
|
2017-10-23 15:51:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 11:48:40 +00:00
|
|
|
void drm_prime_destroy_framebuffer(struct mp_log *log, int fd,
|
|
|
|
struct drm_prime_framebuffer *framebuffer,
|
|
|
|
struct drm_prime_handle_refs *handle_refs)
|
2017-10-23 15:51:49 +00:00
|
|
|
{
|
|
|
|
if (framebuffer->fb_id)
|
|
|
|
drmModeRmFB(fd, framebuffer->fb_id);
|
|
|
|
|
|
|
|
for (int i = 0; i < AV_DRM_MAX_PLANES; i++) {
|
2020-03-04 11:48:40 +00:00
|
|
|
if (framebuffer->gem_handles[i]) {
|
|
|
|
drm_prime_remove_handle_ref(handle_refs,
|
|
|
|
framebuffer->gem_handles[i]);
|
|
|
|
if (!drm_prime_get_handle_ref_count(handle_refs,
|
|
|
|
framebuffer->gem_handles[i])) {
|
|
|
|
drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &framebuffer->gem_handles[i]);
|
|
|
|
}
|
|
|
|
}
|
2017-10-23 15:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
memset(framebuffer, 0, sizeof(*framebuffer));
|
|
|
|
}
|
2020-03-04 11:48:40 +00:00
|
|
|
|
|
|
|
void drm_prime_init_handle_ref_count(void *talloc_parent,
|
|
|
|
struct drm_prime_handle_refs *handle_refs)
|
|
|
|
{
|
|
|
|
handle_refs->handle_ref_count = talloc_zero(talloc_parent, uint32_t);
|
|
|
|
handle_refs->size = 1;
|
|
|
|
handle_refs->ctx = talloc_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void drm_prime_add_handle_ref(struct drm_prime_handle_refs *handle_refs,
|
|
|
|
uint32_t handle)
|
|
|
|
{
|
|
|
|
if (handle) {
|
|
|
|
if (handle > handle_refs->size) {
|
|
|
|
MP_TARRAY_GROW(handle_refs->ctx, handle_refs->handle_ref_count,
|
2024-05-07 11:15:36 +00:00
|
|
|
handle - 1);
|
|
|
|
uint32_t *p = handle_refs->handle_ref_count;
|
|
|
|
memset(&p[handle_refs->size], 0, (handle - handle_refs->size) * sizeof(p[0]));
|
2022-05-07 15:18:06 +00:00
|
|
|
handle_refs->size = handle;
|
2020-03-04 11:48:40 +00:00
|
|
|
}
|
|
|
|
handle_refs->handle_ref_count[handle - 1]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void drm_prime_remove_handle_ref(struct drm_prime_handle_refs *handle_refs,
|
|
|
|
uint32_t handle)
|
|
|
|
{
|
|
|
|
if (handle) {
|
|
|
|
if (handle <= handle_refs->size &&
|
|
|
|
handle_refs->handle_ref_count[handle - 1])
|
|
|
|
{
|
|
|
|
handle_refs->handle_ref_count[handle - 1]--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t drm_prime_get_handle_ref_count(struct drm_prime_handle_refs *handle_refs,
|
|
|
|
uint32_t handle)
|
|
|
|
{
|
|
|
|
if (handle) {
|
|
|
|
if (handle <= handle_refs->size)
|
|
|
|
return handle_refs->handle_ref_count[handle - 1];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|