~ubuntu-branches/ubuntu/quantal/gstreamer-vaapi/quantal

« back to all changes in this revision

Viewing changes to gst-libs/gst/vaapi/gstvaapidisplaycache.c

  • Committer: Package Import Robot
  • Author(s): Timo Aaltonen
  • Date: 2012-02-10 14:35:09 UTC
  • Revision ID: package-import@ubuntu.com-20120210143509-wq9j8uqb5leu1iik
Tags: upstream-0.3.4
ImportĀ upstreamĀ versionĀ 0.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  gstvaapidisplaycache.c - VA display cache
 
3
 *
 
4
 *  Copyright (C) 2012 Intel Corporation
 
5
 *
 
6
 *  This library is free software; you can redistribute it and/or
 
7
 *  modify it under the terms of the GNU Lesser General Public License
 
8
 *  as published by the Free Software Foundation; either version 2.1
 
9
 *  of the License, or (at your option) any later version.
 
10
 *
 
11
 *  This library is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 *  Lesser General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU Lesser General Public
 
17
 *  License along with this library; if not, write to the Free
 
18
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
 *  Boston, MA 02110-1301 USA
 
20
 */
 
21
 
 
22
#include "sysdeps.h"
 
23
#include <glib.h>
 
24
#include <string.h>
 
25
#include "gstvaapidisplaycache.h"
 
26
 
 
27
#define DEBUG 1
 
28
#include "gstvaapidebug.h"
 
29
 
 
30
typedef struct _CacheEntry CacheEntry;
 
31
struct _CacheEntry {
 
32
    GstVaapiDisplayInfo info;
 
33
};
 
34
 
 
35
struct _GstVaapiDisplayCache {
 
36
    GStaticMutex        mutex;
 
37
    GList              *list;
 
38
};
 
39
 
 
40
static void
 
41
cache_entry_free(CacheEntry *entry)
 
42
{
 
43
    GstVaapiDisplayInfo *info;
 
44
 
 
45
    if (!entry)
 
46
        return;
 
47
 
 
48
    info = &entry->info;
 
49
 
 
50
    if (info->display_name) {
 
51
        g_free(info->display_name);
 
52
        info->display_name = NULL;
 
53
    }
 
54
    g_slice_free(CacheEntry, entry);
 
55
}
 
56
 
 
57
static CacheEntry *
 
58
cache_entry_new(const GstVaapiDisplayInfo *di)
 
59
{
 
60
    GstVaapiDisplayInfo *info;
 
61
    CacheEntry *entry;
 
62
 
 
63
    entry = g_slice_new(CacheEntry);
 
64
    if (!entry)
 
65
        return NULL;
 
66
 
 
67
    info                 = &entry->info;
 
68
    info->display        = di->display;
 
69
    info->va_display     = di->va_display;
 
70
    info->native_display = di->native_display;
 
71
    info->display_name   = NULL;
 
72
 
 
73
    if (di->display_name) {
 
74
        info->display_name = g_strdup(di->display_name);
 
75
        if (!info->display_name)
 
76
            goto error;
 
77
    }
 
78
    return entry;
 
79
 
 
80
error:
 
81
    cache_entry_free(entry);
 
82
    return NULL;
 
83
}
 
84
 
 
85
#define CACHE_LOOKUP(cache, res, prop, comp_func, comp_data, user_data) do { \
 
86
        GList *l;                                                       \
 
87
                                                                        \
 
88
        g_static_mutex_lock(&(cache)->mutex);                           \
 
89
        for (l = (cache)->list; l != NULL; l = l->next) {               \
 
90
            GstVaapiDisplayInfo * const info =                          \
 
91
                &((CacheEntry *)l->data)->info;                         \
 
92
            if (comp_func(info->prop, comp_data, user_data))            \
 
93
                break;                                                  \
 
94
        }                                                               \
 
95
        g_static_mutex_unlock(&(cache)->mutex);                         \
 
96
        res = l;                                                        \
 
97
    } while (0)
 
98
 
 
99
#define compare_equal(a, b, user_data) \
 
100
    ((a) == (b))
 
101
 
 
102
#define compare_string(a, b, user_data) \
 
103
    ((a) == (b) || ((a) && (b) && strcmp(a, b) == 0))
 
104
 
 
105
static GList *
 
106
cache_lookup_display(GstVaapiDisplayCache *cache, GstVaapiDisplay *display)
 
107
{
 
108
    GList *m;
 
109
 
 
110
    CACHE_LOOKUP(cache, m, display, compare_equal, display, NULL);
 
111
    return m;
 
112
}
 
113
 
 
114
static GList *
 
115
cache_lookup_va_display(GstVaapiDisplayCache *cache, VADisplay va_display)
 
116
{
 
117
    GList *m;
 
118
 
 
119
    CACHE_LOOKUP(cache, m, va_display, compare_equal, va_display, NULL);
 
120
    return m;
 
121
}
 
