rename dri_sw to drisw_util for consistency
[mesa.git] / src / mesa / drivers / dri / common / drisw_util.c
1 /*
2 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
3 * All Rights Reserved.
4 * Copyright 2010 George Sapountzis <gsapountzis@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file drisw_util.c
26 *
27 * DRISW utility functions, i.e. dri_util.c stripped from drm-specific bits.
28 */
29
30 #include "drisw_util.h"
31 #include "utils.h"
32
33
34 /**
35 * Screen functions
36 */
37
38 static void
39 setupLoaderExtensions(__DRIscreen *psp,
40 const __DRIextension **extensions)
41 {
42 int i;
43
44 for (i = 0; extensions[i]; i++) {
45 if (strcmp(extensions[i]->name, __DRI_SWRAST_LOADER) == 0)
46 psp->swrast_loader = (__DRIswrastLoaderExtension *) extensions[i];
47 }
48 }
49
50 static __DRIscreen *
51 driCreateNewScreen(int scrn, const __DRIextension **extensions,
52 const __DRIconfig ***driver_configs, void *data)
53 {
54 static const __DRIextension *emptyExtensionList[] = { NULL };
55 __DRIscreen *psp;
56
57 (void) data;
58
59 psp = CALLOC_STRUCT(__DRIscreenRec);
60 if (!psp)
61 return NULL;
62
63 setupLoaderExtensions(psp, extensions);
64
65 psp->extensions = emptyExtensionList;
66 psp->myNum = scrn;
67
68 *driver_configs = driDriverAPI.InitScreen(psp);
69
70 if (*driver_configs == NULL) {
71 FREE(psp);
72 return NULL;
73 }
74
75 return psp;
76 }
77
78 static void driDestroyScreen(__DRIscreen *psp)
79 {
80 if (psp) {
81 driDriverAPI.DestroyScreen(psp);
82
83 FREE(psp);
84 }
85 }
86
87 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
88 {
89 return psp->extensions;
90 }
91
92
93 /**
94 * Context functions
95 */
96
97 static __DRIcontext *
98 driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
99 __DRIcontext *shared, void *data)
100 {
101 __DRIcontext *pcp;
102 void * const shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
103
104 pcp = CALLOC_STRUCT(__DRIcontextRec);
105 if (!pcp)
106 return NULL;
107
108 pcp->loaderPrivate = data;
109
110 pcp->driScreenPriv = psp;
111 pcp->driDrawablePriv = NULL;
112 pcp->driReadablePriv = NULL;
113
114 if (!driDriverAPI.CreateContext(&config->modes, pcp, shareCtx)) {
115 FREE(pcp);
116 return NULL;
117 }
118
119 return pcp;
120 }
121
122 static void
123 driDestroyContext(__DRIcontext *pcp)
124 {
125 if (pcp) {
126 driDriverAPI.DestroyContext(pcp);
127 FREE(pcp);
128 }
129 }
130
131 static int
132 driCopyContext(__DRIcontext *dst, __DRIcontext *src, unsigned long mask)
133 {
134 return GL_FALSE;
135 }
136
137 static void dri_get_drawable(__DRIdrawable *pdp);
138 static void dri_put_drawable(__DRIdrawable *pdp);
139
140 static int driBindContext(__DRIcontext *pcp,
141 __DRIdrawable *pdp,
142 __DRIdrawable *prp)
143 {
144 /* Bind the drawable to the context */
145 if (pcp) {
146 pcp->driDrawablePriv = pdp;
147 pcp->driReadablePriv = prp;
148 if (pdp) {
149 dri_get_drawable(pdp);
150 }
151 if ( prp && pdp != prp ) {
152 dri_get_drawable(prp);
153 }
154 }
155
156 return driDriverAPI.MakeCurrent(pcp, pdp, prp);
157 }
158
159 static int driUnbindContext(__DRIcontext *pcp)
160 {
161 __DRIdrawable *pdp;
162 __DRIdrawable *prp;
163
164 if (pcp == NULL)
165 return GL_FALSE;
166
167 pdp = pcp->driDrawablePriv;
168 prp = pcp->driReadablePriv;
169
170 /* already unbound */
171 if (!pdp && !prp)
172 return GL_TRUE;
173
174 driDriverAPI.UnbindContext(pcp);
175
176 dri_put_drawable(pdp);
177
178 if (prp != pdp) {
179 dri_put_drawable(prp);
180 }
181
182 pcp->driDrawablePriv = NULL;
183 pcp->driReadablePriv = NULL;
184
185 return GL_TRUE;
186 }
187
188
189 /**
190 * Drawable functions
191 */
192
193 static void dri_get_drawable(__DRIdrawable *pdp)
194 {
195 pdp->refcount++;
196 }
197
198 static void dri_put_drawable(__DRIdrawable *pdp)
199 {
200 if (pdp) {
201 pdp->refcount--;
202 if (pdp->refcount)
203 return;
204
205 driDriverAPI.DestroyBuffer(pdp);
206
207 FREE(pdp);
208 }
209 }
210
211 static __DRIdrawable *
212 driCreateNewDrawable(__DRIscreen *psp,
213 const __DRIconfig *config, void *data)
214 {
215 __DRIdrawable *pdp;
216
217 pdp = CALLOC_STRUCT(__DRIdrawableRec);
218 if (!pdp)
219 return NULL;
220
221 pdp->loaderPrivate = data;
222
223 pdp->driScreenPriv = psp;
224
225 dri_get_drawable(pdp);
226
227 if (!driDriverAPI.CreateBuffer(psp, pdp, &config->modes, GL_FALSE)) {
228 FREE(pdp);
229 return NULL;
230 }
231
232 return pdp;
233 }
234
235 static void
236 driDestroyDrawable(__DRIdrawable *pdp)
237 {
238 dri_put_drawable(pdp);
239 }
240
241 static void driSwapBuffers(__DRIdrawable *pdp)
242 {
243 driDriverAPI.SwapBuffers(pdp);
244 }
245
246 const __DRIcoreExtension driCoreExtension = {
247 { __DRI_CORE, __DRI_CORE_VERSION },
248 NULL, /* driCreateNewScreen */
249 driDestroyScreen,
250 driGetExtensions,
251 driGetConfigAttrib,
252 driIndexConfigAttrib,
253 NULL, /* driCreateNewDrawable */
254 driDestroyDrawable,
255 driSwapBuffers,
256 driCreateNewContext,
257 driCopyContext,
258 driDestroyContext,
259 driBindContext,
260 driUnbindContext
261 };
262
263 const __DRIswrastExtension driSWRastExtension = {
264 { __DRI_SWRAST, __DRI_SWRAST_VERSION },
265 driCreateNewScreen,
266 driCreateNewDrawable
267 };