updates from mesa3d@billbaxter.com
[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 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * ---------------------------------------------------------------------
19 * This code was derived from the following source of information:
20 *
21 * svgamesa.c and ddsample.c by Brian Paul
22 *
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "conf.h"
27 #endif
28
29 #ifdef GGI
30
31 #include <ggi/mesa/ggimesa_int.h>
32 #include <ggi/mesa/debug.h>
33
34 #undef VIS
35 #undef FLIP
36 #define VIS (GGIMesa->ggi_vis)
37 #define FLIP(y) (GGIMesa->flip_y-(y))
38
39 GGIMesaContext GGIMesa = NULL; /* the current context */
40
41 ggi_extid ggiMesaID = -1;
42 static int _ggimesaLibIsUp = 0;
43 static void *_ggimesaConfigHandle;
44
45 /* FIXME: These should really be defined in the make system using -Dxxx */
46 #define GGIMESACONFFILE "/usr/local/etc/ggi/ggimesa.conf"
47 #define GGIMESATAGLEN 0
48 static char ggimesaconfstub[512] = GGIMESACONFFILE;
49 static char *ggimesaconffile = ggimesaconfstub + GGIMESATAGLEN;
50
51 int gl_ggi_init = GL_FALSE;
52 int gl_ggi_debug = GL_FALSE;
53
54 static void gl_ggiUpdateState(GLcontext *ctx);
55 static int changed(ggi_visual_t vis, int whatchanged);
56
57 #if 0
58 /* FIXME: Move this debugging stuff to include/ggi/mesa/internal/ */
59 void gl_ggiPrint(char *format,...)
60 {
61 va_list args;
62 va_start(args,format);
63 fprintf(stderr,"GGIMesa: ");
64 vfprintf(stderr,format,args);
65 va_end(args);
66 }
67
68 void gl_ggiDEBUG(char *format,...)
69 {
70 va_list args;
71 if (gl_ggi_debug)
72 {
73 va_start(args,format);
74 fprintf(stderr,"GGIMesa: ");
75 vfprintf(stderr,format,args);
76 va_end(args);
77 }
78 }
79 #endif
80
81 static void gl_ggiGetSize(GLcontext *ctx, GLuint *width, GLuint *height)
82 {
83 *width = GGIMesa->width;
84 *height = GGIMesa->height;
85 }
86
87 static void gl_ggiSetIndex(GLcontext *ctx, GLuint ci)
88 {
89 ggiSetGCForeground(VIS, ci);
90 GGICTX->color = (ggi_pixel)ci;
91 }
92
93 static void gl_ggiSetClearIndex(GLcontext *ctx, GLuint ci)
94 {
95 ggiSetGCForeground(VIS, ci);
96 GGICTX->clearcolor = (ggi_pixel)ci;
97 }
98
99 static void gl_ggiSetColor(GLcontext *ctx, GLubyte red, GLubyte green,
100 GLubyte blue, GLubyte alpha)
101 {
102 ggi_color rgb;
103 ggi_pixel col;
104
105 rgb.r = (uint16)red << SHIFT;
106 rgb.g = (uint16)green << SHIFT;
107 rgb.b = (uint16)blue << SHIFT;
108 col = ggiMapColor(VIS, &rgb);
109 ggiSetGCForeground(VIS, col);
110 GGICTX->color = col;
111 }
112
113 static void gl_ggiSetClearColor(GLcontext *ctx, GLubyte red, GLubyte green,
114 GLubyte blue, GLubyte alpha)
115 {
116 ggi_color rgb;
117 ggi_pixel col;
118 rgb.r = (uint16)red << SHIFT;
119 rgb.g = (uint16)green << SHIFT;
120 rgb.b = (uint16)blue << SHIFT;
121 col = ggiMapColor(VIS, &rgb);
122 ggiSetGCForeground(VIS, col);
123 GGICTX->clearcolor = col;
124 }
125
126 static GLbitfield gl_ggiClear(GLcontext *ctx,GLbitfield mask, GLboolean all,
127 GLint x, GLint y, GLint width, GLint height)
128 {
129
130 if (mask & GL_COLOR_BUFFER_BIT)
131 {
132 ggiSetGCForeground(VIS, GGICTX->clearcolor);
133
134 if (all)
135 {
136 ggiDrawBox(VIS, 0, GGIMesa->origin.y,
137 GGIMesa->width, GGIMesa->height);
138 }
139 else
140 {
141 ggiDrawBox(VIS, x, FLIP(y), width, height);
142 }
143
144 ggiSetGCForeground(VIS, GGICTX->color);
145 }
146 return mask & (~GL_COLOR_BUFFER_BIT);
147 }
148
149 #if 0
150 static GLboolean gl_ggiSetBuffer(GLcontext *ctx, GLenum mode)
151 {
152 if (mode == GL_FRONT)
153 GGICTX->active_buffer = 1;
154 else
155 GGICTX->active_buffer = 0;
156
157 return GL_TRUE;
158 }
159 #endif
160
161 /* Set the buffer used for drawing */
162 static GLboolean gl_ggiSetDrawBuffer(GLcontext *ctx, GLenum mode)
163 {
164 if (mode == GL_FRONT_LEFT)
165 {
166 GGICTX->active_buffer = 1;
167 return GL_TRUE;
168 }
169 else if (mode == GL_BACK_LEFT)
170 {
171 GGICTX->active_buffer = 0;
172 return GL_TRUE;
173 }
174 else
175 {
176 return GL_FALSE;
177 }
178 }
179
180
181 /* Set the buffer used for reading */
182 /* XXX support for separate read/draw buffers hasn't been tested */
183 static void gl_ggiSetReadBuffer(GLcontext *ctx, GLframebuffer *buffer, GLenum mode)
184 {
185 if (mode == GL_FRONT_LEFT)
186 {
187 GGICTX->active_buffer = 1;
188 }
189 else if (mode == GL_BACK_LEFT)
190 {
191 GGICTX->active_buffer = 0;
192 }
193 }
194
195
196 static const GLubyte * gl_ggiGetString(GLcontext *ctx, GLenum name)
197 {
198 if (name == GL_RENDERER)
199 return (GLubyte *) "Mesa GGI";
200 else
201 return NULL;
202 }
203
204 static void gl_ggiFlush(GLcontext *ctx)
205 {
206 ggiFlush(VIS);
207 }
208
209 static void gl_ggiSetupPointers( GLcontext *ctx )
210 {
211 /* Initialize all the pointers in the DD struct. Do this whenever */
212 /* a new context is made current or we change buffers via set_buffer! */
213
214 ctx->Driver.GetString = gl_ggiGetString;
215 ctx->Driver.UpdateState = gl_ggiUpdateState;
216
217 ctx->Driver.ClearIndex = gl_ggiSetClearIndex;
218 ctx->Driver.ClearColor = gl_ggiSetClearColor;
219 ctx->Driver.Clear = gl_ggiClear;
220 ctx->Driver.Index = gl_ggiSetIndex;
221 ctx->Driver.Color = gl_ggiSetColor;
222
223 ctx->Driver.SetDrawBuffer = gl_ggiSetDrawBuffer;
224 ctx->Driver.SetReadBuffer = gl_ggiSetReadBuffer;
225
226 ctx->Driver.GetBufferSize = gl_ggiGetSize;
227 ctx->Driver.Finish = gl_ggiFlush;
228 ctx->Driver.Flush = gl_ggiFlush;
229
230 }
231
232 static int gl_ggiInitInfo(GGIMesaContext ctx, struct ggi_mesa_info *info)
233 {
234 ggi_mode mode;
235
236 ggiGetMode(ctx->ggi_vis, &mode);
237
238 info->depth_bits = DEFAULT_SOFTWARE_DEPTH_BITS;
239 info->stencil_bits = STENCIL_BITS;
240 info->accum_bits = ACCUM_BITS;
241
242 if (info->rgb_flag)
243 {
244 info->rgb_flag = GL_TRUE;
245 info->alpha_flag = GL_FALSE;
246 info->index_bits = 0;
247
248 info->red_bits = 8;
249 info->green_bits = 8;
250 info->blue_bits = 8;
251
252 info->alpha_bits = 0;
253 }
254 else
255 {
256 info->alpha_flag = GL_FALSE;
257
258 info->index_bits = GT_SIZE(mode.graphtype);
259 info->red_bits = info->green_bits =
260 info->blue_bits = info->alpha_bits = 0;
261 }
262
263 return 0;
264 }
265
266 int ggiMesaInit()
267 {
268 int err;
269
270 _ggimesaLibIsUp++;
271 if (_ggimesaLibIsUp > 1)
272 return 0; /* Initialize only at first call */
273
274 err = ggLoadConfig(ggimesaconffile, &_ggimesaConfigHandle);
275 if (err != GGI_OK)
276 {
277 fprintf(stderr, "GGIMesa: Couldn't open %s\n", ggimesaconffile);
278 _ggimesaLibIsUp--;
279 return err;
280 }
281
282 ggiMesaID = ggiExtensionRegister("GGIMesa", sizeof(struct mesa_ext), changed);
283
284 if (ggiMesaID < 0)
285 {
286 fprintf(stderr, "GGIMesa: failed to register as extension\n");
287 _ggimesaLibIsUp--;
288 ggFreeConfig(_ggimesaConfigHandle);
289 return ggiMesaID;
290 }
291
292 return 0;
293 }
294
295
296 GGIMesaContext GGIMesaCreateContext(void)
297 {
298 GGIMesaContext ctx;
299 char *s;
300
301 s = getenv("GGIMESA_DEBUG");
302 gl_ggi_debug = (s && atoi(s));
303
304 if (ggiMesaInit() < 0) /* register extensions*/
305 {
306 return NULL;
307 }
308
309 ctx = (GGIMesaContext)calloc(1, sizeof(struct ggi_mesa_context));
310 if (!ctx)
311 return NULL;
312
313 ctx->gl_vis = (GLvisual *)calloc(1, sizeof(GLvisual));
314 if (!ctx->gl_vis)
315 return NULL;
316
317 ctx->viewport_init = GL_FALSE;
318 ctx->gl_vis->DBflag = GL_FALSE;
319
320 ctx->gl_ctx = gl_create_context(ctx->gl_vis, NULL, (void *)ctx, GL_TRUE);
321 if (!ctx->gl_ctx)
322 return NULL;
323
324 return ctx;
325 }
326
327 void GGIMesaDestroyContext(GGIMesaContext ctx)
328 {
329 if (ctx)
330 {
331 gl_destroy_visual(ctx->gl_vis);
332 gl_destroy_context(ctx->gl_ctx);
333 gl_destroy_framebuffer(ctx->gl_buffer);
334 if (ctx == GGIMesa)
335 GGIMesa = NULL;
336 if (ctx->ggi_vis)
337 ggiExtensionDetach(ctx->ggi_vis, ggiMesaID);
338 ggiExtensionUnregister(ggiMesaID);
339 free(ctx);
340 }
341 }
342
343 int GGIMesaSetVisual(GGIMesaContext ctx, ggi_visual_t vis,
344 GLboolean rgb_flag, GLboolean db_flag)
345 {
346 struct ggi_mesa_info info;
347 int err;
348 uint16 r,g,b;
349 ggi_color pal[256];
350 int i;
351 void *func;
352 ggi_mode mode;
353 int num_buf;
354
355 if (!ctx) return -1;
356 if (!vis) return -1;
357
358 if (ctx->ggi_vis)
359 ggiExtensionDetach(ctx->ggi_vis, ggiMesaID);
360
361 ctx->ggi_vis=vis;
362
363 err = ggiExtensionAttach(vis, ggiMesaID);
364 if (err < 0)
365 return -1;
366 if (err == 0)
367 changed(vis, GGI_CHG_APILIST);
368
369 if (ctx->gl_vis)
370 gl_destroy_visual(ctx->gl_vis);
371
372 if (ctx->gl_buffer)
373 gl_destroy_framebuffer(ctx->gl_buffer);
374
375 info.rgb_flag = rgb_flag;
376 info.db_flag = db_flag;
377
378 err = gl_ggiInitInfo(ctx, &info);
379 if (err)
380 return -1;
381
382 gl_ggiSetupPointers(ctx->gl_ctx);
383
384 func = (void *)LIBGGI_MESAEXT(ctx->ggi_vis)->setup_driver;
385
386 if (!func)
387 {
388 fprintf(stderr, "setup_driver==NULL!\n");
389 fprintf(stderr, "Please check your config files!\n");
390 return -1;
391 }
392
393 err = LIBGGI_MESAEXT(ctx->ggi_vis)->setup_driver(ctx, &info);
394 if (err)
395 return -1;
396
397 ctx->gl_vis = gl_create_visual(info.rgb_flag,
398 info.alpha_flag,
399 info.db_flag,
400 GL_FALSE,
401 info.depth_bits,
402 info.stencil_bits,
403 info.accum_bits,
404 info.index_bits,
405 info.red_bits, info.green_bits,
406 info.blue_bits, info.alpha_bits);
407 if (!ctx->gl_vis)
408 {
409 fprintf(stderr, "Can't create gl_visual!\n");
410 return -1;
411 }
412
413 ctx->gl_buffer = gl_create_framebuffer(ctx->gl_vis,
414 ctx->gl_vis->DepthBits > 0,
415 ctx->gl_vis->StencilBits > 0,
416 ctx->gl_vis->AccumRedBits > 0,
417 ctx->gl_vis->AlphaBits > 0);
418
419
420 if (!ctx->gl_buffer)
421 {
422 fprintf(stderr, "Can't create gl_buffer!\n");
423 return -1;
424 }
425
426 ggiGetMode(ctx->ggi_vis, &mode);
427 ctx->width = mode.visible.x;
428 ctx->height = mode.visible.y;
429 ctx->stride = mode.virt.x;
430 ctx->origin.x = 0;
431 ctx->origin.y = 0;
432 ctx->flip_y = ctx->origin.y + ctx->height - 1;
433
434 ctx->color = 0;
435
436 ctx->lfb[0] = ctx->lfb[1] = NULL;
437 num_buf = ggiDBGetNumBuffers(ctx->ggi_vis);
438
439 for (i = 0; i < num_buf; i++)
440 {
441 if (ggiDBGetBuffer(ctx->ggi_vis,i)->layout == blPixelLinearBuffer)
442 {
443 ctx->stride = ggiDBGetBuffer(ctx->ggi_vis, i)->buffer.plb.stride /
444 (ggiDBGetBuffer(ctx->ggi_vis, i)->buffer.plb.pixelformat->size / 8);
445 ctx->lfb[0] = ggiDBGetBuffer(ctx->ggi_vis, i)->write;
446 }
447 }
448
449 if (ctx->lfb[0] == NULL)
450 {
451 fprintf(stderr, "No linear frame buffer!\n");
452 return -1;
453 }
454
455 /* FIXME: Use separate buffers */
456 ctx->lfb[1] = malloc(ctx->stride * ctx->height);
457 ctx->bufsize = (ctx->stride * ctx->height);
458
459 ctx->gl_ctx->Visual = ctx->gl_vis;
460 ctx->gl_ctx->Pixel.ReadBuffer =
461 ctx->gl_ctx->Color.DrawBuffer = (db_flag) ? GL_BACK : GL_FRONT;
462
463 if (GGIMesa == ctx)
464 gl_make_current(ctx->gl_ctx, ctx->gl_buffer);
465
466 if (rgb_flag && mode.graphtype==GT_8BIT)
467 {
468 for (i = r = 0; r < 8; r++)
469 for (g = 0; g < 8; g++)
470 for (b = 0; b < 4; b++, i++)
471 {
472 pal[i].r = r << (GGI_COLOR_PRECISION - 3);
473 pal[i].g = g << (GGI_COLOR_PRECISION - 3);
474 pal[i].b = b << (GGI_COLOR_PRECISION - 2);
475 }
476 ggiSetPalette(ctx->ggi_vis, 0, 256, pal);
477 }
478
479 return 0;
480 }
481
482 void GGIMesaMakeCurrent(GGIMesaContext ctx)
483 {
484 if (!ctx->ggi_vis)
485 return;
486
487 GGIMesa = ctx;
488 gl_make_current(ctx->gl_ctx, ctx->gl_buffer);
489
490 if (!ctx->viewport_init)
491 {
492 gl_Viewport(ctx->gl_ctx, 0, 0, ctx->width, ctx->height);
493 ctx->viewport_init = GL_TRUE;
494 }
495 }
496
497
498 GGIMesaContext GGIMesaGetCurrentContext(void)
499 {
500 return GGIMesa;
501 }
502
503 /*
504 * Swap front/back buffers for current context if double buffered.
505 */
506 void GGIMesaSwapBuffers(void)
507 {
508 GGIMESADPRINT_CORE("GGIMesaSwapBuffers\n");
509
510 FLUSH_VB(GGIMesa->gl_ctx, "swap buffers");
511 gl_ggiFlush(GGIMesa->gl_ctx);
512
513 if (GGIMesa->gl_vis->DBflag)
514 {
515 memcpy(GGIMesa->lfb[0], GGIMesa->lfb[1], GGIMesa->bufsize);
516 }
517 }
518
519 static void gl_ggiUpdateState(GLcontext *ctx)
520 {
521 void *func;
522
523 func = (void *)CTX_OPMESA(ctx)->update_state;
524
525 if (!func) {
526 fprintf(stderr, "update_state == NULL!\n");
527 fprintf(stderr, "Please check your config files!\n");
528 ggiPanic("");
529 }
530
531 CTX_OPMESA(ctx)->update_state(ctx);
532 }
533
534 static int changed(ggi_visual_t vis, int whatchanged)
535 {
536 switch (whatchanged)
537 {
538 case GGI_CHG_APILIST:
539 {
540 char api[256];
541 char args[256];
542 int i;
543 const char *fname;
544 ggi_dlhandle *lib;
545
546 for (i = 0; ggiGetAPI(vis, i, api, args) == 0; i++)
547 {
548 strcat(api, "-mesa");
549 fname = ggMatchConfig(_ggimesaConfigHandle, api, NULL);
550 if (fname == NULL)
551 {
552 /* No special implementation for this sublib */
553 continue;
554 }
555 lib = ggiExtensionLoadDL(vis, fname, args, NULL, GGI_SYMNAME_PREFIX);
556 }
557 }
558 break;
559 }
560 return 0;
561 }
562
563
564 int ggiMesaExit(void)
565 {
566 int rc;
567
568 if (!_ggimesaLibIsUp)
569 return -1;
570
571 if (_ggimesaLibIsUp > 1)
572 {
573 /* Exit only at last call */
574 _ggimesaLibIsUp--;
575 return 0;
576 }
577
578 rc = ggiExtensionUnregister(ggiMesaID);
579 ggFreeConfig(_ggimesaConfigHandle);
580
581 _ggimesaLibIsUp = 0;
582
583 return rc;
584 }
585
586 static int _ggi_error(void)
587 {
588 return -1;
589 }
590
591 int ggiMesaAttach(ggi_visual_t vis)
592 {
593 int rc;
594
595 rc = ggiExtensionAttach(vis, ggiMesaID);
596 if (rc == 0)
597 {
598 /* We are creating the primary instance */
599 memset(LIBGGI_MESAEXT(vis), 0, sizeof(mesaext));
600 LIBGGI_MESAEXT(vis)->update_state = (void *)_ggi_error;
601 LIBGGI_MESAEXT(vis)->setup_driver = (void *)_ggi_error;
602
603 /* Now fake an "API change" so the right libs get loaded */
604 changed(vis, GGI_CHG_APILIST);
605 }
606
607 return rc;
608 }
609
610 int ggiMesaDetach(ggi_visual_t vis)
611 {
612 return ggiExtensionDetach(vis, ggiMesaID);
613 }
614
615
616 #else
617 /*
618 * Need this to provide at least one external definition when GGI is
619 * not defined on the compiler command line.
620 */
621
622 int gl_ggi_dummy_function(void)
623 {
624 return 0;
625 }
626
627 #endif