intel_context.c revision 96ee3db6f76f25f8dcf36b365f6be93341d90472
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/matrix.h"
32#include "main/simple_list.h"
33#include "main/extensions.h"
34#include "main/framebuffer.h"
35#include "main/imports.h"
36#include "main/points.h"
37
38#include "swrast/swrast.h"
39#include "swrast_setup/swrast_setup.h"
40#include "tnl/tnl.h"
41
42#include "tnl/t_pipeline.h"
43#include "tnl/t_vertex.h"
44
45#include "drivers/common/driverfuncs.h"
46
47#include "intel_screen.h"
48
49#include "i830_dri.h"
50
51#include "intel_chipset.h"
52#include "intel_buffers.h"
53#include "intel_tex.h"
54#include "intel_batchbuffer.h"
55#include "intel_blit.h"
56#include "intel_pixel.h"
57#include "intel_regions.h"
58#include "intel_buffer_objects.h"
59#include "intel_fbo.h"
60#include "intel_decode.h"
61#include "intel_bufmgr.h"
62
63#include "drirenderbuffer.h"
64#include "vblank.h"
65#include "utils.h"
66#include "xmlpool.h"            /* for symbolic values of enum-type options */
67#ifndef INTEL_DEBUG
68int INTEL_DEBUG = (0);
69#endif
70
71#define need_GL_ARB_framebuffer_object
72#define need_GL_ARB_multisample
73#define need_GL_ARB_occlusion_query
74#define need_GL_ARB_point_parameters
75#define need_GL_ARB_shader_objects
76#define need_GL_ARB_texture_compression
77#define need_GL_ARB_vertex_buffer_object
78#define need_GL_ARB_vertex_program
79#define need_GL_ARB_vertex_shader
80#define need_GL_ARB_window_pos
81#define need_GL_EXT_blend_color
82#define need_GL_EXT_blend_equation_separate
83#define need_GL_EXT_blend_func_separate
84#define need_GL_EXT_blend_minmax
85#define need_GL_EXT_cull_vertex
86#define need_GL_EXT_fog_coord
87#define need_GL_EXT_framebuffer_object
88#define need_GL_EXT_framebuffer_blit
89#define need_GL_EXT_multi_draw_arrays
90#define need_GL_EXT_point_parameters
91#define need_GL_EXT_secondary_color
92#define need_GL_EXT_stencil_two_side
93#define need_GL_ATI_separate_stencil
94#define need_GL_NV_point_sprite
95#define need_GL_NV_vertex_program
96#define need_GL_VERSION_2_0
97#define need_GL_VERSION_2_1
98
99#include "extension_helper.h"
100
101#define DRIVER_DATE                     "20090114"
102#define DRIVER_DATE_GEM                 "GEM " DRIVER_DATE
103
104static const GLubyte *
105intelGetString(GLcontext * ctx, GLenum name)
106{
107   const struct intel_context *const intel = intel_context(ctx);
108   const char *chipset;
109   static char buffer[128];
110
111   switch (name) {
112   case GL_VENDOR:
113      return (GLubyte *) "Tungsten Graphics, Inc";
114      break;
115
116   case GL_RENDERER:
117      switch (intel->intelScreen->deviceID) {
118      case PCI_CHIP_845_G:
119         chipset = "Intel(R) 845G";
120         break;
121      case PCI_CHIP_I830_M:
122         chipset = "Intel(R) 830M";
123         break;
124      case PCI_CHIP_I855_GM:
125         chipset = "Intel(R) 852GM/855GM";
126         break;
127      case PCI_CHIP_I865_G:
128         chipset = "Intel(R) 865G";
129         break;
130      case PCI_CHIP_I915_G:
131         chipset = "Intel(R) 915G";
132         break;
133      case PCI_CHIP_E7221_G:
134	 chipset = "Intel (R) E7221G (i915)";
135	 break;
136      case PCI_CHIP_I915_GM:
137         chipset = "Intel(R) 915GM";
138         break;
139      case PCI_CHIP_I945_G:
140         chipset = "Intel(R) 945G";
141         break;
142      case PCI_CHIP_I945_GM:
143         chipset = "Intel(R) 945GM";
144         break;
145      case PCI_CHIP_I945_GME:
146         chipset = "Intel(R) 945GME";
147         break;
148      case PCI_CHIP_G33_G:
149	 chipset = "Intel(R) G33";
150	 break;
151      case PCI_CHIP_Q35_G:
152	 chipset = "Intel(R) Q35";
153	 break;
154      case PCI_CHIP_Q33_G:
155	 chipset = "Intel(R) Q33";
156	 break;
157      case PCI_CHIP_I965_Q:
158	 chipset = "Intel(R) 965Q";
159	 break;
160      case PCI_CHIP_I965_G:
161      case PCI_CHIP_I965_G_1:
162	 chipset = "Intel(R) 965G";
163	 break;
164      case PCI_CHIP_I946_GZ:
165	 chipset = "Intel(R) 946GZ";
166	 break;
167      case PCI_CHIP_I965_GM:
168	 chipset = "Intel(R) 965GM";
169	 break;
170      case PCI_CHIP_I965_GME:
171	 chipset = "Intel(R) 965GME/GLE";
172	 break;
173      case PCI_CHIP_GM45_GM:
174	 chipset = "Mobile Intel® GM45 Express Chipset";
175	 break;
176      case PCI_CHIP_IGD_E_G:
177	 chipset = "Intel(R) Integrated Graphics Device";
178	 break;
179      case PCI_CHIP_G45_G:
180         chipset = "Intel(R) G45/G43";
181         break;
182      case PCI_CHIP_Q45_G:
183         chipset = "Intel(R) Q45/Q43";
184         break;
185      case PCI_CHIP_G41_G:
186         chipset = "Intel(R) G41";
187         break;
188      default:
189         chipset = "Unknown Intel Chipset";
190         break;
191      }
192
193      (void) driGetRendererString(buffer, chipset,
194				  (intel->ttm) ? DRIVER_DATE_GEM : DRIVER_DATE,
195				  0);
196      return (GLubyte *) buffer;
197
198   default:
199      return NULL;
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;
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   i = 0;
223   if (intel_fb->color_rb[0])
224      attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
225   if (intel_fb->color_rb[1])
226      attachments[i++] = __DRI_BUFFER_BACK_LEFT;
227   if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH))
228      attachments[i++] = __DRI_BUFFER_DEPTH;
229   if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL))
230      attachments[i++] = __DRI_BUFFER_STENCIL;
231
232   buffers = (*screen->dri2.loader->getBuffers)(drawable,
233						&drawable->w,
234						&drawable->h,
235						attachments, i,
236						&count,
237						drawable->loaderPrivate);
238
239   if (buffers == NULL)
240      return;
241
242   drawable->x = 0;
243   drawable->y = 0;
244   drawable->backX = 0;
245   drawable->backY = 0;
246   drawable->numClipRects = 1;
247   drawable->pClipRects[0].x1 = 0;
248   drawable->pClipRects[0].y1 = 0;
249   drawable->pClipRects[0].x2 = drawable->w;
250   drawable->pClipRects[0].y2 = drawable->h;
251   drawable->numBackClipRects = 1;
252   drawable->pBackClipRects[0].x1 = 0;
253   drawable->pBackClipRects[0].y1 = 0;
254   drawable->pBackClipRects[0].x2 = drawable->w;
255   drawable->pBackClipRects[0].y2 = drawable->h;
256
257   depth_region = NULL;
258   for (i = 0; i < count; i++) {
259       switch (buffers[i].attachment) {
260       case __DRI_BUFFER_FRONT_LEFT:
261	   rb = intel_fb->color_rb[0];
262	   region_name = "dri2 front buffer";
263	   break;
264
265       case __DRI_BUFFER_BACK_LEFT:
266	   rb = intel_fb->color_rb[1];
267	   region_name = "dri2 back buffer";
268	   break;
269
270       case __DRI_BUFFER_DEPTH:
271	   rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
272	   region_name = "dri2 depth buffer";
273	   break;
274
275       case __DRI_BUFFER_STENCIL:
276	   rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
277	   region_name = "dri2 stencil buffer";
278	   break;
279
280       case __DRI_BUFFER_ACCUM:
281       default:
282	   fprintf(stderr,
283		   "unhandled buffer attach event, attacment type %d\n",
284		   buffers[i].attachment);
285	   return;
286       }
287
288       if (rb->region) {
289	  dri_bo_flink(rb->region->buffer, &name);
290	  if (name == buffers[i].name)
291	     continue;
292       }
293
294       if (INTEL_DEBUG & DEBUG_DRI)
295	  fprintf(stderr,
296		  "attaching buffer %d, at %d, cpp %d, pitch %d\n",
297		  buffers[i].name, buffers[i].attachment,
298		  buffers[i].cpp, buffers[i].pitch);
299
300       if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
301	  if (INTEL_DEBUG & DEBUG_DRI)
302	     fprintf(stderr, "(reusing depth buffer as stencil)\n");
303	  intel_region_reference(&region, depth_region);
304       }
305       else
306          region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
307						 drawable->w,
308						 drawable->h,
309						 buffers[i].pitch / buffers[i].cpp,
310						 buffers[i].name,
311						 region_name);
312
313       if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
314	  depth_region = region;
315
316       intel_renderbuffer_set_region(rb, region);
317       intel_region_release(&region);
318   }
319
320   driUpdateFramebufferSize(&intel->ctx, drawable);
321}
322
323void
324intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
325{
326    struct intel_context *intel = intel_context(ctx);
327    __DRIcontext *driContext = intel->driContext;
328    void (*old_viewport)(GLcontext *ctx, GLint x, GLint y,
329			 GLsizei w, GLsizei h);
330
331    if (!driContext->driScreenPriv->dri2.enabled)
332	return;
333
334    intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
335    if (driContext->driDrawablePriv != driContext->driReadablePriv)
336	intel_update_renderbuffers(driContext, driContext->driReadablePriv);
337
338    old_viewport = ctx->Driver.Viewport;
339    ctx->Driver.Viewport = NULL;
340    intel->driDrawable = driContext->driDrawablePriv;
341    intelWindowMoved(intel);
342    intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
343    ctx->Driver.Viewport = old_viewport;
344}
345
346/**
347 * Extension strings exported by the intel driver.
348 *
349 * Extensions supported by all chips supported by i830_dri, i915_dri, or
350 * i965_dri.
351 */
352static const struct dri_extension card_extensions[] = {
353   { "GL_ARB_multisample",                GL_ARB_multisample_functions },
354   { "GL_ARB_multitexture",               NULL },
355   { "GL_ARB_point_parameters",           GL_ARB_point_parameters_functions },
356   { "GL_ARB_texture_border_clamp",       NULL },
357   { "GL_ARB_texture_compression",        GL_ARB_texture_compression_functions },
358   { "GL_ARB_texture_cube_map",           NULL },
359   { "GL_ARB_texture_env_add",            NULL },
360   { "GL_ARB_texture_env_combine",        NULL },
361   { "GL_ARB_texture_env_crossbar",       NULL },
362   { "GL_ARB_texture_env_dot3",           NULL },
363   { "GL_ARB_texture_mirrored_repeat",    NULL },
364   { "GL_ARB_texture_rectangle",          NULL },
365   { "GL_ARB_vertex_buffer_object",       GL_ARB_vertex_buffer_object_functions },
366   { "GL_ARB_vertex_program",             GL_ARB_vertex_program_functions },
367   { "GL_ARB_window_pos",                 GL_ARB_window_pos_functions },
368   { "GL_EXT_blend_color",                GL_EXT_blend_color_functions },
369   { "GL_EXT_blend_equation_separate",    GL_EXT_blend_equation_separate_functions },
370   { "GL_EXT_blend_func_separate",        GL_EXT_blend_func_separate_functions },
371   { "GL_EXT_blend_minmax",               GL_EXT_blend_minmax_functions },
372   { "GL_EXT_blend_logic_op",             NULL },
373   { "GL_EXT_blend_subtract",             NULL },
374   { "GL_EXT_cull_vertex",                GL_EXT_cull_vertex_functions },
375   { "GL_EXT_fog_coord",                  GL_EXT_fog_coord_functions },
376   { "GL_EXT_multi_draw_arrays",          GL_EXT_multi_draw_arrays_functions },
377   { "GL_EXT_packed_depth_stencil",       NULL },
378   { "GL_EXT_secondary_color",            GL_EXT_secondary_color_functions },
379   { "GL_EXT_stencil_wrap",               NULL },
380   { "GL_EXT_texture_edge_clamp",         NULL },
381   { "GL_EXT_texture_env_combine",        NULL },
382   { "GL_EXT_texture_env_dot3",           NULL },
383   { "GL_EXT_texture_filter_anisotropic", NULL },
384   { "GL_EXT_texture_lod_bias",           NULL },
385   { "GL_3DFX_texture_compression_FXT1",  NULL },
386   { "GL_APPLE_client_storage",           NULL },
387   { "GL_MESA_pack_invert",               NULL },
388   { "GL_MESA_ycbcr_texture",             NULL },
389   { "GL_NV_blend_square",                NULL },
390   { "GL_NV_point_sprite",                GL_NV_point_sprite_functions },
391   { "GL_NV_texture_env_combine4",        NULL },
392   { "GL_NV_vertex_program",              GL_NV_vertex_program_functions },
393   { "GL_NV_vertex_program1_1",           NULL },
394   { "GL_SGIS_generate_mipmap",           NULL },
395   { NULL, NULL }
396};
397
398static const struct dri_extension brw_extensions[] = {
399   { "GL_ARB_depth_texture",              NULL },
400   { "GL_ARB_draw_buffers",               NULL },
401   { "GL_ARB_fragment_program",           NULL },
402   { "GL_ARB_fragment_program_shadow",    NULL },
403   { "GL_ARB_fragment_shader",            NULL },
404   { "GL_ARB_framebuffer_object",         GL_ARB_framebuffer_object_functions},
405   { "GL_ARB_occlusion_query",            GL_ARB_occlusion_query_functions },
406   { "GL_ARB_point_sprite", 		  NULL },
407   { "GL_ARB_shader_objects",             GL_ARB_shader_objects_functions },
408   { "GL_ARB_shading_language_100",       GL_VERSION_2_0_functions },
409#if 0
410   /* Support for GLSL 1.20 is currently broken in core Mesa.
411    */
412   { "GL_ARB_shading_language_120",       GL_VERSION_2_1_functions },
413#endif
414   { "GL_ARB_shadow",                     NULL },
415   { "GL_ARB_texture_non_power_of_two",   NULL },
416   { "GL_ARB_vertex_shader",              GL_ARB_vertex_shader_functions },
417   { "GL_EXT_shadow_funcs",               NULL },
418   { "GL_EXT_stencil_two_side",           GL_EXT_stencil_two_side_functions },
419   { "GL_EXT_texture_sRGB",		  NULL },
420   { "GL_EXT_vertex_array_bgra",	  NULL },
421   { "GL_ATI_separate_stencil",           GL_ATI_separate_stencil_functions },
422   { "GL_ATI_texture_env_combine3",       NULL },
423   { NULL,                                NULL }
424};
425
426static const struct dri_extension arb_oq_extensions[] = {
427   { NULL, NULL }
428};
429
430static const struct dri_extension ttm_extensions[] = {
431   { "GL_ARB_pixel_buffer_object",      NULL },
432   { "GL_EXT_framebuffer_blit",         GL_EXT_framebuffer_blit_functions },
433   { "GL_EXT_framebuffer_object",       GL_EXT_framebuffer_object_functions },
434   { NULL, NULL }
435};
436
437/**
438 * Initializes potential list of extensions if ctx == NULL, or actually enables
439 * extensions for a context.
440 */
441void intelInitExtensions(GLcontext *ctx, GLboolean enable_imaging)
442{
443   struct intel_context *intel = ctx?intel_context(ctx):NULL;
444
445   /* Disable imaging extension until convolution is working in teximage paths.
446    */
447   enable_imaging = GL_FALSE;
448
449   driInitExtensions(ctx, card_extensions, enable_imaging);
450
451   if (intel == NULL || intel->ttm)
452      driInitExtensions(ctx, ttm_extensions, GL_FALSE);
453
454   if (intel == NULL || IS_965(intel->intelScreen->deviceID))
455      driInitExtensions(ctx, brw_extensions, GL_FALSE);
456}
457
458static const struct dri_debug_control debug_control[] = {
459   { "tex",   DEBUG_TEXTURE},
460   { "state", DEBUG_STATE},
461   { "ioctl", DEBUG_IOCTL},
462   { "blit",  DEBUG_BLIT},
463   { "mip",   DEBUG_MIPTREE},
464   { "fall",  DEBUG_FALLBACKS},
465   { "verb",  DEBUG_VERBOSE},
466   { "bat",   DEBUG_BATCH},
467   { "pix",   DEBUG_PIXEL},
468   { "buf",   DEBUG_BUFMGR},
469   { "reg",   DEBUG_REGION},
470   { "fbo",   DEBUG_FBO},
471   { "lock",  DEBUG_LOCK},
472   { "sync",  DEBUG_SYNC},
473   { "prim",  DEBUG_PRIMS },
474   { "vert",  DEBUG_VERTS },
475   { "dri",   DEBUG_DRI },
476   { "dma",   DEBUG_DMA },
477   { "san",   DEBUG_SANITY },
478   { "sleep", DEBUG_SLEEP },
479   { "stats", DEBUG_STATS },
480   { "tile",  DEBUG_TILE },
481   { "sing",  DEBUG_SINGLE_THREAD },
482   { "thre",  DEBUG_SINGLE_THREAD },
483   { "wm",    DEBUG_WM },
484   { "urb",   DEBUG_URB },
485   { "vs",    DEBUG_VS },
486   { NULL,    0 }
487};
488
489
490static void
491intelInvalidateState(GLcontext * ctx, GLuint new_state)
492{
493    struct intel_context *intel = intel_context(ctx);
494
495   _swrast_InvalidateState(ctx, new_state);
496   _swsetup_InvalidateState(ctx, new_state);
497   _vbo_InvalidateState(ctx, new_state);
498   _tnl_InvalidateState(ctx, new_state);
499   _tnl_invalidate_vertex_state(ctx, new_state);
500
501   intel->NewGLState |= new_state;
502
503   if (intel->vtbl.invalidate_state)
504      intel->vtbl.invalidate_state( intel, new_state );
505}
506
507
508void
509intelFlush(GLcontext * ctx)
510{
511   struct intel_context *intel = intel_context(ctx);
512
513   if (intel->Fallback)
514      _swrast_flush(ctx);
515
516   if (!IS_965(intel->intelScreen->deviceID))
517      INTEL_FIREVERTICES(intel);
518
519   /* Emit a flush so that any frontbuffer rendering that might have occurred
520    * lands onscreen in a timely manner, even if the X Server doesn't trigger
521    * a flush for us.
522    */
523   intel_batchbuffer_emit_mi_flush(intel->batch);
524
525   if (intel->batch->map != intel->batch->ptr)
526      intel_batchbuffer_flush(intel->batch);
527}
528
529void
530intelFinish(GLcontext * ctx)
531{
532   struct gl_framebuffer *fb = ctx->DrawBuffer;
533   int i;
534
535   intelFlush(ctx);
536
537   for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
538       struct intel_renderbuffer *irb;
539
540       irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
541
542       if (irb->region)
543	  dri_bo_wait_rendering(irb->region->buffer);
544   }
545   if (fb->_DepthBuffer) {
546      /* XXX: Wait on buffer idle */
547   }
548}
549
550void
551intelInitDriverFunctions(struct dd_function_table *functions)
552{
553   _mesa_init_driver_functions(functions);
554
555   functions->Flush = intelFlush;
556   functions->Finish = intelFinish;
557   functions->GetString = intelGetString;
558   functions->UpdateState = intelInvalidateState;
559
560   functions->CopyColorTable = _swrast_CopyColorTable;
561   functions->CopyColorSubTable = _swrast_CopyColorSubTable;
562   functions->CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
563   functions->CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
564
565   intelInitTextureFuncs(functions);
566   intelInitStateFuncs(functions);
567   intelInitBufferFuncs(functions);
568   intelInitPixelFuncs(functions);
569}
570
571
572GLboolean
573intelInitContext(struct intel_context *intel,
574                 const __GLcontextModes * mesaVis,
575                 __DRIcontextPrivate * driContextPriv,
576                 void *sharedContextPrivate,
577                 struct dd_function_table *functions)
578{
579   GLcontext *ctx = &intel->ctx;
580   GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
581   __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
582   intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
583   int fthrottle_mode;
584
585   if (!_mesa_initialize_context(&intel->ctx, mesaVis, shareCtx,
586                                 functions, (void *) intel)) {
587      _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
588      return GL_FALSE;
589   }
590
591   driContextPriv->driverPrivate = intel;
592   intel->intelScreen = intelScreen;
593   intel->driScreen = sPriv;
594   intel->sarea = intelScreen->sarea;
595   intel->driContext = driContextPriv;
596
597   /* Dri stuff */
598   intel->hHWContext = driContextPriv->hHWContext;
599   intel->driFd = sPriv->fd;
600   intel->driHwLock = sPriv->lock;
601
602   driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
603                       intel->driScreen->myNum,
604		       IS_965(intelScreen->deviceID) ? "i965" : "i915");
605   if (intelScreen->deviceID == PCI_CHIP_I865_G)
606      intel->maxBatchSize = 4096;
607   else
608      intel->maxBatchSize = BATCH_SZ;
609
610   intel->bufmgr = intelScreen->bufmgr;
611   intel->ttm = intelScreen->ttm;
612   if (intel->ttm) {
613      int bo_reuse_mode;
614
615      bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
616      switch (bo_reuse_mode) {
617      case DRI_CONF_BO_REUSE_DISABLED:
618	 break;
619      case DRI_CONF_BO_REUSE_ALL:
620	 intel_bufmgr_gem_enable_reuse(intel->bufmgr);
621	 break;
622      }
623   }
624
625   ctx->Const.MaxTextureMaxAnisotropy = 2.0;
626
627   /* This doesn't yet catch all non-conformant rendering, but it's a
628    * start.
629    */
630   if (getenv("INTEL_STRICT_CONFORMANCE")) {
631      intel->strict_conformance = 1;
632   }
633
634   if (intel->strict_conformance) {
635      ctx->Const.MinLineWidth = 1.0;
636      ctx->Const.MinLineWidthAA = 1.0;
637      ctx->Const.MaxLineWidth = 1.0;
638      ctx->Const.MaxLineWidthAA = 1.0;
639      ctx->Const.LineWidthGranularity = 1.0;
640   }
641   else {
642      ctx->Const.MinLineWidth = 1.0;
643      ctx->Const.MinLineWidthAA = 1.0;
644      ctx->Const.MaxLineWidth = 5.0;
645      ctx->Const.MaxLineWidthAA = 5.0;
646      ctx->Const.LineWidthGranularity = 0.5;
647   }
648
649   ctx->Const.MinPointSize = 1.0;
650   ctx->Const.MinPointSizeAA = 1.0;
651   ctx->Const.MaxPointSize = 255.0;
652   ctx->Const.MaxPointSizeAA = 3.0;
653   ctx->Const.PointSizeGranularity = 1.0;
654
655   /* reinitialize the context point state.
656    * It depend on constants in __GLcontextRec::Const
657    */
658   _mesa_init_point(ctx);
659
660   ctx->Const.MaxColorAttachments = 4;  /* XXX FBO: review this */
661
662   /* Initialize the software rasterizer and helper modules. */
663   _swrast_CreateContext(ctx);
664   _vbo_CreateContext(ctx);
665   _tnl_CreateContext(ctx);
666   _swsetup_CreateContext(ctx);
667
668   /* Configure swrast to match hardware characteristics: */
669   _swrast_allow_pixel_fog(ctx, GL_FALSE);
670   _swrast_allow_vertex_fog(ctx, GL_TRUE);
671
672   intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
673   intel->hw_stipple = 1;
674
675   /* XXX FBO: this doesn't seem to be used anywhere */
676   switch (mesaVis->depthBits) {
677   case 0:                     /* what to do in this case? */
678   case 16:
679      intel->polygon_offset_scale = 1.0;
680      break;
681   case 24:
682      intel->polygon_offset_scale = 2.0;     /* req'd to pass glean */
683      break;
684   default:
685      assert(0);
686      break;
687   }
688
689   if (IS_965(intelScreen->deviceID))
690      intel->polygon_offset_scale /= 0xffff;
691
692   intel->RenderIndex = ~0;
693
694   fthrottle_mode = driQueryOptioni(&intel->optionCache, "fthrottle_mode");
695   intel->irqsEmitted = 0;
696
697   intel->do_irqs = (intel->intelScreen->irq_active &&
698                     fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS);
699
700   intel->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
701
702   _math_matrix_ctr(&intel->ViewportMatrix);
703
704   if (IS_965(intelScreen->deviceID) && !intel->intelScreen->irq_active) {
705      _mesa_printf("IRQs not active.  Exiting\n");
706      exit(1);
707   }
708
709   intelInitExtensions(ctx, GL_FALSE);
710
711   INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
712   if (INTEL_DEBUG & DEBUG_BUFMGR)
713      dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
714
715   if (!sPriv->dri2.enabled)
716      intel_recreate_static_regions(intel);
717
718   intel->batch = intel_batchbuffer_alloc(intel);
719
720   intel_bufferobj_init(intel);
721   intel_fbo_init(intel);
722
723   if (intel->ctx.Mesa_DXTn) {
724      _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
725      _mesa_enable_extension(ctx, "GL_S3_s3tc");
726   }
727   else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
728      _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
729   }
730
731   intel->prim.primitive = ~0;
732
733   /* Force all software fallbacks */
734   if (driQueryOptionb(&intel->optionCache, "no_rast")) {
735      fprintf(stderr, "disabling 3D rasterization\n");
736      intel->no_rast = 1;
737   }
738
739   /* Disable all hardware rendering (skip emitting batches and fences/waits
740    * to the kernel)
741    */
742   intel->no_hw = getenv("INTEL_NO_HW") != NULL;
743
744   return GL_TRUE;
745}
746
747void
748intelDestroyContext(__DRIcontextPrivate * driContextPriv)
749{
750   struct intel_context *intel =
751      (struct intel_context *) driContextPriv->driverPrivate;
752
753   assert(intel);               /* should never be null */
754   if (intel) {
755      GLboolean release_texture_heaps;
756
757      INTEL_FIREVERTICES(intel);
758
759      intel->vtbl.destroy(intel);
760
761      release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
762      _swsetup_DestroyContext(&intel->ctx);
763      _tnl_DestroyContext(&intel->ctx);
764      _vbo_DestroyContext(&intel->ctx);
765
766      _swrast_DestroyContext(&intel->ctx);
767      intel->Fallback = 0;      /* don't call _swrast_Flush later */
768
769      intel_batchbuffer_free(intel->batch);
770      intel->batch = NULL;
771
772      free(intel->prim.vb);
773      intel->prim.vb = NULL;
774      dri_bo_unreference(intel->prim.vb_bo);
775      intel->prim.vb_bo = NULL;
776
777      if (release_texture_heaps) {
778         /* This share group is about to go away, free our private
779          * texture object data.
780          */
781         if (INTEL_DEBUG & DEBUG_TEXTURE)
782            fprintf(stderr, "do something to free texture heaps\n");
783      }
784
785      intel_region_release(&intel->front_region);
786      intel_region_release(&intel->back_region);
787      intel_region_release(&intel->depth_region);
788
789      driDestroyOptionCache(&intel->optionCache);
790
791      /* free the Mesa context */
792      _mesa_free_context_data(&intel->ctx);
793   }
794}
795
796GLboolean
797intelUnbindContext(__DRIcontextPrivate * driContextPriv)
798{
799   return GL_TRUE;
800}
801
802GLboolean
803intelMakeCurrent(__DRIcontextPrivate * driContextPriv,
804                 __DRIdrawablePrivate * driDrawPriv,
805                 __DRIdrawablePrivate * driReadPriv)
806{
807   __DRIscreenPrivate *psp = driDrawPriv->driScreenPriv;
808
809   if (driContextPriv) {
810      struct intel_context *intel =
811         (struct intel_context *) driContextPriv->driverPrivate;
812      struct intel_framebuffer *intel_fb =
813	 (struct intel_framebuffer *) driDrawPriv->driverPrivate;
814      GLframebuffer *readFb = (GLframebuffer *) driReadPriv->driverPrivate;
815
816      if (driContextPriv->driScreenPriv->dri2.enabled) {
817          intel_update_renderbuffers(driContextPriv, driDrawPriv);
818          if (driDrawPriv != driReadPriv)
819              intel_update_renderbuffers(driContextPriv, driReadPriv);
820      } else {
821          /* XXX FBO temporary fix-ups! */
822          /* if the renderbuffers don't have regions, init them from the context */
823         struct intel_renderbuffer *irbDepth
824            = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
825         struct intel_renderbuffer *irbStencil
826            = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
827
828         if (intel_fb->color_rb[0]) {
829	    intel_renderbuffer_set_region(intel_fb->color_rb[0],
830					  intel->front_region);
831         }
832         if (intel_fb->color_rb[1]) {
833	    intel_renderbuffer_set_region(intel_fb->color_rb[1],
834					  intel->back_region);
835         }
836
837         if (irbDepth) {
838	    intel_renderbuffer_set_region(irbDepth, intel->depth_region);
839         }
840         if (irbStencil) {
841	    intel_renderbuffer_set_region(irbStencil, intel->depth_region);
842         }
843      }
844
845      /* set GLframebuffer size to match window, if needed */
846      driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
847
848      if (driReadPriv != driDrawPriv) {
849	 driUpdateFramebufferSize(&intel->ctx, driReadPriv);
850      }
851
852      _mesa_make_current(&intel->ctx, &intel_fb->Base, readFb);
853
854      /* The drawbuffer won't always be updated by _mesa_make_current:
855       */
856      if (intel->ctx.DrawBuffer == &intel_fb->Base) {
857
858	 if (intel->driReadDrawable != driReadPriv)
859	    intel->driReadDrawable = driReadPriv;
860
861	 if (intel->driDrawable != driDrawPriv) {
862	    if (driDrawPriv->swap_interval == (unsigned)-1) {
863	       int i;
864
865	       driDrawPriv->vblFlags = (intel->intelScreen->irq_active != 0)
866		  ? driGetDefaultVBlankFlags(&intel->optionCache)
867		 : VBLANK_FLAG_NO_IRQ;
868
869	       (*psp->systemTime->getUST) (&intel_fb->swap_ust);
870	       driDrawableInitVBlank(driDrawPriv);
871	       intel_fb->vbl_waited = driDrawPriv->vblSeq;
872
873	       for (i = 0; i < 2; i++) {
874		  if (intel_fb->color_rb[i])
875		     intel_fb->color_rb[i]->vbl_pending = driDrawPriv->vblSeq;
876	       }
877	    }
878	    intel->driDrawable = driDrawPriv;
879	    intelWindowMoved(intel);
880	 }
881
882	 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
883      }
884   }
885   else {
886      _mesa_make_current(NULL, NULL, NULL);
887   }
888
889   return GL_TRUE;
890}
891
892static void
893intelContendedLock(struct intel_context *intel, GLuint flags)
894{
895   __DRIdrawablePrivate *dPriv = intel->driDrawable;
896   __DRIscreenPrivate *sPriv = intel->driScreen;
897   volatile drm_i915_sarea_t *sarea = intel->sarea;
898   int me = intel->hHWContext;
899
900   drmGetLock(intel->driFd, intel->hHWContext, flags);
901   intel->locked = 1;
902
903   if (INTEL_DEBUG & DEBUG_LOCK)
904      _mesa_printf("%s - got contended lock\n", __progname);
905
906   /* If the window moved, may need to set a new cliprect now.
907    *
908    * NOTE: This releases and regains the hw lock, so all state
909    * checking must be done *after* this call:
910    */
911   if (dPriv)
912       DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
913
914   if (sarea && sarea->ctxOwner != me) {
915      if (INTEL_DEBUG & DEBUG_BUFMGR) {
916	 fprintf(stderr, "Lost Context: sarea->ctxOwner %x me %x\n",
917		 sarea->ctxOwner, me);
918      }
919      sarea->ctxOwner = me;
920   }
921
922   /* If the last consumer of the texture memory wasn't us, notify the fake
923    * bufmgr and record the new owner.  We should have the memory shared
924    * between contexts of a single fake bufmgr, but this will at least make
925    * things correct for now.
926    */
927   if (!intel->ttm && sarea->texAge != intel->hHWContext) {
928      sarea->texAge = intel->hHWContext;
929      intel_bufmgr_fake_contended_lock_take(intel->bufmgr);
930      if (INTEL_DEBUG & DEBUG_BATCH)
931	 intel_decode_context_reset();
932      if (INTEL_DEBUG & DEBUG_BUFMGR)
933	 fprintf(stderr, "Lost Textures: sarea->texAge %x hw context %x\n",
934		 sarea->ctxOwner, intel->hHWContext);
935   }
936
937   /* Drawable changed?
938    */
939   if (dPriv && intel->lastStamp != dPriv->lastStamp) {
940       intelWindowMoved(intel);
941       intel->lastStamp = dPriv->lastStamp;
942   }
943}
944
945
946_glthread_DECLARE_STATIC_MUTEX(lockMutex);
947
948/* Lock the hardware and validate our state.
949 */
950void LOCK_HARDWARE( struct intel_context *intel )
951{
952    __DRIdrawable *dPriv = intel->driDrawable;
953    __DRIscreen *sPriv = intel->driScreen;
954    char __ret = 0;
955    struct intel_framebuffer *intel_fb = NULL;
956    struct intel_renderbuffer *intel_rb = NULL;
957
958    _glthread_LOCK_MUTEX(lockMutex);
959    assert(!intel->locked);
960    intel->locked = 1;
961
962    if (intel->driDrawable) {
963       intel_fb = intel->driDrawable->driverPrivate;
964
965       if (intel_fb)
966	  intel_rb =
967	     intel_get_renderbuffer(&intel_fb->Base,
968				    intel_fb->Base._ColorDrawBufferIndexes[0]);
969    }
970
971    if (intel_rb && dPriv->vblFlags &&
972	!(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) &&
973	(intel_fb->vbl_waited - intel_rb->vbl_pending) > (1<<23)) {
974	drmVBlank vbl;
975
976	vbl.request.type = DRM_VBLANK_ABSOLUTE;
977
978	if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
979	    vbl.request.type |= DRM_VBLANK_SECONDARY;
980	}
981
982	vbl.request.sequence = intel_rb->vbl_pending;
983	drmWaitVBlank(intel->driFd, &vbl);
984	intel_fb->vbl_waited = vbl.reply.sequence;
985    }
986
987    if (!sPriv->dri2.enabled) {
988	DRM_CAS(intel->driHwLock, intel->hHWContext,
989		(DRM_LOCK_HELD|intel->hHWContext), __ret);
990
991	if (__ret)
992	    intelContendedLock( intel, 0 );
993    }
994
995
996    if (INTEL_DEBUG & DEBUG_LOCK)
997      _mesa_printf("%s - locked\n", __progname);
998}
999
1000
1001/* Unlock the hardware using the global current context
1002 */
1003void UNLOCK_HARDWARE( struct intel_context *intel )
1004{
1005    __DRIscreen *sPriv = intel->driScreen;
1006
1007   intel->vtbl.note_unlock( intel );
1008   intel->locked = 0;
1009
1010   if (!sPriv->dri2.enabled)
1011      DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
1012
1013   _glthread_UNLOCK_MUTEX(lockMutex);
1014
1015   if (INTEL_DEBUG & DEBUG_LOCK)
1016      _mesa_printf("%s - unlocked\n", __progname);
1017
1018   /**
1019    * Nothing should be left in batch outside of LOCK/UNLOCK which references
1020    * cliprects.
1021    */
1022   if (intel->batch->cliprect_mode == REFERENCES_CLIPRECTS)
1023      intel_batchbuffer_flush(intel->batch);
1024}
1025
1026