summaryrefslogtreecommitdiff
path: root/gst-libs/gst/vaapi/gstvaapitexturemap.c
blob: 0a17bac9fbdbecdab0cc27c3b8644409b3bf3709 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 *  gstvaapitexture.c - VA texture Hash map
 *
 *  Copyright (C) 2016 Intel Corporation
 *  Copyright (C) 2016 Igalia S.L.
 *
 *  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 Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

/**
 * SECTION:gstvaapitexturemap
 * @short_description: VA/GLX/EGL texture hash map abstraction
 */

#include "gstvaapitexturemap.h"

#define DEBUG 1
#include "gstvaapidebug.h"

/**
 * GstVaapiTextureMap:
 *
 * Base class for API-dependent texture map.
 */
struct _GstVaapiTextureMap
{
  GstObject parent_instance;

  /*< private > */
  GHashTable *texture_map;
};

/**
 * GstVaapiTextureMapClass:
 *
 * Base class for API-dependent texture map.
 */
struct _GstVaapiTextureMapClass
{
  GstObjectClass parent_class;
};

#define MAX_NUM_TEXTURE 10

G_DEFINE_TYPE (GstVaapiTextureMap, gst_vaapi_texture_map, GST_TYPE_OBJECT);

static void
gst_vaapi_texture_map_init (GstVaapiTextureMap * map)
{
  map->texture_map =
      g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
      (GDestroyNotify) gst_vaapi_texture_unref);
}

static void
gst_vaapi_texture_map_finalize (GObject * object)
{
  GstVaapiTextureMap *map = GST_VAAPI_TEXTURE_MAP (object);

  if (map->texture_map) {
    g_hash_table_remove_all (map->texture_map);
    g_hash_table_destroy (map->texture_map);
  }

  G_OBJECT_CLASS (gst_vaapi_texture_map_parent_class)->finalize (object);
}

static void
gst_vaapi_texture_map_class_init (GstVaapiTextureMapClass * klass)
{
  GObjectClass *const object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gst_vaapi_texture_map_finalize;
}

/**
 * gst_vaapi_texture_map_new:
 *
 * Creates a texture hash map.
 *
 * Return value: the newly created #GstVaapiTextureMap object
 */
GstVaapiTextureMap *
gst_vaapi_texture_map_new (void)
{
  GstVaapiTextureMap *map;

  map = g_object_new (GST_TYPE_VAAPI_TEXTURE_MAP, NULL);
  return map;
}

/**
 * gst_vaapi_texture_map_add:
 * @map: a #GstVaapiTextureMap instance
 * @texture: a #GstVaapiTexture instance to add
 * @id: the id of the GLTexture
 *
 * Adds @texture into the @map table.
 *
 * Returns: %TRUE if @texture was inserted correctly.
 **/
gboolean
gst_vaapi_texture_map_add (GstVaapiTextureMap * map, GstVaapiTexture * texture,
    guint id)
{
  g_return_val_if_fail (map != NULL, FALSE);
  g_return_val_if_fail (map->texture_map != NULL, FALSE);
  g_return_val_if_fail (texture != NULL, FALSE);

  if (g_hash_table_size (map->texture_map) > MAX_NUM_TEXTURE) {
    GST_WARNING ("Texture map is full");
    return FALSE;
  }

  g_hash_table_insert (map->texture_map, GUINT_TO_POINTER (id), texture);

  return TRUE;
}

/**
 * gst_vaapi_texture_map_lookup:
 * @map: a #GstVaapiTextureMap instance
 * @id: the id of the GLTexture
 *
 * Search for the #GstVaapiTexture associated with the GLTexture @id
 * in the @map.
 *
 * Returns: a pointer to #GstVaapiTexture if found; otherwise %NULL.
 **/
GstVaapiTexture *
gst_vaapi_texture_map_lookup (GstVaapiTextureMap * map, guint id)
{
  g_return_val_if_fail (map != NULL, NULL);
  g_return_val_if_fail (map->texture_map != NULL, NULL);

  return g_hash_table_lookup (map->texture_map, GUINT_TO_POINTER (id));
}

/**
 * gst_vaapi_texture_map_reset:
 * @map: a #GstVaapiTextureMap instance
 *
 * Removes all the #GstVaapiTexture in the @map.
 **/
void
gst_vaapi_texture_map_reset (GstVaapiTextureMap * map)
{
  g_return_if_fail (map != NULL);
  g_return_if_fail (map->texture_map != NULL);

  g_hash_table_remove_all (map->texture_map);
}