pb_validate.c revision 287c94ea4987033f9c99a2f91c5750c9083504ca
1/**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/**
29 * @file
30 * Buffer validation.
31 *
32 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
33 */
34
35
36#include "pipe/p_compiler.h"
37#include "pipe/p_defines.h"
38#include "util/u_memory.h"
39#include "util/u_debug.h"
40
41#include "pb_buffer.h"
42#include "pb_validate.h"
43
44
45#define PB_VALIDATE_INITIAL_SIZE 1 /* 512 */
46
47
48struct pb_validate_entry
49{
50   struct pb_buffer *buf;
51   unsigned flags;
52};
53
54
55struct pb_validate
56{
57   struct pb_validate_entry *entries;
58   unsigned used;
59   unsigned size;
60};
61
62
63enum pipe_error
64pb_validate_add_buffer(struct pb_validate *vl,
65                       struct pb_buffer *buf,
66                       unsigned flags)
67{
68   assert(buf);
69   if(!buf)
70      return PIPE_ERROR;
71
72   assert(flags & PB_USAGE_GPU_READ_WRITE);
73   assert(!(flags & ~PB_USAGE_GPU_READ_WRITE));
74   flags &= PB_USAGE_GPU_READ_WRITE;
75
76   /* We only need to store one reference for each buffer, so avoid storing
77    * consecutive references for the same buffer. It might not be the most
78    * common pattern, but it is easy to implement.
79    */
80   if(vl->used && vl->entries[vl->used - 1].buf == buf) {
81      vl->entries[vl->used - 1].flags |= flags;
82      return PIPE_OK;
83   }
84
85   /* Grow the table */
86   if(vl->used == vl->size) {
87      unsigned new_size;
88      struct pb_validate_entry *new_entries;
89
90      new_size = vl->size * 2;
91      if(!new_size)
92	 return PIPE_ERROR_OUT_OF_MEMORY;
93
94      new_entries = (struct pb_validate_entry *)REALLOC(vl->entries,
95                                                        vl->size*sizeof(struct pb_validate_entry),
96                                                        new_size*sizeof(struct pb_validate_entry));
97      if(!new_entries)
98         return PIPE_ERROR_OUT_OF_MEMORY;
99
100      memset(new_entries + vl->size, 0, (new_size - vl->size)*sizeof(struct pb_validate_entry));
101
102      vl->size = new_size;
103      vl->entries = new_entries;
104   }
105
106   assert(!vl->entries[vl->used].buf);
107   pb_reference(&vl->entries[vl->used].buf, buf);
108   vl->entries[vl->used].flags = flags;
109   ++vl->used;
110
111   return PIPE_OK;
112}
113
114
115enum pipe_error
116pb_validate_foreach(struct pb_validate *vl,
117                    enum pipe_error (*callback)(struct pb_buffer *buf, void *data),
118                    void *data)
119{
120   unsigned i;
121   for(i = 0; i < vl->used; ++i) {
122      enum pipe_error ret;
123      ret = callback(vl->entries[i].buf, data);
124      if(ret != PIPE_OK)
125         return ret;
126   }
127   return PIPE_OK;
128}
129
130
131enum pipe_error
132pb_validate_validate(struct pb_validate *vl)
133{
134   unsigned i;
135
136   for(i = 0; i < vl->used; ++i) {
137      enum pipe_error ret;
138      ret = pb_validate(vl->entries[i].buf, vl, vl->entries[i].flags);
139      if(ret != PIPE_OK) {
140         while(i--)
141            pb_validate(vl->entries[i].buf, NULL, 0);
142         return ret;
143      }
144   }
145
146   return PIPE_OK;
147}
148
149
150void
151pb_validate_fence(struct pb_validate *vl,
152                  struct pipe_fence_handle *fence)
153{
154   unsigned i;
155   for(i = 0; i < vl->used; ++i) {
156      pb_fence(vl->entries[i].buf, fence);
157      pb_reference(&vl->entries[i].buf, NULL);
158   }
159   vl->used = 0;
160}
161
162
163void
164pb_validate_destroy(struct pb_validate *vl)
165{
166   unsigned i;
167   for(i = 0; i < vl->used; ++i)
168      pb_reference(&vl->entries[i].buf, NULL);
169   FREE(vl->entries);
170   FREE(vl);
171}
172
173
174struct pb_validate *
175pb_validate_create()
176{
177   struct pb_validate *vl;
178
179   vl = CALLOC_STRUCT(pb_validate);
180   if(!vl)
181      return NULL;
182
183   vl->size = PB_VALIDATE_INITIAL_SIZE;
184   vl->entries = (struct pb_validate_entry *)CALLOC(vl->size, sizeof(struct pb_validate_entry));
185   if(!vl->entries) {
186      FREE(vl);
187      return NULL;
188   }
189
190   return vl;
191}
192
193