Fix so many things that there is little place in this column to list them all but the result is that blending modes just work now for drawing primitives.
authorSunny Sachanandani <sunnysachanandani@gmail.com>
Fri, 09 Jul 2010 21:36:41 +0530
changeset 4590 1ad70fb49fcb
parent 4589 8d8a889530eb
child 4591 1e998db9b597
Fix so many things that there is little place in this column to list them all but the result is that blending modes just work now for drawing primitives. Fixes involved: 1. Fix handling of alpha channel when SDL_BLENDMODE_NONE is set. 2. Make xrendercolor use floating-point values for color channels and then convert to 16 bit ints. 3. Fix handling of visuals in SDL_x11modes.c so that a 32 bit ARGB visual is used. 4. Fix the background pixel value in SDL_x11window.c so that the window background has an alpha value of 0xFF and not 0.
src/video/x11/SDL_x11modes.c
src/video/x11/SDL_x11render.c
src/video/x11/SDL_x11window.c
--- a/src/video/x11/SDL_x11modes.c	Thu Jul 01 07:35:15 2010 +0530
+++ b/src/video/x11/SDL_x11modes.c	Fri Jul 09 21:36:41 2010 +0530
@@ -23,7 +23,7 @@
 
 #include "SDL_x11video.h"
 
-//#define X11MODES_DEBUG
+#define X11MODES_DEBUG
 #undef SDL_VIDEO_DRIVER_X11_XINERAMA
 #undef SDL_VIDEO_DRIVER_X11_XRANDR
 #undef SDL_VIDEO_DRIVER_X11_VIDMODE
@@ -33,11 +33,12 @@
 {
     const char *visual_id = SDL_getenv("SDL_VIDEO_X11_VISUALID");
     int depth;
+    XVisualInfo *vi;
+    int nvis;
 
     /* Look for an exact visual, if requested */
     if (visual_id) {
-        XVisualInfo *vi, template;
-        int nvis;
+        XVisualInfo template;
 
         SDL_zero(template);
         template.visualid = SDL_strtol(visual_id, NULL, 0);
@@ -48,7 +49,22 @@
             return 0;
         }
     }
