summaryrefslogtreecommitdiffstats
path: root/video/out/wlbuf_pool.c
blob: 8c05adb5fbc3e287eb008a582a48eb25144ae8a1 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * 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 "mpv_talloc.h"
#include "common/global.h"
#include "vo.h"
#include "video/mp_image.h"

#include "wayland_common.h"
#include "generated/wayland/linux-dmabuf-unstable-v1.h"
#include "pthread.h"
#include "wlbuf_pool.h"

#define WLBUF_POOL_NUM_ALLOCATED_INIT 30

static void wlbuf_pool_entry_free(struct wlbuf_pool_entry *entry, bool force);

/**
 * When trying to free pool entries that are being held by Wayland,
 * these entries are set to pending free state. They will be freed
 * when either the frame listener gets called, or the vo is uninitialized
*/
static bool set_pending_free(struct wlbuf_pool_entry *entry) {
    if (entry->image) {
        MP_TRACE(entry->vo, "Pending free for buffer pool entry : %lu\n",
                entry->key);
        entry->pending_free = true;
        // add to purgatory
        struct wlbuf_pool *pool = entry->pool;
        for (int i = 0; i < WLBUF_NUM_PURG_ENTRIES; ++i) {
            if (pool->purg.entries[i] == NULL) {
                pool->purg.entries[i] = entry;
                break;
            }
        }
    }

    return entry->image;
}

/**
 * Complete pending free of entry.
 *
 */
static void complete_pending_free(struct wlbuf_pool_entry *entry) {
    if (entry->pending_free) {
        // 1. remove from purgatory
        struct wlbuf_pool *pool = entry->pool;
        for (int i = 0; i < WLBUF_NUM_PURG_ENTRIES; ++i) {
            if (pool->purg.entries[i] == entry) {
                pool->purg.entries[i] = NULL;
                break;
            }
        }
        // 2. free entry
        wlbuf_pool_entry_free(entry, false);
    }
}

/**
 * Free all purgatory entries
 */
static void free_purgatory(struct wlbuf_pool *pool){
    for (int i = 0; i < WLBUF_NUM_PURG_ENTRIES; ++i)
        wlbuf_pool_entry_free(pool->purg.entries[i], true);
}

struct wlbuf_pool *wlbuf_pool_alloc(struct vo *vo, struct vo_wayland_state *wl, wlbuf_pool_key_provider key_provider,
                                    wlbuf_pool_dmabuf_importer dmabuf_importer)
{
    struct wlbuf_pool *pool = talloc(NULL, struct wlbuf_pool);
    memset(pool, 0, sizeof(struct wlbuf_pool));
    pool->num_allocated = WLBUF_POOL_NUM_ALLOCATED_INIT;
    pool->entries = talloc_array(pool, struct wlbuf_pool_entry *, pool->num_allocated);
    memset(pool->entries, 0, pool->num_allocated * sizeof(struct wlbuf_pool_entry *));
    pool->vo = vo;
    pool->key_provider = key_provider;
    pool->dmabuf_importer = dmabuf_importer;
    pool->wl = wl;
    for (int i = 0; i < WLBUF_NUM_PURG_ENTRIES; ++i)
        pool->purg.entries[i] = NULL;

    return pool;
}

void wlbuf_pool_clean(struct wlbuf_pool *pool, bool final_clean)
{
    int i;
    if (!pool)
        return;
    MP_TRACE(pool->vo, "Begin clean pool\n");
    if (final_clean)
        free_purgatory(pool);
    for (i = 0; i < pool->num_allocated; ++i) {
        struct wlbuf_pool_entry *entry = pool->entries[i];
        if (!entry)
            continue;
        wlbuf_pool_entry_free(entry, final_clean);
        pool->entries[i] = NULL;
    }
    pool->num_entries = 0;
    MP_TRACE(pool->vo, "End clean pool\n");
}

