mesa: Change "BRIAN PAUL" to "THE AUTHORS" in license text.
[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 * THE AUTHORS 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 "dri_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 psp = CALLOC_STRUCT(__DRIscreenRec);
58 if (!psp)
59 return NULL;
60
61 setupLoaderExtensions(psp, extensions);
62
63 psp->loaderPrivate = data;
64
65 psp->extensions = emptyExtensionList;
66 psp->fd = -1;
67 psp->myNum = scrn;
68
69 *driver_configs = driDriverAPI.InitScreen(psp);
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 free(psp);
83 }
84 }
85
86 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
87 {
88 return psp->extensions;
89 }
90
91
92 /**
93 * Context functions
94 */
95
96 static __DRIcontext *
97 driCreateContextAttribs(__DRIscreen *screen, int api,
98 const __DRIconfig *config,
99 __DRIcontext *shared,
100 unsigned num_attribs,
101 const uint32_t *attribs,
102 unsigned *error,
103 void *data)
104 {
105 __DRIcontext *pcp;
106 const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
107 void * const shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
108 gl_api mesa_api;
109 unsigned major_version = 1;
110 unsigned minor_version = 0;
111 uint32_t flags = 0;
112
113 /* Either num_attribs is zero and attribs is NULL, or num_attribs is not
114 * zero and attribs is not NULL.
115 */
116 assert((num_attribs == 0) == (attribs == NULL));
117
118 switch (api) {
119 case __DRI_API_OPENGL:
120 mesa_api = API_OPENGL_COMPAT;
121 break;
122 case __DRI_API_GLES:
123 mesa_api = API_OPENGLES;
124 break;
125 case __DRI_API_GLES2:
126 case __DRI_API_GLES3:
127 mesa_api = API_OPENGLES2;
128 break;
129 case __DRI_API_OPENGL_CORE:
130 mesa_api = API_OPENGL_CORE;
131 break;
132 default:
133 *error = __DRI_CTX_ERROR_BAD_API;
134 return NULL;
135 }
136
137 for (unsigned i = 0; i < num_attribs; i++) {
138 switch (attribs[i * 2]) {
139 case __DRI_CTX_ATTRIB_MAJOR_VERSION:
140 major_version = attribs[i * 2 + 1];
141 break;
142 case __DRI_CTX_ATTRIB_MINOR_VERSION:
143 minor_version = attribs[i * 2 + 1];
144 break;
145 case __DRI_CTX_ATTRIB_FLAGS:
146 flags = attribs[i * 2 + 1];
147 break;
148 default:
149 /* We can't create a context that satisfies the requirements of an
150 * attribute that we don't understand. Return failure.
151 */
152 return NULL;
153 }
154 }
155
156 /* Mesa does not support the GL_ARB_compatibilty extension or the
157 * compatibility profile. This means that we treat a API_OPENGL_COMPAT 3.1 as
158 * API_OPENGL_CORE and reject API_OPENGL_COMPAT 3.2+.
159 */
160 if (mesa_api == API_OPENGL_COMPAT && major_version == 3 && minor_version == 1)
161 mesa_api = API_OPENGL_CORE;
162
163 if (mesa_api == API_OPENGL_COMPAT
164 && ((major_version > 3)
165 || (major_version == 3 && minor_version >= 2))) {
166 *error = __DRI_CTX_ERROR_BAD_API;
167 return NULL;
168 }
169 /* There are no forward-compatible contexts before OpenGL 3.0. The
170 * GLX_ARB_create_context spec says:
171 *
172 * "Forward-compatible contexts are defined only for OpenGL versions
173 * 3.0 and later."
174 *
175 * Moreover, Mesa can't fulfill the requirements of a forward-looking
176 * context. Return failure if a forward-looking context is requested.
177 *
178 * In Mesa, a debug context is the same as a regular context.
179 */
180 if (major_version >= 3) {
181 if ((flags & ~__DRI_CTX_FLAG_DEBUG) != 0)
182 return NULL;
183 }
184
185 pcp = CALLOC_STRUCT(__DRIcontextRec);
186 if (!pcp)
187 return NULL;
188
189 pcp->loaderPrivate = data;
190
191 pcp->driScreenPriv = screen;
192 pcp->driDrawablePriv = NULL;
193 pcp->driReadablePriv = NULL;
194
195 if (!driDriverAPI.CreateContext(mesa_api, modes, pcp,
196 major_version, minor_version,
197 flags, error, shareCtx)) {
198 free(pcp);
199 return NULL;
200 }
201
202 return pcp;
203 }
204
205 static __DRIcontext *
206 driCreateNewContextForAPI(__DRIscreen *psp, int api,
207 const __DRIconfig *config,
208 __DRIcontext *shared, void *data)
209 {
210 unsigned error;
211
212 return driCreateContextAttribs(psp, api, config, shared, 0, NULL,
213 &error, data);
214 }
215
216 static __DRIcontext *
217 driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
218 __DRIcontext *shared, void *data)
219 {
220 return driCreateNewContextForAPI(psp, __DRI_API_OPENGL,
221 config, shared, data);
222 }
223
224 static void
225 driDestroyContext(__DRIcontext *pcp)
226 {
227 if (pcp) {
228 driDriverAPI.DestroyContext(pcp);
229 free(pcp);
230 }
231 }
232
233 static int
234 driCopyContext(__DRIcontext *dst, __DRIcontext *src, unsigned long mask)
235 {
236 return GL_FALSE;
237 }
238
239 static void dri_get_drawable(__DRIdrawable *pdp);
240 static void dri_put_drawable(__DRIdrawable *pdp);
241
242 static int driBindContext(__DRIcontext *pcp,
243 __DRIdrawable *pdp,
244 __DRIdrawable *prp)
245 {
246 /* Bind the drawable to the context */
247 if (pcp) {
248 pcp->driDrawablePriv = pdp;
249 pcp->driReadablePriv = prp;
250 if (pdp) {
251 pdp->driContextPriv = pcp;
252 dri_get_drawable(pdp);
253 }
254 if (prp && pdp != prp) {
255 dri_get_drawable(prp);
256 }
257 }
258
259 return driDriverAPI.MakeCurrent(pcp, pdp, prp);
260 }
261
262 static int driUnbindContext(__DRIcontext *pcp)
263 {
264 __DRIdrawable *pdp;
265 __DRIdrawable *prp;
266
267 if (pcp == NULL)
268 return GL_FALSE;
269
270 pdp = pcp->driDrawablePriv;
271 prp = pcp->driReadablePriv;
272
273 /* already unbound */
274 if (!pdp && !prp)
275 return GL_TRUE;
276
277 driDriverAPI.UnbindContext(pcp);
278
279 dri_put_drawable(pdp);
280
281 if (prp != pdp) {
282 dri_put_drawable(prp);
283 }
284
285 pcp->driDrawablePriv = NULL;
286 pcp->driReadablePriv = NULL;
287
288 return GL_TRUE;
289 }
290
291
292 /**
293 * Drawable functions
294 */
295
296 static void dri_get_drawable(__DRIdrawable *pdp)
297 {
298 pdp->refcount++;
299 }
300
301 static void dri_put_drawable(__DRIdrawable *pdp)
302 {
303 if (pdp) {
304 pdp->refcount--;
305 if (pdp->refcount)
306 return;
307
308 driDriverAPI.DestroyBuffer(pdp);
309 free(pdp);
310 }
311 }
312
313 static __DRIdrawable *
314 driCreateNewDrawable(__DRIscreen *psp,
315 const __DRIconfig *config, void *data)
316 {
317 __DRIdrawable *pdp;
318
319 pdp = CALLOC_STRUCT(__DRIdrawableRec);
320 if (!pdp)
321 return NULL;
322
323 pdp->loaderPrivate = data;
324
325 pdp->driScreenPriv = psp;
326 pdp->driContextPriv = NULL;
327
328 dri_get_drawable(pdp);
329
330 if (!driDriverAPI.CreateBuffer(psp, pdp, &config->modes, GL_FALSE)) {
331 free(pdp);
332 return NULL;
333 }
334
335 pdp->lastStamp = 1; /* const */
336
337 return pdp;
338 }
339
340 static void
341 driDestroyDrawable(__DRIdrawable *pdp)
342 {
343 dri_put_drawable(pdp);
344 }
345
346 static void driSwapBuffers(__DRIdrawable *pdp)
347 {
348 driDriverAPI.SwapBuffers(pdp);
349 }
350
351 const __DRIcoreExtension driCoreExtension = {
352 { __DRI_CORE, __DRI_CORE_VERSION },
353 NULL, /* driCreateNewScreen */
354 driDestroyScreen,
355 driGetExtensions,
356 driGetConfigAttrib,
357 driIndexConfigAttrib,
358 NULL, /* driCreateNewDrawable */
359 driDestroyDrawable,
360 driSwapBuffers,
361 driCreateNewContext,
362 driCopyContext,
363 driDestroyContext,
364 driBindContext,
365 driUnbindContext
366 };
367
368 const __DRIswrastExtension driSWRastExtension = {
369 { __DRI_SWRAST, __DRI_SWRAST_VERSION },
370 driCreateNewScreen,
371 driCreateNewDrawable,
372 driCreateNewContextForAPI,
373 driCreateContextAttribs
374 };