Revert "r300: Gracefully exit after GART memory is exhausted."
[mesa.git] / src / mesa / drivers / ggi / default / linear.c
index 6acdf4d801212180f0e9a14274dd40d9c45de317..9d29761ad5b45336b16ac529ba009950f083acd3 100644 (file)
@@ -24,6 +24,8 @@
 
 #include <ggi/mesa/ggimesa.h>
 #include <ggi/mesa/ggimesa_int.h>
+#include <ggi/mesa/debug.h>
+#include "swrast/swrast.h"
 
 #define RMASK ((1<<R)-1)
 #define GMASK ((1<<G)-1)
 #define GS (8-G)
 #define BS (8-B)
 
+#define PACK(color) (((color[RCOMP]>>RS) << (G+B)) |  \
+                     ((color[GCOMP]>>GS) << B)     |  \
+                     ((color[BCOMP]>>BS)))
+
+#define FLIP(coord) (LIBGGI_VIRTY(ggi_ctx->ggi_visual) - (coord) - 1)
+
 
 /**********************************************************************/
 /*****            Write spans of pixels                           *****/
 /**********************************************************************/
 
-void GGIwrite_ci32_span(const GLcontext *ctx,
-                         GLuint n, GLint x, GLint y,
-                         const GLuint ci[],
-                         const GLubyte mask[])
+void GGIwrite_ci32_span(const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                       const GLuint ci[], const GLubyte mask[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,x,FLIP(y));
-       if (mask)
-       {
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       FB_TYPE *fb;
+       fb = (FB_TYPE *)((char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual) +
+                        FLIP(y)*LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual)) + x;
+
+       if (mask) {
                while (n--) {
-                       if (*mask++) *fb=*ci;
+                       if (*mask++)
+                               *fb = *ci;
                        fb++;
                        ci++;
                }
-       }
-       else
-       {
+       } else {
                while (n--) *fb++ = *ci++;
        }
 }
 
-void GGIwrite_ci8_span(const GLcontext *ctx,
-                         GLuint n, GLint x, GLint y,
-                         const GLubyte ci[],
-                         const GLubyte mask[] )
+void GGIwrite_ci8_span(const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                      const GLubyte ci[], const GLubyte mask[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,x,FLIP(y));
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       FB_TYPE *fb;
+       fb = (FB_TYPE *)((char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual) +
+                        FLIP(y)*LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual)) + x;
 
-       if (mask)
-       {
+       if (mask) {
                while (n--) {
-                       if (*mask++) *fb=*ci;
+                       if (*mask++)
+                               *fb = *ci;
                        fb++;
                        ci++;
                }       
-       }
-       else
-       {
+       } else {
                while (n--) *fb++ = *ci++;
        }
 }
 
 
-void GGIwrite_rgba_span(const GLcontext *ctx,
-                          GLuint n, GLint x, GLint y,
-                          const GLubyte rgba[][4],
-                          const GLubyte mask[])
+void GGIwrite_rgba_span(const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                        const GLchan rgba[][4], const GLubyte mask[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,x,FLIP(y));
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       FB_TYPE *fb;
+       fb = (FB_TYPE *)((char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual) +
+                        FLIP(y)*LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual)) + x;
 
-       if (mask)
-       {
+       if (mask) {
                while (n--) {
-                       if (*mask++) {
-                               *fb=    ((rgba[0][RCOMP]>>RS) << (G+B)) | 
-                                       ((rgba[0][GCOMP]>>GS) << B) |
-                                       ((rgba[0][BCOMP]>>BS));
-                       }
+                       if (*mask++)
+                               *fb = PACK(rgba[0]);
                        fb++;
                        rgba++;
                }
-       }
-       else
-       {
+       } else {
                while (n--) {
-                       *fb++=  ((rgba[0][RCOMP]>>RS) << (G+B)) | 
-                               ((rgba[0][GCOMP]>>GS) << B)|
-                               ((rgba[0][BCOMP]>>BS));
+                       *fb++ = PACK(rgba[0]);
                        rgba++;
                }
        }
 }
 
