xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/vdpau/htab.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /**************************************************************************
2*61046927SAndroid Build Coastguard Worker  *
3*61046927SAndroid Build Coastguard Worker  * Copyright 2010 Younes Manton.
4*61046927SAndroid Build Coastguard Worker  * All Rights Reserved.
5*61046927SAndroid Build Coastguard Worker  *
6*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
7*61046927SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the
8*61046927SAndroid Build Coastguard Worker  * "Software"), to deal in the Software without restriction, including
9*61046927SAndroid Build Coastguard Worker  * without limitation the rights to use, copy, modify, merge, publish,
10*61046927SAndroid Build Coastguard Worker  * distribute, sub license, and/or sell copies of the Software, and to
11*61046927SAndroid Build Coastguard Worker  * permit persons to whom the Software is furnished to do so, subject to
12*61046927SAndroid Build Coastguard Worker  * the following conditions:
13*61046927SAndroid Build Coastguard Worker  *
14*61046927SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the
15*61046927SAndroid Build Coastguard Worker  * next paragraph) shall be included in all copies or substantial portions
16*61046927SAndroid Build Coastguard Worker  * of the Software.
17*61046927SAndroid Build Coastguard Worker  *
18*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19*61046927SAndroid Build Coastguard Worker  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20*61046927SAndroid Build Coastguard Worker  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21*61046927SAndroid Build Coastguard Worker  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22*61046927SAndroid Build Coastguard Worker  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23*61046927SAndroid Build Coastguard Worker  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24*61046927SAndroid Build Coastguard Worker  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25*61046927SAndroid Build Coastguard Worker  *
26*61046927SAndroid Build Coastguard Worker  **************************************************************************/
27*61046927SAndroid Build Coastguard Worker 
28*61046927SAndroid Build Coastguard Worker #include "util/simple_mtx.h"
29*61046927SAndroid Build Coastguard Worker #include "util/u_handle_table.h"
30*61046927SAndroid Build Coastguard Worker #include "util/u_thread.h"
31*61046927SAndroid Build Coastguard Worker #include "vdpau_private.h"
32*61046927SAndroid Build Coastguard Worker 
33*61046927SAndroid Build Coastguard Worker static struct handle_table *htab = NULL;
34*61046927SAndroid Build Coastguard Worker static simple_mtx_t htab_lock = SIMPLE_MTX_INITIALIZER;
35*61046927SAndroid Build Coastguard Worker 
vlCreateHTAB(void)36*61046927SAndroid Build Coastguard Worker bool vlCreateHTAB(void)
37*61046927SAndroid Build Coastguard Worker {
38*61046927SAndroid Build Coastguard Worker    bool ret;
39*61046927SAndroid Build Coastguard Worker 
40*61046927SAndroid Build Coastguard Worker    /* Make sure handle table handles match VDPAU handles. */
41*61046927SAndroid Build Coastguard Worker    assert(sizeof(unsigned) <= sizeof(vlHandle));
42*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&htab_lock);
43*61046927SAndroid Build Coastguard Worker    if (!htab)
44*61046927SAndroid Build Coastguard Worker       htab = handle_table_create();
45*61046927SAndroid Build Coastguard Worker    ret = htab != NULL;
46*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&htab_lock);
47*61046927SAndroid Build Coastguard Worker    return ret;
48*61046927SAndroid Build Coastguard Worker }
49*61046927SAndroid Build Coastguard Worker 
vlDestroyHTAB(void)50*61046927SAndroid Build Coastguard Worker void vlDestroyHTAB(void)
51*61046927SAndroid Build Coastguard Worker {
52*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&htab_lock);
53*61046927SAndroid Build Coastguard Worker    if (htab && !handle_table_get_first_handle(htab)) {
54*61046927SAndroid Build Coastguard Worker       handle_table_destroy(htab);
55*61046927SAndroid Build Coastguard Worker       htab = NULL;
56*61046927SAndroid Build Coastguard Worker    }
57*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&htab_lock);
58*61046927SAndroid Build Coastguard Worker }
59*61046927SAndroid Build Coastguard Worker 
vlAddDataHTAB(void * data)60*61046927SAndroid Build Coastguard Worker vlHandle vlAddDataHTAB(void *data)
61*61046927SAndroid Build Coastguard Worker {
62*61046927SAndroid Build Coastguard Worker    vlHandle handle = 0;
63*61046927SAndroid Build Coastguard Worker 
64*61046927SAndroid Build Coastguard Worker    assert(data);
65*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&htab_lock);
66*61046927SAndroid Build Coastguard Worker    if (htab)
67*61046927SAndroid Build Coastguard Worker       handle = handle_table_add(htab, data);
68*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&htab_lock);
69*61046927SAndroid Build Coastguard Worker    return handle;
70*61046927SAndroid Build Coastguard Worker }
71*61046927SAndroid Build Coastguard Worker 
vlGetDataHTAB(vlHandle handle)72*61046927SAndroid Build Coastguard Worker void* vlGetDataHTAB(vlHandle handle)
73*61046927SAndroid Build Coastguard Worker {
74*61046927SAndroid Build Coastguard Worker    void *data = NULL;
75*61046927SAndroid Build Coastguard Worker 
76*61046927SAndroid Build Coastguard Worker    assert(handle);
77*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&htab_lock);
78*61046927SAndroid Build Coastguard Worker    if (htab)
79*61046927SAndroid Build Coastguard Worker       data = handle_table_get(htab, handle);
80*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&htab_lock);
81*61046927SAndroid Build Coastguard Worker    return data;
82*61046927SAndroid Build Coastguard Worker }
83*61046927SAndroid Build Coastguard Worker 
vlRemoveDataHTAB(vlHandle handle)84*61046927SAndroid Build Coastguard Worker void vlRemoveDataHTAB(vlHandle handle)
85*61046927SAndroid Build Coastguard Worker {
86*61046927SAndroid Build Coastguard Worker    simple_mtx_lock(&htab_lock);
87*61046927SAndroid Build Coastguard Worker    if (htab)
88*61046927SAndroid Build Coastguard Worker       handle_table_remove(htab, handle);
89*61046927SAndroid Build Coastguard Worker    simple_mtx_unlock(&htab_lock);
90*61046927SAndroid Build Coastguard Worker }
91