remove one of the two _tnl_isolate_materials function calls
[mesa.git] / src / mesa / drivers / ggi / ggimesa.c
1 /* GGI-Driver for MESA
2 *
3 * Copyright (C) 1997-1998 Uwe Maurer - uwe_maurer@t-online.de
4 * 2002 Filip Spacek
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * ---------------------------------------------------------------------
20 * This code was derived from the following source of information:
21 *
22 * svgamesa.c and ddsample.c by Brian Paul
23 *
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "conf.h"
28 #endif
29
30 #include <ggi/mesa/ggimesa_int.h>
31 #include <ggi/mesa/debug.h>
32 #include "extensions.h"
33 #include "buffers.h"
34 #include "colormac.h"
35 #include "imports.h"
36 #include "matrix.h"
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
39 #include "tnl/tnl.h"
40 #include "tnl/t_context.h"
41 #include "tnl/t_pipeline.h"
42 #include "array_cache/acache.h"
43 #include "teximage.h"
44 #include "texformat.h"
45 #include "texstore.h"
46
47 /* We use LibGG to manage config files */
48 #include <ggi/gg.h>
49
50
51 /* XXX: Those #defines should be provided via
52 * config.h
53 */
54 #define GGIMESAPATHTAG "pAtHTAg"
55 #define GGIMESACONFDIR "pAtHTAg/usr/local/etc/ggi"
56 #define GGIMESATAGLEN 7
57 #define GGIMESACONFFILE "ggimesa.conf"
58
59
60 /* Static variables
61 */
62 static int _ggimesaLibIsUp = 0;
63 static void *_ggimesaConfigHandle;
64 static char _ggimesaconfstub[512] = GGIMESACONFDIR;
65 static char *_ggimesaconfdir = _ggimesaconfstub+GGIMESATAGLEN;
66
67 int _ggimesaDebugSync = 0;
68 uint32 _ggimesaDebugState = 0;
69
70
71
72 /* Extension ID. Defaulting to -1 should make segfault on abuse more likely...
73 */
74 ggi_extid _ggiMesaID = -1;
75
76
77 #define SUBLIB_PREFIX "MesaGGIdl_"
78
79
80 /*
81 * Returns the directory where global config files are kept
82 */
83
84 const char *ggiMesaGetConfDir(void)
85 {
86 #ifdef __WIN32__
87 /* On Win32 we allow overriding of the compiled in path. */
88 const char *envdir = getenv("GGI_CONFDIR");
89 if (envdir) return envdir;
90 #endif
91 return _ggimesaconfdir;
92 }
93
94
95 /* Dummy function which returns -1
96 We use this to reset the function pointers */
97 static int _ggi_error(void)
98 {
99 GGIMESADPRINT_CORE("_ggi_error() called\n");
100
101 return -1;
102 }
103
104
105 static int changed(ggi_visual_t vis, int whatchanged)
106 {
107 GLcontext *ctx;
108 ctx = _mesa_get_current_context();
109
110 GGIMESADPRINT_CORE("changed() called\n");
111
112 switch (whatchanged) {
113 case GGI_CHG_APILIST:
114 {
115 char api[GGI_MAX_APILEN];
116 char args[GGI_MAX_APILEN];
117 int i;
118 const char *fname;
119 ggi_dlhandle *lib;
120
121 GLvisual *gl_vis = &(LIBGGI_MESAEXT(vis)->mesa_visual.gl_visual);
122 GLframebuffer *gl_fb = &(LIBGGI_MESAEXT(vis)->mesa_buffer);
123
124 /* Initialize the framebuffer to provide all necessary
125 buffers in software. The target libraries that are loaded
126 next are free to modify this according to their
127 capabilities.
128 */
129 /* FIXME: if the target changes capabilities we'll leak
130 swrast's memory !!! Need to deallocate first */
131 _mesa_initialize_framebuffer(gl_fb, gl_vis,
132 gl_vis->depthBits > 0,
133 gl_vis->stencilBits > 0,
134 gl_vis->accumRedBits > 0,
135 gl_vis->alphaBits > 0);
136
137 for (i = 0; ggiGetAPI(vis, i, api, args) == 0; i++) {
138 strcat(api, "-mesa");
139 GGIMESADPRINT_CORE("GGIMesa: looking for"
140 "a sublib named %s\n", api);
141 fname = ggMatchConfig(_ggimesaConfigHandle, api, NULL);
142 if (fname == NULL) {
143 /* No special implementation for this sublib */
144 continue;
145 }
146 lib = ggiExtensionLoadDL(vis, fname, args, NULL,
147 SUBLIB_PREFIX);
148 }
149
150 /* The targets have cleared everything they can do from
151 the framebuffer structure so we provide the rest in sw
152 */
153 _swrast_alloc_buffers(gl_fb);
154
155 break;
156 }
157 }
158 return 0;
159 }
160
161
162 int ggiMesaInit()
163 {
164 int err;
165 char *str;
166 char *conffile;
167
168 GGIMESADPRINT_CORE("ggiMesaInit() called\n");
169
170 _ggimesaLibIsUp++;
171 if (_ggimesaLibIsUp > 1) return 0; /* Initialize only at first call */
172
173 str = getenv("GGIMESA_DEBUGSYNC");
174 if (str != NULL) {
175 _ggimesaDebugSync = 1;
176 }
177
178 str = getenv("GGIMESA_DEBUG");
179 if (str != NULL) {
180 _ggimesaDebugState = atoi(str);
181 GGIMESADPRINT_CORE("%s Debugging=%d\n",
182 _ggimesaDebugSync ? "sync" : "async",
183 _ggimesaDebugState);
184 }
185
186
187 conffile = malloc(strlen(ggiMesaGetConfDir()) + 1
188 + strlen(GGIMESACONFFILE) +1);
189 if (conffile == NULL) {
190 fprintf(stderr, "GGIMesa: unable to allocate memory for config filename.\n");
191 return GGI_ENOMEM;
192 }
193 sprintf(conffile, "%s%c%s",
194 ggiMesaGetConfDir(), '/', GGIMESACONFFILE);
195 err = ggLoadConfig(conffile, &_ggimesaConfigHandle);
196 if (err != GGI_OK) {
197 fprintf(stderr, "GGIMesa: Couldn't open %s\n",
198 conffile);
199 free(conffile);
200 _ggimesaLibIsUp--;
201 return err;
202 }
203 free(conffile);
204
205 _ggiMesaID = ggiExtensionRegister("GGIMesa",
206 sizeof(struct ggi_mesa_ext), changed);
207 if (_ggiMesaID < 0) {
208 fprintf(stderr, "GGIMesa: failed to register as extension\n");
209 _ggimesaLibIsUp--;
210 ggFreeConfig(_ggimesaConfigHandle);
211 return _ggiMesaID;
212 }
213
214 return 0;
215 }
216
217 int ggiMesaExit(void)
218 {
219 int rc;
220
221 GGIMESADPRINT_CORE("ggiMesaExit() called\n");
222
223 if (!_ggimesaLibIsUp) return -1;
224
225 if (_ggimesaLibIsUp > 1) {
226 /* Exit only at last call */
227 _ggimesaLibIsUp--;
228 return 0;
229 }
230
231 rc = ggiExtensionUnregister(_ggiMesaID);
232 ggFreeConfig(_ggimesaConfigHandle);
233
234 _ggimesaLibIsUp = 0;
235
236 return rc;
237 }
238
239
240
241
242 static void gl_ggiUpdateState(GLcontext *ctx, GLuint new_state);
243
244
245 static void gl_ggiGetSize(GLframebuffer *fb, GLuint *width, GLuint *height)
246 {
247 /* FIXME: this is a hack to work around the new interface */
248 GLcontext *ctx;
249 ggi_mesa_context_t ggi_ctx;
250 ctx = _mesa_get_current_context();
251 ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
252
253 GGIMESADPRINT_CORE("gl_ggiGetSize() called\n");
254
255 *width = LIBGGI_VIRTX(ggi_ctx->ggi_visual);
256 *height = LIBGGI_VIRTY(ggi_ctx->ggi_visual);
257 printf("returning %d, %d\n", *width, *height);
258 }
259
260 static void gl_ggiViewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
261 {
262 /* poll for window size change and realloc software Z/stencil/etc if needed */
263 _mesa_ResizeBuffersMESA();
264 }
265
266
267 static void gl_ggiSetIndex(GLcontext *ctx, GLuint ci)
268 {
269 ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
270
271 GGIMESADPRINT_CORE("gl_ggiSetIndex() called\n");
272
273 ggiSetGCForeground(ggi_ctx->ggi_visual, ci);
274 ggi_ctx->color = (ggi_pixel)ci;
275 }
276
277 static void gl_ggiSetClearIndex(GLcontext *ctx, GLuint ci)
278 {
279 ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
280
281 GGIMESADPRINT_CORE("gl_ggiSetClearIndex() called\n");
282
283 ggiSetGCForeground(ggi_ctx->ggi_visual, ci);
284 ggi_ctx->clearcolor = (ggi_pixel)ci;
285 }
286
287 static void gl_ggiSetClearColor(GLcontext *ctx, const GLfloat color[4])
288 {
289 ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
290 ggi_color rgb;
291 ggi_pixel col;
292 GLubyte byteColor[3];
293
294 GGIMESADPRINT_CORE("gl_ggiSetClearColor() called\n");
295
296 CLAMPED_FLOAT_TO_UBYTE(byteColor[0], color[0]);
297 CLAMPED_FLOAT_TO_UBYTE(byteColor[1], color[1]);
298 CLAMPED_FLOAT_TO_UBYTE(byteColor[2], color[2]);
299
300 rgb.r = (uint16)byteColor[0] << SHIFT;
301 rgb.g = (uint16)byteColor[1] << SHIFT;
302 rgb.b = (uint16)byteColor[2] << SHIFT;
303 col = ggiMapColor(ggi_ctx->ggi_visual, &rgb);
304 ggiSetGCForeground(ggi_ctx->ggi_visual, col);
305 ggi_ctx->clearcolor = col;
306 }
307
308 static void gl_ggiClear(GLcontext *ctx, GLbitfield mask, GLboolean all,
309 GLint x, GLint y, GLint width, GLint height)
310 {
311 ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
312
313 GGIMESADPRINT_CORE("gl_ggiClear() called\n");
314
315 if (mask & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT)) {
316 ggiSetGCForeground(ggi_ctx->ggi_visual, ggi_ctx->clearcolor);
317
318 if (all) {
319 int w, h;
320 w = LIBGGI_VIRTX(ggi_ctx->ggi_visual);
321 h = LIBGGI_VIRTX(ggi_ctx->ggi_visual);
322 ggiDrawBox(ggi_ctx->ggi_visual, 0, 0, w, h);
323 } else {
324 ggiDrawBox(ggi_ctx->ggi_visual, x, y, //FLIP(y),
325 width, height);
326 }
327 ggiSetGCForeground(ggi_ctx->ggi_visual, ggi_ctx->color);
328
329 mask &= ~(DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT);
330 }
331 _swrast_Clear(ctx, mask, all, x, y, width, height);
332
333 }
334
335
336 /* Set the buffer used for reading */
337 /* XXX support for separate read/draw buffers hasn't been tested */
338 static GLboolean gl_ggiSetBuffer(GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit)
339 {
340 ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
341
342 printf("set read %d\n", bufferBit);
343 GGIMESADPRINT_CORE("gl_ggiSetBuffer() called\n");
344
345 if (bufferBit == DD_FRONT_LEFT_BIT)
346 {
347 ggiSetReadFrame(ggi_ctx->ggi_visual,
348 ggiGetDisplayFrame(ggi_ctx->ggi_visual));
349 ggiSetWriteFrame(ggi_ctx->ggi_visual,
350 ggiGetDisplayFrame(ggi_ctx->ggi_visual));
351 return GL_TRUE;
352 }
353 else if (bufferBit == DD_BACK_LEFT_BIT)
354 {
355 ggiSetReadFrame(ggi_ctx->ggi_visual,
356 ggiGetDisplayFrame(ggi_ctx->ggi_visual)?0 : 1);
357 ggiSetWriteFrame(ggi_ctx->ggi_visual,
358 ggiGetDisplayFrame(ggi_ctx->ggi_visual)?0 : 1);
359 return GL_TRUE;
360 }
361 else
362 return GL_FALSE;
363 }
364
365
366 static const GLubyte * gl_ggiGetString(GLcontext *ctx, GLenum name)
367 {
368 GGIMESADPRINT_CORE("gl_ggiGetString() called\n");
369
370 if (name == GL_RENDERER) {
371 return (GLubyte *) "Mesa GGI";
372 } else {
373 return NULL;
374 }
375 }
376
377 static void gl_ggiFlush(GLcontext *ctx)
378 {
379 ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
380
381 GGIMESADPRINT_CORE("gl_ggiFlush() called\n");
382
383 ggiFlush(ggi_ctx->ggi_visual);
384 }
385
386 static void gl_ggiIndexMask(GLcontext *ctx, GLuint mask)
387 {
388 GGIMESADPRINT_CORE("gl_ggiIndexMask() called\n");
389 }
390
391 static void gl_ggiColorMask(GLcontext *ctx, GLboolean rmask, GLboolean gmask,
392 GLboolean bmask, GLboolean amask)
393 {
394 GGIMESADPRINT_CORE("gl_ggiColorMask() called\n");
395 }
396
397 static void gl_ggiEnable(GLcontext *ctx, GLenum pname, GLboolean state)
398 {
399 GGIMESADPRINT_CORE("gl_ggiEnable() called\n");
400 }
401
402 static void gl_ggiSetupPointers(GLcontext *ctx)
403 {
404 TNLcontext *tnl;
405
406 GGIMESADPRINT_CORE("gl_ggiSetupPointers() called\n");
407
408 /* General information */
409 ctx->Driver.GetString = gl_ggiGetString;
410 ctx->Driver.GetBufferSize = gl_ggiGetSize;
411 ctx->Driver.Viewport = gl_ggiViewport;
412 ctx->Driver.Finish = gl_ggiFlush;
413 ctx->Driver.Flush = gl_ggiFlush;
414
415 /* Software rasterizer pixel paths */
416 ctx->Driver.Accum = _swrast_Accum;
417 ctx->Driver.Bitmap = _swrast_Bitmap;
418 ctx->Driver.Clear = gl_ggiClear;
419 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
420 ctx->Driver.CopyPixels = _swrast_CopyPixels;
421 ctx->Driver.DrawPixels = _swrast_DrawPixels;
422 ctx->Driver.ReadPixels = _swrast_ReadPixels;
423 ctx->Driver.DrawBuffer = _swrast_DrawBuffer;
424
425 /* Software texturing */
426 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
427 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
428 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
429 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
430 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
431 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
432 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
433 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
434
435 ctx->Driver.CompressedTexImage1D = _mesa_store_compressed_teximage1d;
436 ctx->Driver.CompressedTexImage2D = _mesa_store_compressed_teximage2d;
437 ctx->Driver.CompressedTexImage3D = _mesa_store_compressed_teximage3d;
438 ctx->Driver.CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
439 ctx->Driver.CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
440 ctx->Driver.CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
441
442 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
443 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
444 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
445 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
446 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
447
448 /* Imaging extensions */
449 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
450 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
451 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
452 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
453
454 /* State change callbacks */
455 ctx->Driver.ClearIndex = gl_ggiSetClearIndex;
456 ctx->Driver.ClearColor = gl_ggiSetClearColor;
457 ctx->Driver.IndexMask = gl_ggiIndexMask;
458 ctx->Driver.ColorMask = gl_ggiColorMask;
459 ctx->Driver.Enable = gl_ggiEnable;
460
461 ctx->Driver.UpdateState = gl_ggiUpdateState;
462
463 /* Initialize TNL driver interface */
464 tnl = TNL_CONTEXT(ctx);
465 tnl->Driver.RunPipeline = _tnl_run_pipeline;
466
467 /* Install setup for tnl */
468 _swsetup_Wakeup(ctx);
469 }
470
471 static void get_mode_info(ggi_visual_t vis, int *r, int *g, int *b,
472 GLboolean *rgb, GLboolean *db, int *ci)
473 {
474 unsigned int i;
475
476 *r = 0;
477 *g = 0;
478 *b = 0;
479
480 for(i = 0; i < sizeof(ggi_pixel)*8; ++i) {
481 int mask = 1 << i;
482 if (LIBGGI_PIXFMT(vis)->red_mask & mask)
483 ++(*r);
484 if (LIBGGI_PIXFMT(vis)->green_mask & mask)
485 ++(*g);
486 if (LIBGGI_PIXFMT(vis)->blue_mask & mask)
487 ++(*b);
488 }
489
490 *rgb = GT_SCHEME(LIBGGI_MODE(vis)->graphtype) == GT_TRUECOLOR;
491 *db = LIBGGI_MODE(vis)->frames > 1;
492 *ci = GT_SIZE(LIBGGI_MODE(vis)->graphtype);
493
494 printf("rgb (%d, %d, %d) db %d, rgb %d ci %d\n",*r,*g,*b,*db,*rgb,*ci);
495 }
496
497
498 int ggiMesaAttach(ggi_visual_t vis)
499 {
500 int rc;
501
502 GGIMESADPRINT_CORE("ggiMesaAttach() called\n");
503
504 rc = ggiExtensionAttach(vis, _ggiMesaID);
505 if (rc == 0)
506 {
507 int r, g, b, ci;
508 GLboolean rgb, db;
509 GLvisual *gl_visual;
510
511 /* We are creating the primary instance */
512 memset(LIBGGI_MESAEXT(vis), 0, sizeof(struct ggi_mesa_ext));
513 LIBGGI_MESAEXT(vis)->update_state = (void *)_ggi_error;
514 LIBGGI_MESAEXT(vis)->setup_driver = (void *)_ggi_error;
515
516 /* Initialize default mesa visual */
517 get_mode_info(vis, &r, &g, &b, &rgb, &db, &ci);
518 gl_visual = &(LIBGGI_MESAEXT(vis)->mesa_visual.gl_visual);
519 _mesa_initialize_visual(gl_visual,
520 rgb, db, 0 /* No stereo */,
521 r, g, b, 0 /* No alpha */, ci,
522 0 /* No depth */, 0 /* No stencil */,
523 0, 0, 0, 0 /* No accum */, 0);
524
525 /* Now fake an "API change" so the right libs get loaded */
526 changed(vis, GGI_CHG_APILIST);
527 }
528
529 return rc;
530 }
531
532 int ggiMesaDetach(ggi_visual_t vis)
533 {
534 GGIMESADPRINT_CORE("ggiMesaDetach() called\n");
535
536 return ggiExtensionDetach(vis, _ggiMesaID);
537 }
538
539 int ggiMesaExtendVisual(ggi_visual_t vis, GLboolean alpha_flag,
540 GLboolean stereo_flag, GLint depth_size,
541 GLint stencil_size, GLint accum_red_size,
542 GLint accum_green_size, GLint accum_blue_size,
543 GLint accum_alpha_size, GLint num_samples)
544 {
545 GLvisual *gl_vis = &(LIBGGI_MESAEXT(vis)->mesa_visual.gl_visual);
546 int r, g, b, ci;
547 GLboolean db, rgb;
548
549 get_mode_info(vis, &r, &g, &b, &rgb, &db, &ci);
550
551 /* Initialize the visual with the provided information */
552 _mesa_initialize_visual(gl_vis,
553 rgb, db, stereo_flag,
554 r, g, b, 0 /* FIXME */, ci,
555 depth_size, stencil_size,
556 accum_red_size, accum_green_size,
557 accum_blue_size, accum_alpha_size, 0);
558
559 /* Now fake an "API change" so the right libs get loaded. After all,
560 extending the visual by all these new buffers could be considered
561 a "mode change" which requires an "API change".
562 */
563 changed(vis, GGI_CHG_APILIST);
564
565 return 0;
566 }
567
568
569 ggi_mesa_context_t ggiMesaCreateContext(ggi_visual_t vis)
570 {
571 ggi_mesa_context_t ctx;
572 int err;
573
574 GGIMESADPRINT_CORE("ggiMesaCreateContext() called\n");
575
576 ctx = (ggi_mesa_context_t)malloc(sizeof(struct ggi_mesa_context));
577 if (!ctx)
578 return NULL;
579
580 ctx->ggi_visual = vis;
581 ctx->color = 0;
582
583 ctx->gl_ctx =
584 _mesa_create_context(&(LIBGGI_MESAEXT(vis)->mesa_visual.gl_visual),
585 NULL, (void *) ctx, GL_FALSE);
586 if (!ctx->gl_ctx)
587 goto free_context;
588
589 _mesa_enable_sw_extensions(ctx->gl_ctx);
590
591 _swrast_CreateContext(ctx->gl_ctx);
592 _ac_CreateContext(ctx->gl_ctx);
593 _tnl_CreateContext(ctx->gl_ctx);
594 _swsetup_CreateContext(ctx->gl_ctx);
595
596 gl_ggiSetupPointers(ctx->gl_ctx);
597
598 /* Make sure that an appropriate sublib has been loaded */
599 if (!LIBGGI_MESAEXT(ctx->ggi_visual)->setup_driver){
600 GGIMESADPRINT_CORE("setup_driver==NULL!\n");
601 GGIMESADPRINT_CORE("Please check your config files!\n");
602 goto free_context;
603 }
604
605 /* Set up the sublib driver */
606 err = LIBGGI_MESAEXT(ctx->ggi_visual)->setup_driver(ctx);
607 if (err){
608 GGIMESADPRINT_CORE("setup_driver failed (err = %d)", err);
609 goto free_gl_context;
610 }
611
612 return ctx;
613
614 free_gl_context:
615 _mesa_destroy_context(ctx->gl_ctx);
616 free_context:
617 free(ctx);
618
619 return NULL;
620 }
621
622 void ggiMesaDestroyContext(ggi_mesa_context_t ctx)
623 {
624 GGIMESADPRINT_CORE("ggiMesaDestroyContext() called\n");
625
626 if(!ctx)
627 return;
628
629 _mesa_destroy_context(ctx->gl_ctx);
630 free(ctx);
631 }
632
633 void ggiMesaMakeCurrent(ggi_mesa_context_t ctx, ggi_visual_t vis)
634 {
635 GGIMESADPRINT_CORE("ggiMesaMakeCurrent(ctx = %p) called\n", ctx);
636
637 /* FIXME: clean up where are ggi_vis */
638 if (ctx->ggi_visual != vis) {
639 GGIMESADPRINT_CORE("Cannot migrate GL contexts\n");
640 return;
641 }
642
643 _mesa_make_current(ctx->gl_ctx, &LIBGGI_MESAEXT(vis)->mesa_buffer);
644 }
645
646
647 /*
648 * Swap front/back buffers for current context if double buffered.
649 */
650 void ggiMesaSwapBuffers(void)
651 {
652 GLcontext *ctx;
653 ggi_mesa_context_t ggi_ctx;
654 ctx = _mesa_get_current_context();
655 ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
656
657 GGIMESADPRINT_CORE("ggiMesaSwapBuffers() called\n");
658
659 _mesa_notifySwapBuffers(ctx);
660 gl_ggiFlush(ctx);
661
662 ggiSetDisplayFrame(ggi_ctx->ggi_visual,
663 !ggiGetDisplayFrame(ggi_ctx->ggi_visual));
664 ggiSetWriteFrame(ggi_ctx->ggi_visual,
665 !ggiGetWriteFrame(ggi_ctx->ggi_visual));
666 ggiSetReadFrame(ggi_ctx->ggi_visual,
667 !ggiGetReadFrame(ggi_ctx->ggi_visual));
668
669 GGIMESADPRINT_CORE("swap disp: %d, write %d\n",
670 ggiGetDisplayFrame(ggi_ctx->ggi_visual),
671 ggiGetWriteFrame(ggi_ctx->ggi_visual));
672 }
673
674 static void gl_ggiUpdateState(GLcontext *ctx, GLuint new_state)
675 {
676 ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
677
678 GGIMESADPRINT_CORE("gl_ggiUpdateState() called\n");
679
680 /* Propogate statechange information to swrast and swrast_setup
681 * modules. The GGI driver has no internal GL-dependent state.
682 */
683 _swrast_InvalidateState(ctx, new_state);
684 _swsetup_InvalidateState(ctx, new_state);
685 _tnl_InvalidateState(ctx, new_state);
686
687 /* XXX: Better use an assertion that bails out here on failure */
688 if (!LIBGGI_MESAEXT(ggi_ctx->ggi_visual)->update_state) {
689 GGIMESADPRINT_CORE("update_state == NULL!\n");
690 GGIMESADPRINT_CORE("Please check your config files!\n");
691 ggiPanic("");
692 }
693
694 LIBGGI_MESAEXT(ggi_ctx->ggi_visual)->update_state(ggi_ctx);
695 }
696