2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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.
31 #include "framebuffer.h"
32 #include "renderbuffer.h"
34 #include "simple_list.h"
37 #include "via_state.h"
41 #include "via_ioctl.h"
42 #include "via_screen.h"
46 #include "GL/internal/dri_interface.h"
47 #include "drirenderbuffer.h"
51 const char __driConfigOptions
[] =
53 DRI_CONF_SECTION_PERFORMANCE
54 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0
)
56 DRI_CONF_SECTION_QUALITY
57 DRI_CONF_EXCESS_MIPMAP(false)
59 DRI_CONF_SECTION_DEBUG
60 DRI_CONF_NO_RAST(false)
63 static const GLuint __driNConfigOptions
= 3;
65 extern const struct dri_extension card_extensions
[];
67 static int getSwapInfo( __DRIdrawablePrivate
*dPriv
, __DRIswapInfo
* sInfo
);
69 static drmBufMapPtr
via_create_empty_buffers(void)
73 retval
= (drmBufMapPtr
)MALLOC(sizeof(drmBufMap
));
74 if (retval
== NULL
) return NULL
;
75 memset(retval
, 0, sizeof(drmBufMap
));
77 retval
->list
= (drmBufPtr
)MALLOC(sizeof(drmBuf
) * VIA_DMA_BUF_NR
);
78 if (retval
->list
== NULL
) {
82 memset(retval
->list
, 0, sizeof(drmBuf
) * VIA_DMA_BUF_NR
);
86 static void via_free_empty_buffers( drmBufMapPtr bufs
)
88 if (bufs
&& bufs
->list
)
97 viaInitDriver(__DRIscreenPrivate
*sPriv
)
99 viaScreenPrivate
*viaScreen
;
100 VIADRIPtr gDRIPriv
= (VIADRIPtr
)sPriv
->pDevPriv
;
101 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension
=
102 (PFNGLXSCRENABLEEXTENSIONPROC
) (*dri_interface
->getProcAddress("glxEnableExtension"));
103 void * const psc
= sPriv
->psc
->screenConfigs
;
105 if (sPriv
->devPrivSize
!= sizeof(VIADRIRec
)) {
106 fprintf(stderr
,"\nERROR! sizeof(VIADRIRec) does not match passed size from device driver\n");
110 /* Allocate the private area */
111 viaScreen
= (viaScreenPrivate
*) CALLOC(sizeof(viaScreenPrivate
));
113 __driUtilMessage("viaInitDriver: alloc viaScreenPrivate struct failed");
117 /* parse information in __driConfigOptions */
118 driParseOptionInfo (&viaScreen
->optionCache
,
119 __driConfigOptions
, __driNConfigOptions
);
122 viaScreen
->driScrnPriv
= sPriv
;
123 sPriv
->private = (void *)viaScreen
;
125 viaScreen
->deviceID
= gDRIPriv
->deviceID
;
126 viaScreen
->width
= gDRIPriv
->width
;
127 viaScreen
->height
= gDRIPriv
->height
;
128 viaScreen
->mem
= gDRIPriv
->mem
;
129 viaScreen
->bitsPerPixel
= gDRIPriv
->bytesPerPixel
* 8;
130 viaScreen
->bytesPerPixel
= gDRIPriv
->bytesPerPixel
;
131 viaScreen
->fbOffset
= 0;
132 viaScreen
->fbSize
= gDRIPriv
->fbSize
;
133 viaScreen
->irqEnabled
= gDRIPriv
->irqEnabled
;
135 if (VIA_DEBUG
& DEBUG_DRI
) {
136 fprintf(stderr
, "deviceID = %08x\n", viaScreen
->deviceID
);
137 fprintf(stderr
, "width = %08x\n", viaScreen
->width
);
138 fprintf(stderr
, "height = %08x\n", viaScreen
->height
);
139 fprintf(stderr
, "cpp = %08x\n", viaScreen
->cpp
);
140 fprintf(stderr
, "fbOffset = %08x\n", viaScreen
->fbOffset
);
143 viaScreen
->bufs
= via_create_empty_buffers();
144 if (viaScreen
->bufs
== NULL
) {
145 __driUtilMessage("viaInitDriver: via_create_empty_buffers() failed");
150 if (drmMap(sPriv
->fd
,
151 gDRIPriv
->regs
.handle
,
153 &viaScreen
->reg
) != 0) {
155 sPriv
->private = NULL
;
156 __driUtilMessage("viaInitDriver: drmMap regs failed");
160 if (gDRIPriv
->agp
.size
) {
161 if (drmMap(sPriv
->fd
,
162 gDRIPriv
->agp
.handle
,
164 (drmAddress
*)&viaScreen
->agpLinearStart
) != 0) {
165 drmUnmap(viaScreen
->reg
, gDRIPriv
->regs
.size
);
167 sPriv
->private = NULL
;
168 __driUtilMessage("viaInitDriver: drmMap agp failed");
172 viaScreen
->agpBase
= drmAgpBase(sPriv
->fd
);
174 viaScreen
->agpLinearStart
= 0;
176 viaScreen
->sareaPrivOffset
= gDRIPriv
->sarea_priv_offset
;
178 if ( glx_enable_extension
!= NULL
) {
179 if ( viaScreen
->irqEnabled
) {
180 (*glx_enable_extension
)( psc
, "GLX_SGI_swap_control" );
181 (*glx_enable_extension
)( psc
, "GLX_SGI_video_sync" );
182 (*glx_enable_extension
)( psc
, "GLX_MESA_swap_control" );
185 (*glx_enable_extension
)( psc
, "GLX_SGI_make_current_read" );
186 (*glx_enable_extension
)( psc
, "GLX_MESA_swap_frame_usage" );
193 viaDestroyScreen(__DRIscreenPrivate
*sPriv
)
195 viaScreenPrivate
*viaScreen
= (viaScreenPrivate
*)sPriv
->private;
196 VIADRIPtr gDRIPriv
= (VIADRIPtr
)sPriv
->pDevPriv
;
198 drmUnmap(viaScreen
->reg
, gDRIPriv
->regs
.size
);
199 if (gDRIPriv
->agp
.size
)
200 drmUnmap(viaScreen
->agpLinearStart
, gDRIPriv
->agp
.size
);
202 via_free_empty_buffers(viaScreen
->bufs
);
204 driDestroyOptionInfo(&viaScreen
->optionCache
);
207 sPriv
->private = NULL
;
212 viaCreateBuffer(__DRIscreenPrivate
*driScrnPriv
,
213 __DRIdrawablePrivate
*driDrawPriv
,
214 const __GLcontextModes
*mesaVis
,
217 viaScreenPrivate
*screen
= (viaScreenPrivate
*) driScrnPriv
->private;
219 GLboolean swStencil
= (mesaVis
->stencilBits
> 0 &&
220 mesaVis
->depthBits
!= 24);
221 GLboolean swAccum
= mesaVis
->accumRedBits
> 0;
224 /* KW: This needs work, disabled for now:
227 driDrawPriv
->driverPrivate
= (void *)
228 _mesa_create_framebuffer(mesaVis
,
229 GL_FALSE
, /* software depth buffer? */
231 mesaVis
->accumRedBits
> 0,
232 GL_FALSE
/* s/w alpha planes */);
234 return (driDrawPriv
->driverPrivate
!= NULL
);
239 struct gl_framebuffer
*fb
= _mesa_create_framebuffer(mesaVis
);
241 /* The front color, back color and depth renderbuffers are
242 * set up later in calculate_buffer_parameters().
243 * Only create/connect software-based buffers here.
247 /* This code _should_ be put to use. We have to move the
248 * viaRenderbuffer members out of the via_context structure.
249 * Those members should just be the renderbuffers hanging off the
250 * gl_framebuffer object.
252 /* XXX check/fix the offset/pitch parameters! */
254 driRenderbuffer
*frontRb
255 = driNewRenderbuffer(GL_RGBA
, NULL
,
256 screen
->bytesPerPixel
,
257 0, screen
->width
, driDrawPriv
);
258 viaSetSpanFunctions(frontRb
, mesaVis
);
259 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontRb
->Base
);
262 if (mesaVis
->doubleBufferMode
) {
263 driRenderbuffer
*backRb
264 = driNewRenderbuffer(GL_RGBA
, NULL
,
265 screen
->bytesPerPixel
,
266 0, screen
->width
, driDrawPriv
);
267 viaSetSpanFunctions(backRb
, mesaVis
);
268 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backRb
->Base
);
271 if (mesaVis
->depthBits
== 16) {
272 driRenderbuffer
*depthRb
273 = driNewRenderbuffer(GL_DEPTH_COMPONENT16
, NULL
,
274 screen
->bytesPerPixel
,
275 0, screen
->width
, driDrawPriv
);
276 viaSetSpanFunctions(depthRb
, mesaVis
);
277 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
279 else if (mesaVis
->depthBits
== 24) {
280 driRenderbuffer
*depthRb
281 = driNewRenderbuffer(GL_DEPTH_COMPONENT24
, NULL
,
282 screen
->bytesPerPixel
,
283 0, screen
->width
, driDrawPriv
);
284 viaSetSpanFunctions(depthRb
, mesaVis
);
285 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
287 else if (mesaVis
->depthBits
== 32) {
288 driRenderbuffer
*depthRb
289 = driNewRenderbuffer(GL_DEPTH_COMPONENT32
, NULL
,
290 screen
->bytesPerPixel
,
291 0, screen
->width
, driDrawPriv
);
292 viaSetSpanFunctions(depthRb
, mesaVis
);
293 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
296 if (mesaVis
->stencilBits
> 0 && !swStencil
) {
297 driRenderbuffer
*stencilRb
298 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT
, NULL
,
299 screen
->bytesPerPixel
,
300 0, screen
->width
, driDrawPriv
);
301 viaSetSpanFunctions(stencilRb
, mesaVis
);
302 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &stencilRb
->Base
);
306 _mesa_add_soft_renderbuffers(fb
,
307 GL_FALSE
, /* color */
308 GL_FALSE
, /* depth */
311 GL_FALSE
, /* alpha */
313 driDrawPriv
->driverPrivate
= (void *) fb
;
315 return (driDrawPriv
->driverPrivate
!= NULL
);
321 viaDestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
323 _mesa_unreference_framebuffer((GLframebuffer
**)(&(driDrawPriv
->driverPrivate
)));
328 static struct __DriverAPIRec viaAPI
= {
329 .InitDriver
= viaInitDriver
,
330 .DestroyScreen
= viaDestroyScreen
,
331 .CreateContext
= viaCreateContext
,
332 .DestroyContext
= viaDestroyContext
,
333 .CreateBuffer
= viaCreateBuffer
,
334 .DestroyBuffer
= viaDestroyBuffer
,
335 .SwapBuffers
= viaSwapBuffers
,
336 .MakeCurrent
= viaMakeCurrent
,
337 .UnbindContext
= viaUnbindContext
,
338 .GetSwapInfo
= getSwapInfo
,
339 .GetMSC
= driGetMSC32
,
340 .WaitForMSC
= driWaitForMSC32
,
342 .SwapBuffersMSC
= NULL
346 static __GLcontextModes
*
347 viaFillInModes( unsigned pixel_bits
, GLboolean have_back_buffer
)
349 __GLcontextModes
* modes
;
350 __GLcontextModes
* m
;
352 const unsigned back_buffer_factor
= (have_back_buffer
) ? 2 : 1;
356 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
357 * enough to add support. Basically, if a context is created with an
358 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
359 * will never be used.
361 static const GLenum back_buffer_modes
[] = {
362 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
/*, GLX_SWAP_COPY_OML */
365 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
368 static const uint8_t depth_bits_array
[4] = { 0, 16, 24, 32 };
369 static const uint8_t stencil_bits_array
[4] = { 0, 0, 8, 0 };
370 const unsigned depth_buffer_factor
= 3;
373 num_modes
= depth_buffer_factor
* back_buffer_factor
* 4;
375 if ( pixel_bits
== 16 ) {
377 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
381 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
384 modes
= (*dri_interface
->createContextModes
)( num_modes
, sizeof( __GLcontextModes
) );
386 if ( ! driFillInModes( & m
, fb_format
, fb_type
,
387 depth_bits_array
, stencil_bits_array
,
389 back_buffer_modes
, back_buffer_factor
,
391 fprintf( stderr
, "[%s:%u] Error creating FBConfig!\n",
392 __func__
, __LINE__
);
396 if ( ! driFillInModes( & m
, fb_format
, fb_type
,
397 depth_bits_array
, stencil_bits_array
,
399 back_buffer_modes
, back_buffer_factor
,
400 GLX_DIRECT_COLOR
) ) {
401 fprintf( stderr
, "[%s:%u] Error creating FBConfig!\n",
402 __func__
, __LINE__
);
411 * This is the bootstrap function for the driver. libGL supplies all of the
412 * requisite information about the system, and the driver initializes itself.
413 * This routine also fills in the linked list pointed to by \c driver_modes
414 * with the \c __GLcontextModes that the driver can support for windows or
417 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
421 void * __driCreateNewScreen_20050727( __DRInativeDisplay
*dpy
, int scrn
,
423 const __GLcontextModes
* modes
,
424 const __DRIversion
* ddx_version
,
425 const __DRIversion
* dri_version
,
426 const __DRIversion
* drm_version
,
427 const __DRIframebuffer
* frame_buffer
,
428 drmAddress pSAREA
, int fd
,
429 int internal_api_version
,
430 const __DRIinterfaceMethods
* interface
,
431 __GLcontextModes
** driver_modes
)
434 __DRIscreenPrivate
*psp
;
435 static const __DRIversion ddx_expected
= { VIA_DRIDDX_VERSION_MAJOR
,
436 VIA_DRIDDX_VERSION_MINOR
,
437 VIA_DRIDDX_VERSION_PATCH
};
438 static const __DRIversion dri_expected
= { 4, 0, 0 };
439 static const __DRIversion drm_expected
= { 2, 3, 0 };
440 static const char *driver_name
= "Unichrome";
442 dri_interface
= interface
;
444 if ( ! driCheckDriDdxDrmVersions2( driver_name
,
445 dri_version
, & dri_expected
,
446 ddx_version
, & ddx_expected
,
447 drm_version
, & drm_expected
) ) {
451 psp
= __driUtilCreateNewScreen(dpy
, scrn
, psc
, NULL
,
452 ddx_version
, dri_version
, drm_version
,
453 frame_buffer
, pSAREA
, fd
,
454 internal_api_version
, &viaAPI
);
456 VIADRIPtr dri_priv
= (VIADRIPtr
) psp
->pDevPriv
;
457 *driver_modes
= viaFillInModes( dri_priv
->bytesPerPixel
* 8,
460 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
461 * enable the extensions. It just makes sure that all the dispatch offsets for all
462 * the extensions that *might* be enables are known. This is needed because the
463 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
464 * enable the extensions until we have a context pointer.
466 * Hello chicken. Hello egg. How are you two today?
468 driInitExtensions( NULL
, card_extensions
, GL_FALSE
);
476 * Get information about previous buffer swaps.
479 getSwapInfo( __DRIdrawablePrivate
*dPriv
, __DRIswapInfo
* sInfo
)
481 struct via_context
*vmesa
;
483 if ( (dPriv
== NULL
) || (dPriv
->driContextPriv
== NULL
)
484 || (dPriv
->driContextPriv
->driverPrivate
== NULL
)
485 || (sInfo
== NULL
) ) {
489 vmesa
= (struct via_context
*) dPriv
->driContextPriv
->driverPrivate
;
490 sInfo
->swap_count
= vmesa
->swap_count
;
491 sInfo
->swap_ust
= vmesa
->swap_ust
;
492 sInfo
->swap_missed_count
= vmesa
->swap_missed_count
;
494 sInfo
->swap_missed_usage
= (sInfo
->swap_missed_count
!= 0)
495 ? driCalculateSwapUsage( dPriv
, 0, vmesa
->swap_missed_ust
)