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"
48 /* Radeon configuration
52 const char __driConfigOptions
[] =
54 DRI_CONF_SECTION_PERFORMANCE
55 DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS
)
56 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0
)
58 DRI_CONF_SECTION_DEBUG
59 DRI_CONF_NO_RAST(false)
62 static const GLuint __driNConfigOptions
= 3;
64 extern const struct dri_extension card_extensions
[];
66 static int getSwapInfo( __DRIdrawablePrivate
*dPriv
, __DRIswapInfo
* sInfo
);
68 static drmBufMapPtr
via_create_empty_buffers(void)
72 retval
= (drmBufMapPtr
)MALLOC(sizeof(drmBufMap
));
73 if (retval
== NULL
) return NULL
;
74 memset(retval
, 0, sizeof(drmBufMap
));
76 retval
->list
= (drmBufPtr
)MALLOC(sizeof(drmBuf
) * VIA_DMA_BUF_NR
);
77 if (retval
->list
== NULL
) {
81 memset(retval
->list
, 0, sizeof(drmBuf
) * VIA_DMA_BUF_NR
);
85 static void via_free_empty_buffers( drmBufMapPtr bufs
)
87 if (bufs
&& bufs
->list
)
96 viaInitDriver(__DRIscreenPrivate
*sPriv
)
98 viaScreenPrivate
*viaScreen
;
99 VIADRIPtr gDRIPriv
= (VIADRIPtr
)sPriv
->pDevPriv
;
100 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension
=
101 (PFNGLXSCRENABLEEXTENSIONPROC
) (*dri_interface
->getProcAddress("glxEnableExtension"));
102 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
;
134 viaScreen
->irqEnabled
= 1;
136 if (VIA_DEBUG
& DEBUG_DRI
) {
137 fprintf(stderr
, "deviceID = %08x\n", viaScreen
->deviceID
);
138 fprintf(stderr
, "width = %08x\n", viaScreen
->width
);
139 fprintf(stderr
, "height = %08x\n", viaScreen
->height
);
140 fprintf(stderr
, "cpp = %08x\n", viaScreen
->cpp
);
141 fprintf(stderr
, "fbOffset = %08x\n", viaScreen
->fbOffset
);
144 viaScreen
->bufs
= via_create_empty_buffers();
145 if (viaScreen
->bufs
== NULL
) {
146 __driUtilMessage("viaInitDriver: via_create_empty_buffers() failed");
151 if (drmMap(sPriv
->fd
,
152 gDRIPriv
->regs
.handle
,
156 sPriv
->private = NULL
;
157 __driUtilMessage("viaInitDriver: drmMap regs failed");
161 if (gDRIPriv
->agp
.size
) {
162 if (drmMap(sPriv
->fd
,
163 gDRIPriv
->agp
.handle
,
165 (drmAddress
*)&viaScreen
->agpLinearStart
) != 0) {
166 drmUnmap(viaScreen
->reg
, gDRIPriv
->agp
.size
);
168 sPriv
->private = NULL
;
169 __driUtilMessage("viaInitDriver: drmMap agp failed");
172 viaScreen
->agpBase
= (GLuint
*)gDRIPriv
->agp
.handle
;
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_MESA_swap_frame_usage" );
192 viaDestroyScreen(__DRIscreenPrivate
*sPriv
)
194 viaScreenPrivate
*viaScreen
= (viaScreenPrivate
*)sPriv
->private;
195 VIADRIPtr gDRIPriv
= (VIADRIPtr
)sPriv
->pDevPriv
;
197 drmUnmap(viaScreen
->reg
, gDRIPriv
->regs
.size
);
198 if (gDRIPriv
->agp
.size
)
199 drmUnmap(viaScreen
->agpLinearStart
, gDRIPriv
->agp
.size
);
201 via_free_empty_buffers(viaScreen
->bufs
);
204 sPriv
->private = NULL
;
209 viaCreateBuffer(__DRIscreenPrivate
*driScrnPriv
,
210 __DRIdrawablePrivate
*driDrawPriv
,
211 const __GLcontextModes
*mesaVis
,
214 viaScreenPrivate
*screen
= (viaScreenPrivate
*) driScrnPriv
->private;
216 GLboolean swStencil
= (mesaVis
->stencilBits
> 0 &&
217 mesaVis
->depthBits
!= 24);
221 /* KW: This needs work, disabled for now:
224 driDrawPriv
->driverPrivate
= (void *)
225 _mesa_create_framebuffer(mesaVis
,
226 GL_FALSE
, /* software depth buffer? */
228 mesaVis
->accumRedBits
> 0,
229 GL_FALSE
/* s/w alpha planes */);
231 return (driDrawPriv
->driverPrivate
!= NULL
);
237 driDrawPriv
->driverPrivate
= (void *)
238 _mesa_create_framebuffer(mesaVis
,
239 GL_FALSE
, /* software depth buffer? */
241 mesaVis
->accumRedBits
> 0,
242 GL_FALSE
/* s/w alpha planes */);
244 struct gl_framebuffer
*fb
= _mesa_create_framebuffer(mesaVis
);
246 /* XXX check/fix the offset/pitch parameters! */
248 driRenderbuffer
*frontRb
249 = driNewRenderbuffer(GL_RGBA
, screen
->bytesPerPixel
,
251 viaSetSpanFunctions(frontRb
, mesaVis
);
252 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontRb
->Base
);
255 if (mesaVis
->doubleBufferMode
) {
256 driRenderbuffer
*backRb
257 = driNewRenderbuffer(GL_RGBA
, screen
->bytesPerPixel
,
259 viaSetSpanFunctions(backRb
, mesaVis
);
260 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backRb
->Base
);
263 if (mesaVis
->depthBits
== 16) {
264 driRenderbuffer
*depthRb
265 = driNewRenderbuffer(GL_DEPTH_COMPONENT16
, screen
->bytesPerPixel
,
267 viaSetSpanFunctions(depthRb
, mesaVis
);
268 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
270 else if (mesaVis
->depthBits
== 24) {
271 driRenderbuffer
*depthRb
272 = driNewRenderbuffer(GL_DEPTH_COMPONENT24
, screen
->bytesPerPixel
,
274 viaSetSpanFunctions(depthRb
, mesaVis
);
275 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
277 else if (mesaVis
->depthBits
== 32) {
278 driRenderbuffer
*depthRb
279 = driNewRenderbuffer(GL_DEPTH_COMPONENT32
, screen
->bytesPerPixel
,
281 viaSetSpanFunctions(depthRb
, mesaVis
);
282 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
285 if (mesaVis
->stencilBits
> 0 && !swStencil
) {
286 driRenderbuffer
*stencilRb
287 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT
, screen
->bytesPerPixel
,
289 viaSetSpanFunctions(stencilRb
, mesaVis
);
290 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &stencilRb
->Base
);
293 _mesa_add_soft_renderbuffers(fb
,
294 GL_FALSE
, /* color */
295 GL_FALSE
, /* depth */
297 mesaVis
->accumRedBits
> 0,
298 GL_FALSE
, /* alpha */
300 driDrawPriv
->driverPrivate
= (void *) fb
;
302 return (driDrawPriv
->driverPrivate
!= NULL
);
308 viaDestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
310 _mesa_destroy_framebuffer((GLframebuffer
*)(driDrawPriv
->driverPrivate
));
315 static struct __DriverAPIRec viaAPI
= {
316 .InitDriver
= viaInitDriver
,
317 .DestroyScreen
= viaDestroyScreen
,
318 .CreateContext
= viaCreateContext
,
319 .DestroyContext
= viaDestroyContext
,
320 .CreateBuffer
= viaCreateBuffer
,
321 .DestroyBuffer
= viaDestroyBuffer
,
322 .SwapBuffers
= viaSwapBuffers
,
323 .MakeCurrent
= viaMakeCurrent
,
324 .UnbindContext
= viaUnbindContext
,
325 .GetSwapInfo
= getSwapInfo
,
326 .GetMSC
= driGetMSC32
,
327 .WaitForMSC
= driWaitForMSC32
,
329 .SwapBuffersMSC
= NULL
333 static __GLcontextModes
*
334 viaFillInModes( unsigned pixel_bits
, GLboolean have_back_buffer
)
336 __GLcontextModes
* modes
;
337 __GLcontextModes
* m
;
339 const unsigned back_buffer_factor
= (have_back_buffer
) ? 2 : 1;
343 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
344 * enough to add support. Basically, if a context is created with an
345 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
346 * will never be used.
348 static const GLenum back_buffer_modes
[] = {
349 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
/*, GLX_SWAP_COPY_OML */
352 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
355 static const u_int8_t depth_bits_array
[4] = { 0, 16, 24, 32 };
356 static const u_int8_t stencil_bits_array
[4] = { 0, 0, 8, 0 };
357 const unsigned depth_buffer_factor
= 3;
360 num_modes
= depth_buffer_factor
* back_buffer_factor
* 4;
362 if ( pixel_bits
== 16 ) {
364 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
368 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
371 modes
= (*dri_interface
->createContextModes
)( num_modes
, sizeof( __GLcontextModes
) );
373 if ( ! driFillInModes( & m
, fb_format
, fb_type
,
374 depth_bits_array
, stencil_bits_array
,
376 back_buffer_modes
, back_buffer_factor
,
378 fprintf( stderr
, "[%s:%u] Error creating FBConfig!\n",
379 __func__
, __LINE__
);
383 if ( ! driFillInModes( & m
, fb_format
, fb_type
,
384 depth_bits_array
, stencil_bits_array
,
386 back_buffer_modes
, back_buffer_factor
,
387 GLX_DIRECT_COLOR
) ) {
388 fprintf( stderr
, "[%s:%u] Error creating FBConfig!\n",
389 __func__
, __LINE__
);
398 * This is the bootstrap function for the driver. libGL supplies all of the
399 * requisite information about the system, and the driver initializes itself.
400 * This routine also fills in the linked list pointed to by \c driver_modes
401 * with the \c __GLcontextModes that the driver can support for windows or
404 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
408 void * __driCreateNewScreen_20050727( __DRInativeDisplay
*dpy
, int scrn
,
410 const __GLcontextModes
* modes
,
411 const __DRIversion
* ddx_version
,
412 const __DRIversion
* dri_version
,
413 const __DRIversion
* drm_version
,
414 const __DRIframebuffer
* frame_buffer
,
415 drmAddress pSAREA
, int fd
,
416 int internal_api_version
,
417 const __DRIinterfaceMethods
* interface
,
418 __GLcontextModes
** driver_modes
)
421 __DRIscreenPrivate
*psp
;
422 static const __DRIversion ddx_expected
= { 4, 0, 0 };
423 static const __DRIversion dri_expected
= { 4, 0, 0 };
424 static const __DRIversion drm_expected
= { 2, 3, 0 };
426 dri_interface
= interface
;
428 if ( ! driCheckDriDdxDrmVersions2( "Unichrome",
429 dri_version
, & dri_expected
,
430 ddx_version
, & ddx_expected
,
431 drm_version
, & drm_expected
) ) {
435 psp
= __driUtilCreateNewScreen(dpy
, scrn
, psc
, NULL
,
436 ddx_version
, dri_version
, drm_version
,
437 frame_buffer
, pSAREA
, fd
,
438 internal_api_version
, &viaAPI
);
440 VIADRIPtr dri_priv
= (VIADRIPtr
) psp
->pDevPriv
;
441 *driver_modes
= viaFillInModes( dri_priv
->bytesPerPixel
* 8,
444 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
445 * enable the extensions. It just makes sure that all the dispatch offsets for all
446 * the extensions that *might* be enables are known. This is needed because the
447 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
448 * enable the extensions until we have a context pointer.
450 * Hello chicken. Hello egg. How are you two today?
452 driInitExtensions( NULL
, card_extensions
, GL_FALSE
);
455 fprintf(stderr
, "%s - succeeded\n", __FUNCTION__
);
461 * Get information about previous buffer swaps.
464 getSwapInfo( __DRIdrawablePrivate
*dPriv
, __DRIswapInfo
* sInfo
)
466 struct via_context
*vmesa
;
468 if ( (dPriv
== NULL
) || (dPriv
->driContextPriv
== NULL
)
469 || (dPriv
->driContextPriv
->driverPrivate
== NULL
)
470 || (sInfo
== NULL
) ) {
474 vmesa
= (struct via_context
*) dPriv
->driContextPriv
->driverPrivate
;
475 sInfo
->swap_count
= vmesa
->swap_count
;
476 sInfo
->swap_ust
= vmesa
->swap_ust
;
477 sInfo
->swap_missed_count
= vmesa
->swap_missed_count
;
479 sInfo
->swap_missed_usage
= (sInfo
->swap_missed_count
!= 0)
480 ? driCalculateSwapUsage( dPriv
, 0, vmesa
->swap_missed_ust
)