3b341c64c280c07a30fe87d71e16620735f6707a
[mesa.git] / src / mesa / pipe / pipebuffer / pb_bufmgr_fenced.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
4 * All Rights Reserved.
5 *
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:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 *
26 *
27 **************************************************************************/
28
29 /**
30 * \file
31 * A buffer manager that wraps buffers in fenced buffers.
32 *
33 * \author José Fonseca <jrfonseca@tungstengraphics.dot.com>
34 */
35
36
37 #include <assert.h>
38 #include <stdlib.h>
39
40 #include "p_util.h"
41
42 #include "pb_buffer.h"
43 #include "pb_buffer_fenced.h"
44 #include "pb_bufmgr.h"
45
46
47 struct fenced_pb_manager
48 {
49 struct pb_manager base;
50
51 struct pb_manager *provider;
52
53 struct fenced_buffer_list *fenced_list;
54 };
55
56
57 static INLINE struct fenced_pb_manager *
58 fenced_pb_manager(struct pb_manager *mgr)
59 {
60 assert(mgr);
61 return (struct fenced_pb_manager *)mgr;
62 }
63
64
65 static struct pb_buffer *
66 fenced_bufmgr_create_buffer(struct pb_manager *mgr,
67 size_t size,
68 const struct pb_desc *desc)
69 {
70 struct fenced_pb_manager *fenced_mgr = fenced_pb_manager(mgr);
71 struct pb_buffer *buf;
72 struct pb_buffer *fenced_buf;
73
74 /* check for free buffers before allocating new ones */
75 fenced_buffer_list_check_free(fenced_mgr->fenced_list, 0);
76
77 buf = fenced_mgr->provider->create_buffer(fenced_mgr->provider, size, desc);
78 if(!buf) {
79 /* try harder to get a buffer */
80 fenced_buffer_list_check_free(fenced_mgr->fenced_list, 1);
81
82 buf = fenced_mgr->provider->create_buffer(fenced_mgr->provider, size, desc);
83 if(!buf) {
84 /* give up */
85 return NULL;
86 }
87 }
88
89 fenced_buf = fenced_buffer_create(fenced_mgr->fenced_list, buf);
90 if(!fenced_buf) {
91 assert(buf->base.refcount == 1);
92 pb_destroy(buf);
93 }
94
95 return fenced_buf;
96 }
97
98
99 static void
100 fenced_bufmgr_destroy(struct pb_manager *mgr)
101 {
102 struct fenced_pb_manager *fenced_mgr = fenced_pb_manager(mgr);
103
104 fenced_buffer_list_destroy(fenced_mgr->fenced_list);
105
106 fenced_mgr->provider->destroy(fenced_mgr->provider);
107
108 FREE(fenced_mgr);
109 }
110
111
112 struct pb_manager *
113 fenced_bufmgr_create(struct pb_manager *provider,
114 struct pipe_winsys *winsys)
115 {
116 struct fenced_pb_manager *fenced_mgr;
117
118 fenced_mgr = (struct fenced_pb_manager *)CALLOC(1, sizeof(*fenced_mgr));
119 if (!fenced_mgr)
120 return NULL;
121
122 fenced_mgr->base.destroy = fenced_bufmgr_destroy;
123 fenced_mgr->base.create_buffer = fenced_bufmgr_create_buffer;
124
125 fenced_mgr->provider = provider;
126 fenced_mgr->fenced_list = fenced_buffer_list_create(winsys);
127 if(!fenced_mgr->fenced_list) {
128 FREE(fenced_mgr);
129 return NULL;
130 }
131
132 return &fenced_mgr->base;
133 }