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