Merge branch 'llvm-cliptest-viewport'
[mesa.git] / src / mesa / drivers / dri / common / dri_util.c
index 75c98825b79e178bc31aedfc6087b9600dab0431..a5b71bd40adb51c683b65a1fefa9e6acfd99c17f 100644 (file)
 #include "dri_util.h"
 #include "drm_sarea.h"
 #include "utils.h"
+#include "xmlpool.h"
+#include "../glsl/glsl_parser_extras.h"
+
+PUBLIC const char __dri2ConfigOptions[] =
+   DRI_CONF_BEGIN
+      DRI_CONF_SECTION_PERFORMANCE
+         DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_1)
+      DRI_CONF_SECTION_END
+   DRI_CONF_END;
+
+static const uint __dri2NConfigOptions = 1;
 
 #ifndef GLX_OML_sync_control
 typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC) (__DRIdrawable *drawable, int32_t *numerator, int32_t *denominator);
@@ -47,28 +58,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 )
 {
@@ -151,11 +140,6 @@ static int driUnbindContext(__DRIcontext *pcp)
      */
     pcp->driDrawablePriv = pcp->driReadablePriv = NULL;
 
-#if 0
-    /* Unbind the drawable */
-    pdp->driContextPriv = &psp->dummyContextPriv;
-#endif
-
     return GL_TRUE;
 }
 
@@ -170,19 +154,24 @@ static int driBindContext(__DRIcontext *pcp,
 {
     __DRIscreen *psp = NULL;
 
-    /* Bind the drawable to the context */
+    /*
+    ** Assume error checking is done properly in glXMakeCurrent before
+    ** calling driUnbindContext.
+    */
 
-    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);
-       }
+    if (!pcp)
+       return GL_FALSE;
+
+    /* Bind the drawable to the context */
+    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);
     }
 
     /*
@@ -190,7 +179,6 @@ static int driBindContext(__DRIcontext *pcp,
     ** 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);
@@ -205,7 +193,6 @@ static int driBindContext(__DRIcontext *pcp,
     }
 
     /* Call device-specific MakeCurrent */
-
     return (*psp->DriverAPI.MakeCurrent)(pcp, pdp, prp);
 }
 
@@ -437,6 +424,7 @@ driCreateNewDrawable(__DRIscreen *psp, const __DRIconfig *config,
        return NULL;
     }
 
+    pdp->driContextPriv = NULL;
     pdp->loaderPrivate = data;
     pdp->hHWDrawable = hwDrawable;
     pdp->refcount = 1;
@@ -455,10 +443,8 @@ 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)) {
+    if (!(*psp->DriverAPI.CreateBuffer)(psp, pdp, &config->modes, 0)) {
        free(pdp);
        return NULL;
     }
@@ -495,6 +481,41 @@ dri2CreateNewDrawable(__DRIscreen *screen,
     return pdraw;
 }
 
+static int
+dri2ConfigQueryb(__DRIscreen *screen, const char *var, GLboolean *val)
+{
+   if (!driCheckOption(&screen->optionCache, var, DRI_BOOL))
+      return -1;
+
+   *val = driQueryOptionb(&screen->optionCache, var);
+
+   return 0;
+}
+
+static int
+dri2ConfigQueryi(__DRIscreen *screen, const char *var, GLint *val)
+{
+   if (!driCheckOption(&screen->optionCache, var, DRI_INT) &&
+       !driCheckOption(&screen->optionCache, var, DRI_ENUM))
+      return -1;
+
+    *val = driQueryOptioni(&screen->optionCache, var);
+
+    return 0;
+}
+
+static int
+dri2ConfigQueryf(__DRIscreen *screen, const char *var, GLfloat *val)
+{
+   if (!driCheckOption(&screen->optionCache, var, DRI_FLOAT))
+      return -1;
+
+    *val = driQueryOptionf(&screen->optionCache, var);
+
+    return 0;
+}
+
+
 static void dri_get_drawable(__DRIdrawable *pdp)
 {
     pdp->refcount++;
@@ -589,21 +610,11 @@ driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
     
     pcp->dri2.draw_stamp = 0;
     pcp->dri2.read_stamp = 0;
-    /* 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->hHWContext = hwContext;
 
-    if ( !(*psp->DriverAPI.CreateContext)(&config->modes, pcp, shareCtx) ) {
+    if ( !(*psp->DriverAPI.CreateContext)(API_OPENGL,
+                                         &config->modes, pcp, shareCtx) ) {
         free(pcp);
         return NULL;
     }
@@ -611,15 +622,65 @@ driCreateNewContext(__DRIscreen *psp, const __DRIconfig *config,
     return pcp;
 }
 
+static unsigned int
+dri2GetAPIMask(__DRIscreen *screen)
+{
+    return screen->api_mask;
+}
+
+static __DRIcontext *
+dri2CreateNewContextForAPI(__DRIscreen *screen, int api,
+                          const __DRIconfig *config,
+                          __DRIcontext *shared, void *data)
+{
+    __DRIcontext *context;
+    const struct gl_config *modes = (config != NULL) ? &config->modes : NULL;
+    void *shareCtx = (shared != NULL) ? shared->driverPrivate : NULL;
+    gl_api mesa_api;
+
+    if (!(screen->api_mask & (1 << api)))
+       return NULL;
+
+    switch (api) {
+    case __DRI_API_OPENGL:
+           mesa_api = API_OPENGL;
+           break;
+    case __DRI_API_GLES:
+           mesa_api = API_OPENGLES;
+           break;
+    case __DRI_API_GLES2:
+           mesa_api = API_OPENGLES2;
+           break;
+    default:
+           return NULL;
+    }
+
+    context = malloc(sizeof *context);
+    if (!context)
+       return NULL;
+
+    context->driScreenPriv = screen;
+    context->driDrawablePriv = NULL;
+    context->loaderPrivate = data;
+    
+    if (!(*screen->DriverAPI.CreateContext)(mesa_api, modes,
+                                           context, shareCtx) ) {
+        free(context);
+        return NULL;
+    }
+
+    return context;
+}
+
 
 static __DRIcontext *
 dri2CreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
                      __DRIcontext *shared, void *data)
 {
-    return driCreateNewContext(screen, config, 0, shared, 0, data);
+   return dri2CreateNewContextForAPI(screen, __DRI_API_OPENGL,
+                                    config, shared, data);
 }
 
-
 static int
 driCopyContext(__DRIcontext *dest, __DRIcontext *src, unsigned long mask)
 {
@@ -649,6 +710,8 @@ static void driDestroyScreen(__DRIscreen *psp)
         * stream open to the X-server anymore.
         */
 
+       _mesa_destroy_shader_compiler();
+
        if (psp->DriverAPI.DestroyScreen)
            (*psp->DriverAPI.DestroyScreen)(psp);
 
@@ -656,6 +719,9 @@ static void driDestroyScreen(__DRIscreen *psp)
           (void)drmUnmap((drmAddress)psp->pSAREA, SAREA_MAX);
           (void)drmUnmap((drmAddress)psp->pFB, psp->fbSize);
           (void)drmCloseOnce(psp->fd);
+       } else {
+          driDestroyOptionCache(&psp->optionCache);
+          driDestroyOptionInfo(&psp->optionInfo);
        }
 
        free(psp);
@@ -679,6 +745,8 @@ setupLoaderExtensions(__DRIscreen *psp,
            psp->dri2.loader = (__DRIdri2LoaderExtension *) extensions[i];
        if (strcmp(extensions[i]->name, __DRI_IMAGE_LOOKUP) == 0)
            psp->dri2.image = (__DRIimageLookupExtension *) extensions[i];
+       if (strcmp(extensions[i]->name, __DRI_USE_INVALIDATE) == 0)
+           psp->dri2.useInvalidate = (__DRIuseInvalidateExtension *) extensions[i];
     }
 }
 
