57c7504bf28e2bfc8c6524b8babc4c2c859047e3
[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 #if 0
243 driDrawPriv->driverPrivate = (void *)
244 _mesa_create_framebuffer(mesaVis,
245 GL_FALSE, /* software depth buffer? */
246 swStencil,
247 mesaVis->accumRedBits > 0,
248 GL_FALSE /* s/w alpha planes */);
249 #else
250 struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
251
252 {
253 driRenderbuffer *frontRb
254 = driNewRenderbuffer(GL_RGBA, screen->cpp,
255 screen->frontOffset, screen->frontPitch);
256 intelSetSpanFunctions(frontRb, mesaVis);
257 _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
258 }
259
260 if (mesaVis->doubleBufferMode) {
261 driRenderbuffer *backRb
262 = driNewRenderbuffer(GL_RGBA, screen->cpp,
263 screen->backOffset, screen->backPitch);
264 intelSetSpanFunctions(backRb, mesaVis);
265 _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
266 }
267
268 if (mesaVis->depthBits == 16) {
269 driRenderbuffer *depthRb
270 = driNewRenderbuffer(GL_DEPTH_COMPONENT16, screen->cpp,
271 screen->depthOffset, screen->depthPitch);
272 intelSetSpanFunctions(depthRb, mesaVis);
273 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
274 }
275 else if (mesaVis->depthBits == 24) {
276 driRenderbuffer *depthRb
277 = driNewRenderbuffer(GL_DEPTH_COMPONENT24, screen->cpp,
278 screen->depthOffset, screen->depthPitch);
279 intelSetSpanFunctions(depthRb, mesaVis);
280 _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
281 }
282
283 if (mesaVis->stencilBits > 0 && !swStencil) {
284 driRenderbuffer *stencilRb
285 = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT, screen->cpp,
286 screen->depthOffset, screen->depthPitch);
287 intelSetSpanFunctions(stencilRb, mesaVis);
288 _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
289 }
290
291 _mesa_add_soft_renderbuffers(fb,
292 GL_FALSE, /* color */
293 GL_FALSE, /* depth */
294 swStencil,
295 mesaVis->accumRedBits > 0,
296 GL_FALSE, /* alpha */
297 GL_FALSE /* aux */);
298 driDrawPriv->driverPrivate = (void *) fb;
299 #endif
300 return (driDrawPriv->driverPrivate != NULL);
301 }
302 }
303
304 static void intelDestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
305 {
306 _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
307 }
308
309
310 /* There are probably better ways to do this, such as an
311 * init-designated function to register chipids and createcontext
312 * functions.
313 */
314 extern GLboolean i830CreateContext( const __GLcontextModes *mesaVis,
315 __DRIcontextPrivate *driContextPriv,
316 void *sharedContextPrivate);
317
318 extern GLboolean i915CreateContext( const __GLcontextModes *mesaVis,
319 __DRIcontextPrivate *driContextPriv,
320 void *sharedContextPrivate);
321
322
323
324
325 static GLboolean intelCreateContext( const __GLcontextModes *mesaVis,
326 __DRIcontextPrivate *driContextPriv,
327 void *sharedContextPrivate)
328 {
329 __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
330 intelScreenPrivate *intelScreen = (intelScreenPrivate *)sPriv->private;
331
332 switch (intelScreen->deviceID) {
333 case PCI_CHIP_845_G:
334 case PCI_CHIP_I830_M:
335 case PCI_CHIP_I855_GM:
336 case PCI_CHIP_I865_G:
337 return i830CreateContext( mesaVis, driContextPriv,
338 sharedContextPrivate );
339
340 case PCI_CHIP_I915_G:
341 case PCI_CHIP_I915_GM:
342 case PCI_CHIP_I945_G:
343 return i915CreateContext( mesaVis, driContextPriv,
344 sharedContextPrivate );
345
346 default:
347 fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
348 return GL_FALSE;
349 }
350 }
351
352
353 static const struct __DriverAPIRec intelAPI = {
354 .InitDriver = intelInitDriver,
355 .DestroyScreen = intelDestroyScreen,
356 .CreateContext = intelCreateContext,
357 .DestroyContext = intelDestroyContext,
358 .CreateBuffer = intelCreateBuffer,
359 .DestroyBuffer = intelDestroyBuffer,
360 .SwapBuffers = intelSwapBuffers,
361 .MakeCurrent = intelMakeCurrent,
362 .UnbindContext = intelUnbindContext,
363 .GetSwapInfo = NULL,
364 .GetMSC = NULL,
365 .WaitForMSC = NULL,
366 .WaitForSBC = NULL,
367 .SwapBuffersMSC = NULL
368 };
369
370
371 static __GLcontextModes *
372 intelFillInModes( unsigned pixel_bits, unsigned depth_bits,
373 unsigned stencil_bits, GLboolean have_back_buffer )
374 {
375 __GLcontextModes * modes;
376 __GLcontextModes * m;
377 unsigned num_modes;
378 unsigned depth_buffer_factor;
379 unsigned back_buffer_factor;
380 GLenum fb_format;
381 GLenum fb_type;
382
383 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
384 * support pageflipping at all.
385 */
386 static const GLenum back_buffer_modes[] = {
387 GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
388 };
389
390 u_int8_t depth_bits_array[3];
391 u_int8_t stencil_bits_array[3];
392
393
394 depth_bits_array[0] = 0;
395 depth_bits_array[1] = depth_bits;
396 depth_bits_array[2] = depth_bits;
397
398 /* Just like with the accumulation buffer, always provide some modes
399 * with a stencil buffer. It will be a sw fallback, but some apps won't
400 * care about that.
401 */
402 stencil_bits_array[0] = 0;
403 stencil_bits_array[1] = 0;
404 stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
405
406 depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
407 back_buffer_factor = (have_back_buffer) ? 3 : 1;
408
409 num_modes = depth_buffer_factor * back_buffer_factor * 4;
410
411 if ( pixel_bits == 16 ) {
412 fb_format = GL_RGB;
413 fb_type = GL_UNSIGNED_SHORT_5_6_5;
414 }
415 else {
416 fb_format = GL_BGRA;
417 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
418 }
419
420 modes = (*dri_interface->createContextModes)( num_modes, sizeof( __GLcontextModes ) );
421 m = modes;
422 if ( ! driFillInModes( & m, fb_format, fb_type,
423 depth_bits_array, stencil_bits_array, depth_buffer_factor,
424 back_buffer_modes, back_buffer_factor,
425 GLX_TRUE_COLOR ) ) {
426 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
427 __func__, __LINE__ );
428 return NULL;
429 }
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_DIRECT_COLOR ) ) {
434 fprintf( stderr, "[%s:%u] Error creating FBConfig!\n",
435 __func__, __LINE__ );
436 return NULL;
437 }
438
439 /* Mark the visual as slow if there are "fake" stencil bits.
440 */
441 for ( m = modes ; m != NULL ; m = m->next ) {
442 if ( (m->stencilBits != 0) && (m->stencilBits != stencil_bits) ) {
443 m->visualRating = GLX_SLOW_CONFIG;
444 }
445 }
446
447 return modes;
448 }
449
450
451 /**
452 * This is the bootstrap function for the driver. libGL supplies all of the
453 * requisite information about the system, and the driver initializes itself.
454 * This routine also fills in the linked list pointed to by \c driver_modes
455 * with the \c __GLcontextModes that the driver can support for windows or
456 * pbuffers.
457 *
458 * \return A pointer to a \c __DRIscreenPrivate on success, or \c NULL on
459 * failure.
460 */
461 PUBLIC
462 void * __driCreateNewScreen_20050727( __DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
463 const __GLcontextModes * modes,
464 const __DRIversion * ddx_version,
465 const __DRIversion * dri_version,
466 const __DRIversion * drm_version,
467 const __DRIframebuffer * frame_buffer,
468 drmAddress pSAREA, int fd,
469 int internal_api_version,
470 const __DRIinterfaceMethods * interface,
471 __GLcontextModes ** driver_modes )
472
473 {
474 __DRIscreenPrivate *psp;
475 static const __DRIversion ddx_expected = { 1, 4, 0 };
476 static const __DRIversion dri_expected = { 4, 0, 0 };
477 static const __DRIversion drm_expected = { 1, 1, 0 };
478
479 dri_interface = interface;
480
481 if ( ! driCheckDriDdxDrmVersions2( "i915",
482 dri_version, & dri_expected,
483 ddx_version, & ddx_expected,
484 drm_version, & drm_expected ) ) {
485 return NULL;
486 }
487
488 psp = __driUtilCreateNewScreen(dpy, scrn, psc, NULL,
489 ddx_version, dri_version, drm_version,
490 frame_buffer, pSAREA, fd,
491 internal_api_version, &intelAPI);
492 if ( psp != NULL ) {
493 I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
494 *driver_modes = intelFillInModes( dri_priv->cpp * 8,
495 (dri_priv->cpp == 2) ? 16 : 24,
496 (dri_priv->cpp == 2) ? 0 : 8,
497 (dri_priv->backOffset != dri_priv->depthOffset) );
498
499 /* Calling driInitExtensions here, with a NULL context pointer, does not actually
500 * enable the extensions. It just makes sure that all the dispatch offsets for all
501 * the extensions that *might* be enables are known. This is needed because the
502 * dispatch offsets need to be known when _mesa_context_create is called, but we can't
503 * enable the extensions until we have a context pointer.
504 *
505 * Hello chicken. Hello egg. How are you two today?
506 */
507 driInitExtensions( NULL, card_extensions, GL_FALSE );
508 }
509
510 return (void *) psp;
511 }