Fixed some suspicious pointer casts that caused lots of
[mesa.git] / src / mesa / drivers / dri / savage / savage_xmesa.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 #include <X11/Xlibint.h>
27 #include <stdio.h>
28
29 #include "savagecontext.h"
30 #include "context.h"
31 #include "matrix.h"
32
33 #include "simple_list.h"
34
35 #include "utils.h"
36
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
39 #include "tnl/tnl.h"
40 #include "array_cache/acache.h"
41
42 #include "tnl/t_pipeline.h"
43
44 #include "drivers/common/driverfuncs.h"
45
46 #include "savagedd.h"
47 #include "savagestate.h"
48 #include "savagetex.h"
49 #include "savagespan.h"
50 #include "savagetris.h"
51 #include "savageioctl.h"
52 #include "savage_bci.h"
53
54 #include "savage_dri.h"
55
56 #include "texmem.h"
57
58 #include "xmlpool.h"
59
60 /* Driver-specific options
61 */
62 #define SAVAGE_ENABLE_VDMA(def) \
63 DRI_CONF_OPT_BEGIN(enable_vdma,bool,def) \
64 DRI_CONF_DESC(en,"Use DMA for vertex transfers") \
65 DRI_CONF_DESC(de,"Benutze DMA für Vertextransfers") \
66 DRI_CONF_OPT_END
67 #define SAVAGE_ENABLE_FASTPATH(def) \
68 DRI_CONF_OPT_BEGIN(enable_fastpath,bool,def) \
69 DRI_CONF_DESC(en,"Use fast path for unclipped primitives") \
70 DRI_CONF_DESC(de,"Schneller Codepfad für ungeschnittene Polygone") \
71 DRI_CONF_OPT_END
72 #define SAVAGE_SYNC_FRAMES(def) \
73 DRI_CONF_OPT_BEGIN(sync_frames,bool,def) \
74 DRI_CONF_DESC(en,"Synchronize with graphics hardware after each frame") \
75 DRI_CONF_DESC(de,"Synchronisiere nach jedem Frame mit Grafikhardware") \
76 DRI_CONF_OPT_END
77
78 /* Configuration
79 */
80 PUBLIC const char __driConfigOptions[] =
81 DRI_CONF_BEGIN
82 DRI_CONF_SECTION_QUALITY
83 DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
84 DRI_CONF_COLOR_REDUCTION(DRI_CONF_COLOR_REDUCTION_DITHER)
85 DRI_CONF_FLOAT_DEPTH(false)
86 DRI_CONF_SECTION_END
87 DRI_CONF_SECTION_PERFORMANCE
88 SAVAGE_ENABLE_VDMA(true)
89 SAVAGE_ENABLE_FASTPATH(true)
90 SAVAGE_SYNC_FRAMES(false)
91 DRI_CONF_MAX_TEXTURE_UNITS(2,1,2)
92 DRI_CONF_TEXTURE_HEAPS(DRI_CONF_TEXTURE_HEAPS_ALL)
93 DRI_CONF_SECTION_END
94 DRI_CONF_SECTION_DEBUG
95 DRI_CONF_NO_RAST(false)
96 DRI_CONF_SECTION_END
97 DRI_CONF_END;
98 static const GLuint __driNConfigOptions = 9;
99
100 #ifdef USE_NEW_INTERFACE
101 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
102 #endif /* USE_NEW_INTERFACE */
103
104 static const struct dri_debug_control debug_control[] =
105 {
106 { "fall", DEBUG_FALLBACKS },
107 { "api", DEBUG_VERBOSE_API },
108 { "tex", DEBUG_VERBOSE_TEX },
109 { "verb", DEBUG_VERBOSE_MSG },
110 { "dma", DEBUG_DMA },
111 { "state", DEBUG_STATE },
112 { NULL, 0 }
113 };
114 #ifndef SAVAGE_DEBUG
115 int SAVAGE_DEBUG = 0;
116 #endif
117
118
119 /*For time caculating test*/
120 #if defined(DEBUG_TIME) && DEBUG_TIME
121 struct timeval tv_s,tv_f;
122 unsigned long time_sum=0;
123 struct timeval tv_s1,tv_f1;
124 #endif
125
126 static const char *const card_extensions[] =
127 {
128 "GL_ARB_multitexture",
129 "GL_EXT_texture_lod_bias",
130 "GL_EXT_texture_env_add",
131 NULL
132 };
133
134 extern struct tnl_pipeline_stage _savage_texnorm_stage;
135 extern struct tnl_pipeline_stage _savage_render_stage;
136
137 static const struct tnl_pipeline_stage *savage_pipeline[] = {
138
139 &_tnl_vertex_transform_stage,
140 &_tnl_normal_transform_stage,
141 &_tnl_lighting_stage,
142 &_tnl_fog_coordinate_stage,
143 &_tnl_texgen_stage,
144 &_tnl_texture_transform_stage,
145 &_savage_texnorm_stage,
146 &_savage_render_stage,
147 &_tnl_render_stage,
148 0,
149 };
150
151
152 /* this is first function called in dirver*/
153
154 static GLboolean
155 savageInitDriver(__DRIscreenPrivate *sPriv)
156 {
157 savageScreenPrivate *savageScreen;
158 SAVAGEDRIPtr gDRIPriv = (SAVAGEDRIPtr)sPriv->pDevPriv;
159
160 /* Allocate the private area */
161 savageScreen = (savageScreenPrivate *)Xmalloc(sizeof(savageScreenPrivate));
162 if (!savageScreen)
163 return GL_FALSE;
164
165 savageScreen->driScrnPriv = sPriv;
166 sPriv->private = (void *)savageScreen;
167
168 savageScreen->chipset=gDRIPriv->chipset;
169 savageScreen->width=gDRIPriv->width;
170 savageScreen->height=gDRIPriv->height;
171 savageScreen->mem=gDRIPriv->mem;
172 savageScreen->cpp=gDRIPriv->cpp;
173 savageScreen->zpp=gDRIPriv->zpp;
174
175 savageScreen->agpMode=gDRIPriv->agpMode;
176
177 savageScreen->bufferSize=gDRIPriv->bufferSize;
178
179 if (gDRIPriv->cpp == 4)
180 savageScreen->frontFormat = DV_PF_8888;
181 else
182 savageScreen->frontFormat = DV_PF_565;
183 savageScreen->frontOffset=gDRIPriv->frontOffset;
184 savageScreen->backOffset = gDRIPriv->backOffset;
185 savageScreen->depthOffset=gDRIPriv->depthOffset;
186
187 savageScreen->textureOffset[SAVAGE_CARD_HEAP] =
188 gDRIPriv->textureOffset;
189 savageScreen->textureSize[SAVAGE_CARD_HEAP] =
190 gDRIPriv->textureSize;
191 savageScreen->logTextureGranularity[SAVAGE_CARD_HEAP] =
192 gDRIPriv->logTextureGranularity;
193
194 savageScreen->textureOffset[SAVAGE_AGP_HEAP] =
195 gDRIPriv->agpTextureHandle;
196 savageScreen->textureSize[SAVAGE_AGP_HEAP] =
197 gDRIPriv->agpTextureSize;
198 savageScreen->logTextureGranularity[SAVAGE_AGP_HEAP] =
199 gDRIPriv->logAgpTextureGranularity;
200
201 savageScreen->agpTextures.handle = gDRIPriv->agpTextureHandle;
202 savageScreen->agpTextures.size = gDRIPriv->agpTextureSize;
203 if (gDRIPriv->agpTextureSize) {
204 if (drmMap(sPriv->fd,
205 savageScreen->agpTextures.handle,
206 savageScreen->agpTextures.size,
207 (drmAddress *)&(savageScreen->agpTextures.map)) != 0) {
208 Xfree(savageScreen);
209 sPriv->private = NULL;
210 return GL_FALSE;
211 }
212 } else
213 savageScreen->agpTextures.map = NULL;
214
215 savageScreen->texVirtual[SAVAGE_CARD_HEAP] =
216 (drmAddress)(((GLubyte *)sPriv->pFB)+gDRIPriv->textureOffset);
217 savageScreen->texVirtual[SAVAGE_AGP_HEAP] =
218 (drmAddress)(savageScreen->agpTextures.map);
219
220 savageScreen->aperture.handle = gDRIPriv->apertureHandle;
221 savageScreen->aperture.size = gDRIPriv->apertureSize;
222 savageScreen->aperturePitch = gDRIPriv->aperturePitch;
223 if (drmMap(sPriv->fd,
224 savageScreen->aperture.handle,
225 savageScreen->aperture.size,
226 (drmAddress *)&savageScreen->aperture.map) != 0)
227 {
228 Xfree(savageScreen);
229 sPriv->private = NULL;
230 return GL_FALSE;
231 }
232
233 savageScreen->bufs = drmMapBufs(sPriv->fd);
234
235 savageScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
236
237 /* parse information in __driConfigOptions */
238 driParseOptionInfo (&savageScreen->optionCache,
239 __driConfigOptions, __driNConfigOptions);
240
241 #if 0
242 savageDDFastPathInit();
243 savageDDTrifuncInit();
244 savageDDSetupInit();
245 #endif
246 return GL_TRUE;
247 }
248
249 /* Accessed by dlsym from dri_mesa_init.c
250 */
251 static void
252 savageDestroyScreen(__DRIscreenPrivate *sPriv)
253 {
254 savageScreenPrivate *savageScreen = (savageScreenPrivate *)sPriv->private;
255
256 if (savageScreen->bufs)
257 drmUnmapBufs(savageScreen->bufs);
258
259 /* free all option information */
260 driDestroyOptionInfo (&savageScreen->optionCache);
261
262 Xfree(savageScreen);
263 sPriv->private = NULL;
264 }
265
266 #if 0
267 GLvisual *XMesaCreateVisual(Display *dpy,
268 __DRIscreenPrivate *driScrnPriv,
269 const XVisualInfo *visinfo,
270 const __GLXvisualConfig *config)
271 {
272 /* Drivers may change the args to _mesa_create_visual() in order to
273 * setup special visuals.
274 */
275 return _mesa_create_visual( config->rgba,
276 config->doubleBuffer,
277 config->stereo,
278 _mesa_bitcount(visinfo->red_mask),
279 _mesa_bitcount(visinfo->green_mask),
280 _mesa_bitcount(visinfo->blue_mask),
281 config->alphaSize,
282 0, /* index bits */
283 config->depthSize,
284 config->stencilSize,
285 config->accumRedSize,
286 config->accumGreenSize,
287 config->accumBlueSize,
288 config->accumAlphaSize,
289 0 /* num samples */ );
290 }
291 #endif
292
293
294 static GLboolean
295 savageCreateContext( const __GLcontextModes *mesaVis,
296 __DRIcontextPrivate *driContextPriv,
297 void *sharedContextPrivate )
298 {
299 GLcontext *ctx, *shareCtx;
300 savageContextPtr imesa;
301 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
302 struct dd_function_table functions;
303 savageScreenPrivate *savageScreen = (savageScreenPrivate *)sPriv->private;
304 drm_savage_sarea_t *saPriv=(drm_savage_sarea_t *)(((char*)sPriv->pSAREA)+
305 savageScreen->sarea_priv_offset);
306 int textureSize[SAVAGE_NR_TEX_HEAPS];
307 int i;
308 imesa = (savageContextPtr)Xcalloc(sizeof(savageContext), 1);
309 if (!imesa) {
310 return GL_FALSE;
311 }
312
313 /* Init default driver functions then plug in savage-specific texture
314 * functions that are needed as early as during context creation. */
315 _mesa_init_driver_functions( &functions );
316 savageDDInitTextureFuncs( &functions );
317
318 /* Allocate the Mesa context */
319 if (sharedContextPrivate)
320 shareCtx = ((savageContextPtr) sharedContextPrivate)->glCtx;
321 else
322 shareCtx = NULL;
323 ctx = _mesa_create_context(mesaVis, shareCtx, &functions, imesa);
324 if (!ctx) {
325 Xfree(imesa);
326 return GL_FALSE;
327 }
328 driContextPriv->driverPrivate = imesa;
329
330 imesa->cmdBuf.size = SAVAGE_CMDBUF_SIZE;
331 imesa->cmdBuf.base = imesa->cmdBuf.write =
332 malloc(SAVAGE_CMDBUF_SIZE * sizeof(drm_savage_cmd_header_t));
333 if (!imesa->cmdBuf.base)
334 return GL_FALSE;
335
336 /* Parse configuration files */
337 driParseConfigFiles (&imesa->optionCache, &savageScreen->optionCache,
338 sPriv->myNum, "savage");
339
340 imesa->float_depth = driQueryOptionb(&imesa->optionCache, "float_depth") &&
341 savageScreen->chipset >= S3_SAVAGE4;
342 imesa->no_rast = driQueryOptionb(&imesa->optionCache, "no_rast");
343
344 #if 0
345 ctx->Const.MinLineWidth = 1.0;
346 ctx->Const.MinLineWidthAA = 1.0;
347 ctx->Const.MaxLineWidth = 3.0;
348 ctx->Const.MaxLineWidthAA = 3.0;
349 ctx->Const.LineWidthGranularity = 1.0;
350 #endif
351
352 /* Dri stuff
353 */
354 imesa->hHWContext = driContextPriv->hHWContext;
355 imesa->driFd = sPriv->fd;
356 imesa->driHwLock = &sPriv->pSAREA->lock;
357
358 imesa->savageScreen = savageScreen;
359 imesa->driScreen = sPriv;
360 imesa->sarea = saPriv;
361 imesa->glBuffer = NULL;
362
363 /* DMA buffer */
364
365 for(i=0;i<5;i++)
366 {
367 imesa->apertureBase[i] = (GLubyte *)savageScreen->aperture.map +
368 0x01000000 * i;
369 }
370
371 imesa->aperturePitch = savageScreen->aperturePitch;
372
373 /* change texHeap initialize to support two kind of texture heap*/
374 /* here is some parts of initialization, others in InitDriver() */
375
376 (void) memset( imesa->textureHeaps, 0, sizeof( imesa->textureHeaps ) );
377 make_empty_list( & imesa->swapped );
378
379 textureSize[SAVAGE_CARD_HEAP] = savageScreen->textureSize[SAVAGE_CARD_HEAP];
380 textureSize[SAVAGE_AGP_HEAP] = savageScreen->textureSize[SAVAGE_AGP_HEAP];
381 imesa->lastTexHeap = savageScreen->texVirtual[SAVAGE_AGP_HEAP] ? 2 : 1;
382 switch(driQueryOptioni (&imesa->optionCache, "texture_heaps")) {
383 case DRI_CONF_TEXTURE_HEAPS_CARD: /* only use card memory, if available */
384 if (textureSize[SAVAGE_CARD_HEAP])
385 imesa->lastTexHeap = 1;
386 break;
387 case DRI_CONF_TEXTURE_HEAPS_GART: /* only use gart memory, if available */
388 if (imesa->lastTexHeap == 2 && textureSize[SAVAGE_AGP_HEAP])
389 textureSize[SAVAGE_CARD_HEAP] = 0;
390 break;
391 /*default: Nothing to do, use all available memory. */
392 }
393
394 for (i = 0; i < imesa->lastTexHeap; i++) {
395 imesa->textureHeaps[i] = driCreateTextureHeap(
396 i, imesa,
397 textureSize[i],
398 11, /* 2^11 = 2k alignment */
399 SAVAGE_NR_TEX_REGIONS,
400 (drmTextureRegionPtr)imesa->sarea->texList[i],
401 &imesa->sarea->texAge[i],
402 &imesa->swapped,
403 sizeof( savageTexObj ),
404 (destroy_texture_object_t *) savageDestroyTexObj );
405 /* If textureSize[i] == 0 textureHeaps[i] is NULL. This can happen
406 * if there is not enough card memory for a card texture heap. */
407 if (imesa->textureHeaps[i])
408 driSetTextureSwapCounterLocation( imesa->textureHeaps[i],
409 & imesa->c_textureSwaps );
410 }
411 imesa->texture_depth = driQueryOptioni (&imesa->optionCache,
412 "texture_depth");
413 if (imesa->texture_depth == DRI_CONF_TEXTURE_DEPTH_FB)
414 imesa->texture_depth = ( savageScreen->cpp == 4 ) ?
415 DRI_CONF_TEXTURE_DEPTH_32 : DRI_CONF_TEXTURE_DEPTH_16;
416
417 if (savageScreen->chipset >= S3_SAVAGE4)
418 ctx->Const.MaxTextureUnits = 2;
419 else
420 ctx->Const.MaxTextureUnits = 1;
421 if (driQueryOptioni(&imesa->optionCache, "texture_units") <
422 ctx->Const.MaxTextureUnits)
423 ctx->Const.MaxTextureUnits =
424 driQueryOptioni(&imesa->optionCache, "texture_units");
425 ctx->Const.MaxTextureImageUnits = ctx->Const.MaxTextureUnits;
426 ctx->Const.MaxTextureCoordUnits = ctx->Const.MaxTextureUnits;
427
428 driCalculateMaxTextureLevels( imesa->textureHeaps,
429 imesa->lastTexHeap,
430 & ctx->Const,
431 4,
432 11, /* max 2D texture size is 2048x2048 */
433 0, /* 3D textures unsupported. */
434 0, /* cube textures unsupported. */
435 0, /* texture rectangles unsupported. */
436 12,
437 GL_FALSE );
438 if (ctx->Const.MaxTextureLevels <= 6) { /*spec requires at least 64x64*/
439 __driUtilMessage("Not enough texture memory. "
440 "Falling back to indirect rendering.");
441 Xfree(imesa);
442 return GL_FALSE;
443 }
444
445 imesa->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
446 imesa->depth_scale = (imesa->savageScreen->zpp == 2) ?
447 (1.0F/0xffff):(1.0F/0xffffff);
448
449 imesa->bufferSize = savageScreen->bufferSize;
450 imesa->dmaVtxBuf.total = 0;
451 imesa->dmaVtxBuf.used = 0;
452 imesa->dmaVtxBuf.flushed = 0;
453
454 imesa->clientVtxBuf.total = imesa->bufferSize / 4;
455 imesa->clientVtxBuf.used = 0;
456 imesa->clientVtxBuf.flushed = 0;
457 imesa->clientVtxBuf.buf = (u_int32_t *)malloc(imesa->bufferSize);
458
459 imesa->vtxBuf = &imesa->clientVtxBuf;
460
461 imesa->firstElt = -1;
462
463 /* Uninitialized vertex format. Force setting the vertex state in
464 * savageRenderStart.
465 */
466 imesa->vertex_size = 0;
467
468 /* Utah stuff
469 */
470 imesa->new_state = ~0;
471 imesa->new_gl_state = ~0;
472 imesa->RenderIndex = ~0;
473 imesa->dirty = ~0;
474 imesa->lostContext = GL_TRUE;
475 imesa->CurrentTexObj[0] = 0;
476 imesa->CurrentTexObj[1] = 0;
477
478 /* Initialize the software rasterizer and helper modules.
479 */
480 _swrast_CreateContext( ctx );
481 _ac_CreateContext( ctx );
482 _tnl_CreateContext( ctx );
483
484 _swsetup_CreateContext( ctx );
485
486 /* Install the customized pipeline:
487 */
488 _tnl_destroy_pipeline( ctx );
489 _tnl_install_pipeline( ctx, savage_pipeline );
490
491 imesa->enable_fastpath = driQueryOptionb(&imesa->optionCache,
492 "enable_fastpath");
493 /* DRM versions before 2.1.3 would only render triangle lists. ELTS
494 * support was added in 2.2.0. */
495 if (imesa->enable_fastpath && sPriv->drmMinor < 2) {
496 fprintf (stderr,
497 "*** Disabling fast path because your DRM version is buggy "
498 "or doesn't\n*** support ELTS. You need at least Savage DRM "
499 "version 2.2.\n");
500 imesa->enable_fastpath = GL_FALSE;
501 }
502
503 if (!savageScreen->bufs || savageScreen->chipset == S3_SUPERSAVAGE)
504 imesa->enable_vdma = GL_FALSE;
505 else
506 imesa->enable_vdma = driQueryOptionb(&imesa->optionCache, "enable_vdma");
507
508 imesa->sync_frames = driQueryOptionb(&imesa->optionCache, "sync_frames");
509
510 /* Configure swrast to match hardware characteristics:
511 */
512 _tnl_allow_pixel_fog( ctx, GL_FALSE );
513 _tnl_allow_vertex_fog( ctx, GL_TRUE );
514 _swrast_allow_pixel_fog( ctx, GL_FALSE );
515 _swrast_allow_vertex_fog( ctx, GL_TRUE );
516
517 ctx->DriverCtx = (void *) imesa;
518 imesa->glCtx = ctx;
519
520 #ifndef SAVAGE_DEBUG
521 SAVAGE_DEBUG = driParseDebugString( getenv( "SAVAGE_DEBUG" ),
522 debug_control );
523 #endif
524
525 driInitExtensions( ctx, card_extensions, GL_TRUE );
526
527 savageDDInitStateFuncs( ctx );
528 savageDDInitSpanFuncs( ctx );
529 savageDDInitDriverFuncs( ctx );
530 savageDDInitIoctlFuncs( ctx );
531 savageInitTriFuncs( ctx );
532
533 savageDDInitState( imesa );
534
535 driContextPriv->driverPrivate = (void *) imesa;
536
537 return GL_TRUE;
538 }
539
540 static void
541 savageDestroyContext(__DRIcontextPrivate *driContextPriv)
542 {
543 savageContextPtr imesa = (savageContextPtr) driContextPriv->driverPrivate;
544 GLuint i;
545
546 assert (imesa); /* should never be NULL */
547 if (imesa) {
548 savageFlushVertices(imesa);
549 savageReleaseIndexedVerts(imesa);
550 savageFlushCmdBuf(imesa, GL_TRUE); /* release DMA buffer */
551 WAIT_IDLE_EMPTY(imesa);
552
553 for (i = 0; i < imesa->lastTexHeap; i++)
554 driDestroyTextureHeap(imesa->textureHeaps[i]);
555
556 free(imesa->cmdBuf.base);
557 free(imesa->clientVtxBuf.buf);
558
559 _swsetup_DestroyContext(imesa->glCtx );
560 _tnl_DestroyContext( imesa->glCtx );
561 _ac_DestroyContext( imesa->glCtx );
562 _swrast_DestroyContext( imesa->glCtx );
563
564 /* free the Mesa context */
565 imesa->glCtx->DriverCtx = NULL;
566 _mesa_destroy_context(imesa->glCtx);
567
568 /* no longer use vertex_dma_buf*/
569 Xfree(imesa);
570 }
571 }
572
573 static GLboolean
574 savageCreateBuffer( __DRIscreenPrivate *driScrnPriv,
575 __DRIdrawablePrivate *driDrawPriv,
576 const __GLcontextModes *mesaVis,
577 GLboolean isPixmap)
578 {
579 if (isPixmap) {
580 return GL_FALSE; /* not implemented */
581 }
582 else {
583 GLboolean swStencil = mesaVis->stencilBits > 0 && mesaVis->depthBits != 24;
584 driDrawPriv->driverPrivate = (void *)
585 _mesa_create_framebuffer(mesaVis,
586 GL_FALSE, /* software depth buffer? */
587 swStencil,
588 mesaVis->accumRedBits > 0,
589 mesaVis->alphaBits > 0 );
590
591 return (driDrawPriv->driverPrivate != NULL);
592 }
593 }
594
595 static void
596 savageDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
597 {
598 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
599 }
600
601 #if 0
602 void XMesaSwapBuffers(__DRIdrawablePrivate *driDrawPriv)
603 {
604 /* XXX should do swap according to the buffer, not the context! */
605 savageContextPtr imesa = savageCtx;
606
607 FLUSH_VB( imesa->glCtx, "swap buffers" );
608 savageSwapBuffers(imesa);
609 }
610 #endif
611
612 void savageXMesaSetFrontClipRects( savageContextPtr imesa )
613 {
614 __DRIdrawablePrivate *dPriv = imesa->driDrawable;
615
616 imesa->numClipRects = dPriv->numClipRects;
617 imesa->pClipRects = dPriv->pClipRects;
618 imesa->drawX = dPriv->x;
619 imesa->drawY = dPriv->y;
620
621 savageCalcViewport( imesa->glCtx );
622 }
623
624
625 void savageXMesaSetBackClipRects( savageContextPtr imesa )
626 {
627 __DRIdrawablePrivate *dPriv = imesa->driDrawable;
628
629 if (dPriv->numBackClipRects == 0)
630 {
631
632
633 imesa->numClipRects = dPriv->numClipRects;
634 imesa->pClipRects = dPriv->pClipRects;
635 imesa->drawX = dPriv->x;
636 imesa->drawY = dPriv->y;
637 } else {
638
639
640 imesa->numClipRects = dPriv->numBackClipRects;
641 imesa->pClipRects = dPriv->pBackClipRects;
642 imesa->drawX = dPriv->backX;
643 imesa->drawY = dPriv->backY;
644 }
645
646 savageCalcViewport( imesa->glCtx );
647 }
648
649
650 static void savageXMesaWindowMoved( savageContextPtr imesa )
651 {
652 if (0)
653 fprintf(stderr, "savageXMesaWindowMoved\n\n");
654
655 switch (imesa->glCtx->Color._DrawDestMask[0]) {
656 case DD_FRONT_LEFT_BIT:
657 savageXMesaSetFrontClipRects( imesa );
658 break;
659 case DD_BACK_LEFT_BIT:
660 savageXMesaSetBackClipRects( imesa );
661 break;
662 default:
663 break;
664 }
665 }
666
667
668 static GLboolean
669 savageUnbindContext(__DRIcontextPrivate *driContextPriv)
670 {
671 savageContextPtr savage = (savageContextPtr) driContextPriv->driverPrivate;
672 if (savage)
673 savage->dirty = ~0;
674
675 return GL_TRUE;
676 }
677
678 #if 0
679 static GLboolean
680 savageOpenFullScreen(__DRIcontextPrivate *driContextPriv)
681 {
682
683
684
685 if (driContextPriv) {
686 savageContextPtr imesa = (savageContextPtr) driContextPriv->driverPrivate;
687 imesa->IsFullScreen = GL_TRUE;
688 imesa->backup_frontOffset = imesa->savageScreen->frontOffset;
689 imesa->backup_backOffset = imesa->savageScreen->backOffset;
690 imesa->backup_frontBitmapDesc = imesa->savageScreen->frontBitmapDesc;
691 imesa->savageScreen->frontBitmapDesc = imesa->savageScreen->backBitmapDesc;
692 imesa->toggle = TARGET_BACK;
693 }
694
695 return GL_TRUE;
696 }
697
698 static GLboolean
699 savageCloseFullScreen(__DRIcontextPrivate *driContextPriv)
700 {
701
702 if (driContextPriv) {
703 savageContextPtr imesa = (savageContextPtr) driContextPriv->driverPrivate;
704 WAIT_IDLE_EMPTY(imesa);
705 imesa->IsFullScreen = GL_FALSE;
706 imesa->savageScreen->frontOffset = imesa->backup_frontOffset;
707 imesa->savageScreen->backOffset = imesa->backup_backOffset;
708 imesa->savageScreen->frontBitmapDesc = imesa->backup_frontBitmapDesc;
709 }
710 return GL_TRUE;
711 }
712 #endif
713
714 static GLboolean
715 savageMakeCurrent(__DRIcontextPrivate *driContextPriv,
716 __DRIdrawablePrivate *driDrawPriv,
717 __DRIdrawablePrivate *driReadPriv)
718 {
719 if (driContextPriv) {
720 savageContextPtr imesa = (savageContextPtr) driContextPriv->driverPrivate;
721
722 imesa->driReadable = driReadPriv;
723 imesa->driDrawable = driDrawPriv;
724 imesa->mesa_drawable = driDrawPriv;
725 imesa->dirty = ~0;
726
727 _mesa_make_current2(imesa->glCtx,
728 (GLframebuffer *) driDrawPriv->driverPrivate,
729 (GLframebuffer *) driReadPriv->driverPrivate);
730
731 savageXMesaWindowMoved( imesa );
732 }
733 else
734 {
735 _mesa_make_current(NULL, NULL);
736 }
737 return GL_TRUE;
738 }
739
740
741 void savageGetLock( savageContextPtr imesa, GLuint flags )
742 {
743 __DRIdrawablePrivate *dPriv = imesa->driDrawable;
744 __DRIscreenPrivate *sPriv = imesa->driScreen;
745 drm_savage_sarea_t *sarea = imesa->sarea;
746 int me = imesa->hHWContext;
747 int stamp = dPriv->lastStamp;
748 int heap;
749 unsigned int timestamp = 0;
750
751
752
753 /* We know there has been contention.
754 */
755 drmGetLock(imesa->driFd, imesa->hHWContext, flags);
756
757
758 /* Note contention for throttling hint
759 */
760 imesa->any_contend = 1;
761
762 /* If the window moved, may need to set a new cliprect now.
763 *
764 * NOTE: This releases and regains the hw lock, so all state
765 * checking must be done *after* this call:
766 */
767 DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
768
769
770
771
772 /* If we lost context, need to dump all registers to hardware.
773 * Note that we don't care about 2d contexts, even if they perform
774 * accelerated commands, so the DRI locking in the X server is even
775 * more broken than usual.
776 */
777 if (sarea->ctxOwner != me) {
778 imesa->dirty |= (SAVAGE_UPLOAD_LOCAL |
779 SAVAGE_UPLOAD_GLOBAL |
780 SAVAGE_UPLOAD_FOGTBL |
781 SAVAGE_UPLOAD_TEX0 |
782 SAVAGE_UPLOAD_TEX1 |
783 SAVAGE_UPLOAD_TEXGLOBAL);
784 imesa->lostContext = GL_TRUE;
785 sarea->ctxOwner = me;
786 }
787
788 for (heap = 0; heap < imesa->lastTexHeap; ++heap) {
789 /* If a heap was changed, update its timestamp. Do this before
790 * DRI_AGE_TEXTURES updates the local_age. */
791 if (imesa->textureHeaps[heap] &&
792 imesa->textureHeaps[heap]->global_age[0] >
793 imesa->textureHeaps[heap]->local_age) {
794 if (timestamp == 0)
795 timestamp = savageEmitEventLocked(imesa, 0);
796 imesa->textureHeaps[heap]->timestamp = timestamp;
797 }
798 DRI_AGE_TEXTURES( imesa->textureHeaps[heap] );
799 }
800
801 if (dPriv->lastStamp != stamp)
802 savageXMesaWindowMoved( imesa );
803
804
805
806 }
807
808
809
810 static const struct __DriverAPIRec savageAPI = {
811 savageInitDriver,
812 savageDestroyScreen,
813 savageCreateContext,
814 savageDestroyContext,
815 savageCreateBuffer,
816 savageDestroyBuffer,
817 savageSwapBuffers,
818 savageMakeCurrent,
819 savageUnbindContext
820 };
821
822
823
824 #ifndef DRI_NEW_INTERFACE_ONLY
825 /*
826 * This is the (old) bootstrap function for the driver.
827 * The __driCreateScreen name is the symbol that libGL.so fetches.
828 * Return: pointer to a __DRIscreenPrivate.
829 */
830 void *__driCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
831 int numConfigs, __GLXvisualConfig *config)
832 {
833 __DRIscreenPrivate *psp;
834 psp = __driUtilCreateScreen(dpy, scrn, psc, numConfigs, config, &savageAPI);
835 return (void *) psp;
836 }
837 #endif /* DRI_NEW_INTERFACE_ONLY */
838
839
840
841 #ifdef USE_NEW_INTERFACE
842 static __GLcontextModes *
843 savageFillInModes( unsigned pixel_bits, unsigned depth_bits,
844 unsigned stencil_bits, GLboolean have_back_buffer )
845 {
846 __GLcontextModes * modes;
847 __GLcontextModes * m;
848 unsigned num_modes;
849 unsigned depth_buffer_factor;
850 unsigned back_buffer_factor;
851 GLenum fb_format;
852 GLenum fb_type;
853
854 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
855 * enough to add support. Basically, if a context is created with an
856 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
857 * will never be used.
858 *
859 * FK: What about drivers that don't use page flipping? Could they
860 * just expose GLX_SWAP_COPY_OML?
861 */
862 static const GLenum back_buffer_modes[] = {
863 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
864 };
865
866 u_int8_t depth_bits_array[2];
867 u_int8_t stencil_bits_array[2];
868
869
870 depth_bits_array[0] = depth_bits;
871 depth_bits_array[1] = depth_bits;
872
873 /* Just like with the accumulation buffer, always provide some modes
874 * with a stencil buffer. It will be a sw fallback, but some apps won't
875 * care about that.
876 */
877 stencil_bits_array[0] = 0;
878 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
879
880 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
881 back_buffer_factor = (have_back_buffer) ? 2 : 1;
882
883 num_modes = depth_buffer_factor * back_buffer_factor * 4;
884
885 if ( pixel_bits == 16 ) {
886 fb_format = GL_RGB;
887 fb_type = GL_UNSIGNED_SHORT_5_6_5;
888 }
889 else {
890 fb_format = GL_BGR;
891 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
892 }
893
894 modes = (*create_context_modes)( num_modes, sizeof( __GLcontextModes ) );
895 m = modes;
896 if ( ! driFillInModes( & m, fb_format, fb_type,
897 depth_bits_array, stencil_bits_array, depth_buffer_factor,
898 back_buffer_modes, back_buffer_factor,
899 GLX_TRUE_COLOR ) ) {
900 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
901 __func__, __LINE__ );
902 return NULL;
903 }
904
905 if ( ! driFillInModes( & m, fb_format, fb_type,
906 depth_bits_array, stencil_bits_array, depth_buffer_factor,
907 back_buffer_modes, back_buffer_factor,
908 GLX_DIRECT_COLOR ) ) {
909 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
910 __func__, __LINE__ );
911 return NULL;
912 }
913
914 /* Mark the visual as slow if there are "fake" stencil bits.
915 */
916 for ( m = modes ; m != NULL ; m = m->next ) {
917 if ( (m->stencilBits != 0) && (m->stencilBits != stencil_bits) ) {
918 m->visualRating = GLX_SLOW_CONFIG;
919 }
920 }
921
922 return modes;
923 }
924
925
926 /**
927 * This is the bootstrap function for the driver. libGL supplies all of the
928 * requisite information about the system, and the driver initializes itself.
929 * This routine also fills in the linked list pointed to by \c driver_modes
930 * with the \c __GLcontextModes that the driver can support for windows or
931 * pbuffers.
932 *
933 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
934 * failure.
935 */
936 PUBLIC
937 void * __driCreateNewScreen( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
938 const __GLcontextModes * modes,
939 const __DRIversion * ddx_version,
940 const __DRIversion * dri_version,
941 const __DRIversion * drm_version,
942 const __DRIframebuffer * frame_buffer,
943 drmAddress pSAREA, int fd,
944 int internal_api_version,
945 __GLcontextModes ** driver_modes )
946
947 {
948 __DRIscreenPrivate *psp;
949 static const __DRIversion ddx_expected = { 2, 0, 0 };
950 static const __DRIversion dri_expected = { 4, 0, 0 };
951 static const __DRIversion drm_expected = { 2, 1, 0 };
952
953 if ( ! driCheckDriDdxDrmVersions2( "Savage",
954 dri_version, & dri_expected,
955 ddx_version, & ddx_expected,
956 drm_version, & drm_expected ) ) {
957 return NULL;
958 }
959
960 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
961 ddx_version, dri_version, drm_version,
962 frame_buffer, pSAREA, fd,
963 internal_api_version, &savageAPI);
964 if ( psp != NULL ) {
965 create_context_modes = (PFNGLXCREATECONTEXTMODES)
966 glXGetProcAddress( (const GLubyte *) "__glXCreateContextModes" );
967 if ( create_context_modes != NULL ) {
968 SAVAGEDRIPtr dri_priv = (SAVAGEDRIPtr)psp->pDevPriv;
969 *driver_modes = savageFillInModes( dri_priv->cpp*8,
970 (dri_priv->cpp == 2) ? 16 : 24,
971 (dri_priv->cpp == 2) ? 0 : 8,
972 (dri_priv->backOffset != dri_priv->depthOffset) );
973 }
974 }
975
976 return (void *) psp;
977 }
978 #endif /* USE_NEW_INTERFACE */