Add new void *addr and __DRIdrawablePrivate parameters to
[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 extern const struct dri_extension card_extensions[];
58
59 static void intelPrintDRIInfo(intelScreenPrivate *intelScreen,
60 __DRIscreenPrivate *sPriv,
61 I830DRIPtr gDRIPriv)
62 {
63 fprintf(stderr, "Front size : 0x%x\n", sPriv->fbSize);
64 fprintf(stderr, "Front offset : 0x%x\n", intelScreen->frontOffset);
65 fprintf(stderr, "Back size : 0x%x\n", intelScreen->back.size);
66 fprintf(stderr, "Back offset : 0x%x\n", intelScreen->backOffset);
67 fprintf(stderr, "Depth size : 0x%x\n", intelScreen->depth.size);
68 fprintf(stderr, "Depth offset : 0x%x\n", intelScreen->depthOffset);
69 fprintf(stderr, "Texture size : 0x%x\n", intelScreen->textureSize);
70 fprintf(stderr, "Texture offset : 0x%x\n", intelScreen->textureOffset);
71 fprintf(stderr, "Memory : 0x%x\n", gDRIPriv->mem);
72 }
73
74 static GLboolean intelInitDriver(__DRIscreenPrivate *sPriv)
75 {
76 intelScreenPrivate *intelScreen;
77 I830DRIPtr gDRIPriv = (I830DRIPtr)sPriv->pDevPriv;
78 PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
79 (PFNGLXSCRENABLEEXTENSIONPROC) (*dri_interface->getProcAddress("glxEnableExtension"));
80 void * const psc = sPriv->psc->screenConfigs;
81
82 if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
83 fprintf(stderr,"\nERROR! sizeof(I830DRIRec) does not match passed size from device driver\n");
84 return GL_FALSE;
85 }
86
87 /* Allocate the private area */
88 intelScreen = (intelScreenPrivate *)CALLOC(sizeof(intelScreenPrivate));
89 if (!intelScreen) {
90 fprintf(stderr,"\nERROR! Allocating private area failed\n");
91 return GL_FALSE;
92 }
93 /* parse information in __driConfigOptions */
94 driParseOptionInfo (&intelScreen->optionCache,
95 __driConfigOptions, __driNConfigOptions);
96
97 intelScreen->driScrnPriv = sPriv;
98 sPriv->private = (void *)intelScreen;
99
100 intelScreen->deviceID = gDRIPriv->deviceID;
101 intelScreen->width = gDRIPriv->width;
102 intelScreen->height = gDRIPriv->height;
103 intelScreen->mem = gDRIPriv->mem;
104 intelScreen->cpp = gDRIPriv->cpp;
105 intelScreen->frontPitch = gDRIPriv->fbStride;
106 intelScreen->frontOffset = gDRIPriv->fbOffset;
107
108 switch (gDRIPriv->bitsPerPixel) {
109 case 15: intelScreen->fbFormat = DV_PF_555; break;
110 case 16: intelScreen->fbFormat = DV_PF_565; break;
111 case 32: intelScreen->fbFormat = DV_PF_8888; break;
112 }
113
114 intelScreen->backOffset = gDRIPriv->backOffset;
115 intelScreen->backPitch = gDRIPriv->backPitch;
116 intelScreen->depthOffset = gDRIPriv->depthOffset;
117 intelScreen->depthPitch = gDRIPriv->depthPitch;
118 intelScreen->textureOffset = gDRIPriv->textureOffset;
119 intelScreen->textureSize = gDRIPriv->textureSize;
120 intelScreen->logTextureGranularity = gDRIPriv->logTextureGranularity;
121 intelScreen->back.handle = gDRIPriv->backbuffer;
122 intelScreen->back.size = gDRIPriv->backbufferSize;
123
124 if (drmMap(sPriv->fd,
125 intelScreen->back.handle,
126 intelScreen->back.size,
127 (drmAddress *)&intelScreen->back.map) != 0) {
128 fprintf(stderr, "\nERROR: line %d, Function %s, File %s\n",
129 __LINE__, __FUNCTION__, __FILE__);
130 FREE(intelScreen);
131 sPriv->private = NULL;
132 return GL_FALSE;
133 }
134
135 intelScreen->depth.handle = gDRIPriv->depthbuffer;
136 intelScreen->depth.size = gDRIPriv->depthbufferSize;
137
138 if (drmMap(sPriv->fd,
139 intelScreen->depth.handle,
140 intelScreen->depth.size,
141 (drmAddress *)&intelScreen->depth.map) != 0) {
142 fprintf(stderr, "\nERROR: line %d, Function %s, File %s\n",
143 __LINE__, __FUNCTION__, __FILE__);
144 FREE(intelScreen);
145 drmUnmap(intelScreen->back.map, intelScreen->back.size);
146 sPriv->private = NULL;
147 return GL_FALSE;
148 }
149
150 intelScreen->tex.handle = gDRIPriv->textures;
151 intelScreen->tex.size = gDRIPriv->textureSize;
152
153 if (drmMap(sPriv->fd,
154 intelScreen->tex.handle,
155 intelScreen->tex.size,
156 (drmAddress *)&intelScreen->tex.map) != 0) {
157 fprintf(stderr, "\nERROR: line %d, Function %s, File %s\n",
158 __LINE__, __FUNCTION__, __FILE__);
159 FREE(intelScreen);
160 drmUnmap(intelScreen->back.map, intelScreen->back.size);
161 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
162 sPriv->private = NULL;
163 return GL_FALSE;
164 }
165
166 intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
167
168 if (0) intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
169
170 intelScreen->drmMinor = sPriv->drmMinor;
171
172 {
173 int ret;
174 drmI830GetParam gp;
175
176 gp.param = I830_PARAM_IRQ_ACTIVE;
177 gp.value = &intelScreen->irq_active;
178
179 ret = drmCommandWriteRead( sPriv->fd, DRM_I830_GETPARAM,
180 &gp, sizeof(gp));
181 if (ret) {
182 fprintf(stderr, "drmI830GetParam: %d\n", ret);
183 return GL_FALSE;
184 }
185 }
186
187 {
188 int ret;
189 drmI830GetParam gp;
190
191 gp.param = I830_PARAM_ALLOW_BATCHBUFFER;
192 gp.value = &intelScreen->allow_batchbuffer;
193
194 ret = drmCommandWriteRead( sPriv->fd, DRM_I830_GETPARAM,
195 &gp, sizeof(gp));
196 if (ret) {
197 fprintf(stderr, "drmI830GetParam: (%d) %d\n", gp.param, ret);
198 return GL_FALSE;
199 }
200 }
201
202 if (glx_enable_extension != NULL) {
203 (*glx_enable_extension)( psc, "GLX_SGI_make_current_read" );
204 (*glx_enable_extension)( psc, "GLX_MESA_allocate_memory" );
205 }
206
207 sPriv->psc->allocateMemory = (void *) intelAllocateMemoryMESA;
208 sPriv->psc->freeMemory = (void *) intelFreeMemoryMESA;
209 sPriv->psc->memoryOffset = (void *) intelGetMemoryOffsetMESA;
210
211 return GL_TRUE;
212 }
213
214
215 static void intelDestroyScreen(__DRIscreenPrivate *sPriv)
216 {
217 intelScreenPrivate *intelScreen = (intelScreenPrivate *)sPriv->private;
218
219 /* Need to unmap all the bufs and maps here:
220 */
221 drmUnmap(intelScreen->back.map, intelScreen->back.size);
222 drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
223 drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
224 FREE(intelScreen);
225 sPriv->private = NULL;
226 }
227
228
229 static GLboolean intelCreateBuffer( __DRIscreenPrivate *driScrnPriv,
230 __DRIdrawablePrivate *driDrawPriv,
231 const __GLcontextModes *mesaVis,
232 GLboolean isPixmap )
233 {
234 intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
235
236 if (isPixmap) {
237 return GL_FALSE; /* not implemented */
238 } else {
239 GLboolean swStencil = (mesaVis->stencilBits > 0 &&
240 mesaVis->depthBits != 24);
241
242 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
243
244 {
245 driRenderbuffer *frontRb
246 = driNewRenderbuffer(GL_RGBA,
247 driScrnPriv->pFB,
248 screen->cpp,
249 screen->frontOffset, screen->frontPitch,
250 driDrawPriv);
251 intelSetSpanFunctions(frontRb, mesaVis);
252 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
253 }
254
255 if (mesaVis->doubleBufferMode) {
256 driRenderbuffer *backRb
257 = driNewRenderbuffer(GL_RGBA,
258 screen->back.map,
259 screen->cpp,
260 screen->backOffset, screen->backPitch,
261 driDrawPriv);
262 intelSetSpanFunctions(backRb, mesaVis);
263 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
264 }
265
266 if (mesaVis->depthBits == 16) {
267 driRenderbuffer *depthRb
268 = driNewRenderbuffer(GL_DEPTH_COMPONENT16,
269 screen->depth.map,
270 screen->cpp,
271 screen->depthOffset, screen->depthPitch,
272 driDrawPriv);
273 intelSetSpanFunctions(depthRb, mesaVis);
274 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
275 }
276 else if (mesaVis->depthBits == 24) {
277 driRenderbuffer *depthRb
278 = driNewRenderbuffer(GL_DEPTH_COMPONENT24,
279 screen->depth.map,
280 screen->cpp,
281 screen->depthOffset, screen->depthPitch,
282 driDrawPriv);
283 intelSetSpanFunctions(depthRb, mesaVis);
284 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
285 }
286
287 if (mesaVis->stencilBits > 0 && !swStencil) {
288 driRenderbuffer *stencilRb
289 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT,
290 screen->depth.map,
291 screen->cpp,
292 screen->depthOffset, screen->depthPitch,
293 driDrawPriv);
294 intelSetSpanFunctions(stencilRb, mesaVis);
295 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
296 }
297
298 _mesa_add_soft_renderbuffers(fb,
299 GL_FALSE, /* color */
300 GL_FALSE, /* depth */
301 swStencil,
302 mesaVis->accumRedBits > 0,
303 GL_FALSE, /* alpha */
304 GL_FALSE /* aux */);
305 driDrawPriv->driverPrivate = (void *) fb;
306
307 return (driDrawPriv->driverPrivate != NULL);
308 }
309 }
310
311 static void intelDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
312 {
313 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
314 }
315
316
317 /* There are probably better ways to do this, such as an
318 * init-designated function to register chipids and createcontext
319 * functions.
320 */
321 extern GLboolean i830CreateContext( const __GLcontextModes *mesaVis,
322 __DRIcontextPrivate *driContextPriv,
323 void *sharedContextPrivate);
324
325 extern GLboolean i915CreateContext( const __GLcontextModes *mesaVis,
326 __DRIcontextPrivate *driContextPriv,
327 void *sharedContextPrivate);
328
329
330
331
332 static GLboolean intelCreateContext( const __GLcontextModes *mesaVis,
333 __DRIcontextPrivate *driContextPriv,
334 void *sharedContextPrivate)
335 {
336 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
337 intelScreenPrivate *intelScreen = (intelScreenPrivate *)sPriv->private;
338
339 switch (intelScreen->deviceID) {
340 case PCI_CHIP_845_G:
341 case PCI_CHIP_I830_M:
342 case PCI_CHIP_I855_GM:
343 case PCI_CHIP_I865_G:
344 return i830CreateContext( mesaVis, driContextPriv,
345 sharedContextPrivate );
346
347 case PCI_CHIP_I915_G:
348 case PCI_CHIP_I915_GM:
349 case PCI_CHIP_I945_G:
350 return i915CreateContext( mesaVis, driContextPriv,
351 sharedContextPrivate );
352
353 default:
354 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
355 return GL_FALSE;
356 }
357 }
358
359
360 static const struct __DriverAPIRec intelAPI = {
361 .InitDriver = intelInitDriver,
362 .DestroyScreen = intelDestroyScreen,
363 .CreateContext = intelCreateContext,
364 .DestroyContext = intelDestroyContext,
365 .CreateBuffer = intelCreateBuffer,
366 .DestroyBuffer = intelDestroyBuffer,
367 .SwapBuffers = intelSwapBuffers,
368 .MakeCurrent = intelMakeCurrent,
369 .UnbindContext = intelUnbindContext,
370 .GetSwapInfo = NULL,
371 .GetMSC = NULL,
372 .WaitForMSC = NULL,
373 .WaitForSBC = NULL,
374 .SwapBuffersMSC = NULL
375 };
376
377
378 static __GLcontextModes *
379 intelFillInModes( unsigned pixel_bits, unsigned depth_bits,
380 unsigned stencil_bits, GLboolean have_back_buffer )
381 {
382 __GLcontextModes * modes;
383 __GLcontextModes * m;
384 unsigned num_modes;
385 unsigned depth_buffer_factor;
386 unsigned back_buffer_factor;
387 GLenum fb_format;
388 GLenum fb_type;
389
390 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
391 * support pageflipping at all.
392 */
393 static const GLenum back_buffer_modes[] = {
394 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
395 };
396
397 u_int8_t depth_bits_array[3];
398 u_int8_t stencil_bits_array[3];
399
400
401 depth_bits_array[0] = 0;
402 depth_bits_array[1] = depth_bits;
403 depth_bits_array[2] = depth_bits;
404
405 /* Just like with the accumulation buffer, always provide some modes
406 * with a stencil buffer. It will be a sw fallback, but some apps won't
407 * care about that.
408 */
409 stencil_bits_array[0] = 0;
410 stencil_bits_array[1] = 0;
411 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
412
413 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
414 back_buffer_factor = (have_back_buffer) ? 3 : 1;
415
416 num_modes = depth_buffer_factor * back_buffer_factor * 4;
417
418 if ( pixel_bits == 16 ) {
419 fb_format = GL_RGB;
420 fb_type = GL_UNSIGNED_SHORT_5_6_5;
421 }
422 else {
423 fb_format = GL_BGRA;
424 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
425 }
426
427 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
428 m = modes;
429 if ( ! driFillInModes( & m, fb_format, fb_type,
430 depth_bits_array, stencil_bits_array, depth_buffer_factor,
431 back_buffer_modes, back_buffer_factor,
432 GLX_TRUE_COLOR ) ) {
433 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
434 __func__, __LINE__ );
435 return NULL;
436 }
437 if ( ! driFillInModes( & m, fb_format, fb_type,
438 depth_bits_array, stencil_bits_array, depth_buffer_factor,
439 back_buffer_modes, back_buffer_factor,
440 GLX_DIRECT_COLOR ) ) {
441 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
442 __func__, __LINE__ );
443 return NULL;
444 }
445
446 /* Mark the visual as slow if there are "fake" stencil bits.
447 */
448 for ( m = modes ; m != NULL ; m = m->next ) {
449 if ( (m->stencilBits != 0) && (m->stencilBits != stencil_bits) ) {
450 m->visualRating = GLX_SLOW_CONFIG;
451 }
452 }
453
454 return modes;
455 }
456
457
458 /**
459 * This is the bootstrap function for the driver. libGL supplies all of the
460 * requisite information about the system, and the driver initializes itself.
461 * This routine also fills in the linked list pointed to by \c driver_modes
462 * with the \c __GLcontextModes that the driver can support for windows or
463 * pbuffers.
464 *
465 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
466 * failure.
467 */
468 PUBLIC
469 void * __driCreateNewScreen_20050727( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
470 const __GLcontextModes * modes,
471 const __DRIversion * ddx_version,
472 const __DRIversion * dri_version,
473 const __DRIversion * drm_version,
474 const __DRIframebuffer * frame_buffer,
475 drmAddress pSAREA, int fd,
476 int internal_api_version,
477 const __DRIinterfaceMethods * interface,
478 __GLcontextModes ** driver_modes )
479
480 {
481 __DRIscreenPrivate *psp;
482 static const __DRIversion ddx_expected = { 1, 4, 0 };
483 static const __DRIversion dri_expected = { 4, 0, 0 };
484 static const __DRIversion drm_expected = { 1, 1, 0 };
485
486 dri_interface = interface;
487
488 if ( ! driCheckDriDdxDrmVersions2( "i915",
489 dri_version, & dri_expected,
490 ddx_version, & ddx_expected,
491 drm_version, & drm_expected ) ) {
492 return NULL;
493 }
494
495 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
496 ddx_version, dri_version, drm_version,
497 frame_buffer, pSAREA, fd,
498 internal_api_version, &intelAPI);
499 if ( psp != NULL ) {
500 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
501 *driver_modes = intelFillInModes( dri_priv->cpp * 8,
502 (dri_priv->cpp == 2) ? 16 : 24,
503 (dri_priv->cpp == 2) ? 0 : 8,
504 (dri_priv->backOffset != dri_priv->depthOffset) );
505
506 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
507 * enable the extensions. It just makes sure that all the dispatch offsets for all
508 * the extensions that *might* be enables are known. This is needed because the
509 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
510 * enable the extensions until we have a context pointer.
511 *
512 * Hello chicken. Hello egg. How are you two today?
513 */
514 driInitExtensions( NULL, card_extensions, GL_FALSE );
515 }
516
517 return (void *) psp;
518 }