Merge branch '7.8'
[mesa.git] / src / mesa / drivers / dri / common / dri_util.c
index d7bcd565d73cde7e8e083b4cacef8c5ef31b505c..f1bbd38612868381b49ba75dfdbaa547cc111ed8 100644 (file)
@@ -47,28 +47,6 @@ const __DRIextension driReadDrawableExtension = {
     __DRI_READ_DRAWABLE, __DRI_READ_DRAWABLE_VERSION
 };
 
-/**
- * Print message to \c stderr if the \c LIBGL_DEBUG environment variable
- * is set. 
- * 
- * Is called from the drivers.
- * 
- * \param f \c printf like format string.
- */
-void
-__driUtilMessage(const char *f, ...)
-{
-    va_list args;
-
-    if (getenv("LIBGL_DEBUG")) {
-        fprintf(stderr, "libGL: ");
-        va_start(args, f);
-        vfprintf(stderr, f, args);
-        va_end(args);
-        fprintf(stderr, "\n");
-    }
-}
-
 GLint
 driIntersectArea( drm_clip_rect_t rect1, drm_clip_rect_t rect2 )
 {
@@ -82,46 +60,6 @@ driIntersectArea( drm_clip_rect_t rect1, drm_clip_rect_t rect2 )
    return (rect1.x2 - rect1.x1) * (rect1.y2 - rect1.y1);
 }
 
-static int driFreeDrawable(__DRIcontext *pcp)
-{
-    __DRIdrawable *pdp;
-    __DRIdrawable *prp;
-
-       if (pcp == NULL)
-               return GL_FALSE;
-
-    pdp = pcp->driDrawablePriv;
-    prp = pcp->driReadablePriv;
-
-    /* already unbound */
-    if (!pdp && !prp)
-      return GL_TRUE;
-
-    if (pdp->refcount == 0) {
-       /* ERROR!!! */
-       return GL_FALSE;
-    }
-
-    dri_put_drawable(pdp);
-
-    if (prp != pdp) {
-        if (prp->refcount == 0) {
-           /* ERROR!!! */
-           return GL_FALSE;
-       }
-
-    dri_put_drawable(prp);
-    }
-
-
-    /* XXX this is disabled so that if we call SwapBuffers on an unbound
-     * window we can determine the last context bound to the window and
-     * use that context's lock. (BrianP, 2-Dec-2000)
-     */
-    pcp->driDrawablePriv = pcp->driReadablePriv = NULL;
-       return GL_TRUE;
-}
-
 /*****************************************************************/
 /** \name Context (un)binding functions                          */
 /*****************************************************************/
@@ -137,7 +75,7 @@ static int driFreeDrawable(__DRIcontext *pcp)
  * 
  * \internal
  * This function calls __DriverAPIRec::UnbindContext, and then decrements
- * __DRIdrawablePrivateRec::refcount which must be non-zero for a successful
+ * __DRIdrawableRec::refcount which must be non-zero for a successful
  * return.
  * 
  * While casting the opaque private pointers associated with the parameters
@@ -146,6 +84,8 @@ static int driFreeDrawable(__DRIcontext *pcp)
 static int driUnbindContext(__DRIcontext *pcp)
 {
     __DRIscreen *psp;
+    __DRIdrawable *pdp;
+    __DRIdrawable *prp;
 
     /*
     ** Assume error checking is done properly in glXMakeCurrent before
@@ -156,14 +96,38 @@ static int driUnbindContext(__DRIcontext *pcp)
         return GL_FALSE;
 
     psp = pcp->driScreenPriv;
+    pdp = pcp->driDrawablePriv;
+    prp = pcp->driReadablePriv;
 
-       /* Let driver unbind drawable from context */
+    /* already unbound */
+    if (!pdp && !prp)
+      return GL_TRUE;
+    /* Let driver unbind drawable from context */
     (*psp->DriverAPI.UnbindContext)(pcp);
 
-#if 0
-    /* Unbind the drawable */
-    pdp->driContextPriv = &psp->dummyContextPriv;
-#endif
+    assert(pdp);
+    if (pdp->refcount == 0) {
+       /* ERROR!!! */
+       return GL_FALSE;
+    }
+
+    dri_put_drawable(pdp);
+
+    if (prp != pdp) {
+        if (prp->refcount == 0) {
+           /* ERROR!!! */
+           return GL_FALSE;
+       }
+
+       dri_put_drawable(prp);
+    }
+
+
+    /* XXX this is disabled so that if we call SwapBuffers on an unbound
+     * window we can determine the last context bound to the window and
+     * use that context's lock. (BrianP, 2-Dec-2000)
+     */
+    pcp->driDrawablePriv = pcp->driReadablePriv = NULL;
 
     return GL_TRUE;
 }