-
+#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
+    depth = 32;
+    long vinfo_mask;
+    XVisualInfo vinfo_templ;
+    vinfo_mask = (VisualDepthMask | VisualRedMaskMask | VisualGreenMaskMask | VisualBlueMaskMask);
+    vinfo_templ.depth = 32;
+    vinfo_templ.red_mask = 0xFF0000;
+    vinfo_templ.green_mask = 0xFF00;
+    vinfo_templ.blue_mask = 0xFF;
+    vi = XGetVisualInfo(display, vinfo_mask, &vinfo_templ, &nvis);
+    if(vi) {
+        *vinfo = *vi;
+        XFree(vi);
+        return 0;
+    }
+#endif
     depth = DefaultDepth(display, screen);
     if ((X11_UseDirectColorVisuals() &&
          XMatchVisualInfo(display, screen, depth, DirectColor, vinfo)) ||
--- a/src/video/x11/SDL_x11render.c	Thu Jul 01 07:35:15 2010 +0530
+++ b/src/video/x11/SDL_x11render.c	Fri Jul 09 21:36:41 2010 +0530
@@ -215,38 +215,7 @@
     data->depth = displaydata->depth;
     data->scanline_pad = displaydata->scanline_pad;
     data->xwindow = windowdata->xwindow;
-#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
-    int event_basep, error_basep;
-    if(XRenderQueryExtension(data->display, &event_basep, &error_basep) == True) {
-        data->use_xrender = SDL_TRUE;
-        data->xwindow_pict_fmt = XRenderFindVisualFormat(data->display, data->visual);
-        if(!data->xwindow_pict_fmt) {
-            data->use_xrender = SDL_FALSE;
-            goto fallback;
-        }
-        data->xwindow_pict = XRenderCreatePicture(data->display, data->xwindow, data->xwindow_pict_fmt,
-                                                  0, NULL);
-        if(!data->xwindow_pict) {
-            data->use_xrender = SDL_FALSE;
-            goto fallback;
-        }
-        // Create a 1 bit depth mask
-        data->mask = XCreatePixmap(data->display, data->xwindow,
-                                   window->w, window->h, 1);
-        data->mask_pict = XRenderCreatePicture(data->display, data->mask,
-                                               XRenderFindStandardFormat(data->display,
-                                                                         PictStandardA1),
-                                               0, NULL);
-        XGCValues gcv_mask;
-        gcv_mask.foreground = 1;
-        gcv_mask.background = 0;
-        data->mask_gc = XCreateGC(data->display, data->mask, GCBackground | GCForeground, &gcv_mask);
-    }
-    else {
-        data->use_xrender = SDL_FALSE;
-    }
-    fallback:
-#endif
+    
     renderer->DisplayModeChanged = X11_DisplayModeChanged;
     renderer->CreateTexture = X11_CreateTexture;
     renderer->QueryTexturePixels = X11_QueryTexturePixels;
@@ -272,6 +241,43 @@
 
     renderer->info.flags = SDL_RENDERER_ACCELERATED;
 
+#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
+    int event_basep, error_basep;
+    if(XRenderQueryExtension(data->display, &event_basep, &error_basep) == True) {
+        data->use_xrender = SDL_TRUE;
+        data->xwindow_pict_fmt = XRenderFindStandardFormat(data->display, PictStandardARGB32);
+        if(!data->xwindow_pict_fmt) {
+            data->use_xrender = SDL_FALSE;
+            goto fallback;
+        }
+        data->xwindow_pict = XRenderCreatePicture(data->display, data->xwindow, data->xwindow_pict_fmt,
+                                                  0, NULL);
+        if(!data->xwindow_pict) {
+            data->use_xrender = SDL_FALSE;
+            goto fallback;
+        }
+        renderer->info.blend_modes |=
+            (SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MASK);
+        // Create a 1 bit depth mask
+        data->mask = XCreatePixmap(data->display, data->xwindow,
+                                   window->w, window->h, 1);
+        data->mask_pict = XRenderCreatePicture(data->display, data->mask,
+                                               XRenderFindStandardFormat(data->display,
+                                                                         PictStandardA1),
+                                               0, NULL);
+        XGCValues gcv_mask;
+        gcv_mask.foreground = 1;
+        gcv_mask.background = 0;
+        data->mask_gc = XCreateGC(data->display, data->mask, GCBackground | GCForeground, &gcv_mask);
+        renderer->blendMode = SDL_BLENDMODE_BLEND;
+        data->blend_op = PictOpOver;
+    }
+    else {
+        data->use_xrender = SDL_FALSE;
+    }
+    fallback:
+#endif
+    
     if (flags & SDL_RENDERER_SINGLEBUFFER) {
         renderer->info.flags |=
             (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY);
@@ -734,26 +740,30 @@
     switch (renderer->blendMode) {
     case SDL_BLENDMODE_NONE:
 #ifdef SDL_VIDEO_DRIVER_X11_XRENDER
+        //PictOpSrc
         data->blend_op = PictOpSrc;
         return 0;
-    case SDL_BLENDMODE_MASK: // Use src pict as mask
-        data->blend_op = PictOpSrc;
+    case SDL_BLENDMODE_BLEND: // PictOpOver
+        data->blend_op = PictOpOver;
         return 0;
     case SDL_BLENDMODE_ADD: // PictOpAdd
         data->blend_op = PictOpAdd;
         return 0;
-    case SDL_BLENDMODE_BLEND: // PictOpOver
-        data->blend_op = PictOpOver;
-        return 0;
     /* FIXME case SDL_BLENDMODE_MOD: */
 #endif
         return 0;
     default:
         SDL_Unsupported();
-        renderer->blendMode = SDL_BLENDMODE_NONE;
 #ifdef SDL_VIDEO_DRIVER_X11_XRENDER
-        data->blend_op = PictOpSrc;
+        if(data->use_xrender) {
+            renderer->blendMode = SDL_BLENDMODE_BLEND;
+            data->blend_op = PictOpOver;
+        }
+        else
 #endif
+        {
+            renderer->blendMode = SDL_BLENDMODE_NONE;
+        }
         return -1;
     }
 }
@@ -779,10 +789,20 @@
 {
     // Premultiply the color channels as well as modulate them to a 16 bit color space
     XRenderColor xrender_color;
-    xrender_color.red = ((unsigned short)renderer->r + 1) * ((unsigned short)renderer->a + 1) - 1;
-    xrender_color.green = ((unsigned short)renderer->g + 1) * ((unsigned short)renderer->a + 1) - 1;
-    xrender_color.blue = ((unsigned short)renderer->b + 1) * ((unsigned short)renderer->a + 1) - 1;
-    xrender_color.alpha = ((unsigned short)renderer->a + 1) * ((unsigned short)renderer->a + 1) - 1;
+    double alphad;
+    if(renderer->blendMode == SDL_BLENDMODE_NONE)
+        alphad = 1.0;
+    else
+        alphad = (renderer->a) / 255.0;
+
+    xrender_color.alpha = (unsigned short) (alphad * 0xFFFF);
+
+    xrender_color.red =
+        (unsigned short) ((renderer->r / 255.0) * alphad * 0xFFFF);
+    xrender_color.green =
+        (unsigned short) ((renderer->g / 255.0) * alphad * 0xFFFF);
+    xrender_color.blue =
+        (unsigned short) ((renderer->b / 255.0) * alphad * 0xFFFF);
     return xrender_color;
 }
 
@@ -1050,6 +1070,12 @@
     XRectangle *xrects, *xrect;
     xrect = xrects = SDL_stack_alloc(XRectangle, count);
     xcount = 0;
+    
+    clip.x = 0;
+    clip.y = 0;
+    clip.w = window->w;
+    clip.h = window->h;
+
     for (i = 0; i < count; ++i) {
         if (!SDL_IntersectRect(rects[i], &clip, &rect)) {
             continue;
@@ -1066,11 +1092,7 @@
             SDL_AddDirtyRect(&data->dirty, &rect);
         }
     }
-    clip.x = 0;
-    clip.y = 0;
-    clip.w = window->w;
-    clip.h = window->h;
-
+     
 #ifdef SDL_VIDEO_DRIVER_X11_XRENDER
     if(data->use_xrender == SDL_TRUE) {
         XRenderColor foreground;
@@ -1151,7 +1173,6 @@
         unsigned long valuemask;
 
         foreground = xrenderdrawcolor(renderer);
-        attributes.clip_mask = data->mask;
         valuemask = CPClipMask;
         attributes.clip_mask = data->mask;
         
@@ -1424,15 +1445,9 @@
 #ifdef SDL_VIDEO_DRIVER_X11_XRENDER
             if(data->use_xrender == SDL_TRUE)
             {
-                if(renderer->blendMode == SDL_BLENDMODE_MASK)
-                    XRenderComposite(data->display, data->blend_op, data->drawable_pict,
-                                     data->drawable_pict, data->xwindow_pict, rect->x, rect->y,
-                                     0, 0, rect->x, rect->y, rect->w, rect->h);
-                else
-                    XRenderComposite(data->display, data->blend_op, data->drawable_pict, None,
-                                     data->xwindow_pict, rect->x, rect->y, 0, 0, rect->x, rect->y,
-                                     rect->w, rect->h);
-
+                XRenderComposite(data->display, PictOpOver, data->drawable_pict, None,
+                                 data->xwindow_pict, rect->x, rect->y, 0, 0, rect->x, rect->y,
+                                 rect->w+1, rect->h+1);
             }
             else
 #endif
--- a/src/video/x11/SDL_x11window.c	Thu Jul 01 07:35:15 2010 +0530
+++ b/src/video/x11/SDL_x11window.c	Fri Jul 09 21:36:41 2010 +0530
@@ -513,6 +513,9 @@
     } else {
         y = window->y;
     }
+#ifdef SDL_VIDEO_DRIVER_X11_XRENDER
+    xattr.background_pixel = 0xFF000000;
+#endif
 
     w = XCreateWindow(data->display,
                       RootWindow(data->display, displaydata->screen), x, y,