fix missing width/height error
[mesa.git] / src / mesa / drivers / x11 / xm_api.c
index 16963ab71c798ba4909dd76dc5c7c9247807072f..47c19c87a7f32bba6162058d514e5cba0ed213d7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.1
+ * Version:  6.3
  *
  * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
  *
@@ -220,28 +220,6 @@ static int check_for_xshm( XMesaDisplay *display )
 #endif
 
 
-/*
- * Return the width and height of the given drawable.
- */
-static void get_drawable_size( XMesaDisplay *dpy, XMesaDrawable d,
-                               unsigned int *width, unsigned int *height)
-{
-#ifdef XFree86Server
-    (void) dpy;
-    *width = d->width;
-    *height = d->height;
-#else
-   Window root;
-   int x, y;
-   unsigned int bw, depth;
-
-   _glthread_LOCK_MUTEX(_xmesa_lock);
-   XGetGeometry( dpy, d, &root, &x, &y, width, height, &bw, &depth );
-   _glthread_UNLOCK_MUTEX(_xmesa_lock);
-#endif
-}
-
-
 /*
  * Apply gamma correction to an intensity value in [0..max].  Return the
  * new intensity value.
@@ -476,11 +454,16 @@ static GLboolean alloc_shm_back_buffer( XMesaBuffer b )
    GC gc;
    int (*old_handler)( XMesaDisplay *, XErrorEvent * );
 
-   b->backimage = XShmCreateImage( b->xm_visual->display,
-                                   b->xm_visual->visinfo->visual,
-                                   b->xm_visual->visinfo->depth,
-                                  ZPixmap, NULL, &b->shminfo,
-                                  b->width, b->height );
+   if (b->mesa_buffer.Width == 0 || b->mesa_buffer.Height == 0) {
+      /* this will be true the first time we're called on 'b' */
+      return GL_FALSE;
+   }
+
+   b->backimage = XShmCreateImage(b->xm_visual->display,
+                                  b->xm_visual->visinfo->visual,
+                                  b->xm_visual->visinfo->depth,
+                                  ZPixmap, NULL, &b->shminfo,
+                                  b->mesa_buffer.Width, b->mesa_buffer.Height);
    if (b->backimage == NULL) {
       _mesa_warning(NULL, "alloc_back_buffer: Shared memory error (XShmCreateImage), disabling.");
       b->shm = 0;
@@ -606,7 +589,8 @@ void xmesa_alloc_back_buffer( XMesaBuffer b )
       {
         /* Allocate a regular XImage for the back buffer. */
         b->backimage = XMesaCreateImage(b->xm_visual->BitsPerPixel,
-                                        b->width, b->height, NULL);
+                                        b->mesa_buffer.Width,
+                                         b->mesa_buffer.Height, NULL);
 #else
       if (b->shm==0 || alloc_shm_back_buffer(b)==GL_FALSE) {
         /* Allocate a regular XImage for the back buffer. */
@@ -614,7 +598,9 @@ void xmesa_alloc_back_buffer( XMesaBuffer b )
                                       b->xm_visual->visinfo->visual,
                                       GET_VISUAL_DEPTH(b->xm_visual),
                                      ZPixmap, 0,   /* format, offset */
-                                     NULL, b->width, b->height,
+                                     NULL,
+                                      b->mesa_buffer.Width,
+                                      b->mesa_buffer.Height,
                                      8, 0 );  /* pad, bytes_per_line */
 #endif
         if (!b->backimage) {
@@ -638,7 +624,8 @@ void xmesa_alloc_back_buffer( XMesaBuffer b )
       }
       /* Allocate new back pixmap */
       b->backpixmap = XMesaCreatePixmap( b->xm_visual->display, b->frontbuffer,
-                                        b->width, b->height,
+                                        b->mesa_buffer.Width,
+                                         b->mesa_buffer.Height,
                                         GET_VISUAL_DEPTH(b->xm_visual) );
       b->backimage = NULL;
       /* update other references to backpixmap */
@@ -882,8 +869,6 @@ static GLboolean setup_grayscale( int client, XMesaVisual v,
 static GLboolean setup_dithered_color( int client, XMesaVisual v,
                                        XMesaBuffer buffer, XMesaColormap cmap )
 {
-   (void) DitherValues;  /* silence warning */
-
    if (GET_VISUAL_DEPTH(v)<4 || GET_VISUAL_DEPTH(v)>16) {
       return GL_FALSE;
    }
@@ -1250,12 +1235,6 @@ static GLboolean initialize_visual_and_buffer( int client,
    if (b && window) {
       /* Do window-specific initializations */
 
-      /* Window dimensions */
-      unsigned int w, h;
-      get_drawable_size( v->display, window, &w, &h );
-      b->width = w;
-      b->height = h;
-
       b->frontbuffer = window;
 
       /* Setup for single/double buffering */
@@ -1343,6 +1322,8 @@ static GLboolean initialize_visual_and_buffer( int client,
                                   32,                   /*bitmap_pad*/
                                   0                     /*bytes_per_line*/ );
 #endif
+      if (!b->rowimage)
+         return GL_FALSE;
    }
 
    return GL_TRUE;
@@ -1604,6 +1585,7 @@ XMesaVisual XMesaCreateVisual( XMesaDisplay *display,
                             accum_blue_size, accum_alpha_size,
                             0 );
 
+   /* XXX minor hack */
    v->mesa_visual.level = level;
    return v;
 }
@@ -1666,6 +1648,14 @@ XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list )
    _mesa_enable_1_3_extensions(mesaCtx);
    _mesa_enable_1_4_extensions(mesaCtx);
    _mesa_enable_1_5_extensions(mesaCtx);
+   _mesa_enable_2_0_extensions(mesaCtx);
+#if SWTC
+    if (c->Mesa_DXTn) {
+       _mesa_enable_extension(c, "GL_EXT_texture_compression_s3tc");
+       _mesa_enable_extension(c, "GL_S3_s3tc");
+    }
+    _mesa_enable_extension(c, "GL_3DFX_texture_compression_FXT1");
+#endif
 
    /* finish up xmesa context initializations */
    c->swapbytes = CHECK_BYTE_ORDER(v) ? GL_FALSE : GL_TRUE;
@@ -1678,10 +1668,14 @@ XMesaContext XMesaCreateContext( XMesaVisual v, XMesaContext share_list )
 
    /* Initialize the software rasterizer and helper modules.
     */
-   _swrast_CreateContext( mesaCtx );
-   _ac_CreateContext( mesaCtx );
-   _tnl_CreateContext( mesaCtx );
-   _swsetup_CreateContext( mesaCtx );
+   if (!_swrast_CreateContext( mesaCtx ) ||
+       !_ac_CreateContext( mesaCtx ) ||
+       !_tnl_CreateContext( mesaCtx ) ||
+       !_swsetup_CreateContext( mesaCtx )) {
+      _mesa_free_context_data(&c->mesa);
+      _mesa_free(c);
+      return NULL;
+   }
 
    /* tnl setup */
    tnl = TNL_CONTEXT(mesaCtx);
@@ -1791,6 +1785,8 @@ XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v, XMesaWindow w,
                                 v->mesa_visual.stencilBits > 0,
                                 v->mesa_visual.accumRedBits > 0,
                                 v->mesa_visual.alphaBits > 0 );
+   /* XXX hack */
+   b->mesa_buffer.UseSoftwareAuxBuffers = GL_TRUE;
 
    if (!initialize_visual_and_buffer( client, v, b, v->mesa_visual.rgbMode,
                                       (XMesaDrawable)w, b->cmap )) {
@@ -1830,15 +1826,25 @@ XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v, XMesaWindow w,
        }
        attribs[numAttribs++] = FXMESA_NONE;
 
-       /* [dBorca]  we should take an envvar for `fxMesaSelectCurrentBoard'!!! */
-       if ((hw = fxMesaSelectCurrentBoard(0))==GR_SSTTYPE_VOODOO) {
-         b->FXctx = fxMesaCreateBestContext(0, b->width, b->height, attribs);
+       /* [dBorca] we should take an envvar for `fxMesaSelectCurrentBoard'!!! */
+       hw = fxMesaSelectCurrentBoard(0);
+
+       /* if these fail, there's a new bug somewhere */
+       ASSERT(b->mesa_buffer.Width > 0);
+       ASSERT(b->mesa_buffer.Height > 0);
+
+       if ((hw == GR_SSTTYPE_VOODOO) || (hw == GR_SSTTYPE_Voodoo2)) {
+         b->FXctx = fxMesaCreateBestContext(0, b->mesa_buffer.Width,
+                                            b->mesa_buffer.Height, attribs);
          if ((v->undithered_pf!=PF_Index) && (b->backimage)) {
           b->FXisHackUsable = b->FXctx ? GL_TRUE : GL_FALSE;
-          if (fxEnvVar[0]=='w' || fxEnvVar[0]=='W')
-            b->FXwindowHack = b->FXctx ? GL_TRUE : GL_FALSE;
-          else
+          if (b->FXctx && (fxEnvVar[0]=='w' || fxEnvVar[0]=='W')) {
+            b->FXwindowHack = GL_TRUE;
+            FX_grSstControl(GR_CONTROL_DEACTIVATE);
+          }
+           else {
             b->FXwindowHack = GL_FALSE;
+          }
          }
        }
        else {
@@ -1846,7 +1852,8 @@ XMesaBuffer XMesaCreateWindowBuffer2( XMesaVisual v, XMesaWindow w,
           b->FXctx = fxMesaCreateContext(w, GR_RESOLUTION_NONE,
                                          GR_REFRESH_75Hz, attribs);
          else
-          b->FXctx = fxMesaCreateBestContext(0, b->width, b->height, attribs);
+          b->FXctx = fxMesaCreateBestContext(0, b->mesa_buffer.Width,
+                                              b->mesa_buffer.Height, attribs);
          b->FXisHackUsable = GL_FALSE;
          b->FXwindowHack = GL_FALSE;
        }
@@ -2087,13 +2094,6 @@ GLboolean XMesaMakeCurrent2( XMesaContext c, XMesaBuffer drawBuffer,
                           &drawBuffer->mesa_buffer,
                           &readBuffer->mesa_buffer);
 
-      if (c->mesa.Viewport.Width == 0) {
-        /* initialize viewport to window size */
-        _mesa_Viewport( 0, 0, drawBuffer->width, drawBuffer->height );
-        c->mesa.Scissor.Width = drawBuffer->width;
-        c->mesa.Scissor.Height = drawBuffer->height;
-      }
-
       if (c->xm_visual->mesa_visual.rgbMode) {
          /*
           * Must recompute and set these pixel values because colormap
@@ -2210,6 +2210,10 @@ GLboolean XMesaSetFXmode( GLint mode )
          return GL_FALSE;
       }
       if (ctx) {
+         /* [dBorca] Hack alert: 
+         * oh, this is sooo wrong: ctx above is
+         * really an fxMesaContext, not an XMesaContext
+         */
          XMesaContext xmesa = XMESA_CONTEXT(ctx);
          if (mode == XMESA_FX_WINDOW) {
            if (xmesa->xm_draw_buffer->FXisHackUsable) {
@@ -2261,44 +2265,41 @@ static void FXgetImage( XMesaBuffer b )
    XGetGeometry( b->xm_visual->display, b->frontbuffer,
                  &root, &xpos, &ypos, &width, &height, &bw, &depth);
 #endif
-   if (b->width != width || b->height != height) {
-      b->width = MIN2((int)width, b->FXctx->width);
-      b->height = MIN2((int)height, b->FXctx->height);
-      if (b->width & 1)
+   if (b->mesa_buffer.Width != width || b->mesa_buffer.Height != height) {
+      b->mesa_buffer.Width = MIN2((int)width, b->FXctx->width);
+      b->mesa_buffer.Height = MIN2((int)height, b->FXctx->height);
+      if (b->mesa_buffer.Width & 1)
          b->width--;  /* prevent odd width */
       xmesa_alloc_back_buffer( b );
    }
 
-   /* [dBorca]
-    * not needed for Voodoo2 anymore.
-    * should we test fxMesa->bgrOrder?
-    */
+   /* [dBorca] we're always in the right GR_COLORFORMAT... aren't we? */
    /* grLfbWriteColorFormat(GR_COLORFORMAT_ARGB); */
    if (b->xm_visual->undithered_pf==PF_5R6G5B) {
       /* Special case: 16bpp RGB */
       grLfbReadRegion( GR_BUFFER_FRONTBUFFER,       /* src buffer */
-                       0, b->FXctx->height - b->height,  /*pos*/
-                       b->width, b->height,         /* size */
-                       b->width * sizeof(GLushort), /* stride */
+                       0, b->FXctx->height - b->mesa_buffer.Height,  /*pos*/
+                       b->mesa_buffer.Width, b->mesa_buffer.Height,  /* size */
+                       b->mesa_buffer.Width * sizeof(GLushort), /* stride */
                        b->backimage->data);         /* dest buffer */
    }
    else if (b->xm_visual->dithered_pf==PF_Dither
            && GET_VISUAL_DEPTH(b->xm_visual)==8) {
       /* Special case: 8bpp RGB */
-      for (y=0;y<b->height;y++) {
+      for (y=0;y<b->mesa_buffer.Height;y++) {
          GLubyte *ptr = (GLubyte*) b->backimage->data
                         + b->backimage->bytes_per_line * y;
          XDITHER_SETUP(y);
 
          /* read row from 3Dfx frame buffer */
          grLfbReadRegion( GR_BUFFER_FRONTBUFFER,
-                          0, b->FXctx->height-(b->height-y),
-                          b->width, 1,
+                          0, b->FXctx->height-(b->mesa_buffer.Height-y),
+                          b->mesa_buffer.Width, 1,
                           0,
                           pixbuf );
 
          /* write to XImage back buffer */
-         for (x=0;x<b->width;x++) {
+         for (x=0;x<b->mesa_buffer.Width;x++) {
             GLubyte r = (pixbuf[x] & 0xf800) >> 8;
             GLubyte g = (pixbuf[x] & 0x07e0) >> 3;
             GLubyte b = (pixbuf[x] & 0x001f) << 3;
@@ -2308,16 +2309,16 @@ static void FXgetImage( XMesaBuffer b )
    }
    else {
       /* General case: slow! */
-      for (y=0;y<b->height;y++) {
+      for (y=0;y<b->mesa_buffer.Height;y++) {
          /* read row from 3Dfx frame buffer */
          grLfbReadRegion( GR_BUFFER_FRONTBUFFER,
-                          0, b->FXctx->height-(b->height-y),
-                          b->width, 1,
+                          0, b->FXctx->height-(b->mesa_buffer.Height-y),
+                          b->mesa_buffer.Width, 1,
                           0,
                           pixbuf );
 
          /* write to XImage back buffer */
-         for (x=0;x<b->width;x++) {
+         for (x=0;x<b->mesa_buffer.Width;x++) {
             XMesaPutPixel(b->backimage,x,y,
                          xmesa_color_to_pixel(xmesa,
                                               (pixbuf[x] & 0xf800) >> 8,
@@ -2366,7 +2367,8 @@ void XMesaSwapBuffers( XMesaBuffer b )
            XShmPutImage( b->xm_visual->display, b->frontbuffer,
                          b->swapgc,
                          b->backimage, 0, 0,
-                         0, 0, b->width, b->height, False );
+                         0, 0, b->mesa_buffer.Width, b->mesa_buffer.Height,
+                          False );
             /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
         }
         else
@@ -2376,7 +2378,7 @@ void XMesaSwapBuffers( XMesaBuffer b )
             XMesaPutImage( b->xm_visual->display, b->frontbuffer,
                           b->swapgc,
                           b->backimage, 0, 0,
-                          0, 0, b->width, b->height );
+                          0, 0, b->mesa_buffer.Width, b->mesa_buffer.Height );
             /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
          }
       }
@@ -2387,7 +2389,7 @@ void XMesaSwapBuffers( XMesaBuffer b )
                        b->backpixmap,   /* source drawable */
                        b->frontbuffer,  /* dest. drawable */
                        b->swapgc,
-                       0, 0, b->width, b->height,  /* source region */
+                       0, 0, b->mesa_buffer.Width, b->mesa_buffer.Height,
                        0, 0                 /* dest region */
                      );
          /*_glthread_UNLOCK_MUTEX(_xmesa_lock);*/
@@ -2414,7 +2416,7 @@ void XMesaCopySubBuffer( XMesaBuffer b, int x, int y, int width, int height )
       _mesa_notifySwapBuffers(ctx);
 
    if (b->db_state) {
-      int yTop = b->height - y - height;
+      int yTop = b->mesa_buffer.Height - y - height;
 #ifdef FX
       if (b->FXctx) {
          fxMesaSwapBuffers();
@@ -2597,8 +2599,6 @@ unsigned long XMesaDitherColor( XMesaContext xmesa, GLint x, GLint y,
    GLint b = (GLint) (blue  * 255.0F);
    GLint a = (GLint) (alpha * 255.0F);
 
-   (void) DitherValues; /* silence warning */
-
    switch (xmesa->pixelformat) {
       case PF_Index:
          return 0;