1 /*
2  *  gstvaapitexture.c - VA texture Hash map
3  *
4  *  Copyright (C) 2016 Intel Corporation
5  *  Copyright (C) 2016 Igalia S.L.
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public License
9  *  as published by the Free Software Foundation; either version 2.1
10  *  of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free
19  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  *  Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * SECTION:gstvaapitexturemap
25  * @short_description: VA/GLX/EGL texture hash map abstraction
26  */
27 
28 #include "gstvaapitexturemap.h"
29 
30 #define DEBUG 1
31 #include "gstvaapidebug.h"
32 
33 /**
34  * GstVaapiTextureMap:
35  *
36  * Base class for API-dependent texture map.
37  */
38 struct _GstVaapiTextureMap
39 {
40   GstObject parent_instance;
41 
42   /*< private > */
43   GHashTable *texture_map;
44 };
45 
46 /**
47  * GstVaapiTextureMapClass:
48  *
49  * Base class for API-dependent texture map.
50  */
51 struct _GstVaapiTextureMapClass
52 {
53   GstObjectClass parent_class;
54 };
55 
56 #define MAX_NUM_TEXTURE 10
57 
58 G_DEFINE_TYPE (GstVaapiTextureMap, gst_vaapi_texture_map, GST_TYPE_OBJECT);
59 
60 static void
gst_vaapi_texture_map_init(GstVaapiTextureMap * map)61 gst_vaapi_texture_map_init (GstVaapiTextureMap * map)
62 {
63   map->texture_map =
64       g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
65       (GDestroyNotify) gst_vaapi_texture_unref);
66 }
67 
68 static void
gst_vaapi_texture_map_finalize(GObject * object)69 gst_vaapi_texture_map_finalize (GObject * object)
70 {
71   GstVaapiTextureMap *map = GST_VAAPI_TEXTURE_MAP (object);
72 
73   if (map->texture_map) {
74     g_hash_table_remove_all (map->texture_map);
75     g_hash_table_destroy (map->texture_map);
76   }
77 
78   G_OBJECT_CLASS (gst_vaapi_texture_map_parent_class)->finalize (object);
79 }
80 
81 static void
gst_vaapi_texture_map_class_init(GstVaapiTextureMapClass * klass)82 gst_vaapi_texture_map_class_init (GstVaapiTextureMapClass * klass)
83 {
84   GObjectClass *const object_class = G_OBJECT_CLASS (klass);
85 
86   object_class->finalize = gst_vaapi_texture_map_finalize;
87 }
88 
89 /**
90  * gst_vaapi_texture_map_new:
91  *
92  * Creates a texture hash map.
93  *
94  * Return value: the newly created #GstVaapiTextureMap object
95  */
96 GstVaapiTextureMap *
gst_vaapi_texture_map_new(void)97 gst_vaapi_texture_map_new (void)
98 {
99   GstVaapiTextureMap *map;
100 
101   map = g_object_new (GST_TYPE_VAAPI_TEXTURE_MAP, NULL);
102   return map;
103 }
104 
105 /**
106  * gst_vaapi_texture_map_add:
107  * @map: a #GstVaapiTextureMap instance
108  * @texture: a #GstVaapiTexture instance to add
109  * @id: the id of the GLTexture
110  *
111  * Adds @texture into the @map table.
112  *
113  * Returns: %TRUE if @texture was inserted correctly.
114  **/
115 gboolean
gst_vaapi_texture_map_add(GstVaapiTextureMap * map,GstVaapiTexture * texture,guint id)116 gst_vaapi_texture_map_add (GstVaapiTextureMap * map, GstVaapiTexture * texture,
117     guint id)
118 {
119   g_return_val_if_fail (map != NULL, FALSE);
120   g_return_val_if_fail (map->texture_map != NULL, FALSE);
121   g_return_val_if_fail (texture != NULL, FALSE);
122 
123   if (g_hash_table_size (map->texture_map) > MAX_NUM_TEXTURE) {
124     GST_WARNING ("Texture map is full");
125     return FALSE;
126   }
127 
128   g_hash_table_insert (map->texture_map, GUINT_TO_POINTER (id), texture);
129 
130   return TRUE;
131 }
132 
133 /**
134  * gst_vaapi_texture_map_lookup:
135  * @map: a #GstVaapiTextureMap instance
136  * @id: the id of the GLTexture
137  *
138  * Search for the #GstVaapiTexture associated with the GLTexture @id
139  * in the @map.
140  *
141  * Returns: a pointer to #GstVaapiTexture if found; otherwise %NULL.
142  **/
143 GstVaapiTexture *
gst_vaapi_texture_map_lookup(GstVaapiTextureMap * map,guint id)144 gst_vaapi_texture_map_lookup (GstVaapiTextureMap * map, guint id)
145 {
146   g_return_val_if_fail (map != NULL, NULL);
147   g_return_val_if_fail (map->texture_map != NULL, NULL);
148 
149   return g_hash_table_lookup (map->texture_map, GUINT_TO_POINTER (id));
150 }
151 
152 /**
153  * gst_vaapi_texture_map_reset:
154  * @map: a #GstVaapiTextureMap instance
155  *
156  * Removes all the #GstVaapiTexture in the @map.
157  **/
158 void
gst_vaapi_texture_map_reset(GstVaapiTextureMap * map)159 gst_vaapi_texture_map_reset (GstVaapiTextureMap * map)
160 {
161   g_return_if_fail (map != NULL);
162   g_return_if_fail (map->texture_map != NULL);
163 
164   g_hash_table_remove_all (map->texture_map);
165 }
166