i965/miptree: Replace is_lossless_compressed with mt->aux_usage checks
[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 &dri2NoErrorExtension.base,
212 NULL
213 };
214
215 static __DRIconfig **
216 swrastFillInModes(__DRIscreen *psp,
217 unsigned pixel_bits, unsigned depth_bits,
218 unsigned stencil_bits, GLboolean have_back_buffer)
219 {
220 __DRIconfig **configs;
221 unsigned depth_buffer_factor;
222 unsigned back_buffer_factor;
223 mesa_format format;
224
225 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
226 * support pageflipping at all.
227 */
228 static const GLenum back_buffer_modes[] = {
229 GLX_NONE, GLX_SWAP_UNDEFINED_OML
230 };
231
232 uint8_t depth_bits_array[4];
233 uint8_t stencil_bits_array[4];
234 uint8_t msaa_samples_array[1];
235
236 (void) psp;
237 (void) have_back_buffer;
238
239 depth_bits_array[0] = 0;
240 depth_bits_array[1] = 0;
241 depth_bits_array[2] = depth_bits;
242 depth_bits_array[3] = depth_bits;
243
244 /* Just like with the accumulation buffer, always provide some modes
245 * with a stencil buffer.
246 */
247 stencil_bits_array[0] = 0;
248 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
249 stencil_bits_array[2] = 0;
250 stencil_bits_array[3] = (stencil_bits == 0) ? 8 : stencil_bits;
251
252 msaa_samples_array[0] = 0;
253
254 depth_buffer_factor = 4;
255 back_buffer_factor = 2;
256
257 switch (pixel_bits) {
258 case 16:
259 format = MESA_FORMAT_B5G6R5_UNORM;
260 break;
261 case 24:
262 format = MESA_FORMAT_B8G8R8X8_UNORM;
263 break;
264 case 32:
265 format = MESA_FORMAT_B8G8R8A8_UNORM;
266 break;
267 default:
268 fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
269 pixel_bits);
270 return NULL;
271 }
272
273 configs = driCreateConfigs(format,
274 depth_bits_array, stencil_bits_array,
275 depth_buffer_factor, back_buffer_modes,
276 back_buffer_factor, msaa_samples_array, 1,
277 GL_TRUE, GL_FALSE);
278 if (configs == NULL) {
279 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
280 __LINE__);
281 return NULL;
282 }
283
284 return configs;
285 }
286
287 static const __DRIconfig **
288 dri_init_screen(__DRIscreen * psp)
289 {
290 __DRIconfig **configs16, **configs24, **configs32;
291
292 TRACE;
293
294 psp->max_gl_compat_version = 21;
295 psp->max_gl_es1_version = 11;
296 psp->max_gl_es2_version = 20;
297
298 psp->extensions = dri_screen_extensions;
299
300 configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
301 configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
302 configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
303
304 configs24 = driConcatConfigs(configs16, configs24);
305 configs32 = driConcatConfigs(configs24, configs32);
306
307 return (const __DRIconfig **)configs32;
308 }
309
310 static void
311 dri_destroy_screen(__DRIscreen * sPriv)
312 {
313 TRACE;
314 (void) sPriv;
315 }
316
317
318 /**
319 * Framebuffer and renderbuffer-related functions.
320 */
321
322 static GLuint
323 choose_pixel_format(const struct gl_config *v)
324 {
325 int depth = v->rgbBits;
326
327 if (depth == 32
328 && v->redMask == 0xff0000
329 && v->greenMask == 0x00ff00
330 && v->blueMask == 0x0000ff)
331 return PF_A8R8G8B8;
332 else if (depth == 24
333 && v->redMask == 0xff0000
334 && v->greenMask == 0x00ff00
335 && v->blueMask == 0x0000ff)
336 return PF_X8R8G8B8;
337 else if (depth == 16
338 && v->redMask == 0xf800
339 && v->greenMask == 0x07e0
340 && v->blueMask == 0x001f)
341 return PF_R5G6B5;
342 else if (depth == 8
343 && v->redMask == 0x07
344 && v->greenMask == 0x38
345 && v->blueMask == 0xc0)
346 return PF_R3G3B2;
347
348 _mesa_problem( NULL, "unexpected format in %s", __func__ );
349 return 0;
350 }
351
352 static void
353 swrast_delete_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
354 {
355 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
356
357 TRACE;
358
359 free(xrb->Base.Buffer);
360 _mesa_delete_renderbuffer(ctx, rb);
361 }
362
363 /* see bytes_per_line in libGL */
364 static inline int
365 bytes_per_line(unsigned pitch_bits, unsigned mul)
366 {
367 unsigned mask = mul - 1;
368
369 return ((pitch_bits + mask) & ~mask) / 8;
370 }
371
372 static GLboolean
373 swrast_alloc_front_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
374 GLenum internalFormat, GLuint width, GLuint height)
375 {
376 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
377
378 TRACE;
379
380 (void) ctx;
381 (void) internalFormat;
382
383 xrb->Base.Buffer = NULL;
384 rb->Width = width;
385 rb->Height = height;
386 xrb->pitch = bytes_per_line(width * xrb->bpp, 32);
387
388 return GL_TRUE;
389 }
390
391 static GLboolean
392 swrast_alloc_back_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
393 GLenum internalFormat, GLuint width, GLuint height)
394 {
395 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
396
397 TRACE;
398
399 free(xrb->Base.Buffer);
400
401 swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
402
403 xrb->Base.Buffer = malloc(height * xrb->pitch);
404
405 return GL_TRUE;
406 }
407
408 static struct dri_swrast_renderbuffer *
409 swrast_new_renderbuffer(const struct gl_config *visual, __DRIdrawable *dPriv,
410 GLboolean front)
411 {
412 struct dri_swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
413 struct gl_renderbuffer *rb;
414 GLuint pixel_format;
415
416 TRACE;
417
418 if (!xrb)
419 return NULL;
420
421 rb = &xrb->Base.Base;
422
423 _mesa_init_renderbuffer(rb, 0);
424
425 pixel_format = choose_pixel_format(visual);
426
427 xrb->dPriv = dPriv;
428 xrb->Base.Base.Delete = swrast_delete_renderbuffer;
429 if (front) {
430 rb->AllocStorage = swrast_alloc_front_storage;
431 }
432 else {
433 rb->AllocStorage = swrast_alloc_back_storage;
434 }
435
436 switch (pixel_format) {
437 case PF_A8R8G8B8:
438 rb->Format = MESA_FORMAT_B8G8R8A8_UNORM;
439 rb->InternalFormat = GL_RGBA;
440 rb->_BaseFormat = GL_RGBA;
441 xrb->bpp = 32;
442 break;
443 case PF_X8R8G8B8:
444 rb->Format = MESA_FORMAT_B8G8R8A8_UNORM; /* XXX */
445 rb->InternalFormat = GL_RGB;
446 rb->_BaseFormat = GL_RGB;
447 xrb->bpp = 32;
448 break;
449 case PF_R5G6B5:
450 rb->Format = MESA_FORMAT_B5G6R5_UNORM;
451 rb->InternalFormat = GL_RGB;
452 rb->_BaseFormat = GL_RGB;
453 xrb->bpp = 16;
454 break;
455 case PF_R3G3B2:
456 rb->Format = MESA_FORMAT_B2G3R3_UNORM;
457 rb->InternalFormat = GL_RGB;
458 rb->_BaseFormat = GL_RGB;
459 xrb->bpp = 8;
460 break;
461 default:
462 free(xrb);
463 return NULL;
464 }
465
466 return xrb;
467 }
468
469 static void
470 swrast_map_renderbuffer(struct gl_context *ctx,
471 struct gl_renderbuffer *rb,
472 GLuint x, GLuint y, GLuint w, GLuint h,
473 GLbitfield mode,
474 GLubyte **out_map,
475 GLint *out_stride)
476 {
477 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
478 GLubyte *map = xrb->Base.Buffer;
479 int cpp = _mesa_get_format_bytes(rb->Format);
480 int stride = rb->Width * cpp;
481
482 if (rb->AllocStorage == swrast_alloc_front_storage) {
483 __DRIdrawable *dPriv = xrb->dPriv;
484 __DRIscreen *sPriv = dPriv->driScreenPriv;
485
486 xrb->map_mode = mode;
487 xrb->map_x = x;
488 xrb->map_y = rb->Height - y - h;
489 xrb->map_w = w;
490 xrb->map_h = h;
491
492 stride = w * cpp;
493 xrb->Base.Buffer = malloc(h * stride);
494
495 sPriv->swrast_loader->getImage(dPriv, x, xrb->map_y, w, h,
496 (char *) xrb->Base.Buffer,
497 dPriv->loaderPrivate);
498
499 *out_map = xrb->Base.Buffer + (h - 1) * stride;
500 *out_stride = -stride;
501 return;
502 }
503
504 assert(xrb->Base.Buffer);
505
506 if (rb->AllocStorage == swrast_alloc_back_storage) {
507 map += (rb->Height - 1) * stride;
508 stride = -stride;
509 }
510
511 map += (GLsizei)y * stride;
512 map += (GLsizei)x * cpp;
513
514 *out_map = map;
515 *out_stride = stride;
516 }
517
518 static void
519 swrast_unmap_renderbuffer(struct gl_context *ctx,
520 struct gl_renderbuffer *rb)
521 {
522 struct dri_swrast_renderbuffer *xrb = dri_swrast_renderbuffer(rb);
523
524 if (rb->AllocStorage == swrast_alloc_front_storage) {
525 __DRIdrawable *dPriv = xrb->dPriv;
526 __DRIscreen *sPriv = dPriv->driScreenPriv;
527
528 if (xrb->map_mode & GL_MAP_WRITE_BIT) {
529 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_DRAW,
530 xrb->map_x, xrb->map_y,
531 xrb->map_w, xrb->map_h,
532 (char *) xrb->Base.Buffer,
533 dPriv->loaderPrivate);
534 }
535
536 free(xrb->Base.Buffer);
537 xrb->Base.Buffer = NULL;
538 }
539 }
540
541 static GLboolean
542 dri_create_buffer(__DRIscreen * sPriv,
543 __DRIdrawable * dPriv,
544 const struct gl_config * visual, GLboolean isPixmap)
545 {
546 struct dri_drawable *drawable = NULL;
547 struct gl_framebuffer *fb;
548 struct dri_swrast_renderbuffer *frontrb, *backrb;
549
550 TRACE;
551
552 (void) sPriv;
553 (void) isPixmap;
554
555 drawable = CALLOC_STRUCT(dri_drawable);
556 if (drawable == NULL)
557 goto drawable_fail;
558
559 dPriv->driverPrivate = drawable;
560 drawable->dPriv = dPriv;
561
562 drawable->row = malloc(SWRAST_MAX_WIDTH * 4);
563 if (drawable->row == NULL)
564 goto drawable_fail;
565
566 fb = &drawable->Base;
567
568 /* basic framebuffer setup */
569 _mesa_initialize_window_framebuffer(fb, visual);
570
571 /* add front renderbuffer */
572 frontrb = swrast_new_renderbuffer(visual, dPriv, GL_TRUE);
573 _mesa_attach_and_own_rb(fb, BUFFER_FRONT_LEFT, &frontrb->Base.Base);
574
575 /* add back renderbuffer */
576 if (visual->doubleBufferMode) {
577 backrb = swrast_new_renderbuffer(visual, dPriv, GL_FALSE);
578 _mesa_attach_and_own_rb(fb, BUFFER_BACK_LEFT, &backrb->Base.Base);
579 }
580
581 /* add software renderbuffers */
582 _swrast_add_soft_renderbuffers(fb,
583 GL_FALSE, /* color */
584 visual->haveDepthBuffer,
585 visual->haveStencilBuffer,
586 visual->haveAccumBuffer,
587 GL_FALSE, /* alpha */
588 GL_FALSE /* aux bufs */);
589
590 return GL_TRUE;
591
592 drawable_fail:
593
594 if (drawable)
595 free(drawable->row);
596
597 free(drawable);
598
599 return GL_FALSE;
600 }
601
602 static void
603 dri_destroy_buffer(__DRIdrawable * dPriv)
604 {
605 TRACE;
606
607 if (dPriv) {
608 struct dri_drawable *drawable = dri_drawable(dPriv);
609 struct gl_framebuffer *fb;
610
611 free(drawable->row);
612
613 fb = &drawable->Base;
614
615 fb->DeletePending = GL_TRUE;
616 _mesa_reference_framebuffer(&fb, NULL);
617 }
618 }
619
620 static void
621 dri_swap_buffers(__DRIdrawable * dPriv)
622 {
623 __DRIscreen *sPriv = dPriv->driScreenPriv;
624
625 GET_CURRENT_CONTEXT(ctx);
626
627 struct dri_drawable *drawable = dri_drawable(dPriv);
628 struct gl_framebuffer *fb;
629 struct dri_swrast_renderbuffer *frontrb, *backrb;
630
631 TRACE;
632
633 fb = &drawable->Base;
634
635 frontrb =
636 dri_swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
637 backrb =
638 dri_swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
639
640 /* check for signle-buffered */
641 if (backrb == NULL)
642 return;
643
644 /* check if swapping currently bound buffer */
645 if (ctx && ctx->DrawBuffer == fb) {
646 /* flush pending rendering */
647 _mesa_notifySwapBuffers(ctx);
648 }
649
650 sPriv->swrast_loader->putImage(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
651 0, 0,
652 frontrb->Base.Base.Width,
653 frontrb->Base.Base.Height,
654 (char *) backrb->Base.Buffer,
655 dPriv->loaderPrivate);
656 }
657
658
659 /**
660 * General device driver functions.
661 */
662
663 static void
664 get_window_size( struct gl_framebuffer *fb, GLsizei *w, GLsizei *h )
665 {
666 __DRIdrawable *dPriv = swrast_drawable(fb)->dPriv;
667 __DRIscreen *sPriv = dPriv->driScreenPriv;
668 int x, y;
669
670 sPriv->swrast_loader->getDrawableInfo(dPriv,
671 &x, &y, w, h,
672 dPriv->loaderPrivate);
673 }
674
675 static void
676 swrast_check_and_update_window_size( struct gl_context *ctx, struct gl_framebuffer *fb )
677 {
678 GLsizei width, height;
679
680 get_window_size(fb, &width, &height);
681 if (fb->Width != width || fb->Height != height) {
682 _mesa_resize_framebuffer(ctx, fb, width, height);
683 }
684 }
685
686 static const GLubyte *
687 get_string(struct gl_context *ctx, GLenum pname)
688 {
689 (void) ctx;
690 switch (pname) {
691 case GL_VENDOR:
692 return (const GLubyte *) swrast_vendor_string;
693 case GL_RENDERER:
694 return (const GLubyte *) swrast_renderer_string;
695 default:
696 return NULL;
697 }
698 }
699
700 static void
701 update_state(struct gl_context *ctx)
702 {
703 GLuint new_state = ctx->NewState;
704
705 if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
706 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
707
708 /* not much to do here - pass it on */
709 _swrast_InvalidateState( ctx, new_state );
710 _swsetup_InvalidateState( ctx, new_state );
711 _tnl_InvalidateState( ctx, new_state );
712 }
713
714 static void
715 viewport(struct gl_context *ctx)
716 {
717 struct gl_framebuffer *draw = ctx->WinSysDrawBuffer;
718 struct gl_framebuffer *read = ctx->WinSysReadBuffer;
719
720 swrast_check_and_update_window_size(ctx, draw);
721 swrast_check_and_update_window_size(ctx, read);
722 }
723
724 static mesa_format swrastChooseTextureFormat(struct gl_context * ctx,
725 GLenum target,
726 GLint internalFormat,
727 GLenum format,
728 GLenum type)
729 {
730 if (internalFormat == GL_RGB)
731 return MESA_FORMAT_B8G8R8X8_UNORM;
732 return _mesa_choose_tex_format(ctx, target, internalFormat, format, type);
733 }
734
735 static void
736 swrast_init_driver_functions(struct dd_function_table *driver)
737 {
738 driver->GetString = get_string;
739 driver->UpdateState = update_state;
740 driver->Viewport = viewport;
741 driver->ChooseTextureFormat = swrastChooseTextureFormat;
742 driver->MapRenderbuffer = swrast_map_renderbuffer;
743 driver->UnmapRenderbuffer = swrast_unmap_renderbuffer;
744 }
745
746 /**
747 * Context-related functions.
748 */
749
750 static GLboolean
751 dri_create_context(gl_api api,
752 const struct gl_config * visual,
753 __DRIcontext * cPriv,
754 unsigned major_version,
755 unsigned minor_version,
756 uint32_t flags,
757 bool notify_reset,
758 unsigned *error,
759 void *sharedContextPrivate)
760 {
761 struct dri_context *ctx = NULL;
762 struct dri_context *share = (struct dri_context *)sharedContextPrivate;
763 struct gl_context *mesaCtx = NULL;
764 struct gl_context *sharedCtx = NULL;
765 struct dd_function_table functions;
766
767 TRACE;
768
769 /* Flag filtering is handled in dri2CreateContextAttribs.
770 */
771 (void) flags;
772
773 ctx = CALLOC_STRUCT(dri_context);
774 if (ctx == NULL) {
775 *error = __DRI_CTX_ERROR_NO_MEMORY;
776 goto context_fail;
777 }
778
779 cPriv->driverPrivate = ctx;
780 ctx->cPriv = cPriv;
781
782 /* build table of device driver functions */
783 _mesa_init_driver_functions(&functions);
784 swrast_init_driver_functions(&functions);
785
786 if (share) {
787 sharedCtx = &share->Base;
788 }
789
790 mesaCtx = &ctx->Base;
791
792 /* basic context setup */
793 if (!_mesa_initialize_context(mesaCtx, api, visual, sharedCtx, &functions)) {
794 *error = __DRI_CTX_ERROR_NO_MEMORY;
795 goto context_fail;
796 }
797
798 driContextSetFlags(mesaCtx, flags);
799
800 /* create module contexts */
801 _swrast_CreateContext( mesaCtx );
802 _vbo_CreateContext( mesaCtx );
803 _tnl_CreateContext( mesaCtx );
804 _swsetup_CreateContext( mesaCtx );
805 _swsetup_Wakeup( mesaCtx );
806
807 /* use default TCL pipeline */
808 {
809 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
810 tnl->Driver.RunPipeline = _tnl_run_pipeline;
811 }
812
813 _mesa_meta_init(mesaCtx);
814 _mesa_enable_sw_extensions(mesaCtx);
815
816 _mesa_compute_version(mesaCtx);
817
818 _mesa_initialize_dispatch_tables(mesaCtx);
819 _mesa_initialize_vbo_vtxfmt(mesaCtx);
820
821 *error = __DRI_CTX_ERROR_SUCCESS;
822 return GL_TRUE;
823
824 context_fail:
825
826 free(ctx);
827
828 return GL_FALSE;
829 }
830
831 static void
832 dri_destroy_context(__DRIcontext * cPriv)
833 {
834 TRACE;
835
836 if (cPriv) {
837 struct dri_context *ctx = dri_context(cPriv);
838 struct gl_context *mesaCtx;
839
840 mesaCtx = &ctx->Base;
841
842 _mesa_meta_free(mesaCtx);
843 _swsetup_DestroyContext( mesaCtx );
844 _swrast_DestroyContext( mesaCtx );
845 _tnl_DestroyContext( mesaCtx );
846 _vbo_DestroyContext( mesaCtx );
847 _mesa_destroy_context( mesaCtx );
848 }
849 }
850
851 static GLboolean
852 dri_make_current(__DRIcontext * cPriv,
853 __DRIdrawable * driDrawPriv,
854 __DRIdrawable * driReadPriv)
855 {
856 struct gl_context *mesaCtx;
857 struct gl_framebuffer *mesaDraw;
858 struct gl_framebuffer *mesaRead;
859 TRACE;
860
861 if (cPriv) {
862 struct dri_context *ctx = dri_context(cPriv);
863 struct dri_drawable *draw;
864 struct dri_drawable *read;
865
866 if (!driDrawPriv || !driReadPriv)
867 return GL_FALSE;
868
869 draw = dri_drawable(driDrawPriv);
870 read = dri_drawable(driReadPriv);
871 mesaCtx = &ctx->Base;
872 mesaDraw = &draw->Base;
873 mesaRead = &read->Base;
874
875 /* check for same context and buffer */
876 if (mesaCtx == _mesa_get_current_context()
877 && mesaCtx->DrawBuffer == mesaDraw
878 && mesaCtx->ReadBuffer == mesaRead) {
879 return GL_TRUE;
880 }
881
882 _glapi_check_multithread();
883
884 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
885 if (mesaRead != mesaDraw)
886 swrast_check_and_update_window_size(mesaCtx, mesaRead);
887
888 _mesa_make_current( mesaCtx,
889 mesaDraw,
890 mesaRead );
891 }
892 else {
893 /* unbind */
894 _mesa_make_current( NULL, NULL, NULL );
895 }
896
897 return GL_TRUE;
898 }
899
900 static GLboolean
901 dri_unbind_context(__DRIcontext * cPriv)
902 {
903 TRACE;
904 (void) cPriv;
905
906 /* Unset current context and dispath table */
907 _mesa_make_current(NULL, NULL, NULL);
908
909 return GL_TRUE;
910 }
911
912 static void
913 dri_copy_sub_buffer(__DRIdrawable *dPriv, int x, int y,
914 int w, int h)
915 {
916 __DRIscreen *sPriv = dPriv->driScreenPriv;
917 void *data;
918 int iy;
919 struct dri_drawable *drawable = dri_drawable(dPriv);
920 struct gl_framebuffer *fb;
921 struct dri_swrast_renderbuffer *frontrb, *backrb;
922
923 TRACE;
924
925 fb = &drawable->Base;
926
927 frontrb =
928 dri_swrast_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
929 backrb =
930 dri_swrast_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
931
932 /* check for signle-buffered */
933 if (backrb == NULL)
934 return;
935
936 iy = frontrb->Base.Base.Height - y - h;
937 data = (char *)backrb->Base.Buffer + (iy * backrb->pitch) + (x * ((backrb->bpp + 7) / 8));
938 sPriv->swrast_loader->putImage2(dPriv, __DRI_SWRAST_IMAGE_OP_SWAP,
939 x, iy, w, h,
940 frontrb->pitch,
941 data,
942 dPriv->loaderPrivate);
943 }
944
945
946 static const struct __DriverAPIRec swrast_driver_api = {
947 .InitScreen = dri_init_screen,
948 .DestroyScreen = dri_destroy_screen,
949 .CreateContext = dri_create_context,
950 .DestroyContext = dri_destroy_context,
951 .CreateBuffer = dri_create_buffer,
952 .DestroyBuffer = dri_destroy_buffer,
953 .SwapBuffers = dri_swap_buffers,
954 .MakeCurrent = dri_make_current,
955 .UnbindContext = dri_unbind_context,
956 .CopySubBuffer = dri_copy_sub_buffer,
957 };
958
959 static const struct __DRIDriverVtableExtensionRec swrast_vtable = {
960 .base = { __DRI_DRIVER_VTABLE, 1 },
961 .vtable = &swrast_driver_api,
962 };
963
964 static const __DRIextension *swrast_driver_extensions[] = {
965 &driCoreExtension.base,
966 &driSWRastExtension.base,
967 &driCopySubBufferExtension.base,
968 &dri2ConfigQueryExtension.base,
969 &swrast_vtable.base,
970 NULL
971 };
972
973 PUBLIC const __DRIextension **__driDriverGetExtensions_swrast(void)
974 {
975 globalDriverAPI = &swrast_driver_api;
976
977 return swrast_driver_extensions;
978 }