OSDN Git Service

fe504166427bae4b06ae415a375213a9f007d4d1
[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
33 #include "utils.h"
34 #include "xmlpool.h"
35
36 #include "intel_batchbuffer.h"
37 #include "intel_buffers.h"
38 #include "intel_bufmgr.h"
39 #include "intel_chipset.h"
40 #include "intel_fbo.h"
41 #include "intel_regions.h"
42 #include "intel_screen.h"
43 #include "intel_tex.h"
44
45 #include "i915_drm.h"
46 #include "i830_dri.h"
47
48 #define DRI_CONF_TEXTURE_TILING(def) \
49         DRI_CONF_OPT_BEGIN(texture_tiling, bool, def)           \
50                 DRI_CONF_DESC(en, "Enable texture tiling")      \
51         DRI_CONF_OPT_END                                        \
52
53 PUBLIC const char __driConfigOptions[] =
54    DRI_CONF_BEGIN
55    DRI_CONF_SECTION_PERFORMANCE
56       DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_ALWAYS_SYNC)
57       /* Options correspond to DRI_CONF_BO_REUSE_DISABLED,
58        * DRI_CONF_BO_REUSE_ALL
59        */
60       DRI_CONF_OPT_BEGIN_V(bo_reuse, enum, 1, "0:1")
61          DRI_CONF_DESC_BEGIN(en, "Buffer object reuse")
62             DRI_CONF_ENUM(0, "Disable buffer object reuse")
63             DRI_CONF_ENUM(1, "Enable reuse of all sizes of buffer objects")
64          DRI_CONF_DESC_END
65       DRI_CONF_OPT_END
66
67 #ifdef I915
68      DRI_CONF_TEXTURE_TILING(false)
69 #else
70      DRI_CONF_TEXTURE_TILING(true)
71 #endif
72
73       DRI_CONF_OPT_BEGIN(early_z, bool, false)
74          DRI_CONF_DESC(en, "Enable early Z in classic mode (unstable, 945-only).")
75       DRI_CONF_OPT_END
76
77       DRI_CONF_OPT_BEGIN(fragment_shader, bool, false)
78          DRI_CONF_DESC(en, "Enable limited ARB_fragment_shader support on 915/945.")
79       DRI_CONF_OPT_END
80
81    DRI_CONF_SECTION_END
82    DRI_CONF_SECTION_QUALITY
83       DRI_CONF_FORCE_S3TC_ENABLE(false)
84       DRI_CONF_ALLOW_LARGE_TEXTURES(2)
85    DRI_CONF_SECTION_END
86    DRI_CONF_SECTION_DEBUG
87      DRI_CONF_NO_RAST(false)
88      DRI_CONF_ALWAYS_FLUSH_BATCH(false)
89      DRI_CONF_ALWAYS_FLUSH_CACHE(false)
90
91       DRI_CONF_OPT_BEGIN(stub_occlusion_query, bool, false)
92          DRI_CONF_DESC(en, "Enable stub ARB_occlusion_query support on 915/945.")
93       DRI_CONF_OPT_END
94    DRI_CONF_SECTION_END
95 DRI_CONF_END;
96
97 const GLuint __driNConfigOptions = 11;
98
99 #ifdef USE_NEW_INTERFACE
100 static PFNGLXCREATECONTEXTMODES create_context_modes = NULL;
101 #endif /*USE_NEW_INTERFACE */
102
103 static const __DRItexOffsetExtension intelTexOffsetExtension = {
104    { __DRI_TEX_OFFSET },
105    intelSetTexOffset,
106 };
107
108 static const __DRItexBufferExtension intelTexBufferExtension = {
109     { __DRI_TEX_BUFFER, __DRI_TEX_BUFFER_VERSION },
110    intelSetTexBuffer,
111    intelSetTexBuffer2,
112 };
113
114 static void
115 intelDRI2Flush(__DRIdrawable *drawable)
116 {
117    struct intel_context *intel = drawable->driContextPriv->driverPrivate;
118
119    if (intel->gen < 4)
120       INTEL_FIREVERTICES(intel);
121
122    if (intel->batch->map != intel->batch->ptr)
123       intel_batchbuffer_flush(intel->batch);
124 }
125
126 static void
127 intelDRI2FlushInvalidate(__DRIdrawable *drawable)
128 {
129    struct intel_context *intel = drawable->driContextPriv->driverPrivate;
130
131    intelDRI2Flush(drawable);
132    drawable->validBuffers = GL_FALSE;
133
134    /* We're using FlushInvalidate as an indicator that a frame is
135     * done.  It's only called immediately after SwapBuffers, so it
136     * won't affect front-buffer rendering or applications explicitly
137     * managing swap regions using MESA_copy_buffer.
138     *
139     * Wait for the swapbuffers before the one we just emitted, so we don't
140     * get too many swaps outstanding for apps that are GPU-heavy but not
141     * CPU-heavy.
142     *
143     * Unfortunately, we don't have a handle to the batch containing the swap,
144     * and getting our hands on that doesn't seem worth it, so we just use the
145     * first batch we emitted after the last swap.
146     */
147    if (intel->first_post_swapbuffers_batch != NULL) {
148       drm_intel_bo_wait_rendering(intel->first_post_swapbuffers_batch);
149       drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
150       intel->first_post_swapbuffers_batch = NULL;
151    }
152 }
153
154 static const struct __DRI2flushExtensionRec intelFlushExtension = {
155     { __DRI2_FLUSH, __DRI2_FLUSH_VERSION },
156     intelDRI2Flush,
157     intelDRI2FlushInvalidate,
158 };
159
160 static const __DRIextension *intelScreenExtensions[] = {
161     &driReadDrawableExtension,
162     &intelTexOffsetExtension.base,
163     &intelTexBufferExtension.base,
164     &intelFlushExtension.base,
165     NULL
166 };
167
168 static GLboolean
169 intel_get_param(__DRIscreen *psp, int param, int *value)
170 {
171    int ret;
172    struct drm_i915_getparam gp;
173
174    gp.param = param;
175    gp.value = value;
176
177    ret = drmCommandWriteRead(psp->fd, DRM_I915_GETPARAM, &gp, sizeof(gp));
178    if (ret) {
179       _mesa_warning(NULL, "drm_i915_getparam: %d", ret);
180       return GL_FALSE;
181    }
182
183    return GL_TRUE;
184 }
185
186 static void
187 intelDestroyScreen(__DRIscreen * sPriv)
188 {
189    intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
190
191    dri_bufmgr_destroy(intelScreen->bufmgr);
192    driDestroyOptionInfo(&intelScreen->optionCache);
193
194    FREE(intelScreen);
195    sPriv->private = NULL;
196 }
197
198
199 /**
200  * This is called when we need to set up GL rendering to a new X window.
201  */
202 static GLboolean
203 intelCreateBuffer(__DRIscreen * driScrnPriv,
204                   __DRIdrawable * driDrawPriv,
205                   const __GLcontextModes * mesaVis, GLboolean isPixmap)
206 {
207    struct intel_renderbuffer *rb;
208
209    if (isPixmap) {
210       return GL_FALSE;          /* not implemented */
211    }
212    else {
213       GLboolean swStencil = (mesaVis->stencilBits > 0 &&
214                              mesaVis->depthBits != 24);
215       gl_format rgbFormat;
216
217       struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
218
219       if (!fb)
220          return GL_FALSE;
221
222       _mesa_initialize_framebuffer(fb, mesaVis);
223
224       if (mesaVis->redBits == 5)
225          rgbFormat = MESA_FORMAT_RGB565;
226       else if (mesaVis->alphaBits == 0)
227          rgbFormat = MESA_FORMAT_XRGB8888;
228       else
229          rgbFormat = MESA_FORMAT_ARGB8888;
230
231       /* setup the hardware-based renderbuffers */
232       rb = intel_create_renderbuffer(rgbFormat);
233       _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &rb->Base);
234
235       if (mesaVis->doubleBufferMode) {
236          rb = intel_create_renderbuffer(rgbFormat);
237          _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &rb->Base);
238       }
239
240       if (mesaVis->depthBits == 24) {
241          if (mesaVis->stencilBits == 8) {
242             /* combined depth/stencil buffer */
243             struct intel_renderbuffer *depthStencilRb
244                = intel_create_renderbuffer(MESA_FORMAT_S8_Z24);
245             /* note: bind RB to two attachment points */
246             _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthStencilRb->Base);
247             _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &depthStencilRb->Base);
248          } else {
249             struct intel_renderbuffer *depthRb
250                = intel_create_renderbuffer(MESA_FORMAT_X8_Z24);
251             _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
252          }
253       }
254       else if (mesaVis->depthBits == 16) {
255          /* just 16-bit depth buffer, no hw stencil */
256          struct intel_renderbuffer *depthRb
257             = intel_create_renderbuffer(MESA_FORMAT_Z16);
258          _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
259       }
260
261       /* now add any/all software-based renderbuffers we may need */
262       _mesa_add_soft_renderbuffers(fb,
263                                    GL_FALSE, /* never sw color */
264                                    GL_FALSE, /* never sw depth */
265                                    swStencil, mesaVis->accumRedBits > 0,
266                                    GL_FALSE, /* never sw alpha */
267                                    GL_FALSE  /* never sw aux */ );
268       driDrawPriv->driverPrivate = fb;
269
270       return GL_TRUE;
271    }
272 }
273
274 static void
275 intelDestroyBuffer(__DRIdrawable * driDrawPriv)
276 {
277     struct gl_framebuffer *fb = driDrawPriv->driverPrivate;
278   
279     _mesa_reference_framebuffer(&fb, NULL);
280 }
281
282 /* There are probably better ways to do this, such as an
283  * init-designated function to register chipids and createcontext
284  * functions.
285  */
286 extern GLboolean i830CreateContext(const __GLcontextModes * mesaVis,
287                                    __DRIcontext * driContextPriv,
288                                    void *sharedContextPrivate);
289
290 extern GLboolean i915CreateContext(const __GLcontextModes * mesaVis,
291                                    __DRIcontext * driContextPriv,
292                                    void *sharedContextPrivate);
293 extern GLboolean brwCreateContext(const __GLcontextModes * mesaVis,
294                                   __DRIcontext * driContextPriv,
295                                   void *sharedContextPrivate);
296
297 static GLboolean
298 intelCreateContext(const __GLcontextModes * mesaVis,
299                    __DRIcontext * driContextPriv,
300                    void *sharedContextPrivate)
301 {
302    __DRIscreen *sPriv = driContextPriv->driScreenPriv;
303    intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
304
305 #ifdef I915
306    if (IS_9XX(intelScreen->deviceID)) {
307       if (!IS_965(intelScreen->deviceID)) {
308          return i915CreateContext(mesaVis, driContextPriv,
309                                   sharedContextPrivate);
310       }
311    } else {
312       intelScreen->no_vbo = GL_TRUE;
313       return i830CreateContext(mesaVis, driContextPriv, sharedContextPrivate);
314    }
315 #else
316    if (IS_965(intelScreen->deviceID))
317       return brwCreateContext(mesaVis, driContextPriv, sharedContextPrivate);
318 #endif
319    fprintf(stderr, "Unrecognized deviceID %x\n", intelScreen->deviceID);
320    return GL_FALSE;
321 }
322
323 static GLboolean
324 intel_init_bufmgr(intelScreenPrivate *intelScreen)
325 {
326    __DRIscreen *spriv = intelScreen->driScrnPriv;
327    int num_fences = 0;
328
329    intelScreen->no_hw = getenv("INTEL_NO_HW") != NULL;
330
331    intelScreen->bufmgr = intel_bufmgr_gem_init(spriv->fd, BATCH_SZ);
332    /* Otherwise, use the classic buffer manager. */
333    if (intelScreen->bufmgr == NULL) {
334       fprintf(stderr, "[%s:%u] Error initializing buffer manager.\n",
335               __func__, __LINE__);
336       return GL_FALSE;
337    }
338
339    if (intel_get_param(spriv, I915_PARAM_NUM_FENCES_AVAIL, &num_fences))
340       intelScreen->kernel_exec_fencing = !!num_fences;
341    else
342       intelScreen->kernel_exec_fencing = GL_FALSE;
343
344    return GL_TRUE;
345 }
346
347 /**
348  * This is the driver specific part of the createNewScreen entry point.
349  * Called when using DRI2.
350  *
351  * \return the __GLcontextModes supported by this driver
352  */
353 static const
354 __DRIconfig **intelInitScreen2(__DRIscreen *psp)
355 {
356    intelScreenPrivate *intelScreen;
357    GLenum fb_format[3];
358    GLenum fb_type[3];
359
360    static const GLenum back_buffer_modes[] = {
361        GLX_NONE, GLX_SWAP_UNDEFINED_OML,
362        GLX_SWAP_EXCHANGE_OML, GLX_SWAP_COPY_OML
363    };
364    uint8_t depth_bits[4], stencil_bits[4], msaa_samples_array[1];
365    int color;
366    __DRIconfig **configs = NULL;
367
368    /* Allocate the private area */
369    intelScreen = (intelScreenPrivate *) CALLOC(sizeof(intelScreenPrivate));
370    if (!intelScreen) {
371       fprintf(stderr, "\nERROR!  Allocating private area failed\n");
372       return GL_FALSE;
373    }
374    /* parse information in __driConfigOptions */
375    driParseOptionInfo(&intelScreen->optionCache,
376                       __driConfigOptions, __driNConfigOptions);
377
378    intelScreen->driScrnPriv = psp;
379    psp->private = (void *) intelScreen;
380
381    intelScreen->drmMinor = psp->drm_version.minor;
382
383    /* Determine chipset ID */
384    if (!intel_get_param(psp, I915_PARAM_CHIPSET_ID,
385                         &intelScreen->deviceID))
386       return GL_FALSE;
387
388    if (!intel_init_bufmgr(intelScreen))
389        return GL_FALSE;
390
391    intelScreen->irq_active = 1;
392    psp->extensions = intelScreenExtensions;
393
394    depth_bits[0] = 0;
395    stencil_bits[0] = 0;
396    depth_bits[1] = 16;
397    stencil_bits[1] = 0;
398    depth_bits[2] = 24;
399    stencil_bits[2] = 0;
400    depth_bits[3] = 24;
401    stencil_bits[3] = 8;
402
403    msaa_samples_array[0] = 0;
404
405    fb_format[0] = GL_RGB;
406    fb_type[0] = GL_UNSIGNED_SHORT_5_6_5;
407
408    fb_format[1] = GL_BGR;
409    fb_type[1] = GL_UNSIGNED_INT_8_8_8_8_REV;
410
411    fb_format[2] = GL_BGRA;
412    fb_type[2] = GL_UNSIGNED_INT_8_8_8_8_REV;
413
414    depth_bits[0] = 0;
415    stencil_bits[0] = 0;
416
417    for (color = 0; color < ARRAY_SIZE(fb_format); color++) {
418       __DRIconfig **new_configs;
419       int depth_factor;
420
421       /* With DRI2 right now, GetBuffers always returns a depth/stencil buffer
422        * with the same cpp as the drawable.  So we can't support depth cpp !=
423        * color cpp currently.
424        */
425       if (fb_type[color] == GL_UNSIGNED_SHORT_5_6_5) {
426          depth_bits[1] = 16;
427          stencil_bits[1] = 0;
428
429          depth_factor = 2;
430       } else {
431          depth_bits[1] = 24;
432          stencil_bits[1] = 0;
433          depth_bits[2] = 24;
434          stencil_bits[2] = 8;
435
436          depth_factor = 3;
437       }
438       new_configs = driCreateConfigs(fb_format[color], fb_type[color],
439                                      depth_bits,
440                                      stencil_bits,
441                                      depth_factor,
442                                      back_buffer_modes,
443                                      ARRAY_SIZE(back_buffer_modes),
444                                      msaa_samples_array,
445                                      ARRAY_SIZE(msaa_samples_array));
446       if (configs == NULL)
447          configs = new_configs;
448       else
449          configs = driConcatConfigs(configs, new_configs);
450    }
451
452    if (configs == NULL) {
453       fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
454               __LINE__);
455       return NULL;
456    }
457
458    return (const __DRIconfig **)configs;
459 }
460
461 const struct __DriverAPIRec driDriverAPI = {
462    .DestroyScreen        = intelDestroyScreen,
463    .CreateContext        = intelCreateContext,
464    .DestroyContext       = intelDestroyContext,
465    .CreateBuffer         = intelCreateBuffer,
466    .DestroyBuffer        = intelDestroyBuffer,
467    .MakeCurrent          = intelMakeCurrent,
468    .UnbindContext        = intelUnbindContext,
469    .InitScreen2          = intelInitScreen2,
470 };
471
472 /* This is the table of extensions that the loader will dlsym() for. */
473 PUBLIC const __DRIextension *__driDriverExtensions[] = {
474     &driCoreExtension.base,
475     &driDRI2Extension.base,
476     NULL
477 };