Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / mesa / drivers / dri / r128 / r128_screen.c
index f77c325db1922a30b659d258f19b0ae7adfbee05..7cda4ca5d38a952559c607185d5249dd3619671c 100644 (file)
@@ -1,4 +1,3 @@
-/* $XFree86: xc/lib/GL/mesa/src/drv/r128/r128_screen.c,v 1.9 2003/03/26 20:43:49 tsi Exp $ */
 /**************************************************************************
 
 Copyright 1999, 2000 ATI Technologies Inc. and Precision Insight, Inc.,
@@ -37,24 +36,24 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 #include "r128_context.h"
 #include "r128_ioctl.h"
+#include "r128_span.h"
 #include "r128_tris.h"
-#include "r128_vb.h"
 
-#include "context.h"
-#include "imports.h"
+#include "main/context.h"
+#include "main/imports.h"
+#include "main/framebuffer.h"
+#include "main/renderbuffer.h"
 
 #include "utils.h"
 #include "vblank.h"
 
-#ifndef _SOLO
-#include "glxextensions.h"
-#endif
+#include "GL/internal/dri_interface.h"
 
 /* R128 configuration
  */
 #include "xmlpool.h"
 
-const char __driConfigOptions[] =
+PUBLIC const char __driConfigOptions[] =
 DRI_CONF_BEGIN
     DRI_CONF_SECTION_PERFORMANCE
         DRI_CONF_VBLANK_MODE(DRI_CONF_VBLANK_DEF_INTERVAL_0)
@@ -62,18 +61,21 @@ DRI_CONF_BEGIN
     DRI_CONF_SECTION_QUALITY
         DRI_CONF_TEXTURE_DEPTH(DRI_CONF_TEXTURE_DEPTH_FB)
     DRI_CONF_SECTION_END
-#if ENABLE_PERF_BOXES
     DRI_CONF_SECTION_DEBUG
+        DRI_CONF_NO_RAST(false)
+#if ENABLE_PERF_BOXES
         DRI_CONF_PERFORMANCE_BOXES(false)
-    DRI_CONF_SECTION_END
 #endif
+    DRI_CONF_SECTION_END
 DRI_CONF_END;
 #if ENABLE_PERF_BOXES
-static const GLuint __driNConfigOptions = 3;
+static const GLuint __driNConfigOptions = 4;
 #else
-static const GLuint __driNConfigOptions = 2;
+static const GLuint __driNConfigOptions = 3;
 #endif
 
+extern const struct dri_extension card_extensions[];
+
 #if 1
 /* Including xf86PciInfo.h introduces a bunch of errors...
  */