-void GGIwrite_rgb_span( const GLcontext *ctx,
-                          GLuint n, GLint x, GLint y,
-                          const GLubyte rgba[][3],
-                          const GLubyte mask[] )
+void GGIwrite_rgb_span(const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                      const GLchan rgba[][3], const GLubyte mask[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,x,FLIP(y));
-       if (mask)
-       {
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       FB_TYPE *fb;
+       fb = (FB_TYPE *)((char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual) +
+                        FLIP(y)*LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual)) + x;
+
+       if (mask) {
                while (n--) {
-                       if (*mask++) {
-                               *fb=    ((rgba[0][RCOMP]>>RS) << (G+B)) | 
-                                       ((rgba[0][GCOMP]>>GS) << B) |
-                                       ((rgba[0][BCOMP]>>BS));
-                       }
+                       if (*mask++)
+                               *fb = PACK(rgba[0]);
                        fb++;
                        rgba++;
                }
-       }
-       else
-       {
+       } else {
                while (n--) {
-                       *fb++=  ((rgba[0][RCOMP]>>RS) << (G+B)) | 
-                               ((rgba[0][GCOMP]>>GS) << B) |
-                               ((rgba[0][BCOMP]>>BS));
+                       *fb++ = PACK(rgba[0]);
                        rgba++;
                }
        }
 }
 
 
-void GGIwrite_mono_span( const GLcontext *ctx,
-                              GLuint n, GLint x, GLint y,
-                              const GLubyte mask[])
+void GGIwrite_mono_rgba_span(const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                            const GLchan color[4], const GLubyte mask[])
 {
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
        FB_TYPE *fb;
-       FB_TYPE color;
+       fb = (FB_TYPE *)((char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual) +
+                        FLIP(y)*LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual)) + x;
+
+       if (mask) {
+               while (n--){
+                       if (*mask++)
+                               *fb = PACK(color);
+                       ++fb;
+               }
+       } else {
+               while (n--) 
+                       *fb++ = PACK(color);
 
-       if (mask)
-       {
-               fb=LFB(FB_TYPE,x,FLIP(y));
-               color=(FB_TYPE) GGICTX->color;
+               /* Alternatively we could write a potentialy faster HLine
+               ggiSetGCForeground(ggi_ctx->ggi_visual, color);
+               ggiDrawHLine(ggi_ctx->ggi_visual,x,FLIP(y),n);
+               */
+       }
+}
 
-               while (n--) 
-               {
-                       if (*mask++) *fb=color; 
-                       fb++;
+void GGIwrite_mono_ci_span(const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                          const GLuint ci, const GLubyte mask[])
+{
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       FB_TYPE *fb;
+       fb = (FB_TYPE *)((char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual) +
+                        FLIP(y)*LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual)) + x;
+
+       if (mask){
+               while (n--){
+                       if (*mask++)
+                               *fb = ci;
+                       ++fb;
                }
-       }
-       else
-       {
-               ggiDrawHLine(VIS,x,FLIP(y),n);
+       } else {
+               while (n--) 
+                       *fb++ = ci;
+
+               /* Alternatively we could write a potentialy faster HLine
+               ggiSetGCForeground(ggi_ctx->ggi_visual, ci);
+               ggiDrawHLine(ggi_ctx->ggi_visual, x, FLIP(y), n);
+               */
        }
 }
 
@@ -171,27 +191,32 @@ void GGIwrite_mono_span( const GLcontext *ctx,
 
 
 void GGIread_ci32_span(const GLcontext *ctx,
-                         GLuint n, GLint x, GLint y, GLuint ci[])
+                      GLuint n, GLint x, GLint y, GLuint ci[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,x,FLIP(y));
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       FB_TYPE *fb;
+       fb = (FB_TYPE *)((char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual) +
+                        FLIP(y)*LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual)) + x;
+
        while (n--)
