Merge commit 'origin/7.8'
[mesa.git] / src / mesa / drivers / dri / swrast / swrast.c
1 /*
2 * Copyright 2008, 2010 George Sapountzis <gsapountzis@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /*
23 * DRI software rasterizer
24 *
25 * This is the mesa swrast module packaged into a DRI driver structure.
26 *
27 * The front-buffer is allocated by the loader. The loader provides read/write
28 * callbacks for access to the front-buffer. The driver uses a scratch row for
29 * front-buffer rendering to avoid repeated calls to the loader.
30 *
31 * The back-buffer is allocated by the driver and is private.
32 */
33
34 #include "main/context.h"
35 #include "main/extensions.h"
36 #include "main/formats.h"
37 #include "main/framebuffer.h"
38 #include "main/imports.h"
39 #include "main/renderbuffer.h"
40 #include "swrast/swrast.h"
41 #include "swrast_setup/swrast_setup.h"
42 #include "tnl/tnl.h"
43 #include "tnl/t_context.h"
44 #include "tnl/t_pipeline.h"
45 #include "vbo/vbo.h"
46 #include "drivers/common/driverfuncs.h"
47 #include "drivers/common/meta.h"
48 #include "utils.h"
49
50 #include "swrast_priv.h"
51
52
53 /**
54 * Screen and config-related functions
55 */
56
57 static const __DRIextension *dri_screen_extensions[] = {
58 NULL
59 };
60
61 static __DRIconfig **
62 swrastFillInModes(__DRIscreen *psp,
63 unsigned pixel_bits, unsigned depth_bits,
64 unsigned stencil_bits, GLboolean have_back_buffer)
65 {
66 __DRIconfig **configs;
67 unsigned depth_buffer_factor;
68 unsigned back_buffer_factor;
69 GLenum fb_format;
70 GLenum fb_type;
71
72 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
73 * support pageflipping at all.
74 */
75 static const GLenum back_buffer_modes[] = {
76 GLX_NONE, GLX_SWAP_UNDEFINED_OML
77 };
78
79 uint8_t depth_bits_array[4];
80 uint8_t stencil_bits_array[4];
81 uint8_t msaa_samples_array[1];
82
83 depth_bits_array[0] = 0;
84 depth_bits_array[1] = 0;
85 depth_bits_array[2] = depth_bits;
86 depth_bits_array[3] = depth_bits;
87
88 /* Just like with the accumulation buffer, always provide some modes
89 * with a stencil buffer.
90 */
91 stencil_bits_array[0] = 0;
92 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
93 stencil_bits_array[2] = 0;
94 stencil_bits_array[3] = (stencil_bits == 0) ? 8 : stencil_bits;
95
96 msaa_samples_array[0] = 0;
97
98 depth_buffer_factor = 4;
99 back_buffer_factor = 2;
100
101 switch (pixel_bits) {
102 case 8:
103 fb_format = GL_RGB;
104 fb_type = GL_UNSIGNED_BYTE_2_3_3_REV;
105 break;
106 case 16:
107 fb_format = GL_RGB;
108 fb_type = GL_UNSIGNED_SHORT_5_6_5;
109 break;
110 case 24:
111 fb_format = GL_BGR;
112 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
113 break;
114 case 32:
115 fb_format = GL_BGRA;
116 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
117 break;
118 default:
119 fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
120 pixel_bits);
121 return NULL;
122 }
123
124 configs = driCreateConfigs(fb_format, fb_type,
125 depth_bits_array, stencil_bits_array,
126 depth_buffer_factor, back_buffer_modes,
127 back_buffer_factor, msaa_samples_array, 1,
128 GL_TRUE);
129 if (configs == NULL) {
130 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
131 __LINE__);
132 return NULL;
133 }
134
135 return configs;
136 }
137
138 static const __DRIconfig **
139 dri_init_screen(__DRIscreen * psp)
140 {
141 __DRIconfig **configs8, **configs16, **configs24, **configs32;
142
143 TRACE;
144
145 psp->extensions = dri_screen_extensions;
146
147 configs8 = swrastFillInModes(psp, 8, 8, 0, 1);
148 configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
149 configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
150 configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
151
152 configs16 = driConcatConfigs(configs8, configs16);
153 configs24 = driConcatConfigs(configs16, configs24);
154 configs32 = driConcatConfigs(configs24, configs32);
155
156 return (const __DRIconfig **)configs32;
157 }
158
159 static void
160 dri_destroy_screen(__DRIscreen * sPriv)
161 {
162 TRACE;
163 }
164
165
166 /**
167 * Framebuffer and renderbuffer-related functions.
168 */
169
170 static GLuint
171 choose_pixel_format(const GLvisual *v)
172 {
173 int depth = v->rgbBits;
174
175 if (depth == 32
176 && v->redMask == 0xff0000
177 && v->greenMask == 0x00ff00
178 && v->blueMask == 0x0000ff)
179 return PF_A8R8G8B8;
180 else if (depth == 24
181 && v->redMask == 0xff0000
182 && v->greenMask == 0x00ff00
183 && v->blueMask == 0x0000ff)
184 return PF_X8R8G8B8;
185 else if (depth == 16
186 && v->redMask == 0xf800
187 && v->greenMask == 0x07e0
188 && v->blueMask == 0x001f)
189 return PF_R5G6B5;
190 else if (depth == 8
191 && v->redMask == 0x07
192 && v->greenMask == 0x38
193 && v->blueMask == 0xc0)
194 return PF_R3G3B2;
195
196 _mesa_problem( NULL, "unexpected format in %s", __FUNCTION__ );
197 return 0;
198 }
199
200 static void
201 swrast_delete_renderbuffer(struct gl_renderbuffer *rb)
202 {
203 TRACE;
204
205 free(rb->Data);
206 free(rb);
207 }
208
209 /* see bytes_per_line in libGL */
210 static INLINE int
211 bytes_per_line(unsigned pitch_bits, unsigned mul)
212 {
213 unsigned mask = mul - 1;
214
215 return ((pitch_bits + mask) & ~mask) / 8;
216 }
217
218 static GLboolean
219 swrast_alloc_front_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
220 GLenum internalFormat, GLuint width, GLuint height)
221 {
222 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
223
224 TRACE;
225
226 rb->Data = NULL;
227 rb->Width = width;
228 rb->Height = height;
229
230 xrb->pitch = bytes_per_line(width * xrb->bpp, 32);
231
232 return GL_TRUE;
233 }
234
235 static GLboolean
236 swrast_alloc_back_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
237 GLenum internalFormat, GLuint width, GLuint height)
238 {
239 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
240
241 TRACE;
242
243 free(rb->Data);
244
245 swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
246
247 rb->Data = malloc(height * xrb->pitch);
248
249 return GL_TRUE;
250 }
251
252 static struct swrast_renderbuffer *
253 swrast_new_renderbuffer(const GLvisual *visual, GLboolean front)
254 {
255 struct swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
256 GLuint pixel_format;
257
258 TRACE;
259
260 if (!xrb)
261 return NULL;
262
263 _mesa_init_renderbuffer(&xrb->Base, 0);
264
265 pixel_format = choose_pixel_format(visual);
266
267 xrb->Base.Delete = swrast_delete_renderbuffer;
268 if (front) {
269 xrb->Base.AllocStorage = swrast_alloc_front_storage;
270 swrast_set_span_funcs_front(xrb, pixel_format);
271 }
272 else {
273 xrb->Base.AllocStorage = swrast_alloc_back_storage;
274 swrast_set_span_funcs_back(xrb, pixel_format);
275 }
276
277 switch (pixel_format) {
278 case PF_A8R8G8B8:
279 xrb->Base.Format = MESA_FORMAT_ARGB8888;
280 xrb->Base.InternalFormat = GL_RGBA;
281 xrb->Base._BaseFormat = GL_RGBA;
282 xrb->Base.DataType = GL_UNSIGNED_BYTE;
283 xrb->bpp = 32;
284 break;
285 case PF_X8R8G8B8:
286 xrb->Base.Format = MESA_FORMAT_ARGB8888; /* XXX */
287 xrb->Base.InternalFormat = GL_RGB;
288 xrb->Base._BaseFormat = GL_RGB;
289 xrb->Base.DataType = GL_UNSIGNED_BYTE;
290 xrb->bpp = 32;
291 break;
292 case PF_R5G6B5:
293 xrb->Base.Format = MESA_FORMAT_RGB565;
294 xrb->Base.InternalFormat = GL_RGB;
295 xrb->Base._BaseFormat = GL_RGB;
296 xrb->Base.DataType = GL_UNSIGNED_BYTE;
297 xrb->bpp = 16;
298 break;
299 case PF_R3G3B2:
300 xrb->Base.Format = MESA_FORMAT_RGB332;
301 xrb->Base.InternalFormat = GL_RGB;
302 xrb->Base._BaseFormat = GL_RGB;
303 xrb->Base.DataType = GL_UNSIGNED_BYTE;
304 xrb->bpp = 8;
305 break;
306 default:
307 return NULL;
308 }
309
310 return xrb;
311 }
312
313 static GLboolean
314 dri_create_buffer(__DRIscreen * sPriv,
315 __DRIdrawable * dPriv,
316 const __GLcontextModes * visual, GLboolean isPixmap)
317 {
318 struct dri_drawable *drawable = NULL;
319 GLframebuffer *fb;
320 struct swrast_renderbuffer *frontrb, *backrb;
321
322 TRACE;
323
324 drawable = CALLOC_STRUCT(dri_drawable);
325 if (drawable == NULL)
326 goto drawable_fail;
327
328 dPriv->driverPrivate = drawable;
329 drawable->dPriv = dPriv;
330
331 drawable->row = malloc(MAX_WIDTH * 4);
332 if (drawable->row == NULL)
333 goto drawable_fail;
334
335 fb = &drawable->Base;
336
337 /* basic framebuffer setup */
338 _mesa_initialize_window_framebuffer(fb, visual);
339
340 /* add front renderbuffer */
341 frontrb = swrast_new_renderbuffer(visual, GL_TRUE);
342 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontrb->Base);
343
344 /* add back renderbuffer */
345 if (visual->doubleBufferMode) {
346 backrb = swrast_new_renderbuffer(visual, GL_FALSE);
347 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backrb->Base);
348 }
349
350 /* add software renderbuffers */
351 _mesa_add_soft_renderbuffers(fb,
352 GL_FALSE, /* color */
353 visual->haveDepthBuffer,
354 visual->haveStencilBuffer,
355 visual->haveAccumBuffer,
356 GL_FALSE, /* alpha */
357 GL_FALSE /* aux bufs */);
358
359 return GL_TRUE;
360
361 drawable_fail:
362
363 if (drawable)
364 free(drawable->row);
365
366 FREE(drawable);
367
368 return GL_FALSE;
369 }
370
371 static void
372 dri_destroy_buffer(__DRIdrawable * dPriv)
373 {
374 TRACE;
375
376 if (dPriv) {
377 struct dri_drawable *drawable = dri_drawable(dPriv);
378 GLframebuffer *fb;
379
380 free(drawable->row);
381
382 fb = &drawable->Base;
383
384 fb->DeletePending = GL_TRUE;
385 _mesa_reference_framebuffer(&fb, NULL);
386 }
387 }
388
389 static void
390 dri_swap_buffers(__DRIdrawable * dPriv)
391 {
392 __DRIscreen *sPriv = dPriv->driScreenPriv;
393
394 GET_CURRENT_CONTEXT(ctx);
395
396 struct dri_drawable *drawable = dri_drawable(dPriv);
397 GLframebuffer *fb;
398 struct swrast_renderbuffer *frontrb, *backrb;
399
400 TRACE;
401
402 fb = &drawable->Base;
403
404 frontrb =
405 swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
406 backrb =
407 swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
408
409 /* check for signle-buffered */
410 if (backrb == NULL)
411 return;
412
413 /* check if swapping currently bound buffer */
414 if (ctx && ctx->DrawBuffer == fb) {
415 /* flush pending rendering */
416 _mesa_notifySwapBuffers(ctx);
417 }
418
419 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
420 0, 0,
421 frontrb->Base.Width,
422 frontrb->Base.Height,
423 backrb->Base.Data,
424 dPriv->loaderPrivate);
425 }
426
427
428 /**
429 * General device driver functions.
430 */
431
432 static void
433 get_window_size( GLframebuffer *fb, GLsizei *w, GLsizei *h )
434 {
435 __DRIdrawable *dPriv = swrast_drawable(fb)->dPriv;
436 __DRIscreen *sPriv = dPriv->driScreenPriv;
437 int x, y;
438
439 sPriv->swrast_loader->getDrawableInfo(dPriv,
440 &x, &y, w, h,
441 dPriv->loaderPrivate);
442 }
443
444 static void
445 swrast_check_and_update_window_size( GLcontext *ctx, GLframebuffer *fb )
446 {
447 GLsizei width, height;
448
449 get_window_size(fb, &width, &height);
450 if (fb->Width != width || fb->Height != height) {
451 _mesa_resize_framebuffer(ctx, fb, width, height);
452 }
453 }
454
455 static const GLubyte *
456 get_string(GLcontext *ctx, GLenum pname)
457 {
458 (void) ctx;
459 switch (pname) {
460 case GL_VENDOR:
461 return (const GLubyte *) "Mesa Project";
462 case GL_RENDERER:
463 return (const GLubyte *) "Software Rasterizer";
464 default:
465 return NULL;
466 }
467 }
468
469 static void
470 update_state( GLcontext *ctx, GLuint new_state )
471 {
472 /* not much to do here - pass it on */
473 _swrast_InvalidateState( ctx, new_state );
474 _swsetup_InvalidateState( ctx, new_state );
475 _vbo_InvalidateState( ctx, new_state );
476 _tnl_InvalidateState( ctx, new_state );
477 }
478
479 static void
480 viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
481 {
482 GLframebuffer *draw = ctx->WinSysDrawBuffer;
483 GLframebuffer *read = ctx->WinSysReadBuffer;
484
485 swrast_check_and_update_window_size(ctx, draw);
486 swrast_check_and_update_window_size(ctx, read);
487 }
488
489 static void
490 swrast_init_driver_functions(struct dd_function_table *driver)
491 {
492 driver->GetString = get_string;
493 driver->UpdateState = update_state;
494 driver->GetBufferSize = NULL;
495 driver->Viewport = viewport;
496 }
497
498
499 /**
500 * Context-related functions.
501 */
502
503 static GLboolean
504 dri_create_context(const __GLcontextModes * visual,
505 __DRIcontext * cPriv, void *sharedContextPrivate)
506 {
507 struct dri_context *ctx = NULL;
508 struct dri_context *share = (struct dri_context *)sharedContextPrivate;
509 GLcontext *mesaCtx = NULL;
510 GLcontext *sharedCtx = NULL;
511 struct dd_function_table functions;
512
513 TRACE;
514
515 ctx = CALLOC_STRUCT(dri_context);
516 if (ctx == NULL)
517 goto context_fail;
518
519 cPriv->driverPrivate = ctx;
520 ctx->cPriv = cPriv;
521
522 /* build table of device driver functions */
523 _mesa_init_driver_functions(&functions);
524 swrast_init_driver_functions(&functions);
525
526 if (share) {
527 sharedCtx = &share->Base;
528 }
529
530 mesaCtx = &ctx->Base;
531
532 /* basic context setup */
533 if (!_mesa_initialize_context(mesaCtx, visual, sharedCtx, &functions, (void *) cPriv)) {
534 goto context_fail;
535 }
536
537 /* do bounds checking to prevent segfaults and server crashes! */
538 mesaCtx->Const.CheckArrayBounds = GL_TRUE;
539
540 /* create module contexts */
541 _swrast_CreateContext( mesaCtx );
542 _vbo_CreateContext( mesaCtx );
543 _tnl_CreateContext( mesaCtx );
544 _swsetup_CreateContext( mesaCtx );
545 _swsetup_Wakeup( mesaCtx );
546
547 /* use default TCL pipeline */
548 {
549 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
550 tnl->Driver.RunPipeline = _tnl_run_pipeline;
551 }
552
553 _mesa_enable_sw_extensions(mesaCtx);
554 _mesa_enable_1_3_extensions(mesaCtx);
555 _mesa_enable_1_4_extensions(mesaCtx);
556 _mesa_enable_1_5_extensions(mesaCtx);
557 _mesa_enable_2_0_extensions(mesaCtx);
558 _mesa_enable_2_1_extensions(mesaCtx);
559
560 _mesa_meta_init(mesaCtx);
561
562 driInitExtensions( mesaCtx, NULL, GL_FALSE );
563
564 return GL_TRUE;
565
566 context_fail:
567
568 FREE(ctx);
569
570 return GL_FALSE;
571 }
572
573 static void
574 dri_destroy_context(__DRIcontext * cPriv)
575 {
576 TRACE;
577
578 if (cPriv) {
579 struct dri_context *ctx = dri_context(cPriv);
580 GLcontext *mesaCtx;
581
582 mesaCtx = &ctx->Base;
583
584 _mesa_meta_free(mesaCtx);
585 _swsetup_DestroyContext( mesaCtx );
586 _swrast_DestroyContext( mesaCtx );
587 _tnl_DestroyContext( mesaCtx );
588 _vbo_DestroyContext( mesaCtx );
589 _mesa_destroy_context( mesaCtx );
590 }
591 }
592
593 static GLboolean
594 dri_make_current(__DRIcontext * cPriv,
595 __DRIdrawable * driDrawPriv,
596 __DRIdrawable * driReadPriv)
597 {
598 GLcontext *mesaCtx;
599 GLframebuffer *mesaDraw;
600 GLframebuffer *mesaRead;
601 TRACE;
602
603 if (cPriv) {
604 struct dri_context *ctx = dri_context(cPriv);
605 struct dri_drawable *draw = dri_drawable(driDrawPriv);
606 struct dri_drawable *read = dri_drawable(driReadPriv);
607
608 if (!driDrawPriv || !driReadPriv)
609 return GL_FALSE;
610
611 mesaCtx = &ctx->Base;
612 mesaDraw = &draw->Base;
613 mesaRead = &read->Base;
614
615 /* check for same context and buffer */
616 if (mesaCtx == _mesa_get_current_context()
617 && mesaCtx->DrawBuffer == mesaDraw
618 && mesaCtx->ReadBuffer == mesaRead) {
619 return GL_TRUE;
620 }
621
622 _glapi_check_multithread();
623
624 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
625 if (mesaRead != mesaDraw)
626 swrast_check_and_update_window_size(mesaCtx, mesaRead);
627
628 _mesa_make_current( mesaCtx,
629 mesaDraw,
630 mesaRead );
631 }
632 else {
633 /* unbind */
634 _mesa_make_current( NULL, NULL, NULL );
635 }
636
637 return GL_TRUE;
638 }
639
640 static GLboolean
641 dri_unbind_context(__DRIcontext * cPriv)
642 {
643 TRACE;
644 (void) cPriv;
645 return GL_TRUE;
646 }
647
648
649 const struct __DriverAPIRec driDriverAPI = {
650 .InitScreen = dri_init_screen,
651 .DestroyScreen = dri_destroy_screen,
652 .CreateContext = dri_create_context,
653 .DestroyContext = dri_destroy_context,
654 .CreateBuffer = dri_create_buffer,
655 .DestroyBuffer = dri_destroy_buffer,
656 .SwapBuffers = dri_swap_buffers,
657 .MakeCurrent = dri_make_current,
658 .UnbindContext = dri_unbind_context,
659 };
660
661 /* This is the table of extensions that the loader will dlsym() for. */
662 PUBLIC const __DRIextension *__driDriverExtensions[] = {
663 &driCoreExtension.base,
664 &driSWRastExtension.base,
665 NULL
666 };