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
,
212 viaScreenPrivate
*screen
= (viaScreenPrivate
*) driScrnPriv
->private;
215 GLboolean swStencil
= (mesaVis
->stencilBits
> 0 &&
216 mesaVis
->depthBits
!= 24);
217 GLboolean swAccum
= mesaVis
->accumRedBits
> 0;
220 /* KW: This needs work, disabled for now:
223 driDrawPriv
->driverPrivate
= (void *)
224 _mesa_create_framebuffer(mesaVis
,
225 GL_FALSE
, /* software depth buffer? */
227 mesaVis
->accumRedBits
> 0,
228 GL_FALSE
/* s/w alpha planes */);
230 return (driDrawPriv
->driverPrivate
!= NULL
);
235 struct gl_framebuffer
*fb
= _mesa_create_framebuffer(mesaVis
);
237 /* The front color, back color and depth renderbuffers are
238 * set up later in calculate_buffer_parameters().
239 * Only create/connect software-based buffers here.
243 /* This code _should_ be put to use. We have to move the
244 * viaRenderbuffer members out of the via_context structure.
245 * Those members should just be the renderbuffers hanging off the
246 * gl_framebuffer object.
248 /* XXX check/fix the offset/pitch parameters! */
250 driRenderbuffer
*frontRb
251 = driNewRenderbuffer(MESA_FORMAT_ARGB8888
, NULL
,
252 screen
->bytesPerPixel
,
253 0, screen
->width
, driDrawPriv
);
254 viaSetSpanFunctions(frontRb
, mesaVis
);
255 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontRb
->Base
);
258 if (mesaVis
->doubleBufferMode
) {
259 driRenderbuffer
*backRb
260 = driNewRenderbuffer(MESA_FORMAT_ARGB8888
, NULL
,
261 screen
->bytesPerPixel
,
262 0, screen
->width
, driDrawPriv
);
263 viaSetSpanFunctions(backRb
, mesaVis
);
264 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backRb
->Base
);
267 if (mesaVis
->depthBits
== 16) {
268 driRenderbuffer
*depthRb
269 = driNewRenderbuffer(MESA_FORMAT_Z16
, NULL
,
270 screen
->bytesPerPixel
,
271 0, screen
->width
, driDrawPriv
);
272 viaSetSpanFunctions(depthRb
, mesaVis
);
273 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
275 else if (mesaVis
->depthBits
== 24) {
276 driRenderbuffer
*depthRb
277 = driNewRenderbuffer(MESA_FORMAT_Z24_S8
, NULL
,
278 screen
->bytesPerPixel
,
279 0, screen
->width
, driDrawPriv
);
280 viaSetSpanFunctions(depthRb
, mesaVis
);
281 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
283 else if (mesaVis
->depthBits
== 32) {
284 driRenderbuffer
*depthRb
285 = driNewRenderbuffer(MESA_FORMAT_Z32
, NULL
,
286 screen
->bytesPerPixel
,
287 0, screen
->width
, driDrawPriv
);
288 viaSetSpanFunctions(depthRb
, mesaVis
);
289 _mesa_add_renderbuffer(fb
, BUFFER_DEPTH
, &depthRb
->Base
);
292 if (mesaVis
->stencilBits
> 0 && !swStencil
) {
293 driRenderbuffer
*stencilRb
294 = driNewRenderbuffer(MESA_FORMAT_S8
, NULL
,
295 screen
->bytesPerPixel
,
296 0, screen
->width
, driDrawPriv
);
297 viaSetSpanFunctions(stencilRb
, mesaVis
);
298 _mesa_add_renderbuffer(fb
, BUFFER_STENCIL
, &stencilRb
->Base
);
302 _mesa_add_soft_renderbuffers(fb
,
303 GL_FALSE
, /* color */
304 GL_FALSE
, /* depth */
307 GL_FALSE
, /* alpha */
309 driDrawPriv
->driverPrivate
= (void *) fb
;
311 return (driDrawPriv
->driverPrivate
!= NULL
);
317 viaDestroyBuffer(__DRIdrawablePrivate
*driDrawPriv
)
319 _mesa_reference_framebuffer((GLframebuffer
**)(&(driDrawPriv
->driverPrivate
)), NULL
);
322 static const __DRIconfig
**
323 viaFillInModes( __DRIscreenPrivate
*psp
,
324 unsigned pixel_bits
, GLboolean have_back_buffer
)
326 __DRIconfig
**configs
;
327 const unsigned back_buffer_factor
= (have_back_buffer
) ? 2 : 1;
331 /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
332 * enough to add support. Basically, if a context is created with an
333 * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
334 * will never be used.
336 static const GLenum back_buffer_modes
[] = {
337 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
/*, GLX_SWAP_COPY_OML */
340 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
343 static const uint8_t depth_bits_array
[4] = { 0, 16, 24, 32 };
344 static const uint8_t stencil_bits_array
[4] = { 0, 0, 8, 0 };
345 uint8_t msaa_samples_array
[1] = { 0 };
346 const unsigned depth_buffer_factor
= 3;
348 if ( pixel_bits
== 16 ) {
350 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
354 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
357 configs
= driCreateConfigs(fb_format
, fb_type
,
358 depth_bits_array
, stencil_bits_array
,
359 depth_buffer_factor
, back_buffer_modes
,
361 msaa_samples_array
, 1);
362 if (configs
== NULL
) {
363 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
368 return (const __DRIconfig
**) configs
;
373 * This is the driver specific part of the createNewScreen entry point.
375 * \todo maybe fold this into intelInitDriver
377 * \return the __GLcontextModes supported by this driver
379 static const __DRIconfig
**
380 viaInitScreen(__DRIscreenPrivate
*psp
)
382 static const __DRIversion ddx_expected
= { VIA_DRIDDX_VERSION_MAJOR
,
383 VIA_DRIDDX_VERSION_MINOR
,
384 VIA_DRIDDX_VERSION_PATCH
};
385 static const __DRIversion dri_expected
= { 4, 0, 0 };
386 static const __DRIversion drm_expected
= { 2, 3, 0 };
387 static const char *driver_name
= "Unichrome";
388 VIADRIPtr dri_priv
= (VIADRIPtr
) psp
->pDevPriv
;
390 if ( ! driCheckDriDdxDrmVersions2( driver_name
,
391 &psp
->dri_version
, & dri_expected
,
392 &psp
->ddx_version
, & ddx_expected
,
393 &psp
->drm_version
, & drm_expected
) )
396 if (!viaInitDriver(psp
))
399 return viaFillInModes( psp
, dri_priv
->bytesPerPixel
* 8, GL_TRUE
);
405 * Get information about previous buffer swaps.
408 getSwapInfo( __DRIdrawablePrivate
*dPriv
, __DRIswapInfo
* sInfo
)
410 struct via_context
*vmesa
;
412 if ( (dPriv
== NULL
) || (dPriv
->driContextPriv
== NULL
)
413 || (dPriv
->driContextPriv
->driverPrivate
== NULL
)
414 || (sInfo
== NULL
) ) {
418 vmesa
= (struct via_context
*) dPriv
->driContextPriv
->driverPrivate
;
419 sInfo
->swap_count
= vmesa
->swap_count
;
420 sInfo
->swap_ust
= vmesa
->swap_ust
;
421 sInfo
->swap_missed_count
= vmesa
->swap_missed_count
;
423 sInfo
->swap_missed_usage
= (sInfo
->swap_missed_count
!= 0)
424 ? driCalculateSwapUsage( dPriv
, 0, vmesa
->swap_missed_ust
)
430 const struct __DriverAPIRec driDriverAPI
= {
431 .InitScreen
= viaInitScreen
,
432 .DestroyScreen
= viaDestroyScreen
,
433 .CreateContext
= viaCreateContext
,
434 .DestroyContext
= viaDestroyContext
,
435 .CreateBuffer
= viaCreateBuffer
,
436 .DestroyBuffer
= viaDestroyBuffer
,
437 .SwapBuffers
= viaSwapBuffers
,
438 .MakeCurrent
= viaMakeCurrent
,
439 .UnbindContext
= viaUnbindContext
,
440 .GetSwapInfo
= getSwapInfo
,
441 .GetDrawableMSC
= driDrawableGetMSC32
,
442 .WaitForMSC
= driWaitForMSC32
,
444 .SwapBuffersMSC
= NULL