-               *ci++=(GLuint)*fb++;
+               *ci++ = (GLuint)*fb++;
 }
 
 void GGIread_rgba_span(const GLcontext *ctx,
-                         GLuint n, GLint x, GLint y,
-                         GLubyte rgba[][4])
+                      GLuint n, GLint x, GLint y, GLchan rgba[][4])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,x,FLIP(y));
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
        FB_TYPE color;
-
-       while (n--)
-       {
-               color=*fb++;
+       FB_TYPE *fb;
+       fb = (FB_TYPE *)((char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual) +
+                        FLIP(y)*LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual)) + x;
+       
+       while (n--) {
+               color = *fb++;
                rgba[0][RCOMP] = (GLubyte) (color>>(G+B))<<RS;  
                rgba[0][GCOMP] = (GLubyte) ((color>>B)& ((1<<G)-1))<<GS;  
                rgba[0][BCOMP] = (GLubyte) (color & ((1<<B)-1))<<BS;  
-               rgba[0][ACOMP] =0;
+               rgba[0][ACOMP] = 0;
                rgba++;
        }
 }
@@ -201,68 +226,97 @@ void GGIread_rgba_span(const GLcontext *ctx,
 /**********************************************************************/
 
 void GGIwrite_ci32_pixels(const GLcontext *ctx,
-                            GLuint n, const GLint x[], const GLint y[],
-                            const GLuint ci[], const GLubyte mask[] )
+                         GLuint n, const GLint x[], const GLint y[],
+                         const GLuint ci[], const GLubyte mask[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,0,0);
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       int stride = LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual);
+       char *fb = (char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual);
 
        while (n--) {
-               if (*mask++) *(fb+ *x + FLIP(*y)*GGICTX->width)=*ci;
+               if (*mask++){
+                       FB_TYPE *dst = (FB_TYPE*)(fb + FLIP(*y)*stride) + *x;
+                       *dst = *ci;
+               }
                ci++;
                x++;
                y++;
        }
 }
 
-void GGIwrite_mono_pixels(const GLcontext *ctx,
-                                GLuint n,
-                                const GLint x[], const GLint y[],
-                                const GLubyte mask[] )
+void GGIwrite_mono_ci_pixels(const GLcontext *ctx,
+                            GLuint n, const GLint x[], const GLint y[],
+                            GLuint ci, const GLubyte mask[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,0,0);
-       FB_TYPE color=(FB_TYPE) GGICTX->color;
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       int stride = LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual);
+       char *fb = (char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual);
 
        while (n--) {
-               if (*mask++) *(fb+ *x + FLIP(*y)*GGICTX->width)=color;
+               if (*mask++){
+                       FB_TYPE *dst = (FB_TYPE*)(fb + FLIP(*y)*stride) + *x;
+                       *dst = ci;
+               }
                x++;
                y++;
        }
 }
 
 void GGIwrite_rgba_pixels(const GLcontext *ctx,
-                            GLuint n, const GLint x[], const GLint y[],
-                            const GLubyte rgba[][4],
-                            const GLubyte mask[] )
+                         GLuint n, const GLint x[], const GLint y[],
+                         const GLchan rgba[][4], const GLubyte mask[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,0,0);
-       FB_TYPE color;
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       int stride = LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual);
+       char *fb = (char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual);
 
        while (n--) {
-               if (*mask++) {
-                       color=  ((rgba[0][RCOMP]>>RS) << (G+B)) | 
-                               ((rgba[0][GCOMP]>>GS) << B) |
-                               ((rgba[0][BCOMP]>>BS));
-                        *(fb+ *x + FLIP(*y)*GGICTX->width)=color;
+               if (*mask++){
+                       FB_TYPE *dst = (FB_TYPE*)(fb + FLIP(*y)*stride) + *x;
+                       *dst = PACK(rgba[0]);
                }
-               x++;y++;
+               x++;
+               y++;
                rgba++;
        }
 }
 
