Merge branch 'gles2-2'
[profile/ivi/mesa.git] / src / mesa / drivers / dri / intel / intel_screen.c
1 /**************************************************************************
2  * 
3  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * All Rights Reserved.
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  * 
26  **************************************************************************/
27
28 #include "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 #include "intel_batchbuffer.h"
39 #include "intel_buffers.h"
40 #include "intel_bufmgr.h"
41 #include "intel_chipset.h"
42 #include "intel_fbo.h"
43 #include "intel_screen.h"
44 #include "intel_tex.h"
45 #include "intel_regions.h"
46
47 #include "i915_drm.h"
48
49 #define DRI_CONF_TEXTURE_TILING(def) \
50
51 PUBLIC const char __driConfigOptions[] =
52    DRI_CONF_BEGIN
53    DRI_CONF_SECTION_PERFORMANCE
54       DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
55       /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
56        * DRI_CONF_BO_REUSE_ALL
57        */
58       DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
59          DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
60             DRI_CONF_ENUM(0, "Disable buffer object reuse")
61             DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
62          DRI_CONF_DESC_END
63       DRI_CONF_OPT_END
64
65       DRI_CONF_OPT_BEGIN(texture_tiling, bool, true)
66          DRI_CONF_DESC(en, "Enable texture tiling")
67       DRI_CONF_OPT_END
68
69       DRI_CONF_OPT_BEGIN(early_z, bool, false)
70          DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
71       DRI_CONF_OPT_END
72
73       DRI_CONF_OPT_BEGIN(fragment_shader, bool, false)
74          DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
75       DRI_CONF_OPT_END
76
77    DRI_CONF_SECTION_END
78    DRI_CONF_SECTION_QUALITY
79       DRI_CONF_FORCE_S3TC_ENABLE(false)
80       DRI_CONF_ALLOW_LARGE_TEXTURES(2)
81    DRI_CONF_SECTION_END
82    DRI_CONF_SECTION_DEBUG
83      DRI_CONF_NO_RAST(false)
84      DRI_CONF_ALWAYS_FLUSH_BATCH(false)
85      DRI_CONF_ALWAYS_FLUSH_CACHE(false)
86
87       DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
88          DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
89       DRI_CONF_OPT_END
90    DRI_CONF_SECTION_END
91 DRI_CONF_END;
92
93 const GLuint __driNConfigOptions = 11;
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    struct intel_context *intel = drawable->driContextPriv->driverPrivate;
109
110    if (intel->gen < 4)
111       INTEL_FIREVERTICES(intel);
112
113    if (intel->batch->map != intel->batch->ptr)
114       intel_batchbuffer_flush(intel->batch);
115 }
116
117 static void
118 intelDRI2Invalidate(__DRIdrawable *drawable)
119 {
120    struct intel_context *intel = drawable->driContextPriv->driverPrivate;
121
122    intel->using_dri2_swapbuffers = GL_TRUE;
123    dri2InvalidateDrawable(drawable);
124 }
125
126 static const struct __DRI2flushExtensionRec intelFlushExtension = {
127     { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
128     intelDRI2Flush,
129     intelDRI2Invalidate,
130 };
131
132 static __DRIimage *
133 intel_create_image_from_name(__DRIcontext *context,
134                              int width, int height, int format,
135                              int name, int pitch, void *loaderPrivate)
136 {
137     __DRIimage *image;
138     struct intel_context *intel = context->driverPrivate;
139     int cpp;
140
141     image = CALLOC(sizeof *image);
142     if (image == NULL)
143         return NULL;
144
145     switch (format) {
146     case __DRI_IMAGE_FORMAT_RGB565:
147        image->format = MESA_FORMAT_RGB565;
148        image->internal_format = GL_RGB;
149        image->data_type = GL_UNSIGNED_BYTE;
150        break;
151     case __DRI_IMAGE_FORMAT_XRGB8888:
152        image->format = MESA_FORMAT_XRGB8888;
153        image->internal_format = GL_RGB;
154        image->data_type = GL_UNSIGNED_BYTE;
155        break;
156     case __DRI_IMAGE_FORMAT_ARGB8888:
157        image->format = MESA_FORMAT_ARGB8888;
158        image->internal_format = GL_RGBA;
159        image->data_type = GL_UNSIGNED_BYTE;
160        break;
161     default:
162        free(image);
163        return NULL;
164     }
165
166     image->data = loaderPrivate;
167     cpp = _mesa_get_format_bytes(image->format);
168
169     image->region = intel_region_alloc_for_handle(intel, cpp, width, height,
170                                                   pitch, name, "image");
171     if (image->region == NULL) {
172        FREE(image);
173        return NULL;
174     }
175
176     return image;       
177 }
178
179 static __DRIimage *
180 intel_create_image_from_renderbuffer(__DRIcontext *context,
181                                      int renderbuffer, void *loaderPrivate)
182 {
183    __DRIimage *image;
184    struct intel_context *intel = context->driverPrivate;
185    struct gl_renderbuffer *rb;
186    struct intel_renderbuffer *irb;
187
188    rb = _mesa_lookup_renderbuffer(&intel->ctx, renderbuffer);
189    if (!rb) {
190       _mesa_error(&intel->ctx,
191                   GL_INVALID_OPERATION, "glRenderbufferExternalMESA");
192       return NULL;
193    }
194
195    irb = intel_renderbuffer(rb);
196    image = CALLOC(sizeof *image);
197    if (image == NULL)
198       return NULL;
199
200    image->internal_format = rb->InternalFormat;
201    image->format = rb->Format;
202    image->data_type = rb->DataType;
203    image->data = loaderPrivate;
204    intel_region_reference(&image->region, irb->region);
205
206    return image;
207 }
208
209 static void
210 intel_destroy_image(__DRIimage *image)
211 {
212     intel_region_release(&image->region);
213     FREE(image);
214 }
215
216 static struct __DRIimageExtensionRec intelImageExtension = {
217     { __DRI_IMAGE, __DRI_IMAGE_VERSION },
218     intel_create_image_from_name,
219     intel_create_image_from_renderbuffer,
220     intel_destroy_image,
221 };
222
223 static const __DRIextension *intelScreenExtensions[] = {
224     &driReadDrawableExtension,
225     &intelTexBufferExtension.base,
226     &intelFlushExtension.base,
227     &intelImageExtension.base,
228     &dri2ConfigQueryExtension.base,
229     NULL
230 };
231
232 static GLboolean
233 intel_get_param(__DRIscreen *psp, int param, int *value)
234 {
235    int ret;
236    struct drm_i915_getparam gp;
237
238    gp.param = param;
239    gp.value = value;
240
241    ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
242    if (ret) {
243       _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
244       return GL_FALSE;
245    }
246
247    return GL_TRUE;
248 }
249
250 static void
251 nop_callback(GLuint key, void *data, void *userData)
252 {
253 }
254
255 static void
256 intelDestroyScreen(__DRIscreen * sPriv)
257 {
258    struct intel_screen *intelScreen = sPriv->private;
259
260    dri_bufmgr_destroy(intelScreen->bufmgr);
261    driDestroyOptionInfo(&intelScreen->optionCache);
262
263    /* Some regions may still have references to them at this point, so
264     * flush the hash table to prevent _mesa_DeleteHashTable() from
265     * complaining about the hash not being empty; */
266    _mesa_HashDeleteAll(intelScreen->named_regions, nop_callback, NULL);
267    _mesa_DeleteHashTable(intelScreen->named_regions);
268
269    FREE(intelScreen);
270    sPriv->private = NULL;
271 }
272
273
274 /**
275  * This is called when we need to set up GL rendering to a new X window.
276  */
277 static GLboolean
278 intelCreateBuffer(__DRIscreen * driScrnPriv,
279                   __DRIdrawable * driDrawPriv,
280                   const __GLcontextModes * mesaVis, GLboolean isPixmap)
281 {
282    struct intel_renderbuffer *rb;
283
284    if (isPixmap) {
285       return GL_FALSE;          /* not implemented */
286    }
287    else {
288       GLboolean swStencil = (mesaVis->stencilBits > 0 &&
289                              mesaVis->depthBits != 24);
290       gl_format rgbFormat;
291
292       struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
293
294       if (!fb)
295          return GL_FALSE;
296
297       _mesa_initialize_window_framebuffer(fb, mesaVis);
298
299       if (mesaVis->redBits == 5)
300          rgbFormat = MESA_FORMAT_RGB565;
301       else if (mesaVis->alphaBits == 0)
302          rgbFormat = MESA_FORMAT_XRGB8888;
303       else
304          rgbFormat = MESA_FORMAT_ARGB8888;
305
306       /* setup the hardware-based renderbuffers */
307       rb = intel_create_renderbuffer(rgbFormat);
308       _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
309
310       if (mesaVis->doubleBufferMode) {
311          rb = intel_create_renderbuffer(rgbFormat);
312          _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
313       }
314
315       if (mesaVis->depthBits == 24) {
316          assert(mesaVis->stencilBits == 8);
317          /* combined depth/stencil buffer */
318          struct intel_renderbuffer *depthStencilRb
319             = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
320          /* note: bind RB to two attachment points */
321          _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
322          _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
323       }
324       else if (mesaVis->depthBits == 16) {
325          /* just 16-bit depth buffer, no hw stencil */
326          struct intel_renderbuffer *depthRb
327             = intel_create_renderbuffer(MESA_FORMAT_Z16);
328          _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
329       }
330
331       /* now add any/all software-based renderbuffers we may need */
332       _mesa_add_soft_renderbuffers(fb,
333                                    GL_FALSE, /* never sw color */
334                                    GL_FALSE, /* never sw depth */
335                                    swStencil, mesaVis->accumRedBits > 0,
336                                    GL_FALSE, /* never sw alpha */
337                                    GL_FALSE  /* never sw aux */ );
338       driDrawPriv->driverPrivate = fb;
339
340       return GL_TRUE;
341    }
342 }
343
344 static void
345 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
346 {
347     struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
348   
349     _mesa_reference_framebuffer(&fb, NULL);
350 }
351
352 /* There are probably better ways to do this, such as an
353  * init-designated function to register chipids and createcontext
354  * functions.
355  */
356 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
357                                    __DRIcontext * driContextPriv,
358                                    void *sharedContextPrivate);
359
360 extern GLboolean i915CreateContext(int api,
361                                    const __GLcontextModes * mesaVis,
362                                    __DRIcontext * driContextPriv,
363                                    void *sharedContextPrivate);
364 extern GLboolean brwCreateContext(int api,
365                                   const __GLcontextModes * mesaVis,
366                                   __DRIcontext * driContextPriv,
367                                   void *sharedContextPrivate);
368
369 static GLboolean
370 intelCreateContext(gl_api api,
371                    const __GLcontextModes * mesaVis,
372                    __DRIcontext * driContextPriv,
373                    void *sharedContextPrivate)
374 {
375    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
376    struct intel_screen *intelScreen = sPriv->private;
377
378 #ifdef I915
379    if (IS_9XX(intelScreen->deviceID)) {
380       if (!IS_965(intelScreen->deviceID)) {
381          return i915CreateContext(api, mesaVis, driContextPriv,
382                                   sharedContextPrivate);
383       }
384    } else {
385       intelScreen->no_vbo = GL_TRUE;
386       return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
387    }
388 #else
389    if (IS_965(intelScreen->deviceID))
390       return brwCreateContext(api, mesaVis,
391                               driContextPriv, sharedContextPrivate);
392 #endif
393    fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
394    return GL_FALSE;
395 }
396
397 static GLboolean
398 intel_init_bufmgr(struct intel_screen *intelScreen)
399 {
400    __DRIscreen *spriv = intelScreen->driScrnPriv;
401    int num_fences = 0;
402
403    intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
404
405    intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
406    /* Otherwise, use the classic buffer manager. */
407    if (intelScreen->bufmgr == NULL) {
408       fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
409               __func__, __LINE__);
410       return GL_FALSE;
411    }
412
413    if (!intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences) ||
414        num_fences == 0) {
415       fprintf(stderr, "[%s: %u] Kernel 2.6.29 required.\n", __func__, __LINE__);
416       return GL_FALSE;
417    }
418
419    drm_intel_bufmgr_gem_enable_fenced_relocs(intelScreen->bufmgr);
420
421    intelScreen->named_regions = _mesa_NewHashTable();
422
423    return GL_TRUE;
424 }
425
426 /**
427  * This is the driver specific part of the createNewScreen entry point.
428  * Called when using DRI2.
429  *
430  * \return the __GLcontextModes supported by this driver
431  */
432 static const
433 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
434 {
435    struct intel_screen *intelScreen;
436    GLenum fb_format[3];
437    GLenum fb_type[3];
438    unsigned int api_mask;
439
440    static const GLenum back_buffer_modes[] = {
441        GLX_NONE, GLX_SWAP_UNDEFINED_OML, GLX_SWAP_COPY_OML
442    };
443    uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
444    int color;
445    __DRIconfig **configs = NULL;
446
447    /* Allocate the private area */
448    intelScreen = CALLOC(sizeof *intelScreen);
449    if (!intelScreen) {
450       fprintf(stderr, "\nERROR!  Allocating private area failed\n");
451       return GL_FALSE;
452    }
453    /* parse information in __driConfigOptions */
454    driParseOptionInfo(&intelScreen->optionCache,
455                       __driConfigOptions, __driNConfigOptions);
456
457    intelScreen->driScrnPriv = psp;
458    psp->private = (void *) intelScreen;
459
460    /* Determine chipset ID */
461    if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
462                         &intelScreen->deviceID))
463       return GL_FALSE;
464
465    api_mask = (1 << __DRI_API_OPENGL);
466 #if FEATURE_ES1
467    api_mask |= (1 << __DRI_API_GLES);
468 #endif
469 #if FEATURE_ES2
470    api_mask |= (1 << __DRI_API_GLES2);
471 #endif
472
473    if (IS_9XX(intelScreen->deviceID) || IS_965(intelScreen->deviceID))
474       psp->api_mask = api_mask;
475
476    if (!intel_init_bufmgr(intelScreen))
477        return GL_FALSE;
478
479    psp->extensions = intelScreenExtensions;
480
481    msaa_samples_array[0] = 0;
482
483    fb_format[0] = GL_RGB;
484    fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
485
486    fb_format[1] = GL_BGR;
487    fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
488
489    fb_format[2] = GL_BGRA;
490    fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
491
492    depth_bits[0] = 0;
493    stencil_bits[0] = 0;
494
495    /* Generate a rich set of useful configs that do not include an
496     * accumulation buffer.
497     */
498    for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
499       __DRIconfig **new_configs;
500       int depth_factor;
501
502       /* Starting with DRI2 protocol version 1.1 we can request a depth/stencil
503        * buffer that has a diffferent number of bits per pixel than the color
504        * buffer.  This isn't yet supported here.
505        */
506       if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
507          depth_bits[1] = 16;
508          stencil_bits[1] = 0;
509       } else {
510          depth_bits[1] = 24;
511          stencil_bits[1] = 8;
512       }
513
514       depth_factor = 2;
515
516       new_configs = driCreateConfigs(fb_format[color], fb_type[color],
517                                      depth_bits,
518                                      stencil_bits,
519                                      depth_factor,
520                                      back_buffer_modes,
521                                      ARRAY_SIZE(back_buffer_modes),
522                                      msaa_samples_array,
523                                      ARRAY_SIZE(msaa_samples_array),
524                                      GL_FALSE);
525       if (configs == NULL)
526          configs = new_configs;
527       else
528          configs = driConcatConfigs(configs, new_configs);
529    }
530
531    /* Generate the minimum possible set of configs that include an
532     * accumulation buffer.
533     */
534    for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
535       __DRIconfig **new_configs;
536
537       if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
538          depth_bits[0] = 16;
539          stencil_bits[0] = 0;
540       } else {
541          depth_bits[0] = 24;
542          stencil_bits[0] = 8;
543       }
544
545       new_configs = driCreateConfigs(fb_format[color], fb_type[color],
546                                      depth_bits, stencil_bits, 1,
547                                      back_buffer_modes + 1, 1,
548                                      msaa_samples_array, 1,
549                                      GL_TRUE);
550       if (configs == NULL)
551          configs = new_configs;
552       else
553          configs = driConcatConfigs(configs, new_configs);
554    }
555
556    if (configs == NULL) {
557       fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
558               __LINE__);
559       return NULL;
560    }
561
562    return (const __DRIconfig **)configs;
563 }
564
565 const struct __DriverAPIRec driDriverAPI = {
566    .DestroyScreen        = intelDestroyScreen,
567    .CreateContext        = intelCreateContext,
568    .DestroyContext       = intelDestroyContext,
569    .CreateBuffer         = intelCreateBuffer,
570    .DestroyBuffer        = intelDestroyBuffer,
571    .MakeCurrent          = intelMakeCurrent,
572    .UnbindContext        = intelUnbindContext,
573    .InitScreen2          = intelInitScreen2,
574 };
575
576 /* This is the table of extensions that the loader will dlsym() for. */
577 PUBLIC const __DRIextension *__driDriverExtensions[] = {
578     &driCoreExtension.base,
579     &driDRI2Extension.base,
580     NULL
581 };