void wlbuf_pool_free(struct wlbuf_pool *pool)
{
    if (!pool)
        return;

    wlbuf_pool_clean(pool, true);
    talloc_free(pool);
}

static void wlbuf_pool_entry_free(struct wlbuf_pool_entry *entry, bool force)
{
    if (!entry)
        return;
    if (force && entry->image) {
        mp_image_unrefp(&entry->image);
        entry->image = NULL;
    }
    if (!set_pending_free(entry)) {
        MP_TRACE(entry->vo, "Free buffer pool entry : %lu\n", entry->key);
        if (entry->buffer)
            wl_buffer_destroy(entry->buffer);
        talloc_free(entry);
    }
}

/**
 * Unref pool entry's image, and also free entry itself if it's set to pending_free
 */
static void wlbuf_pool_entry_release(void *data, struct wl_buffer *wl_buffer)
{
    struct wlbuf_pool_entry *entry = (struct wlbuf_pool_entry*) data;

    MP_TRACE(entry->vo, "Release buffer pool entry : %lu\n", entry->key);

    // 1. always unref image
    if (entry->image)
        mp_image_unrefp(&entry->image);
    entry->image = NULL;

    // 2. complete pending free if needed
    complete_pending_free(entry);
}

static const struct wl_buffer_listener wlbuf_pool_listener = {
    wlbuf_pool_entry_release,
};

struct wlbuf_pool_entry *wlbuf_pool_get_entry(struct wlbuf_pool *pool, struct mp_image *src)
{
    uintptr_t key;
    struct wlbuf_pool_entry *entry;
    struct vo_wayland_state *wl = pool->wl;
    bool import_successful;
    struct zwp_linux_buffer_params_v1 *params;

    if (!pool || !src)
        return NULL;

    /* 1. try to find existing entry in pool */
    src = mp_image_new_ref(src);
    key = pool->key_provider(src);
    for (int i = 0; i < pool->num_entries; ++i) {
        struct wlbuf_pool_entry *item = pool->entries[i];
        if (!item)
            continue;
        if (item->key == key) {
            if (item->image) {
                mp_image_unrefp(&src);
                MP_DBG(item->vo, "Buffer already scheduled - returning NULL.\n");
                return NULL;
            } else {
                item->image = src;
                return item;
            }
        }
    }
    /* 2. otherwise allocate new entry and buffer */
    entry = talloc(pool, struct wlbuf_pool_entry);
    memset(entry, 0, sizeof(struct wlbuf_pool_entry));
    entry->vo = pool->vo;
    entry->key = pool->key_provider(src);
    entry->pool = pool;
    MP_TRACE(entry->vo, "Allocate buffer pool entry : %lu\n", entry->key);
    params = zwp_linux_dmabuf_v1_create_params(wl->dmabuf);
    import_successful = pool->dmabuf_importer(src, entry, params);
    if (!import_successful) {
        MP_DBG(entry->vo, "Failed to import\n");
        wlbuf_pool_entry_free(entry, false);
    } else {
        entry->buffer = zwp_linux_buffer_params_v1_create_immed(params, src->params.w, src->params.h,
                                                                entry->drm_format, 0);
    }
    zwp_linux_buffer_params_v1_destroy(params);
    if (!import_successful) {
        mp_image_unrefp(&src);
        return NULL;
    }
    /* 3. add new entry to pool */
    if (pool->num_entries == pool->num_allocated) {
        int current_num_allocated = pool->num_allocated;
        pool->num_allocated *= 2;
        pool->entries = talloc_realloc(pool, pool->entries, struct wlbuf_pool_entry *, pool->num_allocated);
        for (int i = current_num_allocated; i < pool->num_allocated; ++i)
            pool->entries[i] = NULL;
    }
    wl_buffer_add_listener(entry->buffer, &wlbuf_pool_listener, entry);
    entry->image = src;
    pool->entries[pool->num_entries++] = entry;

    return entry;
}