auxiliary: add copyright headers
[mesa.git] / src / gallium / auxiliary / util / u_surfaces.h
1 /**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #ifndef U_SURFACES_H_
28 #define U_SURFACES_H_
29
30 #include "pipe/p_compiler.h"
31 #include "pipe/p_state.h"
32 #include "util/u_atomic.h"
33 #include "cso_cache/cso_hash.h"
34
35 struct util_surfaces
36 {
37 union
38 {
39 struct cso_hash *hash;
40 struct pipe_surface **array;
41 void* pv;
42 } u;
43 };
44
45 struct pipe_surface *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);
46
47 /* fast inline path for the very common case */
48 static INLINE struct pipe_surface *
49 util_surfaces_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)
50 {
51 if(likely((pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT) && us->u.array))
52 {
53 struct pipe_surface *ps = us->u.array[level];
54 if(ps)
55 {
56 p_atomic_inc(&ps->reference.count);
57 return ps;
58 }
59 }
60
61 return util_surfaces_do_get(us, surface_struct_size, pscreen, pt, face, level, zslice, flags);
62 }
63
64 static INLINE struct pipe_surface *
65 util_surfaces_peek(struct util_surfaces *us, struct pipe_resource *pt, unsigned face, unsigned level, unsigned zslice)
66 {
67 if(!us->u.pv)
68 return 0;
69
70 if(unlikely(pt->target == PIPE_TEXTURE_3D || pt->target == PIPE_TEXTURE_CUBE))
71 return cso_hash_iter_data(cso_hash_find(us->u.hash, ((zslice + face) << 8) | level));
72 else
73 return us->u.array[level];
74 }
75
76 void util_surfaces_do_detach(struct util_surfaces *us, struct pipe_surface *ps);
77
78 static INLINE void
79 util_surfaces_detach(struct util_surfaces *us, struct pipe_surface *ps)
80 {
81 if(likely(ps->texture->target == PIPE_TEXTURE_2D || ps->texture->target == PIPE_TEXTURE_RECT))
82 {
83 us->u.array[ps->level] = 0;
84 return;
85 }
86
87 util_surfaces_do_detach(us, ps);
88 }
89
90 void util_surfaces_destroy(struct util_surfaces *us, struct pipe_resource *pt, void (*destroy_surface) (struct pipe_surface *));
91
92 #endif