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