@@ -95,9 +97,12 @@ r128CreateScreen( __DRIscreenPrivate *sPriv )
 {
    r128ScreenPtr r128Screen;
    R128DRIPtr r128DRIPriv = (R128DRIPtr)sPriv->pDevPriv;
+   int i;
 
-   if ( ! driCheckDriDdxDrmVersions( sPriv, "Rage128", 4, 0, 4, 0, 2, 2 ) )
-      return NULL;
+   if (sPriv->devPrivSize != sizeof(R128DRIRec)) {
+      fprintf(stderr,"\nERROR!  sizeof(R128DRIRec) does not match passed size from device driver\n");
+      return GL_FALSE;
+   }
 
    /* Allocate the private area */
    r128Screen = (r128ScreenPtr) CALLOC( sizeof(*r128Screen) );
@@ -113,7 +118,7 @@ r128CreateScreen( __DRIscreenPrivate *sPriv )
    r128Screen->IsPCI = r128DRIPriv->IsPCI;
    r128Screen->sarea_priv_offset = r128DRIPriv->sarea_priv_offset;
    
-   if (sPriv->drmMinor >= 3) {
+   if (sPriv->drm_version.minor >= 3) {
       drm_r128_getparam_t gp;
       int ret;
 
@@ -190,11 +195,19 @@ r128CreateScreen( __DRIscreenPrivate *sPriv )
    r128Screen->depthPitch      = r128DRIPriv->depthPitch;
    r128Screen->spanOffset      = r128DRIPriv->spanOffset;
 
-   r128Screen->texOffset[R128_LOCAL_TEX_HEAP] = r128DRIPriv->textureOffset;
-   r128Screen->texSize[R128_LOCAL_TEX_HEAP] = r128DRIPriv->textureSize;
-   r128Screen->logTexGranularity[R128_LOCAL_TEX_HEAP] = r128DRIPriv->log2TexGran;
+   if ( r128DRIPriv->textureSize == 0 ) {
+      r128Screen->texOffset[R128_LOCAL_TEX_HEAP] =
+        r128DRIPriv->agpTexOffset + R128_AGP_TEX_OFFSET;
+      r128Screen->texSize[R128_LOCAL_TEX_HEAP] = r128DRIPriv->agpTexMapSize;
+      r128Screen->logTexGranularity[R128_LOCAL_TEX_HEAP] =
+        r128DRIPriv->log2AGPTexGran;
+   } else {
+      r128Screen->texOffset[R128_LOCAL_TEX_HEAP] = r128DRIPriv->textureOffset;
+      r128Screen->texSize[R128_LOCAL_TEX_HEAP] = r128DRIPriv->textureSize;
+      r128Screen->logTexGranularity[R128_LOCAL_TEX_HEAP] = r128DRIPriv->log2TexGran;
+   }
 
-   if ( r128Screen->IsPCI ) {
+   if ( !r128Screen->agpTextures.map || r128DRIPriv->textureSize == 0 ) {
       r128Screen->numTexHeaps = R128_NR_TEX_HEAPS - 1;
       r128Screen->texOffset[R128_AGP_TEX_HEAP] = 0;
       r128Screen->texSize[R128_AGP_TEX_HEAP] = 0;
@@ -209,23 +222,16 @@ r128CreateScreen( __DRIscreenPrivate *sPriv )
    }
 
    r128Screen->driScreen = sPriv;
-#ifndef _SOLO
-   if ( driCompareGLXAPIVersion( 20030813 ) >= 0 ) {
-      PFNGLXSCRENABLEEXTENSIONPROC glx_enable_extension =
-          (PFNGLXSCRENABLEEXTENSIONPROC) glXGetProcAddress( (const GLubyte *) "__glXScrEnableExtension" );
-      void * const psc = sPriv->psc->screenConfigs;
-
-      if ( glx_enable_extension != NULL ) {
-        if ( r128Screen->irq != 0 ) {
-           (*glx_enable_extension)( psc, "GLX_SGI_swap_control" );
-           (*glx_enable_extension)( psc, "GLX_SGI_video_sync" );
-           (*glx_enable_extension)( psc, "GLX_MESA_swap_control" );
-        }
-
-        (*glx_enable_extension)( psc, "GLX_MESA_swap_frame_usage" );
-      }
+
+   i = 0;
+   r128Screen->extensions[i++] = &driFrameTrackingExtension.base;
+   if ( r128Screen->irq != 0 ) {
+       r128Screen->extensions[i++] = &driSwapControlExtension.base;
+       r128Screen->extensions[i++] = &driMediaStreamCounterExtension.base;
    }
-#endif
+   r128Screen->extensions[i++] = NULL;
+   sPriv->extensions = r128Screen->extensions;
+
    return r128Screen;
 }
 
@@ -254,15 +260,6 @@ r128DestroyScreen( __DRIscreenPrivate *sPriv )
 }
 
 
-/* Initialize the fullscreen mode.
- */
-static GLboolean
-r128OpenCloseFullScreen( __DRIcontextPrivate *driContextPriv )
-{
-   return GL_TRUE;
-}
-
-
 /* Create and initialize the Mesa and driver specific pixmap buffer
  * data.
  */
@@ -272,16 +269,82 @@ r128CreateBuffer( __DRIscreenPrivate *driScrnPriv,
                   const __GLcontextModes *mesaVis,
                   GLboolean isPixmap )
 {
+   r128ScreenPtr screen = (r128ScreenPtr) driScrnPriv->private;
+
    if (isPixmap) {
       return GL_FALSE; /* not implemented */
    }
    else {
-      driDrawPriv->driverPrivate = (void *) 
-         _mesa_create_framebuffer( mesaVis,
-                                   GL_FALSE,  /* software depth buffer? */
-                                   mesaVis->stencilBits > 0,
-                                   mesaVis->accumRedBits > 0,
-                                   mesaVis->alphaBits > 0 );
+      const GLboolean swDepth = GL_FALSE;
+      const GLboolean swAlpha = GL_FALSE;
+      const GLboolean swAccum = mesaVis->accumRedBits > 0;
+      const GLboolean swStencil = mesaVis->stencilBits > 0 &&
+         mesaVis->depthBits != 24;
+      struct gl_framebuffer *fb = _mesa_create_framebuffer(mesaVis);
+
+      {
+         driRenderbuffer *frontRb
+            = driNewRenderbuffer(GL_RGBA,
+                                 NULL,
+                                 screen->cpp,
+                                 screen->frontOffset, screen->frontPitch,
+                                 driDrawPriv);
+         r128SetSpanFunctions(frontRb, mesaVis);
+         _mesa_add_renderbuffer(fb, BUFFER_FRONT_LEFT, &frontRb->Base);
+      }
+
+      if (mesaVis->doubleBufferMode) {
+         driRenderbuffer *backRb
+            = driNewRenderbuffer(GL_RGBA,
+                                 NULL,
+                                 screen->cpp,
+                                 screen->backOffset, screen->backPitch,
+                                 driDrawPriv);
+         r128SetSpanFunctions(backRb, mesaVis);
+         _mesa_add_renderbuffer(fb, BUFFER_BACK_LEFT, &backRb->Base);
+      }
+
+      if (mesaVis->depthBits == 16) {
+         driRenderbuffer *depthRb
+            = driNewRenderbuffer(GL_DEPTH_COMPONENT16,
+                                 NULL,
+                                 screen->cpp,
+                                 screen->depthOffset, screen->depthPitch,
+                                 driDrawPriv);
+         r128SetSpanFunctions(depthRb, mesaVis);
+         _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
+      }
+      else if (mesaVis->depthBits == 24) {
+         driRenderbuffer *depthRb
+            = driNewRenderbuffer(GL_DEPTH_COMPONENT24,
+                                 NULL,
+                                 screen->cpp,
+                                 screen->depthOffset, screen->depthPitch,
+                                 driDrawPriv);
+         r128SetSpanFunctions(depthRb, mesaVis);
+         _mesa_add_renderbuffer(fb, BUFFER_DEPTH, &depthRb->Base);
+      }
+
+      if (mesaVis->stencilBits > 0 && !swStencil) {
+         driRenderbuffer *stencilRb
+            = driNewRenderbuffer(GL_STENCIL_INDEX8_EXT,
+                                 NULL,
+                                 screen->cpp,
+                                 screen->depthOffset, screen->depthPitch,
+                                 driDrawPriv);
+         r128SetSpanFunctions(stencilRb, mesaVis);
+         _mesa_add_renderbuffer(fb, BUFFER_STENCIL, &stencilRb->Base);
+      }
+
+      _mesa_add_soft_renderbuffers(fb,
+                                   GL_FALSE, /* color */
+                                   swDepth,
+                                   swStencil,
+                                   swAccum,
+                                   swAlpha,
+                                   GL_FALSE /* aux */);
+      driDrawPriv->driverPrivate = (void *) fb;
+
       return (driDrawPriv->driverPrivate != NULL);
    }
 }
@@ -290,7 +353,7 @@ r128CreateBuffer( __DRIscreenPrivate *driScrnPriv,
 static void
 r128DestroyBuffer(__DRIdrawablePrivate *driDrawPriv)
 {
-   _mesa_destroy_framebuffer((GLframebuffer *) (driDrawPriv->driverPrivate));
+   _mesa_unreference_framebuffer((GLframebuffer **)(&(driDrawPriv->driverPrivate)));
 }
 
 
@@ -335,33 +398,125 @@ r128InitDriver( __DRIscreenPrivate *sPriv )
    return GL_TRUE;
 }
 
-#ifndef _SOLO
+static const __DRIconfig **
+r128FillInModes( __DRIscreenPrivate *psp,
+                unsigned pixel_bits, unsigned depth_bits,
+                unsigned stencil_bits, GLboolean have_back_buffer )
+{
+    __DRIconfig **configs;
+    __GLcontextModes * m;
+    unsigned depth_buffer_factor;
+    unsigned back_buffer_factor;
+    GLenum fb_format;
+    GLenum fb_type;
+    int i;
+
+    /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
+     * enough to add support.  Basically, if a context is created with an
+     * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
+     * will never be used.
+     */
+    static const GLenum back_buffer_modes[] = {
+       GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
+    };
+
+    uint8_t depth_bits_array[2];
+    uint8_t stencil_bits_array[2];
+    uint8_t msaa_samples_array[1];
+
+    depth_bits_array[0] = depth_bits;
+    depth_bits_array[1] = depth_bits;
+    
+    /* Just like with the accumulation buffer, always provide some modes
+     * with a stencil buffer.  It will be a sw fallback, but some apps won't
+     * care about that.
+     */
+    stencil_bits_array[0] = 0;
+    stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
+
+    msaa_samples_array[0] = 0;
+
+    depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
+    back_buffer_factor  = (have_back_buffer) ? 2 : 1;
+
+    if ( pixel_bits == 16 ) {
+        fb_format = GL_RGB;
+        fb_type = GL_UNSIGNED_SHORT_5_6_5;
+    }
+    else {
+        fb_format = GL_BGR;
+        fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
+    }
+
+    configs = driCreateConfigs(fb_format, fb_type,
+                               depth_bits_array, stencil_bits_array,
+                               depth_buffer_factor, back_buffer_modes,
+                               back_buffer_factor,
+                               msaa_samples_array, 1);
+    if (configs == NULL) {
+        fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
+                __LINE__);
+        return NULL;
+    }
+
+    /* Mark the visual as slow if there are "fake" stencil bits.
+     */
+    for (i = 0; configs[i]; i++) {
+        m = &configs[i]->modes;
+        if ((m->stencilBits != 0) && (m->stencilBits != stencil_bits)) {
+            m->visualRating = GLX_SLOW_CONFIG;
+        }
+    }
+
+    return (const __DRIconfig **) configs;
+}
+
+
 /**
- * This function is called by libGL.so as soon as libGL.so is loaded.
- * This is where we register new extension functions with the dispatcher.
+ * This is the driver specific part of the createNewScreen entry point.
+ * 
+ * \todo maybe fold this into intelInitDriver
  *
- * \todo This interface has been deprecated, so we should probably remove
- *       this function before the next XFree86 release.
+ * \return the __GLcontextModes supported by this driver
  */
-void __driRegisterExtensions( void )
+static const __DRIconfig **
+r128InitScreen(__DRIscreenPrivate *psp)
 {
-   PFNGLXENABLEEXTENSIONPROC glx_enable_extension;
+   static const __DRIversion ddx_expected = { 4, 0, 0 };
+   static const __DRIversion dri_expected = { 4, 0, 0 };
+   static const __DRIversion drm_expected = { 2, 2, 0 };
+   R128DRIPtr dri_priv = (R128DRIPtr) psp->pDevPriv;
+
+   if ( ! driCheckDriDdxDrmVersions2( "Rage128",
+                                     &psp->dri_version, & dri_expected,
+                                     &psp->ddx_version, & ddx_expected,
+                                     &psp->drm_version, & drm_expected ) )
+      return NULL;
 
-   if ( driCompareGLXAPIVersion( 20030317 ) >= 0 ) {
-      glx_enable_extension = (PFNGLXENABLEEXTENSIONPROC)
-         glXGetProcAddress( (const GLubyte *) "__glXEnableExtension" );
+   /* Calling driInitExtensions here, with a NULL context pointer,
+    * does not actually enable the extensions.  It just makes sure
+    * that all the dispatch offsets for all the extensions that
+    * *might* be enables are known.  This is needed because the
+    * dispatch offsets need to be known when _mesa_context_create is
+    * called, but we can't enable the extensions until we have a
+    * context pointer.
+    *
+    * Hello chicken.  Hello egg.  How are you two today?
+    */
+   driInitExtensions( NULL, card_extensions, GL_FALSE );
 
-      if ( glx_enable_extension != NULL ) {
-        glx_enable_extension( "GLX_SGI_swap_control", GL_FALSE );
-        glx_enable_extension( "GLX_SGI_video_sync", GL_FALSE );
-        glx_enable_extension( "GLX_MESA_swap_control", GL_FALSE );
-      }
-   }
+   if (!r128InitDriver(psp))
+       return NULL;
+
+   return r128FillInModes( psp,
+                          dri_priv->bpp,
+                          (dri_priv->bpp == 16) ? 16 : 24,
+                          (dri_priv->bpp == 16) ? 0  : 8,
+                          (dri_priv->backOffset != dri_priv->depthOffset) );
 }
-#endif
 
-static struct __DriverAPIRec r128API = {
-   .InitDriver      = r128InitDriver,
+const struct __DriverAPIRec driDriverAPI = {
+   .InitScreen      = r128InitScreen,
    .DestroyScreen   = r128DestroyScreen,
    .CreateContext   = r128CreateContext,
    .DestroyContext  = r128DestroyContext,
@@ -370,36 +525,9 @@ static struct __DriverAPIRec r128API = {
    .SwapBuffers     = r128SwapBuffers,
    .MakeCurrent     = r128MakeCurrent,
    .UnbindContext   = r128UnbindContext,
-   .OpenFullScreen  = r128OpenCloseFullScreen,
-   .CloseFullScreen = r128OpenCloseFullScreen,
    .GetSwapInfo     = NULL,
-   .GetMSC          = driGetMSC32,
+   .GetDrawableMSC  = driDrawableGetMSC32,
    .WaitForMSC      = driWaitForMSC32,
    .WaitForSBC      = NULL,
    .SwapBuffersMSC  = NULL
-
 };
-
-
-/*
- * This is the bootstrap function for the driver.
- * The __driCreateScreen name is the symbol that libGL.so fetches.
- * Return:  pointer to a __DRIscreenPrivate.
- */
-#ifndef _SOLO 
-void *__driCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
-                        int numConfigs, __GLXvisualConfig *config)
-{
-   __DRIscreenPrivate *psp;
-   psp = __driUtilCreateScreen(dpy, scrn, psc, numConfigs, config, &r128API);
-   return (void *) psp;
-}
-#else
-void *__driCreateScreen(struct DRIDriverRec *driver,
-                        struct DRIDriverContextRec *driverContext)
-{
-   __DRIscreenPrivate *psp;
-   psp = __driUtilCreateScreen(driver, driverContext, &r128API);
-   return (void *) psp;
-}
-#endif