Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / util / u_surfaces.c
1 #include "u_surfaces.h"
2 #include "util/u_hash_table.h"
3 #include "util/u_inlines.h"
4 #include "util/u_memory.h"
5
6 /* TODO: ouch, util_hash_table should do these by default when passed a null function pointer
7 * this indirect function call is quite bad
8 */
9 static unsigned
10 hash(void *key)
11 {
12 return (unsigned)(uintptr_t)key;
13 }
14
15 static int
16 compare(void *key1, void *key2)
17 {
18 return (unsigned)(uintptr_t)key1 - (unsigned)(uintptr_t)key2;
19 }
20
21 struct pipe_surface *
22 util_surfaces_do_get(struct util_surfaces *us, unsigned surface_struct_size, struct pipe_screen *pscreen, struct pipe_resource *pt, unsigned face, unsigned level, unsigned zslice, unsigned flags)
23 {
24 struct pipe_surface *ps;
25 void *key = NULL;
26
27 if(pt->target == PIPE_TEXTURE_3D || pt->target == PIPE_TEXTURE_CUBE)
28 { /* or 2D array */
29 if(!us->u.table)
30 us->u.table = util_hash_table_create(hash, compare);
31 key = (void *)(((zslice + face) << 8) | level);
32 /* TODO: ouch, should have a get-reference function...
33 * also, shouldn't allocate a two-pointer structure for each item... */
34 ps = util_hash_table_get(us->u.table, key);
35 }
36 else
37 {
38 if(!us->u.array)
39 us->u.array = CALLOC(pt->last_level + 1, sizeof(struct pipe_surface *));
40 ps = us->u.array[level];
41 }
42
43 if(ps)
44 {
45 p_atomic_inc(&ps->reference.count);
46 return ps;
47 }
48
49 ps = (struct pipe_surface *)CALLOC(1, surface_struct_size);
50 if(!ps)
51 return NULL;
52
53 pipe_surface_init(ps, pt, face, level, zslice, flags);
54 ps->offset = ~0;
55
56 if(pt->target == PIPE_TEXTURE_3D || pt->target == PIPE_TEXTURE_CUBE)
57 util_hash_table_set(us->u.table, key, ps);
58 else
59 us->u.array[level] = ps;
60
61 return ps;
62 }
63
64 void
65 util_surfaces_do_detach(struct util_surfaces *us, struct pipe_surface *ps)
66 {
67 struct pipe_resource *pt = ps->texture;
68 if(pt->target == PIPE_TEXTURE_3D || pt->target == PIPE_TEXTURE_CUBE)
69 { /* or 2D array */
70 void* key = (void*)(uintptr_t)(((ps->zslice + ps->face) << 8) | ps->level);
71 util_hash_table_remove(us->u.table, key);
72 }
73 else
74 us->u.array[ps->level] = 0;
75 }
76
77 static enum pipe_error
78 util_surfaces_destroy_callback(void *key, void *value, void *data)
79 {
80 void (*destroy_surface) (struct pipe_surface * ps) = data;
81 destroy_surface((struct pipe_surface *)value);
82 return PIPE_OK;
83 }
84
85 void
86 util_surfaces_destroy(struct util_surfaces *us, struct pipe_resource *pt, void (*destroy_surface) (struct pipe_surface *))
87 {
88 if(pt->target == PIPE_TEXTURE_3D || pt->target == PIPE_TEXTURE_CUBE)
89 { /* or 2D array */
90 if(us->u.table)
91 {
92 util_hash_table_foreach(us->u.table, util_surfaces_destroy_callback, destroy_surface);
93 util_hash_table_destroy(us->u.table);
94 us->u.table = NULL;
95 }
96 }
97 else
98 {
99 if(us->u.array)
100 {
101 unsigned i;
102 for(i = 0; i < pt->last_level; ++i)
103 {
104 struct pipe_surface *ps = us->u.array[i];
105 if(ps)
106 destroy_surface(ps);
107 }
108 FREE(us->u.array);
109 us->u.array = NULL;
110 }
111 }
112 }