122
 
 
123
static GList *
 
124
cache_lookup_native_display(GstVaapiDisplayCache *cache, gpointer native_display)
 
125
{
 
126
    GList *m;
 
127
 
 
128
    CACHE_LOOKUP(cache, m, native_display, compare_equal, native_display, NULL);
 
129
    return m;
 
130
}
 
131
 
 
132
/**
 
133
 * gst_vaapi_display_cache_new:
 
134
 *
 
135
 * Creates a new VA display cache.
 
136
 *
 
137
 * Return value: the newly created #GstVaapiDisplayCache object
 
138
 */
 
139
GstVaapiDisplayCache *
 
140
gst_vaapi_display_cache_new(void)
 
141
{
 
142
    GstVaapiDisplayCache *cache;
 
143
 
 
144
    cache = g_slice_new0(GstVaapiDisplayCache);
 
145
    if (!cache)
 
146
        return NULL;
 
147
 
 
148
    g_static_mutex_init(&cache->mutex);
 
149
    return cache;
 
150
}
 
151
 
 
152
/**
 
153
 * gst_vaapi_display_cache_new:
 
154
 * @cache: the #GstVaapiDisplayCache to destroy
 
155
 *
 
156
 * Destroys a VA display cache.
 
157
 */
 
158
void
 
159
gst_vaapi_display_cache_free(GstVaapiDisplayCache *cache)
 
160
{
 
161
    GList *l;
 
162
 
 
163
    if (!cache)
 
164
        return;
 
165
 
 
166
    if (cache->list) {
 
167
        for (l = cache->list; l != NULL; l = l->next)
 
168
            cache_entry_free(l->data);
 
169
        g_list_free(cache->list);
 
170
        cache->list = NULL;
 
171
    }
 
172
    g_slice_free(GstVaapiDisplayCache, cache);
 
173
}
 
174
 
 
175
/**
 
176
 * gst_vaapi_display_cache_get_size:
 
177
 * @cache: the #GstVaapiDisplayCache
 
178
 *
 
179
 * Gets the size of the display cache @cache.
 
180
 *
 
181
 * Return value: the size of the display cache
 
182
 */
 
183
guint
 
184
gst_vaapi_display_cache_get_size(GstVaapiDisplayCache *cache)
 
185
{
 
186
    guint size;
 
187
 
 
188
    g_return_val_if_fail(cache != NULL, 0);
 
189
 
 
190
    g_static_mutex_lock(&cache->mutex);
 
191
    size = g_list_length(cache->list);
 
192
    g_static_mutex_unlock(&cache->mutex);
 
193
    return size;
 
194
}
 
195
 
 
196
/**
 
197
 * gst_vaapi_display_cache_add:
 
198
 * @cache: the #GstVaapiDisplayCache
 
199
 * @info: the display cache info to add
 
200
 *
 
201
 * Adds a new entry with data from @info. The display @info data is
 
202
 * copied into the newly created cache entry.
 
203
 *
 
204
 * Return value: %TRUE on success
 
205
 */
 
206
gboolean
 
207
gst_vaapi_display_cache_add(
 
208
    GstVaapiDisplayCache       *cache,
 
209
    GstVaapiDisplayInfo        *info
 
210
)
 
211
{
 
212
    CacheEntry *entry;
 
213
 
 
214
    g_return_val_if_fail(cache != NULL, FALSE);
 
215
    g_return_val_if_fail(info != NULL, FALSE);
 
216
 
 
217
    entry = cache_entry_new(info);
 
218
    if (!entry)
 
219
        return FALSE;
 
220
 
 
221
    g_static_mutex_lock(&cache->mutex);
 
222
    cache->list = g_list_prepend(cache->list, entry);
 
223
    g_static_mutex_unlock(&cache->mutex);
 
224
    return TRUE;
 
225
}
 
226
 
 
227
/**
 
228
 * gst_vaapi_display_cache_remove:
 
229
 * @cache: the #GstVaapiDisplayCache
 
230
 * @display: the display to remove from cache
 
231
 *
 
232
 * Removes any cache entry that matches the specified #GstVaapiDisplay.
 
233
 */
 
234
void
 
235
gst_vaapi_display_cache_remove(
 
236
    GstVaapiDisplayCache       *cache,
 
237
    GstVaapiDisplay            *display
 
238
)
 
239
{
 
240
    GList *m;
 
241
 
 
242
    m = cache_lookup_display(cache, display);
 
243
    if (!m)
 
244
        return;
 
245
 
 
246
    cache_entry_free(m->data);
 
247
    g_static_mutex_lock(&cache->mutex);
 
248
    cache->list = g_list_delete_link(cache->list, m);
 
249
    g_static_mutex_unlock(&cache->mutex);
 
250
}
 
