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