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 "util/u_debug.h"
38 #include "os/os_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(!pipe_is_referenced(&mm_buf->base.base.reference));
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, (pb_size)1 << mm->align2));
181 if(!pb_check_alignment(desc->alignment, (pb_size)1 << mm->align2))
184 pipe_mutex_lock(mm->mutex);
186 mm_buf = CALLOC_STRUCT(mm_buffer);
188 pipe_mutex_unlock(mm->mutex);
192 pipe_reference_init(&mm_buf->base.base.reference, 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, (int)size, (int)mm->align2, 0);
204 debug_printf("warning: heap full\n");
205 mmDumpMemInfo(mm->heap);
208 pipe_mutex_unlock(mm->mutex);
212 /* Some sanity checks */
213 assert(0 <= (pb_size)mm_buf->block->ofs && (pb_size)mm_buf->block->ofs < mm->size);
214 assert(size <= (pb_size)mm_buf->block->size && (pb_size)mm_buf->block->ofs + (pb_size)mm_buf->block->size <= mm->size);
216 pipe_mutex_unlock(mm->mutex);
217 return SUPER(mm_buf);
222 mm_bufmgr_flush(struct pb_manager *mgr)
229 mm_bufmgr_destroy(struct pb_manager *mgr)
231 struct mm_pb_manager *mm = mm_pb_manager(mgr);
233 pipe_mutex_lock(mm->mutex);
235 u_mmDestroy(mm->heap);
237 pb_unmap(mm->buffer);
238 pb_reference(&mm->buffer, NULL);
240 pipe_mutex_unlock(mm->mutex);
247 mm_bufmgr_create_from_buffer(struct pb_buffer *buffer,
248 pb_size size, pb_size align2)
250 struct mm_pb_manager *mm;
255 mm = CALLOC_STRUCT(mm_pb_manager);
259 mm->base.destroy = mm_bufmgr_destroy;
260 mm->base.create_buffer = mm_bufmgr_create_buffer;
261 mm->base.flush = mm_bufmgr_flush;
264 mm->align2 = align2; /* 64-byte alignment */
266 pipe_mutex_init(mm->mutex);
270 mm->map = pb_map(mm->buffer,
276 mm->heap = u_mmInit(0, (int)size);
284 u_mmDestroy(mm->heap);
286 pb_unmap(mm->buffer);
294 mm_bufmgr_create(struct pb_manager *provider,
295 pb_size size, pb_size align2)
297 struct pb_buffer *buffer;
298 struct pb_manager *mgr;
304 memset(&desc, 0, sizeof(desc));
305 desc.alignment = 1 << align2;
307 buffer = provider->create_buffer(provider, size, &desc);
311 mgr = mm_bufmgr_create_from_buffer(buffer, size, align2);
313 pb_reference(&buffer, NULL);