OSDN Git Service

Merge commit 'origin/gallium-0.1' into gallium-0.2
[android-x86/external-mesa.git] / src / gallium / auxiliary / 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 Jose Fonseca <jrfonseca@tungstengraphics.dot.com>
34  */
35
36
37 #include "pipe/p_debug.h"
38 #include "util/u_memory.h"
39
40 #include "pb_buffer.h"
41 #include "pb_buffer_fenced.h"
42 #include "pb_bufmgr.h"
43
44
45 struct fenced_pb_manager
46 {
47    struct pb_manager base;
48
49    struct pb_manager *provider;
50    
51    struct fenced_buffer_list *fenced_list;
52 };
53
54
55 static INLINE struct fenced_pb_manager *
56 fenced_pb_manager(struct pb_manager *mgr)
57 {
58    assert(mgr);
59    return (struct fenced_pb_manager *)mgr;
60 }
61
62
63 static struct pb_buffer *
64 fenced_bufmgr_create_buffer(struct pb_manager *mgr, 
65                             size_t size,
66                             const struct pb_desc *desc)
67 {
68    struct fenced_pb_manager *fenced_mgr = fenced_pb_manager(mgr);
69    struct pb_buffer *buf;
70    struct pb_buffer *fenced_buf;
71
72    /* check for free buffers before allocating new ones */
73    fenced_buffer_list_check_free(fenced_mgr->fenced_list, 0);
74    
75    buf = fenced_mgr->provider->create_buffer(fenced_mgr->provider, size, desc);
76    if(!buf) {
77       /* try harder to get a buffer */
78       fenced_buffer_list_check_free(fenced_mgr->fenced_list, 1);
79       
80       buf = fenced_mgr->provider->create_buffer(fenced_mgr->provider, size, desc);
81       if(!buf) {
82          /* give up */
83          return NULL;
84       }
85    }
86    
87    fenced_buf = fenced_buffer_create(fenced_mgr->fenced_list, buf);
88    if(!fenced_buf) {
89       pb_reference(&buf, NULL);
90    }
91    
92    return fenced_buf;
93 }
94
95
96 static void
97 fenced_bufmgr_flush(struct pb_manager *mgr)
98 {
99    struct fenced_pb_manager *fenced_mgr = fenced_pb_manager(mgr);
100
101    fenced_buffer_list_check_free(fenced_mgr->fenced_list, TRUE);
102
103    assert(fenced_mgr->provider->flush);
104    if(fenced_mgr->provider->flush)
105       fenced_mgr->provider->flush(fenced_mgr->provider);
106 }
107
108
109 static void
110 fenced_bufmgr_destroy(struct pb_manager *mgr)
111 {
112    struct fenced_pb_manager *fenced_mgr = fenced_pb_manager(mgr);
113
114    fenced_buffer_list_destroy(fenced_mgr->fenced_list);
115
116    if(fenced_mgr->provider)
117       fenced_mgr->provider->destroy(fenced_mgr->provider);
118    
119    FREE(fenced_mgr);
120 }
121
122
123 struct pb_manager *
124 fenced_bufmgr_create(struct pb_manager *provider, 
125                      struct pipe_winsys *winsys) 
126 {
127    struct fenced_pb_manager *fenced_mgr;
128
129    if(!provider)
130       return NULL;
131    
132    fenced_mgr = CALLOC_STRUCT(fenced_pb_manager);
133    if (!fenced_mgr)
134       return NULL;
135
136    fenced_mgr->base.destroy = fenced_bufmgr_destroy;
137    fenced_mgr->base.create_buffer = fenced_bufmgr_create_buffer;
138    fenced_mgr->base.flush = fenced_bufmgr_flush;
139
140    fenced_mgr->provider = provider;
141    fenced_mgr->fenced_list = fenced_buffer_list_create(winsys);
142    if(!fenced_mgr->fenced_list) {
143       FREE(fenced_mgr);
144       return NULL;
145    }
146       
147    return &fenced_mgr->base;
148 }