2 * gstvaapidisplaycache.c - VA display cache
4 * Copyright (C) 2012 Intel Corporation
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.
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.
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
25 #include "gstvaapidisplaycache.h"
28 #include "gstvaapidebug.h"
30 typedef struct _CacheEntry CacheEntry;
32 GstVaapiDisplayInfo info;
35 struct _GstVaapiDisplayCache {
41 cache_entry_free(CacheEntry *entry)
43 GstVaapiDisplayInfo *info;
50 if (info->display_name) {
51 g_free(info->display_name);
52 info->display_name = NULL;
54 g_slice_free(CacheEntry, entry);
58 cache_entry_new(const GstVaapiDisplayInfo *di)
60 GstVaapiDisplayInfo *info;
63 entry = g_slice_new(CacheEntry);
68 info->display = di->display;
69 info->va_display = di->va_display;
70 info->native_display = di->native_display;
71 info->display_type = di->display_type;
72 info->display_name = NULL;
74 if (di->display_name) {
75 info->display_name = g_strdup(di->display_name);
76 if (!info->display_name)
82 cache_entry_free(entry);
86 #define CACHE_LOOKUP(cache, res, prop, comp_func, comp_data, user_data) do { \
89 g_static_mutex_lock(&(cache)->mutex); \
90 for (l = (cache)->list; l != NULL; l = l->next) { \
91 GstVaapiDisplayInfo * const info = \
92 &((CacheEntry *)l->data)->info; \
93 if (comp_func(info->prop, comp_data, user_data)) \
96 g_static_mutex_unlock(&(cache)->mutex); \
100 #define compare_equal(a, b, user_data) \
103 #define compare_string(a, b, user_data) \
104 ((a) == (b) || ((a) && (b) && strcmp(a, b) == 0))
107 cache_lookup_display(GstVaapiDisplayCache *cache, GstVaapiDisplay *display)
111 CACHE_LOOKUP(cache, m, display, compare_equal, display, NULL);
116 cache_lookup_va_display(GstVaapiDisplayCache *cache, VADisplay va_display)
120 CACHE_LOOKUP(cache, m, va_display, compare_equal, va_display, NULL);
125 cache_lookup_native_display(GstVaapiDisplayCache *cache, gpointer native_display)
129 CACHE_LOOKUP(cache, m, native_display, compare_equal, native_display, NULL);
134 * gst_vaapi_display_cache_new:
136 * Creates a new VA display cache.
138 * Return value: the newly created #GstVaapiDisplayCache object
140 GstVaapiDisplayCache *
141 gst_vaapi_display_cache_new(void)
143 GstVaapiDisplayCache *cache;
145 cache = g_slice_new0(GstVaapiDisplayCache);
149 g_static_mutex_init(&cache->mutex);
154 * gst_vaapi_display_cache_new:
155 * @cache: the #GstVaapiDisplayCache to destroy
157 * Destroys a VA display cache.
160 gst_vaapi_display_cache_free(GstVaapiDisplayCache *cache)
168 for (l = cache->list; l != NULL; l = l->next)
169 cache_entry_free(l->data);
170 g_list_free(cache->list);
173 g_static_mutex_free(&cache->mutex);
174 g_slice_free(GstVaapiDisplayCache, cache);
178 * gst_vaapi_display_cache_get_size:
179 * @cache: the #GstVaapiDisplayCache
181 * Gets the size of the display cache @cache.
183 * Return value: the size of the display cache
186 gst_vaapi_display_cache_get_size(GstVaapiDisplayCache *cache)
190 g_return_val_if_fail(cache != NULL, 0);
192 g_static_mutex_lock(&cache->mutex);
193 size = g_list_length(cache->list);
194 g_static_mutex_unlock(&cache->mutex);
199 * gst_vaapi_display_cache_add:
200 * @cache: the #GstVaapiDisplayCache
201 * @info: the display cache info to add
203 * Adds a new entry with data from @info. The display @info data is
204 * copied into the newly created cache entry.
206 * Return value: %TRUE on success
209 gst_vaapi_display_cache_add(
210 GstVaapiDisplayCache *cache,
211 GstVaapiDisplayInfo *info
216 g_return_val_if_fail(cache != NULL, FALSE);
217 g_return_val_if_fail(info != NULL, FALSE);
219 entry = cache_entry_new(info);
223 g_static_mutex_lock(&cache->mutex);
224 cache->list = g_list_prepend(cache->list, entry);
225 g_static_mutex_unlock(&cache->mutex);
230 * gst_vaapi_display_cache_remove:
231 * @cache: the #GstVaapiDisplayCache
232 * @display: the display to remove from cache
234 * Removes any cache entry that matches the specified #GstVaapiDisplay.
237 gst_vaapi_display_cache_remove(
238 GstVaapiDisplayCache *cache,
239 GstVaapiDisplay *display
244 m = cache_lookup_display(cache, display);
248 cache_entry_free(m->data);
249 g_static_mutex_lock(&cache->mutex);
250 cache->list = g_list_delete_link(cache->list, m);
251 g_static_mutex_unlock(&cache->mutex);
255 * gst_vaapi_display_cache_lookup:
256 * @cache: the #GstVaapiDisplayCache
257 * @display: the display to find
259 * Looks up the display cache for the specified #GstVaapiDisplay.
261 * Return value: a #GstVaapiDisplayInfo matching @display, or %NULL if
264 const GstVaapiDisplayInfo *
265 gst_vaapi_display_cache_lookup(
266 GstVaapiDisplayCache *cache,
267 GstVaapiDisplay *display
273 g_return_val_if_fail(cache != NULL, NULL);
274 g_return_val_if_fail(display != NULL, NULL);
276 m = cache_lookup_display(cache, display);
285 * gst_vaapi_display_cache_lookup_by_va_display:
286 * @cache: the #GstVaapiDisplayCache
287 * @va_display: the VA display to find
289 * Looks up the display cache for the specified VA display.
291 * Return value: a #GstVaapiDisplayInfo matching @va_display, or %NULL
294 const GstVaapiDisplayInfo *
295 gst_vaapi_display_cache_lookup_by_va_display(
296 GstVaapiDisplayCache *cache,
303 g_return_val_if_fail(cache != NULL, NULL);
304 g_return_val_if_fail(va_display != NULL, NULL);
306 m = cache_lookup_va_display(cache, va_display);
315 * gst_vaapi_display_cache_lookup_by_native_display:
316 * @cache: the #GstVaapiDisplayCache
317 * @native_display: the native display to find
319 * Looks up the display cache for the specified native display.
321 * Return value: a #GstVaapiDisplayInfo matching @native_display, or
322 * %NULL if none was found
324 const GstVaapiDisplayInfo *
325 gst_vaapi_display_cache_lookup_by_native_display(
326 GstVaapiDisplayCache *cache,
327 gpointer native_display
333 g_return_val_if_fail(cache != NULL, NULL);
334 g_return_val_if_fail(native_display != NULL, NULL);
336 m = cache_lookup_native_display(cache, native_display);
345 * gst_vaapi_display_cache_lookup_by_name:
346 * @cache: the #GstVaapiDisplayCache
347 * @display_name: the display name to match
348 * @compare_func: an optional string comparison function
349 * @user_data: any relevant data pointer to the comparison function
351 * Looks up the display cache for the specified display name. A
352 * specific comparison function can be provided to avoid a plain
355 * Return value: a #GstVaapiDisplayInfo matching @display_name, or
356 * %NULL if none was found
358 const GstVaapiDisplayInfo *
359 gst_vaapi_display_cache_lookup_by_name(
360 GstVaapiDisplayCache *cache,
361 const gchar *display_name,
362 GCompareDataFunc compare_func,
369 g_return_val_if_fail(cache != NULL, NULL);
372 CACHE_LOOKUP(cache, m, display_name, compare_func, display_name, user_data);
374 CACHE_LOOKUP(cache, m, display_name, compare_string, display_name, NULL);