intel: Fix a crash if dri2 is disabled.
[mesa.git] / src / mesa / drivers / dri / intel / intel_context.c
index 46acf7972170b79027ce54358713fb9221a61eac..2975e4c0f0930c02ad39ef830c2641934976e901 100644 (file)
@@ -96,11 +96,13 @@ int INTEL_DEBUG = (0);
 
 #include "extension_helper.h"
 
-#define DRIVER_DATE                     "20061102"
+#define DRIVER_DATE                     "20080716"
+#define DRIVER_DATE_GEM                 "GEM " DRIVER_DATE
 
 static const GLubyte *
 intelGetString(GLcontext * ctx, GLenum name)
 {
+   const struct intel_context *const intel = intel_context(ctx);
    const char *chipset;
    static char buffer[128];
 
@@ -110,7 +112,7 @@ intelGetString(GLcontext * ctx, GLenum name)
       break;
 
    case GL_RENDERER:
-      switch (intel_context(ctx)->intelScreen->deviceID) {
+      switch (intel->intelScreen->deviceID) {
       case PCI_CHIP_845_G:
          chipset = "Intel(R) 845G";
          break;
@@ -166,7 +168,9 @@ intelGetString(GLcontext * ctx, GLenum name)
       case PCI_CHIP_I965_GME:
         chipset = "Intel(R) 965GME/GLE";
         break;
-      case PCI_CHIP_IGD_GM:
+      case PCI_CHIP_GM45_GM:
+        chipset = "Mobile IntelĀ® GM45 Express Chipset";
+        break; 
       case PCI_CHIP_IGD_E_G:
         chipset = "Intel(R) Integrated Graphics Device";
         break;
@@ -181,7 +185,9 @@ intelGetString(GLcontext * ctx, GLenum name)
          break;
       }
 
-      (void) driGetRendererString(buffer, chipset, DRIVER_DATE, 0);
+      (void) driGetRendererString(buffer, chipset, 
+                                 (intel->ttm) ? DRIVER_DATE_GEM : DRIVER_DATE,
+                                 0);
       return (GLubyte *) buffer;
 
    default:
@@ -189,12 +195,147 @@ intelGetString(GLcontext * ctx, GLenum name)
    }
 }
 
