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