intel_context.c revision 76360d6abc9e0195bc5c373101ae616e68b2e6e6
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/extensions.h"
32#include "main/fbobject.h"
33#include "main/framebuffer.h"
34#include "main/imports.h"
35#include "main/points.h"
36
37#include "swrast/swrast.h"
38#include "swrast_setup/swrast_setup.h"
39#include "tnl/tnl.h"
40#include "drivers/common/driverfuncs.h"
41#include "drivers/common/meta.h"
42
43#include "intel_chipset.h"
44#include "intel_buffers.h"
45#include "intel_tex.h"
46#include "intel_batchbuffer.h"
47#include "intel_clear.h"
48#include "intel_extensions.h"
49#include "intel_pixel.h"
50#include "intel_regions.h"
51#include "intel_buffer_objects.h"
52#include "intel_fbo.h"
53#include "intel_bufmgr.h"
54#include "intel_screen.h"
55
56#include "drirenderbuffer.h"
57#include "utils.h"
58
59
60#ifndef INTEL_DEBUG
61int INTEL_DEBUG = (0);
62#endif
63
64
65#define DRIVER_DATE                     "20100330 DEVELOPMENT"
66#define DRIVER_DATE_GEM                 "GEM " DRIVER_DATE
67
68
69static const GLubyte *
70intelGetString(struct gl_context * ctx, GLenum name)
71{
72   const struct intel_context *const intel = intel_context(ctx);
73   const char *chipset;
74   static char buffer[128];
75
76   switch (name) {
77   case GL_VENDOR:
78      return (GLubyte *) "Tungsten Graphics, Inc";
79      break;
80
81   case GL_RENDERER:
82      switch (intel->intelScreen->deviceID) {
83      case PCI_CHIP_845_G:
84         chipset = "Intel(R) 845G";
85         break;
86      case PCI_CHIP_I830_M:
87         chipset = "Intel(R) 830M";
88         break;
89      case PCI_CHIP_I855_GM:
90         chipset = "Intel(R) 852GM/855GM";
91         break;
92      case PCI_CHIP_I865_G:
93         chipset = "Intel(R) 865G";
94         break;
95      case PCI_CHIP_I915_G:
96         chipset = "Intel(R) 915G";
97         break;
98      case PCI_CHIP_E7221_G:
99	 chipset = "Intel (R) E7221G (i915)";
100	 break;
101      case PCI_CHIP_I915_GM:
102         chipset = "Intel(R) 915GM";
103         break;
104      case PCI_CHIP_I945_G:
105         chipset = "Intel(R) 945G";
106         break;
107      case PCI_CHIP_I945_GM:
108         chipset = "Intel(R) 945GM";
109         break;
110      case PCI_CHIP_I945_GME:
111         chipset = "Intel(R) 945GME";
112         break;
113      case PCI_CHIP_G33_G:
114	 chipset = "Intel(R) G33";
115	 break;
116      case PCI_CHIP_Q35_G:
117	 chipset = "Intel(R) Q35";
118	 break;
119      case PCI_CHIP_Q33_G:
120	 chipset = "Intel(R) Q33";
121	 break;
122      case PCI_CHIP_IGD_GM:
123      case PCI_CHIP_IGD_G:
124	 chipset = "Intel(R) IGD";
125	 break;
126      case PCI_CHIP_I965_Q:
127	 chipset = "Intel(R) 965Q";
128	 break;
129      case PCI_CHIP_I965_G:
130      case PCI_CHIP_I965_G_1:
131	 chipset = "Intel(R) 965G";
132	 break;
133      case PCI_CHIP_I946_GZ:
134	 chipset = "Intel(R) 946GZ";
135	 break;
136      case PCI_CHIP_I965_GM:
137	 chipset = "Intel(R) 965GM";
138	 break;
139      case PCI_CHIP_I965_GME:
140	 chipset = "Intel(R) 965GME/GLE";
141	 break;
142      case PCI_CHIP_GM45_GM:
143	 chipset = "Mobile Intel® GM45 Express Chipset";
144	 break;
145      case PCI_CHIP_IGD_E_G:
146	 chipset = "Intel(R) Integrated Graphics Device";
147	 break;
148      case PCI_CHIP_G45_G:
149         chipset = "Intel(R) G45/G43";
150         break;
151      case PCI_CHIP_Q45_G:
152         chipset = "Intel(R) Q45/Q43";
153         break;
154      case PCI_CHIP_G41_G:
155         chipset = "Intel(R) G41";
156         break;
157      case PCI_CHIP_B43_G:
158         chipset = "Intel(R) B43";
159         break;
160      case PCI_CHIP_ILD_G:
161         chipset = "Intel(R) Ironlake Desktop";
162         break;
163      case PCI_CHIP_ILM_G:
164         chipset = "Intel(R) Ironlake Mobile";
165         break;
166      case PCI_CHIP_SANDYBRIDGE_GT1:
167      case PCI_CHIP_SANDYBRIDGE_GT2:
168      case PCI_CHIP_SANDYBRIDGE_GT2_PLUS:
169	 chipset = "Intel(R) Sandybridge Desktop";
170	 break;
171      case PCI_CHIP_SANDYBRIDGE_M_GT1:
172      case PCI_CHIP_SANDYBRIDGE_M_GT2:
173      case PCI_CHIP_SANDYBRIDGE_M_GT2_PLUS:
174	 chipset = "Intel(R) Sandybridge Mobile";
175	 break;
176      case PCI_CHIP_SANDYBRIDGE_S:
177	 chipset = "Intel(R) Sandybridge Server";
178	 break;
179      default:
180         chipset = "Unknown Intel Chipset";
181         break;
182      }
183
184      (void) driGetRendererString(buffer, chipset, DRIVER_DATE_GEM, 0);
185      return (GLubyte *) buffer;
186
187   default:
188      return NULL;
189   }
190}
191
192static void
193intel_flush_front(struct gl_context *ctx)
194{
195   struct intel_context *intel = intel_context(ctx);
196    __DRIcontext *driContext = intel->driContext;
197    __DRIscreen *const screen = intel->intelScreen->driScrnPriv;
198
199   if ((ctx->DrawBuffer->Name == 0) && intel->front_buffer_dirty) {
200      if (screen->dri2.loader &&
201          (screen->dri2.loader->base.version >= 2)
202	  && (screen->dri2.loader->flushFrontBuffer != NULL) &&
203          driContext->driDrawablePriv &&
204	  driContext->driDrawablePriv->loaderPrivate) {
205	 (*screen->dri2.loader->flushFrontBuffer)(driContext->driDrawablePriv,
206						  driContext->driDrawablePriv->loaderPrivate);
207
208	 /* We set the dirty bit in intel_prepare_render() if we're
209	  * front buffer rendering once we get there.
210	  */
211	 intel->front_buffer_dirty = GL_FALSE;
212      }
213   }
214}
215
216static unsigned
217intel_bits_per_pixel(const struct intel_renderbuffer *rb)
218{
219   return _mesa_get_format_bytes(rb->Base.Format) * 8;
220}
221
222void
223intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
224{
225   struct gl_framebuffer *fb = drawable->driverPrivate;
226   struct intel_renderbuffer *rb;
227   struct intel_region *region, *depth_region;
228   struct intel_context *intel = context->driverPrivate;
229   struct intel_renderbuffer *front_rb, *back_rb, *depth_rb, *stencil_rb;
230   __DRIbuffer *buffers = NULL;
231   __DRIscreen *screen;
232   int i, count;
233   unsigned int attachments[10];
234   const char *region_name;
235
236   /* If we're rendering to the fake front buffer, make sure all the
237    * pending drawing has landed on the real front buffer.  Otherwise
238    * when we eventually get to DRI2GetBuffersWithFormat the stale
239    * real front buffer contents will get copied to the new fake front
240    * buffer.
241    */
242   if (intel->is_front_buffer_rendering) {
243      intel_flush(&intel->ctx);
244      intel_flush_front(&intel->ctx);
245   }
246
247   /* Set this up front, so that in case our buffers get invalidated
248    * while we're getting new buffers, we don't clobber the stamp and
249    * thus ignore the invalidate. */
250   drawable->lastStamp = drawable->dri2.stamp;
251
252   if (unlikely(INTEL_DEBUG & DEBUG_DRI))
253      fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
254
255   screen = intel->intelScreen->driScrnPriv;
256
257   if (screen->dri2.loader
258       && (screen->dri2.loader->base.version > 2)
259       && (screen->dri2.loader->getBuffersWithFormat != NULL)) {
260
261      front_rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
262      back_rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
263      depth_rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
264      stencil_rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
265
266      i = 0;
267      if ((intel->is_front_buffer_rendering ||
268	   intel->is_front_buffer_reading ||
269	   !back_rb) && front_rb) {
270	 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
271	 attachments[i++] = intel_bits_per_pixel(front_rb);
272      }
273
274      if (back_rb) {
275	 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
276	 attachments[i++] = intel_bits_per_pixel(back_rb);
277      }
278
279      if ((depth_rb != NULL) && (stencil_rb != NULL)) {
280	 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
281	 attachments[i++] = intel_bits_per_pixel(depth_rb);
282      } else if (depth_rb != NULL) {
283	 attachments[i++] = __DRI_BUFFER_DEPTH;
284	 attachments[i++] = intel_bits_per_pixel(depth_rb);
285      } else if (stencil_rb != NULL) {
286	 attachments[i++] = __DRI_BUFFER_STENCIL;
287	 attachments[i++] = intel_bits_per_pixel(stencil_rb);
288      }
289
290      buffers =
291	 (*screen->dri2.loader->getBuffersWithFormat)(drawable,
292						      &drawable->w,
293						      &drawable->h,
294						      attachments, i / 2,
295						      &count,
296						      drawable->loaderPrivate);
297   } else if (screen->dri2.loader) {
298      i = 0;
299      if (intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT))
300	 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
301      if (intel_get_renderbuffer(fb, BUFFER_BACK_LEFT))
302	 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
303      if (intel_get_renderbuffer(fb, BUFFER_DEPTH))
304	 attachments[i++] = __DRI_BUFFER_DEPTH;
305      if (intel_get_renderbuffer(fb, BUFFER_STENCIL))
306	 attachments[i++] = __DRI_BUFFER_STENCIL;
307
308      buffers = (*screen->dri2.loader->getBuffers)(drawable,
309						   &drawable->w,
310						   &drawable->h,
311						   attachments, i,
312						   &count,
313						   drawable->loaderPrivate);
314   }
315
316   if (buffers == NULL)
317      return;
318
319   drawable->x = 0;
320   drawable->y = 0;
321   drawable->backX = 0;
322   drawable->backY = 0;
323   drawable->numClipRects = 1;
324   drawable->pClipRects[0].x1 = 0;
325   drawable->pClipRects[0].y1 = 0;
326   drawable->pClipRects[0].x2 = drawable->w;
327   drawable->pClipRects[0].y2 = drawable->h;
328   drawable->numBackClipRects = 1;
329   drawable->pBackClipRects[0].x1 = 0;
330   drawable->pBackClipRects[0].y1 = 0;
331   drawable->pBackClipRects[0].x2 = drawable->w;
332   drawable->pBackClipRects[0].y2 = drawable->h;
333
334   depth_region = NULL;
335   for (i = 0; i < count; i++) {
336       switch (buffers[i].attachment) {
337       case __DRI_BUFFER_FRONT_LEFT:
338	   rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
339	   region_name = "dri2 front buffer";
340	   break;
341
342       case __DRI_BUFFER_FAKE_FRONT_LEFT:
343	   rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
344	   region_name = "dri2 fake front buffer";
345	   break;
346
347       case __DRI_BUFFER_BACK_LEFT:
348	   rb = intel_get_renderbuffer(fb, BUFFER_BACK_LEFT);
349	   region_name = "dri2 back buffer";
350	   break;
351
352       case __DRI_BUFFER_DEPTH:
353	   rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
354	   region_name = "dri2 depth buffer";
355	   break;
356
357       case __DRI_BUFFER_DEPTH_STENCIL:
358	   rb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
359	   region_name = "dri2 depth / stencil buffer";
360	   break;
361
362       case __DRI_BUFFER_STENCIL:
363	   rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
364	   region_name = "dri2 stencil buffer";
365	   break;
366
367       case __DRI_BUFFER_ACCUM:
368       default:
369	   fprintf(stderr,
370		   "unhandled buffer attach event, attachment type %d\n",
371		   buffers[i].attachment);
372	   return;
373       }
374
375       if (rb == NULL)
376	  continue;
377
378       if (rb->region && rb->region->name == buffers[i].name)
379	     continue;
380
381       if (unlikely(INTEL_DEBUG & DEBUG_DRI))
382	  fprintf(stderr,
383		  "attaching buffer %d, at %d, cpp %d, pitch %d\n",
384		  buffers[i].name, buffers[i].attachment,
385		  buffers[i].cpp, buffers[i].pitch);
386
387       if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
388	  if (unlikely(INTEL_DEBUG & DEBUG_DRI))
389	     fprintf(stderr, "(reusing depth buffer as stencil)\n");
390	  intel_region_reference(&region, depth_region);
391       }
392       else
393          region = intel_region_alloc_for_handle(intel->intelScreen,
394						 buffers[i].cpp,
395						 drawable->w,
396						 drawable->h,
397						 buffers[i].pitch / buffers[i].cpp,
398						 buffers[i].name,
399						 region_name);
400
401       if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
402	  depth_region = region;
403
404       intel_renderbuffer_set_region(intel, rb, region);
405       intel_region_release(&region);
406
407       if (buffers[i].attachment == __DRI_BUFFER_DEPTH_STENCIL) {
408	  rb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
409	  if (rb != NULL) {
410	     struct intel_region *stencil_region = NULL;
411
412	     if (rb->region && rb->region->name == buffers[i].name)
413		   continue;
414
415	     intel_region_reference(&stencil_region, region);
416	     intel_renderbuffer_set_region(intel, rb, stencil_region);
417	     intel_region_release(&stencil_region);
418	  }
419       }
420   }
421
422   driUpdateFramebufferSize(&intel->ctx, drawable);
423}
424
425/**
426 * intel_prepare_render should be called anywhere that curent read/drawbuffer
427 * state is required.
428 */
429void
430intel_prepare_render(struct intel_context *intel)
431{
432   __DRIcontext *driContext = intel->driContext;
433   __DRIdrawable *drawable;
434
435   drawable = driContext->driDrawablePriv;
436   if (drawable && drawable->dri2.stamp != driContext->dri2.draw_stamp) {
437      if (drawable->lastStamp != drawable->dri2.stamp)
438	 intel_update_renderbuffers(driContext, drawable);
439      intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
440      driContext->dri2.draw_stamp = drawable->dri2.stamp;
441   }
442
443   drawable = driContext->driReadablePriv;
444   if (drawable && drawable->dri2.stamp != driContext->dri2.read_stamp) {
445      if (drawable->lastStamp != drawable->dri2.stamp)
446	 intel_update_renderbuffers(driContext, drawable);
447      driContext->dri2.read_stamp = drawable->dri2.stamp;
448   }
449
450   /* If we're currently rendering to the front buffer, the rendering
451    * that will happen next will probably dirty the front buffer.  So
452    * mark it as dirty here.
453    */
454   if (intel->is_front_buffer_rendering)
455      intel->front_buffer_dirty = GL_TRUE;
456
457   /* Wait for the swapbuffers before the one we just emitted, so we
458    * don't get too many swaps outstanding for apps that are GPU-heavy
459    * but not CPU-heavy.
460    *
461    * We're using intelDRI2Flush (called from the loader before
462    * swapbuffer) and glFlush (for front buffer rendering) as the
463    * indicator that a frame is done and then throttle when we get
464    * here as we prepare to render the next frame.  At this point for
465    * round trips for swap/copy and getting new buffers are done and
466    * we'll spend less time waiting on the GPU.
467    *
468    * Unfortunately, we don't have a handle to the batch containing
469    * the swap, and getting our hands on that doesn't seem worth it,
470    * so we just us the first batch we emitted after the last swap.
471    */
472   if (intel->need_throttle && intel->first_post_swapbuffers_batch) {
473      /* Can't use drm_intel_bo_wait_rendering, since that only waits
474       * for rendering *to* the buffer, not from it.
475       */
476      drm_intel_gem_bo_map_gtt(intel->first_post_swapbuffers_batch);
477      drm_intel_gem_bo_unmap_gtt(intel->first_post_swapbuffers_batch);
478      drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
479      intel->first_post_swapbuffers_batch = NULL;
480      intel->need_throttle = GL_FALSE;
481   }
482}
483
484static void
485intel_viewport(struct gl_context *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
486{
487    struct intel_context *intel = intel_context(ctx);
488    __DRIcontext *driContext = intel->driContext;
489
490    if (intel->saved_viewport)
491	intel->saved_viewport(ctx, x, y, w, h);
492
493    if (!intel->meta.internal_viewport_call && ctx->DrawBuffer->Name == 0) {
494       dri2InvalidateDrawable(driContext->driDrawablePriv);
495       dri2InvalidateDrawable(driContext->driReadablePriv);
496    }
497}
498
499static const struct dri_debug_control debug_control[] = {
500   { "tex",   DEBUG_TEXTURE},
501   { "state", DEBUG_STATE},
502   { "ioctl", DEBUG_IOCTL},
503   { "blit",  DEBUG_BLIT},
504   { "mip",   DEBUG_MIPTREE},
505   { "fall",  DEBUG_FALLBACKS},
506   { "verb",  DEBUG_VERBOSE},
507   { "bat",   DEBUG_BATCH},
508   { "pix",   DEBUG_PIXEL},
509   { "buf",   DEBUG_BUFMGR},
510   { "reg",   DEBUG_REGION},
511   { "fbo",   DEBUG_FBO},
512   { "gs",    DEBUG_GS},
513   { "sync",  DEBUG_SYNC},
514   { "prim",  DEBUG_PRIMS },
515   { "vert",  DEBUG_VERTS },
516   { "dri",   DEBUG_DRI },
517   { "sf",    DEBUG_SF },
518   { "san",   DEBUG_SANITY },
519   { "sleep", DEBUG_SLEEP },
520   { "stats", DEBUG_STATS },
521   { "tile",  DEBUG_TILE },
522   { "sing",  DEBUG_SINGLE_THREAD },
523   { "thre",  DEBUG_SINGLE_THREAD },
524   { "wm",    DEBUG_WM },
525   { "glsl_force", DEBUG_GLSL_FORCE },
526   { "urb",   DEBUG_URB },
527   { "vs",    DEBUG_VS },
528   { "clip",  DEBUG_CLIP },
529   { NULL,    0 }
530};
531
532
533static void
534intelInvalidateState(struct gl_context * ctx, GLuint new_state)
535{
536    struct intel_context *intel = intel_context(ctx);
537
538   _swrast_InvalidateState(ctx, new_state);
539   _swsetup_InvalidateState(ctx, new_state);
540   _vbo_InvalidateState(ctx, new_state);
541   _tnl_InvalidateState(ctx, new_state);
542   _tnl_invalidate_vertex_state(ctx, new_state);
543
544   intel->NewGLState |= new_state;
545
546   if (intel->vtbl.invalidate_state)
547      intel->vtbl.invalidate_state( intel, new_state );
548}
549
550void
551intel_flush(struct gl_context *ctx)
552{
553   struct intel_context *intel = intel_context(ctx);
554
555   if (intel->Fallback)
556      _swrast_flush(ctx);
557
558   if (intel->gen < 4)
559      INTEL_FIREVERTICES(intel);
560
561   if (intel->batch->map != intel->batch->ptr)
562      intel_batchbuffer_flush(intel->batch);
563}
564
565static void
566intel_glFlush(struct gl_context *ctx)
567{
568   struct intel_context *intel = intel_context(ctx);
569
570   intel_flush(ctx);
571   intel_flush_front(ctx);
572   intel->need_throttle = GL_TRUE;
573}
574
575void
576intelFinish(struct gl_context * ctx)
577{
578   struct gl_framebuffer *fb = ctx->DrawBuffer;
579   int i;
580
581   intel_flush(ctx);
582   intel_flush_front(ctx);
583
584   for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
585       struct intel_renderbuffer *irb;
586
587       irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
588
589       if (irb && irb->region)
590	  drm_intel_bo_wait_rendering(irb->region->buffer);
591   }
592   if (fb->_DepthBuffer) {
593      /* XXX: Wait on buffer idle */
594   }
595}
596
597void
598intelInitDriverFunctions(struct dd_function_table *functions)
599{
600   _mesa_init_driver_functions(functions);
601
602   functions->Flush = intel_glFlush;
603   functions->Finish = intelFinish;
604   functions->GetString = intelGetString;
605   functions->UpdateState = intelInvalidateState;
606
607   intelInitTextureFuncs(functions);
608   intelInitTextureImageFuncs(functions);
609   intelInitTextureSubImageFuncs(functions);
610   intelInitTextureCopyImageFuncs(functions);
611   intelInitStateFuncs(functions);
612   intelInitClearFuncs(functions);
613   intelInitBufferFuncs(functions);
614   intelInitPixelFuncs(functions);
615   intelInitBufferObjectFuncs(functions);
616   intel_init_syncobj_functions(functions);
617}
618
619
620GLboolean
621intelInitContext(struct intel_context *intel,
622		 int api,
623                 const struct gl_config * mesaVis,
624                 __DRIcontext * driContextPriv,
625                 void *sharedContextPrivate,
626                 struct dd_function_table *functions)
627{
628   struct gl_context *ctx = &intel->ctx;
629   struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
630   __DRIscreen *sPriv = driContextPriv->driScreenPriv;
631   struct intel_screen *intelScreen = sPriv->private;
632   int bo_reuse_mode;
633   struct gl_config visual;
634
635   /* we can't do anything without a connection to the device */
636   if (intelScreen->bufmgr == NULL)
637      return GL_FALSE;
638
639   /* Can't rely on invalidate events, fall back to glViewport hack */
640   if (!driContextPriv->driScreenPriv->dri2.useInvalidate) {
641      intel->saved_viewport = functions->Viewport;
642      functions->Viewport = intel_viewport;
643   }
644
645   if (mesaVis == NULL) {
646      memset(&visual, 0, sizeof visual);
647      mesaVis = &visual;
648   }
649
650   if (!_mesa_initialize_context_for_api(&intel->ctx, api, mesaVis, shareCtx,
651					 functions, (void *) intel)) {
652      printf("%s: failed to init mesa context\n", __FUNCTION__);
653      return GL_FALSE;
654   }
655
656   driContextPriv->driverPrivate = intel;
657   intel->intelScreen = intelScreen;
658   intel->driContext = driContextPriv;
659   intel->driFd = sPriv->fd;
660
661   intel->has_xrgb_textures = GL_TRUE;
662   if (IS_GEN6(intel->intelScreen->deviceID)) {
663      intel->gen = 6;
664      intel->needs_ff_sync = GL_TRUE;
665      intel->has_luminance_srgb = GL_TRUE;
666   } else if (IS_GEN5(intel->intelScreen->deviceID)) {
667      intel->gen = 5;
668      intel->needs_ff_sync = GL_TRUE;
669      intel->has_luminance_srgb = GL_TRUE;
670   } else if (IS_965(intel->intelScreen->deviceID)) {
671      intel->gen = 4;
672      if (IS_G4X(intel->intelScreen->deviceID)) {
673	  intel->has_luminance_srgb = GL_TRUE;
674	  intel->is_g4x = GL_TRUE;
675      }
676   } else if (IS_9XX(intel->intelScreen->deviceID)) {
677      intel->gen = 3;
678      if (IS_945(intel->intelScreen->deviceID)) {
679	 intel->is_945 = GL_TRUE;
680      }
681   } else {
682      intel->gen = 2;
683      if (intel->intelScreen->deviceID == PCI_CHIP_I830_M ||
684	  intel->intelScreen->deviceID == PCI_CHIP_845_G) {
685	 intel->has_xrgb_textures = GL_FALSE;
686      }
687   }
688
689   driParseConfigFiles(&intel->optionCache, &intelScreen->optionCache,
690                       sPriv->myNum, (intel->gen >= 4) ? "i965" : "i915");
691   if (intelScreen->deviceID == PCI_CHIP_I865_G)
692      intel->maxBatchSize = 4096;
693   else
694      intel->maxBatchSize = BATCH_SZ;
695
696   intel->bufmgr = intelScreen->bufmgr;
697
698   bo_reuse_mode = driQueryOptioni(&intel->optionCache, "bo_reuse");
699   switch (bo_reuse_mode) {
700   case DRI_CONF_BO_REUSE_DISABLED:
701      break;
702   case DRI_CONF_BO_REUSE_ALL:
703      intel_bufmgr_gem_enable_reuse(intel->bufmgr);
704      break;
705   }
706
707   /* This doesn't yet catch all non-conformant rendering, but it's a
708    * start.
709    */
710   if (getenv("INTEL_STRICT_CONFORMANCE")) {
711      unsigned int value = atoi(getenv("INTEL_STRICT_CONFORMANCE"));
712      if (value > 0) {
713         intel->conformance_mode = value;
714      }
715      else {
716         intel->conformance_mode = 1;
717      }
718   }
719
720   if (intel->conformance_mode > 0) {
721      ctx->Const.MinLineWidth = 1.0;
722      ctx->Const.MinLineWidthAA = 1.0;
723      ctx->Const.MaxLineWidth = 1.0;
724      ctx->Const.MaxLineWidthAA = 1.0;
725      ctx->Const.LineWidthGranularity = 1.0;
726   }
727   else {
728      ctx->Const.MinLineWidth = 1.0;
729      ctx->Const.MinLineWidthAA = 1.0;
730      ctx->Const.MaxLineWidth = 5.0;
731      ctx->Const.MaxLineWidthAA = 5.0;
732      ctx->Const.LineWidthGranularity = 0.5;
733   }
734
735   ctx->Const.MinPointSize = 1.0;
736   ctx->Const.MinPointSizeAA = 1.0;
737   ctx->Const.MaxPointSize = 255.0;
738   ctx->Const.MaxPointSizeAA = 3.0;
739   ctx->Const.PointSizeGranularity = 1.0;
740
741   ctx->Const.MaxSamples = 1.0;
742
743   /* reinitialize the context point state.
744    * It depend on constants in __struct gl_contextRec::Const
745    */
746   _mesa_init_point(ctx);
747
748   meta_init_metaops(ctx, &intel->meta);
749   if (intel->gen >= 4) {
750      if (MAX_WIDTH > 8192)
751	 ctx->Const.MaxRenderbufferSize = 8192;
752   } else {
753      if (MAX_WIDTH > 2048)
754	 ctx->Const.MaxRenderbufferSize = 2048;
755   }
756
757   /* Initialize the software rasterizer and helper modules. */
758   _swrast_CreateContext(ctx);
759   _vbo_CreateContext(ctx);
760   _tnl_CreateContext(ctx);
761   _swsetup_CreateContext(ctx);
762
763   /* Configure swrast to match hardware characteristics: */
764   _swrast_allow_pixel_fog(ctx, GL_FALSE);
765   _swrast_allow_vertex_fog(ctx, GL_TRUE);
766
767   _mesa_meta_init(ctx);
768
769   intel->hw_stencil = mesaVis->stencilBits && mesaVis->depthBits == 24;
770   intel->hw_stipple = 1;
771
772   /* XXX FBO: this doesn't seem to be used anywhere */
773   switch (mesaVis->depthBits) {
774   case 0:                     /* what to do in this case? */
775   case 16:
776      intel->polygon_offset_scale = 1.0;
777      break;
778   case 24:
779      intel->polygon_offset_scale = 2.0;     /* req'd to pass glean */
780      break;
781   default:
782      assert(0);
783      break;
784   }
785
786   if (intel->gen >= 4)
787      intel->polygon_offset_scale /= 0xffff;
788
789   intel->RenderIndex = ~0;
790
791   switch (ctx->API) {
792   case API_OPENGL:
793      intelInitExtensions(ctx);
794      break;
795   case API_OPENGLES:
796      break;
797   case API_OPENGLES2:
798      intelInitExtensionsES2(ctx);
799      break;
800   }
801
802   INTEL_DEBUG = driParseDebugString(getenv("INTEL_DEBUG"), debug_control);
803   if (INTEL_DEBUG & DEBUG_BUFMGR)
804      dri_bufmgr_set_debug(intel->bufmgr, GL_TRUE);
805
806   /* XXX force SIMD8 kernel for Sandybridge before we fixed
807      SIMD16 interpolation. */
808   if (intel->gen == 6)
809       INTEL_DEBUG |= DEBUG_GLSL_FORCE;
810
811   intel->batch = intel_batchbuffer_alloc(intel);
812
813   intel_fbo_init(intel);
814
815   if (intel->ctx.Mesa_DXTn) {
816      _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
817      _mesa_enable_extension(ctx, "GL_S3_s3tc");
818   }
819   else if (driQueryOptionb(&intel->optionCache, "force_s3tc_enable")) {
820      _mesa_enable_extension(ctx, "GL_EXT_texture_compression_s3tc");
821   }
822   intel->use_texture_tiling = driQueryOptionb(&intel->optionCache,
823					       "texture_tiling");
824   intel->use_early_z = driQueryOptionb(&intel->optionCache, "early_z");
825
826   intel->prim.primitive = ~0;
827
828   /* Force all software fallbacks */
829   if (driQueryOptionb(&intel->optionCache, "no_rast")) {
830      fprintf(stderr, "disabling 3D rasterization\n");
831      intel->no_rast = 1;
832   }
833
834   if (driQueryOptionb(&intel->optionCache, "always_flush_batch")) {
835      fprintf(stderr, "flushing batchbuffer before/after each draw call\n");
836      intel->always_flush_batch = 1;
837   }
838
839   if (driQueryOptionb(&intel->optionCache, "always_flush_cache")) {
840      fprintf(stderr, "flushing GPU caches before/after each draw call\n");
841      intel->always_flush_cache = 1;
842   }
843
844   /* Disable all hardware rendering (skip emitting batches and fences/waits
845    * to the kernel)
846    */
847   intel->no_hw = getenv("INTEL_NO_HW") != NULL;
848
849   return GL_TRUE;
850}
851
852void
853intelDestroyContext(__DRIcontext * driContextPriv)
854{
855   struct intel_context *intel =
856      (struct intel_context *) driContextPriv->driverPrivate;
857
858   assert(intel);               /* should never be null */
859   if (intel) {
860      INTEL_FIREVERTICES(intel);
861
862      _mesa_meta_free(&intel->ctx);
863
864      meta_destroy_metaops(&intel->meta);
865
866      intel->vtbl.destroy(intel);
867
868      _swsetup_DestroyContext(&intel->ctx);
869      _tnl_DestroyContext(&intel->ctx);
870      _vbo_DestroyContext(&intel->ctx);
871
872      _swrast_DestroyContext(&intel->ctx);
873      intel->Fallback = 0x0;      /* don't call _swrast_Flush later */
874
875      intel_batchbuffer_free(intel->batch);
876      intel->batch = NULL;
877
878      free(intel->prim.vb);
879      intel->prim.vb = NULL;
880      drm_intel_bo_unreference(intel->prim.vb_bo);
881      intel->prim.vb_bo = NULL;
882      drm_intel_bo_unreference(intel->first_post_swapbuffers_batch);
883      intel->first_post_swapbuffers_batch = NULL;
884
885      driDestroyOptionCache(&intel->optionCache);
886
887      /* free the Mesa context */
888      _mesa_free_context_data(&intel->ctx);
889
890      FREE(intel);
891      driContextPriv->driverPrivate = NULL;
892   }
893}
894
895GLboolean
896intelUnbindContext(__DRIcontext * driContextPriv)
897{
898   /* Unset current context and dispath table */
899   _mesa_make_current(NULL, NULL, NULL);
900
901   return GL_TRUE;
902}
903
904GLboolean
905intelMakeCurrent(__DRIcontext * driContextPriv,
906                 __DRIdrawable * driDrawPriv,
907                 __DRIdrawable * driReadPriv)
908{
909   struct intel_context *intel;
910   GET_CURRENT_CONTEXT(curCtx);
911
912   if (driContextPriv)
913      intel = (struct intel_context *) driContextPriv->driverPrivate;
914   else
915      intel = NULL;
916
917   /* According to the glXMakeCurrent() man page: "Pending commands to
918    * the previous context, if any, are flushed before it is released."
919    * But only flush if we're actually changing contexts.
920    */
921   if (intel_context(curCtx) && intel_context(curCtx) != intel) {
922      _mesa_flush(curCtx);
923   }
924
925   if (driContextPriv) {
926      struct gl_framebuffer *fb, *readFb;
927
928      if (driDrawPriv == NULL && driReadPriv == NULL) {
929	 fb = _mesa_get_incomplete_framebuffer();
930	 readFb = _mesa_get_incomplete_framebuffer();
931      } else {
932	 fb = driDrawPriv->driverPrivate;
933	 readFb = driReadPriv->driverPrivate;
934	 driContextPriv->dri2.draw_stamp = driDrawPriv->dri2.stamp - 1;
935	 driContextPriv->dri2.read_stamp = driReadPriv->dri2.stamp - 1;
936      }
937
938      intel_prepare_render(intel);
939      _mesa_make_current(&intel->ctx, fb, readFb);
940
941      /* We do this in intel_prepare_render() too, but intel->ctx.DrawBuffer
942       * is NULL at that point.  We can't call _mesa_makecurrent()
943       * first, since we need the buffer size for the initial
944       * viewport.  So just call intel_draw_buffer() again here. */
945      intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
946   }
947   else {
948      _mesa_make_current(NULL, NULL, NULL);
949   }
950
951   return GL_TRUE;
952}
953