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