summaryrefslogtreecommitdiff
path: root/src/mesa/pipe/pipebuffer/pb_bufmgr_pool.c
blob: ee6fa6250045a80b65bcb6a7210cd38212135c57 (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
/**************************************************************************
 * 
 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
 * 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 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
 * THE COPYRIGHT HOLDERS, AUTHORS 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.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * 
 **************************************************************************/

/**
 * \file
 * Batch buffer pool management.
 * 
 * \author José Fonseca <jrfonseca-at-tungstengraphics-dot-com>
 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
 */


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

#include "main/imports.h"
#include "glapi/glthread.h"
#include "linked_list.h"

#include "p_defines.h"

#include "pb_buffer.h"
#include "pb_bufmgr.h"


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


struct pool_buffer_manager
{
   struct buffer_manager base;
   
   _glthread_Mutex mutex;
   
   size_t bufSize;
   
   size_t numFree;
   size_t numTot;
   
   struct list_head free;
   
   struct pipe_buffer *buffer;
   void *map;
   
   struct pool_buffer *bufs;
};


static inline struct pool_buffer_manager *
pool_buffer_manager(struct buffer_manager *mgr)
{
   assert(mgr);
   return (struct pool_buffer_manager *)mgr;
}


struct pool_buffer
{
   struct pipe_buffer base;
   
   struct pool_buffer_manager *mgr;
   
   struct list_head head;
   
   size_t start;
};


static inline struct pool_buffer *
pool_buffer(struct pipe_buffer *buf)
{
   assert(buf);
   return (struct pool_buffer *)buf;
}


static void
pool_buffer_reference(struct pipe_buffer *buf)
{
   /* No-op */
}


static void
pool_buffer_release(struct pipe_buffer *buf)
{
   struct pool_buffer *pool_buf = pool_buffer(buf);
   struct pool_buffer_manager *pool = pool_buf->mgr;
   
   _glthread_LOCK_MUTEX(pool->mutex);
   LIST_ADD(&pool_buf->head, &pool->free);
   pool->numFree++;
   _glthread_UNLOCK_MUTEX(pool->mutex);
}


static void *
pool_buffer_map(struct pipe_buffer *buf, unsigned flags)
{
   struct pool_buffer *pool_buf = pool_buffer(buf);
   struct pool_buffer_manager *pool = pool_buf->mgr;
   void *map;

   _glthread_LOCK_MUTEX(pool->mutex);
   map = (unsigned char *) pool->map + pool_buf->start;
   _glthread_UNLOCK_MUTEX(pool->mutex);
   return map;
}


static void
pool_buffer_unmap(struct pipe_buffer *buf)
{
   /* No-op */
}


static void
pool_buffer_get_base_buffer(struct pipe_buffer *buf,
                            struct pipe_buffer **base_buf,
                            unsigned *offset)
{
   struct pool_buffer *pool_buf = pool_buffer(buf);
   struct pool_buffer_manager *pool = pool_buf->mgr;
   buffer_get_base_buffer(pool->buffer, base_buf, offset);
   *offset += pool_buf->start;
}


static const struct pipe_buffer_vtbl 
pool_buffer_vtbl = {
      pool_buffer_reference,
      pool_buffer_release,
      pool_buffer_map,
      pool_buffer_unmap,
      pool_buffer_get_base_buffer
};


static struct pipe_buffer *
pool_bufmgr_create_buffer(struct buffer_manager *mgr, size_t size)
{
   struct pool_buffer_manager *pool = pool_buffer_manager(mgr);
   struct pool_buffer *pool_buf;
   struct list_head *item;

   assert(size == pool->bufSize);
   
   _glthread_LOCK_MUTEX(pool->mutex);

   if (pool->numFree == 0) {
      _glthread_UNLOCK_MUTEX(pool->mutex);
      fprintf(stderr, "warning: out of fixed size buffer objects\n");
      return NULL;
   }

   item = pool->free.next;

   if (item == &pool->free) {
      _glthread_UNLOCK_MUTEX(pool->mutex);
      fprintf(stderr, "error: fixed size buffer pool corruption\n");
      return NULL;
   }

   LIST_DEL(item);
   --pool->numFree;

   _glthread_UNLOCK_MUTEX(pool->mutex);
   pool_buf = LIST_ENTRY(struct pool_buffer, item, head);
   return SUPER(pool_buf);
}


static void
pool_bufmgr_destroy(struct buffer_manager *mgr)
{
   struct pool_buffer_manager *pool = pool_buffer_manager(mgr);
   _glthread_LOCK_MUTEX(pool->mutex);

   free(pool->bufs);
   
   buffer_unmap(pool->buffer);
   buffer_release(pool->buffer);
   
   _glthread_UNLOCK_MUTEX(pool->mutex);
   
   free(mgr);
}


struct buffer_manager *
pool_bufmgr_create(struct buffer_manager *provider, 
                   size_t numBufs, 
                   size_t bufSize) 
{
   struct pool_buffer_manager *pool;
   struct pool_buffer *pool_buf;
   int i;

   pool = (struct pool_buffer_manager *)calloc(1, sizeof(*pool));
   if (!pool)
      return NULL;

   pool->base.destroy = pool_bufmgr_destroy;
   pool->base.create_buffer = pool_bufmgr_create_buffer;

   LIST_INITHEAD(&pool->free);

   pool->numTot = numBufs;
   pool->numFree = numBufs;
   pool->bufSize = bufSize;
   
   _glthread_INIT_MUTEX(pool->mutex);

   pool->buffer = provider->create_buffer(provider, numBufs*bufSize); 
   if (!pool->buffer)
      goto failure;

   pool->map = buffer_map(pool->buffer,
                          PIPE_BUFFER_FLAG_READ |
                          PIPE_BUFFER_FLAG_WRITE );
   if(!pool->map)
      goto failure;

   pool->bufs = (struct pool_buffer *) malloc(numBufs * sizeof(*pool->bufs));
   if (!pool->bufs)
      goto failure;

   pool_buf = pool->bufs;
   for (i = 0; i < numBufs; ++i) {
      pool_buf->base.vtbl = &pool_buffer_vtbl;
      pool_buf->mgr = pool;
      pool_buf->start = i * bufSize;
      LIST_ADDTAIL(&pool_buf->head, &pool->free);
      pool_buf++;
   }

   return SUPER(pool);
   
failure:
   if(pool->bufs)
      free(pool->bufs);
   if(pool->map)
      buffer_unmap(pool->buffer);
   if(pool->buffer)
      buffer_release(pool->buffer);
   if(pool)
      free(pool);
   return NULL;
}