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