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.
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "main/matrix.h"
34 #include "main/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 PUBLIC
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 static drmBufMapPtr
via_create_empty_buffers(void)
69 retval
= (drmBufMapPtr
)MALLOC(sizeof(drmBufMap
));
70 if (retval
== NULL
) return NULL
;
71 memset(retval
, 0, sizeof(drmBufMap
));
73 retval
->list
= (drmBufPtr
)MALLOC(sizeof(drmBuf
) * VIA_DMA_BUF_NR
);
74 if (retval
->list
== NULL
) {
78 memset(retval
->list
, 0, sizeof(drmBuf
) * VIA_DMA_BUF_NR
);
82 static void via_free_empty_buffers( drmBufMapPtr bufs
)
84 if (bufs
&& bufs
->list
)
93 viaInitDriver(__DRIscreenPrivate
*sPriv
)
95 viaScreenPrivate
*viaScreen
;
96 VIADRIPtr gDRIPriv
= (VIADRIPtr
)sPriv
->pDevPriv
;
99 if (sPriv
->devPrivSize
!= sizeof(VIADRIRec
)) {
100 fprintf(stderr
,"\nERROR! sizeof(VIADRIRec) does not match passed size from device driver\n");
104 /* Allocate the private area */
105 viaScreen
= (viaScreenPrivate
*) CALLOC(sizeof(viaScreenPrivate
));
107 __driUtilMessage("viaInitDriver: alloc viaScreenPrivate struct failed");
111 /* parse information in __driConfigOptions */
112 driParseOptionInfo (&viaScreen
->optionCache
,
113 __driConfigOptions
, __driNConfigOptions
);
116 viaScreen
->driScrnPriv
= sPriv
;
117 sPriv
->private = (void *)viaScreen
;
119 viaScreen
->deviceID
= gDRIPriv
->deviceID
;
120 viaScreen
->width
= gDRIPriv
->width
;
121 viaScreen
->height
= gDRIPriv
->height
;
122 viaScreen
->mem
= gDRIPriv
->mem
;
123 viaScreen
->bitsPerPixel
= gDRIPriv
->bytesPerPixel
* 8;
124 viaScreen
->bytesPerPixel
= gDRIPriv
->bytesPerPixel
;
125 viaScreen
->fbOffset
= 0;
126 viaScreen
->fbSize
= gDRIPriv
->fbSize
;
127 viaScreen
->irqEnabled
= gDRIPriv
->irqEnabled
;
129 if (VIA_DEBUG
& DEBUG_DRI
) {
130 fprintf(stderr
, "deviceID = %08x\n", viaScreen
->deviceID
);
131 fprintf(stderr
, "width = %08x\n", viaScreen
->width
);
132 fprintf(stderr
, "height = %08x\n", viaScreen
->height
);
133 fprintf(stderr
, "cpp = %08x\n", viaScreen
->cpp
);
134 fprintf(stderr
, "fbOffset = %08x\n", viaScreen
->fbOffset
);
137 viaScreen
->bufs
= via_create_empty_buffers();
138 if (viaScreen
->bufs
== NULL
) {
139 __driUtilMessage("viaInitDriver: via_create_empty_buffers() failed");
144 if (drmMap(sPriv
->fd
,
145 gDRIPriv
->regs
.handle
,
147 &viaScreen
->reg
) != 0) {
149 sPriv
->private = NULL
;
150 __driUtilMessage("viaInitDriver: drmMap regs failed");
154 if (gDRIPriv
->agp
.size
) {
155 if (drmMap(sPriv
->fd
,
156 gDRIPriv
->agp
.handle
,
158 (drmAddress
*)&viaScreen
->agpLinearStart
) != 0) {
159 drmUnmap(viaScreen
->reg
, gDRIPriv
->regs
.size
);
161 sPriv
->private = NULL
;
162 __driUtilMessage("viaInitDriver: drmMap agp failed");
166 viaScreen
->agpBase
= drmAgpBase(sPriv
->fd
);
168 viaScreen
->agpLinearStart
= 0;
170 viaScreen
->sareaPrivOffset
= gDRIPriv
->sarea_priv_offset
;
173 viaScreen
->extensions
[i
++] = &driFrameTrackingExtension
.base
;
174 viaScreen
->extensions
[i
++] = &driReadDrawableExtension
;
175 if ( viaScreen
->irqEnabled
) {
176 viaScreen
->extensions
[i
++] = &driSwapControlExtension
.base
;
177 viaScreen
->extensions
[i
++] = &driMediaStreamCounterExtension
.base
;
180 viaScreen
->extensions
[i
++] = NULL
;
181 sPriv
->extensions
= viaScreen
->extensions
;
187 viaDestroyScreen(__DRIscreenPrivate
*sPriv
)
189 viaScreenPrivate
*viaScreen
= (viaScreenPrivate
*)sPriv
->private;
190 VIADRIPtr gDRIPriv
= (VIADRIPtr
)sPriv
->pDevPriv
;
192 drmUnmap(viaScreen
->reg
, gDRIPriv
->regs
.size
);
193 if (gDRIPriv
->agp
.size
)
194 drmUnmap(viaScreen
->agpLinearStart
, gDRIPriv
->agp
.size
);
196 via_free_empty_buffers(viaScreen
->bufs
);
198 driDestroyOptionInfo(&viaScreen
->optionCache
);
201 sPriv
->private = NULL
;
206 viaCreateBuffer(__DRIscreenPrivate
*driScrnPriv
,
207 __DRIdrawablePrivate
*driDrawPriv
,
208 const __GLcontextModes
*mesaVis
,
211 viaScreenPrivate
*screen
= (viaScreenPrivate
*) driScrnPriv
->private;
213 GLboolean swStencil
= (mesaVis
->stencilBits
> 0 &&
214 mesaVis
->depthBits
!= 24);
215 GLboolean swAccum
= mesaVis
->accumRedBits
> 0;
218 /* KW: This needs work, disabled for now:
221 driDrawPriv
->driverPrivate
= (void *)
222 _mesa_create_framebuffer(mesaVis
,
223 GL_FALSE
, /* software depth buffer? */
225 mesaVis
->accumRedBits
> 0,
226 GL_FALSE
/* s/w alpha planes */);
228 return (driDrawPriv
->driverPrivate
!= NULL
);
233 struct gl_framebuffer
*fb
= _mesa_create_framebuffer(mesaVis
);
235 /* The front color, back color and depth renderbuffers are
236 * set up later in calculate_buffer_parameters().
237 * Only create/connect software-based buffers here.
241 /* This code _should_ be put to use. We have to move the
242 * viaRenderbuffer members out of the via_context structure.
243 * Those members should just be the renderbuffers hanging off the
244 * gl_framebuffer object.
246 /* XXX check/fix the offset/pitch parameters! */
248 driRenderbuffer
*frontRb
249 = driNewRenderbuffer(GL_RGBA
, NULL
,
250 screen
->bytesPerPixel
,
251 0, screen
->width
, driDrawPriv
);
252 viaSetSpanFunctions(frontRb
, mesaVis
);
253 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontRb
->Base
);
256 if (mesaVis
->doubleBufferMode
) {
257 driRenderbuffer
*backRb
258 = driNewRenderbuffer(GL_RGBA
, NULL
,
259 screen
->bytesPerPixel
,
260 0, screen
->width
, driDrawPriv
);
261 viaSetSpanFunctions(backRb
, mesaVis
);
262 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backRb
->Base
);
265 if (mesaVis
->depthBits
== 16) {
266 driRenderbuffer
*depthRb
267 = driNewRenderbuffer(GL_DEPTH_COMPONENT16
, NULL
,
268 screen
->bytesPerPixel
,
269 0, screen
->width
, driDrawPriv
);
270 viaSetSpanFunctions(depthRb
, mesaVis
);
271 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
273 else if (mesaVis
->depthBits
== 24) {
274 driRenderbuffer
*depthRb
275 = driNewRenderbuffer(GL_DEPTH_COMPONENT24
, NULL
,
276 screen
->bytesPerPixel
,
277 0, screen
->width
, driDrawPriv
);
278 viaSetSpanFunctions(depthRb
, mesaVis
);
279 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
281 else if (mesaVis
->depthBits
== 32) {
282 driRenderbuffer
*depthRb
283 = driNewRenderbuffer(GL_DEPTH_COMPONENT32
, NULL
,
284 screen
->bytesPerPixel
,
285 0, screen
->width
, driDrawPriv
);
286 viaSetSpanFunctions(depthRb
, mesaVis
);
287 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
290 if (mesaVis
->stencilBits
> 0 && !swStencil
) {
291 driRenderbuffer
*stencilRb
292 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT
, NULL
,
293 screen
->bytesPerPixel
,
294 0, screen
->width
, driDrawPriv
);
295 viaSetSpanFunctions(stencilRb
, mesaVis
);
296 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &stencilRb
->Base
);
300 _mesa_add_soft_renderbuffers(fb
,
301 GL_FALSE
, /* color */
302 GL_FALSE
, /* depth */
305 GL_FALSE
, /* alpha */
307 driDrawPriv
->driverPrivate
= (void *) fb
;
309 return (driDrawPriv
->driverPrivate
!= NULL
);
315 viaDestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
317 _mesa_reference_framebuffer((GLframebuffer
**)(&(driDrawPriv
->driverPrivate
)), NULL
);
320 static const __DRIconfig
**
321 viaFillInModes( __DRIscreenPrivate
*psp
,
322 unsigned pixel_bits
, GLboolean have_back_buffer
)
324 __DRIconfig
**configs
;
325 const unsigned back_buffer_factor
= (have_back_buffer
) ? 2 : 1;
329 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
330 * enough to add support. Basically, if a context is created with an
331 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
332 * will never be used.
334 static const GLenum back_buffer_modes
[] = {
335 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
/*, GLX_SWAP_COPY_OML */
338 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
341 static const uint8_t depth_bits_array
[4] = { 0, 16, 24, 32 };
342 static const uint8_t stencil_bits_array
[4] = { 0, 0, 8, 0 };
343 uint8_t msaa_samples_array
[1] = { 0 };
344 const unsigned depth_buffer_factor
= 3;
346 if ( pixel_bits
== 16 ) {
348 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
352 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
355 configs
= driCreateConfigs(fb_format
, fb_type
,
356 depth_bits_array
, stencil_bits_array
,
357 depth_buffer_factor
, back_buffer_modes
,
359 msaa_samples_array
, 1);
360 if (configs
== NULL
) {
361 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
366 return (const __DRIconfig
**) configs
;
371 * This is the driver specific part of the createNewScreen entry point.
373 * \todo maybe fold this into intelInitDriver
375 * \return the __GLcontextModes supported by this driver
377 static const __DRIconfig
**
378 viaInitScreen(__DRIscreenPrivate
*psp
)
380 static const __DRIversion ddx_expected
= { VIA_DRIDDX_VERSION_MAJOR
,
381 VIA_DRIDDX_VERSION_MINOR
,
382 VIA_DRIDDX_VERSION_PATCH
};
383 static const __DRIversion dri_expected
= { 4, 0, 0 };
384 static const __DRIversion drm_expected
= { 2, 3, 0 };
385 static const char *driver_name
= "Unichrome";
386 VIADRIPtr dri_priv
= (VIADRIPtr
) psp
->pDevPriv
;
388 if ( ! driCheckDriDdxDrmVersions2( driver_name
,
389 &psp
->dri_version
, & dri_expected
,
390 &psp
->ddx_version
, & ddx_expected
,
391 &psp
->drm_version
, & drm_expected
) )
394 if (!viaInitDriver(psp
))
397 return viaFillInModes( psp
, dri_priv
->bytesPerPixel
* 8, GL_TRUE
);
403 * Get information about previous buffer swaps.
406 getSwapInfo( __DRIdrawablePrivate
*dPriv
, __DRIswapInfo
* sInfo
)
408 struct via_context
*vmesa
;
410 if ( (dPriv
== NULL
) || (dPriv
->driContextPriv
== NULL
)
411 || (dPriv
->driContextPriv
->driverPrivate
== NULL
)
412 || (sInfo
== NULL
) ) {
416 vmesa
= (struct via_context
*) dPriv
->driContextPriv
->driverPrivate
;
417 sInfo
->swap_count
= vmesa
->swap_count
;
418 sInfo
->swap_ust
= vmesa
->swap_ust
;
419 sInfo
->swap_missed_count
= vmesa
->swap_missed_count
;
421 sInfo
->swap_missed_usage
= (sInfo
->swap_missed_count
!= 0)
422 ? driCalculateSwapUsage( dPriv
, 0, vmesa
->swap_missed_ust
)
428 const struct __DriverAPIRec driDriverAPI
= {
429 .InitScreen
= viaInitScreen
,
430 .DestroyScreen
= viaDestroyScreen
,
431 .CreateContext
= viaCreateContext
,
432 .DestroyContext
= viaDestroyContext
,
433 .CreateBuffer
= viaCreateBuffer
,
434 .DestroyBuffer
= viaDestroyBuffer
,
435 .SwapBuffers
= viaSwapBuffers
,
436 .MakeCurrent
= viaMakeCurrent
,
437 .UnbindContext
= viaUnbindContext
,
438 .GetSwapInfo
= getSwapInfo
,
439 .GetDrawableMSC
= driDrawableGetMSC32
,
440 .WaitForMSC
= driWaitForMSC32
,
442 .SwapBuffersMSC
= NULL