summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/pipebuffer/pb_bufmgr_ondemand.c
blob: 4f7e6b1c4df6d0f5777cf17b585c5649aa13ba39 (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
302
303
/**************************************************************************
 *
 * Copyright 2008 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
 * A variation of malloc buffers which get transferred to real graphics memory
 * when there is an attempt to validate them. 
 * 
 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
 */


#include "util/u_debug.h"
#include "util/u_memory.h"
#include "pb_buffer.h"
#include "pb_bufmgr.h"


struct pb_ondemand_manager;


struct pb_ondemand_buffer 
{
   struct pb_buffer base;
   
   struct pb_ondemand_manager *mgr;
   
   /** Regular malloc'ed memory */
   void *data;
   unsigned mapcount;
   
   /** Real buffer */
   struct pb_buffer *buffer;
   size_t size;
   struct pb_desc desc;
};


struct pb_ondemand_manager
{
   struct pb_manager base;
   
   struct pb_manager *provider;
};


extern const struct pb_vtbl pb_ondemand_buffer_vtbl;

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

static INLINE struct pb_ondemand_manager *
pb_ondemand_manager(struct pb_manager *mgr)
{
   assert(mgr);
   return (struct pb_ondemand_manager *)mgr;
}


static void
pb_ondemand_buffer_destroy(struct pb_buffer *_buf)
{
   struct pb_ondemand_buffer *buf = pb_ondemand_buffer(_buf);
   
   pb_reference(&buf->buffer, NULL);
   
   align_free(buf->data);
   
   FREE(buf);
}


static void *
pb_ondemand_buffer_map(struct pb_buffer *_buf, 
                       unsigned flags)
{
   struct pb_ondemand_buffer *buf = pb_ondemand_buffer(_buf);

   if(buf->buffer) {
      assert(!buf->data);
      return pb_map(buf->buffer, flags);
   }
   else {
      assert(buf->data);
      ++buf->mapcount;
      return buf->data;
   }
}


static void
pb_ondemand_buffer_unmap(struct pb_buffer *_buf)
{
   struct pb_ondemand_buffer *buf = pb_ondemand_buffer(_buf);

   if(buf->buffer) {
      assert(!buf->data);
      pb_unmap(buf->buffer);
   }
   else {
      assert(buf->data);
      assert(buf->mapcount);
      if(buf->mapcount)
         --buf->mapcount;
   }
}


static enum pipe_error 
pb_ondemand_buffer_instantiate(struct pb_ondemand_buffer *buf)
{
   if(!buf->buffer) {
      struct pb_manager *provider = buf->mgr->provider;
      uint8_t *map;
      
      assert(!buf->mapcount);
      
      buf->buffer = provider->create_buffer(provider, buf->size, &buf->desc);
      if(!buf->buffer)
         return PIPE_ERROR_OUT_OF_MEMORY;
      
      map = pb_map(buf->buffer, PIPE_BUFFER_USAGE_CPU_READ);
      if(!map) {
         pb_reference(&buf->buffer, NULL);
         return PIPE_ERROR;
      }
      
      memcpy(map, buf->data, buf->size);
      
      pb_unmap(buf->buffer);
      
      if(!buf->mapcount) {
         FREE(buf->data);
         buf->data = NULL;
      }
   }
   
   return PIPE_OK;
}

static enum pipe_error 
pb_ondemand_buffer_validate(struct pb_buffer *_buf, 
                            struct pb_validate *vl,
                            unsigned flags)
{
   struct pb_ondemand_buffer *buf = pb_ondemand_buffer(_buf);
   enum pipe_error ret; 

   assert(!buf->mapcount);
   if(buf->mapcount)
      return PIPE_ERROR;

   ret = pb_ondemand_buffer_instantiate(buf);
   if(ret != PIPE_OK)
      return ret;
   
   return pb_validate(buf->buffer, vl, flags);
}


static void
pb_ondemand_buffer_fence(struct pb_buffer *_buf, 
                         struct pipe_fence_handle *fence)
{
   struct pb_ondemand_buffer *buf = pb_ondemand_buffer(_buf);
   
   assert(buf->buffer);
   if(!buf->buffer)
      return;
   
   pb_fence(buf->buffer, fence);
}


static void
pb_ondemand_buffer_get_base_buffer(struct pb_buffer *_buf,
                                   struct pb_buffer **base_buf,
                                   unsigned *offset)
{
   struct pb_ondemand_buffer *buf = pb_ondemand_buffer(_buf);

   if(pb_ondemand_buffer_instantiate(buf) != PIPE_OK) {
      assert(0);
      *base_buf = &buf->base;
      *offset = 0;
      return;
   }

   pb_get_base_buffer(buf->buffer, base_buf, offset);
}


const struct pb_vtbl 
pb_ondemand_buffer_vtbl = {
      pb_ondemand_buffer_destroy,
      pb_ondemand_buffer_map,
      pb_ondemand_buffer_unmap,
      pb_ondemand_buffer_validate,
      pb_ondemand_buffer_fence,
      pb_ondemand_buffer_get_base_buffer
};


static struct pb_buffer *
pb_ondemand_manager_create_buffer(struct pb_manager *_mgr, 
                                  size_t size,
                                  const struct pb_desc *desc) 
{
   struct pb_ondemand_manager *mgr = pb_ondemand_manager(_mgr);
   struct pb_ondemand_buffer *buf;
   
   buf = CALLOC_STRUCT(pb_ondemand_buffer);
   if(!buf)
      return NULL;

   pipe_reference_init(&buf->base.base.reference, 1);
   buf->base.base.alignment = desc->alignment;
   buf->base.base.usage = desc->usage;
   buf->base.base.size = size;
   buf->base.vtbl = &pb_ondemand_buffer_vtbl;
   
   buf->mgr = mgr;
   
   buf->data = align_malloc(size, desc->alignment < sizeof(void*) ? sizeof(void*) : desc->alignment);
   if(!buf->data) {
      FREE(buf);
      return NULL;
   }
   
   buf->size = size;
   buf->desc = *desc;

   return &buf->base;
}


static void
pb_ondemand_manager_flush(struct pb_manager *_mgr) 
{
   struct pb_ondemand_manager *mgr = pb_ondemand_manager(_mgr);
   
   mgr->provider->flush(mgr->provider);
}


static void
pb_ondemand_manager_destroy(struct pb_manager *_mgr) 
{
   struct pb_ondemand_manager *mgr = pb_ondemand_manager(_mgr);

   FREE(mgr);
}


struct pb_manager *
pb_ondemand_manager_create(struct pb_manager *provider) 
{
   struct pb_ondemand_manager *mgr;

   if(!provider)
      return NULL;
   
   mgr = CALLOC_STRUCT(pb_ondemand_manager);
   if(!mgr)
      return NULL;
   
   mgr->base.destroy = pb_ondemand_manager_destroy;
   mgr->base.create_buffer = pb_ondemand_manager_create_buffer;
   mgr->base.flush = pb_ondemand_manager_flush;
   
   mgr->provider = provider;

   return &mgr->base;
}