OSDN Git Service

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