summaryrefslogtreecommitdiffstats
path: root/video/out/drm_prime.c
blob: 9335fa8e0257a934ba325d9f3728bb9942dcceae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * 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/>.
 */

#include <errno.h>
#include <unistd.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm_mode.h>

#include "common/common.h"
#include "common/msg.h"
#include "drm_common.h"
#include "drm_prime.h"

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)
{
    AVDRMLayerDescriptor *layer = NULL;
    uint32_t pitches[4] = { 0 };
    uint32_t offsets[4] = { 0 };
    uint32_t handles[4] = { 0 };
    uint64_t modifiers[4] = { 0 };
    int ret, layer_fd;

    if (descriptor && descriptor->nb_layers) {
        *framebuffer = (struct drm_prime_framebuffer){0};

        for (int object = 0; object < descriptor->nb_objects; object++) {
            ret = drmPrimeFDToHandle(fd, descriptor->objects[object].fd,
                                     &framebuffer->gem_handles[object]);
            if (ret < 0) {
                mp_err(log, "Failed to retrieve the Prime Handle from handle %d (%d).\n",
                       object, descriptor->objects[object].fd);
                goto fail;
            }
            modifiers[object] = descriptor->objects[object].format_modifier;
        }

        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;
                modifiers[plane] = 0;
            }
        }

        ret = drmModeAddFB2WithModifiers(fd, width, height, layer->format,
                                         handles, pitches, offsets,
                                         modifiers, &framebuffer->fb_id,
                                         DRM_MODE_FB_MODIFIERS);
        if (ret < 0) {
            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;
            }
        }

        for (int plane = 0; plane < AV_DRM_MAX_PLANES; plane++) {
            drm_prime_add_handle_ref(handle_refs, framebuffer->gem_handles[plane]);
        }
   }

   return 0;

fail:
   memset(framebuffer, 0, sizeof(*framebuffer));
   return -1;
}

void drm_prime_destroy_framebuffer(struct mp_log *log, int fd,
                                   struct drm_prime_framebuffer *framebuffer,
                                   struct drm_prime_handle_refs *handle_refs)
{
    if (framebuffer->fb_id)
        drmModeRmFB(fd, framebuffer->fb_id);

    for (int i = 0; i < AV_DRM_MAX_PLANES; i++) {
        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]);
            }
        }
    }

    memset(framebuffer, 0, sizeof(*framebuffer));
}

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) {
            handle_refs->size = handle;
            MP_TARRAY_GROW(handle_refs->ctx, handle_refs->handle_ref_count,
                           handle_refs->size);
        }
        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;
}