1 #include "u_surfaces.h"
2 #include "util/u_hash_table.h"
3 #include "util/u_inlines.h"
4 #include "util/u_memory.h"
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
16 compare(void *key1
, void *key2
)
18 return (unsigned)key1
- (unsigned)key2
;
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
)
24 struct pipe_surface
*ps
;
27 if(pt
->target
== PIPE_TEXTURE_3D
|| pt
->target
== PIPE_TEXTURE_CUBE
)
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
);
39 us
->u
.array
= CALLOC(pt
->last_level
+ 1, sizeof(struct pipe_surface
*));
40 ps
= us
->u
.array
[level
];
45 p_atomic_inc(&ps
->reference
.count
);
49 ps
= (struct pipe_surface
*)CALLOC(1, surface_struct_size
);
53 pipe_surface_init(ps
, pt
, face
, level
, zslice
, flags
);
56 if(pt
->target
== PIPE_TEXTURE_3D
|| pt
->target
== PIPE_TEXTURE_CUBE
)
57 util_hash_table_set(us
->u
.table
, key
, ps
);
59 us
->u
.array
[level
] = ps
;
65 util_surfaces_do_detach(struct util_surfaces
*us
, struct pipe_surface
*ps
)
67 struct pipe_resource
*pt
= ps
->texture
;
68 if(pt
->target
== PIPE_TEXTURE_3D
|| pt
->target
== PIPE_TEXTURE_CUBE
)
70 void* key
= (void*)(((ps
->zslice
+ ps
->face
) << 8) | ps
->level
);
71 util_hash_table_remove(us
->u
.table
, key
);
74 us
->u
.array
[ps
->level
] = 0;
77 static enum pipe_error
78 util_surfaces_destroy_callback(void *key
, void *value
, void *data
)
80 void (*destroy_surface
) (struct pipe_surface
* ps
) = data
;
81 destroy_surface((struct pipe_surface
*)value
);
86 util_surfaces_destroy(struct util_surfaces
*us
, struct pipe_resource
*pt
, void (*destroy_surface
) (struct pipe_surface
*))
88 if(pt
->target
== PIPE_TEXTURE_3D
|| pt
->target
== PIPE_TEXTURE_CUBE
)
92 util_hash_table_foreach(us
->u
.table
, util_surfaces_destroy_callback
, destroy_surface
);
93 util_hash_table_destroy(us
->u
.table
);
102 for(i
= 0; i
< pt
->last_level
; ++i
)
104 struct pipe_surface
*ps
= us
->u
.array
[i
];