OSDN Git Service

Merge remote branch 'vdpau/pipe-video' into pipe-video
[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 "main/glheader.h"
29 #include "main/context.h"
30 #include "main/framebuffer.h"
31 #include "main/renderbuffer.h"
32 #include "main/hash.h"
33 #include "main/fbobject.h"
34
35 #include "utils.h"
36 #include "xmlpool.h"
37
38 PUBLIC const char __driConfigOptions[] =
39    DRI_CONF_BEGIN
40    DRI_CONF_SECTION_PERFORMANCE
41       DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
42       /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
43        * DRI_CONF_BO_REUSE_ALL
44        */
45       DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
46          DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
47             DRI_CONF_ENUM(0, "Disable buffer object reuse")
48             DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
49          DRI_CONF_DESC_END
50       DRI_CONF_OPT_END
51
52       DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
53          DRI_CONF_DESC(en, "Enable texture tiling")
54       DRI_CONF_OPT_END
55
56       DRI_CONF_OPT_BEGIN(early_z, bool, false)
57          DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
58       DRI_CONF_OPT_END
59
60       DRI_CONF_OPT_BEGIN(fragment_shader, bool, true)
61          DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
62       DRI_CONF_OPT_END
63
64    DRI_CONF_SECTION_END
65    DRI_CONF_SECTION_QUALITY
66       DRI_CONF_FORCE_S3TC_ENABLE(false)
67       DRI_CONF_ALLOW_LARGE_TEXTURES(2)
68    DRI_CONF_SECTION_END
69    DRI_CONF_SECTION_DEBUG
70      DRI_CONF_NO_RAST(false)
71      DRI_CONF_ALWAYS_FLUSH_BATCH(false)
72      DRI_CONF_ALWAYS_FLUSH_CACHE(false)
73
74       DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
75          DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
76       DRI_CONF_OPT_END
77    DRI_CONF_SECTION_END
78 DRI_CONF_END;
79
80 const GLuint __driNConfigOptions = 11;
81
82 #include "intel_batchbuffer.h"
83 #include "intel_buffers.h"
84 #include "intel_bufmgr.h"
85 #include "intel_chipset.h"
86 #include "intel_fbo.h"
87 #include "intel_screen.h"
88 #include "intel_tex.h"
89 #include "intel_regions.h"
90
91 #include "i915_drm.h"
92
93 #ifdef USE_NEW_INTERFACE
94 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
95 #endif /*USE_NEW_INTERFACE */
96
97 static const __DRItexBufferExtension intelTexBufferExtension = {
98     { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
99    intelSetTexBuffer,
100    intelSetTexBuffer2,
101 };
102
103 static void
104 intelDRI2Flush(__DRIdrawable *drawable)
105 {
106    struct intel_context *intel = drawable->driContextPriv->driverPrivate;
107
108    if (intel->gen < 4)
109       INTEL_FIREVERTICES(intel);
110
111    intel->need_throttle = GL_TRUE;
112
113    if (intel->batch->map != intel->batch->ptr)
114       intel_batchbuffer_flush(intel->batch);
115 }
116
117 static const struct __DRI2flushExtensionRec intelFlushExtension = {
118     { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
119     intelDRI2Flush,
120     dri2InvalidateDrawable,
121 };
122
123 static __DRIimage *
124 intel_create_image_from_name(__DRIscreen *screen,
125                              int width, int height, int format,
126                              int name, int pitch, void *loaderPrivate)
127 {
128     struct intel_screen *intelScreen = screen->private;
129     __DRIimage *image;
130     int cpp;
131
132     image = CALLOC(sizeof *image);
133     if (image == NULL)
134         return NULL;
135
136     switch (format) {
137     case __DRI_IMAGE_FORMAT_RGB565:
138        image->format = MESA_FORMAT_RGB565;
139        image->internal_format = GL_RGB;
140        image->data_type = GL_UNSIGNED_BYTE;
141        break;
142     case __DRI_IMAGE_FORMAT_XRGB8888:
143        image->format = MESA_FORMAT_XRGB8888;
144        image->internal_format = GL_RGB;
145        image->data_type = GL_UNSIGNED_BYTE;
146        break;
147     case __DRI_IMAGE_FORMAT_ARGB8888:
148        image->format = MESA_FORMAT_ARGB8888;
149        image->internal_format = GL_RGBA;
150        image->data_type = GL_UNSIGNED_BYTE;
151        break;
152     default:
153        free(image);
154        return NULL;
155     }
156
157     image->data = loaderPrivate;
158     cpp = _mesa_get_format_bytes(image->format);
159
160     image->region = intel_region_alloc_for_handle(intelScreen,
161                                                   cpp, width, height,
162                                                   pitch, name, "image");
163     if (image->region == NULL) {
164        FREE(image);
165        return NULL;
166     }
167
168     return image;       
169 }
170
171 static __DRIimage *
172 intel_create_image_from_renderbuffer(__DRIcontext *context,
173                                      int renderbuffer, void *loaderPrivate)
174 {
175    __DRIimage *image;
176    struct intel_context *intel = context->driverPrivate;
177    struct gl_renderbuffer *rb;
178    struct intel_renderbuffer *irb;
179
180    rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
181    if (!rb) {
182       _mesa_error(&intel->ctx,
183                   GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
184       return NULL;
185    }
186
187    irb = intel_renderbuffer(rb);
188    image = CALLOC(sizeof *image);
189    if (image == NULL)
190       return NULL;
191
192    image->internal_format = rb->InternalFormat;
193    image->format = rb->Format;
194    image->data_type = rb->DataType;
195    image->data = loaderPrivate;
196    intel_region_reference(&image->region, irb->region);
197
198    return image;
199 }
200
201 static void
202 intel_destroy_image(__DRIimage *image)
203 {
204     intel_region_release(&image->region);
205     FREE(image);
206 }
207
208 static __DRIimage *
209 intel_create_image(__DRIscreen *screen,
210                    int width, int height, int format,
211                    unsigned int use,
212                    void *loaderPrivate)
213 {
214    __DRIimage *image;
215    struct intel_screen *intelScreen = screen->private;
216    int cpp;
217
218    image = CALLOC(sizeof *image);
219    if (image == NULL)
220       return NULL;
221
222    switch (format) {
223    case __DRI_IMAGE_FORMAT_RGB565:
224       image->format = MESA_FORMAT_RGB565;
225       image->internal_format = GL_RGB;
226       image->data_type = GL_UNSIGNED_BYTE;
227       break;
228    case __DRI_IMAGE_FORMAT_XRGB8888:
229       image->format = MESA_FORMAT_XRGB8888;
230       image->internal_format = GL_RGB;
231       image->data_type = GL_UNSIGNED_BYTE;
232       break;
233    case __DRI_IMAGE_FORMAT_ARGB8888:
234       image->format = MESA_FORMAT_ARGB8888;
235       image->internal_format = GL_RGBA;
236       image->data_type = GL_UNSIGNED_BYTE;
237       break;
238    default:
239       free(image);
240       return NULL;
241    }
242
243    image->data = loaderPrivate;
244    cpp = _mesa_get_format_bytes(image->format);
245
246    image->region =
247       intel_region_alloc(intelScreen, I915_TILING_NONE,
248                          cpp, width, height, GL_TRUE);
249    if (image->region == NULL) {
250       FREE(image);
251       return NULL;
252    }
253    
254    return image;
255 }
256
257 static GLboolean
258 intel_query_image(__DRIimage *image, int attrib, int *value)
259 {
260    switch (attrib) {
261    case __DRI_IMAGE_ATTRIB_STRIDE:
262       *value = image->region->pitch * image->region->cpp;
263       return GL_TRUE;
264    case __DRI_IMAGE_ATTRIB_HANDLE:
265       *value = image->region->buffer->handle;
266       return GL_TRUE;
267    case __DRI_IMAGE_ATTRIB_NAME:
268       return intel_region_flink(image->region, (uint32_t *) value);
269    default:
270       return GL_FALSE;
271    }
272 }
273
274 static struct __DRIimageExtensionRec intelImageExtension = {
275     { __DRI_IMAGE, __DRI_IMAGE_VERSION },
276     intel_create_image_from_name,
277     intel_create_image_from_renderbuffer,
278     intel_destroy_image,
279     intel_create_image,
280     intel_query_image
281 };
282
283 static const __DRIextension *intelScreenExtensions[] = {
284     &driReadDrawableExtension,
285     &intelTexBufferExtension.base,
286     &intelFlushExtension.base,
287     &intelImageExtension.base,
288     &dri2ConfigQueryExtension.base,
289     NULL
290 };
291
292 static GLboolean
293 intel_get_param(__DRIscreen *psp, int param, int *value)
294 {
295    int ret;
296    struct drm_i915_getparam gp;
297
298    gp.param = param;
299    gp.value = value;
300
301    ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
302    if (ret) {
303       _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
304       return GL_FALSE;
305    }
306
307    return GL_TRUE;
308 }
309
310 static void
311 nop_callback(GLuint key, void *data, void *userData)
312 {
313 }
314
315 static void
316 intelDestroyScreen(__DRIscreen * sPriv)
317 {
318    struct intel_screen *intelScreen = sPriv->private;
319
320    dri_bufmgr_destroy(intelScreen->bufmgr);
321    driDestroyOptionInfo(&intelScreen->optionCache);
322
323    /* Some regions may still have references to them at this point, so
324     * flush the hash table to prevent _mesa_DeleteHashTable() from
325     * complaining about the hash not being empty; */
326    _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
327    _mesa_DeleteHashTable(intelScreen->named_regions);
328
329    FREE(intelScreen);
330    sPriv->private = NULL;
331 }
332
333
334 /**
335  * This is called when we need to set up GL rendering to a new X window.
336  */
337 static GLboolean
338 intelCreateBuffer(__DRIscreen * driScrnPriv,
339                   __DRIdrawable * driDrawPriv,
340                   const struct gl_config * mesaVis, GLboolean isPixmap)
341 {
342    struct intel_renderbuffer *rb;
343
344    if (isPixmap) {
345       return GL_FALSE;          /* not implemented */
346    }
347    else {
348       GLboolean swStencil = (mesaVis->stencilBits > 0 &&
349                              mesaVis->depthBits != 24);
350       gl_format rgbFormat;
351
352       struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
353
354       if (!fb)
355          return GL_FALSE;
356
357       _mesa_initialize_window_framebuffer(fb, mesaVis);
358
359       if (mesaVis->redBits == 5)
360          rgbFormat = MESA_FORMAT_RGB565;
361       else if (mesaVis->alphaBits == 0)
362          rgbFormat = MESA_FORMAT_XRGB8888;
363       else
364          rgbFormat = MESA_FORMAT_ARGB8888;
365
366       /* setup the hardware-based renderbuffers */
367       rb = intel_create_renderbuffer(rgbFormat);
368       _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
369
370       if (mesaVis->doubleBufferMode) {
371          rb = intel_create_renderbuffer(rgbFormat);
372          _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
373       }
374
375       if (mesaVis->depthBits == 24) {
376          assert(mesaVis->stencilBits == 8);
377          /* combined depth/stencil buffer */
378          struct intel_renderbuffer *depthStencilRb
379             = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
380          /* note: bind RB to two attachment points */
381          _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
382          _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
383       }
384       else if (mesaVis->depthBits == 16) {
385          /* just 16-bit depth buffer, no hw stencil */
386          struct intel_renderbuffer *depthRb
387             = intel_create_renderbuffer(MESA_FORMAT_Z16);
388          _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
389       }
390
391       /* now add any/all software-based renderbuffers we may need */
392       _mesa_add_soft_renderbuffers(fb,
393                                    GL_FALSE, /* never sw color */
394                                    GL_FALSE, /* never sw depth */
395                                    swStencil, mesaVis->accumRedBits > 0,
396                                    GL_FALSE, /* never sw alpha */
397                                    GL_FALSE  /* never sw aux */ );
398       driDrawPriv->driverPrivate = fb;
399
400       return GL_TRUE;
401    }
402 }
403
404 static void
405 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
406 {
407     struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
408   
409     _mesa_reference_framebuffer(&fb, NULL);
410 }
411
412 /* There are probably better ways to do this, such as an
413  * init-designated function to register chipids and createcontext
414  * functions.
415  */
416 extern GLboolean i830CreateContext(const struct gl_config * mesaVis,
417                                    __DRIcontext * driContextPriv,
418                                    void *sharedContextPrivate);
419
420 extern GLboolean i915CreateContext(int api,
421                                    const struct gl_config * mesaVis,
422                                    __DRIcontext * driContextPriv,
423                                    void *sharedContextPrivate);
424 extern GLboolean brwCreateContext(int api,
425                                   const struct gl_config * mesaVis,
426                                   __DRIcontext * driContextPriv,
427                                   void *sharedContextPrivate);
428
429 static GLboolean
430 intelCreateContext(gl_api api,
431                    const struct gl_config * mesaVis,
432                    __DRIcontext * driContextPriv,
433                    void *sharedContextPrivate)
434 {
435    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
436    struct intel_screen *intelScreen = sPriv->private;
437
438 #ifdef I915
439    if (IS_9XX(intelScreen->deviceID)) {
440       if (!IS_965(intelScreen->deviceID)) {
441          return i915CreateContext(api, mesaVis, driContextPriv,
442                                   sharedContextPrivate);
443       }
444    } else {
445       intelScreen->no_vbo = GL_TRUE;
446       return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
447    }
448 #else
449    if (IS_965(intelScreen->deviceID))
450       return brwCreateContext(api, mesaVis,
451                               driContextPriv, sharedContextPrivate);
452 #endif
453    fprintf(stderr, "Unrecognized deviceID 0x%x\n", intelScreen->deviceID);
454    return GL_FALSE;
455 }
456
457 static GLboolean
458 intel_init_bufmgr(struct intel_screen *intelScreen)
459 {
460    __DRIscreen *spriv = intelScreen->driScrnPriv;
461    int num_fences = 0;
462
463    intelScreen->no_hw = (getenv("INTEL_NO_HW") != NULL ||
464                          getenv("INTEL_DEVID_OVERRIDE") != NULL);
465
466    intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
467    if (intelScreen->bufmgr == NULL) {
468       fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
469               __func__, __LINE__);
470       return GL_FALSE;
471    }
472
473    if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
474        num_fences == 0) {
475       fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
476       return GL_FALSE;
477    }
478
479    drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
480
481    intelScreen->named_regions = _mesa_NewHashTable();
482
483    return GL_TRUE;
484 }
485
486 /**
487  * This is the driver specific part of the createNewScreen entry point.
488  * Called when using DRI2.
489  *
490  * \return the struct gl_config supported by this driver
491  */
492 static const
493 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
494 {
495    struct intel_screen *intelScreen;
496    GLenum fb_format[3];
497    GLenum fb_type[3];
498    unsigned int api_mask;
499    char *devid_override;
500
501    static const GLenum back_buffer_modes[] = {
502        GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
503    };
504    uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
505    int color;
506    __DRIconfig **configs = NULL;
507
508    /* Allocate the private area */
509    intelScreen = CALLOC(sizeof *intelScreen);
510    if (!intelScreen) {
511       fprintf(stderr, "\nERROR!  Allocating private area failed\n");
512       return GL_FALSE;
513    }
514    /* parse information in __driConfigOptions */
515    driParseOptionInfo(&intelScreen->optionCache,
516                       __driConfigOptions, __driNConfigOptions);
517
518    intelScreen->driScrnPriv = psp;
519    psp->private = (void *) intelScreen;
520
521    /* Determine chipset ID */
522    if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
523                         &intelScreen->deviceID))
524       return GL_FALSE;
525
526    /* Allow an override of the device ID for the purpose of making the
527     * driver produce dumps for debugging of new chipset enablement.
528     * This implies INTEL_NO_HW, to avoid programming your actual GPU
529     * incorrectly.
530     */
531    devid_override = getenv("INTEL_DEVID_OVERRIDE");
532    if (devid_override) {
533       intelScreen->deviceID = strtod(devid_override, NULL);
534    }
535
536    api_mask = (1 << __DRI_API_OPENGL);
537 #if FEATURE_ES1
538    api_mask |= (1 << __DRI_API_GLES);
539 #endif
540 #if FEATURE_ES2
541    api_mask |= (1 << __DRI_API_GLES2);
542 #endif
543
544    if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
545       psp->api_mask = api_mask;
546
547    if (!intel_init_bufmgr(intelScreen))
548        return GL_FALSE;
549
550    psp->extensions = intelScreenExtensions;
551
552    msaa_samples_array[0] = 0;
553
554    fb_format[0] = GL_RGB;
555    fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
556
557    fb_format[1] = GL_BGR;
558    fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
559
560    fb_format[2] = GL_BGRA;
561    fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
562
563    depth_bits[0] = 0;
564    stencil_bits[0] = 0;
565
566    /* Generate a rich set of useful configs that do not include an
567     * accumulation buffer.
568     */
569    for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
570       __DRIconfig **new_configs;
571       int depth_factor;
572
573       /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
574        * buffer that has a diffferent number of bits per pixel than the color
575        * buffer.  This isn't yet supported here.
576        */
577       if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
578          depth_bits[1] = 16;
579          stencil_bits[1] = 0;
580       } else {
581          depth_bits[1] = 24;
582          stencil_bits[1] = 8;
583       }
584
585       depth_factor = 2;
586
587       new_configs = driCreateConfigs(fb_format[color], fb_type[color],
588                                      depth_bits,
589                                      stencil_bits,
590                                      depth_factor,
591                                      back_buffer_modes,
592                                      ARRAY_SIZE(back_buffer_modes),
593                                      msaa_samples_array,
594                                      ARRAY_SIZE(msaa_samples_array),
595                                      GL_FALSE);
596       if (configs == NULL)
597          configs = new_configs;
598       else
599          configs = driConcatConfigs(configs, new_configs);
600    }
601
602    /* Generate the minimum possible set of configs that include an
603     * accumulation buffer.
604     */
605    for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
606       __DRIconfig **new_configs;
607
608       if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
609          depth_bits[0] = 16;
610          stencil_bits[0] = 0;
611       } else {
612          depth_bits[0] = 24;
613          stencil_bits[0] = 8;
614       }
615
616       new_configs = driCreateConfigs(fb_format[color], fb_type[color],
617                                      depth_bits, stencil_bits, 1,
618                                      back_buffer_modes + 1, 1,
619                                      msaa_samples_array, 1,
620                                      GL_TRUE);
621       if (configs == NULL)
622          configs = new_configs;
623       else
624          configs = driConcatConfigs(configs, new_configs);
625    }
626
627    if (configs == NULL) {
628       fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
629               __LINE__);
630       return NULL;
631    }
632
633    return (const __DRIconfig **)configs;
634 }
635
636 const struct __DriverAPIRec driDriverAPI = {
637    .DestroyScreen        = intelDestroyScreen,
638    .CreateContext        = intelCreateContext,
639    .DestroyContext       = intelDestroyContext,
640    .CreateBuffer         = intelCreateBuffer,
641    .DestroyBuffer        = intelDestroyBuffer,
642    .MakeCurrent          = intelMakeCurrent,
643    .UnbindContext        = intelUnbindContext,
644    .InitScreen2          = intelInitScreen2,
645 };
646
647 /* This is the table of extensions that the loader will dlsym() for. */
648 PUBLIC const __DRIextension *__driDriverExtensions[] = {
649     &driCoreExtension.base,
650     &driDRI2Extension.base,
651     NULL
652 };