e0bf58ca9a2125d9c8b971cb00b4f583fd4bd229
[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 static drmBufMapPtr via_create_empty_buffers(void)
66 {
67 drmBufMapPtr retval;
68
69 retval = (drmBufMapPtr)MALLOC(sizeof(drmBufMap));
70 if (retval == NULL) return NULL;
71 memset(retval, 0, sizeof(drmBufMap));
72
73 retval->list = (drmBufPtr)MALLOC(sizeof(drmBuf) * VIA_DMA_BUF_NR);
74 if (retval->list == NULL) {
75 FREE(retval);
76 return NULL;
77 }
78 memset(retval->list, 0, sizeof(drmBuf) * VIA_DMA_BUF_NR);
79 return retval;
80 }
81
82 static void via_free_empty_buffers( drmBufMapPtr bufs )
83 {
84 if (bufs && bufs->list)
85 FREE(bufs->list);
86
87 if (bufs)
88 FREE(bufs);
89 }
90
91
92 static GLboolean
93 viaInitDriver(__DRIscreenPrivate *sPriv)
94 {
95 viaScreenPrivate *viaScreen;
96 VIADRIPtr gDRIPriv = (VIADRIPtr)sPriv->pDevPriv;
97 int i;
98
99 if (sPriv->devPrivSize != sizeof(VIADRIRec)) {
100 fprintf(stderr,"\nERROR! sizeof(VIADRIRec) does not match passed size from device driver\n");
101 return GL_FALSE;
102 }
103
104 /* Allocate the private area */
105 viaScreen = (viaScreenPrivate *) CALLOC(sizeof(viaScreenPrivate));
106 if (!viaScreen) {
107 __driUtilMessage("viaInitDriver: alloc viaScreenPrivate struct failed");
108 return GL_FALSE;
109 }
110
111 /* parse information in __driConfigOptions */
112 driParseOptionInfo (&viaScreen->optionCache,
113 __driConfigOptions, __driNConfigOptions);
114
115
116 viaScreen->driScrnPriv = sPriv;
117 sPriv->private = (void *)viaScreen;
118
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;
128
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);
135 }
136
137 viaScreen->bufs = via_create_empty_buffers();
138 if (viaScreen->bufs == NULL) {
139 __driUtilMessage("viaInitDriver: via_create_empty_buffers() failed");
140 FREE(viaScreen);
141 return GL_FALSE;
142 }
143
144 if (drmMap(sPriv->fd,
145 gDRIPriv->regs.handle,
146 gDRIPriv->regs.size,
147 &viaScreen->reg) != 0) {
148 FREE(viaScreen);
149 sPriv->private = NULL;
150 __driUtilMessage("viaInitDriver: drmMap regs failed");
151 return GL_FALSE;
152 }
153
154 if (gDRIPriv->agp.size) {
155 if (drmMap(sPriv->fd,
156 gDRIPriv->agp.handle,
157 gDRIPriv->agp.size,
158 (drmAddress *)&viaScreen->agpLinearStart) != 0) {
159 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
160 FREE(viaScreen);
161 sPriv->private = NULL;
162 __driUtilMessage("viaInitDriver: drmMap agp failed");
163 return GL_FALSE;
164 }
165
166 viaScreen->agpBase = drmAgpBase(sPriv->fd);
167 } else
168 viaScreen->agpLinearStart = 0;
169
170 viaScreen->sareaPrivOffset = gDRIPriv->sarea_priv_offset;
171
172 i = 0;
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;
178 }
179
180 viaScreen->extensions[i++] = NULL;
181 sPriv->extensions = viaScreen->extensions;
182
183 return GL_TRUE;
184 }
185
186 static void
187 viaDestroyScreen(__DRIscreenPrivate *sPriv)
188 {
189 viaScreenPrivate *viaScreen = (viaScreenPrivate *)sPriv->private;
190 VIADRIPtr gDRIPriv = (VIADRIPtr)sPriv->pDevPriv;
191
192 drmUnmap(viaScreen->reg, gDRIPriv->regs.size);
193 if (gDRIPriv->agp.size)
194 drmUnmap(viaScreen->agpLinearStart, gDRIPriv->agp.size);
195
196 via_free_empty_buffers(viaScreen->bufs);
197
198 driDestroyOptionInfo(&viaScreen->optionCache);
199
200 FREE(viaScreen);
201 sPriv->private = NULL;
202 }
203
204
205 static GLboolean
206 viaCreateBuffer(__DRIscreenPrivate *driScrnPriv,
207 __DRIdrawablePrivate *driDrawPriv,
208 const __GLcontextModes *mesaVis,
209 GLboolean isPixmap)
210 {
211 #if 0
212 viaScreenPrivate *screen = (viaScreenPrivate *) driScrnPriv->private;
213 #endif
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(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);
256 }
257
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);
265 }
266
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);
274 }
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);
282 }
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);
290 }
291
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);
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_reference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)), NULL);
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 if (!viaInitDriver(psp))
397 return NULL;
398
399 return viaFillInModes( psp, dri_priv->bytesPerPixel * 8, GL_TRUE );
400
401 }
402
403
404 /**
405 * Get information about previous buffer swaps.
406 */
407 static int
408 getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo )
409 {
410 struct via_context *vmesa;
411
412 if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
413 || (dPriv->driContextPriv->driverPrivate == NULL)
414 || (sInfo == NULL) ) {
415 return -1;
416 }
417
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;
422
423 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
424 ? driCalculateSwapUsage( dPriv, 0, vmesa->swap_missed_ust )
425 : 0.0;
426
427 return 0;
428 }
429
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,
443 .WaitForSBC = NULL,
444 .SwapBuffersMSC = NULL
445 };