Tizen 2.0 Release
[profile/ivi/osmesa.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    image->region = NULL;
295    intel_region_reference(&image->region, orig_image->region);
296    if (image->region == NULL) {
297       FREE(image);
298       return NULL;
299    }
300
301    image->internal_format = orig_image->internal_format;
302    image->format          = orig_image->format;
303    image->data_type       = orig_image->data_type;
304    image->data            = loaderPrivate;
305    
306    return image;
307 }
308
309 static struct __DRIimageExtensionRec intelImageExtension = {
310     { __DRI_IMAGE, __DRI_IMAGE_VERSION },
311     intel_create_image_from_name,
312     intel_create_image_from_renderbuffer,
313     intel_destroy_image,
314     intel_create_image,
315     intel_query_image,
316     intel_dup_image
317 };
318
319 static const __DRIextension *intelScreenExtensions[] = {
320     &driReadDrawableExtension,
321     &intelTexBufferExtension.base,
322     &intelFlushExtension.base,
323     &intelImageExtension.base,
324     &dri2ConfigQueryExtension.base,
325     NULL
326 };
327
328 static GLboolean
329 intel_get_param(__DRIscreen *psp, int param, int *value)
330 {
331    int ret;
332    struct drm_i915_getparam gp;
333
334    gp.param = param;
335    gp.value = value;
336
337    ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
338    if (ret) {
339       if (ret != -EINVAL)
340          _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
341       return GL_FALSE;
342    }
343
344    return GL_TRUE;
345 }
346
347 static GLboolean
348 intel_get_boolean(__DRIscreen *psp, int param)
349 {
350    int value = 0;
351    return intel_get_param(psp, param, &value) && value;
352 }
353
354 static void
355 nop_callback(GLuint key, void *data, void *userData)
356 {
357 }
358
359 static void
360 intelDestroyScreen(__DRIscreen * sPriv)
361 {
362    struct intel_screen *intelScreen = sPriv->private;
363
364    dri_bufmgr_destroy(intelScreen->bufmgr);
365    driDestroyOptionInfo(&intelScreen->optionCache);
366
367    /* Some regions may still have references to them at this point, so
368     * flush the hash table to prevent _mesa_DeleteHashTable() from
369     * complaining about the hash not being empty; */
370    _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
371    _mesa_DeleteHashTable(intelScreen->named_regions);
372
373    FREE(intelScreen);
374    sPriv->private = NULL;
375 }
376
377
378 /**
379  * This is called when we need to set up GL rendering to a new X window.
380  */
381 static GLboolean
382 intelCreateBuffer(__DRIscreen * driScrnPriv,
383                   __DRIdrawable * driDrawPriv,
384                   const struct gl_config * mesaVis, GLboolean isPixmap)
385 {
386    struct intel_renderbuffer *rb;
387    struct intel_screen *screen = (struct intel_screen*) driScrnPriv->private;
388
389    if (isPixmap) {
390       return GL_FALSE;          /* not implemented */
391    }
392    else {
393       gl_format rgbFormat;
394
395       struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
396
397       if (!fb)
398          return GL_FALSE;
399
400       _mesa_initialize_window_framebuffer(fb, mesaVis);
401
402       if (mesaVis->redBits == 5)
403          rgbFormat = MESA_FORMAT_RGB565;
404       else if (mesaVis->alphaBits == 0)
405          rgbFormat = MESA_FORMAT_XRGB8888;
406       else
407          rgbFormat = MESA_FORMAT_ARGB8888;
408
409       /* setup the hardware-based renderbuffers */
410       rb = intel_create_renderbuffer(rgbFormat);
411       _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
412
413       if (mesaVis->doubleBufferMode) {
414          rb = intel_create_renderbuffer(rgbFormat);
415          _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
416       }
417
418       /*
419        * Assert here that the gl_config has an expected depth/stencil bit
420        * combination: one of d24/s8, d16/s0, d0/s0. (See intelInitScreen2(),
421        * which constructs the advertised configs.)
422        */
423       if (mesaVis->depthBits == 24) {
424          assert(mesaVis->stencilBits == 8);
425
426          if (screen->hw_has_separate_stencil
427              && screen->dri2_has_hiz != INTEL_DRI2_HAS_HIZ_FALSE) {
428             /*
429              * Request a separate stencil buffer even if we do not yet know if
430              * the screen supports it. (See comments for
431              * enum intel_dri2_has_hiz).
432              */
433             rb = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
434             _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base);
435             rb = intel_create_renderbuffer(MESA_FORMAT_S8);
436             _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base);
437          } else {
438             /*
439              * Use combined depth/stencil. Note that the renderbuffer is
440              * attached to two attachment points.
441              */
442             rb = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
443             _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &rb->Base);
444             _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &rb->Base);
445          }
446       }
447       else if (mesaVis->depthBits == 16) {
448          assert(mesaVis->stencilBits == 0);
449          /* just 16-bit depth buffer, no hw stencil */
450          struct intel_renderbuffer *depthRb
451             = intel_create_renderbuffer(MESA_FORMAT_Z16);
452          _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
453       }
454       else {
455          assert(mesaVis->depthBits == 0);
456          assert(mesaVis->stencilBits == 0);
457       }
458
459       /* now add any/all software-based renderbuffers we may need */
460       _mesa_add_soft_renderbuffers(fb,
461                                    GL_FALSE, /* never sw color */
462                                    GL_FALSE, /* never sw depth */
463                                    GL_FALSE, /* never sw stencil */
464                                    mesaVis->accumRedBits > 0,
465                                    GL_FALSE, /* never sw alpha */
466                                    GL_FALSE  /* never sw aux */ );
467       driDrawPriv->driverPrivate = fb;
468
469       return GL_TRUE;
470    }
471 }
472
473 static void
474 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
475 {
476     struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
477   
478     _mesa_reference_framebuffer(&fb, NULL);
479 }
480
481 /* There are probably better ways to do this, such as an
482  * init-designated function to register chipids and createcontext
483  * functions.
484  */
485 extern GLboolean i830CreateContext(const struct gl_config * mesaVis,
486                                    __DRIcontext * driContextPriv,
487                                    void *sharedContextPrivate);
488
489 extern GLboolean i915CreateContext(int api,
490                                    const struct gl_config * mesaVis,
491                                    __DRIcontext * driContextPriv,
492                                    void *sharedContextPrivate);
493 extern GLboolean brwCreateContext(int api,
494                                   const struct gl_config * mesaVis,
495                                   __DRIcontext * driContextPriv,
496                                   void *sharedContextPrivate);
497
498 static GLboolean
499 intelCreateContext(gl_api api,
500                    const struct gl_config * mesaVis,
501                    __DRIcontext * driContextPriv,
502                    void *sharedContextPrivate)
503 {
504    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
505    struct intel_screen *intelScreen = sPriv->private;
506
507 #ifdef I915
508    if (IS_9XX(intelScreen->deviceID)) {
509       if (!IS_965(intelScreen->deviceID)) {
510          return i915CreateContext(api, mesaVis, driContextPriv,
511                                   sharedContextPrivate);
512       }
513    } else {
514       intelScreen->no_vbo = GL_TRUE;
515       return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
516    }
517 #else
518    if (IS_965(intelScreen->deviceID))
519       return brwCreateContext(api, mesaVis,
520                               driContextPriv, sharedContextPrivate);
521 #endif
522    fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
523    return GL_FALSE;
524 }
525
526 static GLboolean
527 intel_init_bufmgr(struct intel_screen *intelScreen)
528 {
529    __DRIscreen *spriv = intelScreen->driScrnPriv;
530    int num_fences = 0;
531
532    intelScreen->no_hw = (getenv("INTEL_NO_HW") != NULL ||
533                          getenv("INTEL_DEVID_OVERRIDE") != NULL);
534
535    intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
536    if (intelScreen->bufmgr == NULL) {
537       fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
538               __func__, __LINE__);
539       return GL_FALSE;
540    }
541
542    if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
543        num_fences == 0) {
544       fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
545       return GL_FALSE;
546    }
547
548    drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
549
550    intelScreen->named_regions = _mesa_NewHashTable();
551
552    intelScreen->relaxed_relocations = 0;
553    intelScreen->relaxed_relocations |=
554       intel_get_boolean(spriv, I915_PARAM_HAS_RELAXED_DELTA) << 0;
555
556    return GL_TRUE;
557 }
558
559 /**
560  * Override intel_screen.hw_has_hiz with environment variable INTEL_HIZ.
561  *
562  * Valid values for INTEL_HIZ are "0" and "1". If an invalid valid value is
563  * encountered, a warning is emitted and INTEL_HIZ is ignored.
564  */
565 static void
566 intel_override_hiz(struct intel_screen *intel)
567 {
568    const char *s = getenv("INTEL_HIZ");
569    if (!s) {
570       return;
571    } else if (!strncmp("0", s, 2)) {
572       intel->hw_has_hiz = false;
573    } else if (!strncmp("1", s, 2)) {
574       intel->hw_has_hiz = true;
575    } else {
576       fprintf(stderr,
577               "warning: env variable INTEL_HIZ=\"%s\" has invalid value "
578               "and is ignored", s);
579    }
580 }
581
582 /**
583  * Override intel_screen.hw_has_separate_stencil with environment variable
584  * INTEL_SEPARATE_STENCIL.
585  *
586  * Valid values for INTEL_SEPARATE_STENCIL are "0" and "1". If an invalid
587  * valid value is encountered, a warning is emitted and INTEL_SEPARATE_STENCIL
588  * is ignored.
589  */
590 static void
591 intel_override_separate_stencil(struct intel_screen *screen)
592 {
593    const char *s = getenv("INTEL_SEPARATE_STENCIL");
594    if (!s) {
595       return;
596    } else if (!strncmp("0", s, 2)) {
597       screen->hw_has_separate_stencil = false;
598    } else if (!strncmp("1", s, 2)) {
599       screen->hw_has_separate_stencil = true;
600    } else {
601       fprintf(stderr,
602               "warning: env variable INTEL_SEPARATE_STENCIL=\"%s\" has "
603               "invalid value and is ignored", s);
604    }
605 }
606
607 /**
608  * This is the driver specific part of the createNewScreen entry point.
609  * Called when using DRI2.
610  *
611  * \return the struct gl_config supported by this driver
612  */
613 static const
614 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
615 {
616    struct intel_screen *intelScreen;
617    GLenum fb_format[3];
618    GLenum fb_type[3];
619    unsigned int api_mask;
620    char *devid_override;
621
622    static const GLenum back_buffer_modes[] = {
623        GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
624    };
625    uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
626    int color;
627    __DRIconfig **configs = NULL;
628
629    /* Allocate the private area */
630    intelScreen = CALLOC(sizeof *intelScreen);
631    if (!intelScreen) {
632       fprintf(stderr, "\nERROR!  Allocating private area failed\n");
633       return GL_FALSE;
634    }
635    /* parse information in __driConfigOptions */
636    driParseOptionInfo(&intelScreen->optionCache,
637                       __driConfigOptions, __driNConfigOptions);
638
639    intelScreen->driScrnPriv = psp;
640    psp->private = (void *) intelScreen;
641
642    /* Determine chipset ID */
643    if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
644                         &intelScreen->deviceID))
645       return GL_FALSE;
646
647    /* Allow an override of the device ID for the purpose of making the
648     * driver produce dumps for debugging of new chipset enablement.
649     * This implies INTEL_NO_HW, to avoid programming your actual GPU
650     * incorrectly.
651     */
652    devid_override = getenv("INTEL_DEVID_OVERRIDE");
653    if (devid_override) {
654       intelScreen->deviceID = strtod(devid_override, NULL);
655    }
656
657    if (IS_GEN7(intelScreen->deviceID)) {
658       intelScreen->gen = 7;
659    } else if (IS_GEN6(intelScreen->deviceID)) {
660       intelScreen->gen = 6;
661    } else if (IS_GEN5(intelScreen->deviceID)) {
662       intelScreen->gen = 5;
663    } else if (IS_965(intelScreen->deviceID)) {
664       intelScreen->gen = 4;
665    } else if (IS_9XX(intelScreen->deviceID)) {
666       intelScreen->gen = 3;
667    } else {
668       intelScreen->gen = 2;
669    }
670
671    /*
672     * FIXME: The hiz and separate stencil fields need updating once the
673     * FIXME: features are completely implemented for a given chipset.
674     */
675    intelScreen->hw_has_separate_stencil = intelScreen->gen >= 7;
676    intelScreen->hw_must_use_separate_stencil = intelScreen->gen >= 7;
677    intelScreen->hw_has_hiz = false;
678    intelScreen->dri2_has_hiz = INTEL_DRI2_HAS_HIZ_UNKNOWN;
679
680    intel_override_hiz(intelScreen);
681    intel_override_separate_stencil(intelScreen);
682
683    api_mask = (1 << __DRI_API_OPENGL);
684 #if FEATURE_ES1
685    api_mask |= (1 << __DRI_API_GLES);
686 #endif
687 #if FEATURE_ES2
688    api_mask |= (1 << __DRI_API_GLES2);
689 #endif
690
691    if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
692       psp->api_mask = api_mask;
693
694    if (!intel_init_bufmgr(intelScreen))
695        return GL_FALSE;
696
697    psp->extensions = intelScreenExtensions;
698
699    msaa_samples_array[0] = 0;
700
701    fb_format[0] = GL_RGB;
702    fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
703
704    fb_format[1] = GL_BGR;
705    fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
706
707    fb_format[2] = GL_BGRA;
708    fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
709
710    depth_bits[0] = 0;
711    stencil_bits[0] = 0;
712
713    /* Generate a rich set of useful configs that do not include an
714     * accumulation buffer.
715     */
716    for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
717       __DRIconfig **new_configs;
718       int depth_factor;
719
720       /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
721        * buffer that has a diffferent number of bits per pixel than the color
722        * buffer.  This isn't yet supported here.
723        */
724       if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
725          depth_bits[1] = 16;
726          stencil_bits[1] = 0;
727       } else {
728          depth_bits[1] = 24;
729          stencil_bits[1] = 8;
730       }
731
732       depth_factor = 2;
733
734       new_configs = driCreateConfigs(fb_format[color], fb_type[color],
735                                      depth_bits,
736                                      stencil_bits,
737                                      depth_factor,
738                                      back_buffer_modes,
739                                      ARRAY_SIZE(back_buffer_modes),
740                                      msaa_samples_array,
741                                      ARRAY_SIZE(msaa_samples_array),
742                                      GL_FALSE);
743       if (configs == NULL)
744          configs = new_configs;
745       else
746          configs = driConcatConfigs(configs, new_configs);
747    }
748
749    /* Generate the minimum possible set of configs that include an
750     * accumulation buffer.
751     */
752    for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
753       __DRIconfig **new_configs;
754
755       if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
756          depth_bits[0] = 16;
757          stencil_bits[0] = 0;
758       } else {
759          depth_bits[0] = 24;
760          stencil_bits[0] = 8;
761       }
762
763       new_configs = driCreateConfigs(fb_format[color], fb_type[color],
764                                      depth_bits, stencil_bits, 1,
765                                      back_buffer_modes + 1, 1,
766                                      msaa_samples_array, 1,
767                                      GL_TRUE);
768       if (configs == NULL)
769          configs = new_configs;
770       else
771          configs = driConcatConfigs(configs, new_configs);
772    }
773
774    if (configs == NULL) {
775       fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
776               __LINE__);
777       return NULL;
778    }
779
780    return (const __DRIconfig **)configs;
781 }
782
783 struct intel_buffer {
784    __DRIbuffer base;
785    struct intel_region *region;
786 };
787
788 /**
789  * \brief Get tiling format for a DRI buffer.
790  *
791  * \param attachment is the buffer's attachmet point, such as
792  *        __DRI_BUFFER_DEPTH.
793  * \param out_tiling is the returned tiling format for buffer.
794  * \return false if attachment is unrecognized or is incompatible with screen.
795  */
796 static bool
797 intel_get_dri_buffer_tiling(struct intel_screen *screen,
798                             uint32_t attachment,
799                             uint32_t *out_tiling)
800 {
801    if (screen->gen < 4) {
802       *out_tiling = I915_TILING_X;
803       return true;
804    }
805
806    switch (attachment) {
807    case __DRI_BUFFER_DEPTH:
808    case __DRI_BUFFER_DEPTH_STENCIL:
809    case __DRI_BUFFER_HIZ:
810       *out_tiling = I915_TILING_Y;
811       return true;
812    case __DRI_BUFFER_ACCUM:
813    case __DRI_BUFFER_FRONT_LEFT:
814    case __DRI_BUFFER_FRONT_RIGHT:
815    case __DRI_BUFFER_BACK_LEFT:
816    case __DRI_BUFFER_BACK_RIGHT:
817    case __DRI_BUFFER_FAKE_FRONT_LEFT:
818    case __DRI_BUFFER_FAKE_FRONT_RIGHT:
819       *out_tiling = I915_TILING_X;
820       return true;
821    case __DRI_BUFFER_STENCIL:
822       /* The stencil buffer is W tiled. However, we request from the kernel
823        * a non-tiled buffer because the GTT is incapable of W fencing.
824        */
825       *out_tiling = I915_TILING_NONE;
826       return true;
827    default:
828       if(unlikely(INTEL_DEBUG & DEBUG_DRI)) {
829          fprintf(stderr, "error: %s: unrecognized DRI buffer attachment 0x%x\n",
830                  __FUNCTION__, attachment);
831       }
832        return false;
833    }
834 }
835
836 static __DRIbuffer *
837 intelAllocateBuffer(__DRIscreen *screen,
838                     unsigned attachment, unsigned format,
839                     int width, int height)
840 {
841    struct intel_buffer *intelBuffer;
842    struct intel_screen *intelScreen = screen->private;
843
844    uint32_t tiling;
845    uint32_t region_width;
846    uint32_t region_height;
847    uint32_t region_cpp;
848
849    bool ok = true;
850
851    ok = intel_get_dri_buffer_tiling(intelScreen, attachment, &tiling);
852    if (!ok)
853       return NULL;
854
855    intelBuffer = CALLOC(sizeof *intelBuffer);
856    if (intelBuffer == NULL)
857       return NULL;
858
859    if (attachment == __DRI_BUFFER_STENCIL) {
860       /* The stencil buffer has quirky pitch requirements.  From Vol 2a,
861        * 11.5.6.2.1 3DSTATE_STENCIL_BUFFER, field "Surface Pitch":
862        *    The pitch must be set to 2x the value computed based on width, as
863        *    the stencil buffer is stored with two rows interleaved.
864        * To accomplish this, we resort to the nasty hack of doubling the
865        * region's cpp and halving its height.
866        */
867       region_width = ALIGN(width, 64);
868       region_height = ALIGN(ALIGN(height, 2) / 2, 64);
869       region_cpp = format / 4;
870    } else {
871       region_width = width;
872       region_height = height;
873       region_cpp = format / 8;
874    }
875
876    intelBuffer->region = intel_region_alloc(intelScreen,
877                                             tiling,
878                                             region_cpp,
879                                             region_width,
880                                             region_height,
881                                             true);
882    
883    if (intelBuffer->region == NULL) {
884            FREE(intelBuffer);
885            return NULL;
886    }
887    
888    intel_region_flink(intelBuffer->region, &intelBuffer->base.name);
889
890    intelBuffer->base.attachment = attachment;
891    intelBuffer->base.cpp = intelBuffer->region->cpp;
892    intelBuffer->base.pitch =
893          intelBuffer->region->pitch * intelBuffer->region->cpp;
894
895    return &intelBuffer->base;
896 }
897
898 static void
899 intelReleaseBuffer(__DRIscreen *screen, __DRIbuffer *buffer)
900 {
901    struct intel_buffer *intelBuffer = (struct intel_buffer *) buffer;
902
903    intel_region_release(&intelBuffer->region);
904    free(intelBuffer);
905 }
906
907
908 const struct __DriverAPIRec driDriverAPI = {
909    .DestroyScreen        = intelDestroyScreen,
910    .CreateContext        = intelCreateContext,
911    .DestroyContext       = intelDestroyContext,
912    .CreateBuffer         = intelCreateBuffer,
913    .DestroyBuffer        = intelDestroyBuffer,
914    .MakeCurrent          = intelMakeCurrent,
915    .UnbindContext        = intelUnbindContext,
916    .InitScreen2          = intelInitScreen2,
917    .AllocateBuffer       = intelAllocateBuffer,
918    .ReleaseBuffer        = intelReleaseBuffer
919 };
920
921 /* This is the table of extensions that the loader will dlsym() for. */
922 PUBLIC const __DRIextension *__driDriverExtensions[] = {
923     &driCoreExtension.base,
924     &driDRI2Extension.base,
925     NULL
926 };