251
 
 
252
/**
 
253
 * gst_vaapi_display_cache_lookup:
 
254
 * @cache: the #GstVaapiDisplayCache
 
255
 * @display: the display to find
 
256
 *
 
257
 * Looks up the display cache for the specified #GstVaapiDisplay.
 
258
 *
 
259
 * Return value: a #GstVaapiDisplayInfo matching @display, or %NULL if
 
260
 *   none was found
 
261
 */
 
262
const GstVaapiDisplayInfo *
 
263
gst_vaapi_display_cache_lookup(
 
264
    GstVaapiDisplayCache       *cache,
 
265
    GstVaapiDisplay            *display
 
266
)
 
267
{
 
268
    CacheEntry *entry;
 
269
    GList *m;
 
270
 
 
271
    g_return_val_if_fail(cache != NULL, NULL);
 
272
    g_return_val_if_fail(display != NULL, NULL);
 
273
 
 
274
    m = cache_lookup_display(cache, display);
 
275
    if (!m)
 
276
        return NULL;
 
277
 
 
278
    entry = m->data;
 
279
    return &entry->info;
 
280
}
 
281
 
 
282
/**
 
283
 * gst_vaapi_display_cache_lookup_by_va_display:
 
284
 * @cache: the #GstVaapiDisplayCache
 
285
 * @va_display: the VA display to find
 
286
 *
 
287
 * Looks up the display cache for the specified VA display.
 
288
 *
 
289
 * Return value: a #GstVaapiDisplayInfo matching @va_display, or %NULL
 
290
 *   if none was found
 
291
 */
 
292
const GstVaapiDisplayInfo *
 
293
gst_vaapi_display_cache_lookup_by_va_display(
 
294
    GstVaapiDisplayCache       *cache,
 
295
    VADisplay                   va_display
 
296
)
 
297
{
 
298
    CacheEntry *entry;
 
299
    GList *m;
 
300
 
 
301
    g_return_val_if_fail(cache != NULL, NULL);
 
302
    g_return_val_if_fail(va_display != NULL, NULL);
 
303
 
 
304
    m = cache_lookup_va_display(cache, va_display);
 
305
    if (!m)
 
306
        return NULL;
 
307
 
 
308
    entry = m->data;
 
309
    return &entry->info;
 
310
}
 
311
 
 
312
/**
 
313
 * gst_vaapi_display_cache_lookup_by_native_display:
 
314
 * @cache: the #GstVaapiDisplayCache
 
315
 * @native_display: the native display to find
 
316
 *
 
317
 * Looks up the display cache for the specified native display.
 
318
 *
 
319
 * Return value: a #GstVaapiDisplayInfo matching @native_display, or
 
320
 *   %NULL if none was found
 
321
 */
 
322
const GstVaapiDisplayInfo *
 
323
gst_vaapi_display_cache_lookup_by_native_display(
 
324
    GstVaapiDisplayCache       *cache,
 
325
    gpointer                    native_display
 
326
)
 
327
{
 
328
    CacheEntry *entry;
 
329
    GList *m;
 
330
 
 
331
    g_return_val_if_fail(cache != NULL, NULL);
 
332
    g_return_val_if_fail(native_display != NULL, NULL);
 
333
 
 
334
    m = cache_lookup_native_display(cache, native_display);
 
335
    if (!m)
 
336
        return NULL;
 
337
 
 
338
    entry = m->data;
 
339
    return &entry->info;
 
340
}
 
341
 
 
342
/**
 
343
 * gst_vaapi_display_cache_lookup_by_name:
 
344
 * @cache: the #GstVaapiDisplayCache
 
345
 * @display_name: the display name to match
 
346
 * @compare_func: an optional string comparison function
 
347
 * @user_data: any relevant data pointer to the comparison function
 
348
 *
 
349
 * Looks up the display cache for the specified display name. A
 
350
 * specific comparison function can be provided to avoid a plain
 
351
 * strcmp().
 
352
 *
 
353
 * Return value: a #GstVaapiDisplayInfo matching @display_name, or
 
354
 *   %NULL if none was found
 
355
 */
 
356
const GstVaapiDisplayInfo *
 
357
gst_vaapi_display_cache_lookup_by_name(
 
358
    GstVaapiDisplayCache       *cache,
 
359
    const gchar                *display_name,
 
360
    GCompareDataFunc            compare_func,
 
361
    gpointer                    user_data
 
362
)
 
363
{
 
364
    CacheEntry *entry;
 
365
    GList *m;
 
366
 
 
367
    g_return_val_if_fail(cache != NULL, NULL);
 
368
 
 
369
    if (compare_func)
 
370
        CACHE_LOOKUP(cache, m, display_name, compare_func, display_name, user_data);
 
371
    else
 
372
        CACHE_LOOKUP(cache, m, display_name, compare_string, display_name, NULL);
 
373
    if (!m)
 
374
        return NULL;
 
375
 
 
376
    entry = m->data;
 
377
    return &entry->info;
 
378
}