intel_context.c revision f7265e7a57bf49285dd226cf1effc75bf0cd54c0
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_vertex_program",              GL_NV_vertex_program_functions },
392   { "GL_NV_vertex_program1_1",           NULL },
393   { "GL_SGIS_generate_mipmap",           NULL },
394   { NULL, NULL }
395};
396
397static const struct dri_extension brw_extensions[] = {
398   { "GL_ARB_depth_texture",              NULL },
399   { "GL_ARB_draw_buffers",               NULL },
400   { "GL_ARB_fragment_program",           NULL },
401   { "GL_ARB_fragment_program_shadow",    NULL },
402   { "GL_ARB_fragment_shader",            NULL },
403   { "GL_ARB_framebuffer_object",         GL_ARB_framebuffer_object_functions},
404   { "GL_ARB_occlusion_query",            GL_ARB_occlusion_query_functions },
405   { "GL_ARB_point_sprite", 		  NULL },
406   { "GL_ARB_shader_objects",             GL_ARB_shader_objects_functions },
407   { "GL_ARB_shading_language_100",       GL_VERSION_2_0_functions },
408#if 0
409   /* Support for GLSL 1.20 is currently broken in core Mesa.
410    */
411   { "GL_ARB_shading_language_120",       GL_VERSION_2_1_functions },
412#endif
413   { "GL_ARB_shadow",                     NULL },
414   { "GL_ARB_texture_non_power_of_two",   NULL },
415   { "GL_ARB_vertex_shader",              GL_ARB_vertex_shader_functions },
416   { "GL_EXT_shadow_funcs",               NULL },
417   { "GL_EXT_stencil_two_side",           GL_EXT_stencil_two_side_functions },
418   { "GL_EXT_texture_sRGB",		  NULL },
419   { "GL_ATI_separate_stencil",           GL_ATI_separate_stencil_functions },
420   { "GL_ATI_texture_env_combine3",       NULL },
421   { NULL,                                NULL }
422};
423
424static const struct dri_extension arb_oq_extensions[] = {
425   { NULL, NULL }
426};
427
428static const struct dri_extension ttm_extensions[] = {
429   { "GL_ARB_pixel_buffer_object",      NULL },
430   { "GL_EXT_framebuffer_blit",         GL_EXT_framebuffer_blit_functions },
431   { "GL_EXT_framebuffer_object",       GL_EXT_framebuffer_object_functions },
432   { NULL, NULL }
433};
434
435/**
436 * Initializes potential list of extensions if ctx == NULL, or actually enables
437 * extensions for a context.
438 */
439void intelInitExtensions(GLcontext *ctx, GLboolean enable_imaging)
440{
441   struct intel_context *intel = ctx?intel_context(ctx):NULL;
442
443   /* Disable imaging extension until convolution is working in teximage paths.
444    */
445   enable_imaging = GL_FALSE;
446
447   driInitExtensions(ctx, card_extensions, enable_imaging);
448
449   if (intel == NULL || intel->ttm)
450      driInitExtensions(ctx, ttm_extensions, GL_FALSE);
451
452   if (intel == NULL || IS_965(intel->intelScreen->deviceID))
453      driInitExtensions(ctx, brw_extensions, GL_FALSE);
454}
455
456static const struct dri_debug_control debug_control[] = {
457   { "tex",   DEBUG_TEXTURE},
458   { "state", DEBUG_STATE},
459   { "ioctl", DEBUG_IOCTL},
460   { "blit",  DEBUG_BLIT},
461   { "mip",   DEBUG_MIPTREE},
462   { "fall",  DEBUG_FALLBACKS},
463   { "verb",  DEBUG_VERBOSE},
464   { "bat",   DEBUG_BATCH},
465   { "pix",   DEBUG_PIXEL},
466   { "buf",   DEBUG_BUFMGR},
467   { "reg",   DEBUG_REGION},
468   { "fbo",   DEBUG_FBO},
469   { "lock",  DEBUG_LOCK},
470   { "sync",  DEBUG_SYNC},
471   { "prim",  DEBUG_PRIMS },
472   { "vert",  DEBUG_VERTS },
473   { "dri",   DEBUG_DRI },
474   { "dma",   DEBUG_DMA },
475   { "san",   DEBUG_SANITY },
476   { "sleep", DEBUG_SLEEP },
477   { "stats", DEBUG_STATS },
478   { "tile",  DEBUG_TILE },
479   { "sing",  DEBUG_SINGLE_THREAD },
480   { "thre",  DEBUG_SINGLE_THREAD },
481   { "wm",    DEBUG_WM },
482   { "urb",   DEBUG_URB },
483   { "vs",    DEBUG_VS },
484   { NULL,    0 }
485};
486
487
488static void
489intelInvalidateState(GLcontext * ctx, GLuint new_state)
490{
491    struct intel_context *intel = intel_context(ctx);
492
493   _swrast_InvalidateState(ctx, new_state);
494   _swsetup_InvalidateState(ctx, new_state);
495   _vbo_InvalidateState(ctx, new_state);
496   _tnl_InvalidateState(ctx, new_state);
497   _tnl_invalidate_vertex_state(ctx, new_state);
498
499   intel->NewGLState |= new_state;
500
501   if (intel->vtbl.invalidate_state)
502      intel->vtbl.invalidate_state( intel, new_state );
503}
504
505
506void
507intelFlush(GLcontext * ctx)
508{
509   struct intel_context *intel = intel_context(ctx);
510
511   if (intel->Fallback)
512      _swrast_flush(ctx);
513
514   if (!IS_965(intel->intelScreen->deviceID))
515      INTEL_FIREVERTICES(intel);
516
517   /* Emit a flush so that any frontbuffer rendering that might have occurred
518    * lands onscreen in a timely manner, even if the X Server doesn't trigger
519    * a flush for us.
520    */
521   intel_batchbuffer_emit_mi_flush(intel->batch);
522
523   if (intel->batch->map != intel->batch->ptr)
524      intel_batchbuffer_flush(intel->batch);
525}
526
527void
528intelFinish(GLcontext * ctx)
529{
530   struct gl_framebuffer *fb = ctx->DrawBuffer;
531   int i;
532
533   intelFlush(ctx);
534
535   for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
536       struct intel_renderbuffer *irb;
537
538       irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
539
540       if (irb->region)
541	  dri_bo_wait_rendering(irb->region->buffer);
542   }
543   if (fb->_DepthBuffer) {
544      /* XXX: Wait on buffer idle */
545   }
546}
547
548void
549intelInitDriverFunctions(struct dd_function_table *functions)
550{
551   _mesa_init_driver_functions(functions);
552
553   functions->Flush = intelFlush;
554   functions->Finish = intelFinish;
555   functions->GetString = intelGetString;
556   functions->UpdateState = intelInvalidateState;
557
558   functions->CopyColorTable = _swrast_CopyColorTable;
559   functions->CopyColorSubTable = _swrast_CopyColorSubTable;
560   functions->CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
561   functions->CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
562
563   intelInitTextureFuncs(functions);
564   intelInitStateFuncs(functions);
565   intelInitBufferFuncs(functions);
566   intelInitPixelFuncs(functions);
567}
568
569
570GLboolean
571intelInitContext(struct intel_context *intel,
572                 const __GLcontextModes * mesaVis,
573                 __DRIcontextPrivate * driContextPriv,
574                 void *sharedContextPrivate,
575                 struct dd_function_table *functions)
576{
577   GLcontext *ctx = &intel->ctx;
578   GLcontext *shareCtx = (GLcontext *) sharedContextPrivate;
579   __DRIscreenPrivate *sPriv = driContextPriv->driScreenPriv;
580   intelScreenPrivate *intelScreen = (intelScreenPrivate *) sPriv->private;
581   int fthrottle_mode;
582
583   if (!_mesa_initialize_context(&intel->ctx, mesaVis, shareCtx,
584                                 functions, (void *) intel)) {
585      _mesa_printf("%s: failed to init mesa context\n", __FUNCTION__);
586      return GL_FALSE;
587   }
588
589   driContextPriv->driverPrivate = intel;
590   intel->intelScreen = intelScreen;
591   intel->driScreen = sPriv;
592   intel->sarea = intelScreen->sarea;
593   intel->driContext = driContextPriv;
594
595   /* Dri stuff */
596   intel->hHWContext = driContextPriv->hHWContext;
597   intel->driFd = sPriv->fd;
598   intel->driHwLock = sPriv->lock;
599
600   driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
601                       intel->driScreen->myNum,
602		       IS_965(intelScreen->deviceID) ? "i965" : "i915");
603   if (intelScreen->deviceID == PCI_CHIP_I865_G)
604      intel->maxBatchSize = 4096;
605   else
606      intel->maxBatchSize = BATCH_SZ;
607
608   intel->bufmgr = intelScreen->bufmgr;
609   intel->ttm = intelScreen->ttm;
610   if (intel->ttm) {
611      int bo_reuse_mode;
612
613      bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
614      switch (bo_reuse_mode) {
615      case DRI_CONF_BO_REUSE_DISABLED:
616	 break;
617      case DRI_CONF_BO_REUSE_ALL:
618	 intel_bufmgr_gem_enable_reuse(intel->bufmgr);
619	 break;
620      }
621   }
622
623   ctx->Const.MaxTextureMaxAnisotropy = 2.0;
624
625   /* This doesn't yet catch all non-conformant rendering, but it's a
626    * start.
627    */
628   if (getenv("INTEL_STRICT_CONFORMANCE")) {
629      intel->strict_conformance = 1;
630   }
631
632   if (intel->strict_conformance) {
633      ctx->Const.MinLineWidth = 1.0;
634      ctx->Const.MinLineWidthAA = 1.0;
635      ctx->Const.MaxLineWidth = 1.0;
636      ctx->Const.MaxLineWidthAA = 1.0;
637      ctx->Const.LineWidthGranularity = 1.0;
638   }
639   else {
640      ctx->Const.MinLineWidth = 1.0;
641      ctx->Const.MinLineWidthAA = 1.0;
642      ctx->Const.MaxLineWidth = 5.0;
643      ctx->Const.MaxLineWidthAA = 5.0;
644      ctx->Const.LineWidthGranularity = 0.5;
645   }
646
647   ctx->Const.MinPointSize = 1.0;
648   ctx->Const.MinPointSizeAA = 1.0;
649   ctx->Const.MaxPointSize = 255.0;
650   ctx->Const.MaxPointSizeAA = 3.0;
651   ctx->Const.PointSizeGranularity = 1.0;
652
653   /* reinitialize the context point state.
654    * It depend on constants in __GLcontextRec::Const
655    */
656   _mesa_init_point(ctx);
657
658   ctx->Const.MaxColorAttachments = 4;  /* XXX FBO: review this */
659
660   /* Initialize the software rasterizer and helper modules. */
661   _swrast_CreateContext(ctx);
662   _vbo_CreateContext(ctx);
663   _tnl_CreateContext(ctx);
664   _swsetup_CreateContext(ctx);
665
666   /* Configure swrast to match hardware characteristics: */
667   _swrast_allow_pixel_fog(ctx, GL_FALSE);
668   _swrast_allow_vertex_fog(ctx, GL_TRUE);
669
670   intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
671   intel->hw_stipple = 1;
672
673   /* XXX FBO: this doesn't seem to be used anywhere */
674   switch (mesaVis->depthBits) {
675   case 0:                     /* what to do in this case? */
676   case 16:
677      intel->polygon_offset_scale = 1.0;
678      break;
679   case 24:
680      intel->polygon_offset_scale = 2.0;     /* req'd to pass glean */
681      break;
682   default:
683      assert(0);
684      break;
685   }
686
687   if (IS_965(intelScreen->deviceID))
688      intel->polygon_offset_scale /= 0xffff;
689
690   intel->RenderIndex = ~0;
691
692   fthrottle_mode = driQueryOptioni(&intel->optionCache, "fthrottle_mode");
693   intel->irqsEmitted = 0;
694
695   intel->do_irqs = (intel->intelScreen->irq_active &&
696                     fthrottle_mode == DRI_CONF_FTHROTTLE_IRQS);
697
698   intel->do_usleeps = (fthrottle_mode == DRI_CONF_FTHROTTLE_USLEEPS);
699
700   _math_matrix_ctr(&intel->ViewportMatrix);
701
702   if (IS_965(intelScreen->deviceID) && !intel->intelScreen->irq_active) {
703      _mesa_printf("IRQs not active.  Exiting\n");
704      exit(1);
705   }
706
707   intelInitExtensions(ctx, GL_FALSE);
708
709   INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
710   if (INTEL_DEBUG & DEBUG_BUFMGR)
711      dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
712
713   if (!sPriv->dri2.enabled)
714      intel_recreate_static_regions(intel);
715
716   intel->batch = intel_batchbuffer_alloc(intel);
717
718   intel_bufferobj_init(intel);
719   intel_fbo_init(intel);
720
721   if (intel->ctx.Mesa_DXTn) {
722      _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
723      _mesa_enable_extension(ctx, "GL_S3_s3tc");
724   }
725   else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
726      _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
727   }
728
729   intel->prim.primitive = ~0;
730
731   /* Force all software fallbacks */
732   if (driQueryOptionb(&intel->optionCache, "no_rast")) {
733      fprintf(stderr, "disabling 3D rasterization\n");
734      intel->no_rast = 1;
735   }
736
737   /* Disable all hardware rendering (skip emitting batches and fences/waits
738    * to the kernel)
739    */
740   intel->no_hw = getenv("INTEL_NO_HW") != NULL;
741
742   return GL_TRUE;
743}
744
745void
746intelDestroyContext(__DRIcontextPrivate * driContextPriv)
747{
748   struct intel_context *intel =
749      (struct intel_context *) driContextPriv->driverPrivate;
750
751   assert(intel);               /* should never be null */
752   if (intel) {
753      GLboolean release_texture_heaps;
754
755      INTEL_FIREVERTICES(intel);
756
757      intel->vtbl.destroy(intel);
758
759      release_texture_heaps = (intel->ctx.Shared->RefCount == 1);
760      _swsetup_DestroyContext(&intel->ctx);
761      _tnl_DestroyContext(&intel->ctx);
762      _vbo_DestroyContext(&intel->ctx);
763
764      _swrast_DestroyContext(&intel->ctx);
765      intel->Fallback = 0;      /* don't call _swrast_Flush later */
766
767      intel_batchbuffer_free(intel->batch);
768      intel->batch = NULL;
769
770      free(intel->prim.vb);
771      intel->prim.vb = NULL;
772      dri_bo_unreference(intel->prim.vb_bo);
773      intel->prim.vb_bo = NULL;
774
775      if (release_texture_heaps) {
776         /* This share group is about to go away, free our private
777          * texture object data.
778          */
779         if (INTEL_DEBUG & DEBUG_TEXTURE)
780            fprintf(stderr, "do something to free texture heaps\n");
781      }
782
783      intel_region_release(&intel->front_region);
784      intel_region_release(&intel->back_region);
785      intel_region_release(&intel->depth_region);
786
787      driDestroyOptionCache(&intel->optionCache);
788
789      /* free the Mesa context */
790      _mesa_free_context_data(&intel->ctx);
791   }
792}
793
794GLboolean
795intelUnbindContext(__DRIcontextPrivate * driContextPriv)
796{
797   return GL_TRUE;
798}
799
800GLboolean
801intelMakeCurrent(__DRIcontextPrivate * driContextPriv,
802                 __DRIdrawablePrivate * driDrawPriv,
803                 __DRIdrawablePrivate * driReadPriv)
804{
805   __DRIscreenPrivate *psp = driDrawPriv->driScreenPriv;
806
807   if (driContextPriv) {
808      struct intel_context *intel =
809         (struct intel_context *) driContextPriv->driverPrivate;
810      struct intel_framebuffer *intel_fb =
811	 (struct intel_framebuffer *) driDrawPriv->driverPrivate;
812      GLframebuffer *readFb = (GLframebuffer *) driReadPriv->driverPrivate;
813
814      if (driContextPriv->driScreenPriv->dri2.enabled) {
815          intel_update_renderbuffers(driContextPriv, driDrawPriv);
816          if (driDrawPriv != driReadPriv)
817              intel_update_renderbuffers(driContextPriv, driReadPriv);
818      } else {
819          /* XXX FBO temporary fix-ups! */
820          /* if the renderbuffers don't have regions, init them from the context */
821         struct intel_renderbuffer *irbDepth
822            = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
823         struct intel_renderbuffer *irbStencil
824            = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
825
826         if (intel_fb->color_rb[0]) {
827	    intel_renderbuffer_set_region(intel_fb->color_rb[0],
828					  intel->front_region);
829         }
830         if (intel_fb->color_rb[1]) {
831	    intel_renderbuffer_set_region(intel_fb->color_rb[1],
832					  intel->back_region);
833         }
834
835         if (irbDepth) {
836	    intel_renderbuffer_set_region(irbDepth, intel->depth_region);
837         }
838         if (irbStencil) {
839	    intel_renderbuffer_set_region(irbStencil, intel->depth_region);
840         }
841      }
842
843      /* set GLframebuffer size to match window, if needed */
844      driUpdateFramebufferSize(&intel->ctx, driDrawPriv);
845
846      if (driReadPriv != driDrawPriv) {
847	 driUpdateFramebufferSize(&intel->ctx, driReadPriv);
848      }
849
850      _mesa_make_current(&intel->ctx, &intel_fb->Base, readFb);
851
852      /* The drawbuffer won't always be updated by _mesa_make_current:
853       */
854      if (intel->ctx.DrawBuffer == &intel_fb->Base) {
855
856	 if (intel->driReadDrawable != driReadPriv)
857	    intel->driReadDrawable = driReadPriv;
858
859	 if (intel->driDrawable != driDrawPriv) {
860	    if (driDrawPriv->swap_interval == (unsigned)-1) {
861	       int i;
862
863	       driDrawPriv->vblFlags = (intel->intelScreen->irq_active != 0)
864		  ? driGetDefaultVBlankFlags(&intel->optionCache)
865		 : VBLANK_FLAG_NO_IRQ;
866
867	       (*psp->systemTime->getUST) (&intel_fb->swap_ust);
868	       driDrawableInitVBlank(driDrawPriv);
869	       intel_fb->vbl_waited = driDrawPriv->vblSeq;
870
871	       for (i = 0; i < 2; i++) {
872		  if (intel_fb->color_rb[i])
873		     intel_fb->color_rb[i]->vbl_pending = driDrawPriv->vblSeq;
874	       }
875	    }
876	    intel->driDrawable = driDrawPriv;
877	    intelWindowMoved(intel);
878	 }
879
880	 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
881      }
882   }
883   else {
884      _mesa_make_current(NULL, NULL, NULL);
885   }
886
887   return GL_TRUE;
888}
889
890static void
891intelContendedLock(struct intel_context *intel, GLuint flags)
892{
893   __DRIdrawablePrivate *dPriv = intel->driDrawable;
894   __DRIscreenPrivate *sPriv = intel->driScreen;
895   volatile drm_i915_sarea_t *sarea = intel->sarea;
896   int me = intel->hHWContext;
897
898   drmGetLock(intel->driFd, intel->hHWContext, flags);
899   intel->locked = 1;
900
901   if (INTEL_DEBUG & DEBUG_LOCK)
902      _mesa_printf("%s - got contended lock\n", __progname);
903
904   /* If the window moved, may need to set a new cliprect now.
905    *
906    * NOTE: This releases and regains the hw lock, so all state
907    * checking must be done *after* this call:
908    */
909   if (dPriv)
910       DRI_VALIDATE_DRAWABLE_INFO(sPriv, dPriv);
911
912   if (sarea && sarea->ctxOwner != me) {
913      if (INTEL_DEBUG & DEBUG_BUFMGR) {
914	 fprintf(stderr, "Lost Context: sarea->ctxOwner %x me %x\n",
915		 sarea->ctxOwner, me);
916      }
917      sarea->ctxOwner = me;
918   }
919
920   /* If the last consumer of the texture memory wasn't us, notify the fake
921    * bufmgr and record the new owner.  We should have the memory shared
922    * between contexts of a single fake bufmgr, but this will at least make
923    * things correct for now.
924    */
925   if (!intel->ttm && sarea->texAge != intel->hHWContext) {
926      sarea->texAge = intel->hHWContext;
927      intel_bufmgr_fake_contended_lock_take(intel->bufmgr);
928      if (INTEL_DEBUG & DEBUG_BATCH)
929	 intel_decode_context_reset();
930      if (INTEL_DEBUG & DEBUG_BUFMGR)
931	 fprintf(stderr, "Lost Textures: sarea->texAge %x hw context %x\n",
932		 sarea->ctxOwner, intel->hHWContext);
933   }
934
935   /* Drawable changed?
936    */
937   if (dPriv && intel->lastStamp != dPriv->lastStamp) {
938       intelWindowMoved(intel);
939       intel->lastStamp = dPriv->lastStamp;
940   }
941}
942
943
944_glthread_DECLARE_STATIC_MUTEX(lockMutex);
945
946/* Lock the hardware and validate our state.
947 */
948void LOCK_HARDWARE( struct intel_context *intel )
949{
950    __DRIdrawable *dPriv = intel->driDrawable;
951    __DRIscreen *sPriv = intel->driScreen;
952    char __ret = 0;
953    struct intel_framebuffer *intel_fb = NULL;
954    struct intel_renderbuffer *intel_rb = NULL;
955
956    _glthread_LOCK_MUTEX(lockMutex);
957    assert(!intel->locked);
958    intel->locked = 1;
959
960    if (intel->driDrawable) {
961       intel_fb = intel->driDrawable->driverPrivate;
962
963       if (intel_fb)
964	  intel_rb =
965	     intel_get_renderbuffer(&intel_fb->Base,
966				    intel_fb->Base._ColorDrawBufferIndexes[0]);
967    }
968
969    if (intel_rb && dPriv->vblFlags &&
970	!(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) &&
971	(intel_fb->vbl_waited - intel_rb->vbl_pending) > (1<<23)) {
972	drmVBlank vbl;
973
974	vbl.request.type = DRM_VBLANK_ABSOLUTE;
975
976	if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
977	    vbl.request.type |= DRM_VBLANK_SECONDARY;
978	}
979
980	vbl.request.sequence = intel_rb->vbl_pending;
981	drmWaitVBlank(intel->driFd, &vbl);
982	intel_fb->vbl_waited = vbl.reply.sequence;
983    }
984
985    if (!sPriv->dri2.enabled) {
986	DRM_CAS(intel->driHwLock, intel->hHWContext,
987		(DRM_LOCK_HELD|intel->hHWContext), __ret);
988
989	if (__ret)
990	    intelContendedLock( intel, 0 );
991    }
992
993
994    if (INTEL_DEBUG & DEBUG_LOCK)
995      _mesa_printf("%s - locked\n", __progname);
996}
997
998
999/* Unlock the hardware using the global current context
1000 */
1001void UNLOCK_HARDWARE( struct intel_context *intel )
1002{
1003    __DRIscreen *sPriv = intel->driScreen;
1004
1005   intel->vtbl.note_unlock( intel );
1006   intel->locked = 0;
1007
1008   if (!sPriv->dri2.enabled)
1009      DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
1010
1011   _glthread_UNLOCK_MUTEX(lockMutex);
1012
1013   if (INTEL_DEBUG & DEBUG_LOCK)
1014      _mesa_printf("%s - unlocked\n", __progname);
1015
1016   /**
1017    * Nothing should be left in batch outside of LOCK/UNLOCK which references
1018    * cliprects.
1019    */
1020   if (intel->batch->cliprect_mode == REFERENCES_CLIPRECTS)
1021      intel_batchbuffer_flush(intel->batch);
1022}
1023
1024