OSDN Git Service

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