@@ -177,54 +141,29 @@ static int driBindContext(__DRIcontext *pcp,
                          __DRIdrawable *pdp,
                          __DRIdrawable *prp)
 {
-    __DRIscreenPrivate *psp = pcp->driScreenPriv;
+    __DRIscreen *psp = NULL;
 
     /* Bind the drawable to the context */
 
-       if (pcp) {
-
-               if (pcp->driDrawablePriv != pdp 
-                       || pcp->driReadablePriv != prp)
-               {
-                       /* first increment ref count for new drawables */
-
-                       if (pdp)
-                       {
-                               pdp->driContextPriv = pcp;
-                               dri_get_drawable(pdp);
-                       }
-
-                       if (prp && prp != pdp)
-                       {
-                               dri_get_drawable(prp);
-                       }
-
-                       /* free old drawables */ 
-
-                       if (pcp->driReadablePriv 
-                               && pcp->driReadablePriv != pcp->driDrawablePriv)
-                       {
-                               dri_put_drawable(pcp->driReadablePriv);
-                       }
-
-                       if (pcp->driDrawablePriv)
-                       {
-                               dri_put_drawable(pcp->driDrawablePriv);
-                       }
-
-                       /* assign new drawables to context */
-
-                       pcp->driDrawablePriv = pdp;
-                       pcp->driReadablePriv = prp;
-
-               }
+    if (pcp) {
+       psp = pcp->driScreenPriv;
+       pcp->driDrawablePriv = pdp;
+       pcp->driReadablePriv = prp;
+       if (pdp) {
+           pdp->driContextPriv = pcp;
+           dri_get_drawable(pdp);
        }
+       if ( prp && pdp != prp ) {
+           dri_get_drawable(prp);
+       }
+    }
 
     /*
     ** Now that we have a context associated with this drawable, we can
     ** initialize the drawable information if has not been done before.
     */
 
+    assert(psp);
     if (!psp->dri2.enabled) {
        if (pdp && !pdp->pStamp) {
            DRM_SPINLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
@@ -256,7 +195,7 @@ static int driBindContext(__DRIcontext *pcp,
  *
  * \param pdp pointer to the private drawable information to update.
  * 
- * This function basically updates the __DRIdrawablePrivate struct's
+ * This function basically updates the __DRIdrawable struct's
  * cliprect information by calling \c __DRIinterfaceMethods::getDrawableInfo.
  * This is usually called by the DRI_VALIDATE_DRAWABLE_INFO macro which
  * compares the __DRIdrwablePrivate pStamp and lastStamp values.  If
@@ -264,10 +203,10 @@ static int driBindContext(__DRIcontext *pcp,
  * info.
  */
 void
-__driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp)
+__driUtilUpdateDrawableInfo(__DRIdrawable *pdp)
 {
-    __DRIscreenPrivate *psp = pdp->driScreenPriv;
-    __DRIcontextPrivate *pcp = pdp->driContextPriv;
+    __DRIscreen *psp = pdp->driScreenPriv;
+    __DRIcontext *pcp = pdp->driContextPriv;
     
     if (!pcp 
        || ((pdp != pcp->driDrawablePriv) && (pdp != pcp->driReadablePriv))) {
@@ -278,12 +217,12 @@ __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp)
     }
 
     if (pdp->pClipRects) {
-       _mesa_free(pdp->pClipRects); 
+       free(pdp->pClipRects); 
        pdp->pClipRects = NULL;
     }
 
     if (pdp->pBackClipRects) {
-       _mesa_free(pdp->pBackClipRects); 
+       free(pdp->pBackClipRects); 
        pdp->pBackClipRects = NULL;
     }
 
@@ -345,7 +284,7 @@ static void driReportDamage(__DRIdrawable *pdp,
  * \param drawablePrivate opaque pointer to the per-drawable private info.
  * 
  * \internal
- * This function calls __DRIdrawablePrivate::swapBuffers.
+ * This function calls __DRIdrawable::swapBuffers.
  * 
  * Is called directly from glXSwapBuffers().
  */
@@ -360,7 +299,7 @@ static void driSwapBuffers(__DRIdrawable *dPriv)
     if (!dPriv->numClipRects)
         return;
 
-    rects = _mesa_malloc(sizeof(*rects) * dPriv->numClipRects);
+    rects = malloc(sizeof(*rects) * dPriv->numClipRects);
 
     if (!rects)
         return;
@@ -373,7 +312,7 @@ static void driSwapBuffers(__DRIdrawable *dPriv)
     }
 
     driReportDamage(dPriv, rects, dPriv->numClipRects);
-    _mesa_free(rects);
+    free(rects);
 }
 
 static int driDrawableGetMSC( __DRIscreen *sPriv, __DRIdrawable *dPriv,
@@ -466,7 +405,7 @@ driCreateNewDrawable(__DRIscreen *psp, const __DRIconfig *config,
      */
     (void) attrs;
 
-    pdp = _mesa_malloc(sizeof *pdp);
+    pdp = malloc(sizeof *pdp);
     if (!pdp) {
        return NULL;
     }
@@ -489,11 +428,10 @@ driCreateNewDrawable(__DRIscreen *psp, const __DRIconfig *config,
     pdp->vblFlags = 0;
 
     pdp->driScreenPriv = psp;
-    pdp->driContextPriv = &psp->dummyContextPriv;
 
     if (!(*psp->DriverAPI.CreateBuffer)(psp, pdp, &config->modes,
                                        renderType == GLX_PIXMAP_BIT)) {
-       _mesa_free(pdp);
+       free(pdp);
        return NULL;
     }
 
@@ -520,8 +458,11 @@ dri2CreateNewDrawable(__DRIscreen *screen,
     if (!pdraw)
        return NULL;
 
-    pdraw->pClipRects = _mesa_malloc(sizeof *pdraw->pBackClipRects);
-    pdraw->pBackClipRects = _mesa_malloc(sizeof *pdraw->pBackClipRects);
+    pdraw->pClipRects = &pdraw->dri2.clipRect;
+    pdraw->pBackClipRects = &pdraw->dri2.clipRect;
+
+    pdraw->pStamp = &pdraw->dri2.stamp;
+    *pdraw->pStamp = pdraw->lastStamp + 1;
 
     return pdraw;
 }
@@ -533,24 +474,24 @@ static void dri_get_drawable(__DRIdrawable *pdp)
        
 static void dri_put_drawable(__DRIdrawable *pdp)
 {
-    __DRIscreenPrivate *psp;
-
-    pdp->refcount--;
-    if (pdp->refcount)
-       return;
+    __DRIscreen *psp;
 
     if (pdp) {
+       pdp->refcount--;
+       if (pdp->refcount)
+           return;
+
        psp = pdp->driScreenPriv;
         (*psp->DriverAPI.DestroyBuffer)(pdp);
-       if (pdp->pClipRects) {
-           _mesa_free(pdp->pClipRects);
+       if (pdp->pClipRects && pdp->pClipRects != &pdp->dri2.clipRect) {
+           free(pdp->pClipRects);
            pdp->pClipRects = NULL;
        }
-       if (pdp->pBackClipRects) {
-           _mesa_free(pdp->pBackClipRects);
+       if (pdp->pBackClipRects && pdp->pClipRects != &pdp->dri2.clipRect) {
+           free(pdp->pBackClipRects);
            pdp->pBackClipRects = NULL;
        }
-       _mesa_free(pdp);
+       free(pdp);
     }
 }
 
@@ -579,9 +520,8 @@ static void
 driDestroyContext(__DRIcontext *pcp)
 {
     if (pcp) {
-       driFreeDrawable(pcp);
        (*pcp->driScreenPriv->DriverAPI.DestroyContext)(pcp);
-       _mesa_free(pcp);
+       free(pcp);
     }
 }
 
@@ -597,7 +537,7 @@ driDestroyContext(__DRIcontext *pcp)
  *          success, or \c NULL on failure.
  * 
  * \internal
- * This function allocates and fills a __DRIcontextPrivateRec structure.  It
+ * This function allocates and fills a __DRIcontextRec structure.  It
  * performs some device independent initialization and passes all the
  * relevent information to __DriverAPIRec::CreateContext to create the
  * context.
@@ -611,30 +551,21 @@ driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
     __DRIcontext *pcp;
     void * const shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
 
-    pcp = _mesa_malloc(sizeof *pcp);
+    pcp = malloc(sizeof *pcp);
     if (!pcp)
        return NULL;
 
     pcp->driScreenPriv = psp;
     pcp->driDrawablePriv = NULL;
-    pcp->driReadablePriv = NULL;
-
-    /* When the first context is created for a screen, initialize a "dummy"
-     * context.
-     */
-
-    if (!psp->dri2.enabled && !psp->dummyContextPriv.driScreenPriv) {
-        psp->dummyContextPriv.hHWContext = psp->pSAREA->dummy_context;
-        psp->dummyContextPriv.driScreenPriv = psp;
-        psp->dummyContextPriv.driDrawablePriv = NULL;
-        psp->dummyContextPriv.driverPrivate = NULL;
-       /* No other fields should be used! */
-    }
+    pcp->loaderPrivate = data;
+    
+    pcp->dri2.draw_stamp = 0;
+    pcp->dri2.read_stamp = 0;
 
     pcp->hHWContext = hwContext;
 
     if ( !(*psp->DriverAPI.CreateContext)(&config->modes, pcp, shareCtx) ) {
-        _mesa_free(pcp);
+        free(pcp);
         return NULL;
     }
 
@@ -688,7 +619,7 @@ static void driDestroyScreen(__DRIscreen *psp)
           (void)drmCloseOnce(psp->fd);
        }
 
-       _mesa_free(psp);
+       free(psp);
     }
 }
 
@@ -707,6 +638,8 @@ setupLoaderExtensions(__DRIscreen *psp,
            psp->systemTime = (__DRIsystemTimeExtension *) extensions[i];
        if (strcmp(extensions[i]->name, __DRI_DRI2_LOADER) == 0)
            psp->dri2.loader = (__DRIdri2LoaderExtension *) extensions[i];
+       if (strcmp(extensions[i]->name, __DRI_IMAGE_LOOKUP) == 0)
+           psp->dri2.image = (__DRIimageLookupExtension *) extensions[i];
     }
 }
 
@@ -726,7 +659,7 @@ setupLoaderExtensions(__DRIscreen *psp,
  * \param drm_version Version of the kernel DRM.
  * \param frame_buffer Data describing the location and layout of the
  *                     framebuffer.
- * \param pSAREA       Pointer the the SAREA.
+ * \param pSAREA       Pointer to the SAREA.
  * \param fd           Device handle for the DRM.
  * \param extensions   ??
  * \param driver_modes  Returns modes suppoted by the driver
@@ -750,7 +683,7 @@ driCreateNewScreen(int scrn,
     static const __DRIextension *emptyExtensionList[] = { NULL };
     __DRIscreen *psp;
 
-    psp = _mesa_calloc(sizeof *psp);
+    psp = calloc(1, sizeof *psp);
     if (!psp)
        return NULL;
 
@@ -784,18 +717,11 @@ driCreateNewScreen(int scrn,
     psp->myNum = scrn;
     psp->dri2.enabled = GL_FALSE;
 
-    /*
-    ** Do not init dummy context here; actual initialization will be
-    ** done when the first DRI context is created.  Init screen priv ptr
-    ** to NULL to let CreateContext routine that it needs to be inited.
-    */
-    psp->dummyContextPriv.driScreenPriv = NULL;
-
     psp->DriverAPI = driDriverAPI;
 
     *driver_modes = driDriverAPI.InitScreen(psp);
     if (*driver_modes == NULL) {
-       _mesa_free(psp);
+       free(psp);
        return NULL;
     }
 
@@ -817,7 +743,7 @@ dri2CreateNewScreen(int scrn, int fd,
     if (driDriverAPI.InitScreen2 == NULL)
         return NULL;
 
-    psp = _mesa_calloc(sizeof(*psp));
+    psp = calloc(1, sizeof(*psp));
     if (!psp)
        return NULL;
 
@@ -839,7 +765,7 @@ dri2CreateNewScreen(int scrn, int fd,
     psp->DriverAPI = driDriverAPI;
     *driver_configs = driDriverAPI.InitScreen2(psp);
     if (*driver_configs == NULL) {
-       _mesa_free(psp);
+       free(psp);
        return NULL;
     }
 
@@ -879,7 +805,7 @@ const __DRIlegacyExtension driLegacyExtension = {
     driCreateNewContext,
 };
 
-/** Legacy DRI interface */
+/** DRI2 interface */
 const __DRIdri2Extension driDRI2Extension = {
     { __DRI_DRI2, __DRI_DRI2_VERSION },
     dri2CreateNewScreen,
@@ -887,14 +813,6 @@ const __DRIdri2Extension driDRI2Extension = {
     dri2CreateNewContext,
 };
 
-/* This is the table of extensions that the loader will dlsym() for. */
-PUBLIC const __DRIextension *__driDriverExtensions[] = {
-    &driCoreExtension.base,
-    &driLegacyExtension.base,
-    &driDRI2Extension.base,
-    NULL
-};
-
 static int
 driFrameTracking(__DRIdrawable *drawable, GLboolean enable)
 {
@@ -909,7 +827,7 @@ driQueryFrameTracking(__DRIdrawable *dpriv,
    __DRIswapInfo   sInfo;
    int             status;
    int64_t         ust;
-   __DRIscreenPrivate *psp = dpriv->driScreenPriv;
+   __DRIscreen *psp = dpriv->driScreenPriv;
 
    status = dpriv->driScreenPriv->DriverAPI.GetSwapInfo( dpriv, & sInfo );
    if ( status == 0 ) {
@@ -959,14 +877,14 @@ const __DRIframeTrackingExtension driFrameTrackingExtension = {
  *       be possible to cache the sync rate?
  */
 float
-driCalculateSwapUsage( __DRIdrawablePrivate *dPriv, int64_t last_swap_ust,
+driCalculateSwapUsage( __DRIdrawable *dPriv, int64_t last_swap_ust,
                       int64_t current_ust )
 {
    int32_t   n;
    int32_t   d;
    int       interval;
    float     usage = 1.0;
-   __DRIscreenPrivate *psp = dPriv->driScreenPriv;
+   __DRIscreen *psp = dPriv->driScreenPriv;
 
    if ( (*psp->systemTime->getMSCRate)(dPriv, &n, &d, dPriv->loaderPrivate) ) {
       interval = (dPriv->swap_interval != 0) ? dPriv->swap_interval : 1;
@@ -993,4 +911,10 @@ driCalculateSwapUsage( __DRIdrawablePrivate *dPriv, int64_t last_swap_ust,
    return usage;
 }
 
+void
+dri2InvalidateDrawable(__DRIdrawable *drawable)
+{
+    drawable->dri2.stamp++;
+}
+
 /*@}*/