intel_screen.c revision 7c81124d7c4a4d1da9f48cbf7e82ab1a3a970a7a
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 "vblank.h"
36#include "xmlpool.h"
37
38
39#include "intel_screen.h"
40
41#include "intel_buffers.h"
42#include "intel_tex.h"
43#include "intel_span.h"
44#include "intel_ioctl.h"
45#include "intel_fbo.h"
46#include "intel_chipset.h"
47
48#include "i915_drm.h"
49#include "i830_dri.h"
50#include "intel_regions.h"
51#include "intel_batchbuffer.h"
52#include "intel_bufmgr_ttm.h"
53
54PUBLIC const char __driConfigOptions[] =
55   DRI_CONF_BEGIN
56   DRI_CONF_SECTION_PERFORMANCE
57      DRI_CONF_FTHROTTLE_MODE(DRI_CONF_FTHROTTLE_IRQS)
58      DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
59      /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
60       * DRI_CONF_BO_REUSE_ALL
61       */
62      DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 0, "0:1")
63	 DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
64	    DRI_CONF_ENUM(0, "Disable buffer object reuse")
65	    DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
66	 DRI_CONF_DESC_END
67      DRI_CONF_OPT_END
68   DRI_CONF_SECTION_END
69   DRI_CONF_SECTION_QUALITY
70      DRI_CONF_FORCE_S3TC_ENABLE(false)
71      DRI_CONF_ALLOW_LARGE_TEXTURES(2)
72   DRI_CONF_SECTION_END
73   DRI_CONF_SECTION_DEBUG
74     DRI_CONF_NO_RAST(false)
75   DRI_CONF_SECTION_END
76DRI_CONF_END;
77
78const GLuint __driNConfigOptions = 6;
79
80#ifdef USE_NEW_INTERFACE
81static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
82#endif /*USE_NEW_INTERFACE */
83
84/**
85 * Map all the memory regions described by the screen.
86 * \return GL_TRUE if success, GL_FALSE if error.
87 */
88GLboolean
89intelMapScreenRegions(__DRIscreenPrivate * sPriv)
90{
91   intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
92
93   if (intelScreen->front.handle) {
94      if (drmMap(sPriv->fd,
95                 intelScreen->front.handle,
96                 intelScreen->front.size,
97                 (drmAddress *) & intelScreen->front.map) != 0) {
98         _mesa_problem(NULL, "drmMap(frontbuffer) failed!");
99         return GL_FALSE;
100      }
101   }
102   else {
103      _mesa_warning(NULL, "no front buffer handle in intelMapScreenRegions!");
104   }
105
106   if (0)
107      _mesa_printf("Back 0x%08x ", intelScreen->back.handle);
108   if (drmMap(sPriv->fd,
109              intelScreen->back.handle,
110              intelScreen->back.size,
111              (drmAddress *) & intelScreen->back.map) != 0) {
112      intelUnmapScreenRegions(intelScreen);
113      return GL_FALSE;
114   }
115
116   if (intelScreen->third.handle) {
117      if (0)
118	 _mesa_printf("Third 0x%08x ", intelScreen->third.handle);
119      if (drmMap(sPriv->fd,
120		 intelScreen->third.handle,
121		 intelScreen->third.size,
122		 (drmAddress *) & intelScreen->third.map) != 0) {
123	 intelUnmapScreenRegions(intelScreen);
124	 return GL_FALSE;
125      }
126   }
127
128   if (0)
129      _mesa_printf("Depth 0x%08x ", intelScreen->depth.handle);
130   if (drmMap(sPriv->fd,
131              intelScreen->depth.handle,
132              intelScreen->depth.size,
133              (drmAddress *) & intelScreen->depth.map) != 0) {
134      intelUnmapScreenRegions(intelScreen);
135      return GL_FALSE;
136   }
137
138   if (0)
139      _mesa_printf("TEX 0x%08x ", intelScreen->tex.handle);
140   if (intelScreen->tex.size != 0) {
141      if (drmMap(sPriv->fd,
142		 intelScreen->tex.handle,
143		 intelScreen->tex.size,
144		 (drmAddress *) & intelScreen->tex.map) != 0) {
145	 intelUnmapScreenRegions(intelScreen);
146	 return GL_FALSE;
147      }
148   }
149
150   if (0)
151      printf("Mappings:  front: %p  back: %p  third: %p  depth: %p  tex: %p\n",
152             intelScreen->front.map,
153             intelScreen->back.map, intelScreen->third.map,
154             intelScreen->depth.map, intelScreen->tex.map);
155   return GL_TRUE;
156}
157
158void
159intelUnmapScreenRegions(intelScreenPrivate * intelScreen)
160{
161#define REALLY_UNMAP 1
162   if (intelScreen->front.map) {
163#if REALLY_UNMAP
164      if (drmUnmap(intelScreen->front.map, intelScreen->front.size) != 0)
165         printf("drmUnmap front failed!\n");
166#endif
167      intelScreen->front.map = NULL;
168   }
169   if (intelScreen->back.map) {
170#if REALLY_UNMAP
171      if (drmUnmap(intelScreen->back.map, intelScreen->back.size) != 0)
172         printf("drmUnmap back failed!\n");
173#endif
174      intelScreen->back.map = NULL;
175   }
176   if (intelScreen->third.map) {
177#if REALLY_UNMAP
178      if (drmUnmap(intelScreen->third.map, intelScreen->third.size) != 0)
179         printf("drmUnmap third failed!\n");
180#endif
181      intelScreen->third.map = NULL;
182   }
183   if (intelScreen->depth.map) {
184#if REALLY_UNMAP
185      drmUnmap(intelScreen->depth.map, intelScreen->depth.size);
186      intelScreen->depth.map = NULL;
187#endif
188   }
189   if (intelScreen->tex.map) {
190#if REALLY_UNMAP
191      drmUnmap(intelScreen->tex.map, intelScreen->tex.size);
192      intelScreen->tex.map = NULL;
193#endif
194   }
195}
196
197
198static void
199intelPrintDRIInfo(intelScreenPrivate * intelScreen,
200                  __DRIscreenPrivate * sPriv, I830DRIPtr gDRIPriv)
201{
202   fprintf(stderr, "*** Front size:   0x%x  offset: 0x%x  pitch: %d\n",
203           intelScreen->front.size, intelScreen->front.offset,
204           intelScreen->pitch);
205   fprintf(stderr, "*** Back size:    0x%x  offset: 0x%x  pitch: %d\n",
206           intelScreen->back.size, intelScreen->back.offset,
207           intelScreen->pitch);
208   fprintf(stderr, "*** Depth size:   0x%x  offset: 0x%x  pitch: %d\n",
209           intelScreen->depth.size, intelScreen->depth.offset,
210           intelScreen->pitch);
211   fprintf(stderr, "*** Texture size: 0x%x  offset: 0x%x\n",
212           intelScreen->tex.size, intelScreen->tex.offset);
213   fprintf(stderr, "*** Memory : 0x%x\n", gDRIPriv->mem);
214}
215
216
217static void
218intelPrintSAREA(const struct drm_i915_sarea * sarea)
219{
220   fprintf(stderr, "SAREA: sarea width %d  height %d\n", sarea->width,
221           sarea->height);
222   fprintf(stderr, "SAREA: pitch: %d\n", sarea->pitch);
223   fprintf(stderr,
224           "SAREA: front offset: 0x%08x  size: 0x%x  handle: 0x%x\n",
225           sarea->front_offset, sarea->front_size,
226           (unsigned) sarea->front_handle);
227   fprintf(stderr,
228           "SAREA: back  offset: 0x%08x  size: 0x%x  handle: 0x%x\n",
229           sarea->back_offset, sarea->back_size,
230           (unsigned) sarea->back_handle);
231   fprintf(stderr, "SAREA: depth offset: 0x%08x  size: 0x%x  handle: 0x%x\n",
232           sarea->depth_offset, sarea->depth_size,
233           (unsigned) sarea->depth_handle);
234   fprintf(stderr, "SAREA: tex   offset: 0x%08x  size: 0x%x  handle: 0x%x\n",
235           sarea->tex_offset, sarea->tex_size, (unsigned) sarea->tex_handle);
236}
237
238
239/**
240 * A number of the screen parameters are obtained/computed from
241 * information in the SAREA.  This function updates those parameters.
242 */
243void
244intelUpdateScreenFromSAREA(intelScreenPrivate * intelScreen,
245                           struct drm_i915_sarea * sarea)
246{
247   intelScreen->width = sarea->width;
248   intelScreen->height = sarea->height;
249   intelScreen->pitch = sarea->pitch;
250
251   intelScreen->front.offset = sarea->front_offset;
252   intelScreen->front.handle = sarea->front_handle;
253   intelScreen->front.size = sarea->front_size;
254   intelScreen->front.tiled = sarea->front_tiled;
255
256   intelScreen->back.offset = sarea->back_offset;
257   intelScreen->back.handle = sarea->back_handle;
258   intelScreen->back.size = sarea->back_size;
259   intelScreen->back.tiled = sarea->back_tiled;
260
261   if (intelScreen->driScrnPriv->ddx_version.minor >= 8) {
262      intelScreen->third.offset = sarea->third_offset;
263      intelScreen->third.handle = sarea->third_handle;
264      intelScreen->third.size = sarea->third_size;
265      intelScreen->third.tiled = sarea->third_tiled;
266   }
267
268   intelScreen->depth.offset = sarea->depth_offset;
269   intelScreen->depth.handle = sarea->depth_handle;
270   intelScreen->depth.size = sarea->depth_size;
271   intelScreen->depth.tiled = sarea->depth_tiled;
272
273   if (intelScreen->driScrnPriv->ddx_version.minor >= 9) {
274      intelScreen->front.bo_handle = sarea->front_bo_handle;
275      intelScreen->back.bo_handle = sarea->back_bo_handle;
276      intelScreen->third.bo_handle = sarea->third_bo_handle;
277      intelScreen->depth.bo_handle = sarea->depth_bo_handle;
278   } else {
279      intelScreen->front.bo_handle = -1;
280      intelScreen->back.bo_handle = -1;
281      intelScreen->third.bo_handle = -1;
282      intelScreen->depth.bo_handle = -1;
283   }
284
285   intelScreen->tex.offset = sarea->tex_offset;
286   intelScreen->logTextureGranularity = sarea->log_tex_granularity;
287   intelScreen->tex.handle = sarea->tex_handle;
288   intelScreen->tex.size = sarea->tex_size;
289
290   if (0)
291      intelPrintSAREA(sarea);
292}
293
294
295/**
296 * DRI2 entrypoint
297 */
298static void
299intelHandleDrawableConfig(__DRIdrawablePrivate *dPriv,
300			  __DRIcontextPrivate *pcp,
301			  __DRIDrawableConfigEvent *event)
302{
303   struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
304   struct intel_region *region = NULL;
305   struct intel_renderbuffer *rb, *depth_rb, *stencil_rb;
306   struct intel_context *intel = pcp->driverPrivate;
307   int cpp, pitch;
308
309   cpp = intel->ctx.Visual.rgbBits / 8;
310   pitch = ((cpp * dPriv->w + 63) & ~63) / cpp;
311
312   rb = intel_fb->color_rb[1];
313   if (rb) {
314      region = intel_region_alloc(intel, cpp, pitch, dPriv->h);
315      intel_renderbuffer_set_region(rb, region);
316   }
317
318   rb = intel_fb->color_rb[2];
319   if (rb) {
320      region = intel_region_alloc(intel, cpp, pitch, dPriv->h);
321      intel_renderbuffer_set_region(rb, region);
322   }
323
324   depth_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
325   stencil_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
326   if (depth_rb || stencil_rb)
327      region = intel_region_alloc(intel, cpp, pitch, dPriv->h);
328   if (depth_rb)
329      intel_renderbuffer_set_region(depth_rb, region);
330   if (stencil_rb)
331      intel_renderbuffer_set_region(stencil_rb, region);
332
333   /* FIXME: Tell the X server about the regions we just allocated and
334    * attached. */
335}
336
337#define BUFFER_FLAG_TILED 0x0100
338
339/**
340 * DRI2 entrypoint
341 */
342static void
343intelHandleBufferAttach(__DRIdrawablePrivate *dPriv,
344			__DRIcontextPrivate *pcp,
345			__DRIBufferAttachEvent *ba)
346{
347   struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
348   struct intel_renderbuffer *rb;
349   struct intel_region *region;
350   struct intel_context *intel = pcp->driverPrivate;
351   GLuint tiled;
352
353   switch (ba->buffer.attachment) {
354   case DRI_DRAWABLE_BUFFER_FRONT_LEFT:
355      rb = intel_fb->color_rb[0];
356      break;
357
358   case DRI_DRAWABLE_BUFFER_BACK_LEFT:
359      rb = intel_fb->color_rb[0];
360      break;
361
362   case DRI_DRAWABLE_BUFFER_DEPTH:
363     rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
364     break;
365
366   case DRI_DRAWABLE_BUFFER_STENCIL:
367     rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
368     break;
369
370   case DRI_DRAWABLE_BUFFER_ACCUM:
371   default:
372      fprintf(stderr, "unhandled buffer attach event, attacment type %d\n",
373	      ba->buffer.attachment);
374      return;
375   }
376
377#if 0
378   /* FIXME: Add this so we can filter out when the X server sends us
379    * attachment events for the buffers we just allocated.  Need to
380    * get the BO handle for a render buffer. */
381   if (intel_renderbuffer_get_region_handle(rb) == ba->buffer.handle)
382      return;
383#endif
384
385   tiled = (ba->buffer.flags & BUFFER_FLAG_TILED) > 0;
386   region = intel_region_alloc_for_handle(intel, ba->buffer.cpp,
387					  ba->buffer.pitch / ba->buffer.cpp,
388					  dPriv->h, tiled,
389					  ba->buffer.handle);
390
391   intel_renderbuffer_set_region(rb, region);
392}
393
394static const __DRItexOffsetExtension intelTexOffsetExtension = {
395   { __DRI_TEX_OFFSET },
396   intelSetTexOffset,
397};
398
399static const __DRItexBufferExtension intelTexBufferExtension = {
400    { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
401   intelSetTexBuffer,
402};
403
404static const __DRIextension *intelScreenExtensions[] = {
405    &driReadDrawableExtension,
406    &driCopySubBufferExtension.base,
407    &driSwapControlExtension.base,
408    &driFrameTrackingExtension.base,
409    &driMediaStreamCounterExtension.base,
410    &intelTexOffsetExtension.base,
411    &intelTexBufferExtension.base,
412    NULL
413};
414
415static GLboolean
416intel_get_param(__DRIscreenPrivate *psp, int param, int *value)
417{
418   int ret;
419   struct drm_i915_getparam gp;
420
421   gp.param = param;
422   gp.value = value;
423
424   ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
425   if (ret) {
426      fprintf(stderr, "drm_i915_getparam: %d\n", ret);
427      return GL_FALSE;
428   }
429
430   return GL_TRUE;
431}
432
433static GLboolean intelInitDriver(__DRIscreenPrivate *sPriv)
434{
435   intelScreenPrivate *intelScreen;
436   I830DRIPtr gDRIPriv = (I830DRIPtr) sPriv->pDevPriv;
437   struct drm_i915_sarea *sarea;
438
439   if (sPriv->devPrivSize != sizeof(I830DRIRec)) {
440      fprintf(stderr,
441              "\nERROR!  sizeof(I830DRIRec) does not match passed size from device driver\n");
442      return GL_FALSE;
443   }
444
445   /* Allocate the private area */
446   intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
447   if (!intelScreen) {
448      fprintf(stderr, "\nERROR!  Allocating private area failed\n");
449      return GL_FALSE;
450   }
451   /* parse information in __driConfigOptions */
452   driParseOptionInfo(&intelScreen->optionCache,
453                      __driConfigOptions, __driNConfigOptions);
454
455   intelScreen->driScrnPriv = sPriv;
456   sPriv->private = (void *) intelScreen;
457   intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
458   sarea = (struct drm_i915_sarea *)
459      (((GLubyte *) sPriv->pSAREA) + intelScreen->sarea_priv_offset);
460
461   intelScreen->deviceID = gDRIPriv->deviceID;
462
463   intelUpdateScreenFromSAREA(intelScreen, sarea);
464
465   if (!intelMapScreenRegions(sPriv)) {
466      fprintf(stderr, "\nERROR!  mapping regions\n");
467      _mesa_free(intelScreen);
468      sPriv->private = NULL;
469      return GL_FALSE;
470   }
471
472   intelScreen->sarea_priv_offset = gDRIPriv->sarea_priv_offset;
473
474   if (0)
475      intelPrintDRIInfo(intelScreen, sPriv, gDRIPriv);
476
477   intelScreen->drmMinor = sPriv->drm_version.minor;
478
479   /* Determine if IRQs are active? */
480   if (!intel_get_param(sPriv, I915_PARAM_IRQ_ACTIVE,
481			&intelScreen->irq_active))
482      return GL_FALSE;
483
484   /* Determine if batchbuffers are allowed */
485   if (!intel_get_param(sPriv, I915_PARAM_ALLOW_BATCHBUFFER,
486			&intelScreen->allow_batchbuffer))
487      return GL_FALSE;
488
489   sPriv->extensions = intelScreenExtensions;
490
491   return GL_TRUE;
492}
493
494
495static void
496intelDestroyScreen(__DRIscreenPrivate * sPriv)
497{
498   intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
499
500   intelUnmapScreenRegions(intelScreen);
501
502   FREE(intelScreen);
503   sPriv->private = NULL;
504}
505
506
507/**
508 * This is called when we need to set up GL rendering to a new X window.
509 */
510static GLboolean
511intelCreateBuffer(__DRIscreenPrivate * driScrnPriv,
512                  __DRIdrawablePrivate * driDrawPriv,
513                  const __GLcontextModes * mesaVis, GLboolean isPixmap)
514{
515   intelScreenPrivate *screen = (intelScreenPrivate *) driScrnPriv->private;
516
517   if (isPixmap) {
518      return GL_FALSE;          /* not implemented */
519   }
520   else {
521      GLboolean swStencil = (mesaVis->stencilBits > 0 &&
522                             mesaVis->depthBits != 24);
523      GLenum rgbFormat = (mesaVis->redBits == 5 ? GL_RGB5 : GL_RGBA8);
524
525      struct intel_framebuffer *intel_fb = CALLOC_STRUCT(intel_framebuffer);
526
527      if (!intel_fb)
528	 return GL_FALSE;
529
530      _mesa_initialize_framebuffer(&intel_fb->Base, mesaVis);
531
532      /* setup the hardware-based renderbuffers */
533      {
534         intel_fb->color_rb[0] = intel_create_renderbuffer(rgbFormat);
535         _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT,
536				&intel_fb->color_rb[0]->Base);
537      }
538
539      if (mesaVis->doubleBufferMode) {
540         intel_fb->color_rb[1] = intel_create_renderbuffer(rgbFormat);
541         _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT,
542				&intel_fb->color_rb[1]->Base);
543
544	 if (screen->third.handle) {
545	    struct gl_renderbuffer *tmp_rb = NULL;
546
547	    intel_fb->color_rb[2] = intel_create_renderbuffer(rgbFormat);
548	    _mesa_reference_renderbuffer(&tmp_rb, &intel_fb->color_rb[2]->Base);
549	 }
550      }
551
552      if (mesaVis->depthBits == 24) {
553	 if (mesaVis->stencilBits == 8) {
554	    /* combined depth/stencil buffer */
555	    struct intel_renderbuffer *depthStencilRb
556	       = intel_create_renderbuffer(GL_DEPTH24_STENCIL8_EXT);
557	    /* note: bind RB to two attachment points */
558	    _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
559				   &depthStencilRb->Base);
560	    _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_STENCIL,
561				   &depthStencilRb->Base);
562	 } else {
563	    struct intel_renderbuffer *depthRb
564	       = intel_create_renderbuffer(GL_DEPTH_COMPONENT24);
565	    _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH,
566				   &depthRb->Base);
567	 }
568      }
569      else if (mesaVis->depthBits == 16) {
570         /* just 16-bit depth buffer, no hw stencil */
571         struct intel_renderbuffer *depthRb
572            = intel_create_renderbuffer(GL_DEPTH_COMPONENT16);
573         _mesa_add_renderbuffer(&intel_fb->Base, BUFFER_DEPTH, &depthRb->Base);
574      }
575
576      /* now add any/all software-based renderbuffers we may need */
577      _mesa_add_soft_renderbuffers(&intel_fb->Base,
578                                   GL_FALSE, /* never sw color */
579                                   GL_FALSE, /* never sw depth */
580                                   swStencil, mesaVis->accumRedBits > 0,
581                                   GL_FALSE, /* never sw alpha */
582                                   GL_FALSE  /* never sw aux */ );
583      driDrawPriv->driverPrivate = (void *) intel_fb;
584
585      return GL_TRUE;
586   }
587}
588
589static void
590intelDestroyBuffer(__DRIdrawablePrivate * driDrawPriv)
591{
592   _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
593}
594
595
596/**
597 * Get information about previous buffer swaps.
598 */
599static int
600intelGetSwapInfo(__DRIdrawablePrivate * dPriv, __DRIswapInfo * sInfo)
601{
602   struct intel_framebuffer *intel_fb;
603
604   if ((dPriv == NULL) || (dPriv->driverPrivate == NULL)
605       || (sInfo == NULL)) {
606      return -1;
607   }
608
609   intel_fb = dPriv->driverPrivate;
610   sInfo->swap_count = intel_fb->swap_count;
611   sInfo->swap_ust = intel_fb->swap_ust;
612   sInfo->swap_missed_count = intel_fb->swap_missed_count;
613
614   sInfo->swap_missed_usage = (sInfo->swap_missed_count != 0)
615      ? driCalculateSwapUsage(dPriv, 0, intel_fb->swap_missed_ust)
616      : 0.0;
617
618   return 0;
619}
620
621
622/* There are probably better ways to do this, such as an
623 * init-designated function to register chipids and createcontext
624 * functions.
625 */
626extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
627                                   __DRIcontextPrivate * driContextPriv,
628                                   void *sharedContextPrivate);
629
630extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
631                                   __DRIcontextPrivate * driContextPriv,
632                                   void *sharedContextPrivate);
633extern GLboolean brwCreateContext(const __GLcontextModes * mesaVis,
634				  __DRIcontextPrivate * driContextPriv,
635				  void *sharedContextPrivate);
636
637static GLboolean
638intelCreateContext(const __GLcontextModes * mesaVis,
639                   __DRIcontextPrivate * driContextPriv,
640                   void *sharedContextPrivate)
641{
642   __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
643   intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
644
645#ifdef I915
646   if (IS_9XX(intelScreen->deviceID)) {
647      if (!IS_965(intelScreen->deviceID)) {
648	 return i915CreateContext(mesaVis, driContextPriv,
649				  sharedContextPrivate);
650      }
651   } else {
652      return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
653   }
654#else
655   if (IS_965(intelScreen->deviceID))
656      return brwCreateContext(mesaVis, driContextPriv, sharedContextPrivate);
657#endif
658   fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
659   return GL_FALSE;
660}
661
662
663static __DRIconfig **
664intelFillInModes(__DRIscreenPrivate *psp,
665		 unsigned pixel_bits, unsigned depth_bits,
666                 unsigned stencil_bits, GLboolean have_back_buffer)
667{
668   __DRIconfig **configs;
669   __GLcontextModes *m;
670   unsigned depth_buffer_factor;
671   unsigned back_buffer_factor;
672   GLenum fb_format;
673   GLenum fb_type;
674   int i;
675
676   /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
677    * support pageflipping at all.
678    */
679   static const GLenum back_buffer_modes[] = {
680      GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
681   };
682
683   u_int8_t depth_bits_array[3];
684   u_int8_t stencil_bits_array[3];
685
686   depth_bits_array[0] = 0;
687   depth_bits_array[1] = depth_bits;
688   depth_bits_array[2] = depth_bits;
689
690   /* Just like with the accumulation buffer, always provide some modes
691    * with a stencil buffer.  It will be a sw fallback, but some apps won't
692    * care about that.
693    */
694   stencil_bits_array[0] = 0;
695   stencil_bits_array[1] = 0;
696   if (depth_bits == 24)
697      stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
698
699   stencil_bits_array[2] = (stencil_bits == 0) ? 8 : stencil_bits;
700
701   depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 3 : 1;
702   back_buffer_factor = (have_back_buffer) ? 3 : 1;
703
704   if (pixel_bits == 16) {
705      fb_format = GL_RGB;
706      fb_type = GL_UNSIGNED_SHORT_5_6_5;
707   }
708   else {
709      fb_format = GL_BGRA;
710      fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
711   }
712
713   configs = driCreateConfigs(fb_format, fb_type,
714			      depth_bits_array, stencil_bits_array,
715			      depth_buffer_factor, back_buffer_modes,
716			      back_buffer_factor);
717   if (configs == NULL) {
718    fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
719              __LINE__);
720      return NULL;
721   }
722
723   /* Mark the visual as slow if there are "fake" stencil bits.
724    */
725   for (i = 0; configs[i]; i++) {
726      m = &configs[i]->modes;
727      if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
728         m->visualRating = GLX_SLOW_CONFIG;
729      }
730   }
731
732   return configs;
733}
734
735
736/**
737 * This is the driver specific part of the createNewScreen entry point.
738 * Called when using legacy DRI.
739 *
740 * \todo maybe fold this into intelInitDriver
741 *
742 * \return the __GLcontextModes supported by this driver
743 */
744static const __DRIconfig **intelInitScreen(__DRIscreenPrivate *psp)
745{
746#ifdef I915
747   static const __DRIversion ddx_expected = { 1, 5, 0 };
748#else
749   static const __DRIversion ddx_expected = { 1, 6, 0 };
750#endif
751   static const __DRIversion dri_expected = { 4, 0, 0 };
752   static const __DRIversion drm_expected = { 1, 5, 0 };
753   I830DRIPtr dri_priv = (I830DRIPtr) psp->pDevPriv;
754
755   if (!driCheckDriDdxDrmVersions2("i915",
756                                   &psp->dri_version, &dri_expected,
757                                   &psp->ddx_version, &ddx_expected,
758                                   &psp->drm_version, &drm_expected)) {
759      return NULL;
760   }
761
762   /* Calling driInitExtensions here, with a NULL context pointer,
763    * does not actually enable the extensions.  It just makes sure
764    * that all the dispatch offsets for all the extensions that
765    * *might* be enables are known.  This is needed because the
766    * dispatch offsets need to be known when _mesa_context_create is
767    * called, but we can't enable the extensions until we have a
768    * context pointer.
769    *
770    * Hello chicken.  Hello egg.  How are you two today?
771    */
772   intelInitExtensions(NULL, GL_TRUE);
773
774   if (!intelInitDriver(psp))
775       return NULL;
776
777   psp->extensions = intelScreenExtensions;
778
779   return (const __DRIconfig **)
780       intelFillInModes(psp, dri_priv->cpp * 8,
781			(dri_priv->cpp == 2) ? 16 : 24,
782			(dri_priv->cpp == 2) ? 0  : 8, 1);
783}
784
785struct intel_context *intelScreenContext(intelScreenPrivate *intelScreen)
786{
787  /*
788   * This should probably change to have the screen allocate a dummy
789   * context at screen creation. For now just use the current context.
790   */
791
792  GET_CURRENT_CONTEXT(ctx);
793  if (ctx == NULL) {
794     _mesa_problem(NULL, "No current context in intelScreenContext\n");
795     return NULL;
796  }
797  return intel_context(ctx);
798}
799
800/**
801 * This is the driver specific part of the createNewScreen entry point.
802 * Called when using DRI2.
803 *
804 * \return the __GLcontextModes supported by this driver
805 */
806static const
807__DRIconfig **intelInitScreen2(__DRIscreenPrivate *psp)
808{
809   intelScreenPrivate *intelScreen;
810
811   /* Calling driInitExtensions here, with a NULL context pointer,
812    * does not actually enable the extensions.  It just makes sure
813    * that all the dispatch offsets for all the extensions that
814    * *might* be enables are known.  This is needed because the
815    * dispatch offsets need to be known when _mesa_context_create is
816    * called, but we can't enable the extensions until we have a
817    * context pointer.
818    *
819    * Hello chicken.  Hello egg.  How are you two today?
820    */
821   intelInitExtensions(NULL, GL_TRUE);
822
823   /* Allocate the private area */
824   intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
825   if (!intelScreen) {
826      fprintf(stderr, "\nERROR!  Allocating private area failed\n");
827      return GL_FALSE;
828   }
829   /* parse information in __driConfigOptions */
830   driParseOptionInfo(&intelScreen->optionCache,
831                      __driConfigOptions, __driNConfigOptions);
832
833   intelScreen->driScrnPriv = psp;
834   psp->private = (void *) intelScreen;
835
836   intelScreen->drmMinor = psp->drm_version.minor;
837
838   /* Determine chipset ID? */
839   if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
840			&intelScreen->deviceID))
841      return GL_FALSE;
842
843   /* Determine if IRQs are active? */
844   if (!intel_get_param(psp, I915_PARAM_IRQ_ACTIVE,
845			&intelScreen->irq_active))
846      return GL_FALSE;
847
848   /* Determine if batchbuffers are allowed */
849   if (!intel_get_param(psp, I915_PARAM_ALLOW_BATCHBUFFER,
850			&intelScreen->allow_batchbuffer))
851      return GL_FALSE;
852
853   if (!intelScreen->allow_batchbuffer) {
854      fprintf(stderr, "batch buffer not allowed\n");
855      return GL_FALSE;
856   }
857
858   psp->extensions = intelScreenExtensions;
859
860   return driConcatConfigs(intelFillInModes(psp, 16, 16, 0, 1),
861			   intelFillInModes(psp, 32, 24, 8, 1));
862}
863
864const struct __DriverAPIRec driDriverAPI = {
865   .InitScreen		 = intelInitScreen,
866   .DestroyScreen	 = intelDestroyScreen,
867   .CreateContext	 = intelCreateContext,
868   .DestroyContext	 = intelDestroyContext,
869   .CreateBuffer	 = intelCreateBuffer,
870   .DestroyBuffer	 = intelDestroyBuffer,
871   .SwapBuffers		 = intelSwapBuffers,
872   .MakeCurrent		 = intelMakeCurrent,
873   .UnbindContext	 = intelUnbindContext,
874   .GetSwapInfo		 = intelGetSwapInfo,
875   .GetDrawableMSC	 = driDrawableGetMSC32,
876   .WaitForMSC		 = driWaitForMSC32,
877   .CopySubBuffer	 = intelCopySubBuffer,
878
879   .InitScreen2		 = intelInitScreen2,
880   .HandleDrawableConfig = intelHandleDrawableConfig,
881   .HandleBufferAttach	 = intelHandleBufferAttach,
882};
883