intel: Rely on intel_region_reference()'s support of *dst != NULL.
[profile/ivi/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 <errno.h>
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "main/hash.h"
34 #include "main/fbobject.h"
35 #include "main/mfeatures.h"
36
37 #include "utils.h"
38 #include "xmlpool.h"
39
40 PUBLIC const char __driConfigOptions[] =
41    DRI_CONF_BEGIN
42    DRI_CONF_SECTION_PERFORMANCE
43       DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
44       /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
45        * DRI_CONF_BO_REUSE_ALL
46        */
47       DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
48          DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
49             DRI_CONF_ENUM(0, "Disable buffer object reuse")
50             DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
51          DRI_CONF_DESC_END
52       DRI_CONF_OPT_END
53
54       DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
55          DRI_CONF_DESC(en, "Enable texture tiling")
56       DRI_CONF_OPT_END
57
58       DRI_CONF_OPT_BEGIN(early_z, bool, false)
59          DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
60       DRI_CONF_OPT_END
61
62       DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
63          DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
64       DRI_CONF_OPT_END
65
66    DRI_CONF_SECTION_END
67    DRI_CONF_SECTION_QUALITY
68       DRI_CONF_FORCE_S3TC_ENABLE(false)
69       DRI_CONF_ALLOW_LARGE_TEXTURES(2)
70    DRI_CONF_SECTION_END
71    DRI_CONF_SECTION_DEBUG
72      DRI_CONF_NO_RAST(false)
73      DRI_CONF_ALWAYS_FLUSH_BATCH(false)
74      DRI_CONF_ALWAYS_FLUSH_CACHE(false)
75
76       DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
77          DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
78       DRI_CONF_OPT_END
79    DRI_CONF_SECTION_END
80 DRI_CONF_END;
81
82 const GLuint __driNConfigOptions = 11;
83
84 #include "intel_batchbuffer.h"
85 #include "intel_buffers.h"
86 #include "intel_bufmgr.h"
87 #include "intel_chipset.h"
88 #include "intel_fbo.h"
89 #include "intel_screen.h"
90 #include "intel_tex.h"
91 #include "intel_regions.h"
92
93 #include "i915_drm.h"
94
95 #ifdef USE_NEW_INTERFACE
96 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
97 #endif /*USE_NEW_INTERFACE */
98
99 static const __DRItexBufferExtension intelTexBufferExtension = {
100     { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
101    intelSetTexBuffer,
102    intelSetTexBuffer2,
103 };
104
105 static void
106 intelDRI2Flush(__DRIdrawable *drawable)
107 {
108    GET_CURRENT_CONTEXT(ctx);
109    struct intel_context *intel = intel_context(ctx);
110
111    if (intel->gen < 4)
112       INTEL_FIREVERTICES(intel);
113
114    intel->need_throttle = GL_TRUE;
115
116    if (intel->batch.used)
117       intel_batchbuffer_flush(intel);
118 }
119
120 static const struct __DRI2flushExtensionRec intelFlushExtension = {
121     { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
122     intelDRI2Flush,
123     dri2InvalidateDrawable,
124 };
125
126 static __DRIimage *
127 intel_create_image_from_name(__DRIscreen *screen,
128                              int width, int height, int format,
129                              int name, int pitch, void *loaderPrivate)
130 {
131     struct intel_screen *intelScreen = screen->private;
132     __DRIimage *image;
133     int cpp;
134
135     image = CALLOC(sizeof *image);
136     if (image == NULL)
137         return NULL;
138
139     switch (format) {
140     case __DRI_IMAGE_FORMAT_RGB565:
141        image->format = MESA_FORMAT_RGB565;
142        image->internal_format = GL_RGB;
143        image->data_type = GL_UNSIGNED_BYTE;
144        break;
145     case __DRI_IMAGE_FORMAT_XRGB8888:
146        image->format = MESA_FORMAT_XRGB8888;
147        image->internal_format = GL_RGB;
148        image->data_type = GL_UNSIGNED_BYTE;
149        break;
150     case __DRI_IMAGE_FORMAT_ARGB8888:
151        image->format = MESA_FORMAT_ARGB8888;
152        image->internal_format = GL_RGBA;
153        image->data_type = GL_UNSIGNED_BYTE;
154        break;
155     default:
156        free(image);
157        return NULL;
158     }
159
160     image->data = loaderPrivate;
161     cpp = _mesa_get_format_bytes(image->format);
162
163     image->region = intel_region_alloc_for_handle(intelScreen,
164                                                   cpp, width, height,
165                                                   pitch, name, "image");
166     if (image->region == NULL) {
167        FREE(image);
168        return NULL;
169     }
170
171     return image;       
172 }
173
174 static __DRIimage *
175 intel_create_image_from_renderbuffer(__DRIcontext *context,
176                                      int renderbuffer, void *loaderPrivate)
177 {
178    __DRIimage *image;
179    struct intel_context *intel = context->driverPrivate;
180    struct gl_renderbuffer *rb;
181    struct intel_renderbuffer *irb;
182
183    rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
184    if (!rb) {
185       _mesa_error(&intel->ctx,
186                   GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
187       return NULL;
188    }
189
190    irb = intel_renderbuffer(rb);
191    image = CALLOC(sizeof *image);
192    if (image == NULL)
193       return NULL;
194
195    image->internal_format = rb->InternalFormat;
196    image->format = rb->Format;
197    image->data_type = rb->DataType;
198    image->data = loaderPrivate;
199    intel_region_reference(&image->region, irb->region);
200
201    return image;
202 }
203
204 static void
205 intel_destroy_image(__DRIimage *image)
206 {
207     intel_region_release(&image->region);
208     FREE(image);
209 }
210
211 static __DRIimage *
212 intel_create_image(__DRIscreen *screen,
213                    int width, int height, int format,
214                    unsigned int use,
215                    void *loaderPrivate)
216 {
217    __DRIimage *image;
218    struct intel_screen *intelScreen = screen->private;
219    uint32_t tiling;
220    int cpp;
221
222    tiling = I915_TILING_X;
223    if (use & __DRI_IMAGE_USE_CURSOR) {
224       if (width != 64 || height != 64)
225          return NULL;
226       tiling = I915_TILING_NONE;
227    }
228
229    image = CALLOC(sizeof *image);
230    if (image == NULL)
231       return NULL;
232
233    switch (format) {
234    case __DRI_IMAGE_FORMAT_RGB565:
235       image->format = MESA_FORMAT_RGB565;
236       image->internal_format = GL_RGB;
237       image->data_type = GL_UNSIGNED_BYTE;
238       break;
239    case __DRI_IMAGE_FORMAT_XRGB8888:
240       image->format = MESA_FORMAT_XRGB8888;
241       image->internal_format = GL_RGB;
242       image->data_type = GL_UNSIGNED_BYTE;
243       break;
244    case __DRI_IMAGE_FORMAT_ARGB8888:
245       image->format = MESA_FORMAT_ARGB8888;
246       image->internal_format = GL_RGBA;
247       image->data_type = GL_UNSIGNED_BYTE;
248       break;
249    default:
250       free(image);
251       return NULL;
252    }
253
254    image->data = loaderPrivate;
255    cpp = _mesa_get_format_bytes(image->format);
256
257    image->region =
258       intel_region_alloc(intelScreen, tiling,
259                          cpp, width, height, GL_TRUE);
260    if (image->region == NULL) {
261       FREE(image);
262       return NULL;
263    }
264    
265    return image;
266 }
267
268 static GLboolean
269 intel_query_image(__DRIimage *image, int attrib, int *value)
270 {
271    switch (attrib) {
272    case __DRI_IMAGE_ATTRIB_STRIDE:
273       *value = image->region->pitch * image->region->cpp;
274       return GL_TRUE;
275    case __DRI_IMAGE_ATTRIB_HANDLE:
276       *value = image->region->buffer->handle;
277       return GL_TRUE;
278    case __DRI_IMAGE_ATTRIB_NAME:
279       return intel_region_flink(image->region, (uint32_t *) value);
280    default:
281       return GL_FALSE;
282    }
283 }
284
285 static __DRIimage *
286 intel_dup_image(__DRIimage *orig_image, void *loaderPrivate)
287 {
288    __DRIimage *image;
289
290    image = CALLOC(sizeof *image);
291    if (image == NULL)
292       return NULL;
293
294    intel_region_reference(&image->region, orig_image->region);
295    if (image->region == NULL) {
296       FREE(image);
297       return NULL;
298    }
299
300    image->internal_format = orig_image->internal_format;
301    image->format          = orig_image->format;
302    image->data_type       = orig_image->data_type;
303    image->data            = loaderPrivate;
304    
305    return image;
306 }
307
308 static struct __DRIimageExtensionRec intelImageExtension = {
309     { __DRI_IMAGE, __DRI_IMAGE_VERSION },
310     intel_create_image_from_name,
311     intel_create_image_from_renderbuffer,
312     intel_destroy_image,
313     intel_create_image,
314     intel_query_image,
315     intel_dup_image
316 };
317
318 static const __DRIextension *intelScreenExtensions[] = {
319     &driReadDrawableExtension,
320     &intelTexBufferExtension.base,
321     &intelFlushExtension.base,
322     &intelImageExtension.base,
323     &dri2ConfigQueryExtension.base,
324     NULL
325 };
326
327 static GLboolean
328 intel_get_param(__DRIscreen *psp, int param, int *value)
329 {
330    int ret;
331    struct drm_i915_getparam gp;
332
333    gp.param = param;
334    gp.value = value;
335
336    ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
337    if (ret) {
338       if (ret != -EINVAL)
339          _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
340       return GL_FALSE;
341    }
342
343    return GL_TRUE;
344 }
345
346 static GLboolean
347 intel_get_boolean(__DRIscreen *psp, int param)
348 {
349    int value = 0;
350    return intel_get_param(psp, param, &value) && value;
351 }
352
353 static void
354 nop_callback(GLuint key, void *data, void *userData)
355 {
356 }
357
358 static void
359 intelDestroyScreen(__DRIscreen * sPriv)
360 {
361    struct intel_screen *intelScreen = sPriv->private;
362
363    dri_bufmgr_destroy(intelScreen->bufmgr);
364    driDestroyOptionInfo(&intelScreen->optionCache);
365
366    /* Some regions may still have references to them at this point, so
367     * flush the hash table to prevent _mesa_DeleteHashTable() from
368     * complaining about the hash not being empty; */
369    _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
370    _mesa_DeleteHashTable(intelScreen->named_regions);
371
372    FREE(intelScreen);
373    sPriv->private = NULL;
374 }
375
376
377 /**
378  * This is called when we need to set up GL rendering to a new X window.
379  */
380 static GLboolean
381 intelCreateBuffer(__DRIscreen * driScrnPriv,
382                   __DRIdrawable * driDrawPriv,
383                   const struct gl_config * mesaVis, GLboolean isPixmap)
384 {
385    struct intel_renderbuffer *rb;
386    struct intel_screen *screen = (struct intel_screen*) driScrnPriv->private;
387
388    if (isPixmap) {
389       return GL_FALSE;          /* not implemented */
390    }
391    else {
392       gl_format rgbFormat;
393
394       struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
395
396       if (!fb)
397          return GL_FALSE;
398
399       _mesa_initialize_window_framebuffer(fb, mesaVis);
400
401       if (mesaVis->redBits == 5)
402          rgbFormat = MESA_FORMAT_RGB565;
403       else if (mesaVis->alphaBits == 0)
404          rgbFormat = MESA_FORMAT_XRGB8888;
405       else
406          rgbFormat = MESA_FORMAT_ARGB8888;
407
408       /* setup the hardware-based renderbuffers */
409       rb = intel_create_renderbuffer(rgbFormat);
410       _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
411
412       if (mesaVis->doubleBufferMode) {
413          rb = intel_create_renderbuffer(rgbFormat);
414          _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
415       }
416
417       /*
418        * Assert here that the gl_config has an expected depth/stencil bit
419        * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
420        * which constructs the advertised configs.)
421        */
422       if (mesaVis->depthBits == 24) {
423          assert(mesaVis->stencilBits == 8);
424
425          if (screen->hw_has_separate_stencil
426              && screen->dri2_has_hiz != INTEL_DRI2_HAS_HIZ_FALSE) {
427             /*
428              * Request a separate stencil buffer even if we do not yet know if
429              * the screen supports it. (See comments for
430              * enum intel_dri2_has_hiz).
431              */
432             rb = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
433             _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base);
434             rb = intel_create_renderbuffer(MESA_FORMAT_S8);
435             _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base);
436          } else {
437             /*
438              * Use combined depth/stencil. Note that the renderbuffer is
439              * attached to two attachment points.
440              */
441             rb = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
442             _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base);
443             _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base);
444          }
445       }
446       else if (mesaVis->depthBits == 16) {
447          assert(mesaVis->stencilBits == 0);
448          /* just 16-bit depth buffer, no hw stencil */
449          struct intel_renderbuffer *depthRb
450             = intel_create_renderbuffer(MESA_FORMAT_Z16);
451          _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
452       }
453       else {
454          assert(mesaVis->depthBits == 0);
455          assert(mesaVis->stencilBits == 0);
456       }
457
458       /* now add any/all software-based renderbuffers we may need */
459       _mesa_add_soft_renderbuffers(fb,
460                                    GL_FALSE, /* never sw color */
461                                    GL_FALSE, /* never sw depth */
462                                    GL_FALSE, /* never sw stencil */
463                                    mesaVis->accumRedBits > 0,
464                                    GL_FALSE, /* never sw alpha */
465                                    GL_FALSE  /* never sw aux */ );
466       driDrawPriv->driverPrivate = fb;
467
468       return GL_TRUE;
469    }
470 }
471
472 static void
473 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
474 {
475     struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
476   
477     _mesa_reference_framebuffer(&fb, NULL);
478 }
479
480 /* There are probably better ways to do this, such as an
481  * init-designated function to register chipids and createcontext
482  * functions.
483  */
484 extern GLboolean i830CreateContext(const struct gl_config * mesaVis,
485                                    __DRIcontext * driContextPriv,
486                                    void *sharedContextPrivate);
487
488 extern GLboolean i915CreateContext(int api,
489                                    const struct gl_config * mesaVis,
490                                    __DRIcontext * driContextPriv,
491                                    void *sharedContextPrivate);
492 extern GLboolean brwCreateContext(int api,
493                                   const struct gl_config * mesaVis,
494                                   __DRIcontext * driContextPriv,
495                                   void *sharedContextPrivate);
496
497 static GLboolean
498 intelCreateContext(gl_api api,
499                    const struct gl_config * mesaVis,
500                    __DRIcontext * driContextPriv,
501                    void *sharedContextPrivate)
502 {
503    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
504    struct intel_screen *intelScreen = sPriv->private;
505
506 #ifdef I915
507    if (IS_9XX(intelScreen->deviceID)) {
508       if (!IS_965(intelScreen->deviceID)) {
509          return i915CreateContext(api, mesaVis, driContextPriv,
510                                   sharedContextPrivate);
511       }
512    } else {
513       intelScreen->no_vbo = GL_TRUE;
514       return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
515    }
516 #else
517    if (IS_965(intelScreen->deviceID))
518       return brwCreateContext(api, mesaVis,
519                               driContextPriv, sharedContextPrivate);
520 #endif
521    fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
522    return GL_FALSE;
523 }
524
525 static GLboolean
526 intel_init_bufmgr(struct intel_screen *intelScreen)
527 {
528    __DRIscreen *spriv = intelScreen->driScrnPriv;
529    int num_fences = 0;
530
531    intelScreen->no_hw = (getenv("INTEL_NO_HW") != NULL ||
532                          getenv("INTEL_DEVID_OVERRIDE") != NULL);
533
534    intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
535    if (intelScreen->bufmgr == NULL) {
536       fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
537               __func__, __LINE__);
538       return GL_FALSE;
539    }
540
541    if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
542        num_fences == 0) {
543       fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
544       return GL_FALSE;
545    }
546
547    drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
548
549    intelScreen->named_regions = _mesa_NewHashTable();
550
551    intelScreen->relaxed_relocations = 0;
552    intelScreen->relaxed_relocations |=
553       intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA) << 0;
554
555    return GL_TRUE;
556 }
557
558 /**
559  * Override intel_screen.hw_has_hiz with environment variable INTEL_HIZ.
560  *
561  * Valid values for INTEL_HIZ are "0" and "1". If an invalid valid value is
562  * encountered, a warning is emitted and INTEL_HIZ is ignored.
563  */
564 static void
565 intel_override_hiz(struct intel_screen *intel)
566 {
567    const char *s = getenv("INTEL_HIZ");
568    if (!s) {
569       return;
570    } else if (!strncmp("0", s, 2)) {
571       intel->hw_has_hiz = false;
572    } else if (!strncmp("1", s, 2)) {
573       intel->hw_has_hiz = true;
574    } else {
575       fprintf(stderr,
576               "warning: env variable INTEL_HIZ=\"%s\" has invalid value "
577               "and is ignored", s);
578    }
579 }
580
581 /**
582  * Override intel_screen.hw_has_separate_stencil with environment variable
583  * INTEL_SEPARATE_STENCIL.
584  *
585  * Valid values for INTEL_SEPARATE_STENCIL are "0" and "1". If an invalid
586  * valid value is encountered, a warning is emitted and INTEL_SEPARATE_STENCIL
587  * is ignored.
588  */
589 static void
590 intel_override_separate_stencil(struct intel_screen *screen)
591 {
592    const char *s = getenv("INTEL_SEPARATE_STENCIL");
593    if (!s) {
594       return;
595    } else if (!strncmp("0", s, 2)) {
596       screen->hw_has_separate_stencil = false;
597    } else if (!strncmp("1", s, 2)) {
598       screen->hw_has_separate_stencil = true;
599    } else {
600       fprintf(stderr,
601               "warning: env variable INTEL_SEPARATE_STENCIL=\"%s\" has "
602               "invalid value and is ignored", s);
603    }
604 }
605
606 /**
607  * This is the driver specific part of the createNewScreen entry point.
608  * Called when using DRI2.
609  *
610  * \return the struct gl_config supported by this driver
611  */
612 static const
613 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
614 {
615    struct intel_screen *intelScreen;
616    GLenum fb_format[3];
617    GLenum fb_type[3];
618    unsigned int api_mask;
619    char *devid_override;
620
621    static const GLenum back_buffer_modes[] = {
622        GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
623    };
624    uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
625    int color;
626    __DRIconfig **configs = NULL;
627
628    /* Allocate the private area */
629    intelScreen = CALLOC(sizeof *intelScreen);
630    if (!intelScreen) {
631       fprintf(stderr, "\nERROR!  Allocating private area failed\n");
632       return GL_FALSE;
633    }
634    /* parse information in __driConfigOptions */
635    driParseOptionInfo(&intelScreen->optionCache,
636                       __driConfigOptions, __driNConfigOptions);
637
638    intelScreen->driScrnPriv = psp;
639    psp->private = (void *) intelScreen;
640
641    /* Determine chipset ID */
642    if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
643                         &intelScreen->deviceID))
644       return GL_FALSE;
645
646    /* Allow an override of the device ID for the purpose of making the
647     * driver produce dumps for debugging of new chipset enablement.
648     * This implies INTEL_NO_HW, to avoid programming your actual GPU
649     * incorrectly.
650     */
651    devid_override = getenv("INTEL_DEVID_OVERRIDE");
652    if (devid_override) {
653       intelScreen->deviceID = strtod(devid_override, NULL);
654    }
655
656    if (IS_GEN7(intelScreen->deviceID)) {
657       intelScreen->gen = 7;
658    } else if (IS_GEN6(intelScreen->deviceID)) {
659       intelScreen->gen = 6;
660    } else if (IS_GEN5(intelScreen->deviceID)) {
661       intelScreen->gen = 5;
662    } else if (IS_965(intelScreen->deviceID)) {
663       intelScreen->gen = 4;
664    } else if (IS_9XX(intelScreen->deviceID)) {
665       intelScreen->gen = 3;
666    } else {
667       intelScreen->gen = 2;
668    }
669
670    /*
671     * FIXME: The hiz and separate stencil fields need updating once the
672     * FIXME: features are completely implemented for a given chipset.
673     */
674    intelScreen->hw_has_separate_stencil = intelScreen->gen >= 7;
675    intelScreen->hw_must_use_separate_stencil = intelScreen->gen >= 7;
676    intelScreen->hw_has_hiz = false;
677    intelScreen->dri2_has_hiz = INTEL_DRI2_HAS_HIZ_UNKNOWN;
678
679    intel_override_hiz(intelScreen);
680    intel_override_separate_stencil(intelScreen);
681
682    api_mask = (1 << __DRI_API_OPENGL);
683 #if FEATURE_ES1
684    api_mask |= (1 << __DRI_API_GLES);
685 #endif
686 #if FEATURE_ES2
687    api_mask |= (1 << __DRI_API_GLES2);
688 #endif
689
690    if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
691       psp->api_mask = api_mask;
692
693    if (!intel_init_bufmgr(intelScreen))
694        return GL_FALSE;
695
696    psp->extensions = intelScreenExtensions;
697
698    msaa_samples_array[0] = 0;
699
700    fb_format[0] = GL_RGB;
701    fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
702
703    fb_format[1] = GL_BGR;
704    fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
705
706    fb_format[2] = GL_BGRA;
707    fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
708
709    depth_bits[0] = 0;
710    stencil_bits[0] = 0;
711
712    /* Generate a rich set of useful configs that do not include an
713     * accumulation buffer.
714     */
715    for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
716       __DRIconfig **new_configs;
717       int depth_factor;
718
719       /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
720        * buffer that has a diffferent number of bits per pixel than the color
721        * buffer.  This isn't yet supported here.
722        */
723       if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
724          depth_bits[1] = 16;
725          stencil_bits[1] = 0;
726       } else {
727          depth_bits[1] = 24;
728          stencil_bits[1] = 8;
729       }
730
731       depth_factor = 2;
732
733       new_configs = driCreateConfigs(fb_format[color], fb_type[color],
734                                      depth_bits,
735                                      stencil_bits,
736                                      depth_factor,
737                                      back_buffer_modes,
738                                      ARRAY_SIZE(back_buffer_modes),
739                                      msaa_samples_array,
740                                      ARRAY_SIZE(msaa_samples_array),
741                                      GL_FALSE);
742       if (configs == NULL)
743          configs = new_configs;
744       else
745          configs = driConcatConfigs(configs, new_configs);
746    }
747
748    /* Generate the minimum possible set of configs that include an
749     * accumulation buffer.
750     */
751    for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
752       __DRIconfig **new_configs;
753
754       if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
755          depth_bits[0] = 16;
756          stencil_bits[0] = 0;
757       } else {
758          depth_bits[0] = 24;
759          stencil_bits[0] = 8;
760       }
761
762       new_configs = driCreateConfigs(fb_format[color], fb_type[color],
763                                      depth_bits, stencil_bits, 1,
764                                      back_buffer_modes + 1, 1,
765                                      msaa_samples_array, 1,
766                                      GL_TRUE);
767       if (configs == NULL)
768          configs = new_configs;
769       else
770          configs = driConcatConfigs(configs, new_configs);
771    }
772
773    if (configs == NULL) {
774       fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
775               __LINE__);
776       return NULL;
777    }
778
779    return (const __DRIconfig **)configs;
780 }
781
782 struct intel_buffer {
783    __DRIbuffer base;
784    struct intel_region *region;
785 };
786
787 static __DRIbuffer *
788 intelAllocateBuffer(__DRIscreen *screen,
789                     unsigned attachment, unsigned format,
790                     int width, int height)
791 {
792    struct intel_buffer *intelBuffer;
793    struct intel_screen *intelScreen = screen->private;
794    uint32_t tiling;
795
796    intelBuffer = CALLOC(sizeof *intelBuffer);
797    if (intelBuffer == NULL)
798       return NULL;
799
800    if ((attachment == __DRI_BUFFER_DEPTH ||
801         attachment == __DRI_BUFFER_STENCIL ||
802         attachment == __DRI_BUFFER_DEPTH_STENCIL) &&
803        intelScreen->gen >= 4)
804       tiling = I915_TILING_Y;
805    else
806       tiling = I915_TILING_X;
807
808    intelBuffer->region = intel_region_alloc(intelScreen, tiling,
809                                             format / 8, width, height, GL_TRUE);
810    
811    if (intelBuffer->region == NULL) {
812            FREE(intelBuffer);
813            return NULL;
814    }
815    
816    intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
817
818    intelBuffer->base.attachment = attachment;
819    intelBuffer->base.cpp = intelBuffer->region->cpp;
820    intelBuffer->base.pitch =
821          intelBuffer->region->pitch * intelBuffer->region->cpp;
822
823    return &intelBuffer->base;
824 }
825
826 static void
827 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
828 {
829    struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
830
831    intel_region_release(&intelBuffer->region);
832    free(intelBuffer);
833 }
834
835
836 const struct __DriverAPIRec driDriverAPI = {
837    .DestroyScreen        = intelDestroyScreen,
838    .CreateContext        = intelCreateContext,
839    .DestroyContext       = intelDestroyContext,
840    .CreateBuffer         = intelCreateBuffer,
841    .DestroyBuffer        = intelDestroyBuffer,
842    .MakeCurrent          = intelMakeCurrent,
843    .UnbindContext        = intelUnbindContext,
844    .InitScreen2          = intelInitScreen2,
845    .AllocateBuffer       = intelAllocateBuffer,
846    .ReleaseBuffer        = intelReleaseBuffer
847 };
848
849 /* This is the table of extensions that the loader will dlsym() for. */
850 PUBLIC const __DRIextension *__driDriverExtensions[] = {
851     &driCoreExtension.base,
852     &driDRI2Extension.base,
853     NULL
854 };