remove old set_buffer routine and misc code
[mesa.git] / src / mesa / drivers / fbdev / glfbdev.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.1
4 *
5 * Copyright (C) 1999-2004 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * OpenGL (Mesa) interface for fbdev.
28 * For info about fbdev:
29 * http://www.tldp.org/HOWTO/Framebuffer-HOWTO.html
30 *
31 * known VGA modes
32 * Colours 640x400 640x480 800x600 1024x768 1152x864 1280x1024 1600x1200
33 * --------+--------------------------------------------------------------
34 * 4 bits | ? ? 0x302 ? ? ? ?
35 * 8 bits | 0x300 0x301 0x303 0x305 0x161 0x307 0x31C
36 * 15 bits | ? 0x310 0x313 0x316 0x162 0x319 0x31D
37 * 16 bits | ? 0x311 0x314 0x317 0x163 0x31A 0x31E
38 * 24 bits | ? 0x312 0x315 0x318 ? 0x31B 0x31F
39 * 32 bits | ? ? ? ? 0x164 ?
40 */
41 #ifdef USE_GLFBDEV_DRIVER
42
43 #include "glheader.h"
44 #include <linux/fb.h>
45 #include "GL/glfbdev.h"
46 #include "buffers.h"
47 #include "context.h"
48 #include "extensions.h"
49 #include "fbobject.h"
50 #include "framebuffer.h"
51 #include "imports.h"
52 #include "renderbuffer.h"
53 #include "texformat.h"
54 #include "teximage.h"
55 #include "texstore.h"
56 #include "array_cache/acache.h"
57 #include "swrast/swrast.h"
58 #include "swrast_setup/swrast_setup.h"
59 #include "tnl/tnl.h"
60 #include "tnl/t_context.h"
61 #include "tnl/t_pipeline.h"
62 #include "drivers/common/driverfuncs.h"
63
64
65 #define PF_B8G8R8 1
66 #define PF_B8G8R8A8 2
67 #define PF_B5G6R5 3
68 #define PF_B5G5R5 4
69 #define PF_CI8 5
70
71
72 /*
73 * Derived from Mesa's GLvisual class.
74 */
75 struct GLFBDevVisualRec {
76 GLvisual glvisual; /* base class */
77 struct fb_fix_screeninfo fix;
78 struct fb_var_screeninfo var;
79 int pixelFormat;
80 };
81
82 /*
83 * Derived from Mesa's GLframebuffer class.
84 */
85 struct GLFBDevBufferRec {
86 GLframebuffer glframebuffer; /* base class */
87 GLFBDevVisualPtr visual;
88 struct fb_fix_screeninfo fix;
89 struct fb_var_screeninfo var;
90 size_t size; /* color buffer size in bytes */
91 GLuint bytesPerPixel;
92 };
93
94 /*
95 * Derived from Mesa's GLcontext class.
96 */
97 struct GLFBDevContextRec {
98 GLcontext glcontext; /* base class */
99 GLFBDevVisualPtr visual;
100 GLFBDevBufferPtr drawBuffer;
101 GLFBDevBufferPtr readBuffer;
102 GLFBDevBufferPtr curBuffer;
103 };
104
105 /*
106 * Derived from Mesa's gl_renderbuffer class.
107 */
108 struct GLFBDevRenderbufferRec {
109 struct gl_renderbuffer Base;
110 GLubyte *bottom; /* pointer to last row */
111 GLuint rowStride; /* in bytes */
112 GLboolean mallocedBuffer;
113 };
114
115
116
117 #define GLFBDEV_CONTEXT(CTX) ((GLFBDevContextPtr) (CTX))
118 #define GLFBDEV_BUFFER(BUF) ((GLFBDevBufferPtr) (BUF))
119
120
121 /**********************************************************************/
122 /* Internal device driver functions */
123 /**********************************************************************/
124
125
126 static const GLubyte *
127 get_string(GLcontext *ctx, GLenum pname)
128 {
129 (void) ctx;
130 switch (pname) {
131 case GL_RENDERER:
132 return (const GLubyte *) "Mesa glfbdev";
133 default:
134 return NULL;
135 }
136 }
137
138
139 static void
140 update_state( GLcontext *ctx, GLuint new_state )
141 {
142 /* not much to do here - pass it on */
143 _swrast_InvalidateState( ctx, new_state );
144 _swsetup_InvalidateState( ctx, new_state );
145 _ac_InvalidateState( ctx, new_state );
146 _tnl_InvalidateState( ctx, new_state );
147 }
148
149
150 static void
151 get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
152 {
153 const GLFBDevBufferPtr fbdevbuffer = GLFBDEV_BUFFER(buffer);
154 *width = fbdevbuffer->var.xres_virtual;
155 *height = fbdevbuffer->var.yres_virtual;
156 }
157
158
159 static void
160 viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
161 {
162 /* poll for window size change and realloc software Z/stencil/etc if needed */
163 _mesa_ResizeBuffersMESA();
164 }
165
166
167 /*
168 * Generate code for span functions.
169 */
170
171 /* 24-bit BGR */
172 #define NAME(PREFIX) PREFIX##_B8G8R8
173 #define FORMAT GL_RGBA8
174 #define SPAN_VARS \
175 struct GLFBDevRenderbufferRec *frb = (struct GLFBDevRenderbufferRec *) rb;
176 #define INIT_PIXEL_PTR(P, X, Y) \
177 GLubyte *P = frb->bottom - (Y) * frb->rowStride + (X) * 3
178 #define INC_PIXEL_PTR(P) P += 3
179 #define STORE_PIXEL(DST, X, Y, VALUE) \
180 DST[0] = VALUE[BCOMP]; \
181 DST[1] = VALUE[GCOMP]; \
182 DST[2] = VALUE[RCOMP]
183 #define FETCH_PIXEL(DST, SRC) \
184 DST[RCOMP] = SRC[2]; \
185 DST[GCOMP] = SRC[1]; \
186 DST[BCOMP] = SRC[0]; \
187 DST[ACOMP] = CHAN_MAX
188
189 #include "swrast/s_spantemp.h"
190
191
192 /* 32-bit BGRA */
193 #define NAME(PREFIX) PREFIX##_B8G8R8A8
194 #define FORMAT GL_RGBA8
195 #define SPAN_VARS \
196 struct GLFBDevRenderbufferRec *frb = (struct GLFBDevRenderbufferRec *) rb;
197 #define INIT_PIXEL_PTR(P, X, Y) \
198 GLubyte *P = frb->bottom - (Y) * frb->rowStride + (X) * 4
199 #define INC_PIXEL_PTR(P) P += 4
200 #define STORE_PIXEL(DST, X, Y, VALUE) \
201 DST[0] = VALUE[BCOMP]; \
202 DST[1] = VALUE[GCOMP]; \
203 DST[2] = VALUE[RCOMP]; \
204 DST[3] = VALUE[ACOMP]
205 #define FETCH_PIXEL(DST, SRC) \
206 DST[RCOMP] = SRC[2]; \
207 DST[GCOMP] = SRC[1]; \
208 DST[BCOMP] = SRC[0]; \
209 DST[ACOMP] = SRC[3]
210
211 #include "swrast/s_spantemp.h"
212
213
214 /* 16-bit BGR (XXX implement dithering someday) */
215 #define NAME(PREFIX) PREFIX##_B5G6R5
216 #define FORMAT GL_RGBA8
217 #define SPAN_VARS \
218 struct GLFBDevRenderbufferRec *frb = (struct GLFBDevRenderbufferRec *) rb;
219 #define INIT_PIXEL_PTR(P, X, Y) \
220 GLushort *P = (GLushort *) (frb->bottom - (Y) * frb->rowStride + (X) * 2)
221 #define INC_PIXEL_PTR(P) P += 1
222 #define STORE_PIXEL(DST, X, Y, VALUE) \
223 DST[0] = ( (((VALUE[RCOMP]) & 0xf8) << 8) | (((VALUE[GCOMP]) & 0xfc) << 3) | ((VALUE[BCOMP]) >> 3) )
224 #define FETCH_PIXEL(DST, SRC) \
225 DST[RCOMP] = ( (((SRC[0]) >> 8) & 0xf8) | (((SRC[0]) >> 11) & 0x7) ); \
226 DST[GCOMP] = ( (((SRC[0]) >> 3) & 0xfc) | (((SRC[0]) >> 5) & 0x3) ); \
227 DST[BCOMP] = ( (((SRC[0]) << 3) & 0xf8) | (((SRC[0]) ) & 0x7) ); \
228 DST[ACOMP] = CHAN_MAX
229
230 #include "swrast/s_spantemp.h"
231
232
233 /* 15-bit BGR (XXX implement dithering someday) */
234 #define NAME(PREFIX) PREFIX##_B5G5R5
235 #define FORMAT GL_RGBA8
236 #define SPAN_VARS \
237 struct GLFBDevRenderbufferRec *frb = (struct GLFBDevRenderbufferRec *) rb;
238 #define INIT_PIXEL_PTR(P, X, Y) \
239 GLushort *P = (GLushort *) (frb->bottom - (Y) * frb->rowStride + (X) * 2)
240 #define INC_PIXEL_PTR(P) P += 1
241 #define STORE_PIXEL(DST, X, Y, VALUE) \
242 DST[0] = ( (((VALUE[RCOMP]) & 0xf8) << 7) | (((VALUE[GCOMP]) & 0xf8) << 2) | ((VALUE[BCOMP]) >> 3) )
243 #define FETCH_PIXEL(DST, SRC) \
244 DST[RCOMP] = ( (((SRC[0]) >> 7) & 0xf8) | (((SRC[0]) >> 10) & 0x7) ); \
245 DST[GCOMP] = ( (((SRC[0]) >> 2) & 0xf8) | (((SRC[0]) >> 5) & 0x7) ); \
246 DST[BCOMP] = ( (((SRC[0]) << 3) & 0xf8) | (((SRC[0]) ) & 0x7) ); \
247 DST[ACOMP] = CHAN_MAX
248
249 #include "swrast/s_spantemp.h"
250
251
252 /* 8-bit color index */
253 #define NAME(PREFIX) PREFIX##_CI8
254 #define FORMAT GL_COLOR_INDEX8_EXT
255 #define SPAN_VARS \
256 struct GLFBDevRenderbufferRec *frb = (struct GLFBDevRenderbufferRec *) rb;
257 #define INIT_PIXEL_PTR(P, X, Y) \
258 GLubyte *P = frb->bottom - (Y) * frb->rowStride + (X)
259 #define INC_PIXEL_PTR(P) P += 1
260 #define STORE_PIXEL(DST, X, Y, VALUE) \
261 *DST = VALUE[0]
262 #define FETCH_PIXEL(DST, SRC) \
263 DST = SRC[0]
264
265 #include "swrast/s_spantemp.h"
266
267
268
269
270 /**********************************************************************/
271 /* Public API functions */
272 /**********************************************************************/
273
274
275 const char *
276 glFBDevGetString( int str )
277 {
278 switch (str) {
279 case GLFBDEV_VENDOR:
280 return "Mesa Project";
281 case GLFBDEV_VERSION:
282 return "1.0.0";
283 default:
284 return NULL;
285 }
286 }
287
288
289 const GLFBDevProc
290 glFBDevGetProcAddress( const char *procName )
291 {
292 struct name_address {
293 const char *name;
294 const GLFBDevProc func;
295 };
296 static const struct name_address functions[] = {
297 { "glFBDevGetString", (GLFBDevProc) glFBDevGetString },
298 { "glFBDevGetProcAddress", (GLFBDevProc) glFBDevGetProcAddress },
299 { "glFBDevCreateVisual", (GLFBDevProc) glFBDevCreateVisual },
300 { "glFBDevDestroyVisual", (GLFBDevProc) glFBDevDestroyVisual },
301 { "glFBDevGetVisualAttrib", (GLFBDevProc) glFBDevGetVisualAttrib },
302 { "glFBDevCreateBuffer", (GLFBDevProc) glFBDevCreateBuffer },
303 { "glFBDevDestroyBuffer", (GLFBDevProc) glFBDevDestroyBuffer },
304 { "glFBDevGetBufferAttrib", (GLFBDevProc) glFBDevGetBufferAttrib },
305 { "glFBDevGetCurrentDrawBuffer", (GLFBDevProc) glFBDevGetCurrentDrawBuffer },
306 { "glFBDevGetCurrentReadBuffer", (GLFBDevProc) glFBDevGetCurrentReadBuffer },
307 { "glFBDevSwapBuffers", (GLFBDevProc) glFBDevSwapBuffers },
308 { "glFBDevCreateContext", (GLFBDevProc) glFBDevCreateContext },
309 { "glFBDevDestroyContext", (GLFBDevProc) glFBDevDestroyContext },
310 { "glFBDevGetContextAttrib", (GLFBDevProc) glFBDevGetContextAttrib },
311 { "glFBDevGetCurrentContext", (GLFBDevProc) glFBDevGetCurrentContext },
312 { "glFBDevMakeCurrent", (GLFBDevProc) glFBDevMakeCurrent },
313 { NULL, NULL }
314 };
315 const struct name_address *entry;
316 for (entry = functions; entry->name; entry++) {
317 if (_mesa_strcmp(entry->name, procName) == 0) {
318 return entry->func;
319 }
320 }
321 return _glapi_get_proc_address(procName);
322 }
323
324
325 GLFBDevVisualPtr
326 glFBDevCreateVisual( const struct fb_fix_screeninfo *fixInfo,
327 const struct fb_var_screeninfo *varInfo,
328 const int *attribs )
329 {
330 GLFBDevVisualPtr vis;
331 const int *attrib;
332 GLboolean rgbFlag = GL_TRUE, dbFlag = GL_FALSE, stereoFlag = GL_FALSE;
333 GLint redBits = 0, greenBits = 0, blueBits = 0, alphaBits = 0;
334 GLint indexBits = 0, depthBits = 0, stencilBits = 0;
335 GLint accumRedBits = 0, accumGreenBits = 0;
336 GLint accumBlueBits = 0, accumAlphaBits = 0;
337 GLint numSamples = 0;
338
339 ASSERT(fixInfo);
340 ASSERT(varInfo);
341
342 vis = CALLOC_STRUCT(GLFBDevVisualRec);
343 if (!vis)
344 return NULL;
345
346 vis->fix = *fixInfo; /* struct assignment */
347 vis->var = *varInfo; /* struct assignment */
348
349 for (attrib = attribs; attrib && *attrib != GLFBDEV_NONE; attrib++) {
350 switch (*attrib) {
351 case GLFBDEV_DOUBLE_BUFFER:
352 dbFlag = GL_TRUE;
353 break;
354 case GLFBDEV_COLOR_INDEX:
355 rgbFlag = GL_FALSE;
356 break;
357 case GLFBDEV_DEPTH_SIZE:
358 depthBits = attrib[1];
359 attrib++;
360 break;
361 case GLFBDEV_STENCIL_SIZE:
362 stencilBits = attrib[1];
363 attrib++;
364 break;
365 case GLFBDEV_ACCUM_SIZE:
366 accumRedBits = accumGreenBits = accumBlueBits = accumAlphaBits
367 = attrib[1];
368 attrib++;
369 break;
370 case GLFBDEV_LEVEL:
371 /* ignored for now */
372 break;
373 default:
374 /* unexpected token */
375 _mesa_free(vis);
376 return NULL;
377 }
378 }
379
380 if (rgbFlag) {
381 redBits = varInfo->red.length;
382 greenBits = varInfo->green.length;
383 blueBits = varInfo->blue.length;
384 alphaBits = varInfo->transp.length;
385
386 if ((fixInfo->visual == FB_VISUAL_TRUECOLOR ||
387 fixInfo->visual == FB_VISUAL_DIRECTCOLOR)
388 && varInfo->bits_per_pixel == 24
389 && varInfo->red.offset == 16
390 && varInfo->green.offset == 8
391 && varInfo->blue.offset == 0) {
392 vis->pixelFormat = PF_B8G8R8;
393 }
394 else if ((fixInfo->visual == FB_VISUAL_TRUECOLOR ||
395 fixInfo->visual == FB_VISUAL_DIRECTCOLOR)
396 && varInfo->bits_per_pixel == 32
397 && varInfo->red.offset == 16
398 && varInfo->green.offset == 8
399 && varInfo->blue.offset == 0
400 && varInfo->transp.offset == 24) {
401 vis->pixelFormat = PF_B8G8R8A8;
402 }
403 else if ((fixInfo->visual == FB_VISUAL_TRUECOLOR ||
404 fixInfo->visual == FB_VISUAL_DIRECTCOLOR)
405 && varInfo->bits_per_pixel == 16
406 && varInfo->red.offset == 11
407 && varInfo->green.offset == 5
408 && varInfo->blue.offset == 0) {
409 vis->pixelFormat = PF_B5G6R5;
410 }
411 else if ((fixInfo->visual == FB_VISUAL_TRUECOLOR ||
412 fixInfo->visual == FB_VISUAL_DIRECTCOLOR)
413 && varInfo->bits_per_pixel == 16
414 && varInfo->red.offset == 10
415 && varInfo->green.offset == 5
416 && varInfo->blue.offset == 0) {
417 vis->pixelFormat = PF_B5G5R5;
418 }
419 else {
420 _mesa_problem(NULL, "Unsupported fbdev RGB visual/bitdepth!\n");
421 /*
422 printf("fixInfo->visual = 0x%x\n", fixInfo->visual);
423 printf("varInfo->bits_per_pixel = %d\n", varInfo->bits_per_pixel);
424 printf("varInfo->red.offset = %d\n", varInfo->red.offset);
425 printf("varInfo->green.offset = %d\n", varInfo->green.offset);
426 printf("varInfo->blue.offset = %d\n", varInfo->blue.offset);
427 */
428 _mesa_free(vis);
429 return NULL;
430 }
431 }
432 else {
433 indexBits = varInfo->bits_per_pixel;
434 if ((fixInfo->visual == FB_VISUAL_PSEUDOCOLOR ||
435 fixInfo->visual == FB_VISUAL_STATIC_PSEUDOCOLOR)
436 && varInfo->bits_per_pixel == 8) {
437 vis->pixelFormat = PF_CI8;
438 }
439 else {
440 _mesa_problem(NULL, "Unsupported fbdev CI visual/bitdepth!\n");
441 _mesa_free(vis);
442 return NULL;
443 }
444 }
445
446 if (!_mesa_initialize_visual(&vis->glvisual, rgbFlag, dbFlag, stereoFlag,
447 redBits, greenBits, blueBits, alphaBits,
448 indexBits, depthBits, stencilBits,
449 accumRedBits, accumGreenBits,
450 accumBlueBits, accumAlphaBits,
451 numSamples)) {
452 /* something was invalid */
453 _mesa_free(vis);
454 return NULL;
455 }
456
457 return vis;
458 }
459
460
461 void
462 glFBDevDestroyVisual( GLFBDevVisualPtr visual )
463 {
464 if (visual)
465 _mesa_free(visual);
466 }
467
468
469 int
470 glFBDevGetVisualAttrib( const GLFBDevVisualPtr visual, int attrib)
471 {
472 /* XXX unfinished */
473 (void) visual;
474 (void) attrib;
475 return -1;
476 }
477
478
479 static void
480 delete_renderbuffer(struct gl_renderbuffer *rb)
481 {
482 struct GLFBDevRenderbufferRec *frb = (struct GLFBDevRenderbufferRec *) rb;
483 if (frb->mallocedBuffer) {
484 _mesa_free(frb->Base.Data);
485 }
486 _mesa_free(frb);
487 }
488
489
490 static GLboolean
491 renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
492 GLenum internalFormat, GLuint width, GLuint height)
493 {
494 /* no-op: the renderbuffer storage is allocated just once when it's
495 * created. Never resized or reallocated.
496 */
497 return GL_TRUE;
498 }
499
500
501 static struct GLFBDevRenderbufferRec *
502 new_glfbdev_renderbuffer(void *bufferStart, int pixelFormat)
503 {
504 struct GLFBDevRenderbufferRec *rb = CALLOC_STRUCT(GLFBDevRenderbufferRec);
505 if (rb) {
506 GLuint name = 0;
507 _mesa_init_renderbuffer(&rb->Base, name);
508
509 rb->Base.Delete = delete_renderbuffer;
510 rb->Base.AllocStorage = renderbuffer_storage;
511
512 if (pixelFormat == PF_B8G8R8) {
513 rb->Base.GetRow = get_row_B8G8R8;
514 rb->Base.GetValues = get_values_B8G8R8;
515 rb->Base.PutRow = put_row_B8G8R8;
516 rb->Base.PutMonoRow = put_mono_row_B8G8R8;
517 rb->Base.PutValues = put_values_B8G8R8;
518 rb->Base.PutMonoValues = put_mono_values_B8G8R8;
519 }
520 else if (pixelFormat == PF_B8G8R8A8) {
521 rb->Base.GetRow = get_row_B8G8R8A8;
522 rb->Base.GetValues = get_values_B8G8R8A8;
523 rb->Base.PutRow = put_row_B8G8R8A8;
524 rb->Base.PutMonoRow = put_mono_row_B8G8R8A8;
525 rb->Base.PutValues = put_values_B8G8R8A8;
526 rb->Base.PutMonoValues = put_mono_values_B8G8R8A8;
527 }
528 else if (pixelFormat == PF_B5G6R5) {
529 rb->Base.GetRow = get_row_B5G6R5;
530 rb->Base.GetValues = get_values_B5G6R5;
531 rb->Base.PutRow = put_row_B5G6R5;
532 rb->Base.PutMonoRow = put_mono_row_B5G6R5;
533 rb->Base.PutValues = put_values_B5G6R5;
534 rb->Base.PutMonoValues = put_mono_values_B5G6R5;
535 }
536 else if (pixelFormat == PF_B5G5R5) {
537 rb->Base.GetRow = get_row_B5G5R5;
538 rb->Base.GetValues = get_values_B5G5R5;
539 rb->Base.PutRow = put_row_B5G5R5;
540 rb->Base.PutMonoRow = put_mono_row_B5G5R5;
541 rb->Base.PutValues = put_values_B5G5R5;
542 rb->Base.PutMonoValues = put_mono_values_B5G5R5;
543 }
544 else if (pixelFormat == PF_CI8) {
545 rb->Base.GetRow = get_row_CI8;
546 rb->Base.GetValues = get_values_CI8;
547 rb->Base.PutRow = put_row_CI8;
548 rb->Base.PutMonoRow = put_mono_row_CI8;
549 rb->Base.PutValues = put_values_CI8;
550 rb->Base.PutMonoValues = put_mono_values_CI8;
551 }
552
553 if (pixelFormat == PF_CI8) {
554 rb->Base.InternalFormat = GL_COLOR_INDEX8_EXT;
555 rb->Base._BaseFormat = GL_COLOR_INDEX;
556 }
557 else {
558 rb->Base.InternalFormat = GL_RGBA;
559 rb->Base._BaseFormat = GL_RGBA;
560 }
561 rb->Base.DataType = GL_UNSIGNED_BYTE;
562 rb->Base.Data = bufferStart;
563 }
564 return rb;
565 }
566
567
568 GLFBDevBufferPtr
569 glFBDevCreateBuffer( const struct fb_fix_screeninfo *fixInfo,
570 const struct fb_var_screeninfo *varInfo,
571 const GLFBDevVisualPtr visual,
572 void *frontBuffer, void *backBuffer, size_t size )
573 {
574 struct GLFBDevRenderbufferRec *frontrb, *backrb;
575 GLFBDevBufferPtr buf;
576
577 ASSERT(visual);
578 ASSERT(frontBuffer);
579 ASSERT(size > 0);
580
581 if (visual->fix.visual != fixInfo->visual ||
582 visual->fix.type != fixInfo->type ||
583 visual->var.bits_per_pixel != varInfo->bits_per_pixel ||
584 visual->var.grayscale != varInfo->grayscale ||
585 visual->var.red.offset != varInfo->red.offset ||
586 visual->var.green.offset != varInfo->green.offset ||
587 visual->var.blue.offset != varInfo->blue.offset ||
588 visual->var.transp.offset != varInfo->transp.offset) {
589 /* visual mismatch! */
590 return NULL;
591 }
592
593 buf = CALLOC_STRUCT(GLFBDevBufferRec);
594 if (!buf)
595 return NULL;
596
597 /* basic framebuffer setup */
598 _mesa_initialize_framebuffer(&buf->glframebuffer, &visual->glvisual);
599 /* add front renderbuffer */
600 frontrb = new_glfbdev_renderbuffer(frontBuffer, visual->pixelFormat);
601 _mesa_add_renderbuffer(&buf->glframebuffer, BUFFER_FRONT_LEFT,
602 &frontrb->Base);
603 /* add back renderbuffer */
604 if (visual->glvisual.doubleBufferMode) {
605 backrb = new_glfbdev_renderbuffer(backBuffer, visual->pixelFormat);
606 _mesa_add_renderbuffer(&buf->glframebuffer, BUFFER_BACK_LEFT,
607 &backrb->Base);
608 }
609 /* add software renderbuffers */
610 _mesa_add_soft_renderbuffers(&buf->glframebuffer,
611 GL_FALSE, /* color */
612 visual->glvisual.haveDepthBuffer,
613 visual->glvisual.haveStencilBuffer,
614 visual->glvisual.haveAccumBuffer,
615 GL_FALSE, /* alpha */
616 GL_FALSE /* aux bufs */);
617
618
619
620 buf->fix = *fixInfo; /* struct assignment */
621 buf->var = *varInfo; /* struct assignment */
622 buf->visual = visual; /* ptr assignment */
623 buf->size = size;
624 buf->bytesPerPixel = visual->var.bits_per_pixel / 8;
625 frontrb->rowStride = visual->var.xres_virtual * buf->bytesPerPixel;
626 frontrb->bottom = (GLubyte *) frontrb->Base.Data
627 + (visual->var.yres_virtual - 1) * frontrb->rowStride;
628
629 if (visual->glvisual.doubleBufferMode) {
630 if (!backBuffer) {
631 /* malloc a back buffer */
632 backrb->Base.Data = _mesa_malloc(size);
633 if (!backrb->Base.Data) {
634 _mesa_free_framebuffer_data(&buf->glframebuffer);
635 _mesa_free(buf);
636 return NULL;
637 }
638 backrb->mallocedBuffer = GL_TRUE;
639 }
640 backrb->rowStride = frontrb->rowStride;
641 backrb->bottom = (GLubyte *) backrb->Base.Data
642 + (visual->var.yres_virtual - 1) * backrb->rowStride;
643 }
644 else {
645 backrb->bottom = NULL;
646 backrb->rowStride = 0;
647 }
648
649 return buf;
650 }
651
652
653 void
654 glFBDevDestroyBuffer( GLFBDevBufferPtr buffer )
655 {
656 if (buffer) {
657 /* check if destroying the current buffer */
658 GLFBDevBufferPtr curDraw = glFBDevGetCurrentDrawBuffer();
659 GLFBDevBufferPtr curRead = glFBDevGetCurrentReadBuffer();
660 if (buffer == curDraw || buffer == curRead) {
661 glFBDevMakeCurrent( NULL, NULL, NULL);
662 }
663 /* free the software depth, stencil, accum buffers */
664 _mesa_free_framebuffer_data(&buffer->glframebuffer);
665 _mesa_free(buffer);
666 }
667 }
668
669
670 int
671 glFBDevGetBufferAttrib( const GLFBDevBufferPtr buffer, int attrib)
672 {
673 (void) buffer;
674 (void) attrib;
675 return -1;
676 }
677
678
679 GLFBDevBufferPtr
680 glFBDevGetCurrentDrawBuffer( void )
681 {
682 GLFBDevContextPtr fbdevctx = glFBDevGetCurrentContext();
683 if (fbdevctx)
684 return fbdevctx->drawBuffer;
685 else
686 return NULL;
687 }
688
689
690 GLFBDevBufferPtr
691 glFBDevGetCurrentReadBuffer( void )
692 {
693 GLFBDevContextPtr fbdevctx = glFBDevGetCurrentContext();
694 if (fbdevctx)
695 return fbdevctx->readBuffer;
696 else
697 return NULL;
698 }
699
700
701 void
702 glFBDevSwapBuffers( GLFBDevBufferPtr buffer )
703 {
704 GLFBDevContextPtr fbdevctx = glFBDevGetCurrentContext();
705 struct GLFBDevRenderbufferRec *frontrb = (struct GLFBDevRenderbufferRec *)
706 buffer->glframebuffer.Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
707 struct GLFBDevRenderbufferRec *backrb = (struct GLFBDevRenderbufferRec *)
708 buffer->glframebuffer.Attachment[BUFFER_BACK_LEFT].Renderbuffer;
709
710 if (!buffer || !buffer->visual->glvisual.doubleBufferMode)
711 return;
712
713 /* check if swapping currently bound buffer */
714 if (fbdevctx->drawBuffer == buffer) {
715 /* flush pending rendering */
716 _mesa_notifySwapBuffers(&fbdevctx->glcontext);
717 }
718
719 ASSERT(frontrb->Base.Data);
720 ASSERT(backrb->Base.Data);
721 _mesa_memcpy(frontrb->Base.Data, backrb->Base.Data, buffer->size);
722 }
723
724
725 GLFBDevContextPtr
726 glFBDevCreateContext( const GLFBDevVisualPtr visual, GLFBDevContextPtr share )
727 {
728 GLFBDevContextPtr ctx;
729 GLcontext *glctx;
730 struct dd_function_table functions;
731
732 ASSERT(visual);
733
734 ctx = CALLOC_STRUCT(GLFBDevContextRec);
735 if (!ctx)
736 return NULL;
737
738 /* build table of device driver functions */
739 _mesa_init_driver_functions(&functions);
740 functions.GetString = get_string;
741 functions.UpdateState = update_state;
742 functions.GetBufferSize = get_buffer_size;
743 functions.Viewport = viewport;
744
745 if (!_mesa_initialize_context(&ctx->glcontext, &visual->glvisual,
746 share ? &share->glcontext : NULL,
747 &functions, (void *) ctx)) {
748 _mesa_free(ctx);
749 return NULL;
750 }
751
752 ctx->visual = visual;
753
754 /* Create module contexts */
755 glctx = (GLcontext *) &ctx->glcontext;
756 _swrast_CreateContext( glctx );
757 _ac_CreateContext( glctx );
758 _tnl_CreateContext( glctx );
759 _swsetup_CreateContext( glctx );
760 _swsetup_Wakeup( glctx );
761
762 /* use default TCL pipeline */
763 {
764 TNLcontext *tnl = TNL_CONTEXT(glctx);
765 tnl->Driver.RunPipeline = _tnl_run_pipeline;
766 }
767
768 _mesa_enable_sw_extensions(glctx);
769
770 return ctx;
771 }
772
773
774 void
775 glFBDevDestroyContext( GLFBDevContextPtr context )
776 {
777 GLFBDevContextPtr fbdevctx = glFBDevGetCurrentContext();
778
779 if (context) {
780 if (fbdevctx == context) {
781 /* destroying current context */
782 _mesa_make_current(NULL, NULL, NULL);
783 _mesa_notifyDestroy(&context->glcontext);
784 }
785 _mesa_free_context_data(&context->glcontext);
786 _mesa_free(context);
787 }
788 }
789
790
791 int
792 glFBDevGetContextAttrib( const GLFBDevContextPtr context, int attrib)
793 {
794 (void) context;
795 (void) attrib;
796 return -1;
797 }
798
799
800 GLFBDevContextPtr
801 glFBDevGetCurrentContext( void )
802 {
803 GET_CURRENT_CONTEXT(ctx);
804 return (GLFBDevContextPtr) ctx;
805 }
806
807
808 int
809 glFBDevMakeCurrent( GLFBDevContextPtr context,
810 GLFBDevBufferPtr drawBuffer,
811 GLFBDevBufferPtr readBuffer )
812 {
813 if (context && drawBuffer && readBuffer) {
814 /* Make sure the context's visual and the buffers' visuals match.
815 * XXX we might do this by comparing specific fields like bits_per_pixel,
816 * visual, etc. in the future.
817 */
818 if (context->visual != drawBuffer->visual ||
819 context->visual != readBuffer->visual) {
820 return 0;
821 }
822 _mesa_make_current( &context->glcontext,
823 &drawBuffer->glframebuffer,
824 &readBuffer->glframebuffer );
825 context->drawBuffer = drawBuffer;
826 context->readBuffer = readBuffer;
827 context->curBuffer = drawBuffer;
828 }
829 else {
830 /* unbind */
831 _mesa_make_current( NULL, NULL, NULL );
832 }
833
834 return 1;
835 }
836
837 #endif