6eb19ac079020d6befacc6c9d968a0b82606d453
[mesa.git] / src / mesa / drivers / dri / unichrome / via_context.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file via_context.c
27 *
28 * \author John Sheng (presumably of either VIA Technologies or S3 Graphics)
29 * \author Others at VIA Technologies?
30 * \author Others at S3 Graphics?
31 */
32
33 #include "main/glheader.h"
34 #include "main/context.h"
35 #include "main/matrix.h"
36 #include "main/state.h"
37 #include "main/simple_list.h"
38 #include "main/extensions.h"
39 #include "main/framebuffer.h"
40 #include "main/renderbuffer.h"
41
42 #include "swrast/swrast.h"
43 #include "swrast_setup/swrast_setup.h"
44 #include "tnl/tnl.h"
45 #include "vbo/vbo.h"
46
47 #include "tnl/t_pipeline.h"
48
49 #include "drivers/common/driverfuncs.h"
50
51 #include "via_screen.h"
52 #include "via_dri.h"
53
54 #include "via_state.h"
55 #include "via_tex.h"
56 #include "via_span.h"
57 #include "via_tris.h"
58 #include "via_ioctl.h"
59 #include "via_fb.h"
60
61 #include <stdio.h>
62 #include "main/macros.h"
63 #include "drirenderbuffer.h"
64
65 #define need_GL_ARB_point_parameters
66 #define need_GL_EXT_fog_coord
67 #define need_GL_EXT_secondary_color
68 #include "extension_helper.h"
69
70 #define DRIVER_DATE "20060710"
71
72 #include "vblank.h"
73 #include "utils.h"
74
75 GLuint VIA_DEBUG = 0;
76
77 /**
78 * Return various strings for \c glGetString.
79 *
80 * \sa glGetString
81 */
82 static const GLubyte *viaGetString(GLcontext *ctx, GLenum name)
83 {
84 static char buffer[128];
85 unsigned offset;
86
87
88 switch (name) {
89 case GL_VENDOR:
90 return (GLubyte *)"VIA Technology";
91
92 case GL_RENDERER: {
93 static const char * const chipset_names[] = {
94 "UniChrome",
95 "CastleRock (CLE266)",
96 "UniChrome (KM400)",
97 "UniChrome (K8M800)",
98 "UniChrome (PM8x0/CN400)",
99 };
100 struct via_context *vmesa = VIA_CONTEXT(ctx);
101 unsigned id = vmesa->viaScreen->deviceID;
102
103 offset = driGetRendererString( buffer,
104 chipset_names[(id > VIA_PM800) ? 0 : id],
105 DRIVER_DATE, 0 );
106 return (GLubyte *)buffer;
107 }
108
109 default:
110 return NULL;
111 }
112 }
113
114
115 /**
116 * Calculate a width that satisfies the hardware's alignment requirements.
117 * On the Unichrome hardware, each scanline must be aligned to a multiple of
118 * 16 pixels.
119 *
120 * \param width Minimum buffer width, in pixels.
121 *
122 * \returns A pixel width that meets the alignment requirements.
123 */
124 static INLINE unsigned
125 buffer_align( unsigned width )
126 {
127 return (width + 0x0f) & ~0x0f;
128 }
129
130
131 static void
132 viaDeleteRenderbuffer(struct gl_renderbuffer *rb)
133 {
134 /* Don't free() since we're contained in via_context struct. */
135 }
136
137 static GLboolean
138 viaRenderbufferStorage(GLcontext *ctx, struct gl_renderbuffer *rb,
139 GLenum internalFormat, GLuint width, GLuint height)
140 {
141 rb->Width = width;
142 rb->Height = height;
143 rb->InternalFormat = internalFormat;
144 return GL_TRUE;
145 }
146
147
148 static void
149 viaInitRenderbuffer(struct via_renderbuffer *vrb, GLenum format,
150 __DRIdrawablePrivate *dPriv)
151 {
152 const GLuint name = 0;
153 struct gl_renderbuffer *rb = & vrb->Base;
154
155 vrb->dPriv = dPriv;
156 _mesa_init_renderbuffer(rb, name);
157
158 /* Make sure we're using a null-valued GetPointer routine */
159 assert(rb->GetPointer(NULL, rb, 0, 0) == NULL);
160
161 rb->InternalFormat = format;
162
163 if (format == GL_RGBA) {
164 /* Color */
165 rb->_BaseFormat = GL_RGBA;
166 rb->DataType = GL_UNSIGNED_BYTE;
167 }
168 else if (format == GL_DEPTH_COMPONENT16) {
169 /* Depth */
170 rb->_BaseFormat = GL_DEPTH_COMPONENT;
171 /* we always Get/Put 32-bit Z values */
172 rb->DataType = GL_UNSIGNED_INT;
173 }
174 else if (format == GL_DEPTH_COMPONENT24) {
175 /* Depth */
176 rb->_BaseFormat = GL_DEPTH_COMPONENT;
177 /* we always Get/Put 32-bit Z values */
178 rb->DataType = GL_UNSIGNED_INT;
179 }
180 else {
181 /* Stencil */
182 ASSERT(format == GL_STENCIL_INDEX8_EXT);
183 rb->_BaseFormat = GL_STENCIL_INDEX;
184 rb->DataType = GL_UNSIGNED_BYTE;
185 }
186
187 rb->Delete = viaDeleteRenderbuffer;
188 rb->AllocStorage = viaRenderbufferStorage;
189 }
190
191
192 /**
193 * Calculate the framebuffer parameters for all buffers (front, back, depth,
194 * and stencil) associated with the specified context.
195 *
196 * \warning
197 * This function also calls \c AllocateBuffer to actually allocate the
198 * buffers.
199 *
200 * \sa AllocateBuffer
201 */
202 static GLboolean
203 calculate_buffer_parameters(struct via_context *vmesa,
204 struct gl_framebuffer *fb,
205 __DRIdrawablePrivate *dPriv)
206 {
207 const unsigned shift = vmesa->viaScreen->bitsPerPixel / 16;
208 const unsigned extra = 32;
209 unsigned w;
210 unsigned h;
211
212 /* Normally, the renderbuffer would be added to the framebuffer just once
213 * when the framebuffer was created. The VIA driver is a bit funny
214 * though in that the front/back/depth renderbuffers are in the per-context
215 * state!
216 * That should be fixed someday.
217 */
218
219 if (!vmesa->front.Base.InternalFormat) {
220 /* do one-time init for the renderbuffers */
221 viaInitRenderbuffer(&vmesa->front, GL_RGBA, dPriv);
222 viaSetSpanFunctions(&vmesa->front, &fb->Visual);
223 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &vmesa->front.Base);
224
225 if (fb->Visual.doubleBufferMode) {
226 viaInitRenderbuffer(&vmesa->back, GL_RGBA, dPriv);
227 viaSetSpanFunctions(&vmesa->back, &fb->Visual);
228 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &vmesa->back.Base);
229 }
230
231 if (vmesa->glCtx->Visual.depthBits > 0) {
232 viaInitRenderbuffer(&vmesa->depth,
233 (vmesa->glCtx->Visual.depthBits == 16
234 ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT24),
235 dPriv);
236 viaSetSpanFunctions(&vmesa->depth, &fb->Visual);
237 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &vmesa->depth.Base);
238 }
239
240 if (vmesa->glCtx->Visual.stencilBits > 0) {
241 viaInitRenderbuffer(&vmesa->stencil, GL_STENCIL_INDEX8_EXT,
242 dPriv);
243 viaSetSpanFunctions(&vmesa->stencil, &fb->Visual);
244 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &vmesa->stencil.Base);
245 }
246 }
247
248 assert(vmesa->front.Base.InternalFormat);
249 assert(vmesa->front.Base.AllocStorage);
250 if (fb->Visual.doubleBufferMode) {
251 assert(vmesa->back.Base.AllocStorage);
252 }
253 if (fb->Visual.depthBits) {
254 assert(vmesa->depth.Base.AllocStorage);
255 }
256
257
258 /* Allocate front-buffer */
259 if (vmesa->drawType == GLX_PBUFFER_BIT) {
260 w = vmesa->driDrawable->w;
261 h = vmesa->driDrawable->h;
262
263 vmesa->front.bpp = vmesa->viaScreen->bitsPerPixel;
264 vmesa->front.pitch = buffer_align( w ) << shift; /* bytes, not pixels */
265 vmesa->front.size = vmesa->front.pitch * h;
266
267 if (vmesa->front.map)
268 via_free_draw_buffer(vmesa, &vmesa->front);
269 if (!via_alloc_draw_buffer(vmesa, &vmesa->front))
270 return GL_FALSE;
271
272 } else {
273 w = vmesa->viaScreen->width;
274 h = vmesa->viaScreen->height;
275
276 vmesa->front.bpp = vmesa->viaScreen->bitsPerPixel;
277 vmesa->front.pitch = buffer_align( w ) << shift; /* bytes, not pixels */
278 vmesa->front.size = vmesa->front.pitch * h;
279 if (getenv("ALTERNATE_SCREEN"))
280 vmesa->front.offset = vmesa->front.size;
281 else
282 vmesa->front.offset = 0;
283 vmesa->front.map = (char *) vmesa->driScreen->pFB;
284 }
285
286
287 /* Allocate back-buffer */
288 if (vmesa->hasBack) {
289 vmesa->back.bpp = vmesa->viaScreen->bitsPerPixel;
290 vmesa->back.pitch = (buffer_align( vmesa->driDrawable->w ) << shift);
291 vmesa->back.pitch += extra;
292 vmesa->back.pitch = MIN2(vmesa->back.pitch, vmesa->front.pitch);
293 vmesa->back.size = vmesa->back.pitch * vmesa->driDrawable->h;
294 if (vmesa->back.map)
295 via_free_draw_buffer(vmesa, &vmesa->back);
296 if (!via_alloc_draw_buffer(vmesa, &vmesa->back))
297 return GL_FALSE;
298 }
299 else {
300 if (vmesa->back.map)
301 via_free_draw_buffer(vmesa, &vmesa->back);
302 (void) memset( &vmesa->back, 0, sizeof( vmesa->back ) );
303 }
304
305
306 /* Allocate depth-buffer */
307 if ( vmesa->hasStencil || vmesa->hasDepth ) {
308 vmesa->depth.bpp = vmesa->depthBits;
309 if (vmesa->depth.bpp == 24)
310 vmesa->depth.bpp = 32;
311
312 vmesa->depth.pitch = (buffer_align( vmesa->driDrawable->w ) *
313 (vmesa->depth.bpp/8)) + extra;
314 vmesa->depth.size = vmesa->depth.pitch * vmesa->driDrawable->h;
315
316 if (vmesa->depth.map)
317 via_free_draw_buffer(vmesa, &vmesa->depth);
318 if (!via_alloc_draw_buffer(vmesa, &vmesa->depth)) {
319 return GL_FALSE;
320 }
321 }
322 else {
323 if (vmesa->depth.map)
324 via_free_draw_buffer(vmesa, &vmesa->depth);
325 (void) memset( & vmesa->depth, 0, sizeof( vmesa->depth ) );
326 }
327
328 /* stencil buffer is same as depth buffer */
329 vmesa->stencil.handle = vmesa->depth.handle;
330 vmesa->stencil.size = vmesa->depth.size;
331 vmesa->stencil.offset = vmesa->depth.offset;
332 vmesa->stencil.index = vmesa->depth.index;
333 vmesa->stencil.pitch = vmesa->depth.pitch;
334 vmesa->stencil.bpp = vmesa->depth.bpp;
335 vmesa->stencil.map = vmesa->depth.map;
336 vmesa->stencil.orig = vmesa->depth.orig;
337 vmesa->stencil.origMap = vmesa->depth.origMap;
338
339 if( vmesa->viaScreen->width == vmesa->driDrawable->w &&
340 vmesa->viaScreen->height == vmesa->driDrawable->h ) {
341 vmesa->doPageFlip = vmesa->allowPageFlip;
342 if (vmesa->hasBack) {
343 assert(vmesa->back.pitch == vmesa->front.pitch);
344 }
345 }
346 else
347 vmesa->doPageFlip = GL_FALSE;
348
349 return GL_TRUE;
350 }
351
352
353 void viaReAllocateBuffers(GLcontext *ctx, GLframebuffer *drawbuffer,
354 GLuint width, GLuint height)
355 {
356 struct via_context *vmesa = VIA_CONTEXT(ctx);
357
358 calculate_buffer_parameters(vmesa, drawbuffer, vmesa->driDrawable);
359
360 _mesa_resize_framebuffer(ctx, drawbuffer, width, height);
361 }
362
363 /* Extension strings exported by the Unichrome driver.
364 */
365 const struct dri_extension card_extensions[] =
366 {
367 { "GL_ARB_multitexture", NULL },
368 { "GL_ARB_point_parameters", GL_ARB_point_parameters_functions },
369 { "GL_ARB_texture_env_add", NULL },
370 { "GL_ARB_texture_env_combine", NULL },
371 /* { "GL_ARB_texture_env_dot3", NULL }, */
372 { "GL_ARB_texture_mirrored_repeat", NULL },
373 { "GL_EXT_fog_coord", GL_EXT_fog_coord_functions },
374 { "GL_EXT_secondary_color", GL_EXT_secondary_color_functions },
375 { "GL_EXT_stencil_wrap", NULL },
376 { "GL_EXT_texture_env_combine", NULL },
377 /* { "GL_EXT_texture_env_dot3", NULL }, */
378 { "GL_EXT_texture_lod_bias", NULL },
379 { "GL_NV_blend_square", NULL },
380 { NULL, NULL }
381 };
382
383 extern const struct tnl_pipeline_stage _via_fastrender_stage;
384 extern const struct tnl_pipeline_stage _via_render_stage;
385
386 static const struct tnl_pipeline_stage *via_pipeline[] = {
387 &_tnl_vertex_transform_stage,
388 &_tnl_normal_transform_stage,
389 &_tnl_lighting_stage,
390 &_tnl_fog_coordinate_stage,
391 &_tnl_texgen_stage,
392 &_tnl_texture_transform_stage,
393 /* REMOVE: point attenuation stage */
394 #if 1
395 &_via_fastrender_stage, /* ADD: unclipped rastersetup-to-dma */
396 #endif
397 &_tnl_render_stage,
398 0,
399 };
400
401
402 static const struct dri_debug_control debug_control[] =
403 {
404 { "fall", DEBUG_FALLBACKS },
405 { "tex", DEBUG_TEXTURE },
406 { "ioctl", DEBUG_IOCTL },
407 { "prim", DEBUG_PRIMS },
408 { "vert", DEBUG_VERTS },
409 { "state", DEBUG_STATE },
410 { "verb", DEBUG_VERBOSE },
411 { "dri", DEBUG_DRI },
412 { "dma", DEBUG_DMA },
413 { "san", DEBUG_SANITY },
414 { "sync", DEBUG_SYNC },
415 { "sleep", DEBUG_SLEEP },
416 { "pix", DEBUG_PIXEL },
417 { "2d", DEBUG_2D },
418 { NULL, 0 }
419 };
420
421
422 static GLboolean
423 AllocateDmaBuffer(struct via_context *vmesa)
424 {
425 if (vmesa->dma)
426 via_free_dma_buffer(vmesa);
427
428 if (!via_alloc_dma_buffer(vmesa))
429 return GL_FALSE;
430
431 vmesa->dmaLow = 0;
432 vmesa->dmaCliprectAddr = ~0;
433 return GL_TRUE;
434 }
435
436 static void
437 FreeBuffer(struct via_context *vmesa)
438 {
439 if (vmesa->front.map && vmesa->drawType == GLX_PBUFFER_BIT)
440 via_free_draw_buffer(vmesa, &vmesa->front);
441
442 if (vmesa->back.map)
443 via_free_draw_buffer(vmesa, &vmesa->back);
444
445 if (vmesa->depth.map)
446 via_free_draw_buffer(vmesa, &vmesa->depth);
447
448 if (vmesa->breadcrumb.map)
449 via_free_draw_buffer(vmesa, &vmesa->breadcrumb);
450
451 if (vmesa->dma)
452 via_free_dma_buffer(vmesa);
453 }
454
455
456 GLboolean
457 viaCreateContext(const __GLcontextModes *visual,
458 __DRIcontextPrivate *driContextPriv,
459 void *sharedContextPrivate)
460 {
461 GLcontext *ctx, *shareCtx;
462 struct via_context *vmesa;
463 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
464 viaScreenPrivate *viaScreen = (viaScreenPrivate *)sPriv->private;
465 drm_via_sarea_t *saPriv = (drm_via_sarea_t *)
466 (((GLubyte *)sPriv->pSAREA) + viaScreen->sareaPrivOffset);
467 struct dd_function_table functions;
468
469 /* Allocate via context */
470 vmesa = (struct via_context *) CALLOC_STRUCT(via_context);
471 if (!vmesa) {
472 return GL_FALSE;
473 }
474
475 /* Parse configuration files.
476 */
477 driParseConfigFiles (&vmesa->optionCache, &viaScreen->optionCache,
478 sPriv->myNum, "unichrome");
479
480 /* pick back buffer */
481 vmesa->hasBack = visual->doubleBufferMode;
482
483 switch(visual->depthBits) {
484 case 0:
485 vmesa->hasDepth = GL_FALSE;
486 vmesa->depthBits = 0;
487 vmesa->depth_max = 1.0;
488 break;
489 case 16:
490 vmesa->hasDepth = GL_TRUE;
491 vmesa->depthBits = visual->depthBits;
492 vmesa->have_hw_stencil = GL_FALSE;
493 vmesa->depth_max = (GLfloat)0xffff;
494 vmesa->depth_clear_mask = 0xf << 28;
495 vmesa->ClearDepth = 0xffff;
496 vmesa->polygon_offset_scale = 1.0 / vmesa->depth_max;
497 break;
498 case 24:
499 vmesa->hasDepth = GL_TRUE;
500 vmesa->depthBits = visual->depthBits;
501 vmesa->depth_max = (GLfloat) 0xffffff;
502 vmesa->depth_clear_mask = 0xe << 28;
503 vmesa->ClearDepth = 0xffffff00;
504
505 assert(visual->haveStencilBuffer);
506 assert(visual->stencilBits == 8);
507
508 vmesa->have_hw_stencil = GL_TRUE;
509 vmesa->stencilBits = visual->stencilBits;
510 vmesa->stencil_clear_mask = 0x1 << 28;
511 vmesa->polygon_offset_scale = 2.0 / vmesa->depth_max;
512 break;
513 case 32:
514 vmesa->hasDepth = GL_TRUE;
515 vmesa->depthBits = visual->depthBits;
516 assert(!visual->haveStencilBuffer);
517 vmesa->have_hw_stencil = GL_FALSE;
518 vmesa->depth_max = (GLfloat)0xffffffff;
519 vmesa->depth_clear_mask = 0xf << 28;
520 vmesa->ClearDepth = 0xffffffff;
521 vmesa->polygon_offset_scale = 2.0 / vmesa->depth_max;
522 break;
523 default:
524 assert(0);
525 break;
526 }
527
528 make_empty_list(&vmesa->freed_tex_buffers);
529 make_empty_list(&vmesa->tex_image_list[VIA_MEM_VIDEO]);
530 make_empty_list(&vmesa->tex_image_list[VIA_MEM_AGP]);
531 make_empty_list(&vmesa->tex_image_list[VIA_MEM_SYSTEM]);
532
533 _mesa_init_driver_functions(&functions);
534 viaInitTextureFuncs(&functions);
535
536 /* Allocate the Mesa context */
537 if (sharedContextPrivate)
538 shareCtx = ((struct via_context *) sharedContextPrivate)->glCtx;
539 else
540 shareCtx = NULL;
541
542 vmesa->glCtx = _mesa_create_context(visual, shareCtx, &functions,
543 (void*) vmesa);
544
545 vmesa->shareCtx = shareCtx;
546
547 if (!vmesa->glCtx) {
548 FREE(vmesa);
549 return GL_FALSE;
550 }
551 driContextPriv->driverPrivate = vmesa;
552
553 ctx = vmesa->glCtx;
554
555 if (driQueryOptionb(&vmesa->optionCache, "excess_mipmap"))
556 ctx->Const.MaxTextureLevels = 11;
557 else
558 ctx->Const.MaxTextureLevels = 10;
559
560 ctx->Const.MaxTextureUnits = 2;
561 ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
562 ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
563
564 ctx->Const.MinLineWidth = 1.0;
565 ctx->Const.MinLineWidthAA = 1.0;
566 ctx->Const.MaxLineWidth = 1.0;
567 ctx->Const.MaxLineWidthAA = 1.0;
568 ctx->Const.LineWidthGranularity = 1.0;
569
570 ctx->Const.MinPointSize = 1.0;
571 ctx->Const.MinPointSizeAA = 1.0;
572 ctx->Const.MaxPointSize = 1.0;
573 ctx->Const.MaxPointSizeAA = 1.0;
574 ctx->Const.PointSizeGranularity = 1.0;
575
576 ctx->Const.MaxDrawBuffers = 1;
577
578 ctx->Driver.GetString = viaGetString;
579
580 ctx->DriverCtx = (void *)vmesa;
581 vmesa->glCtx = ctx;
582
583 /* Initialize the software rasterizer and helper modules.
584 */
585 _swrast_CreateContext(ctx);
586 _vbo_CreateContext(ctx);
587 _tnl_CreateContext(ctx);
588 _swsetup_CreateContext(ctx);
589
590 /* Install the customized pipeline:
591 */
592 _tnl_destroy_pipeline(ctx);
593 _tnl_install_pipeline(ctx, via_pipeline);
594
595 /* Configure swrast and T&L to match hardware characteristics:
596 */
597 _swrast_allow_pixel_fog(ctx, GL_FALSE);
598 _swrast_allow_vertex_fog(ctx, GL_TRUE);
599 _tnl_allow_pixel_fog(ctx, GL_FALSE);
600 _tnl_allow_vertex_fog(ctx, GL_TRUE);
601
602 vmesa->hHWContext = driContextPriv->hHWContext;
603 vmesa->driFd = sPriv->fd;
604 vmesa->driHwLock = &sPriv->pSAREA->lock;
605
606 vmesa->viaScreen = viaScreen;
607 vmesa->driScreen = sPriv;
608 vmesa->sarea = saPriv;
609
610 vmesa->renderIndex = ~0;
611 vmesa->setupIndex = ~0;
612 vmesa->hwPrimitive = GL_POLYGON+1;
613
614 /* KW: Hardwire this. Was previously set bogusly in
615 * viaCreateBuffer. Needs work before PBUFFER can be used:
616 */
617 vmesa->drawType = GLX_WINDOW_BIT;
618
619
620 _math_matrix_ctr(&vmesa->ViewportMatrix);
621
622 /* Do this early, before VIA_FLUSH_DMA can be called:
623 */
624 if (!AllocateDmaBuffer(vmesa)) {
625 fprintf(stderr ,"AllocateDmaBuffer fail\n");
626 FreeBuffer(vmesa);
627 FREE(vmesa);
628 return GL_FALSE;
629 }
630
631 /* Allocate a small piece of fb memory for synchronization:
632 */
633 vmesa->breadcrumb.bpp = 32;
634 vmesa->breadcrumb.pitch = buffer_align( 64 ) << 2;
635 vmesa->breadcrumb.size = vmesa->breadcrumb.pitch;
636
637 if (!via_alloc_draw_buffer(vmesa, &vmesa->breadcrumb)) {
638 fprintf(stderr ,"AllocateDmaBuffer fail\n");
639 FreeBuffer(vmesa);
640 FREE(vmesa);
641 return GL_FALSE;
642 }
643
644 driInitExtensions( ctx, card_extensions, GL_TRUE );
645 viaInitStateFuncs(ctx);
646 viaInitTriFuncs(ctx);
647 viaInitSpanFuncs(ctx);
648 viaInitIoctlFuncs(ctx);
649 viaInitState(ctx);
650
651 if (getenv("VIA_DEBUG"))
652 VIA_DEBUG = driParseDebugString( getenv( "VIA_DEBUG" ),
653 debug_control );
654
655 if (getenv("VIA_NO_RAST") ||
656 driQueryOptionb(&vmesa->optionCache, "no_rast"))
657 FALLBACK(vmesa, VIA_FALLBACK_USER_DISABLE, 1);
658
659 if (getenv("VIA_PAGEFLIP"))
660 vmesa->allowPageFlip = 1;
661
662 (*sPriv->systemTime->getUST)( &vmesa->swap_ust );
663
664
665 vmesa->regMMIOBase = (GLuint *)((unsigned long)viaScreen->reg);
666 vmesa->pnGEMode = (GLuint *)((unsigned long)viaScreen->reg + 0x4);
667 vmesa->regEngineStatus = (GLuint *)((unsigned long)viaScreen->reg + 0x400);
668 vmesa->regTranSet = (GLuint *)((unsigned long)viaScreen->reg + 0x43C);
669 vmesa->regTranSpace = (GLuint *)((unsigned long)viaScreen->reg + 0x440);
670 vmesa->agpBase = viaScreen->agpBase;
671
672
673 return GL_TRUE;
674 }
675
676 void
677 viaDestroyContext(__DRIcontextPrivate *driContextPriv)
678 {
679 GET_CURRENT_CONTEXT(ctx);
680 struct via_context *vmesa =
681 (struct via_context *)driContextPriv->driverPrivate;
682 struct via_context *current = ctx ? VIA_CONTEXT(ctx) : NULL;
683
684 assert(vmesa); /* should never be null */
685
686 if (vmesa->driDrawable) {
687 viaWaitIdle(vmesa, GL_FALSE);
688
689 if (vmesa->doPageFlip) {
690 LOCK_HARDWARE(vmesa);
691 if (vmesa->pfCurrentOffset != 0) {
692 fprintf(stderr, "%s - reset pf\n", __FUNCTION__);
693 viaResetPageFlippingLocked(vmesa);
694 }
695 UNLOCK_HARDWARE(vmesa);
696 }
697 }
698
699 /* check if we're deleting the currently bound context */
700 if (vmesa == current) {
701 VIA_FLUSH_DMA(vmesa);
702 _mesa_make_current(NULL, NULL, NULL);
703 }
704
705 _swsetup_DestroyContext(vmesa->glCtx);
706 _tnl_DestroyContext(vmesa->glCtx);
707 _vbo_DestroyContext(vmesa->glCtx);
708 _swrast_DestroyContext(vmesa->glCtx);
709 /* free the Mesa context */
710 _mesa_destroy_context(vmesa->glCtx);
711 /* release our data */
712 FreeBuffer(vmesa);
713
714 assert (is_empty_list(&vmesa->tex_image_list[VIA_MEM_AGP]));
715 assert (is_empty_list(&vmesa->tex_image_list[VIA_MEM_VIDEO]));
716 assert (is_empty_list(&vmesa->tex_image_list[VIA_MEM_SYSTEM]));
717 assert (is_empty_list(&vmesa->freed_tex_buffers));
718
719 driDestroyOptionCache(&vmesa->optionCache);
720
721 FREE(vmesa);
722 }
723
724
725 void viaXMesaWindowMoved(struct via_context *vmesa)
726 {
727 __DRIdrawablePrivate *const drawable = vmesa->driDrawable;
728 __DRIdrawablePrivate *const readable = vmesa->driReadable;
729 struct via_renderbuffer * draw_buffer;
730 struct via_renderbuffer * read_buffer;
731 GLuint bytePerPixel = vmesa->viaScreen->bitsPerPixel >> 3;
732
733 if (!drawable)
734 return;
735
736 draw_buffer = (struct via_renderbuffer *) drawable->driverPrivate;
737 read_buffer = (struct via_renderbuffer *) readable->driverPrivate;
738
739 switch (vmesa->glCtx->DrawBuffer->_ColorDrawBufferIndexes[0]) {
740 case BUFFER_BACK_LEFT:
741 if (drawable->numBackClipRects == 0) {
742 vmesa->numClipRects = drawable->numClipRects;
743 vmesa->pClipRects = drawable->pClipRects;
744 }
745 else {
746 vmesa->numClipRects = drawable->numBackClipRects;
747 vmesa->pClipRects = drawable->pBackClipRects;
748 }
749 break;
750 case BUFFER_FRONT_LEFT:
751 vmesa->numClipRects = drawable->numClipRects;
752 vmesa->pClipRects = drawable->pClipRects;
753 break;
754 default:
755 vmesa->numClipRects = 0;
756 break;
757 }
758
759 if ((draw_buffer->drawW != drawable->w)
760 || (draw_buffer->drawH != drawable->h)) {
761 calculate_buffer_parameters(vmesa, vmesa->glCtx->DrawBuffer,
762 drawable);
763 }
764
765 draw_buffer->drawX = drawable->x;
766 draw_buffer->drawY = drawable->y;
767 draw_buffer->drawW = drawable->w;
768 draw_buffer->drawH = drawable->h;
769
770 if (drawable != readable) {
771 if ((read_buffer->drawW != readable->w)
772 || (read_buffer->drawH != readable->h)) {
773 calculate_buffer_parameters(vmesa, vmesa->glCtx->ReadBuffer,
774 readable);
775 }
776
777 read_buffer->drawX = readable->x;
778 read_buffer->drawY = readable->y;
779 read_buffer->drawW = readable->w;
780 read_buffer->drawH = readable->h;
781 }
782
783 vmesa->front.orig = (vmesa->front.offset +
784 draw_buffer->drawY * vmesa->front.pitch +
785 draw_buffer->drawX * bytePerPixel);
786
787 vmesa->front.origMap = (vmesa->front.map +
788 draw_buffer->drawY * vmesa->front.pitch +
789 draw_buffer->drawX * bytePerPixel);
790
791 vmesa->back.orig = (vmesa->back.offset +
792 draw_buffer->drawY * vmesa->back.pitch +
793 draw_buffer->drawX * bytePerPixel);
794
795 vmesa->back.origMap = (vmesa->back.map +
796 draw_buffer->drawY * vmesa->back.pitch +
797 draw_buffer->drawX * bytePerPixel);
798
799 vmesa->depth.orig = (vmesa->depth.offset +
800 draw_buffer->drawY * vmesa->depth.pitch +
801 draw_buffer->drawX * bytePerPixel);
802
803 vmesa->depth.origMap = (vmesa->depth.map +
804 draw_buffer->drawY * vmesa->depth.pitch +
805 draw_buffer->drawX * bytePerPixel);
806
807 viaCalcViewport(vmesa->glCtx);
808 }
809
810 GLboolean
811 viaUnbindContext(__DRIcontextPrivate *driContextPriv)
812 {
813 return GL_TRUE;
814 }
815
816 GLboolean
817 viaMakeCurrent(__DRIcontextPrivate *driContextPriv,
818 __DRIdrawablePrivate *driDrawPriv,
819 __DRIdrawablePrivate *driReadPriv)
820 {
821 if (VIA_DEBUG & DEBUG_DRI) {
822 fprintf(stderr, "driContextPriv = %016lx\n", (unsigned long)driContextPriv);
823 fprintf(stderr, "driDrawPriv = %016lx\n", (unsigned long)driDrawPriv);
824 fprintf(stderr, "driReadPriv = %016lx\n", (unsigned long)driReadPriv);
825 }
826
827 if (driContextPriv) {
828 struct via_context *vmesa =
829 (struct via_context *)driContextPriv->driverPrivate;
830 GLcontext *ctx = vmesa->glCtx;
831 struct gl_framebuffer *drawBuffer, *readBuffer;
832
833 drawBuffer = (GLframebuffer *)driDrawPriv->driverPrivate;
834 readBuffer = (GLframebuffer *)driReadPriv->driverPrivate;
835
836 if ((vmesa->driDrawable != driDrawPriv)
837 || (vmesa->driReadable != driReadPriv)) {
838 if (driDrawPriv->swap_interval == (unsigned)-1) {
839 driDrawPriv->vblFlags =
840 vmesa->viaScreen->irqEnabled ?
841 driGetDefaultVBlankFlags(&vmesa->optionCache) :
842 VBLANK_FLAG_NO_IRQ;
843
844 driDrawableInitVBlank(driDrawPriv);
845 }
846
847 vmesa->driDrawable = driDrawPriv;
848 vmesa->driReadable = driReadPriv;
849
850 if ((drawBuffer->Width != driDrawPriv->w)
851 || (drawBuffer->Height != driDrawPriv->h)) {
852 _mesa_resize_framebuffer(ctx, drawBuffer,
853 driDrawPriv->w, driDrawPriv->h);
854 drawBuffer->Initialized = GL_TRUE;
855 }
856
857 if (!calculate_buffer_parameters(vmesa, drawBuffer, driDrawPriv)) {
858 return GL_FALSE;
859 }
860
861 if (driDrawPriv != driReadPriv) {
862 if ((readBuffer->Width != driReadPriv->w)
863 || (readBuffer->Height != driReadPriv->h)) {
864 _mesa_resize_framebuffer(ctx, readBuffer,
865 driReadPriv->w, driReadPriv->h);
866 readBuffer->Initialized = GL_TRUE;
867 }
868
869 if (!calculate_buffer_parameters(vmesa, readBuffer, driReadPriv)) {
870 return GL_FALSE;
871 }
872 }
873 }
874
875 _mesa_make_current(vmesa->glCtx, drawBuffer, readBuffer);
876
877 ctx->Driver.DrawBuffer( ctx, ctx->Color.DrawBuffer[0] );
878
879 viaXMesaWindowMoved(vmesa);
880 ctx->Driver.Scissor(vmesa->glCtx,
881 vmesa->glCtx->Scissor.X,
882 vmesa->glCtx->Scissor.Y,
883 vmesa->glCtx->Scissor.Width,
884 vmesa->glCtx->Scissor.Height);
885 }
886 else {
887 _mesa_make_current(NULL, NULL, NULL);
888 }
889
890 return GL_TRUE;
891 }
892
893 void viaGetLock(struct via_context *vmesa, GLuint flags)
894 {
895 __DRIdrawablePrivate *dPriv = vmesa->driDrawable;
896 __DRIscreenPrivate *sPriv = vmesa->driScreen;
897
898 drmGetLock(vmesa->driFd, vmesa->hHWContext, flags);
899
900 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
901 if (dPriv != vmesa->driReadable) {
902 DRI_VALIDATE_DRAWABLE_INFO(sPriv, vmesa->driReadable);
903 }
904
905 if (vmesa->sarea->ctxOwner != vmesa->hHWContext) {
906 vmesa->sarea->ctxOwner = vmesa->hHWContext;
907 vmesa->newEmitState = ~0;
908 }
909
910 if (vmesa->lastStamp != dPriv->lastStamp) {
911 viaXMesaWindowMoved(vmesa);
912 driUpdateFramebufferSize(vmesa->glCtx, dPriv);
913 vmesa->newEmitState = ~0;
914 vmesa->lastStamp = dPriv->lastStamp;
915 }
916
917 if (vmesa->doPageFlip &&
918 vmesa->pfCurrentOffset != vmesa->sarea->pfCurrentOffset) {
919 fprintf(stderr, "%s - reset pf\n", __FUNCTION__);
920 viaResetPageFlippingLocked(vmesa);
921 }
922 }
923
924
925 void
926 viaSwapBuffers(__DRIdrawablePrivate *drawablePrivate)
927 {
928 __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *)drawablePrivate;
929
930 if (dPriv &&
931 dPriv->driContextPriv &&
932 dPriv->driContextPriv->driverPrivate) {
933 struct via_context *vmesa =
934 (struct via_context *)dPriv->driContextPriv->driverPrivate;
935 GLcontext *ctx = vmesa->glCtx;
936
937 _mesa_notifySwapBuffers(ctx);
938
939 if (ctx->Visual.doubleBufferMode) {
940 if (vmesa->doPageFlip) {
941 viaPageFlip(dPriv);
942 }
943 else {
944 viaCopyBuffer(dPriv);
945 }
946 }
947 else
948 VIA_FLUSH_DMA(vmesa);
949 }
950 else {
951 _mesa_problem(NULL, "viaSwapBuffers: drawable has no context!\n");
952 }
953 }