Destroy the GL context after driDestroyTextureHeap, because the driver's
[mesa.git] / src / mesa / drivers / dri / r200 / r200_context.c
1 /* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_context.c,v 1.3 2003/05/06 23:52:08 daenzer Exp $ */
2 /*
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4
5 The Weather Channel (TM) funded Tungsten Graphics to develop the
6 initial release of the Radeon 8500 driver under the XFree86 license.
7 This notice must be preserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial
19 portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
25 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 /*
32 * Authors:
33 * Keith Whitwell <keith@tungstengraphics.com>
34 */
35
36 #include "glheader.h"
37 #include "api_arrayelt.h"
38 #include "context.h"
39 #include "simple_list.h"
40 #include "imports.h"
41 #include "matrix.h"
42 #include "extensions.h"
43 #include "framebuffer.h"
44 #include "state.h"
45
46 #include "swrast/swrast.h"
47 #include "swrast_setup/swrast_setup.h"
48 #include "array_cache/acache.h"
49
50 #include "tnl/tnl.h"
51 #include "tnl/t_pipeline.h"
52
53 #include "drivers/common/driverfuncs.h"
54
55 #include "r200_context.h"
56 #include "r200_ioctl.h"
57 #include "r200_state.h"
58 #include "r200_span.h"
59 #include "r200_pixel.h"
60 #include "r200_tex.h"
61 #include "r200_swtcl.h"
62 #include "r200_tcl.h"
63 #include "r200_vtxfmt.h"
64 #include "r200_maos.h"
65
66 #define need_GL_ARB_multisample
67 #define need_GL_ARB_texture_compression
68 #define need_GL_ARB_vertex_buffer_object
69 #define need_GL_ARB_vertex_program
70 #define need_GL_ATI_fragment_shader
71 #define need_GL_EXT_blend_minmax
72 #define need_GL_EXT_fog_coord
73 #define need_GL_EXT_secondary_color
74 #define need_GL_EXT_blend_equation_separate
75 #define need_GL_EXT_blend_func_separate
76 #define need_GL_NV_vertex_program
77 #include "extension_helper.h"
78
79 #define DRIVER_DATE "20060327"
80
81 #include "vblank.h"
82 #include "utils.h"
83 #include "xmlpool.h" /* for symbolic values of enum-type options */
84 #ifndef R200_DEBUG
85 int R200_DEBUG = (0);
86 #endif
87
88
89 /* Return the width and height of the given buffer.
90 */
91 static void r200GetBufferSize( GLframebuffer *buffer,
92 GLuint *width, GLuint *height )
93 {
94 GET_CURRENT_CONTEXT(ctx);
95 r200ContextPtr rmesa = R200_CONTEXT(ctx);
96
97 LOCK_HARDWARE( rmesa );
98 *width = rmesa->dri.drawable->w;
99 *height = rmesa->dri.drawable->h;
100 UNLOCK_HARDWARE( rmesa );
101 }
102
103 /* Return various strings for glGetString().
104 */
105 static const GLubyte *r200GetString( GLcontext *ctx, GLenum name )
106 {
107 r200ContextPtr rmesa = R200_CONTEXT(ctx);
108 static char buffer[128];
109 unsigned offset;
110 GLuint agp_mode = (rmesa->r200Screen->card_type == RADEON_CARD_PCI)? 0 :
111 rmesa->r200Screen->AGPMode;
112
113 switch ( name ) {
114 case GL_VENDOR:
115 return (GLubyte *)"Tungsten Graphics, Inc.";
116
117 case GL_RENDERER:
118 offset = driGetRendererString( buffer, "R200", DRIVER_DATE,
119 agp_mode );
120
121 sprintf( & buffer[ offset ], " %sTCL",
122 !(rmesa->TclFallback & R200_TCL_FALLBACK_TCL_DISABLE)
123 ? "" : "NO-" );
124
125 return (GLubyte *)buffer;
126
127 default:
128 return NULL;
129 }
130 }
131
132
133 /* Extension strings exported by the R200 driver.
134 */
135 const struct dri_extension card_extensions[] =
136 {
137 { "GL_ARB_multisample", GL_ARB_multisample_functions },
138 { "GL_ARB_multitexture", NULL },
139 { "GL_ARB_texture_border_clamp", NULL },
140 { "GL_ARB_texture_compression", GL_ARB_texture_compression_functions },
141 { "GL_ARB_texture_env_add", NULL },
142 { "GL_ARB_texture_env_combine", NULL },
143 { "GL_ARB_texture_env_dot3", NULL },
144 { "GL_ARB_texture_env_crossbar", NULL },
145 { "GL_ARB_texture_mirrored_repeat", NULL },
146 { "GL_ARB_vertex_buffer_object", GL_ARB_vertex_buffer_object_functions },
147 { "GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions },
148 { "GL_EXT_blend_subtract", NULL },
149 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
150 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
151 { "GL_EXT_stencil_wrap", NULL },
152 { "GL_EXT_texture_edge_clamp", NULL },
153 { "GL_EXT_texture_env_combine", NULL },
154 { "GL_EXT_texture_env_dot3", NULL },
155 { "GL_EXT_texture_filter_anisotropic", NULL },
156 { "GL_EXT_texture_lod_bias", NULL },
157 { "GL_EXT_texture_mirror_clamp", NULL },
158 { "GL_EXT_texture_rectangle", NULL },
159 { "GL_ATI_texture_env_combine3", NULL },
160 { "GL_ATI_texture_mirror_once", NULL },
161 { "GL_MESA_pack_invert", NULL },
162 { "GL_NV_blend_square", NULL },
163 { "GL_SGIS_generate_mipmap", NULL },
164 { NULL, NULL }
165 };
166
167 const struct dri_extension blend_extensions[] = {
168 { "GL_EXT_blend_equation_separate", GL_EXT_blend_equation_separate_functions },
169 { "GL_EXT_blend_func_separate", GL_EXT_blend_func_separate_functions },
170 { NULL, NULL }
171 };
172
173 const struct dri_extension ARB_vp_extension[] = {
174 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions }
175 };
176
177 const struct dri_extension NV_vp_extension[] = {
178 { "GL_NV_vertex_program", GL_NV_vertex_program_functions }
179 };
180
181 const struct dri_extension ATI_fs_extension[] = {
182 { "GL_ATI_fragment_shader", GL_ATI_fragment_shader_functions }
183 };
184
185 extern const struct tnl_pipeline_stage _r200_render_stage;
186 extern const struct tnl_pipeline_stage _r200_tcl_stage;
187
188 static const struct tnl_pipeline_stage *r200_pipeline[] = {
189
190 /* Try and go straight to t&l
191 */
192 &_r200_tcl_stage,
193
194 /* Catch any t&l fallbacks
195 */
196 &_tnl_vertex_transform_stage,
197 &_tnl_normal_transform_stage,
198 &_tnl_lighting_stage,
199 &_tnl_fog_coordinate_stage,
200 &_tnl_texgen_stage,
201 &_tnl_texture_transform_stage,
202 &_tnl_arb_vertex_program_stage,
203 &_tnl_vertex_program_stage,
204
205 /* Try again to go to tcl?
206 * - no good for asymmetric-twoside (do with multipass)
207 * - no good for asymmetric-unfilled (do with multipass)
208 * - good for material
209 * - good for texgen
210 * - need to manipulate a bit of state
211 *
212 * - worth it/not worth it?
213 */
214
215 /* Else do them here.
216 */
217 /* &_r200_render_stage, */ /* FIXME: bugs with ut2003 */
218 &_tnl_render_stage, /* FALLBACK: */
219 NULL,
220 };
221
222
223
224 /* Initialize the driver's misc functions.
225 */
226 static void r200InitDriverFuncs( struct dd_function_table *functions )
227 {
228 functions->GetBufferSize = r200GetBufferSize;
229 functions->ResizeBuffers = _mesa_resize_framebuffer;
230 functions->GetString = r200GetString;
231
232 functions->Error = NULL;
233 functions->DrawPixels = NULL;
234 functions->Bitmap = NULL;
235 }
236
237 static const struct dri_debug_control debug_control[] =
238 {
239 { "fall", DEBUG_FALLBACKS },
240 { "tex", DEBUG_TEXTURE },
241 { "ioctl", DEBUG_IOCTL },
242 { "prim", DEBUG_PRIMS },
243 { "vert", DEBUG_VERTS },
244 { "state", DEBUG_STATE },
245 { "code", DEBUG_CODEGEN },
246 { "vfmt", DEBUG_VFMT },
247 { "vtxf", DEBUG_VFMT },
248 { "verb", DEBUG_VERBOSE },
249 { "dri", DEBUG_DRI },
250 { "dma", DEBUG_DMA },
251 { "san", DEBUG_SANITY },
252 { "sync", DEBUG_SYNC },
253 { "pix", DEBUG_PIXEL },
254 { "mem", DEBUG_MEMORY },
255 { NULL, 0 }
256 };
257
258
259 /* Create the device specific rendering context.
260 */
261 GLboolean r200CreateContext( const __GLcontextModes *glVisual,
262 __DRIcontextPrivate *driContextPriv,
263 void *sharedContextPrivate)
264 {
265 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
266 radeonScreenPtr screen = (radeonScreenPtr)(sPriv->private);
267 struct dd_function_table functions;
268 r200ContextPtr rmesa;
269 GLcontext *ctx, *shareCtx;
270 int i;
271 int tcl_mode, fthrottle_mode;
272
273 assert(glVisual);
274 assert(driContextPriv);
275 assert(screen);
276
277 /* Allocate the R200 context */
278 rmesa = (r200ContextPtr) CALLOC( sizeof(*rmesa) );
279 if ( !rmesa )
280 return GL_FALSE;
281
282 /* init exp fog table data */
283 r200InitStaticFogData();
284
285 /* Parse configuration files.
286 * Do this here so that initialMaxAnisotropy is set before we create
287 * the default textures.
288 */
289 driParseConfigFiles (&rmesa->optionCache, &screen->optionCache,
290 screen->driScreen->myNum, "r200");
291 rmesa->initialMaxAnisotropy = driQueryOptionf(&rmesa->optionCache,
292 "def_max_anisotropy");
293
294 if ( driQueryOptionb( &rmesa->optionCache, "hyperz" ) ) {
295 if ( sPriv->drmMinor < 13 )
296 fprintf( stderr, "DRM version 1.%d too old to support HyperZ, "
297 "disabling.\n",sPriv->drmMinor );
298 else
299 rmesa->using_hyperz = GL_TRUE;
300 }
301
302 if ( sPriv->drmMinor >= 15 )
303 rmesa->texmicrotile = GL_TRUE;
304
305 /* Init default driver functions then plug in our R200-specific functions
306 * (the texture functions are especially important)
307 */
308 _mesa_init_driver_functions(&functions);
309 r200InitDriverFuncs(&functions);
310 r200InitIoctlFuncs(&functions);
311 r200InitStateFuncs(&functions);
312 r200InitTextureFuncs(&functions);
313
314 /* Allocate and initialize the Mesa context */
315 if (sharedContextPrivate)
316 shareCtx = ((r200ContextPtr) sharedContextPrivate)->glCtx;
317 else
318 shareCtx = NULL;
319 rmesa->glCtx = _mesa_create_context(glVisual, shareCtx,
320 &functions, (void *) rmesa);
321 if (!rmesa->glCtx) {
322 FREE(rmesa);
323 return GL_FALSE;
324 }
325 driContextPriv->driverPrivate = rmesa;
326
327 /* Init r200 context data */
328 rmesa->dri.context = driContextPriv;
329 rmesa->dri.screen = sPriv;
330 rmesa->dri.drawable = NULL; /* Set by XMesaMakeCurrent */
331 rmesa->dri.hwContext = driContextPriv->hHWContext;
332 rmesa->dri.hwLock = &sPriv->pSAREA->lock;
333 rmesa->dri.fd = sPriv->fd;
334 rmesa->dri.drmMinor = sPriv->drmMinor;
335
336 rmesa->r200Screen = screen;
337 rmesa->sarea = (drm_radeon_sarea_t *)((GLubyte *)sPriv->pSAREA +
338 screen->sarea_priv_offset);
339
340
341 rmesa->dma.buf0_address = rmesa->r200Screen->buffers->list[0].address;
342
343 (void) memset( rmesa->texture_heaps, 0, sizeof( rmesa->texture_heaps ) );
344 make_empty_list( & rmesa->swapped );
345
346 rmesa->nr_heaps = 1 /* screen->numTexHeaps */ ;
347 assert(rmesa->nr_heaps < RADEON_NR_TEX_HEAPS);
348 for ( i = 0 ; i < rmesa->nr_heaps ; i++ ) {
349 rmesa->texture_heaps[i] = driCreateTextureHeap( i, rmesa,
350 screen->texSize[i],
351 12,
352 RADEON_NR_TEX_REGIONS,
353 (drmTextureRegionPtr)rmesa->sarea->tex_list[i],
354 & rmesa->sarea->tex_age[i],
355 & rmesa->swapped,
356 sizeof( r200TexObj ),
357 (destroy_texture_object_t *) r200DestroyTexObj );
358 }
359 rmesa->texture_depth = driQueryOptioni (&rmesa->optionCache,
360 "texture_depth");
361 if (rmesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
362 rmesa->texture_depth = ( screen->cpp == 4 ) ?
363 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
364
365 rmesa->swtcl.RenderIndex = ~0;
366 rmesa->hw.all_dirty = 1;
367
368 /* Set the maximum texture size small enough that we can guarentee that
369 * all texture units can bind a maximal texture and have all of them in
370 * texturable memory at once. Depending on the allow_large_textures driconf
371 * setting allow larger textures.
372 */
373
374 ctx = rmesa->glCtx;
375 ctx->Const.MaxTextureUnits = driQueryOptioni (&rmesa->optionCache,
376 "texture_units");
377 ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
378 ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
379
380 i = driQueryOptioni( &rmesa->optionCache, "allow_large_textures");
381
382 driCalculateMaxTextureLevels( rmesa->texture_heaps,
383 rmesa->nr_heaps,
384 & ctx->Const,
385 4,
386 11, /* max 2D texture size is 2048x2048 */
387 #if ENABLE_HW_3D_TEXTURE
388 8, /* max 3D texture size is 256^3 */
389 #else
390 0, /* 3D textures unsupported */
391 #endif
392 11, /* max cube texture size is 2048x2048 */
393 11, /* max texture rectangle size is 2048x2048 */
394 12,
395 GL_FALSE,
396 i );
397
398 ctx->Const.MaxTextureMaxAnisotropy = 16.0;
399
400 /* No wide AA points.
401 */
402 ctx->Const.MinPointSize = 1.0;
403 ctx->Const.MinPointSizeAA = 1.0;
404 ctx->Const.MaxPointSizeAA = 1.0;
405 ctx->Const.PointSizeGranularity = 0.0625;
406 if (rmesa->r200Screen->drmSupportsPointSprites)
407 ctx->Const.MaxPointSize = 2047.0;
408 else
409 ctx->Const.MaxPointSize = 1.0;
410
411 /* mesa initialization problem - _mesa_init_point was already called */
412 ctx->Point.MaxSize = ctx->Const.MaxPointSize;
413
414 ctx->Const.MinLineWidth = 1.0;
415 ctx->Const.MinLineWidthAA = 1.0;
416 ctx->Const.MaxLineWidth = 10.0;
417 ctx->Const.MaxLineWidthAA = 10.0;
418 ctx->Const.LineWidthGranularity = 0.0625;
419
420 /* Initialize the software rasterizer and helper modules.
421 */
422 _swrast_CreateContext( ctx );
423 _ac_CreateContext( ctx );
424 _tnl_CreateContext( ctx );
425 _swsetup_CreateContext( ctx );
426 _ae_create_context( ctx );
427
428 /* Install the customized pipeline:
429 */
430 _tnl_destroy_pipeline( ctx );
431 _tnl_install_pipeline( ctx, r200_pipeline );
432 ctx->Driver.FlushVertices = r200FlushVertices;
433
434 /* Try and keep materials and vertices separate:
435 */
436 _tnl_isolate_materials( ctx, GL_TRUE );
437
438
439 /* Configure swrast and TNL to match hardware characteristics:
440 */
441 _swrast_allow_pixel_fog( ctx, GL_FALSE );
442 _swrast_allow_vertex_fog( ctx, GL_TRUE );
443 _tnl_allow_pixel_fog( ctx, GL_FALSE );
444 _tnl_allow_vertex_fog( ctx, GL_TRUE );
445
446
447 for ( i = 0 ; i < R200_MAX_TEXTURE_UNITS ; i++ ) {
448 _math_matrix_ctr( &rmesa->TexGenMatrix[i] );
449 _math_matrix_set_identity( &rmesa->TexGenMatrix[i] );
450 }
451 _math_matrix_ctr( &rmesa->tmpmat );
452 _math_matrix_set_identity( &rmesa->tmpmat );
453
454 driInitExtensions( ctx, card_extensions, GL_TRUE );
455 if (!(rmesa->r200Screen->chip_flags & R200_CHIPSET_YCBCR_BROKEN)) {
456 /* yuv textures don't work with some chips - R200 / rv280 okay so far
457 others get the bit ordering right but don't actually do YUV-RGB conversion */
458 _mesa_enable_extension( ctx, "GL_MESA_ycbcr_texture" );
459 }
460 if (rmesa->glCtx->Mesa_DXTn) {
461 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
462 _mesa_enable_extension( ctx, "GL_S3_s3tc" );
463 }
464 else if (driQueryOptionb (&rmesa->optionCache, "force_s3tc_enable")) {
465 _mesa_enable_extension( ctx, "GL_EXT_texture_compression_s3tc" );
466 }
467
468 if (rmesa->r200Screen->drmSupportsCubeMapsR200)
469 _mesa_enable_extension( ctx, "GL_ARB_texture_cube_map" );
470 if (rmesa->r200Screen->drmSupportsBlendColor) {
471 driInitExtensions( ctx, blend_extensions, GL_FALSE );
472 }
473 if(driQueryOptionb(&rmesa->optionCache, "arb_vertex_program"))
474 driInitSingleExtension( ctx, ARB_vp_extension );
475 if(driQueryOptionb(&rmesa->optionCache, "nv_vertex_program"))
476 driInitSingleExtension( ctx, NV_vp_extension );
477
478 if ((ctx->Const.MaxTextureUnits == 6) && rmesa->r200Screen->drmSupportsFragShader)
479 driInitSingleExtension( ctx, ATI_fs_extension );
480 #if 0
481 r200InitDriverFuncs( ctx );
482 r200InitIoctlFuncs( ctx );
483 r200InitStateFuncs( ctx );
484 r200InitTextureFuncs( ctx );
485 #endif
486 /* plug in a few more device driver functions */
487 /* XXX these should really go right after _mesa_init_driver_functions() */
488 r200InitPixelFuncs( ctx );
489 r200InitSpanFuncs( ctx );
490 r200InitTnlFuncs( ctx );
491 r200InitState( rmesa );
492 r200InitSwtcl( ctx );
493
494 fthrottle_mode = driQueryOptioni(&rmesa->optionCache, "fthrottle_mode");
495 rmesa->iw.irq_seq = -1;
496 rmesa->irqsEmitted = 0;
497 rmesa->do_irqs = (rmesa->dri.drmMinor >= 6 &&
498 fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS &&
499 rmesa->r200Screen->irq);
500
501 rmesa->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
502
503 if (!rmesa->do_irqs)
504 fprintf(stderr,
505 "IRQ's not enabled, falling back to %s: %d %d %d\n",
506 rmesa->do_usleeps ? "usleeps" : "busy waits",
507 rmesa->dri.drmMinor,
508 fthrottle_mode,
509 rmesa->r200Screen->irq);
510
511 rmesa->vblank_flags = (rmesa->r200Screen->irq != 0)
512 ? driGetDefaultVBlankFlags(&rmesa->optionCache) : VBLANK_FLAG_NO_IRQ;
513
514 rmesa->prefer_gart_client_texturing =
515 (getenv("R200_GART_CLIENT_TEXTURES") != 0);
516
517 (*dri_interface->getUST)( & rmesa->swap_ust );
518
519
520 #if DO_DEBUG
521 R200_DEBUG = driParseDebugString( getenv( "R200_DEBUG" ),
522 debug_control );
523 R200_DEBUG |= driParseDebugString( getenv( "RADEON_DEBUG" ),
524 debug_control );
525 #endif
526
527 tcl_mode = driQueryOptioni(&rmesa->optionCache, "tcl_mode");
528 if (driQueryOptionb(&rmesa->optionCache, "no_rast")) {
529 fprintf(stderr, "disabling 3D acceleration\n");
530 FALLBACK(rmesa, R200_FALLBACK_DISABLE, 1);
531 }
532 else if (tcl_mode == DRI_CONF_TCL_SW || getenv("R200_NO_TCL") ||
533 !(rmesa->r200Screen->chip_flags & RADEON_CHIPSET_TCL)) {
534 if (rmesa->r200Screen->chip_flags & RADEON_CHIPSET_TCL) {
535 rmesa->r200Screen->chip_flags &= ~RADEON_CHIPSET_TCL;
536 fprintf(stderr, "Disabling HW TCL support\n");
537 }
538 TCL_FALLBACK(rmesa->glCtx, R200_TCL_FALLBACK_TCL_DISABLE, 1);
539 }
540
541 if (rmesa->r200Screen->chip_flags & RADEON_CHIPSET_TCL) {
542 if (tcl_mode >= DRI_CONF_TCL_VTXFMT)
543 r200VtxfmtInit( ctx, tcl_mode >= DRI_CONF_TCL_CODEGEN );
544
545 _tnl_need_dlist_norm_lengths( ctx, GL_FALSE );
546 }
547 return GL_TRUE;
548 }
549
550
551 /* Destroy the device specific context.
552 */
553 /* Destroy the Mesa and driver specific context data.
554 */
555 void r200DestroyContext( __DRIcontextPrivate *driContextPriv )
556 {
557 GET_CURRENT_CONTEXT(ctx);
558 r200ContextPtr rmesa = (r200ContextPtr) driContextPriv->driverPrivate;
559 r200ContextPtr current = ctx ? R200_CONTEXT(ctx) : NULL;
560
561 /* check if we're deleting the currently bound context */
562 if (rmesa == current) {
563 R200_FIREVERTICES( rmesa );
564 _mesa_make_current(NULL, NULL, NULL);
565 }
566
567 /* Free r200 context resources */
568 assert(rmesa); /* should never be null */
569 if ( rmesa ) {
570 GLboolean release_texture_heaps;
571
572
573 release_texture_heaps = (rmesa->glCtx->Shared->RefCount == 1);
574 _swsetup_DestroyContext( rmesa->glCtx );
575 _tnl_DestroyContext( rmesa->glCtx );
576 _ac_DestroyContext( rmesa->glCtx );
577 _swrast_DestroyContext( rmesa->glCtx );
578
579 r200DestroySwtcl( rmesa->glCtx );
580 r200ReleaseArrays( rmesa->glCtx, ~0 );
581
582 if (rmesa->dma.current.buf) {
583 r200ReleaseDmaRegion( rmesa, &rmesa->dma.current, __FUNCTION__ );
584 r200FlushCmdBuf( rmesa, __FUNCTION__ );
585 }
586
587 if (!(rmesa->TclFallback & R200_TCL_FALLBACK_TCL_DISABLE)) {
588 int tcl_mode = driQueryOptioni(&rmesa->optionCache, "tcl_mode");
589 if (tcl_mode >= DRI_CONF_TCL_VTXFMT)
590 r200VtxfmtDestroy( rmesa->glCtx );
591 }
592
593 if (rmesa->state.scissor.pClipRects) {
594 FREE(rmesa->state.scissor.pClipRects);
595 rmesa->state.scissor.pClipRects = NULL;
596 }
597
598 if ( release_texture_heaps ) {
599 /* This share group is about to go away, free our private
600 * texture object data.
601 */
602 int i;
603
604 for ( i = 0 ; i < rmesa->nr_heaps ; i++ ) {
605 driDestroyTextureHeap( rmesa->texture_heaps[ i ] );
606 rmesa->texture_heaps[ i ] = NULL;
607 }
608
609 assert( is_empty_list( & rmesa->swapped ) );
610 }
611
612 /* free the Mesa context */
613 rmesa->glCtx->DriverCtx = NULL;
614 _mesa_destroy_context( rmesa->glCtx );
615
616 /* free the option cache */
617 driDestroyOptionCache (&rmesa->optionCache);
618
619 FREE( rmesa );
620 }
621 }
622
623
624
625
626 void
627 r200SwapBuffers( __DRIdrawablePrivate *dPriv )
628 {
629 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
630 r200ContextPtr rmesa;
631 GLcontext *ctx;
632 rmesa = (r200ContextPtr) dPriv->driContextPriv->driverPrivate;
633 ctx = rmesa->glCtx;
634 if (ctx->Visual.doubleBufferMode) {
635 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
636 if ( rmesa->doPageFlip ) {
637 r200PageFlip( dPriv );
638 }
639 else {
640 r200CopyBuffer( dPriv, NULL );
641 }
642 }
643 }
644 else {
645 /* XXX this shouldn't be an error but we can't handle it for now */
646 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
647 }
648 }
649
650 void
651 r200CopySubBuffer( __DRIdrawablePrivate *dPriv,
652 int x, int y, int w, int h )
653 {
654 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
655 r200ContextPtr rmesa;
656 GLcontext *ctx;
657 rmesa = (r200ContextPtr) dPriv->driContextPriv->driverPrivate;
658 ctx = rmesa->glCtx;
659 if (ctx->Visual.doubleBufferMode) {
660 drm_clip_rect_t rect;
661 rect.x1 = x + dPriv->x;
662 rect.y1 = (dPriv->h - y - h) + dPriv->y;
663 rect.x2 = rect.x1 + w;
664 rect.y2 = rect.y1 + h;
665 _mesa_notifySwapBuffers( ctx ); /* flush pending rendering comands */
666 r200CopyBuffer( dPriv, &rect );
667 }
668 }
669 else {
670 /* XXX this shouldn't be an error but we can't handle it for now */
671 _mesa_problem(NULL, "%s: drawable has no context!", __FUNCTION__);
672 }
673 }
674
675 /* Force the context `c' to be the current context and associate with it
676 * buffer `b'.
677 */
678 GLboolean
679 r200MakeCurrent( __DRIcontextPrivate *driContextPriv,
680 __DRIdrawablePrivate *driDrawPriv,
681 __DRIdrawablePrivate *driReadPriv )
682 {
683 if ( driContextPriv ) {
684 r200ContextPtr newCtx =
685 (r200ContextPtr) driContextPriv->driverPrivate;
686
687 if (R200_DEBUG & DEBUG_DRI)
688 fprintf(stderr, "%s ctx %p\n", __FUNCTION__, (void *)newCtx->glCtx);
689
690 if ( newCtx->dri.drawable != driDrawPriv ) {
691 driDrawableInitVBlank( driDrawPriv, newCtx->vblank_flags );
692 newCtx->dri.drawable = driDrawPriv;
693 r200UpdateWindow( newCtx->glCtx );
694 r200UpdateViewportOffset( newCtx->glCtx );
695 }
696
697 _mesa_make_current( newCtx->glCtx,
698 (GLframebuffer *) driDrawPriv->driverPrivate,
699 (GLframebuffer *) driReadPriv->driverPrivate );
700
701 if (newCtx->vb.enabled)
702 r200VtxfmtMakeCurrent( newCtx->glCtx );
703
704 _mesa_update_state( newCtx->glCtx );
705 r200ValidateState( newCtx->glCtx );
706
707 } else {
708 if (R200_DEBUG & DEBUG_DRI)
709 fprintf(stderr, "%s ctx is null\n", __FUNCTION__);
710 _mesa_make_current( NULL, NULL, NULL );
711 }
712
713 if (R200_DEBUG & DEBUG_DRI)
714 fprintf(stderr, "End %s\n", __FUNCTION__);
715 return GL_TRUE;
716 }
717
718 /* Force the context `c' to be unbound from its buffer.
719 */
720 GLboolean
721 r200UnbindContext( __DRIcontextPrivate *driContextPriv )
722 {
723 r200ContextPtr rmesa = (r200ContextPtr) driContextPriv->driverPrivate;
724
725 if (R200_DEBUG & DEBUG_DRI)
726 fprintf(stderr, "%s ctx %p\n", __FUNCTION__, (void *)rmesa->glCtx);
727
728 r200VtxfmtUnbindContext( rmesa->glCtx );
729 return GL_TRUE;
730 }