Major check-in of changes for GL_EXT_framebuffer_object extension.
[mesa.git] / src / mesa / drivers / dri / i915 / intel_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "framebuffer.h"
31 #include "matrix.h"
32 #include "renderbuffer.h"
33 #include "simple_list.h"
34 #include "utils.h"
35 #include "xmlpool.h"
36
37
38 #include "intel_screen.h"
39
40 #include "intel_tex.h"
41 #include "intel_span.h"
42 #include "intel_tris.h"
43 #include "intel_ioctl.h"
44
45
46
47 #include "i830_dri.h"
48
49 PUBLIC const char __driConfigOptions[] =
50 DRI_CONF_BEGIN
51 DRI_CONF_SECTION_PERFORMANCE
52 DRI_CONF_FORCE_S3TC_ENABLE(false)
53 DRI_CONF_SECTION_END
54 DRI_CONF_END;
55 const GLuint __driNConfigOptions = 1;
56
57 #ifdef USE_NEW_INTERFACE
58 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
59 #endif /*USE_NEW_INTERFACE*/
60
61
62 static void intelPrintDRIInfo(intelScreenPrivate *intelScreen,
63 __DRIscreenPrivate *sPriv,
64 I830DRIPtr gDRIPriv)
65 {
66 fprintf(stderr, "Front size : 0x%x\n", sPriv->fbSize);
67 fprintf(stderr, "Front offset : 0x%x\n", intelScreen->frontOffset);
68 fprintf(stderr, "Back size : 0x%x\n", intelScreen->back.size);
69 fprintf(stderr, "Back offset : 0x%x\n", intelScreen->backOffset);
70 fprintf(stderr, "Depth size : 0x%x\n", intelScreen->depth.size);
71 fprintf(stderr, "Depth offset : 0x%x\n", intelScreen->depthOffset);
72 fprintf(stderr, "Texture size : 0x%x\n", intelScreen->textureSize);
73 fprintf(stderr, "Texture offset : 0x%x\n", intelScreen->textureOffset);
74 fprintf(stderr, "Memory : 0x%x\n", gDRIPriv->mem);
75 }
76
77 static GLboolean intelInitDriver(__DRIscreenPrivate *sPriv)
78 {
79 intelScreenPrivate *intelScreen;
80 I830DRIPtr gDRIPriv = (I830DRIPtr)sPriv->pDevPriv;
81
82
83 /* Allocate the private area */
84 intelScreen = (intelScreenPrivate *)MALLOC(sizeof(intelScreenPrivate));
85 if (!intelScreen) {
86 fprintf(stderr,"\nERROR! Allocating private area failed\n");
87 return GL_FALSE;
88 }
89 /* parse information in __driConfigOptions */
90 driParseOptionInfo (&intelScreen->optionCache,
91 __driConfigOptions, __driNConfigOptions);
92
93 intelScreen->driScrnPriv = sPriv;
94 sPriv->private = (void *)intelScreen;
95
96 intelScreen->deviceID = gDRIPriv->deviceID;
97 intelScreen->width = gDRIPriv->width;
98 intelScreen->height = gDRIPriv->height;
99 intelScreen->mem = gDRIPriv->mem;
100 intelScreen->cpp = gDRIPriv->cpp;
101 intelScreen->frontPitch = gDRIPriv->fbStride;
102 intelScreen->frontOffset = gDRIPriv->fbOffset;
103
104 switch (gDRIPriv->bitsPerPixel) {
105 case 15: intelScreen->fbFormat = DV_PF_555; break;
106 case 16: intelScreen->fbFormat = DV_PF_565; break;
107 case 32: intelScreen->fbFormat = DV_PF_8888; break;
108 }
109
110 intelScreen->backOffset = gDRIPriv->backOffset;
111 intelScreen->backPitch = gDRIPriv->backPitch;
112 intelScreen->depthOffset = gDRIPriv->depthOffset;
113 intelScreen->depthPitch = gDRIPriv->depthPitch;
114 intelScreen->textureOffset = gDRIPriv->textureOffset;
115 intelScreen->textureSize = gDRIPriv->textureSize;
116 intelScreen->logTextureGranularity = gDRIPriv->logTextureGranularity;
117 intelScreen->back.handle = gDRIPriv->backbuffer;
118 intelScreen->back.size = gDRIPriv->backbufferSize;
119
120 if (drmMap(sPriv->fd,
121 intelScreen->back.handle,
122 intelScreen->back.size,
123 (drmAddress *)&intelScreen->back.map) != 0) {
124 fprintf(stderr, "\nERROR: line %d, Function %s, File %s\n",
125 __LINE__, __FUNCTION__, __FILE__);
126 FREE(intelScreen);
127 sPriv->private = NULL;
128 return GL_FALSE;
129 }
130
131 intelScreen->depth.handle = gDRIPriv->depthbuffer;
132 intelScreen->depth.size = gDRIPriv->depthbufferSize;
133
134 if (drmMap(sPriv->fd,
135 intelScreen->depth.handle,
136 intelScreen->depth.size,
137 (drmAddress *)&intelScreen->depth.map) != 0) {
138 fprintf(stderr, "\nERROR: line %d, Function %s, File %s\n",
139 __LINE__, __FUNCTION__, __FILE__);
140 FREE(intelScreen);
141 drmUnmap(intelScreen->back.map, intelScreen->back.size);
142 sPriv->private = NULL;
143 return GL_FALSE;
144 }
145
146 intelScreen->tex.handle = gDRIPriv->textures;
147 intelScreen->tex.size = gDRIPriv->textureSize;
148
149 if (drmMap(sPriv->fd,
150 intelScreen->tex.handle,
151 intelScreen->tex.size,
152 (drmAddress *)&intelScreen->tex.map) != 0) {
153 fprintf(stderr, "\nERROR: line %d, Function %s, File %s\n",
154 __LINE__, __FUNCTION__, __FILE__);
155 FREE(intelScreen);
156 drmUnmap(intelScreen->back.map, intelScreen->back.size);
157 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
158 sPriv->private = NULL;
159 return GL_FALSE;
160 }
161
162 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
163
164 if (0) intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
165
166 intelScreen->drmMinor = sPriv->drmMinor;
167
168 {
169 int ret;
170 drmI830GetParam gp;
171
172 gp.param = I830_PARAM_IRQ_ACTIVE;
173 gp.value = &intelScreen->irq_active;
174
175 ret = drmCommandWriteRead( sPriv->fd, DRM_I830_GETPARAM,
176 &gp, sizeof(gp));
177 if (ret) {
178 fprintf(stderr, "drmI830GetParam: %d\n", ret);
179 return GL_FALSE;
180 }
181 }
182
183 {
184 int ret;
185 drmI830GetParam gp;
186
187 gp.param = I830_PARAM_ALLOW_BATCHBUFFER;
188 gp.value = &intelScreen->allow_batchbuffer;
189
190 ret = drmCommandWriteRead( sPriv->fd, DRM_I830_GETPARAM,
191 &gp, sizeof(gp));
192 if (ret) {
193 fprintf(stderr, "drmI830GetParam: (%d) %d\n", gp.param, ret);
194 return GL_FALSE;
195 }
196 }
197
198 if ( driCompareGLXAPIVersion( 20030813 ) >= 0 ) {
199 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
200 (PFNGLXSCRENABLEEXTENSIONPROC) glXGetProcAddress( (const GLubyte *) "__glXScrEnableExtension" );
201 void * const psc = sPriv->psc->screenConfigs;
202
203 if (glx_enable_extension != NULL) {
204 (*glx_enable_extension)( psc, "GLX_SGI_make_current_read" );
205
206 if ( driCompareGLXAPIVersion( 20030915 ) >= 0 ) {
207 (*glx_enable_extension)( psc, "GLX_SGIX_fbconfig" );
208 (*glx_enable_extension)( psc, "GLX_OML_swap_method" );
209 }
210
211 if ( driCompareGLXAPIVersion( 20030818 ) >= 0 ) {
212 sPriv->psc->allocateMemory = (void *) intelAllocateMemoryMESA;
213 sPriv->psc->freeMemory = (void *) intelFreeMemoryMESA;
214 sPriv->psc->memoryOffset = (void *) intelGetMemoryOffsetMESA;
215
216 (*glx_enable_extension)( psc, "GLX_MESA_allocate_memory" );
217 }
218 }
219 }
220
221 return GL_TRUE;
222 }
223
224
225 static void intelDestroyScreen(__DRIscreenPrivate *sPriv)
226 {
227 intelScreenPrivate *intelScreen = (intelScreenPrivate *)sPriv->private;
228
229 /* Need to unmap all the bufs and maps here:
230 */
231 drmUnmap(intelScreen->back.map, intelScreen->back.size);
232 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
233 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
234 FREE(intelScreen);
235 sPriv->private = NULL;
236 }
237
238
239 static GLboolean intelCreateBuffer( __DRIscreenPrivate *driScrnPriv,
240 __DRIdrawablePrivate *driDrawPriv,
241 const __GLcontextModes *mesaVis,
242 GLboolean isPixmap )
243 {
244 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
245
246 if (isPixmap) {
247 return GL_FALSE; /* not implemented */
248 } else {
249 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
250 mesaVis->depthBits != 24);
251
252 #if 0
253 driDrawPriv->driverPrivate = (void *)
254 _mesa_create_framebuffer(mesaVis,
255 GL_FALSE, /* software depth buffer? */
256 swStencil,
257 mesaVis->accumRedBits > 0,
258 GL_FALSE /* s/w alpha planes */);
259 #else
260 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
261
262 {
263 driRenderbuffer *frontRb
264 = driNewRenderbuffer(GL_RGBA, screen->cpp,
265 screen->frontOffset, screen->frontPitch);
266 intelSetSpanFunctions(frontRb, mesaVis);
267 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
268 }
269
270 if (mesaVis->doubleBufferMode) {
271 driRenderbuffer *backRb
272 = driNewRenderbuffer(GL_RGBA, screen->cpp,
273 screen->backOffset, screen->backPitch);
274 intelSetSpanFunctions(backRb, mesaVis);
275 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
276 }
277
278 if (mesaVis->depthBits == 16) {
279 driRenderbuffer *depthRb
280 = driNewRenderbuffer(GL_DEPTH_COMPONENT16, screen->cpp,
281 screen->depthOffset, screen->depthPitch);
282 intelSetSpanFunctions(depthRb, mesaVis);
283 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
284 }
285 else if (mesaVis->depthBits == 24) {
286 driRenderbuffer *depthRb
287 = driNewRenderbuffer(GL_DEPTH_COMPONENT24, screen->cpp,
288 screen->depthOffset, screen->depthPitch);
289 intelSetSpanFunctions(depthRb, mesaVis);
290 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
291 }
292
293 if (mesaVis->stencilBits > 0 && !swStencil) {
294 driRenderbuffer *stencilRb
295 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT, screen->cpp,
296 screen->depthOffset, screen->depthPitch);
297 intelSetSpanFunctions(stencilRb, mesaVis);
298 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
299 }
300
301 _mesa_add_soft_renderbuffers(fb,
302 GL_FALSE, /* color */
303 GL_FALSE, /* depth */
304 swStencil,
305 mesaVis->accumRedBits > 0,
306 GL_FALSE, /* alpha */
307 GL_FALSE /* aux */);
308 driDrawPriv->driverPrivate = (void *) fb;
309 #endif
310 return (driDrawPriv->driverPrivate != NULL);
311 }
312 }
313
314 static void intelDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
315 {
316 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
317 }
318
319
320 /* There are probably better ways to do this, such as an
321 * init-designated function to register chipids and createcontext
322 * functions.
323 */
324 extern GLboolean i830CreateContext( const __GLcontextModes *mesaVis,
325 __DRIcontextPrivate *driContextPriv,
326 void *sharedContextPrivate);
327
328 extern GLboolean i915CreateContext( const __GLcontextModes *mesaVis,
329 __DRIcontextPrivate *driContextPriv,
330 void *sharedContextPrivate);
331
332
333
334
335 static GLboolean intelCreateContext( const __GLcontextModes *mesaVis,
336 __DRIcontextPrivate *driContextPriv,
337 void *sharedContextPrivate)
338 {
339 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
340 intelScreenPrivate *intelScreen = (intelScreenPrivate *)sPriv->private;
341
342 switch (intelScreen->deviceID) {
343 case PCI_CHIP_845_G:
344 case PCI_CHIP_I830_M:
345 case PCI_CHIP_I855_GM:
346 case PCI_CHIP_I865_G:
347 return i830CreateContext( mesaVis, driContextPriv,
348 sharedContextPrivate );
349
350 case PCI_CHIP_I915_G:
351 case PCI_CHIP_I915_GM:
352 return i915CreateContext( mesaVis, driContextPriv,
353 sharedContextPrivate );
354
355 default:
356 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
357 return GL_FALSE;
358 }
359 }
360
361
362 static const struct __DriverAPIRec intelAPI = {
363 .InitDriver = intelInitDriver,
364 .DestroyScreen = intelDestroyScreen,
365 .CreateContext = intelCreateContext,
366 .DestroyContext = intelDestroyContext,
367 .CreateBuffer = intelCreateBuffer,
368 .DestroyBuffer = intelDestroyBuffer,
369 .SwapBuffers = intelSwapBuffers,
370 .MakeCurrent = intelMakeCurrent,
371 .UnbindContext = intelUnbindContext,
372 .GetSwapInfo = NULL,
373 .GetMSC = NULL,
374 .WaitForMSC = NULL,
375 .WaitForSBC = NULL,
376 .SwapBuffersMSC = NULL
377 };
378
379 /*
380 * This is the bootstrap function for the driver.
381 * The __driCreateScreen name is the symbol that libGL.so fetches.
382 * Return: pointer to a __DRIscreenPrivate.
383 */
384 #if !defined(DRI_NEW_INTERFACE_ONLY)
385 void *__driCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
386 int numConfigs, __GLXvisualConfig *config)
387 {
388 __DRIscreenPrivate *psp;
389 psp = __driUtilCreateScreen(dpy, scrn, psc, numConfigs, config, &intelAPI);
390 return (void *) psp;
391 }
392 #endif /* !defined(DRI_NEW_INTERFACE_ONLY) */
393
394
395 #ifdef USE_NEW_INTERFACE
396 static __GLcontextModes *
397 intelFillInModes( unsigned pixel_bits, unsigned depth_bits,
398 unsigned stencil_bits, GLboolean have_back_buffer )
399 {
400 __GLcontextModes * modes;
401 __GLcontextModes * m;
402 unsigned num_modes;
403 unsigned depth_buffer_factor;
404 unsigned back_buffer_factor;
405 GLenum fb_format;
406 GLenum fb_type;
407
408 /* GLX_SWAP_COPY_OML is only supported because the MGA driver doesn't
409 * support pageflipping at all.
410 */
411 static const GLenum back_buffer_modes[] = {
412 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
413 };
414
415 u_int8_t depth_bits_array[2];
416 u_int8_t stencil_bits_array[2];
417
418
419 depth_bits_array[0] = 0;
420 depth_bits_array[1] = depth_bits;
421
422 /* Just like with the accumulation buffer, always provide some modes
423 * with a stencil buffer. It will be a sw fallback, but some apps won't
424 * care about that.
425 */
426 stencil_bits_array[0] = 0;
427 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
428
429 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
430 back_buffer_factor = (have_back_buffer) ? 3 : 1;
431
432 num_modes = depth_buffer_factor * back_buffer_factor * 4;
433
434 if ( pixel_bits == 16 ) {
435 fb_format = GL_RGB;
436 fb_type = GL_UNSIGNED_SHORT_5_6_5;
437 }
438 else {
439 fb_format = GL_BGRA;
440 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
441 }
442
443 modes = (*create_context_modes)( num_modes, sizeof( __GLcontextModes ) );
444 m = modes;
445 if ( ! driFillInModes( & m, fb_format, fb_type,
446 depth_bits_array, stencil_bits_array, depth_buffer_factor,
447 back_buffer_modes, back_buffer_factor,
448 GLX_TRUE_COLOR ) ) {
449 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
450 __func__, __LINE__ );
451 return NULL;
452 }
453
454 /* There's no direct color modes on intel? */
455
456 /* Mark the visual as slow if there are "fake" stencil bits.
457 */
458 for ( m = modes ; m != NULL ; m = m->next ) {
459 if ( (m->stencilBits != 0) && (m->stencilBits != stencil_bits) ) {
460 m->visualRating = GLX_SLOW_CONFIG;
461 }
462 }
463
464 return modes;
465 }
466 #endif /* USE_NEW_INTERFACE */
467
468
469 /**
470 * This is the bootstrap function for the driver. libGL supplies all of the
471 * requisite information about the system, and the driver initializes itself.
472 * This routine also fills in the linked list pointed to by \c driver_modes
473 * with the \c __GLcontextModes that the driver can support for windows or
474 * pbuffers.
475 *
476 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
477 * failure.
478 */
479 #ifdef USE_NEW_INTERFACE
480 PUBLIC
481 void * __driCreateNewScreen( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
482 const __GLcontextModes * modes,
483 const __DRIversion * ddx_version,
484 const __DRIversion * dri_version,
485 const __DRIversion * drm_version,
486 const __DRIframebuffer * frame_buffer,
487 drmAddress pSAREA, int fd,
488 int internal_api_version,
489 __GLcontextModes ** driver_modes )
490
491 {
492 __DRIscreenPrivate *psp;
493 static const __DRIversion ddx_expected = { 1, 0, 0 };
494 static const __DRIversion dri_expected = { 4, 0, 0 };
495 static const __DRIversion drm_expected = { 1, 1, 0 };
496
497 if ( ! driCheckDriDdxDrmVersions2( "i915",
498 dri_version, & dri_expected,
499 ddx_version, & ddx_expected,
500 drm_version, & drm_expected ) ) {
501 return NULL;
502 }
503
504 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
505 ddx_version, dri_version, drm_version,
506 frame_buffer, pSAREA, fd,
507 internal_api_version, &intelAPI);
508 if ( psp != NULL ) {
509 create_context_modes = (PFNGLXCREATECONTEXTMODES)
510 glXGetProcAddress( (const GLubyte *) "__glXCreateContextModes" );
511 if ( create_context_modes != NULL ) {
512 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
513 *driver_modes = intelFillInModes( dri_priv->cpp * 8,
514 (dri_priv->cpp == 2) ? 16 : 24,
515 (dri_priv->cpp == 2) ? 0 : 8,
516 (dri_priv->backOffset != dri_priv->depthOffset) );
517 }
518 }
519
520 return (void *) psp;
521 }
522 #endif /* USE_NEW_INTERFACE */