mesa: Enable remap table in core.
[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 viaScreenPrivate *screen = (viaScreenPrivate *) driScrnPriv->private;
212
213 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
214 mesaVis->depthBits != 24);
215 GLboolean swAccum = mesaVis->accumRedBits > 0;
216
217 if (isPixmap) {
218 /* KW: This needs work, disabled for now:
219 */
220 #if 0
221 driDrawPriv->driverPrivate = (void *)
222 _mesa_create_framebuffer(mesaVis,
223 GL_FALSE, /* software depth buffer? */
224 swStencil,
225 mesaVis->accumRedBits > 0,
226 GL_FALSE /* s/w alpha planes */);
227
228 return (driDrawPriv->driverPrivate != NULL);
229 #endif
230 return GL_FALSE;
231 }
232 else {
233 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
234
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.
238 */
239
240 #if 000
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.
245 */
246 /* XXX check/fix the offset/pitch parameters! */
247 {
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);
254 }
255
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);
263 }
264
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);
272 }
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);
280 }
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);
288 }
289
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);
297 }
298 #endif
299
300 _mesa_add_soft_renderbuffers(fb,
301 GL_FALSE, /* color */
302 GL_FALSE, /* depth */
303 swStencil,
304 swAccum,
305 GL_FALSE, /* alpha */
306 GL_FALSE /* aux */);
307 driDrawPriv->driverPrivate = (void *) fb;
308
309 return (driDrawPriv->driverPrivate != NULL);
310 }
311 }
312
313
314 static void
315 viaDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
316 {
317 _mesa_reference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)), NULL);
318 }
319
320 static const __DRIconfig **
321 viaFillInModes( __DRIscreenPrivate *psp,
322 unsigned pixel_bits, GLboolean have_back_buffer )
323 {
324 __DRIconfig **configs;
325 const unsigned back_buffer_factor = (have_back_buffer) ? 2 : 1;
326 GLenum fb_format;
327 GLenum fb_type;
328
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.
333 */
334 static const GLenum back_buffer_modes[] = {
335 GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
336 };
337
338 /* The 32-bit depth-buffer mode isn't supported yet, so don't actually
339 * enable it.
340 */
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;
345
346 if ( pixel_bits == 16 ) {
347 fb_format = GL_RGB;
348 fb_type = GL_UNSIGNED_SHORT_5_6_5;
349 }
350 else {
351 fb_format = GL_BGRA;
352 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
353 }
354
355 configs = driCreateConfigs(fb_format, fb_type,
356 depth_bits_array, stencil_bits_array,
357 depth_buffer_factor, back_buffer_modes,
358 back_buffer_factor,
359 msaa_samples_array, 1);
360 if (configs == NULL) {
361 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
362 __LINE__);
363 return NULL;
364 }
365
366 return (const __DRIconfig **) configs;
367 }
368
369
370 /**
371 * This is the driver specific part of the createNewScreen entry point.
372 *
373 * \todo maybe fold this into intelInitDriver
374 *
375 * \return the __GLcontextModes supported by this driver
376 */
377 static const __DRIconfig **
378 viaInitScreen(__DRIscreenPrivate *psp)
379 {
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;
387
388 if ( ! driCheckDriDdxDrmVersions2( driver_name,
389 &psp->dri_version, & dri_expected,
390 &psp->ddx_version, & ddx_expected,
391 &psp->drm_version, & drm_expected) )
392 return NULL;
393
394 if (!viaInitDriver(psp))
395 return NULL;
396
397 return viaFillInModes( psp, dri_priv->bytesPerPixel * 8, GL_TRUE );
398
399 }
400
401
402 /**
403 * Get information about previous buffer swaps.
404 */
405 static int
406 getSwapInfo( __DRIdrawablePrivate *dPriv, __DRIswapInfo * sInfo )
407 {
408 struct via_context *vmesa;
409
410 if ( (dPriv == NULL) || (dPriv->driContextPriv == NULL)
411 || (dPriv->driContextPriv->driverPrivate == NULL)
412 || (sInfo == NULL) ) {
413 return -1;
414 }
415
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;
420
421 sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
422 ? driCalculateSwapUsage( dPriv, 0, vmesa->swap_missed_ust )
423 : 0.0;
424
425 return 0;
426 }
427
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,
441 .WaitForSBC = NULL,
442 .SwapBuffersMSC = NULL
443 };