2022-09-27 16:12:54 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2023-02-03 17:03:29 +00:00
|
|
|
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);
|
|
|
|
}
|
2022-09-27 16:12:54 +00:00
|
|
|
|
|
|
|
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;
|
2023-02-03 17:03:29 +00:00
|
|
|
for (int i = 0; i < WLBUF_NUM_PURG_ENTRIES; ++i)
|
|
|
|
pool->purg.entries[i] = NULL;
|
2022-09-27 16:12:54 +00:00
|
|
|
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
2023-02-03 17:03:29 +00:00
|
|
|
void wlbuf_pool_clean(struct wlbuf_pool *pool, bool final_clean)
|
2022-09-27 16:12:54 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (!pool)
|
|
|
|
return;
|
2023-02-03 17:03:29 +00:00
|
|
|
MP_TRACE(pool->vo, "Begin clean pool\n");
|
|
|
|
if (final_clean)
|
|
|
|
free_purgatory(pool);
|
2022-09-27 16:12:54 +00:00
|
|
|
for (i = 0; i < pool->num_allocated; ++i) {
|
|
|
|
struct wlbuf_pool_entry *entry = pool->entries[i];
|
|
|
|
if (!entry)
|
|
|
|
continue;
|
2023-02-03 17:03:29 +00:00
|
|
|
wlbuf_pool_entry_free(entry, final_clean);
|
2022-09-27 16:12:54 +00:00
|
|
|
pool->entries[i] = NULL;
|
|
|
|
}
|
|
|
|
pool->num_entries = 0;
|
2023-02-03 17:03:29 +00:00
|
|
|
MP_TRACE(pool->vo, "End clean pool\n");
|
2022-09-27 16:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlbuf_pool_free(struct wlbuf_pool *pool)
|
|
|
|
{
|
|
|
|
if (!pool)
|
|
|
|
return;
|
2023-02-03 17:03:29 +00:00
|
|
|
|
|
|
|
wlbuf_pool_clean(pool, true);
|
2022-09-27 16:12:54 +00:00
|
|
|
talloc_free(pool);
|
|
|
|
}
|
|
|
|
|
2023-02-03 17:03:29 +00:00
|
|
|
static void wlbuf_pool_entry_free(struct wlbuf_pool_entry *entry, bool force)
|
2022-09-27 16:12:54 +00:00
|
|
|
{
|
|
|
|
if (!entry)
|
|
|
|
return;
|
2023-02-03 17:03:29 +00:00
|
|
|
if (force && entry->image) {
|
|
|
|
mp_image_unrefp(&entry->image);
|
|
|
|
entry->image = NULL;
|
2022-09-27 16:12:54 +00:00
|
|
|
}
|
2023-02-03 17:03:29 +00:00
|
|
|
if (!set_pending_free(entry)) {
|
|
|
|
MP_TRACE(entry->vo, "Free buffer pool entry : %lu\n", entry->key);
|
2022-09-27 16:12:54 +00:00
|
|
|
if (entry->buffer)
|
|
|
|
wl_buffer_destroy(entry->buffer);
|
|
|
|
talloc_free(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 17:03:29 +00:00
|
|
|
/**
|
|
|
|
* Unref pool entry's image, and also free entry itself if it's set to pending_free
|
|
|
|
*/
|
2022-09-27 16:12:54 +00:00
|
|
|
static void wlbuf_pool_entry_release(void *data, struct wl_buffer *wl_buffer)
|
|
|
|
{
|
2023-02-03 17:03:29 +00:00
|
|
|
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);
|
2022-09-27 16:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
2023-02-03 17:03:29 +00:00
|
|
|
if (!item)
|
|
|
|
continue;
|
2022-09-27 16:12:54 +00:00
|
|
|
if (item->key == key) {
|
2023-02-03 17:03:29 +00:00
|
|
|
if (item->image) {
|
2022-09-27 16:12:54 +00:00
|
|
|
mp_image_unrefp(&src);
|
2023-02-03 17:03:29 +00:00
|
|
|
MP_DBG(item->vo, "Buffer already scheduled - returning NULL.\n");
|
2022-09-27 16:12:54 +00:00
|
|
|
return NULL;
|
|
|
|
} else {
|
2023-02-03 17:03:29 +00:00
|
|
|
item->image = src;
|
2022-09-27 16:12:54 +00:00
|
|
|
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);
|
2023-02-03 17:03:29 +00:00
|
|
|
entry->pool = pool;
|
|
|
|
MP_TRACE(entry->vo, "Allocate buffer pool entry : %lu\n", entry->key);
|
2022-09-27 16:12:54 +00:00
|
|
|
params = zwp_linux_dmabuf_v1_create_params(wl->dmabuf);
|
2023-02-03 17:03:29 +00:00
|
|
|
import_successful = pool->dmabuf_importer(src, entry, params);
|
2022-09-27 16:12:54 +00:00
|
|
|
if (!import_successful) {
|
2023-02-03 17:03:29 +00:00
|
|
|
MP_DBG(entry->vo, "Failed to import\n");
|
|
|
|
wlbuf_pool_entry_free(entry, false);
|
2022-09-27 16:12:54 +00:00
|
|
|
} 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);
|
2023-02-03 17:03:29 +00:00
|
|
|
if (!import_successful) {
|
|
|
|
mp_image_unrefp(&src);
|
|
|
|
return NULL;
|
2022-09-27 16:12:54 +00:00
|
|
|
}
|
|
|
|
/* 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);
|
2023-02-03 17:03:29 +00:00
|
|
|
entry->image = src;
|
2022-09-27 16:12:54 +00:00
|
|
|
pool->entries[pool->num_entries++] = entry;
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|