+void GGIwrite_mono_rgba_pixels(const GLcontext *ctx,
+                              GLuint n, const GLint x[], const GLint y[],
+                              const GLchan rgba[4], const GLubyte mask[])
+{
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       int stride = LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual);
+       char *fb = (char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual);
+
+       while (n--) {
+               if (*mask++){
+                       FB_TYPE *dst = (FB_TYPE*)(fb + FLIP(*y)*stride) + *x;
+                       *dst = PACK(rgba);
+               }
+               
+               x++;
+               y++;
+       }
+}
 
 /**********************************************************************/
 /*****                   Read arrays of pixels                    *****/
 /**********************************************************************/
 
 void GGIread_ci32_pixels(const GLcontext *ctx,
-                           GLuint n, const GLint x[], const GLint y[],
-                           GLuint ci[], const GLubyte mask[])
+                        GLuint n, const GLint x[], const GLint y[],
+                        GLuint ci[], const GLubyte mask[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,0,0);
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       int stride = LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual);
+       char *fb = (char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual);
 
        while (n--) {
-               if (*mask++) 
-                       *ci=*(fb+ *x + FLIP(*y)*GGICTX->width);
+               if (*mask++){
+                       FB_TYPE *src = (FB_TYPE*)(fb + FLIP(*y)*stride) + *x;
+                       *ci = *src;
+               }
                ci++;
                x++;
                y++;
@@ -270,64 +324,86 @@ void GGIread_ci32_pixels(const GLcontext *ctx,
 }
 
 void GGIread_rgba_pixels(const GLcontext *ctx,
-                           GLuint n, const GLint x[], const GLint y[],
-                           GLubyte rgba[][4],
-                           const GLubyte mask[] )
+                        GLuint n, const GLint x[], const GLint y[],
+                        GLubyte rgba[][4], const GLubyte mask[])
 {
-       FB_TYPE *fb=LFB(FB_TYPE,0,0);
+       ggi_mesa_context_t ggi_ctx = (ggi_mesa_context_t)ctx->DriverCtx;
+       int stride = LIBGGI_FB_W_STRIDE(ggi_ctx->ggi_visual);
+       char *fb = (char *)LIBGGI_CURWRITE(ggi_ctx->ggi_visual);
        FB_TYPE color;
 
-       while (n--)
-       {
-               if (*mask++)
-               {
-                       color=*(fb+ *x + FLIP(*y)*GGICTX->width);
-                       rgba[0][RCOMP] =(GLubyte)(color>>(G+B))<<RS;  
-                       rgba[0][GCOMP] =(GLubyte)((color>>B)& ((1<<G)-1))<<GS;  
-                       rgba[0][BCOMP] =(GLubyte) (color & ((1<<B)-1))<<BS;  
-                       rgba[0][ACOMP] =0;
+       while (n--) {
+               if (*mask++) {  
+                       FB_TYPE *src = (FB_TYPE*)(fb + FLIP(*y)*stride) + *x;
+                       color = *src;
+
+                       rgba[0][RCOMP] = (GLubyte)(color>>(G+B))<<RS;  
+                       rgba[0][GCOMP] = (GLubyte)((color>>B)& ((1<<G)-1))<<GS;
+                       rgba[0][BCOMP] = (GLubyte) (color & ((1<<B)-1))<<BS;  
+                       rgba[0][ACOMP] = 0;
                }       
-               x++; y++;
+               x++;
+               y++;
                rgba++;
        }
 }
 
-int GGIsetup_driver(GGIMesaContext ggictx,struct ggi_mesa_info *info)
+void GGIset_buffer(GLcontext *ctx, GLframebuffer *buffer, GLenum mode)
 {
-       GLcontext *ctx=ggictx->gl_ctx;
-
-       ctx->Driver.WriteRGBASpan       = GGIwrite_rgba_span;
-       ctx->Driver.WriteRGBSpan        = GGIwrite_rgb_span;
-       ctx->Driver.WriteMonoRGBASpan   = GGIwrite_mono_span;
-       ctx->Driver.WriteRGBAPixels     = GGIwrite_rgba_pixels;
-       ctx->Driver.WriteMonoRGBAPixels = GGIwrite_mono_pixels;
-
-       ctx->Driver.WriteCI32Span       = GGIwrite_ci32_span;
-       ctx->Driver.WriteCI8Span       = GGIwrite_ci8_span;
-       ctx->Driver.WriteMonoCISpan   = GGIwrite_mono_span;
-       ctx->Driver.WriteCI32Pixels     = GGIwrite_ci32_pixels;
-       ctx->Driver.WriteMonoCIPixels = GGIwrite_mono_pixels;
-
-       ctx->Driver.ReadCI32Span = GGIread_ci32_span;
-       ctx->Driver.ReadRGBASpan = GGIread_rgba_span;
-       ctx->Driver.ReadCI32Pixels = GGIread_ci32_pixels;
-       ctx->Driver.ReadRGBAPixels = GGIread_rgba_pixels;
+}
 
-       info->red_bits=R;
-       info->green_bits=G;
-       info->blue_bits=B;
+int GGIsetup_driver(ggi_mesa_context_t ggi_ctx)
+{
+       struct swrast_device_driver *swdd =
+               _swrast_GetDeviceDriverReference(ggi_ctx->gl_ctx);
+
+       GGIMESADPRINT_LIBS("linear_%d: GGIsetup_driver\n", sizeof(FB_TYPE)*8);
+       
+       swdd->WriteRGBASpan     = GGIwrite_rgba_span;
+       swdd->WriteRGBSpan      = GGIwrite_rgb_span;
+       swdd->WriteMonoRGBASpan = GGIwrite_mono_rgba_span;
+       swdd->WriteRGBAPixels   = GGIwrite_rgba_pixels;
+       swdd->WriteMonoRGBAPixels = GGIwrite_mono_rgba_pixels;
+
+       swdd->WriteCI32Span       = GGIwrite_ci32_span;
+       swdd->WriteCI8Span       = GGIwrite_ci8_span;
+       swdd->WriteMonoCISpan   = GGIwrite_mono_ci_span;
+       swdd->WriteCI32Pixels     = GGIwrite_ci32_pixels;
+       swdd->WriteMonoCIPixels = GGIwrite_mono_ci_pixels;
+
+       swdd->ReadCI32Span = GGIread_ci32_span;
+       swdd->ReadRGBASpan = GGIread_rgba_span;
+       swdd->ReadCI32Pixels = GGIread_ci32_pixels;
+       swdd->ReadRGBAPixels = GGIread_rgba_pixels;
+
+       swdd->SetBuffer = GGIset_buffer;        
 
        return 0;
 }
 
-int GGIdlinit(ggi_visual_t vis,const char *version,void *argptr)
-{      
-       LIBGGI_MESAEXT(vis)->setup_driver=GGIsetup_driver;
+static int GGIopen(ggi_visual_t vis,struct ggi_dlhandle *dlh,
+                  const char *args,void *argptr, uint32 *dlret)
+{
+       GGIMESADPRINT_CORE("linear_%d: GGIOpen\n", sizeof(FB_TYPE)*8);
+       LIBGGI_MESAEXT(vis)->setup_driver = GGIsetup_driver;
 
+       *dlret = GGI_DL_OPDRAW;
        return 0;
 }
 
-int GGIdlcleanup(ggi_visual_t vis)
+int DLOPENFUNC(int func, void **funcptr)
 {
-       return 0;
+       switch (func) {
+       case GGIFUNC_open:
+               *funcptr = GGIopen;
+               return 0;
+       case GGIFUNC_exit:
+       case GGIFUNC_close:
+               *funcptr = NULL;
+               return 0;
+       default:
+               *funcptr = NULL;
+       }
+       return GGI_ENOTFOUND;
 }
+