@@ -686,7 +754,7 @@ setupLoaderExtensions(__DRIscreen *psp,
  * This is the bootstrap function for the driver.  libGL supplies all of the
  * requisite information about the system, and the driver initializes itself.
  * This routine also fills in the linked list pointed to by \c driver_modes
- * with the \c __GLcontextModes that the driver can support for windows or
+ * with the \c struct gl_config that the driver can support for windows or
  * pbuffers.
  *
  * For legacy DRI.
@@ -756,14 +824,8 @@ 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;
+    psp->api_mask = (1 << __DRI_API_OPENGL);
 
     *driver_modes = driDriverAPI.InitScreen(psp);
     if (*driver_modes == NULL) {
@@ -809,6 +871,7 @@ dri2CreateNewScreen(int scrn, int fd,
     psp->dri2.enabled = GL_TRUE;
 
     psp->DriverAPI = driDriverAPI;
+    psp->api_mask = (1 << __DRI_API_OPENGL);
     *driver_configs = driDriverAPI.InitScreen2(psp);
     if (*driver_configs == NULL) {
        free(psp);
@@ -816,6 +879,12 @@ dri2CreateNewScreen(int scrn, int fd,
     }
 
     psp->DriverAPI = driDriverAPI;
+    psp->loaderPrivate = data;
+
+    driParseOptionInfo(&psp->optionInfo, __dri2ConfigOptions,
+                      __dri2NConfigOptions);
+    driParseConfigFiles(&psp->optionCache, &psp->optionInfo, psp->myNum,
+                       "dri2");
 
     return psp;
 }
@@ -857,41 +926,15 @@ const __DRIdri2Extension driDRI2Extension = {
     dri2CreateNewScreen,
     dri2CreateNewDrawable,
     dri2CreateNewContext,
+    dri2GetAPIMask,
+    dri2CreateNewContextForAPI
 };
 
-static int
-driFrameTracking(__DRIdrawable *drawable, GLboolean enable)
-{
-    return GLX_BAD_CONTEXT;
-}
-
-static int
-driQueryFrameTracking(__DRIdrawable *dpriv,
-                     int64_t * sbc, int64_t * missedFrames,
-                     float * lastMissedUsage, float * usage)
-{
-   __DRIswapInfo   sInfo;
-   int             status;
-   int64_t         ust;
-   __DRIscreen *psp = dpriv->driScreenPriv;
-
-   status = dpriv->driScreenPriv->DriverAPI.GetSwapInfo( dpriv, & sInfo );
-   if ( status == 0 ) {
-      *sbc = sInfo.swap_count;
-      *missedFrames = sInfo.swap_missed_count;
-      *lastMissedUsage = sInfo.swap_missed_usage;
-
-      (*psp->systemTime->getUST)( & ust );
-      *usage = driCalculateSwapUsage( dpriv, sInfo.swap_ust, ust );
-   }
-
-   return status;
-}
-
-const __DRIframeTrackingExtension driFrameTrackingExtension = {
-    { __DRI_FRAME_TRACKING, __DRI_FRAME_TRACKING_VERSION },
-    driFrameTracking,
-    driQueryFrameTracking    
+const __DRI2configQueryExtension dri2ConfigQueryExtension = {
+   { __DRI2_CONFIG_QUERY, __DRI2_CONFIG_QUERY_VERSION },
+   dri2ConfigQueryb,
+   dri2ConfigQueryi,
+   dri2ConfigQueryf,
 };
 
 /**