1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2010 Luca Barbieri
4848b8605Smrg *
5848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining
6848b8605Smrg * a copy of this software and associated documentation files (the
7848b8605Smrg * "Software"), to deal in the Software without restriction, including
8848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
9848b8605Smrg * distribute, sublicense, and/or sell copies of the Software, and to
10848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
11848b8605Smrg * the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice (including the
14848b8605Smrg * next paragraph) shall be included in all copies or substantial
15848b8605Smrg * portions of the Software.
16848b8605Smrg *
17848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18848b8605Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20848b8605Smrg * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21848b8605Smrg * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22848b8605Smrg * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23848b8605Smrg * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24848b8605Smrg *
25848b8605Smrg **************************************************************************/
26848b8605Smrg
27848b8605Smrg#ifndef U_SURFACES_H_
28848b8605Smrg#define U_SURFACES_H_
29848b8605Smrg
30848b8605Smrg#include "pipe/p_compiler.h"
31848b8605Smrg#include "pipe/p_state.h"
32848b8605Smrg#include "util/u_atomic.h"
33848b8605Smrg#include "cso_cache/cso_hash.h"
34848b8605Smrg
35848b8605Smrgstruct util_surfaces
36848b8605Smrg{
37848b8605Smrg   union
38848b8605Smrg   {
39848b8605Smrg      struct cso_hash *hash;
40848b8605Smrg      struct pipe_surface **array;
41848b8605Smrg      void* pv;
42848b8605Smrg   } u;
43848b8605Smrg};
44848b8605Smrg
45848b8605Smrg/* Return value indicates if the pipe surface result is new */
46848b8605Smrgboolean
47848b8605Smrgutil_surfaces_do_get(struct util_surfaces *us, unsigned surface_struct_size,
48848b8605Smrg                     struct pipe_context *ctx, struct pipe_resource *pt,
49848b8605Smrg                     unsigned level, unsigned layer,
50848b8605Smrg                     struct pipe_surface **res);
51848b8605Smrg
52848b8605Smrg/* fast inline path for the very common case */
53b8e80941Smrgstatic inline boolean
54848b8605Smrgutil_surfaces_get(struct util_surfaces *us, unsigned surface_struct_size,
55848b8605Smrg                  struct pipe_context *ctx, struct pipe_resource *pt,
56848b8605Smrg                  unsigned level, unsigned layer,
57848b8605Smrg                  struct pipe_surface **res)
58848b8605Smrg{
59848b8605Smrg   if(likely((pt->target == PIPE_TEXTURE_2D || pt->target == PIPE_TEXTURE_RECT) && us->u.array))
60848b8605Smrg   {
61848b8605Smrg      struct pipe_surface *ps = us->u.array[level];
62848b8605Smrg      if(ps && ps->context == ctx)
63848b8605Smrg      {
64848b8605Smrg	 p_atomic_inc(&ps->reference.count);
65848b8605Smrg	 *res = ps;
66848b8605Smrg	 return FALSE;
67848b8605Smrg      }
68848b8605Smrg   }
69848b8605Smrg
70848b8605Smrg   return util_surfaces_do_get(us, surface_struct_size, ctx, pt, level, layer, res);
71848b8605Smrg}
72848b8605Smrg
73b8e80941Smrgstatic inline struct pipe_surface *
74848b8605Smrgutil_surfaces_peek(struct util_surfaces *us, struct pipe_resource *pt, unsigned level, unsigned layer)
75848b8605Smrg{
76848b8605Smrg   if(!us->u.pv)
77848b8605Smrg      return 0;
78848b8605Smrg
79848b8605Smrg   if(unlikely(pt->target == PIPE_TEXTURE_3D || pt->target == PIPE_TEXTURE_CUBE))
80848b8605Smrg      return cso_hash_iter_data(cso_hash_find(us->u.hash, (layer << 8) | level));
81848b8605Smrg   else
82848b8605Smrg      return us->u.array[level];
83848b8605Smrg}
84848b8605Smrg
85848b8605Smrgvoid util_surfaces_do_detach(struct util_surfaces *us, struct pipe_surface *ps);
86848b8605Smrg
87b8e80941Smrgstatic inline void
88848b8605Smrgutil_surfaces_detach(struct util_surfaces *us, struct pipe_surface *ps)
89848b8605Smrg{
90848b8605Smrg   if(likely(ps->texture->target == PIPE_TEXTURE_2D || ps->texture->target == PIPE_TEXTURE_RECT))
91848b8605Smrg   {
92848b8605Smrg      us->u.array[ps->u.tex.level] = 0;
93848b8605Smrg      return;
94848b8605Smrg   }
95848b8605Smrg
96848b8605Smrg   util_surfaces_do_detach(us, ps);
97848b8605Smrg}
98848b8605Smrg
99848b8605Smrgvoid util_surfaces_destroy(struct util_surfaces *us, struct pipe_resource *pt, void (*destroy_surface) (struct pipe_surface *));
100848b8605Smrg
101848b8605Smrg#endif
102