summaryrefslogtreecommitdiff
path: root/src/mesa/pipe/pipebuffer/pb_buffer_fenced.c
blob: 4cf4222db9de8d013049c7bdaf8ec9b77c6f32ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**************************************************************************
 *
 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

/**
 * \file
 * Implementation of fenced buffers.
 * 
 * \author José Fonseca <jrfonseca-at-tungstengraphics-dot-com>
 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
 */


#include <assert.h>
#include <stdlib.h>

#include "linked_list.h"

#include "p_compiler.h"
#include "p_winsys.h"
#include "p_thread.h"
#include "p_util.h"

#include "pb_buffer.h"
#include "pb_buffer_fenced.h"

#ifndef __MSC__
#include <unistd.h>
#endif


/**
 * Convenience macro (type safe).
 */
#define SUPER(__derived) (&(__derived)->base)


struct fenced_buffer_list
{
   _glthread_Mutex mutex;
   
   struct pipe_winsys *winsys;
   
   size_t numDelayed;
   size_t checkDelayed;
   
   struct list_head delayed;
};


/**
 * Wrapper around a pipe buffer which adds fencing and reference counting.
 */
struct fenced_buffer
{
   struct pb_buffer base;
   
   struct pb_buffer *buffer;

   struct pipe_fence_handle *fence;

   struct list_head head;
   struct fenced_buffer_list *list;
};


static INLINE struct fenced_buffer *
fenced_buffer(struct pb_buffer *buf)
{
   assert(buf);
   assert(buf->vtbl == &fenced_buffer_vtbl);
   return (struct fenced_buffer *)buf;
}




static void
_fenced_buffer_list_check_free(struct fenced_buffer_list *fenced_list, 
                               int wait)
{
   struct pipe_winsys *winsys = fenced_list->winsys;
   struct fenced_buffer *fenced_buf;   
   struct list_head *list, *prev;
   int signaled = -1;

   list = fenced_list->delayed.next;

   if (fenced_list->numDelayed > 3) {
      unsigned i;

      for (i = 0; i < fenced_list->numDelayed; i += 3) {
         list = list->next;
      }
   }

   prev = list->prev;
   for (; list != &fenced_list->delayed; list = prev, prev = list->prev) {

      fenced_buf = LIST_ENTRY(struct fenced_buffer, list, head);

      if (signaled != 0) {
         if (wait) {
            signaled = winsys->fence_finish(winsys, fenced_buf->fence, 0);
         }
         else {
            signaled = winsys->fence_signalled(winsys, fenced_buf->fence, 0);
         }
      }

      if (signaled != 0)
	 /* XXX: we are assuming that buffers are freed in the same order they 
	  * are fenced which may not always be true... 
	  */
         break;

      winsys->fence_reference(winsys, &fenced_buf->fence, NULL);
      
      LIST_DEL(list);
      fenced_list->numDelayed--;

      /* Do the delayed destroy:
       */
      pb_reference(&fenced_buf->buffer, NULL);
      FREE(fenced_buf);
   }
}


static void
fenced_buffer_destroy(struct pb_buffer *buf)
{
   struct fenced_buffer *fenced_buf = fenced_buffer(buf);   
   struct fenced_buffer_list *fenced_list = fenced_buf->list;

   if (fenced_buf->fence) {
      LIST_ADDTAIL(&fenced_buf->head, &fenced_list->delayed);
      fenced_list->numDelayed++;
   }
   else {
      pb_reference(&fenced_buf->buffer, NULL);
      FREE(fenced_buf);
   }
   
   if ((fenced_list->numDelayed % fenced_list->checkDelayed) == 0)
      _fenced_buffer_list_check_free(fenced_list, 0);
}


static void *
fenced_buffer_map(struct pb_buffer *buf, 
                  unsigned flags)
{
   struct fenced_buffer *fenced_buf = fenced_buffer(buf);   
   return pb_map(fenced_buf->buffer, flags);
}


static void
fenced_buffer_unmap(struct pb_buffer *buf)
{
   struct fenced_buffer *fenced_buf = fenced_buffer(buf);   
   pb_unmap(fenced_buf->buffer);
}


static void
fenced_buffer_get_base_buffer(struct pb_buffer *buf,
                              struct pb_buffer **base_buf,
                              unsigned *offset)
{
   struct fenced_buffer *fenced_buf = fenced_buffer(buf);
   pb_get_base_buffer(fenced_buf->buffer, base_buf, offset);
}


const struct pb_vtbl 
fenced_buffer_vtbl = {
      fenced_buffer_destroy,
      fenced_buffer_map,
      fenced_buffer_unmap,
      fenced_buffer_get_base_buffer
};


struct pb_buffer *
fenced_buffer_create(struct fenced_buffer_list *fenced_list, 
                     struct pb_buffer *buffer)
{
   struct fenced_buffer *buf;
   
   if(!buffer)
      return NULL;
   
   buf = CALLOC_STRUCT(fenced_buffer);
   if(!buf)
      return NULL;
   
   buf->base.base.refcount = 1;
   buf->base.base.alignment = buffer->base.alignment;
   buf->base.base.usage = buffer->base.usage;
   buf->base.base.size = buffer->base.size;
   
   buf->base.vtbl = &fenced_buffer_vtbl;
   buf->buffer = buffer;
   buf->list = fenced_list;
   
   return &buf->base;
}


void
buffer_fence(struct pb_buffer *buf,
             struct pipe_fence_handle *fence)
{
   struct fenced_buffer *fenced_buf = fenced_buffer(buf);
   struct fenced_buffer_list *fenced_list = fenced_buf->list;
   struct pipe_winsys *winsys = fenced_list->winsys;
   
   _glthread_LOCK_MUTEX(fenced_list->mutex);
   winsys->fence_reference(winsys, &fenced_buf->fence, fence);
   _glthread_UNLOCK_MUTEX(fenced_list->mutex);
}


struct fenced_buffer_list *
fenced_buffer_list_create(struct pipe_winsys *winsys) 
{
   struct fenced_buffer_list *fenced_list;

   fenced_list = (struct fenced_buffer_list *)CALLOC(1, sizeof(*fenced_list));
   if (!fenced_list)
      return NULL;

   fenced_list->winsys = winsys;

   LIST_INITHEAD(&fenced_list->delayed);

   fenced_list->numDelayed = 0;
   
   /* TODO: don't hard code this */ 
   fenced_list->checkDelayed = 5;

   _glthread_INIT_MUTEX(fenced_list->mutex);

   return fenced_list;
}


void
fenced_buffer_list_check_free(struct fenced_buffer_list *fenced_list, 
                              int wait)
{
   _glthread_LOCK_MUTEX(fenced_list->mutex);
   _fenced_buffer_list_check_free(fenced_list, wait);
   _glthread_UNLOCK_MUTEX(fenced_list->mutex);
}


void
fenced_buffer_list_destroy(struct fenced_buffer_list *fenced_list)
{
   _glthread_LOCK_MUTEX(fenced_list->mutex);

   /* Wait on outstanding fences */
   while (fenced_list->numDelayed) {
      _glthread_UNLOCK_MUTEX(fenced_list->mutex);
      sched_yield();
      _fenced_buffer_list_check_free(fenced_list, 1);
      _glthread_LOCK_MUTEX(fenced_list->mutex);
   }

   _glthread_UNLOCK_MUTEX(fenced_list->mutex);
   
   FREE(fenced_list);
}