ARB prog parser: Delete the old parser
[profile/ivi/mesa.git] / src / mesa / drivers / dri / intel / intel_context.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
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/arrayobj.h"
32 #include "main/extensions.h"
33 #include "main/framebuffer.h"
34 #include "main/imports.h"
35 #include "main/points.h"
36
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
39 #include "tnl/tnl.h"
40 #include "drivers/common/driverfuncs.h"
41
42 #include "i830_dri.h"
43
44 #include "intel_chipset.h"
45 #include "intel_buffers.h"
46 #include "intel_tex.h"
47 #include "intel_batchbuffer.h"
48 #include "intel_clear.h"
49 #include "intel_extensions.h"
50 #include "intel_pixel.h"
51 #include "intel_regions.h"
52 #include "intel_buffer_objects.h"
53 #include "intel_fbo.h"
54 #include "intel_decode.h"
55 #include "intel_bufmgr.h"
56 #include "intel_screen.h"
57 #include "intel_swapbuffers.h"
58
59 #include "drirenderbuffer.h"
60 #include "vblank.h"
61 #include "utils.h"
62 #include "xmlpool.h"            /* for symbolic values of enum-type options */
63
64
65 #ifndef INTEL_DEBUG
66 int INTEL_DEBUG = (0);
67 #endif
68
69
70 #define DRIVER_DATE                     "20090712 2009Q2 RC3"
71 #define DRIVER_DATE_GEM                 "GEM " DRIVER_DATE
72
73
74 static void intel_flush(GLcontext *ctx, GLboolean needs_mi_flush);
75
76 static const GLubyte *
77 intelGetString(GLcontext * ctx, GLenum name)
78 {
79    const struct intel_context *const intel = intel_context(ctx);
80    const char *chipset;
81    static char buffer[128];
82
83    switch (name) {
84    case GL_VENDOR:
85       return (GLubyte *) "Tungsten Graphics, Inc";
86       break;
87
88    case GL_RENDERER:
89       switch (intel->intelScreen->deviceID) {
90       case PCI_CHIP_845_G:
91          chipset = "Intel(R) 845G";
92          break;
93       case PCI_CHIP_I830_M:
94          chipset = "Intel(R) 830M";
95          break;
96       case PCI_CHIP_I855_GM:
97          chipset = "Intel(R) 852GM/855GM";
98          break;
99       case PCI_CHIP_I865_G:
100          chipset = "Intel(R) 865G";
101          break;
102       case PCI_CHIP_I915_G:
103          chipset = "Intel(R) 915G";
104          break;
105       case PCI_CHIP_E7221_G:
106          chipset = "Intel (R) E7221G (i915)";
107          break;
108       case PCI_CHIP_I915_GM:
109          chipset = "Intel(R) 915GM";
110          break;
111       case PCI_CHIP_I945_G:
112          chipset = "Intel(R) 945G";
113          break;
114       case PCI_CHIP_I945_GM:
115          chipset = "Intel(R) 945GM";
116          break;
117       case PCI_CHIP_I945_GME:
118          chipset = "Intel(R) 945GME";
119          break;
120       case PCI_CHIP_G33_G:
121          chipset = "Intel(R) G33";
122          break;
123       case PCI_CHIP_Q35_G:
124          chipset = "Intel(R) Q35";
125          break;
126       case PCI_CHIP_Q33_G:
127          chipset = "Intel(R) Q33";
128          break;
129       case PCI_CHIP_IGD_GM:
130       case PCI_CHIP_IGD_G:
131          chipset = "Intel(R) IGD";
132          break;
133       case PCI_CHIP_I965_Q:
134          chipset = "Intel(R) 965Q";
135          break;
136       case PCI_CHIP_I965_G:
137       case PCI_CHIP_I965_G_1:
138          chipset = "Intel(R) 965G";
139          break;
140       case PCI_CHIP_I946_GZ:
141          chipset = "Intel(R) 946GZ";
142          break;
143       case PCI_CHIP_I965_GM:
144          chipset = "Intel(R) 965GM";
145          break;
146       case PCI_CHIP_I965_GME:
147          chipset = "Intel(R) 965GME/GLE";
148          break;
149       case PCI_CHIP_GM45_GM:
150          chipset = "Mobile IntelĀ® GM45 Express Chipset";
151          break; 
152       case PCI_CHIP_IGD_E_G:
153          chipset = "Intel(R) Integrated Graphics Device";
154          break;
155       case PCI_CHIP_G45_G:
156          chipset = "Intel(R) G45/G43";
157          break;
158       case PCI_CHIP_Q45_G:
159          chipset = "Intel(R) Q45/Q43";
160          break;
161       case PCI_CHIP_G41_G:
162          chipset = "Intel(R) G41";
163          break;
164       case PCI_CHIP_ILD_G:
165          chipset = "Intel(R) IGDNG_D";
166          break;
167       case PCI_CHIP_ILM_G:
168          chipset = "Intel(R) IGDNG_M";
169          break;
170       default:
171          chipset = "Unknown Intel Chipset";
172          break;
173       }
174
175       (void) driGetRendererString(buffer, chipset, 
176                                   (intel->ttm) ? DRIVER_DATE_GEM : DRIVER_DATE,
177                                   0);
178       return (GLubyte *) buffer;
179
180    default:
181       return NULL;
182    }
183 }
184
185 static unsigned
186 intel_bits_per_pixel(const struct intel_renderbuffer *rb)
187 {
188    switch (rb->Base._ActualFormat) {
189    case GL_RGB5:
190    case GL_DEPTH_COMPONENT16:
191       return 16;
192    case GL_RGB8:
193    case GL_RGBA8:
194    case GL_DEPTH_COMPONENT24:
195    case GL_DEPTH24_STENCIL8_EXT:
196    case GL_STENCIL_INDEX8_EXT:
197       return 32;
198    default:
199       return 0;
200    }
201 }
202
203 void
204 intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
205 {
206    struct intel_framebuffer *intel_fb = drawable->driverPrivate;
207    struct intel_renderbuffer *rb;
208    struct intel_region *region, *depth_region;
209    struct intel_context *intel = context->driverPrivate;
210    __DRIbuffer *buffers = NULL;
211    __DRIscreen *screen;
212    int i, count;
213    unsigned int attachments[10];
214    uint32_t name;
215    const char *region_name;
216
217    if (INTEL_DEBUG & DEBUG_DRI)
218       fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
219
220    screen = intel->intelScreen->driScrnPriv;
221
222    if (screen->dri2.loader
223        && (screen->dri2.loader->base.version > 2)
224        && (screen->dri2.loader->getBuffersWithFormat != NULL)) {
225       struct intel_renderbuffer *depth_rb;
226       struct intel_renderbuffer *stencil_rb;
227
228       i = 0;
229       if ((intel->is_front_buffer_rendering ||
230            intel->is_front_buffer_reading ||
231            !intel_fb->color_rb[1])
232            && intel_fb->color_rb[0]) {
233          attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
234          attachments[i++] = intel_bits_per_pixel(intel_fb->color_rb[0]);
235       }
236
237       if (intel_fb->color_rb[1]) {
238          attachments[i++] = __DRI_BUFFER_BACK_LEFT;
239          attachments[i++] = intel_bits_per_pixel(intel_fb->color_rb[1]);
240       }
241
242       depth_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
243       stencil_rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
244
245       if ((depth_rb != NULL) && (stencil_rb != NULL)) {
246          attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
247          attachments[i++] = intel_bits_per_pixel(depth_rb);
248       } else if (depth_rb != NULL) {
249          attachments[i++] = __DRI_BUFFER_DEPTH;
250          attachments[i++] = intel_bits_per_pixel(depth_rb);
251       } else if (stencil_rb != NULL) {
252          attachments[i++] = __DRI_BUFFER_STENCIL;
253          attachments[i++] = intel_bits_per_pixel(stencil_rb);
254       }
255
256       buffers =
257          (*screen->dri2.loader->getBuffersWithFormat)(drawable,
258                                                       &drawable->w,
259                                                       &drawable->h,
260                                                       attachments, i / 2,
261                                                       &count,
262                                                       drawable->loaderPrivate);
263    } else if (screen->dri2.loader) {
264       i = 0;
265       if (intel_fb->color_rb[0])
266          attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
267       if (intel_fb->color_rb[1])
268          attachments[i++] = __DRI_BUFFER_BACK_LEFT;
269       if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH))
270          attachments[i++] = __DRI_BUFFER_DEPTH;
271       if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL))
272          attachments[i++] = __DRI_BUFFER_STENCIL;
273
274       buffers = (*screen->dri2.loader->getBuffers)(drawable,
275                                                    &drawable->w,
276                                                    &drawable->h,
277                                                    attachments, i,
278                                                    &count,
279                                                    drawable->loaderPrivate);
280    }
281
282    if (buffers == NULL)
283       return;
284
285    drawable->x = 0;
286    drawable->y = 0;
287    drawable->backX = 0;
288    drawable->backY = 0;
289    drawable->numClipRects = 1;
290    drawable->pClipRects[0].x1 = 0;
291    drawable->pClipRects[0].y1 = 0;
292    drawable->pClipRects[0].x2 = drawable->w;
293    drawable->pClipRects[0].y2 = drawable->h;
294    drawable->numBackClipRects = 1;
295    drawable->pBackClipRects[0].x1 = 0;
296    drawable->pBackClipRects[0].y1 = 0;
297    drawable->pBackClipRects[0].x2 = drawable->w;
298    drawable->pBackClipRects[0].y2 = drawable->h;
299
300    depth_region = NULL;
301    for (i = 0; i < count; i++) {
302        switch (buffers[i].attachment) {
303        case __DRI_BUFFER_FRONT_LEFT:
304            rb = intel_fb->color_rb[0];
305            region_name = "dri2 front buffer";
306            break;
307
308        case __DRI_BUFFER_FAKE_FRONT_LEFT:
309            rb = intel_fb->color_rb[0];
310            region_name = "dri2 fake front buffer";
311            break;
312
313        case __DRI_BUFFER_BACK_LEFT:
314            rb = intel_fb->color_rb[1];
315            region_name = "dri2 back buffer";
316            break;
317
318        case __DRI_BUFFER_DEPTH:
319            rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
320            region_name = "dri2 depth buffer";
321            break;
322
323        case __DRI_BUFFER_DEPTH_STENCIL:
324            rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
325            region_name = "dri2 depth / stencil buffer";
326            break;
327
328        case __DRI_BUFFER_STENCIL:
329            rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
330            region_name = "dri2 stencil buffer";
331            break;
332
333        case __DRI_BUFFER_ACCUM:
334        default:
335            fprintf(stderr,
336                    "unhandled buffer attach event, attacment type %d\n",
337                    buffers[i].attachment);
338            return;
339        }
340
341        if (rb == NULL)
342           continue;
343
344        if (rb->region) {
345           dri_bo_flink(rb->region->buffer, &name);
346           if (name == buffers[i].name)
347              continue;
348        }
349
350        if (INTEL_DEBUG & DEBUG_DRI)
351           fprintf(stderr,
352                   "attaching buffer %d, at %d, cpp %d, pitch %d\n",
353                   buffers[i].name, buffers[i].attachment,
354                   buffers[i].cpp, buffers[i].pitch);
355        
356        if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
357           if (INTEL_DEBUG & DEBUG_DRI)
358              fprintf(stderr, "(reusing depth buffer as stencil)\n");
359           intel_region_reference(&region, depth_region);
360        }
361        else
362           region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
363                                                  drawable->w,
364                                                  drawable->h,
365                                                  buffers[i].pitch / buffers[i].cpp,
366                                                  buffers[i].name,
367                                                  region_name);
368
369        if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
370           depth_region = region;
371
372        intel_renderbuffer_set_region(rb, region);
373        intel_region_release(&region);
374
375        if (buffers[i].attachment == __DRI_BUFFER_DEPTH_STENCIL) {
376           rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
377           if (rb != NULL) {
378              struct intel_region *stencil_region = NULL;
379
380              if (rb->region) {
381                 dri_bo_flink(rb->region->buffer, &name);
382                 if (name == buffers[i].name)
383                    continue;
384              }
385
386              intel_region_reference(&stencil_region, region);
387              intel_renderbuffer_set_region(rb, stencil_region);
388              intel_region_release(&stencil_region);
389           }
390        }
391    }
392
393    driUpdateFramebufferSize(&intel->ctx, drawable);
394 }
395
396 void
397 intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
398 {
399     struct intel_context *intel = intel_context(ctx);
400     __DRIcontext *driContext = intel->driContext;
401     void (*old_viewport)(GLcontext *ctx, GLint x, GLint y,
402                          GLsizei w, GLsizei h);
403
404     if (!driContext->driScreenPriv->dri2.enabled)
405         return;
406
407     if (!intel->meta.internal_viewport_call && ctx->DrawBuffer->Name == 0) {
408        /* If we're rendering to the fake front buffer, make sure all the pending
409         * drawing has landed on the real front buffer.  Otherwise when we
410         * eventually get to DRI2GetBuffersWithFormat the stale real front
411         * buffer contents will get copied to the new fake front buffer.
412         */
413        if (intel->is_front_buffer_rendering) {
414           intel_flush(ctx, GL_FALSE);
415        }
416
417        intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
418        if (driContext->driDrawablePriv != driContext->driReadablePriv)
419           intel_update_renderbuffers(driContext, driContext->driReadablePriv);
420     }
421
422     old_viewport = ctx->Driver.Viewport;
423     ctx->Driver.Viewport = NULL;
424     intel->driDrawable = driContext->driDrawablePriv;
425     intelWindowMoved(intel);
426     intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
427     ctx->Driver.Viewport = old_viewport;
428 }
429
430
431 static const struct dri_debug_control debug_control[] = {
432    { "tex",   DEBUG_TEXTURE},
433    { "state", DEBUG_STATE},
434    { "ioctl", DEBUG_IOCTL},
435    { "blit",  DEBUG_BLIT},
436    { "mip",   DEBUG_MIPTREE},
437    { "fall",  DEBUG_FALLBACKS},
438    { "verb",  DEBUG_VERBOSE},
439    { "bat",   DEBUG_BATCH},
440    { "pix",   DEBUG_PIXEL},
441    { "buf",   DEBUG_BUFMGR},
442    { "reg",   DEBUG_REGION},
443    { "fbo",   DEBUG_FBO},
444    { "lock",  DEBUG_LOCK},
445    { "sync",  DEBUG_SYNC},
446    { "prim",  DEBUG_PRIMS },
447    { "vert",  DEBUG_VERTS },
448    { "dri",   DEBUG_DRI },
449    { "dma",   DEBUG_DMA },
450    { "san",   DEBUG_SANITY },
451    { "sleep", DEBUG_SLEEP },
452    { "stats", DEBUG_STATS },
453    { "tile",  DEBUG_TILE },
454    { "sing",  DEBUG_SINGLE_THREAD },
455    { "thre",  DEBUG_SINGLE_THREAD },
456    { "wm",    DEBUG_WM },
457    { "urb",   DEBUG_URB },
458    { "vs",    DEBUG_VS },
459    { NULL,    0 }
460 };
461
462
463 static void
464 intelInvalidateState(GLcontext * ctx, GLuint new_state)
465 {
466     struct intel_context *intel = intel_context(ctx);
467
468    _swrast_InvalidateState(ctx, new_state);
469    _swsetup_InvalidateState(ctx, new_state);
470    _vbo_InvalidateState(ctx, new_state);
471    _tnl_InvalidateState(ctx, new_state);
472    _tnl_invalidate_vertex_state(ctx, new_state);
473
474    intel->NewGLState |= new_state;
475
476    if (intel->vtbl.invalidate_state)
477       intel->vtbl.invalidate_state( intel, new_state );
478 }
479
480 static void
481 intel_flush(GLcontext *ctx, GLboolean needs_mi_flush)
482 {
483    struct intel_context *intel = intel_context(ctx);
484
485    if (intel->Fallback)
486       _swrast_flush(ctx);
487
488    if (!IS_965(intel->intelScreen->deviceID))
489       INTEL_FIREVERTICES(intel);
490
491    /* Emit a flush so that any frontbuffer rendering that might have occurred
492     * lands onscreen in a timely manner, even if the X Server doesn't trigger
493     * a flush for us.
494     */
495    if (needs_mi_flush)
496       intel_batchbuffer_emit_mi_flush(intel->batch);
497
498    if (intel->batch->map != intel->batch->ptr)
499       intel_batchbuffer_flush(intel->batch);
500
501    if ((ctx->DrawBuffer->Name == 0) && intel->front_buffer_dirty) {
502       __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
503
504       if (screen->dri2.loader &&
505           (screen->dri2.loader->base.version >= 2)
506           && (screen->dri2.loader->flushFrontBuffer != NULL)) {
507          (*screen->dri2.loader->flushFrontBuffer)(intel->driDrawable,
508                                                   intel->driDrawable->loaderPrivate);
509
510          /* Only clear the dirty bit if front-buffer rendering is no longer
511           * enabled.  This is done so that the dirty bit can only be set in
512           * glDrawBuffer.  Otherwise the dirty bit would have to be set at
513           * each of N places that do rendering.  This has worse performances,
514           * but it is much easier to get correct.
515           */
516          if (intel->is_front_buffer_rendering) {
517             intel->front_buffer_dirty = GL_FALSE;
518          }
519       }
520    }
521 }
522
523 void
524 intelFlush(GLcontext * ctx)
525 {
526    intel_flush(ctx, GL_FALSE);
527 }
528
529 static void
530 intel_glFlush(GLcontext *ctx)
531 {
532    intel_flush(ctx, GL_TRUE);
533 }
534
535 void
536 intelFinish(GLcontext * ctx)
537 {
538    struct gl_framebuffer *fb = ctx->DrawBuffer;
539    int i;
540
541    intelFlush(ctx);
542
543    for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
544        struct intel_renderbuffer *irb;
545
546        irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
547
548        if (irb && irb->region)
549           dri_bo_wait_rendering(irb->region->buffer);
550    }
551    if (fb->_DepthBuffer) {
552       /* XXX: Wait on buffer idle */
553    }
554 }
555
556 void
557 intelInitDriverFunctions(struct dd_function_table *functions)
558 {
559    _mesa_init_driver_functions(functions);
560
561    functions->Flush = intel_glFlush;
562    functions->Finish = intelFinish;
563    functions->GetString = intelGetString;
564    functions->UpdateState = intelInvalidateState;
565
566    functions->CopyColorTable = _swrast_CopyColorTable;
567    functions->CopyColorSubTable = _swrast_CopyColorSubTable;
568    functions->CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
569    functions->CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
570
571    intelInitTextureFuncs(functions);
572    intelInitTextureImageFuncs(functions);
573    intelInitTextureSubImageFuncs(functions);
574    intelInitTextureCopyImageFuncs(functions);
575    intelInitStateFuncs(functions);
576    intelInitClearFuncs(functions);
577    intelInitBufferFuncs(functions);
578    intelInitPixelFuncs(functions);
579    intelInitBufferObjectFuncs(functions);
580 }
581
582
583 GLboolean
584 intelInitContext(struct intel_context *intel,
585                  const __GLcontextModes * mesaVis,
586                  __DRIcontextPrivate * driContextPriv,
587                  void *sharedContextPrivate,
588                  struct dd_function_table *functions)
589 {
590    GLcontext *ctx = &intel->ctx;
591    GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
592    __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
593    intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
594    int fthrottle_mode;
595
596    if (!_mesa_initialize_context(&intel->ctx, mesaVis, shareCtx,
597                                  functions, (void *) intel)) {
598       _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
599       return GL_FALSE;
600    }
601
602    driContextPriv->driverPrivate = intel;
603    intel->intelScreen = intelScreen;
604    intel->driScreen = sPriv;
605    intel->sarea = intelScreen->sarea;
606    intel->driContext = driContextPriv;
607
608    /* Dri stuff */
609    intel->hHWContext = driContextPriv->hHWContext;
610    intel->driFd = sPriv->fd;
611    intel->driHwLock = sPriv->lock;
612
613    driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
614                        intel->driScreen->myNum,
615                        IS_965(intelScreen->deviceID) ? "i965" : "i915");
616    if (intelScreen->deviceID == PCI_CHIP_I865_G)
617       intel->maxBatchSize = 4096;
618    else
619       intel->maxBatchSize = BATCH_SZ;
620
621    intel->bufmgr = intelScreen->bufmgr;
622    intel->ttm = intelScreen->ttm;
623    if (intel->ttm) {
624       int bo_reuse_mode;
625
626       bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
627       switch (bo_reuse_mode) {
628       case DRI_CONF_BO_REUSE_DISABLED:
629          break;
630       case DRI_CONF_BO_REUSE_ALL:
631          intel_bufmgr_gem_enable_reuse(intel->bufmgr);
632          break;
633       }
634    }
635
636    /* This doesn't yet catch all non-conformant rendering, but it's a
637     * start.
638     */
639    if (getenv("INTEL_STRICT_CONFORMANCE")) {
640       unsigned int value = atoi(getenv("INTEL_STRICT_CONFORMANCE"));
641       if (value > 0) {
642          intel->conformance_mode = value;
643       }
644       else {
645          intel->conformance_mode = 1;
646       }
647    }
648
649    if (intel->conformance_mode > 0) {
650       ctx->Const.MinLineWidth = 1.0;
651       ctx->Const.MinLineWidthAA = 1.0;
652       ctx->Const.MaxLineWidth = 1.0;
653       ctx->Const.MaxLineWidthAA = 1.0;
654       ctx->Const.LineWidthGranularity = 1.0;
655    }
656    else {
657       ctx->Const.MinLineWidth = 1.0;
658       ctx->Const.MinLineWidthAA = 1.0;
659       ctx->Const.MaxLineWidth = 5.0;
660       ctx->Const.MaxLineWidthAA = 5.0;
661       ctx->Const.LineWidthGranularity = 0.5;
662    }
663
664    ctx->Const.MinPointSize = 1.0;
665    ctx->Const.MinPointSizeAA = 1.0;
666    ctx->Const.MaxPointSize = 255.0;
667    ctx->Const.MaxPointSizeAA = 3.0;
668    ctx->Const.PointSizeGranularity = 1.0;
669
670    /* reinitialize the context point state.
671     * It depend on constants in __GLcontextRec::Const
672     */
673    _mesa_init_point(ctx);
674
675    meta_init_metaops(ctx, &intel->meta);
676    ctx->Const.MaxColorAttachments = 4;  /* XXX FBO: review this */
677    if (IS_965(intelScreen->deviceID)) {
678       if (MAX_WIDTH > 8192)
679          ctx->Const.MaxRenderbufferSize = 8192;
680    } else {
681       if (MAX_WIDTH > 2048)
682          ctx->Const.MaxRenderbufferSize = 2048;
683    }
684
685    /* Initialize the software rasterizer and helper modules. */
686    _swrast_CreateContext(ctx);
687    _vbo_CreateContext(ctx);
688    _tnl_CreateContext(ctx);
689    _swsetup_CreateContext(ctx);
690  
691    /* Configure swrast to match hardware characteristics: */
692    _swrast_allow_pixel_fog(ctx, GL_FALSE);
693    _swrast_allow_vertex_fog(ctx, GL_TRUE);
694
695    intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
696    intel->hw_stipple = 1;
697
698    /* XXX FBO: this doesn't seem to be used anywhere */
699    switch (mesaVis->depthBits) {
700    case 0:                     /* what to do in this case? */
701    case 16:
702       intel->polygon_offset_scale = 1.0;
703       break;
704    case 24:
705       intel->polygon_offset_scale = 2.0;     /* req'd to pass glean */
706       break;
707    default:
708       assert(0);
709       break;
710    }
711
712    if (IS_965(intelScreen->deviceID))
713       intel->polygon_offset_scale /= 0xffff;
714
715    intel->RenderIndex = ~0;
716
717    fthrottle_mode = driQueryOptioni(&intel->optionCache, "fthrottle_mode");
718    intel->irqsEmitted = 0;
719
720    intel->do_irqs = (intel->intelScreen->irq_active &&
721                      fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS);
722
723    intel->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
724
725    if (IS_965(intelScreen->deviceID) && !intel->intelScreen->irq_active) {
726       _mesa_printf("IRQs not active.  Exiting\n");
727       exit(1);
728    }
729
730    intelInitExtensions(ctx, GL_FALSE);
731
732    INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
733    if (INTEL_DEBUG & DEBUG_BUFMGR)
734       dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
735
736    if (!sPriv->dri2.enabled)
737       intel_recreate_static_regions(intel);
738
739    intel->batch = intel_batchbuffer_alloc(intel);
740
741    intel_fbo_init(intel);
742
743    if (intel->ctx.Mesa_DXTn) {
744       _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
745       _mesa_enable_extension(ctx, "GL_S3_s3tc");
746    }
747    else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
748       _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
749    }
750    intel->use_texture_tiling = driQueryOptionb(&intel->optionCache,
751                                                "texture_tiling");
752    if (intel->use_texture_tiling &&
753        !intel->intelScreen->kernel_exec_fencing) {
754       fprintf(stderr, "No kernel support for execution fencing, "
755               "disabling texture tiling");
756       intel->use_texture_tiling = GL_FALSE;
757    }
758    intel->use_early_z = driQueryOptionb(&intel->optionCache, "early_z");
759
760    intel->prim.primitive = ~0;
761
762    /* Force all software fallbacks */
763    if (driQueryOptionb(&intel->optionCache, "no_rast")) {
764       fprintf(stderr, "disabling 3D rasterization\n");
765       intel->no_rast = 1;
766    }
767
768    if (driQueryOptionb(&intel->optionCache, "always_flush_batch")) {
769       fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
770       intel->always_flush_batch = 1;
771    }
772
773    if (driQueryOptionb(&intel->optionCache, "always_flush_cache")) {
774       fprintf(stderr, "flushing GPU caches before/after each draw call\n");
775       intel->always_flush_cache = 1;
776    }
777
778    /* Disable all hardware rendering (skip emitting batches and fences/waits
779     * to the kernel)
780     */
781    intel->no_hw = getenv("INTEL_NO_HW") != NULL;
782
783    return GL_TRUE;
784 }
785
786 void
787 intelDestroyContext(__DRIcontextPrivate * driContextPriv)
788 {
789    struct intel_context *intel =
790       (struct intel_context *) driContextPriv->driverPrivate;
791
792    assert(intel);               /* should never be null */
793    if (intel) {
794       GLboolean release_texture_heaps;
795
796       INTEL_FIREVERTICES(intel);
797
798       meta_destroy_metaops(&intel->meta);
799
800       intel->vtbl.destroy(intel);
801
802       release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
803       _swsetup_DestroyContext(&intel->ctx);
804       _tnl_DestroyContext(&intel->ctx);
805       _vbo_DestroyContext(&intel->ctx);
806
807       _swrast_DestroyContext(&intel->ctx);
808       intel->Fallback = 0;      /* don't call _swrast_Flush later */
809
810       intel_batchbuffer_free(intel->batch);
811       intel->batch = NULL;
812
813       free(intel->prim.vb);
814       intel->prim.vb = NULL;
815       dri_bo_unreference(intel->prim.vb_bo);
816       intel->prim.vb_bo = NULL;
817
818       if (release_texture_heaps) {
819          /* Nothing is currently done here to free texture heaps;
820           * but we're not using the texture heap utilities, so I
821           * rather think we shouldn't.  I've taken a look, and can't
822           * find any private texture data hanging around anywhere, but
823           * I'm not yet certain there isn't any at all...
824           */
825          /* if (INTEL_DEBUG & DEBUG_TEXTURE)
826             fprintf(stderr, "do something to free texture heaps\n");
827           */
828       }
829
830       /* XXX In intelMakeCurrent() below, the context's static regions are 
831        * referenced inside the frame buffer; it's listed as a hack,
832        * with a comment of "XXX FBO temporary fix-ups!", but
833        * as long as it's there, we should release the regions here.
834        * The do/while loop around the block is used to allow the
835        * "continue" statements inside the block to exit the block,
836        * to avoid many layers of "if" constructs.
837        */
838       do {
839          __DRIdrawablePrivate * driDrawPriv = intel->driDrawable;
840          struct intel_framebuffer *intel_fb;
841          struct intel_renderbuffer *irbDepth, *irbStencil;
842          if (!driDrawPriv) {
843             /* We're already detached from the drawable; exit this block. */
844             continue;
845          }
846          intel_fb = (struct intel_framebuffer *) driDrawPriv->driverPrivate;
847          if (!intel_fb) {
848             /* The frame buffer is already gone; exit this block. */
849             continue;
850          }
851          irbDepth = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
852          irbStencil = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
853
854          /* If the regions of the frame buffer still match the regions
855           * of the context, release them.  If they've changed somehow,
856           * leave them alone.
857           */
858          if (intel_fb->color_rb[0] && intel_fb->color_rb[0]->region == intel->front_region) {
859             intel_renderbuffer_set_region(intel_fb->color_rb[0], NULL);
860          }
861          if (intel_fb->color_rb[1] && intel_fb->color_rb[1]->region == intel->back_region) {
862             intel_renderbuffer_set_region(intel_fb->color_rb[1], NULL);
863          }
864
865          if (irbDepth && irbDepth->region == intel->depth_region) {
866             intel_renderbuffer_set_region(irbDepth, NULL);
867          }
868          /* Usually, the stencil buffer is the same as the depth buffer;
869           * but they're handled separately in MakeCurrent, so we'll
870           * handle them separately here.
871           */
872          if (irbStencil && irbStencil->region == intel->depth_region) {
873             intel_renderbuffer_set_region(irbStencil, NULL);
874          }
875       } while (0);
876
877       intel_region_release(&intel->front_region);
878       intel_region_release(&intel->back_region);
879       intel_region_release(&intel->depth_region);
880
881       driDestroyOptionCache(&intel->optionCache);
882
883       /* free the Mesa context */
884       _mesa_free_context_data(&intel->ctx);
885
886       FREE(intel);
887       driContextPriv->driverPrivate = NULL;
888    }
889 }
890
891 GLboolean
892 intelUnbindContext(__DRIcontextPrivate * driContextPriv)
893 {
894    return GL_TRUE;
895 }
896
897 GLboolean
898 intelMakeCurrent(__DRIcontextPrivate * driContextPriv,
899                  __DRIdrawablePrivate * driDrawPriv,
900                  __DRIdrawablePrivate * driReadPriv)
901 {
902    __DRIscreenPrivate *psp = driDrawPriv->driScreenPriv;
903
904    if (driContextPriv) {
905       struct intel_context *intel =
906          (struct intel_context *) driContextPriv->driverPrivate;
907       struct intel_framebuffer *intel_fb =
908          (struct intel_framebuffer *) driDrawPriv->driverPrivate;
909       GLframebuffer *readFb = (GLframebuffer *) driReadPriv->driverPrivate;
910  
911       if (driContextPriv->driScreenPriv->dri2.enabled) {     
912           intel_update_renderbuffers(driContextPriv, driDrawPriv);
913           if (driDrawPriv != driReadPriv)
914               intel_update_renderbuffers(driContextPriv, driReadPriv);
915       } else {
916           /* XXX FBO temporary fix-ups!  These are released in 
917            * intelDextroyContext(), above.  Changes here should be
918            * reflected there.
919            */
920           /* if the renderbuffers don't have regions, init them from the context */
921          struct intel_renderbuffer *irbDepth
922             = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
923          struct intel_renderbuffer *irbStencil
924             = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
925
926          if (intel_fb->color_rb[0]) {
927             intel_renderbuffer_set_region(intel_fb->color_rb[0],
928                                           intel->front_region);
929          }
930          if (intel_fb->color_rb[1]) {
931             intel_renderbuffer_set_region(intel_fb->color_rb[1],
932                                           intel->back_region);
933          }
934
935          if (irbDepth) {
936             intel_renderbuffer_set_region(irbDepth, intel->depth_region);
937          }
938          if (irbStencil) {
939             intel_renderbuffer_set_region(irbStencil, intel->depth_region);
940          }
941       }
942
943       /* set GLframebuffer size to match window, if needed */
944       driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
945
946       if (driReadPriv != driDrawPriv) {
947          driUpdateFramebufferSize(&intel->ctx, driReadPriv);
948       }
949
950       _mesa_make_current(&intel->ctx, &intel_fb->Base, readFb);
951
952       /* The drawbuffer won't always be updated by _mesa_make_current: 
953        */
954       if (intel->ctx.DrawBuffer == &intel_fb->Base) {
955
956          if (intel->driReadDrawable != driReadPriv)
957             intel->driReadDrawable = driReadPriv;
958
959          if (intel->driDrawable != driDrawPriv) {
960             if (driDrawPriv->swap_interval == (unsigned)-1) {
961                int i;
962
963                driDrawPriv->vblFlags = (intel->intelScreen->irq_active != 0)
964                   ? driGetDefaultVBlankFlags(&intel->optionCache)
965                  : VBLANK_FLAG_NO_IRQ;
966
967                /* Prevent error printf if one crtc is disabled, this will
968                 * be properly calculated in intelWindowMoved() next.
969                 */
970                 driDrawPriv->vblFlags = intelFixupVblank(intel, driDrawPriv);
971
972                (*psp->systemTime->getUST) (&intel_fb->swap_ust);
973                driDrawableInitVBlank(driDrawPriv);
974                intel_fb->vbl_waited = driDrawPriv->vblSeq;
975
976                for (i = 0; i < 2; i++) {
977                   if (intel_fb->color_rb[i])
978                      intel_fb->color_rb[i]->vbl_pending = driDrawPriv->vblSeq;
979                }
980             }
981             intel->driDrawable = driDrawPriv;
982             intelWindowMoved(intel);
983          }
984
985          intel_draw_buffer(&intel->ctx, &intel_fb->Base);
986       }
987    }
988    else {
989       _mesa_make_current(NULL, NULL, NULL);
990    }
991
992    return GL_TRUE;
993 }
994
995 static void
996 intelContendedLock(struct intel_context *intel, GLuint flags)
997 {
998    __DRIdrawablePrivate *dPriv = intel->driDrawable;
999    __DRIscreenPrivate *sPriv = intel->driScreen;
1000    volatile drm_i915_sarea_t *sarea = intel->sarea;
1001    int me = intel->hHWContext;
1002
1003    drmGetLock(intel->driFd, intel->hHWContext, flags);
1004
1005    if (INTEL_DEBUG & DEBUG_LOCK)
1006       _mesa_printf("%s - got contended lock\n", __progname);
1007
1008    /* If the window moved, may need to set a new cliprect now.
1009     *
1010     * NOTE: This releases and regains the hw lock, so all state
1011     * checking must be done *after* this call:
1012     */
1013    if (dPriv)
1014        DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
1015
1016    if (sarea && sarea->ctxOwner != me) {
1017       if (INTEL_DEBUG & DEBUG_BUFMGR) {
1018          fprintf(stderr, "Lost Context: sarea->ctxOwner %x me %x\n",
1019                  sarea->ctxOwner, me);
1020       }
1021       sarea->ctxOwner = me;
1022    }
1023
1024    /* If the last consumer of the texture memory wasn't us, notify the fake
1025     * bufmgr and record the new owner.  We should have the memory shared
1026     * between contexts of a single fake bufmgr, but this will at least make
1027     * things correct for now.
1028     */
1029    if (!intel->ttm && sarea->texAge != intel->hHWContext) {
1030       sarea->texAge = intel->hHWContext;
1031       intel_bufmgr_fake_contended_lock_take(intel->bufmgr);
1032       if (INTEL_DEBUG & DEBUG_BATCH)
1033          intel_decode_context_reset();
1034       if (INTEL_DEBUG & DEBUG_BUFMGR)
1035          fprintf(stderr, "Lost Textures: sarea->texAge %x hw context %x\n",
1036                  sarea->ctxOwner, intel->hHWContext);
1037    }
1038
1039    /* Drawable changed?
1040     */
1041    if (dPriv && intel->lastStamp != dPriv->lastStamp) {
1042        intelWindowMoved(intel);
1043        intel->lastStamp = dPriv->lastStamp;
1044    }
1045 }
1046
1047
1048 _glthread_DECLARE_STATIC_MUTEX(lockMutex);
1049
1050 /* Lock the hardware and validate our state.  
1051  */
1052 void LOCK_HARDWARE( struct intel_context *intel )
1053 {
1054     __DRIdrawable *dPriv = intel->driDrawable;
1055     __DRIscreen *sPriv = intel->driScreen;
1056     char __ret = 0;
1057     struct intel_framebuffer *intel_fb = NULL;
1058     struct intel_renderbuffer *intel_rb = NULL;
1059
1060     intel->locked++;
1061     if (intel->locked >= 2)
1062        return;
1063
1064     if (!sPriv->dri2.enabled)
1065        _glthread_LOCK_MUTEX(lockMutex);
1066
1067     if (intel->driDrawable) {
1068        intel_fb = intel->driDrawable->driverPrivate;
1069
1070        if (intel_fb)
1071           intel_rb =
1072              intel_get_renderbuffer(&intel_fb->Base,
1073                                     intel_fb->Base._ColorDrawBufferIndexes[0]);
1074     }
1075
1076     if (intel_rb && dPriv->vblFlags &&
1077         !(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) &&
1078         (intel_fb->vbl_waited - intel_rb->vbl_pending) > (1<<23)) {
1079         drmVBlank vbl;
1080
1081         vbl.request.type = DRM_VBLANK_ABSOLUTE;
1082
1083         if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
1084             vbl.request.type |= DRM_VBLANK_SECONDARY;
1085         }
1086
1087         vbl.request.sequence = intel_rb->vbl_pending;
1088         drmWaitVBlank(intel->driFd, &vbl);
1089         intel_fb->vbl_waited = vbl.reply.sequence;
1090     }
1091
1092     if (!sPriv->dri2.enabled) {
1093         DRM_CAS(intel->driHwLock, intel->hHWContext,
1094                 (DRM_LOCK_HELD|intel->hHWContext), __ret);
1095
1096         if (__ret)
1097             intelContendedLock( intel, 0 );
1098     }
1099
1100
1101     if (INTEL_DEBUG & DEBUG_LOCK)
1102       _mesa_printf("%s - locked\n", __progname);
1103 }
1104
1105
1106 /* Unlock the hardware using the global current context 
1107  */
1108 void UNLOCK_HARDWARE( struct intel_context *intel )
1109 {
1110     __DRIscreen *sPriv = intel->driScreen;
1111
1112    intel->locked--;
1113    if (intel->locked > 0)
1114       return;
1115
1116    assert(intel->locked == 0);
1117
1118    if (!sPriv->dri2.enabled) {
1119       DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
1120       _glthread_UNLOCK_MUTEX(lockMutex);
1121    }
1122
1123    if (INTEL_DEBUG & DEBUG_LOCK)
1124       _mesa_printf("%s - unlocked\n", __progname);
1125
1126    /**
1127     * Nothing should be left in batch outside of LOCK/UNLOCK which references
1128     * cliprects.
1129     */
1130    if (intel->batch->cliprect_mode == REFERENCES_CLIPRECTS)
1131       intel_batchbuffer_flush(intel->batch);
1132 }
1133