1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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 * A variation of malloc buffers which get transferred to real graphics memory
31 * when there is an attempt to validate them.
33 * @author Jose Fonseca <jfonseca@vmware.com>
37 #include "util/u_debug.h"
38 #include "util/u_memory.h"
39 #include "pb_buffer.h"
40 #include "pb_bufmgr.h"
43 struct pb_ondemand_manager
;
46 struct pb_ondemand_buffer
48 struct pb_buffer base
;
50 struct pb_ondemand_manager
*mgr
;
52 /** Regular malloc'ed memory */
57 struct pb_buffer
*buffer
;
63 struct pb_ondemand_manager
65 struct pb_manager base
;
67 struct pb_manager
*provider
;
71 extern const struct pb_vtbl pb_ondemand_buffer_vtbl
;
73 static INLINE
struct pb_ondemand_buffer
*
74 pb_ondemand_buffer(struct pb_buffer
*buf
)
79 assert(buf
->vtbl
== &pb_ondemand_buffer_vtbl
);
80 return (struct pb_ondemand_buffer
*)buf
;
83 static INLINE
struct pb_ondemand_manager
*
84 pb_ondemand_manager(struct pb_manager
*mgr
)
87 return (struct pb_ondemand_manager
*)mgr
;
92 pb_ondemand_buffer_destroy(struct pb_buffer
*_buf
)
94 struct pb_ondemand_buffer
*buf
= pb_ondemand_buffer(_buf
);
96 pb_reference(&buf
->buffer
, NULL
);
98 align_free(buf
->data
);
105 pb_ondemand_buffer_map(struct pb_buffer
*_buf
,
106 unsigned flags
, void *flush_ctx
)
108 struct pb_ondemand_buffer
*buf
= pb_ondemand_buffer(_buf
);
112 return pb_map(buf
->buffer
, flags
, flush_ctx
);
123 pb_ondemand_buffer_unmap(struct pb_buffer
*_buf
)
125 struct pb_ondemand_buffer
*buf
= pb_ondemand_buffer(_buf
);
129 pb_unmap(buf
->buffer
);
133 assert(buf
->mapcount
);
140 static enum pipe_error
141 pb_ondemand_buffer_instantiate(struct pb_ondemand_buffer
*buf
)
144 struct pb_manager
*provider
= buf
->mgr
->provider
;
147 assert(!buf
->mapcount
);
149 buf
->buffer
= provider
->create_buffer(provider
, buf
->size
, &buf
->desc
);
151 return PIPE_ERROR_OUT_OF_MEMORY
;
153 map
= pb_map(buf
->buffer
, PB_USAGE_CPU_READ
, NULL
);
155 pb_reference(&buf
->buffer
, NULL
);
159 memcpy(map
, buf
->data
, buf
->size
);
161 pb_unmap(buf
->buffer
);
172 static enum pipe_error
173 pb_ondemand_buffer_validate(struct pb_buffer
*_buf
,
174 struct pb_validate
*vl
,
177 struct pb_ondemand_buffer
*buf
= pb_ondemand_buffer(_buf
);
180 assert(!buf
->mapcount
);
184 ret
= pb_ondemand_buffer_instantiate(buf
);
188 return pb_validate(buf
->buffer
, vl
, flags
);
193 pb_ondemand_buffer_fence(struct pb_buffer
*_buf
,
194 struct pipe_fence_handle
*fence
)
196 struct pb_ondemand_buffer
*buf
= pb_ondemand_buffer(_buf
);
202 pb_fence(buf
->buffer
, fence
);
207 pb_ondemand_buffer_get_base_buffer(struct pb_buffer
*_buf
,
208 struct pb_buffer
**base_buf
,
211 struct pb_ondemand_buffer
*buf
= pb_ondemand_buffer(_buf
);
213 if(pb_ondemand_buffer_instantiate(buf
) != PIPE_OK
) {
215 *base_buf
= &buf
->base
;
220 pb_get_base_buffer(buf
->buffer
, base_buf
, offset
);
225 pb_ondemand_buffer_vtbl
= {
226 pb_ondemand_buffer_destroy
,
227 pb_ondemand_buffer_map
,
228 pb_ondemand_buffer_unmap
,
229 pb_ondemand_buffer_validate
,
230 pb_ondemand_buffer_fence
,
231 pb_ondemand_buffer_get_base_buffer
235 static struct pb_buffer
*
236 pb_ondemand_manager_create_buffer(struct pb_manager
*_mgr
,
238 const struct pb_desc
*desc
)
240 struct pb_ondemand_manager
*mgr
= pb_ondemand_manager(_mgr
);
241 struct pb_ondemand_buffer
*buf
;
243 buf
= CALLOC_STRUCT(pb_ondemand_buffer
);
247 pipe_reference_init(&buf
->base
.reference
, 1);
248 buf
->base
.alignment
= desc
->alignment
;
249 buf
->base
.usage
= desc
->usage
;
250 buf
->base
.size
= size
;
251 buf
->base
.vtbl
= &pb_ondemand_buffer_vtbl
;
255 buf
->data
= align_malloc(size
, desc
->alignment
< sizeof(void*) ? sizeof(void*) : desc
->alignment
);
269 pb_ondemand_manager_flush(struct pb_manager
*_mgr
)
271 struct pb_ondemand_manager
*mgr
= pb_ondemand_manager(_mgr
);
273 mgr
->provider
->flush(mgr
->provider
);
278 pb_ondemand_manager_destroy(struct pb_manager
*_mgr
)
280 struct pb_ondemand_manager
*mgr
= pb_ondemand_manager(_mgr
);
287 pb_ondemand_manager_create(struct pb_manager
*provider
)
289 struct pb_ondemand_manager
*mgr
;
294 mgr
= CALLOC_STRUCT(pb_ondemand_manager
);
298 mgr
->base
.destroy
= pb_ondemand_manager_destroy
;
299 mgr
->base
.create_buffer
= pb_ondemand_manager_create_buffer
;
300 mgr
->base
.flush
= pb_ondemand_manager_flush
;
302 mgr
->provider
= provider
;