Fixed bug 1550 - SDL_RenderCopy/CopyEx in software should optionally render 8bit alpha
authorSam Lantinga <slouken@libsdl.org>
Fri, 19 Jun 2015 23:20:43 -0700
changeset 97625c4a85c5b648
parent 9761 fd7fe355f173
child 9763 c2ba8ab27c1a
Fixed bug 1550 - SDL_RenderCopy/CopyEx in software should optionally render 8bit alpha

Adam M.

There are three problems in the code that I see.
1. SW_RenderCopyEx enables a color key on surface_scaled even if the source surface didn't have a color key.
2. SW_RenderCopyEx doesn't copy blend mode, color mod, or alpha mod from src to surface_scaled.
3. When SDL_BlitScaled(src, srcrect, surface_scaled, &tmp_rect) is called, it blends the src pixels into surface_scaled instead of overwriting them (if src has blending, etc. enabled).

I've attached a patch that 1) fixes the three problems that I mentioned, 2) adds the requested performance improvement of using the regular blit function if no rotation or flipping is needed, 3) avoids cloning the source surface if no stretching is required, and simplifies the rotation code slightly.
src/render/SDL_render.c
src/render/software/SDL_render_sw.c
src/render/software/SDL_rotate.c
     1.1 --- a/src/render/SDL_render.c	Fri Jun 19 23:12:13 2015 -0700
     1.2 +++ b/src/render/SDL_render.c	Fri Jun 19 23:20:43 2015 -0700
     1.3 @@ -1734,6 +1734,10 @@
     1.4      SDL_FRect frect;
     1.5      SDL_FPoint fcenter;
     1.6  
     1.7 +    if (flip == SDL_FLIP_NONE && angle == 0) { /* fast path when we don't need rotation or flipping */
     1.8 +        return SDL_RenderCopy(renderer, texture, srcrect, dstrect);
     1.9 +    }
    1.10 +
    1.11      CHECK_RENDERER_MAGIC(renderer, -1);
    1.12      CHECK_TEXTURE_MAGIC(texture, -1);
    1.13  
     2.1 --- a/src/render/software/SDL_render_sw.c	Fri Jun 19 23:12:13 2015 -0700
     2.2 +++ b/src/render/software/SDL_render_sw.c	Fri Jun 19 23:20:43 2015 -0700
     2.3 @@ -600,7 +600,6 @@
     2.4      SDL_Surface *src = (SDL_Surface *) texture->driverdata;
     2.5      SDL_Rect final_rect, tmp_rect;
     2.6      SDL_Surface *surface_rotated, *surface_scaled;
     2.7 -    Uint32 colorkey;
     2.8      int retval, dstwidth, dstheight, abscenterx, abscentery;
     2.9      double cangle, sangle, px, py, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y;
    2.10  
    2.11 @@ -618,66 +617,113 @@
    2.12      final_rect.w = (int)dstrect->w;
    2.13      final_rect.h = (int)dstrect->h;
    2.14  
    2.15 -    surface_scaled = SDL_CreateRGBSurface(SDL_SWSURFACE, final_rect.w, final_rect.h, src->format->BitsPerPixel,
    2.16 -                                          src->format->Rmask, src->format->Gmask,
    2.17 -                                          src->format->Bmask, src->format->Amask );
    2.18 -    if (surface_scaled) {
    2.19 -        SDL_GetColorKey(src, &colorkey);
    2.20 -        SDL_SetColorKey(surface_scaled, SDL_TRUE, colorkey);
    2.21 -        tmp_rect = final_rect;
    2.22 -        tmp_rect.x = 0;
    2.23 -        tmp_rect.y = 0;
    2.24 +    /* SDLgfx_rotateSurface doesn't accept a source rectangle, so crop and scale if we need to */
    2.25 +    tmp_rect = final_rect;
    2.26 +    tmp_rect.x = 0;
    2.27 +    tmp_rect.y = 0;
    2.28 +    if (srcrect->w == final_rect.w && srcrect->h == final_rect.h && srcrect->x == 0 && srcrect->y == 0) {
    2.29 +        surface_scaled = src; /* but if we don't need to, just use the original */
    2.30 +        retval = 0;
    2.31 +    } else {
    2.32 +        SDL_Surface *blit_src = src;
    2.33 +        Uint32 colorkey;
    2.34 +        SDL_BlendMode blendMode;
    2.35 +        Uint8 alphaMod, r, g, b;
    2.36 +        SDL_bool cloneSource = SDL_FALSE;
    2.37  
    2.38 -        retval = SDL_BlitScaled(src, srcrect, surface_scaled, &tmp_rect);
    2.39 -        if (!retval) {
    2.40 -            SDLgfx_rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, -angle, &dstwidth, &dstheight, &cangle, &sangle);
    2.41 -            surface_rotated = SDLgfx_rotateSurface(surface_scaled, -angle, dstwidth/2, dstheight/2, GetScaleQuality(), flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL, dstwidth, dstheight, cangle, sangle);
    2.42 -            if(surface_rotated) {
    2.43 -                /* Find out where the new origin is by rotating the four final_rect points around the center and then taking the extremes */
    2.44 -                abscenterx = final_rect.x + (int)center->x;
    2.45 -                abscentery = final_rect.y + (int)center->y;
    2.46 -                /* Compensate the angle inversion to match the behaviour of the other backends */
    2.47 -                sangle = -sangle;
    2.48 +        surface_scaled = SDL_CreateRGBSurface(SDL_SWSURFACE, final_rect.w, final_rect.h, src->format->BitsPerPixel,
    2.49 +                                              src->format->Rmask, src->format->Gmask,
    2.50 +                                              src->format->Bmask, src->format->Amask );
    2.51 +        if (!surface_scaled) {
    2.52 +            return -1;
    2.53 +        }
    2.54  
    2.55 -                /* Top Left */
    2.56 -                px = final_rect.x - abscenterx;
    2.57 -                py = final_rect.y - abscentery;
    2.58 -                p1x = px * cangle - py * sangle + abscenterx;
    2.59 -                p1y = px * sangle + py * cangle + abscentery;
    2.60 +        /* copy the color key, alpha mod, blend mode, and color mod so the scaled surface behaves like the source */
    2.61 +        if (SDL_GetColorKey(src, &colorkey) == 0) {
    2.62 +            SDL_SetColorKey(surface_scaled, SDL_TRUE, colorkey);
    2.63 +            cloneSource = SDL_TRUE;
    2.64 +        }
    2.65 +        SDL_GetSurfaceAlphaMod(src, &alphaMod); /* these will be copied to surface_scaled below if necessary */
    2.66 +        SDL_GetSurfaceBlendMode(src, &blendMode);
    2.67 +        SDL_GetSurfaceColorMod(src, &r, &g, &b);
    2.68  
    2.69 -                /* Top Right */
    2.70 -                px = final_rect.x + final_rect.w - abscenterx;
    2.71 -                py = final_rect.y - abscentery;
    2.72 -                p2x = px * cangle - py * sangle + abscenterx;
    2.73 -                p2y = px * sangle + py * cangle + abscentery;
    2.74 +        /* now we need to blit the src into surface_scaled. since we want to copy the colors from the source to
    2.75 +         * surface_scaled rather than blend them, etc. we'll need to disable the blend mode, alpha mod, etc.
    2.76 +         * but we don't want to modify src (in case it's being used on other threads), so we'll need to clone it
    2.77 +         * before changing the blend options
    2.78 +         */
    2.79 +        cloneSource |= blendMode != SDL_BLENDMODE_NONE || (alphaMod & r & g & b) != 255;
    2.80 +        if (cloneSource) {
    2.81 +            blit_src = SDL_ConvertSurface(src, src->format, src->flags); /* clone src */
    2.82 +            if (!blit_src) {
    2.83 +                SDL_FreeSurface(surface_scaled);
    2.84 +                return -1;
    2.85 +            }
    2.86 +            SDL_SetSurfaceAlphaMod(blit_src, 255); /* disable all blending options in blit_src */
    2.87 +            SDL_SetSurfaceBlendMode(blit_src, SDL_BLENDMODE_NONE);
    2.88 +            SDL_SetColorKey(blit_src, 0, 0);
    2.89 +            SDL_SetSurfaceColorMod(blit_src, 255, 255, 255);
    2.90 +            SDL_SetSurfaceRLE(blit_src, 0); /* don't RLE encode a surface we'll only use once */
    2.91  
    2.92 -                /* Bottom Left */
    2.93 -                px = final_rect.x - abscenterx;
    2.94 -                py = final_rect.y + final_rect.h - abscentery;
    2.95 -                p3x = px * cangle - py * sangle + abscenterx;
    2.96 -                p3y = px * sangle + py * cangle + abscentery;
    2.97 +            SDL_SetSurfaceAlphaMod(surface_scaled, alphaMod); /* copy blending options to surface_scaled */
    2.98 +            SDL_SetSurfaceBlendMode(surface_scaled, blendMode);
    2.99 +            SDL_SetSurfaceColorMod(surface_scaled, r, g, b);
   2.100 +        }
   2.101  
   2.102 -                /* Bottom Right */
   2.103 -                px = final_rect.x + final_rect.w - abscenterx;
   2.104 -                py = final_rect.y + final_rect.h - abscentery;
   2.105 -                p4x = px * cangle - py * sangle + abscenterx;
   2.106 -                p4y = px * sangle + py * cangle + abscentery;
   2.107 -
   2.108 -                tmp_rect.x = (int)MIN(MIN(p1x, p2x), MIN(p3x, p4x));
   2.109 -                tmp_rect.y = (int)MIN(MIN(p1y, p2y), MIN(p3y, p4y));
   2.110 -                tmp_rect.w = dstwidth;
   2.111 -                tmp_rect.h = dstheight;
   2.112 -
   2.113 -                retval = SDL_BlitSurface(surface_rotated, NULL, surface, &tmp_rect);
   2.114 -                SDL_FreeSurface(surface_scaled);
   2.115 -                SDL_FreeSurface(surface_rotated);
   2.116 -                return retval;
   2.117 -            }
   2.118 +        retval = SDL_BlitScaled(blit_src, srcrect, surface_scaled, &tmp_rect);
   2.119 +        if (blit_src != src) {
   2.120 +            SDL_FreeSurface(blit_src);
   2.121          }
   2.122 -        return retval;
   2.123      }
   2.124  
   2.125 -    return -1;
   2.126 +    if (!retval) {
   2.127 +        SDLgfx_rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, -angle, &dstwidth, &dstheight, &cangle, &sangle);
   2.128 +        surface_rotated = SDLgfx_rotateSurface(surface_scaled, -angle, dstwidth/2, dstheight/2, GetScaleQuality(), flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL, dstwidth, dstheight, cangle, sangle);
   2.129 +        if(surface_rotated) {
   2.130 +            /* Find out where the new origin is by rotating the four final_rect points around the center and then taking the extremes */
   2.131 +            abscenterx = final_rect.x + (int)center->x;
   2.132 +            abscentery = final_rect.y + (int)center->y;
   2.133 +            /* Compensate the angle inversion to match the behaviour of the other backends */
   2.134 +            sangle = -sangle;
   2.135 +
   2.136 +            /* Top Left */
   2.137 +            px = final_rect.x - abscenterx;
   2.138 +            py = final_rect.y - abscentery;
   2.139 +            p1x = px * cangle - py * sangle + abscenterx;
   2.140 +            p1y = px * sangle + py * cangle + abscentery;
   2.141 +
   2.142 +            /* Top Right */
   2.143 +            px = final_rect.x + final_rect.w - abscenterx;
   2.144 +            py = final_rect.y - abscentery;
   2.145 +            p2x = px * cangle - py * sangle + abscenterx;
   2.146 +            p2y = px * sangle + py * cangle + abscentery;
   2.147 +
   2.148 +            /* Bottom Left */
   2.149 +            px = final_rect.x - abscenterx;
   2.150 +            py = final_rect.y + final_rect.h - abscentery;
   2.151 +            p3x = px * cangle - py * sangle + abscenterx;
   2.152 +            p3y = px * sangle + py * cangle + abscentery;
   2.153 +
   2.154 +            /* Bottom Right */
   2.155 +            px = final_rect.x + final_rect.w - abscenterx;
   2.156 +            py = final_rect.y + final_rect.h - abscentery;
   2.157 +            p4x = px * cangle - py * sangle + abscenterx;
   2.158 +            p4y = px * sangle + py * cangle + abscentery;
   2.159 +
   2.160 +            tmp_rect.x = (int)MIN(MIN(p1x, p2x), MIN(p3x, p4x));
   2.161 +            tmp_rect.y = (int)MIN(MIN(p1y, p2y), MIN(p3y, p4y));
   2.162 +            tmp_rect.w = dstwidth;
   2.163 +            tmp_rect.h = dstheight;
   2.164 +
   2.165 +            retval = SDL_BlitSurface(surface_rotated, NULL, surface, &tmp_rect);
   2.166 +            SDL_FreeSurface(surface_rotated);
   2.167 +        }
   2.168 +    }
   2.169 +
   2.170 +    if (surface_scaled != src) {
   2.171 +        SDL_FreeSurface(surface_scaled);
   2.172 +    }
   2.173 +    return retval;
   2.174  }
   2.175  
   2.176  static int
     3.1 --- a/src/render/software/SDL_rotate.c	Fri Jun 19 23:12:13 2015 -0700
     3.2 +++ b/src/render/software/SDL_rotate.c	Fri Jun 19 23:20:43 2015 -0700
     3.3 @@ -188,10 +188,8 @@
     3.4                  dy = (sdy >> 16);
     3.5                  if (flipx) dx = sw - dx;
     3.6                  if (flipy) dy = sh - dy;
     3.7 -                if ((dx > -1) && (dy > -1) && (dx < (src->w-1)) && (dy < (src->h-1))) {
     3.8 -                    sp = (tColorRGBA *)src->pixels;
     3.9 -                    sp += ((src->pitch/4) * dy);
    3.10 -                    sp += dx;
    3.11 +                if ((unsigned)dx < (unsigned)sw && (unsigned)dy < (unsigned)sh) {
    3.12 +                    sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy) + dx;
    3.13                      c00 = *sp;
    3.14                      sp += 1;
    3.15                      c01 = *sp;
    3.16 @@ -237,14 +235,12 @@
    3.17              sdx = (ax + (isin * dy)) + xd;
    3.18              sdy = (ay - (icos * dy)) + yd;
    3.19              for (x = 0; x < dst->w; x++) {
    3.20 -                dx = (short) (sdx >> 16);
    3.21 -                dy = (short) (sdy >> 16);
    3.22 -                if (flipx) dx = (src->w-1)-dx;
    3.23 -                if (flipy) dy = (src->h-1)-dy;
    3.24 -                if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
    3.25 -                    sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy);
    3.26 -                    sp += dx;
    3.27 -                    *pc = *sp;
    3.28 +                dx = (sdx >> 16);
    3.29 +                dy = (sdy >> 16);
    3.30 +                if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
    3.31 +                    if(flipx) dx = sw - dx;
    3.32 +                    if(flipy) dy = sh - dy;
    3.33 +                    *pc = *((tColorRGBA *)((Uint8 *)src->pixels + src->pitch * dy) + dx);
    3.34                  }
    3.35                  sdx += icos;
    3.36                  sdy += isin;
    3.37 @@ -277,7 +273,7 @@
    3.38  transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
    3.39  {
    3.40      int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay;
    3.41 -    tColorY *pc, *sp;
    3.42 +    tColorY *pc;
    3.43      int gap;
    3.44  
    3.45      /*
    3.46 @@ -301,14 +297,12 @@
    3.47          sdx = (ax + (isin * dy)) + xd;
    3.48          sdy = (ay - (icos * dy)) + yd;
    3.49          for (x = 0; x < dst->w; x++) {
    3.50 -            dx = (short) (sdx >> 16);
    3.51 -            dy = (short) (sdy >> 16);
    3.52 -            if (flipx) dx = (src->w-1)-dx;
    3.53 -            if (flipy) dy = (src->h-1)-dy;
    3.54 -            if ((dx >= 0) && (dy >= 0) && (dx < src->w) && (dy < src->h)) {
    3.55 -                sp = (tColorY *) (src->pixels);
    3.56 -                sp += (src->pitch * dy + dx);
    3.57 -                *pc = *sp;
    3.58 +            dx = (sdx >> 16);
    3.59 +            dy = (sdy >> 16);
    3.60 +            if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
    3.61 +                if (flipx) dx = (src->w-1)-dx;
    3.62 +                if (flipy) dy = (src->h-1)-dy;
    3.63 +                *pc = *((tColorY *)src->pixels + src->pitch * dy + dx);
    3.64              }
    3.65              sdx += icos;
    3.66              sdy += isin;