+void
+intel_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable)
+{
+   struct intel_framebuffer *intel_fb = drawable->driverPrivate;
+   struct intel_renderbuffer *rb;
+   struct intel_region *region, *depth_region;
+   struct intel_context *intel = context->driverPrivate;
+   __DRIbuffer *buffers;
+   __DRIscreen *screen;
+   int i, count;
+   unsigned int attachments[10];
+   uint32_t name;
+   const char *region_name;
+
+   if (INTEL_DEBUG & DEBUG_DRI)
+      fprintf(stderr, "enter %s, drawable %p\n", __func__, drawable);
+
+   screen = intel->intelScreen->driScrnPriv;
+
+   i = 0;
+   if (intel_fb->color_rb[0])
+      attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
+   if (intel_fb->color_rb[1])
+      attachments[i++] = __DRI_BUFFER_BACK_LEFT;
+   if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH))
+      attachments[i++] = __DRI_BUFFER_DEPTH;
+   if (intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL))
+      attachments[i++] = __DRI_BUFFER_STENCIL;
+
+   buffers = (*screen->dri2.loader->getBuffers)(drawable,
+                                               &drawable->w,
+                                               &drawable->h,
+                                               attachments, i,
+                                               &count,
+                                               drawable->loaderPrivate);
+
+   drawable->x = 0;
+   drawable->y = 0;
+   drawable->backX = 0;
+   drawable->backY = 0;
+   drawable->numClipRects = 1;
+   drawable->pClipRects[0].x1 = 0;
+   drawable->pClipRects[0].y1 = 0;
+   drawable->pClipRects[0].x2 = drawable->w;
+   drawable->pClipRects[0].y2 = drawable->h;
+   drawable->numBackClipRects = 1;
+   drawable->pBackClipRects[0].x1 = 0;
+   drawable->pBackClipRects[0].y1 = 0;
+   drawable->pBackClipRects[0].x2 = drawable->w;
+   drawable->pBackClipRects[0].y2 = drawable->h;
+
+   depth_region = NULL;
+   for (i = 0; i < count; i++) {
+       switch (buffers[i].attachment) {
+       case __DRI_BUFFER_FRONT_LEFT:
+          rb = intel_fb->color_rb[0];
+          region_name = "dri2 front buffer";
+          break;
+
+       case __DRI_BUFFER_BACK_LEFT:
+          rb = intel_fb->color_rb[1];
+          region_name = "dri2 back buffer";
+          break;
+
+       case __DRI_BUFFER_DEPTH:
+          rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
+          region_name = "dri2 depth buffer";
+          break;
+
+       case __DRI_BUFFER_STENCIL:
+          rb = intel_get_renderbuffer(&intel_fb->Base, BUFFER_STENCIL);
+          region_name = "dri2 stencil buffer";
+          break;
+
+       case __DRI_BUFFER_ACCUM:
+       default:
+          fprintf(stderr,
+                  "unhandled buffer attach event, attacment type %d\n",
+                  buffers[i].attachment);
+          return;
+       }
+
+       if (rb->region) {
+         intel_bo_flink(rb->region->buffer, &name);
+         if (name == buffers[i].name)
+            continue;
+       }
+
+       if (INTEL_DEBUG & DEBUG_DRI)
+         fprintf(stderr,
+                 "attaching buffer %d, at %d, cpp %d, pitch %d\n",
+                 buffers[i].name, buffers[i].attachment,
+                 buffers[i].cpp, buffers[i].pitch);
+       
+       if (buffers[i].attachment == __DRI_BUFFER_STENCIL && depth_region) {
+         if (INTEL_DEBUG & DEBUG_DRI)
+            fprintf(stderr, "(reusing depth buffer as stencil)\n");
+          region = depth_region;
+       }
+       else
+          region = intel_region_alloc_for_handle(intel, buffers[i].cpp,
+                                                buffers[i].pitch / buffers[i].cpp,
+                                                drawable->h,
+                                                buffers[i].name,
+                                                region_name);
+
+       if (buffers[i].attachment == __DRI_BUFFER_DEPTH)
+         depth_region = region;
+
+       intel_renderbuffer_set_region(rb, region);
+       intel_region_release(&region);
+   }
+
+   driUpdateFramebufferSize(&intel->ctx, drawable);
+}
+
+static void
+intel_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
+{
+    struct intel_context *intel = intel_context(ctx);
+    __DRIcontext *driContext = intel->driContext;
+
+    if (!driContext->driScreenPriv->dri2.enabled)
+       return;
+
+    intel_update_renderbuffers(driContext, driContext->driDrawablePriv);
+    if (driContext->driDrawablePriv != driContext->driReadablePriv)
+       intel_update_renderbuffers(driContext, driContext->driReadablePriv);
+
+    ctx->Driver.Viewport = NULL;
+    intel->driDrawable = driContext->driDrawablePriv;
+    intelWindowMoved(intel);
+    intel_draw_buffer(ctx, intel->ctx.DrawBuffer);
+    ctx->Driver.Viewport = intel_viewport;
+}
+
 /**
  * Extension strings exported by the intel driver.
  *
- * \note
- * It appears that ARB_texture_env_crossbar has "disappeared" compared to the
- * old i830-specific driver.
+ * Extensions supported by all chips supported by i830_dri, i915_dri, or
+ * i965_dri.
  */
 static const struct dri_extension card_extensions[] = {
    {"GL_ARB_multisample", GL_ARB_multisample_functions},
@@ -206,6 +347,7 @@ static const struct dri_extension card_extensions[] = {
    {"GL_ARB_texture_cube_map", NULL},
    {"GL_ARB_texture_env_add", NULL},
    {"GL_ARB_texture_env_combine", NULL},
+   {"GL_ARB_texture_env_crossbar", NULL},
    {"GL_ARB_texture_env_dot3", NULL},
    {"GL_ARB_texture_mirrored_repeat", NULL},
    {"GL_ARB_texture_non_power_of_two",   NULL },
@@ -260,9 +402,9 @@ static const struct dri_extension brw_extensions[] = {
    { "GL_ARB_fragment_program",           NULL },
    { "GL_ARB_shadow",                     NULL },
    { "GL_EXT_shadow_funcs",               NULL },
+   { "GL_ARB_fragment_program_shadow",    NULL },
    /* ARB extn won't work if not enabled */
    { "GL_SGIX_depth_texture",             NULL },
-   { "GL_ARB_texture_env_crossbar",       NULL },
    { "GL_EXT_texture_sRGB",              NULL},
    { NULL,                                NULL }
 };
@@ -365,6 +507,12 @@ intelFlush(GLcontext * ctx)
    if (!IS_965(intel->intelScreen->deviceID))
       INTEL_FIREVERTICES(intel);
 
+   /* Emit a flush so that any frontbuffer rendering that might have occurred
+    * lands onscreen in a timely manner, even if the X Server doesn't trigger
+    * a flush for us.
+    */
+   intel_batchbuffer_emit_mi_flush(intel->batch);
+
    if (intel->batch->map != intel->batch->ptr)
       intel_batchbuffer_flush(intel->batch);
 }
@@ -453,7 +601,14 @@ intel_init_bufmgr(struct intel_context *intel)
 {
    intelScreenPrivate *intelScreen = intel->intelScreen;
    GLboolean gem_disable = getenv("INTEL_NO_GEM") != NULL;
+   int gem_kernel = 0;
    GLboolean gem_supported;
+   struct drm_i915_getparam gp;
+
+   gp.param = I915_PARAM_HAS_GEM;
+   gp.value = &gem_kernel;
+
+   (void) drmCommandWriteRead(intel->driFd, DRM_I915_GETPARAM, &gp, sizeof(gp));
 
    /* If we've got a new enough DDX that's initializing GEM and giving us
     * object handles for the shared buffers, use that.
@@ -462,7 +617,7 @@ intel_init_bufmgr(struct intel_context *intel)
    if (intel->intelScreen->driScrnPriv->dri2.enabled)
        gem_supported = GL_TRUE;
    else if (intel->intelScreen->driScrnPriv->ddx_version.minor >= 9 &&
-           intel->intelScreen->drmMinor >= 11 &&
+           gem_kernel &&
            intel->intelScreen->front.bo_handle != -1)
        gem_supported = GL_TRUE;
    else
@@ -522,6 +677,7 @@ intelInitDriverFunctions(struct dd_function_table *functions)
    functions->Finish = intelFinish;
    functions->GetString = intelGetString;
    functions->UpdateState = intelInvalidateState;
+   functions->Viewport = intel_viewport;
 
    functions->CopyColorTable = _swrast_CopyColorTable;
    functions->CopyColorSubTable = _swrast_CopyColorSubTable;
@@ -563,6 +719,7 @@ intelInitContext(struct intel_context *intel,
    intel->intelScreen = intelScreen;
    intel->driScreen = sPriv;
    intel->sarea = saPriv;
+   intel->driContext = driContextPriv;
 
    /* Dri stuff */
    intel->hHWContext = driContextPriv->hHWContext;
@@ -764,11 +921,14 @@ intelMakeCurrent(__DRIcontextPrivate * driContextPriv,
       struct intel_framebuffer *intel_fb =
         (struct intel_framebuffer *) driDrawPriv->driverPrivate;
       GLframebuffer *readFb = (GLframebuffer *) driReadPriv->driverPrivate;
-
-
-      /* XXX FBO temporary fix-ups! */
-      /* if the renderbuffers don't have regions, init them from the context */
-      if (!driContextPriv->driScreenPriv->dri2.enabled) {
+      if (driContextPriv->driScreenPriv->dri2.enabled) {     
+          intel_update_renderbuffers(driContextPriv, driDrawPriv);
+          if (driDrawPriv != driReadPriv)
+              intel_update_renderbuffers(driContextPriv, driReadPriv);
+      } else {
+          /* XXX FBO temporary fix-ups! */
+          /* if the renderbuffers don't have regions, init them from the context */
          struct intel_renderbuffer *irbDepth
             = intel_get_renderbuffer(&intel_fb->Base, BUFFER_DEPTH);
          struct intel_renderbuffer *irbStencil
@@ -970,18 +1130,12 @@ void LOCK_HARDWARE( struct intel_context *intel )
        intel_fb->vbl_waited = vbl.reply.sequence;
     }
 
-    DRM_CAS(intel->driHwLock, intel->hHWContext,
-        (DRM_LOCK_HELD|intel->hHWContext), __ret);
+    if (!sPriv->dri2.enabled) {
+       DRM_CAS(intel->driHwLock, intel->hHWContext,
+               (DRM_LOCK_HELD|intel->hHWContext), __ret);
 
-    if (sPriv->dri2.enabled) {
        if (__ret)
-           drmGetLock(intel->driFd, intel->hHWContext, 0);
-       if (__driParseEvents(dPriv->driContextPriv, dPriv)) {
-           intelWindowMoved(intel);
-           intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
-       }
-    } else if (__ret) {
-        intelContendedLock( intel, 0 );
+           intelContendedLock( intel, 0 );
     }
 
 
@@ -994,10 +1148,13 @@ void LOCK_HARDWARE( struct intel_context *intel )
  */
 void UNLOCK_HARDWARE( struct intel_context *intel )
 {
+    __DRIscreen *sPriv = intel->driScreen;
+
    intel->vtbl.note_unlock( intel );
    intel->locked = 0;
 
-   DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
+   if (!sPriv->dri2.enabled)
+      DRM_UNLOCK(intel->driFd, intel->driHwLock, intel->hHWContext);
 
    _glthread_UNLOCK_MUTEX(lockMutex);