Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / mesa / drivers / dri / unichrome / via_screen.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 #include <stdio.h>
26
27 #include "dri_util.h"
28 #include "utils.h"
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"
35 #include "vblank.h"
36
37 #include "via_state.h"
38 #include "via_tex.h"
39 #include "via_span.h"
40 #include "via_tris.h"
41 #include "via_ioctl.h"
42 #include "via_screen.h"
43 #include "via_fb.h"
44 #include "via_dri.h"
45
46 #include "GL/internal/dri_interface.h"
47 #include "drirenderbuffer.h"
48
49 #include "xmlpool.h"
50
51 PUBLIC const char __driConfigOptions[] =
52 DRI_CONF_BEGIN
53 DRI_CONF_SECTION_PERFORMANCE
54 DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
55 DRI_CONF_SECTION_END
56 DRI_CONF_SECTION_QUALITY
57 DRI_CONF_EXCESS_MIPMAP(false)
58 DRI_CONF_SECTION_END
59 DRI_CONF_SECTION_DEBUG
60 DRI_CONF_NO_RAST(false)
61 DRI_CONF_SECTION_END
62 DRI_CONF_END;
63 static const GLuint __driNConfigOptions = 3;
64
65 extern const struct dri_extension card_extensions[];
66
67 static drmBufMapPtr via_create_empty_buffers(void)
68 {
69 drmBufMapPtr retval;
70
71 retval = (drmBufMapPtr)MALLOC(sizeof(drmBufMap));
72 if (retval == NULL) return NULL;
73 memset(retval, 0, sizeof(drmBufMap));
74
75 retval->list = (drmBufPtr)MALLOC(sizeof(drmBuf) * VIA_DMA_BUF_NR);
76 if (retval->list == NULL) {
77 FREE(retval);
78 return NULL;
79 }
80 memset(retval->list, 0, sizeof(drmBuf) * VIA_DMA_BUF_NR);
81 return retval;
82 }
83
84 static void via_free_empty_buffers( drmBufMapPtr bufs )
85 {
86 if (bufs && bufs->list)
87 FREE(bufs->list);
88
89 if (bufs)
90 FREE(bufs);
91 }
92
93
94 static GLboolean
95 viaInitDriver(__DRIscreenPrivate *sPriv)
96 {
97 viaScreenPrivate *viaScreen;
98 VIADRIPtr gDRIPriv = (VIADRIPtr)sPriv->pDevPriv;
99 int i;
100
101 if (sPriv->devPrivSize != sizeof(VIADRIRec)) {
102 fprintf(stderr,"\nERROR! sizeof(VIADRIRec) does not match passed size from device driver\n");
103 return GL_FALSE;
104 }
105
106 /* Allocate the private area */
107 viaScreen = (viaScreenPrivate *) CALLOC(sizeof(viaScreenPrivate));
108 if (!viaScreen) {
109 __driUtilMessage("viaInitDriver: alloc viaScreenPrivate struct failed");
110 return GL_FALSE;
111 }
112
113 /* parse information in __driConfigOptions */
114 driParseOptionInfo (&viaScreen->optionCache,
115 __driConfigOptions, __driNConfigOptions);
116
117
118 viaScreen->driScrnPriv = sPriv;
119 sPriv->private = (void *)viaScreen;
120
121 viaScreen->deviceID = gDRIPriv->deviceID;
122 viaScreen->width = gDRIPriv->width;
123 viaScreen->height = gDRIPriv->height;
124 viaScreen->mem = gDRIPriv->mem;
125 viaScreen->bitsPerPixel = gDRIPriv->bytesPerPixel * 8;
126 viaScreen->bytesPerPixel = gDRIPriv->bytesPerPixel;
127 viaScreen->fbOffset = 0;
128 viaScreen->fbSize = gDRIPriv->fbSize;
129 viaScreen->irqEnabled = gDRIPriv->irqEnabled;
130
131 if (VIA_DEBUG & DEBUG_DRI) {
132 fprintf(stderr, "deviceID = %08x\n", viaScreen->deviceID);
133 fprintf(stderr, "width = %08x\n", viaScreen->width);
134 fprintf(stderr, "height = %08x\n", viaScreen->height);
135 fprintf(stderr, "cpp = %08x\n", viaScreen->cpp);
136 fprintf(stderr, "fbOffset = %08x\n", viaScreen->fbOffset);
137 }
138
139 viaScreen->bufs = via_create_empty_buffers();
140 if (viaScreen->bufs == NULL) {
141 __driUtilMessage("viaInitDriver: via_create_empty_buffers() failed");
142 FREE(viaScreen);
143 return GL_FALSE;
144 }
145
146 if (drmMap(sPriv->fd,
147 gDRIPriv->regs.handle,
148 gDRIPriv->regs.size,
149 &viaScreen->reg) != 0) {
150 FREE(viaScreen);
151 sPriv->private = NULL;
152 __driUtilMessage("viaInitDriver: drmMap regs failed");
153 return GL_FALSE;
154 }
155
156 if (gDRIPriv->agp.size) {
157 if (drmMap(sPriv->fd,
158 gDRIPriv->agp.handle,
159 gDRIPriv->agp.size,
160 (drmAddress *)&viaScreen->agpLinearStart) != 0) {
161 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
162 FREE(viaScreen);
163 sPriv->private = NULL;
164 __driUtilMessage("viaInitDriver: drmMap agp failed");
165 return GL_FALSE;
166 }
167
168 viaScreen->agpBase = drmAgpBase(sPriv->fd);
169 } else
170 viaScreen->agpLinearStart = 0;
171
172 viaScreen->sareaPrivOffset = gDRIPriv->sarea_priv_offset;
173
174 i = 0;
175 viaScreen->extensions[i++] = &driFrameTrackingExtension.base;
176 viaScreen->extensions[i++] = &driReadDrawableExtension;
177 if ( viaScreen->irqEnabled ) {
178 viaScreen->extensions[i++] = &driSwapControlExtension.base;
179 viaScreen->extensions[i++] = &driMediaStreamCounterExtension.base;
180 }
181
182 viaScreen->extensions[i++] = NULL;
183 sPriv->extensions = viaScreen->extensions;
184
185 return GL_TRUE;
186 }
187
188 static void
189 viaDestroyScreen(__DRIscreenPrivate *sPriv)
190 {
191 viaScreenPrivate *viaScreen = (viaScreenPrivate *)sPriv->private;
192 VIADRIPtr gDRIPriv = (VIADRIPtr)sPriv->pDevPriv;
193
194 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
195 if (gDRIPriv->agp.size)
196 drmUnmap(viaScreen->agpLinearStart, gDRIPriv->agp.size);
197
198 via_free_empty_buffers(viaScreen->bufs);
199
200 driDestroyOptionInfo(&viaScreen->optionCache);
201
202 FREE(viaScreen);
203 sPriv->private = NULL;
204 }
205
206
207 static GLboolean
208 viaCreateBuffer(__DRIscreenPrivate *driScrnPriv,
209 __DRIdrawablePrivate *driDrawPriv,
210 const __GLcontextModes *mesaVis,
211 GLboolean isPixmap)
212 {
213 viaScreenPrivate *screen = (viaScreenPrivate *) driScrnPriv->private;
214
215 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
216 mesaVis->depthBits != 24);
217 GLboolean swAccum = mesaVis->accumRedBits > 0;
218
219 if (isPixmap) {
220 /* KW: This needs work, disabled for now:
221 */
222 #if 0
223 driDrawPriv->driverPrivate = (void *)
224 _mesa_create_framebuffer(mesaVis,
225 GL_FALSE, /* software depth buffer? */
226 swStencil,
227 mesaVis->accumRedBits > 0,
228 GL_FALSE /* s/w alpha planes */);
229
230 return (driDrawPriv->driverPrivate != NULL);
231 #endif
232 return GL_FALSE;
233 }
234 else {
235 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
236
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.
240 */
241
242 #if 000
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.
247 */
248 /* XXX check/fix the offset/pitch parameters! */
249 {
250 driRenderbuffer *frontRb
251 = driNewRenderbuffer(GL_RGBA, NULL,
252 screen->bytesPerPixel,
253 0, screen->width, driDrawPriv);
254 viaSetSpanFunctions(frontRb, mesaVis);
255 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
256 }
257
258 if (mesaVis->doubleBufferMode) {
259 driRenderbuffer *backRb
260 = driNewRenderbuffer(GL_RGBA, NULL,
261 screen->bytesPerPixel,
262 0, screen->width, driDrawPriv);
263 viaSetSpanFunctions(backRb, mesaVis);
264 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
265 }
266
267 if (mesaVis->depthBits == 16) {
268 driRenderbuffer *depthRb
269 = driNewRenderbuffer(GL_DEPTH_COMPONENT16, NULL,
270 screen->bytesPerPixel,
271 0, screen->width, driDrawPriv);
272 viaSetSpanFunctions(depthRb, mesaVis);
273 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
274 }
275 else if (mesaVis->depthBits == 24) {
276 driRenderbuffer *depthRb
277 = driNewRenderbuffer(GL_DEPTH_COMPONENT24, NULL,
278 screen->bytesPerPixel,
279 0, screen->width, driDrawPriv);
280 viaSetSpanFunctions(depthRb, mesaVis);
281 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
282 }
283 else if (mesaVis->depthBits == 32) {
284 driRenderbuffer *depthRb
285 = driNewRenderbuffer(GL_DEPTH_COMPONENT32, NULL,
286 screen->bytesPerPixel,
287 0, screen->width, driDrawPriv);
288 viaSetSpanFunctions(depthRb, mesaVis);
289 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
290 }
291
292 if (mesaVis->stencilBits > 0 && !swStencil) {
293 driRenderbuffer *stencilRb
294 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT, NULL,
295 screen->bytesPerPixel,
296 0, screen->width, driDrawPriv);
297 viaSetSpanFunctions(stencilRb, mesaVis);
298 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
299 }
300 #endif
301
302 _mesa_add_soft_renderbuffers(fb,
303 GL_FALSE, /* color */
304 GL_FALSE, /* depth */
305 swStencil,
306 swAccum,
307 GL_FALSE, /* alpha */
308 GL_FALSE /* aux */);
309 driDrawPriv->driverPrivate = (void *) fb;
310
311 return (driDrawPriv->driverPrivate != NULL);
312 }
313 }
314
315
316 static void
317 viaDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
318 {
319 _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
320 }
321
322 static const __DRIconfig **
323 viaFillInModes( __DRIscreenPrivate *psp,
324 unsigned pixel_bits, GLboolean have_back_buffer )
325 {
326 __DRIconfig **configs;
327 const unsigned back_buffer_factor = (have_back_buffer) ? 2 : 1;
328 GLenum fb_format;
329 GLenum fb_type;
330
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.
335 */
336 static const GLenum back_buffer_modes[] = {
337 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
338 };
339
340 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
341 * enable it.
342 */
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;
347
348 if ( pixel_bits == 16 ) {
349 fb_format = GL_RGB;
350 fb_type = GL_UNSIGNED_SHORT_5_6_5;
351 }
352 else {
353 fb_format = GL_BGRA;
354 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
355 }
356
357 configs = driCreateConfigs(fb_format, fb_type,
358 depth_bits_array, stencil_bits_array,
359 depth_buffer_factor, back_buffer_modes,
360 back_buffer_factor,
361 msaa_samples_array, 1);
362 if (configs == NULL) {
363 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
364 __LINE__);
365 return NULL;
366 }
367
368 return (const __DRIconfig **) configs;
369 }
370
371
372 /**
373 * This is the driver specific part of the createNewScreen entry point.
374 *
375 * \todo maybe fold this into intelInitDriver
376 *
377 * \return the __GLcontextModes supported by this driver
378 */
379 static const __DRIconfig **
380 viaInitScreen(__DRIscreenPrivate *psp)
381 {
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;
389
390 if ( ! driCheckDriDdxDrmVersions2( driver_name,
391 &psp->dri_version, & dri_expected,
392 &psp->ddx_version, & ddx_expected,
393 &psp->drm_version, & drm_expected) )
394 return NULL;
395
396 /* Calling driInitExtensions here, with a NULL context pointer,
397 * does not actually enable the extensions. It just makes sure
398 * that all the dispatch offsets for all the extensions that
399 * *might* be enables are known. This is needed because the
400 * dispatch offsets need to be known when _mesa_context_create is
401 * called, but we can't enable the extensions until we have a
402 * context pointer.
403 *
404 * Hello chicken. Hello egg. How are you two today?
405 */
406 driInitExtensions( NULL, card_extensions, GL_FALSE );
407
408 if (!viaInitDriver(psp))
409 return NULL;
410
411 return viaFillInModes( psp, dri_priv->bytesPerPixel * 8, GL_TRUE );
412
413 }
414
415
416 /**
417 * Get information about previous buffer swaps.
418 */
419 static int
420 getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo )
421 {
422 struct via_context *vmesa;
423
424 if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
425 || (dPriv->driContextPriv->driverPrivate == NULL)
426 || (sInfo == NULL) ) {
427 return -1;
428 }
429
430 vmesa = (struct via_context *) dPriv->driContextPriv->driverPrivate;
431 sInfo->swap_count = vmesa->swap_count;
432 sInfo->swap_ust = vmesa->swap_ust;
433 sInfo->swap_missed_count = vmesa->swap_missed_count;
434
435 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
436 ? driCalculateSwapUsage( dPriv, 0, vmesa->swap_missed_ust )
437 : 0.0;
438
439 return 0;
440 }
441
442 const struct __DriverAPIRec driDriverAPI = {
443 .InitScreen = viaInitScreen,
444 .DestroyScreen = viaDestroyScreen,
445 .CreateContext = viaCreateContext,
446 .DestroyContext = viaDestroyContext,
447 .CreateBuffer = viaCreateBuffer,
448 .DestroyBuffer = viaDestroyBuffer,
449 .SwapBuffers = viaSwapBuffers,
450 .MakeCurrent = viaMakeCurrent,
451 .UnbindContext = viaUnbindContext,
452 .GetSwapInfo = getSwapInfo,
453 .GetDrawableMSC = driDrawableGetMSC32,
454 .WaitForMSC = driWaitForMSC32,
455 .WaitForSBC = NULL,
456 .SwapBuffersMSC = NULL
457 };