ae5874f59278b74d0787ec88957e1eed849d7918
[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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 /*
24 * DRI software rasterizer
25 *
26 * This is the mesa swrast module packaged into a DRI driver structure.
27 *
28 * The front-buffer is allocated by the loader. The loader provides read/write
29 * callbacks for access to the front-buffer. The driver uses a scratch row for
30 * front-buffer rendering to avoid repeated calls to the loader.
31 *
32 * The back-buffer is allocated by the driver and is private.
33 */
34
35 #include <stdio.h>
36 #include "main/api_exec.h"
37 #include "main/context.h"
38 #include "main/extensions.h"
39 #include "main/formats.h"
40 #include "main/framebuffer.h"
41 #include "main/imports.h"
42 #include "main/renderbuffer.h"
43 #include "main/version.h"
44 #include "main/vtxfmt.h"
45 #include "swrast/swrast.h"
46 #include "swrast/s_renderbuffer.h"
47 #include "swrast_setup/swrast_setup.h"
48 #include "tnl/tnl.h"
49 #include "tnl/t_context.h"
50 #include "tnl/t_pipeline.h"
51 #include "vbo/vbo.h"
52 #include "drivers/common/driverfuncs.h"
53 #include "drivers/common/meta.h"
54 #include "utils.h"
55
56 #include "main/teximage.h"
57 #include "main/texformat.h"
58 #include "main/texobj.h"
59 #include "main/texstate.h"
60
61 #include "swrast_priv.h"
62 #include "swrast/s_context.h"
63
64 #include <sys/types.h>
65 #ifdef HAVE_SYS_SYSCTL_H
66 # include <sys/sysctl.h>
67 #endif
68
69 const __DRIextension **__driDriverGetExtensions_swrast(void);
70
71 const char * const swrast_vendor_string = "Mesa Project";
72 const char * const swrast_renderer_string = "Software Rasterizer";
73
74 /**
75 * Screen and config-related functions
76 */
77
78 static void swrastSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
79 GLint texture_format, __DRIdrawable *dPriv)
80 {
81 struct dri_context *dri_ctx;
82 int x, y, w, h;
83 __DRIscreen *sPriv = dPriv->driScreenPriv;
84 struct gl_texture_object *texObj;
85 struct gl_texture_image *texImage;
86 struct swrast_texture_image *swImage;
87 uint32_t internalFormat;
88 mesa_format texFormat;
89
90 dri_ctx = pDRICtx->driverPrivate;
91
92 internalFormat = (texture_format == __DRI_TEXTURE_FORMAT_RGB ? 3 : 4);
93
94 texObj = _mesa_get_current_tex_object(&dri_ctx->Base, target);
95 texImage = _mesa_get_tex_image(&dri_ctx->Base, texObj, target, 0);
96 swImage = swrast_texture_image(texImage);
97
98 _mesa_lock_texture(&dri_ctx->Base, texObj);
99
100 sPriv->swrast_loader->getDrawableInfo(dPriv, &x, &y, &w, &h, dPriv->loaderPrivate);
101
102 if (texture_format == __DRI_TEXTURE_FORMAT_RGB)
103 texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
104 else
105 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
106
107 _mesa_init_teximage_fields(&dri_ctx->Base, texImage,
108 w, h, 1, 0, internalFormat, texFormat);
109
110 sPriv->swrast_loader->getImage(dPriv, x, y, w, h, (char *)swImage->Buffer,
111 dPriv->loaderPrivate);
112
113 _mesa_unlock_texture(&dri_ctx->Base, texObj);
114 }
115
116 static void swrastSetTexBuffer(__DRIcontext *pDRICtx, GLint target,
117 __DRIdrawable *dPriv)
118 {
119 swrastSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
120 }
121
122 static const __DRItexBufferExtension swrastTexBufferExtension = {
123 .base = { __DRI_TEX_BUFFER, 3 },
124
125 .setTexBuffer = swrastSetTexBuffer,
126 .setTexBuffer2 = swrastSetTexBuffer2,
127 .releaseTexBuffer = NULL,
128 };
129
130
131 static int
132 swrast_query_renderer_integer(__DRIscreen *psp, int param,
133 unsigned int *value)
134 {
135 switch (param) {
136 case __DRI2_RENDERER_VENDOR_ID:
137 case __DRI2_RENDERER_DEVICE_ID:
138 /* Return 0xffffffff for both vendor and device id */
139 value[0] = 0xffffffff;
140 return 0;
141 case __DRI2_RENDERER_ACCELERATED:
142 value[0] = 0;
143 return 0;
144 case __DRI2_RENDERER_VIDEO_MEMORY: {
145 /* This should probably share code with os_get_total_physical_memory()
146 * from src/gallium/auxiliary/os/os_misc.c
147 */
148 #if defined(CTL_HW) && defined(HW_MEMSIZE)
149 int mib[2] = { CTL_HW, HW_MEMSIZE };
150 unsigned long system_memory_bytes;
151 size_t len = sizeof(system_memory_bytes);
152 if (sysctl(mib, 2, &system_memory_bytes, &len, NULL, 0) != 0)
153 return -1;
154 #elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE)
155 /* XXX: Do we want to return the full amount of system memory ? */
156 const long system_memory_pages = sysconf(_SC_PHYS_PAGES);
157 const long system_page_size = sysconf(_SC_PAGE_SIZE);
158
159 if (system_memory_pages <= 0 || system_page_size <= 0)
160 return -1;
161
162 const uint64_t system_memory_bytes = (uint64_t) system_memory_pages
163 * (uint64_t) system_page_size;
164 #else
165 #error "Unsupported platform"
166 #endif
167
168 const unsigned system_memory_megabytes =
169 (unsigned) (system_memory_bytes / (1024 * 1024));
170
171 value[0] = system_memory_megabytes;
172 return 0;
173 }
174 case __DRI2_RENDERER_UNIFIED_MEMORY_ARCHITECTURE:
175 /**
176 * XXX: Perhaps we should return 1 ?
177 * See issue #7 from the spec, currently UNRESOLVED.
178 */
179 value[0] = 0;
180 return 0;
181 default:
182 return driQueryRendererIntegerCommon(psp, param, value);
183 }
184 }
185
186 static int
187 swrast_query_renderer_string(__DRIscreen *psp, int param, const char **value)
188 {
189 switch (param) {
190 case __DRI2_RENDERER_VENDOR_ID:
191 value[0] = swrast_vendor_string;
192 return 0;
193 case __DRI2_RENDERER_DEVICE_ID:
194 value[0] = swrast_renderer_string;
195 return 0;
196 default:
197 return -1;
198 }
199 }
200
201 static const __DRI2rendererQueryExtension swrast_query_renderer_extension = {
202 .base = { __DRI2_RENDERER_QUERY, 1 },
203
204 .queryInteger = swrast_query_renderer_integer,
205 .queryString = swrast_query_renderer_string
206 };
207
208 static const __DRIextension *dri_screen_extensions[] = {
209 &swrastTexBufferExtension.base,
210 &swrast_query_renderer_extension.base,
211 &dri2ConfigQueryExtension.base,
212 &dri2NoErrorExtension.base,
213 NULL
214 };
215
216 static __DRIconfig **
217 swrastFillInModes(__DRIscreen *psp,
218 unsigned pixel_bits, unsigned depth_bits,
219 unsigned stencil_bits, GLboolean have_back_buffer)
220 {
221 __DRIconfig **configs;
222 unsigned depth_buffer_factor;
223 unsigned back_buffer_factor;
224 mesa_format format;
225
226 static const GLenum back_buffer_modes[] = {
227 __DRI_ATTRIB_SWAP_NONE, __DRI_ATTRIB_SWAP_UNDEFINED
228 };
229
230 uint8_t depth_bits_array[4];
231 uint8_t stencil_bits_array[4];
232 uint8_t msaa_samples_array[1];
233
234 (void) psp;
235 (void) have_back_buffer;
236
237 depth_bits_array[0] = 0;
238 depth_bits_array[1] = 0;
239 depth_bits_array[2] = depth_bits;
240 depth_bits_array[3] = depth_bits;
241
242 /* Just like with the accumulation buffer, always provide some modes
243 * with a stencil buffer.
244 */
245 stencil_bits_array[0] = 0;
246 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
247 stencil_bits_array[2] = 0;
248 stencil_bits_array[3] = (stencil_bits == 0) ? 8 : stencil_bits;
249
250 msaa_samples_array[0] = 0;
251
252 depth_buffer_factor = 4;
253 back_buffer_factor = 2;
254
255 switch (pixel_bits) {
256 case 16:
257 format = MESA_FORMAT_B5G6R5_UNORM;
258 break;
259 case 24:
260 format = MESA_FORMAT_B8G8R8X8_UNORM;
261 break;
262 case 32:
263 format = MESA_FORMAT_B8G8R8A8_UNORM;
264 break;
265 default:
266 fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
267 pixel_bits);
268 return NULL;
269 }
270
271 configs = driCreateConfigs(format,
272 depth_bits_array, stencil_bits_array,
273 depth_buffer_factor, back_buffer_modes,
274 back_buffer_factor, msaa_samples_array, 1,
275 GL_TRUE, GL_FALSE);
276 if (configs == NULL) {
277 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
278 __LINE__);
279 return NULL;
280 }
281
282 return configs;
283 }
284
285 static const __DRIconfig **
286 dri_init_screen(__DRIscreen * psp)
287 {
288 __DRIconfig **configs16, **configs24, **configs32;
289
290 TRACE;
291
292 psp->max_gl_compat_version = 21;
293 psp->max_gl_es1_version = 11;
294 psp->max_gl_es2_version = 20;
295
296 psp->extensions = dri_screen_extensions;
297
298 configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
299 configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
300 configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
301
302 configs24 = driConcatConfigs(configs16, configs24);
303 configs32 = driConcatConfigs(configs24, configs32);
304
305 return (const __DRIconfig **)configs32;
306 }
307
308 static void
309 dri_destroy_screen(__DRIscreen * sPriv)
310 {
311 TRACE;
312 (void) sPriv;
313 }
314
315
316 /**
317 * Framebuffer and renderbuffer-related functions.
318 */
319
320 static GLuint
321 choose_pixel_format(const struct gl_config *v)
322 {
323 int depth = v->rgbBits;
324
325 if (depth == 32
326 && v->redMask == 0xff0000
327 && v->greenMask == 0x00ff00
328 && v->blueMask == 0x0000ff)
329 return PF_A8R8G8B8;
330 else if (depth == 24
331 && v->redMask == 0xff0000
332 && v->greenMask == 0x00ff00
333 && v->blueMask == 0x0000ff)
334 return PF_X8R8G8B8;
335 else if (depth == 16
336 && v->redMask == 0xf800
337 && v->greenMask == 0x07e0
338 && v->blueMask == 0x001f)
339 return PF_R5G6B5;
340 else if (depth == 8
341 && v->redMask == 0x07
342 && v->greenMask == 0x38
343 && v->blueMask == 0xc0)
344 return PF_R3G3B2;
345
346 _mesa_problem( NULL, "unexpected format in %s", __func__ );
347 return 0;
348 }
349
350 static void
351 swrast_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
352 {
353 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
354
355 TRACE;
356
357 free(xrb->Base.Buffer);
358 _mesa_delete_renderbuffer(ctx, rb);
359 }
360
361 /* see bytes_per_line in libGL */
362 static inline int
363 bytes_per_line(unsigned pitch_bits, unsigned mul)
364 {
365 unsigned mask = mul - 1;
366
367 return ((pitch_bits + mask) & ~mask) / 8;
368 }
369
370 static GLboolean
371 swrast_alloc_front_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
372 GLenum internalFormat, GLuint width, GLuint height)
373 {
374 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
375
376 TRACE;
377
378 (void) ctx;
379 (void) internalFormat;
380
381 xrb->Base.Buffer = NULL;
382 rb->Width = width;
383 rb->Height = height;
384 xrb->pitch = bytes_per_line(width * xrb->bpp, 32);
385
386 return GL_TRUE;
387 }
388
389 static GLboolean
390 swrast_alloc_back_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
391 GLenum internalFormat, GLuint width, GLuint height)
392 {
393 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
394
395 TRACE;
396
397 free(xrb->Base.Buffer);
398
399 swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
400
401 xrb->Base.Buffer = malloc(height * xrb->pitch);
402
403 return GL_TRUE;
404 }
405
406 static struct dri_swrast_renderbuffer *
407 swrast_new_renderbuffer(const struct gl_config *visual, __DRIdrawable *dPriv,
408 GLboolean front)
409 {
410 struct dri_swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
411 struct gl_renderbuffer *rb;
412 GLuint pixel_format;
413
414 TRACE;
415
416 if (!xrb)
417 return NULL;
418
419 rb = &xrb->Base.Base;
420
421 _mesa_init_renderbuffer(rb, 0);
422
423 pixel_format = choose_pixel_format(visual);
424
425 xrb->dPriv = dPriv;
426 xrb->Base.Base.Delete = swrast_delete_renderbuffer;
427 if (front) {
428 rb->AllocStorage = swrast_alloc_front_storage;
429 }
430 else {
431 rb->AllocStorage = swrast_alloc_back_storage;
432 }
433
434 switch (pixel_format) {
435 case PF_A8R8G8B8:
436 rb->Format = MESA_FORMAT_B8G8R8A8_UNORM;
437 rb->InternalFormat = GL_RGBA;
438 rb->_BaseFormat = GL_RGBA;
439 xrb->bpp = 32;
440 break;
441 case PF_X8R8G8B8:
442 rb->Format = MESA_FORMAT_B8G8R8A8_UNORM; /* XXX */
443 rb->InternalFormat = GL_RGB;
444 rb->_BaseFormat = GL_RGB;
445 xrb->bpp = 32;
446 break;
447 case PF_R5G6B5:
448 rb->Format = MESA_FORMAT_B5G6R5_UNORM;
449 rb->InternalFormat = GL_RGB;
450 rb->_BaseFormat = GL_RGB;
451 xrb->bpp = 16;
452 break;
453 case PF_R3G3B2:
454 rb->Format = MESA_FORMAT_B2G3R3_UNORM;
455 rb->InternalFormat = GL_RGB;
456 rb->_BaseFormat = GL_RGB;
457 xrb->bpp = 8;
458 break;
459 default:
460 free(xrb);
461 return NULL;
462 }
463
464 return xrb;
465 }
466
467 static void
468 swrast_map_renderbuffer(struct gl_context *ctx,
469 struct gl_renderbuffer *rb,
470 GLuint x, GLuint y, GLuint w, GLuint h,
471 GLbitfield mode,
472 GLubyte **out_map,
473 GLint *out_stride)
474 {
475 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
476 GLubyte *map = xrb->Base.Buffer;
477 int cpp = _mesa_get_format_bytes(rb->Format);
478 int stride = rb->Width * cpp;
479
480 if (rb->AllocStorage == swrast_alloc_front_storage) {
481 __DRIdrawable *dPriv = xrb->dPriv;
482 __DRIscreen *sPriv = dPriv->driScreenPriv;
483
484 xrb->map_mode = mode;
485 xrb->map_x = x;
486 xrb->map_y = rb->Height - y - h;
487 xrb->map_w = w;
488 xrb->map_h = h;
489
490 stride = w * cpp;
491 xrb->Base.Buffer = malloc(h * stride);
492
493 sPriv->swrast_loader->getImage(dPriv, x, xrb->map_y, w, h,
494 (char *) xrb->Base.Buffer,
495 dPriv->loaderPrivate);
496
497 *out_map = xrb->Base.Buffer + (h - 1) * stride;
498 *out_stride = -stride;
499 return;
500 }
501
502 assert(xrb->Base.Buffer);
503
504 if (rb->AllocStorage == swrast_alloc_back_storage) {
505 map += (rb->Height - 1) * stride;
506 stride = -stride;
507 }
508
509 map += (GLsizei)y * stride;
510 map += (GLsizei)x * cpp;
511
512 *out_map = map;
513 *out_stride = stride;
514 }
515
516 static void
517 swrast_unmap_renderbuffer(struct gl_context *ctx,
518 struct gl_renderbuffer *rb)
519 {
520 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
521
522 if (rb->AllocStorage == swrast_alloc_front_storage) {
523 __DRIdrawable *dPriv = xrb->dPriv;
524 __DRIscreen *sPriv = dPriv->driScreenPriv;
525
526 if (xrb->map_mode & GL_MAP_WRITE_BIT) {
527 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_DRAW,
528 xrb->map_x, xrb->map_y,
529 xrb->map_w, xrb->map_h,
530 (char *) xrb->Base.Buffer,
531 dPriv->loaderPrivate);
532 }
533
534 free(xrb->Base.Buffer);
535 xrb->Base.Buffer = NULL;
536 }
537 }
538
539 static GLboolean
540 dri_create_buffer(__DRIscreen * sPriv,
541 __DRIdrawable * dPriv,
542 const struct gl_config * visual, GLboolean isPixmap)
543 {
544 struct dri_drawable *drawable = NULL;
545 struct gl_framebuffer *fb;
546 struct dri_swrast_renderbuffer *frontrb, *backrb;
547
548 TRACE;
549
550 (void) sPriv;
551 (void) isPixmap;
552
553 drawable = CALLOC_STRUCT(dri_drawable);
554 if (drawable == NULL)
555 goto drawable_fail;
556
557 dPriv->driverPrivate = drawable;
558 drawable->dPriv = dPriv;
559
560 drawable->row = malloc(SWRAST_MAX_WIDTH * 4);
561 if (drawable->row == NULL)
562 goto drawable_fail;
563
564 fb = &drawable->Base;
565
566 /* basic framebuffer setup */
567 _mesa_initialize_window_framebuffer(fb, visual);
568
569 /* add front renderbuffer */
570 frontrb = swrast_new_renderbuffer(visual, dPriv, GL_TRUE);
571 _mesa_attach_and_own_rb(fb, BUFFER_FRONT_LEFT, &frontrb->Base.Base);
572
573 /* add back renderbuffer */
574 if (visual->doubleBufferMode) {
575 backrb = swrast_new_renderbuffer(visual, dPriv, GL_FALSE);
576 _mesa_attach_and_own_rb(fb, BUFFER_BACK_LEFT, &backrb->Base.Base);
577 }
578
579 /* add software renderbuffers */
580 _swrast_add_soft_renderbuffers(fb,
581 GL_FALSE, /* color */
582 visual->haveDepthBuffer,
583 visual->haveStencilBuffer,
584 visual->haveAccumBuffer,
585 GL_FALSE, /* alpha */
586 GL_FALSE /* aux bufs */);
587
588 return GL_TRUE;
589
590 drawable_fail:
591
592 if (drawable)
593 free(drawable->row);
594
595 free(drawable);
596
597 return GL_FALSE;
598 }
599
600 static void
601 dri_destroy_buffer(__DRIdrawable * dPriv)
602 {
603 TRACE;
604
605 if (dPriv) {
606 struct dri_drawable *drawable = dri_drawable(dPriv);
607 struct gl_framebuffer *fb;
608
609 free(drawable->row);
610
611 fb = &drawable->Base;
612
613 fb->DeletePending = GL_TRUE;
614 _mesa_reference_framebuffer(&fb, NULL);
615 }
616 }
617
618 static void
619 dri_swap_buffers(__DRIdrawable * dPriv)
620 {
621 __DRIscreen *sPriv = dPriv->driScreenPriv;
622
623 GET_CURRENT_CONTEXT(ctx);
624
625 struct dri_drawable *drawable = dri_drawable(dPriv);
626 struct gl_framebuffer *fb;
627 struct dri_swrast_renderbuffer *frontrb, *backrb;
628
629 TRACE;
630
631 fb = &drawable->Base;
632
633 frontrb =
634 dri_swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
635 backrb =
636 dri_swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
637
638 /* check for signle-buffered */
639 if (backrb == NULL)
640 return;
641
642 /* check if swapping currently bound buffer */
643 if (ctx && ctx->DrawBuffer == fb) {
644 /* flush pending rendering */
645 _mesa_notifySwapBuffers(ctx);
646 }
647
648 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
649 0, 0,
650 frontrb->Base.Base.Width,
651 frontrb->Base.Base.Height,
652 (char *) backrb->Base.Buffer,
653 dPriv->loaderPrivate);
654 }
655
656
657 /**
658 * General device driver functions.
659 */
660
661 static void
662 get_window_size( struct gl_framebuffer *fb, GLsizei *w, GLsizei *h )
663 {
664 __DRIdrawable *dPriv = swrast_drawable(fb)->dPriv;
665 __DRIscreen *sPriv = dPriv->driScreenPriv;
666 int x, y;
667
668 sPriv->swrast_loader->getDrawableInfo(dPriv,
669 &x, &y, w, h,
670 dPriv->loaderPrivate);
671 }
672
673 static void
674 swrast_check_and_update_window_size( struct gl_context *ctx, struct gl_framebuffer *fb )
675 {
676 GLsizei width, height;
677
678 get_window_size(fb, &width, &height);
679 if (fb->Width != width || fb->Height != height) {
680 _mesa_resize_framebuffer(ctx, fb, width, height);
681 }
682 }
683
684 static const GLubyte *
685 get_string(struct gl_context *ctx, GLenum pname)
686 {
687 (void) ctx;
688 switch (pname) {
689 case GL_VENDOR:
690 return (const GLubyte *) swrast_vendor_string;
691 case GL_RENDERER:
692 return (const GLubyte *) swrast_renderer_string;
693 default:
694 return NULL;
695 }
696 }
697
698 static void
699 update_state(struct gl_context *ctx)
700 {
701 GLuint new_state = ctx->NewState;
702
703 if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
704 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
705
706 /* not much to do here - pass it on */
707 _swrast_InvalidateState( ctx, new_state );
708 _swsetup_InvalidateState( ctx, new_state );
709 _tnl_InvalidateState( ctx, new_state );
710 }
711
712 static void
713 viewport(struct gl_context *ctx)
714 {
715 struct gl_framebuffer *draw = ctx->WinSysDrawBuffer;
716 struct gl_framebuffer *read = ctx->WinSysReadBuffer;
717
718 swrast_check_and_update_window_size(ctx, draw);
719 swrast_check_and_update_window_size(ctx, read);
720 }
721
722 static mesa_format swrastChooseTextureFormat(struct gl_context * ctx,
723 GLenum target,
724 GLint internalFormat,
725 GLenum format,
726 GLenum type)
727 {
728 if (internalFormat == GL_RGB)
729 return MESA_FORMAT_B8G8R8X8_UNORM;
730 return _mesa_choose_tex_format(ctx, target, internalFormat, format, type);
731 }
732
733 static void
734 swrast_init_driver_functions(struct dd_function_table *driver)
735 {
736 driver->GetString = get_string;
737 driver->UpdateState = update_state;
738 driver->Viewport = viewport;
739 driver->ChooseTextureFormat = swrastChooseTextureFormat;
740 driver->MapRenderbuffer = swrast_map_renderbuffer;
741 driver->UnmapRenderbuffer = swrast_unmap_renderbuffer;
742 }
743
744 /**
745 * Context-related functions.
746 */
747
748 static GLboolean
749 dri_create_context(gl_api api,
750 const struct gl_config * visual,
751 __DRIcontext * cPriv,
752 const struct __DriverContextConfig *ctx_config,
753 unsigned *error,
754 void *sharedContextPrivate)
755 {
756 struct dri_context *ctx = NULL;
757 struct dri_context *share = (struct dri_context *)sharedContextPrivate;
758 struct gl_context *mesaCtx = NULL;
759 struct gl_context *sharedCtx = NULL;
760 struct dd_function_table functions;
761
762 TRACE;
763
764 /* Flag filtering is handled in dri2CreateContextAttribs.
765 */
766 (void) ctx_config->flags;
767
768 /* The swrast driver doesn't understand any of the attributes */
769 if (ctx_config->attribute_mask != 0) {
770 *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
771 return false;
772 }
773
774 ctx = CALLOC_STRUCT(dri_context);
775 if (ctx == NULL) {
776 *error = __DRI_CTX_ERROR_NO_MEMORY;
777 goto context_fail;
778 }
779
780 cPriv->driverPrivate = ctx;
781 ctx->cPriv = cPriv;
782
783 /* build table of device driver functions */
784 _mesa_init_driver_functions(&functions);
785 swrast_init_driver_functions(&functions);
786 _tnl_init_driver_draw_function(&functions);
787
788 if (share) {
789 sharedCtx = &share->Base;
790 }
791
792 mesaCtx = &ctx->Base;
793
794 /* basic context setup */
795 if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions)) {
796 *error = __DRI_CTX_ERROR_NO_MEMORY;
797 goto context_fail;
798 }
799
800 driContextSetFlags(mesaCtx, ctx_config->flags);
801
802 /* create module contexts */
803 _swrast_CreateContext( mesaCtx );
804 _vbo_CreateContext( mesaCtx );
805 _tnl_CreateContext( mesaCtx );
806 _swsetup_CreateContext( mesaCtx );
807 _swsetup_Wakeup( mesaCtx );
808
809 /* use default TCL pipeline */
810 {
811 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
812 tnl->Driver.RunPipeline = _tnl_run_pipeline;
813 }
814
815 _mesa_meta_init(mesaCtx);
816 _mesa_enable_sw_extensions(mesaCtx);
817
818 _mesa_override_extensions(mesaCtx);
819 _mesa_compute_version(mesaCtx);
820
821 _mesa_initialize_dispatch_tables(mesaCtx);
822 _mesa_initialize_vbo_vtxfmt(mesaCtx);
823
824 *error = __DRI_CTX_ERROR_SUCCESS;
825 return GL_TRUE;
826
827 context_fail:
828
829 free(ctx);
830
831 return GL_FALSE;
832 }
833
834 static void
835 dri_destroy_context(__DRIcontext * cPriv)
836 {
837 TRACE;
838
839 if (cPriv) {
840 struct dri_context *ctx = dri_context(cPriv);
841 struct gl_context *mesaCtx;
842
843 mesaCtx = &ctx->Base;
844
845 _mesa_meta_free(mesaCtx);
846 _swsetup_DestroyContext( mesaCtx );
847 _swrast_DestroyContext( mesaCtx );
848 _tnl_DestroyContext( mesaCtx );
849 _vbo_DestroyContext( mesaCtx );
850 _mesa_destroy_context( mesaCtx );
851 }
852 }
853
854 static GLboolean
855 dri_make_current(__DRIcontext * cPriv,
856 __DRIdrawable * driDrawPriv,
857 __DRIdrawable * driReadPriv)
858 {
859 struct gl_context *mesaCtx;
860 struct gl_framebuffer *mesaDraw;
861 struct gl_framebuffer *mesaRead;
862 TRACE;
863
864 if (cPriv) {
865 struct dri_context *ctx = dri_context(cPriv);
866 struct dri_drawable *draw;
867 struct dri_drawable *read;
868
869 if (!driDrawPriv || !driReadPriv)
870 return GL_FALSE;
871
872 draw = dri_drawable(driDrawPriv);
873 read = dri_drawable(driReadPriv);
874 mesaCtx = &ctx->Base;
875 mesaDraw = &draw->Base;
876 mesaRead = &read->Base;
877
878 /* check for same context and buffer */
879 if (mesaCtx == _mesa_get_current_context()
880 && mesaCtx->DrawBuffer == mesaDraw
881 && mesaCtx->ReadBuffer == mesaRead) {
882 return GL_TRUE;
883 }
884
885 _glapi_check_multithread();
886
887 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
888 if (mesaRead != mesaDraw)
889 swrast_check_and_update_window_size(mesaCtx, mesaRead);
890
891 _mesa_make_current( mesaCtx,
892 mesaDraw,
893 mesaRead );
894 }
895 else {
896 /* unbind */
897 _mesa_make_current( NULL, NULL, NULL );
898 }
899
900 return GL_TRUE;
901 }
902
903 static GLboolean
904 dri_unbind_context(__DRIcontext * cPriv)
905 {
906 TRACE;
907 (void) cPriv;
908
909 /* Unset current context and dispath table */
910 _mesa_make_current(NULL, NULL, NULL);
911
912 return GL_TRUE;
913 }
914
915 static void
916 dri_copy_sub_buffer(__DRIdrawable *dPriv, int x, int y,
917 int w, int h)
918 {
919 __DRIscreen *sPriv = dPriv->driScreenPriv;
920 void *data;
921 int iy;
922 struct dri_drawable *drawable = dri_drawable(dPriv);
923 struct gl_framebuffer *fb;
924 struct dri_swrast_renderbuffer *frontrb, *backrb;
925
926 TRACE;
927
928 fb = &drawable->Base;
929
930 frontrb =
931 dri_swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
932 backrb =
933 dri_swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
934
935 /* check for signle-buffered */
936 if (backrb == NULL)
937 return;
938
939 iy = frontrb->Base.Base.Height - y - h;
940 data = (char *)backrb->Base.Buffer + (iy * backrb->pitch) + (x * ((backrb->bpp + 7) / 8));
941 sPriv->swrast_loader->putImage2(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
942 x, iy, w, h,
943 frontrb->pitch,
944 data,
945 dPriv->loaderPrivate);
946 }
947
948
949 static const struct __DriverAPIRec swrast_driver_api = {
950 .InitScreen = dri_init_screen,
951 .DestroyScreen = dri_destroy_screen,
952 .CreateContext = dri_create_context,
953 .DestroyContext = dri_destroy_context,
954 .CreateBuffer = dri_create_buffer,
955 .DestroyBuffer = dri_destroy_buffer,
956 .SwapBuffers = dri_swap_buffers,
957 .MakeCurrent = dri_make_current,
958 .UnbindContext = dri_unbind_context,
959 .CopySubBuffer = dri_copy_sub_buffer,
960 };
961
962 static const struct __DRIDriverVtableExtensionRec swrast_vtable = {
963 .base = { __DRI_DRIVER_VTABLE, 1 },
964 .vtable = &swrast_driver_api,
965 };
966
967 static const __DRIextension *swrast_driver_extensions[] = {
968 &driCoreExtension.base,
969 &driSWRastExtension.base,
970 &driCopySubBufferExtension.base,
971 &swrast_vtable.base,
972 NULL
973 };
974
975 PUBLIC const __DRIextension **__driDriverGetExtensions_swrast(void)
976 {
977 globalDriverAPI = &swrast_driver_api;
978
979 return swrast_driver_extensions;
980 }