Drop GLcontext typedef and use struct gl_context instead
[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->fd = -1;
67 psp->myNum = scrn;
68
69 *driver_configs = driDriverAPI.InitScreen(psp);
70
71 if (*driver_configs == NULL) {
72 FREE(psp);
73 return NULL;
74 }
75
76 return psp;
77 }
78
79 static void driDestroyScreen(__DRIscreen *psp)
80 {
81 if (psp) {
82 driDriverAPI.DestroyScreen(psp);
83
84 FREE(psp);
85 }
86 }
87
88 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
89 {
90 return psp->extensions;
91 }
92
93
94 /**
95 * Context functions
96 */
97
98 static __DRIcontext *
99 driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
100 __DRIcontext *shared, void *data)
101 {
102 __DRIcontext *pcp;
103 void * const shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
104
105 pcp = CALLOC_STRUCT(__DRIcontextRec);
106 if (!pcp)
107 return NULL;
108
109 pcp->loaderPrivate = data;
110
111 pcp->driScreenPriv = psp;
112 pcp->driDrawablePriv = NULL;
113 pcp->driReadablePriv = NULL;
114
115 if (!driDriverAPI.CreateContext(API_OPENGL,
116 &config->modes, pcp, shareCtx)) {
117 FREE(pcp);
118 return NULL;
119 }
120
121 return pcp;
122 }
123
124 static void
125 driDestroyContext(__DRIcontext *pcp)
126 {
127 if (pcp) {
128 driDriverAPI.DestroyContext(pcp);
129 FREE(pcp);
130 }
131 }
132
133 static int
134 driCopyContext(__DRIcontext *dst, __DRIcontext *src, unsigned long mask)
135 {
136 return GL_FALSE;
137 }
138
139 static void dri_get_drawable(__DRIdrawable *pdp);
140 static void dri_put_drawable(__DRIdrawable *pdp);
141
142 static int driBindContext(__DRIcontext *pcp,
143 __DRIdrawable *pdp,
144 __DRIdrawable *prp)
145 {
146 /* Bind the drawable to the context */
147 if (pcp) {
148 pcp->driDrawablePriv = pdp;
149 pcp->driReadablePriv = prp;
150 if (pdp) {
151 pdp->driContextPriv = pcp;
152 dri_get_drawable(pdp);
153 }
154 if ( prp && pdp != prp ) {
155 dri_get_drawable(prp);
156 }
157 }
158
159 return driDriverAPI.MakeCurrent(pcp, pdp, prp);
160 }
161
162 static int driUnbindContext(__DRIcontext *pcp)
163 {
164 __DRIdrawable *pdp;
165 __DRIdrawable *prp;
166
167 if (pcp == NULL)
168 return GL_FALSE;
169
170 pdp = pcp->driDrawablePriv;
171 prp = pcp->driReadablePriv;
172
173 /* already unbound */
174 if (!pdp && !prp)
175 return GL_TRUE;
176
177 driDriverAPI.UnbindContext(pcp);
178
179 dri_put_drawable(pdp);
180
181 if (prp != pdp) {
182 dri_put_drawable(prp);
183 }
184
185 pcp->driDrawablePriv = NULL;
186 pcp->driReadablePriv = NULL;
187
188 return GL_TRUE;
189 }
190
191
192 /**
193 * Drawable functions
194 */
195
196 static void dri_get_drawable(__DRIdrawable *pdp)
197 {
198 pdp->refcount++;
199 }
200
201 static void dri_put_drawable(__DRIdrawable *pdp)
202 {
203 if (pdp) {
204 pdp->refcount--;
205 if (pdp->refcount)
206 return;
207
208 driDriverAPI.DestroyBuffer(pdp);
209
210 FREE(pdp);
211 }
212 }
213
214 static __DRIdrawable *
215 driCreateNewDrawable(__DRIscreen *psp,
216 const __DRIconfig *config, void *data)
217 {
218 __DRIdrawable *pdp;
219
220 pdp = CALLOC_STRUCT(__DRIdrawableRec);
221 if (!pdp)
222 return NULL;
223
224 pdp->loaderPrivate = data;
225
226 pdp->driScreenPriv = psp;
227 pdp->driContextPriv = NULL;
228
229 dri_get_drawable(pdp);
230
231 if (!driDriverAPI.CreateBuffer(psp, pdp, &config->modes, GL_FALSE)) {
232 FREE(pdp);
233 return NULL;
234 }
235
236 pdp->lastStamp = 1; /* const */
237
238 return pdp;
239 }
240
241 static void
242 driDestroyDrawable(__DRIdrawable *pdp)
243 {
244 dri_put_drawable(pdp);
245 }
246
247 static void driSwapBuffers(__DRIdrawable *pdp)
248 {
249 driDriverAPI.SwapBuffers(pdp);
250 }
251
252 const __DRIcoreExtension driCoreExtension = {
253 { __DRI_CORE, __DRI_CORE_VERSION },
254 NULL, /* driCreateNewScreen */
255 driDestroyScreen,
256 driGetExtensions,
257 driGetConfigAttrib,
258 driIndexConfigAttrib,
259 NULL, /* driCreateNewDrawable */
260 driDestroyDrawable,
261 driSwapBuffers,
262 driCreateNewContext,
263 driCopyContext,
264 driDestroyContext,
265 driBindContext,
266 driUnbindContext
267 };
268
269 const __DRIswrastExtension driSWRastExtension = {
270 { __DRI_SWRAST, __DRI_SWRAST_VERSION },
271 driCreateNewScreen,
272 driCreateNewDrawable
273 };