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