1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Buffer manager using the old texture memory manager.
32 * \author Jose Fonseca <jrfonseca@tungstengraphics.com>
36 #include "pipe/p_defines.h"
37 #include "pipe/p_debug.h"
38 #include "pipe/p_thread.h"
39 #include "util/u_memory.h"
40 #include "util/u_double_list.h"
41 #include "util/u_mm.h"
42 #include "pb_buffer.h"
43 #include "pb_bufmgr.h"
47 * Convenience macro (type safe).
49 #define SUPER(__derived) (&(__derived)->base)
54 struct pb_manager base
;
59 struct mem_block
*heap
;
63 struct pb_buffer
*buffer
;
68 static INLINE
struct mm_pb_manager
*
69 mm_pb_manager(struct pb_manager
*mgr
)
72 return (struct mm_pb_manager
*)mgr
;
78 struct pb_buffer base
;
80 struct mm_pb_manager
*mgr
;
82 struct mem_block
*block
;
86 static INLINE
struct mm_buffer
*
87 mm_buffer(struct pb_buffer
*buf
)
90 return (struct mm_buffer
*)buf
;
95 mm_buffer_destroy(struct pb_buffer
*buf
)
97 struct mm_buffer
*mm_buf
= mm_buffer(buf
);
98 struct mm_pb_manager
*mm
= mm_buf
->mgr
;
100 assert(buf
->base
.refcount
== 0);
102 pipe_mutex_lock(mm
->mutex
);
103 u_mmFreeMem(mm_buf
->block
);
105 pipe_mutex_unlock(mm
->mutex
);
110 mm_buffer_map(struct pb_buffer
*buf
,
113 struct mm_buffer
*mm_buf
= mm_buffer(buf
);
114 struct mm_pb_manager
*mm
= mm_buf
->mgr
;
116 return (unsigned char *) mm
->map
+ mm_buf
->block
->ofs
;
121 mm_buffer_unmap(struct pb_buffer
*buf
)
127 static enum pipe_error
128 mm_buffer_validate(struct pb_buffer
*buf
,
129 struct pb_validate
*vl
,
132 struct mm_buffer
*mm_buf
= mm_buffer(buf
);
133 struct mm_pb_manager
*mm
= mm_buf
->mgr
;
134 return pb_validate(mm
->buffer
, vl
, flags
);
139 mm_buffer_fence(struct pb_buffer
*buf
,
140 struct pipe_fence_handle
*fence
)
142 struct mm_buffer
*mm_buf
= mm_buffer(buf
);
143 struct mm_pb_manager
*mm
= mm_buf
->mgr
;
144 pb_fence(mm
->buffer
, fence
);
149 mm_buffer_get_base_buffer(struct pb_buffer
*buf
,
150 struct pb_buffer
**base_buf
,
153 struct mm_buffer
*mm_buf
= mm_buffer(buf
);
154 struct mm_pb_manager
*mm
= mm_buf
->mgr
;
155 pb_get_base_buffer(mm
->buffer
, base_buf
, offset
);
156 *offset
+= mm_buf
->block
->ofs
;
160 static const struct pb_vtbl
167 mm_buffer_get_base_buffer
171 static struct pb_buffer
*
172 mm_bufmgr_create_buffer(struct pb_manager
*mgr
,
174 const struct pb_desc
*desc
)
176 struct mm_pb_manager
*mm
= mm_pb_manager(mgr
);
177 struct mm_buffer
*mm_buf
;
179 /* We don't handle alignments larger then the one initially setup */
180 assert(pb_check_alignment(desc
->alignment
, 1 << mm
->align2
));
181 if(!pb_check_alignment(desc
->alignment
, 1 << mm
->align2
))
184 pipe_mutex_lock(mm
->mutex
);
186 mm_buf
= CALLOC_STRUCT(mm_buffer
);
188 pipe_mutex_unlock(mm
->mutex
);
192 mm_buf
->base
.base
.refcount
= 1;
193 mm_buf
->base
.base
.alignment
= desc
->alignment
;
194 mm_buf
->base
.base
.usage
= desc
->usage
;
195 mm_buf
->base
.base
.size
= size
;
197 mm_buf
->base
.vtbl
= &mm_buffer_vtbl
;
201 mm_buf
->block
= u_mmAllocMem(mm
->heap
, size
, mm
->align2
, 0);
203 debug_printf("warning: heap full\n");
205 mmDumpMemInfo(mm
->heap
);
208 mm_buf
->block
= u_mmAllocMem(mm
->heap
, size
, mm
->align2
, 0);
211 pipe_mutex_unlock(mm
->mutex
);
216 /* Some sanity checks */
217 assert(0 <= (unsigned)mm_buf
->block
->ofs
&& (unsigned)mm_buf
->block
->ofs
< mm
->size
);
218 assert(size
<= (unsigned)mm_buf
->block
->size
&& (unsigned)mm_buf
->block
->ofs
+ (unsigned)mm_buf
->block
->size
<= mm
->size
);
220 pipe_mutex_unlock(mm
->mutex
);
221 return SUPER(mm_buf
);
226 mm_bufmgr_flush(struct pb_manager
*mgr
)
233 mm_bufmgr_destroy(struct pb_manager
*mgr
)
235 struct mm_pb_manager
*mm
= mm_pb_manager(mgr
);
237 pipe_mutex_lock(mm
->mutex
);
239 u_mmDestroy(mm
->heap
);
241 pb_unmap(mm
->buffer
);
242 pb_reference(&mm
->buffer
, NULL
);
244 pipe_mutex_unlock(mm
->mutex
);
251 mm_bufmgr_create_from_buffer(struct pb_buffer
*buffer
,
252 size_t size
, size_t align2
)
254 struct mm_pb_manager
*mm
;
259 mm
= CALLOC_STRUCT(mm_pb_manager
);
263 mm
->base
.destroy
= mm_bufmgr_destroy
;
264 mm
->base
.create_buffer
= mm_bufmgr_create_buffer
;
265 mm
->base
.flush
= mm_bufmgr_flush
;
268 mm
->align2
= align2
; /* 64-byte alignment */
270 pipe_mutex_init(mm
->mutex
);
274 mm
->map
= pb_map(mm
->buffer
,
275 PIPE_BUFFER_USAGE_CPU_READ
|
276 PIPE_BUFFER_USAGE_CPU_WRITE
);
280 mm
->heap
= u_mmInit(0, size
);
288 u_mmDestroy(mm
->heap
);
290 pb_unmap(mm
->buffer
);
298 mm_bufmgr_create(struct pb_manager
*provider
,
299 size_t size
, size_t align2
)
301 struct pb_buffer
*buffer
;
302 struct pb_manager
*mgr
;
308 memset(&desc
, 0, sizeof(desc
));
309 desc
.alignment
= 1 << align2
;
311 buffer
= provider
->create_buffer(provider
, size
, &desc
);
315 mgr
= mm_bufmgr_create_from_buffer(buffer
, size
, align2
);
317 pb_reference(&buffer
, NULL
);