wg_allocator.c 8.98 KB
Newer Older
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
/*
 * GStreamer memory allocator
 *
 * Copyright 2022 Rémi Bernon for CodeWeavers
 *
 * This library 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.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#if 0
#pragma makedep unix
#endif

#include "config.h"

#include <assert.h>
#include <stdarg.h>

#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/audio/audio.h>

#include "unix_private.h"

36 37
#include "wine/list.h"

38 39 40
typedef struct
{
    GstMemory parent;
41
    struct list entry;
42

43
    GstAllocationParams alloc_params;
44 45
    GstMemory *unix_memory;
    GstMapInfo unix_map_info;
46 47 48

    struct wg_sample *sample;
    gsize written;
49 50 51 52 53
} WgMemory;

typedef struct
{
    GstAllocator parent;
54 55 56 57

    pthread_mutex_t mutex;
    pthread_cond_t release_cond;
    struct list memory_list;
58 59

    struct wg_sample *next_sample;
60 61 62 63 64 65 66 67 68
} WgAllocator;

typedef struct
{
    GstAllocatorClass parent_class;
} WgAllocatorClass;

G_DEFINE_TYPE(WgAllocator, wg_allocator, GST_TYPE_ALLOCATOR);

69 70 71 72 73 74 75 76 77 78 79 80 81
static void *get_unix_memory_data(WgMemory *memory)
{
    if (!memory->unix_memory)
    {
        memory->unix_memory = gst_allocator_alloc(NULL, memory->parent.maxsize, &memory->alloc_params);
        gst_memory_map(memory->unix_memory, &memory->unix_map_info, GST_MAP_WRITE);
        GST_INFO("Allocated unix memory %p, data %p for memory %p, sample %p", memory->unix_memory,
                memory->unix_map_info.data, memory, memory->sample);
    }

    return memory->unix_map_info.data;
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
static void release_memory_sample(WgAllocator *allocator, WgMemory *memory, bool discard_data)
{
    struct wg_sample *sample;

    if (!(sample = memory->sample))
        return;

    while (sample->refcount > 1)
    {
        GST_WARNING("Waiting for sample %p to be unmapped", sample);
        pthread_cond_wait(&allocator->release_cond, &allocator->mutex);
    }
    InterlockedDecrement(&sample->refcount);

    if (memory->written && !discard_data)
    {
        GST_WARNING("Copying %#zx bytes from sample %p, back to memory %p", memory->written, sample, memory);
99
        memcpy(get_unix_memory_data(memory), wg_sample_data(memory->sample), memory->written);
100 101 102 103 104 105
    }

    memory->sample = NULL;
    GST_INFO("Released sample %p from memory %p", sample, memory);
}

106 107
static gpointer wg_allocator_map(GstMemory *gst_memory, GstMapInfo *info, gsize maxsize)
{
108
    WgAllocator *allocator = (WgAllocator *)gst_memory->allocator;
109 110 111 112 113 114 115
    WgMemory *memory = (WgMemory *)gst_memory;

    if (gst_memory->parent)
        return wg_allocator_map(gst_memory->parent, info, maxsize);

    GST_LOG("memory %p, info %p, maxsize %#zx", memory, info, maxsize);

116 117 118
    pthread_mutex_lock(&allocator->mutex);

    if (!memory->sample)
119
        info->data = get_unix_memory_data(memory);
120 121 122
    else
    {
        InterlockedIncrement(&memory->sample->refcount);
123
        info->data = wg_sample_data(memory->sample);
124 125 126 127 128
    }
    if (info->flags & GST_MAP_WRITE)
        memory->written = max(memory->written, maxsize);

    pthread_mutex_unlock(&allocator->mutex);
129 130 131 132 133 134 135

    GST_INFO("Mapped memory %p to %p", memory, info->data);
    return info->data;
}

static void wg_allocator_unmap(GstMemory *gst_memory, GstMapInfo *info)
{
136
    WgAllocator *allocator = (WgAllocator *)gst_memory->allocator;
137 138 139 140 141 142
    WgMemory *memory = (WgMemory *)gst_memory;

    if (gst_memory->parent)
        return wg_allocator_unmap(gst_memory->parent, info);

    GST_LOG("memory %p, info %p", memory, info);
143 144 145

    pthread_mutex_lock(&allocator->mutex);

146
    if (memory->sample && info->data == wg_sample_data(memory->sample))
147 148 149 150 151 152
    {
        InterlockedDecrement(&memory->sample->refcount);
        pthread_cond_signal(&allocator->release_cond);
    }

    pthread_mutex_unlock(&allocator->mutex);
153 154 155 156 157 158 159 160 161 162 163 164
}

static void wg_allocator_init(WgAllocator *allocator)
{
    GST_LOG("allocator %p", allocator);

    allocator->parent.mem_type = "Wine";

    allocator->parent.mem_map_full = wg_allocator_map;
    allocator->parent.mem_unmap_full = wg_allocator_unmap;

    GST_OBJECT_FLAG_SET(allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
165 166 167 168

    pthread_mutex_init(&allocator->mutex, NULL);
    pthread_cond_init(&allocator->release_cond, NULL);
    list_init(&allocator->memory_list);
169 170 171 172 173 174 175 176
}

static void wg_allocator_finalize(GObject *object)
{
    WgAllocator *allocator = (WgAllocator *)object;

    GST_LOG("allocator %p", allocator);

177 178 179
    pthread_cond_destroy(&allocator->release_cond);
    pthread_mutex_destroy(&allocator->mutex);

180 181 182 183 184 185 186 187 188 189 190 191 192 193
    G_OBJECT_CLASS(wg_allocator_parent_class)->finalize(object);
}

static GstMemory *wg_allocator_alloc(GstAllocator *gst_allocator, gsize size,
        GstAllocationParams *params)
{
    WgAllocator *allocator = (WgAllocator *)gst_allocator;
    WgMemory *memory;

    GST_LOG("allocator %p, size %#zx, params %p", allocator, size, params);

    memory = g_slice_new0(WgMemory);
    gst_memory_init(GST_MEMORY_CAST(memory), 0, GST_ALLOCATOR_CAST(allocator),
            NULL, size, 0, 0, size);
194
    memory->alloc_params = *params;
195

196 197
    pthread_mutex_lock(&allocator->mutex);

198
    memory->sample = allocator->next_sample;
199 200 201
    allocator->next_sample = NULL;

    if (memory->sample && memory->sample->max_size < size)
202
        release_memory_sample(allocator, memory, true);
203

204 205 206 207
    list_add_tail(&allocator->memory_list, &memory->entry);

    pthread_mutex_unlock(&allocator->mutex);

208
    GST_INFO("Allocated memory %p, sample %p", memory, memory->sample);
209 210 211 212 213 214 215 216 217 218
    return (GstMemory *)memory;
}

static void wg_allocator_free(GstAllocator *gst_allocator, GstMemory *gst_memory)
{
    WgAllocator *allocator = (WgAllocator *)gst_allocator;
    WgMemory *memory = (WgMemory *)gst_memory;

    GST_LOG("allocator %p, memory %p", allocator, memory);

219 220 221 222 223 224 225 226 227 228
    pthread_mutex_lock(&allocator->mutex);

    if (memory->sample)
        InterlockedDecrement(&memory->sample->refcount);
    memory->sample = NULL;

    list_remove(&memory->entry);

    pthread_mutex_unlock(&allocator->mutex);

229 230 231 232 233
    if (memory->unix_memory)
    {
        gst_memory_unmap(memory->unix_memory, &memory->unix_map_info);
        gst_memory_unref(memory->unix_memory);
    }
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    g_slice_free(WgMemory, memory);
}

static void wg_allocator_class_init(WgAllocatorClass *klass)
{
    GstAllocatorClass *parent_class = (GstAllocatorClass *)klass;
    GObjectClass *root_class = (GObjectClass *)klass;

    GST_LOG("klass %p", klass);

    parent_class->alloc = wg_allocator_alloc;
    parent_class->free = wg_allocator_free;
    root_class->finalize = wg_allocator_finalize;
}

249
GstAllocator *wg_allocator_create(void)
250 251 252 253 254 255 256 257 258
{
    WgAllocator *allocator;

    if (!(allocator = g_object_new(wg_allocator_get_type(), NULL)))
        return NULL;

    return GST_ALLOCATOR(allocator);
}

259 260 261
void wg_allocator_destroy(GstAllocator *gst_allocator)
{
    WgAllocator *allocator = (WgAllocator *)gst_allocator;
262
    WgMemory *memory;
263 264 265

    GST_LOG("allocator %p", allocator);

266 267 268 269 270
    pthread_mutex_lock(&allocator->mutex);
    LIST_FOR_EACH_ENTRY(memory, &allocator->memory_list, WgMemory, entry)
        release_memory_sample(allocator, memory, true);
    pthread_mutex_unlock(&allocator->mutex);

271 272 273 274
    g_object_unref(allocator);

    GST_INFO("Destroyed buffer allocator %p", allocator);
}
275 276 277 278 279 280 281 282 283 284 285 286

static WgMemory *find_sample_memory(WgAllocator *allocator, struct wg_sample *sample)
{
    WgMemory *memory;

    LIST_FOR_EACH_ENTRY(memory, &allocator->memory_list, WgMemory, entry)
        if (memory->sample == sample)
            return memory;

    return NULL;
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
void wg_allocator_provide_sample(GstAllocator *gst_allocator, struct wg_sample *sample)
{
    WgAllocator *allocator = (WgAllocator *)gst_allocator;
    struct wg_sample *previous;

    GST_LOG("allocator %p, sample %p", allocator, sample);

    if (sample)
        InterlockedIncrement(&sample->refcount);

    pthread_mutex_lock(&allocator->mutex);
    previous = allocator->next_sample;
    allocator->next_sample = sample;
    pthread_mutex_unlock(&allocator->mutex);

    if (previous)
        InterlockedDecrement(&previous->refcount);
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
void wg_allocator_release_sample(GstAllocator *gst_allocator, struct wg_sample *sample,
        bool discard_data)
{
    WgAllocator *allocator = (WgAllocator *)gst_allocator;
    WgMemory *memory;

    GST_LOG("allocator %p, sample %p, discard_data %u", allocator, sample, discard_data);

    pthread_mutex_lock(&allocator->mutex);
    if ((memory = find_sample_memory(allocator, sample)))
        release_memory_sample(allocator, memory, discard_data);
    else if (sample->refcount)
        GST_ERROR("Couldn't find memory for sample %p", sample);
    pthread_mutex_unlock(&allocator->mutex);
}