OSDN Git Service

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