Commit ab8bd3d96baf297861d628d1630ac3063c99c83a

Sam Lantinga 2016-11-15T01:12:27

Fixed bug 3359 - Software renderer does incorrect blending with SDL_RenderCopyEx Simon Hug The software renderer produces incorrect results when blending textures at an angle with certain blend modes. It seems that there were some edge cases that weren't considered when the SW_RenderCopyEx function was last changed. Or another bug possibly covered up the problem. (More on that in another bug report.) Most of the issues come from the fact that the rotating function sets a black colorkey. This is problematic because black is most likely appearing in the surface and the final blit will ignore these pixels. Unless a colorkey is already set (the software renderer currently never sets one), it's very hard to find a free color. Of course it could scan over the whole image until one is found, but that seems inefficient. The following blend modes have issues when drawn at an angle. NONE: The black pixels get ignored, making them essentially transparent. This breaks the 'dstRGBA = srcRGBA' definition of the NONE blend mode. MOD: Again, the black pixels get ignored. This also breaks the 'dstRGB = dstRGB * srcRGB' definition of the MOD blend mode, where black pixels would make the destination black as well. A white colorkey will work though, with some preparations. BLEND: There are some issues when blending a texture with a translucent RGBA target texture. I - uh - forgot what the problem here exactly is. This patch fixes the issues mentioned above. It mainly changes the code so it tries to do things without the colorkey and removes the automatic format conversion part from the SDLgfx_rotateSurface function. Getting the format right is something the caller has to do now and the required code has been added to the SW_RenderCopyEx function. There's a small change to the SW_CreateTexture function. RLE encoding a surface with an alpha mask can be a lossy process. Depending on how the user uses the RGBA channels, this may be undesired. The change that surfaces with an alpha mask don't get encoded makes the software renderer consistent with the other renderers. The SW_RenderCopyEx function now does these steps: Lock the source surface if necessary. Create a clone of the source by using the pixel buffer directly. Check the format and set a flag if a conversion is necessary. Check if scaling or cropping is necessary and set the flag for that as well. Check if color and alpha modulation has to be done before the rotate. Check if the source is an opaque surface. If not, it creates a mask surface that is necessary for the NONE blend mode. If any of the flags were set, a new surface is created and the source will be converted, scaled, cropped, and modulated. The rest of the function stays somewhat the same. The mask also needs to be rotated of course and then there is the NONE blend mode... It's surprisingly hard to get the pixel from a rotated surface to the destination buffer without affecting the pixel outside the rotated area. I found a way to do this with three blits which is pretty hard on the performance. Perhaps someone has an idea how to do this faster? As mentioned above, the SDLgfx_rotateSurface now only takes 8-bit paletted or 32-bit with alpha mask surfaces. It additionally sets the new surfaces up for the MOD blend mode. I shortly tested the 8-bit path of SDLgfx_rotateSurface and it seemed to work so far. This path is not used by the software renderer anyway.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
diff --git a/src/render/software/SDL_render_sw.c b/src/render/software/SDL_render_sw.c
index e7a6cd8..91ae8e9 100644
--- a/src/render/software/SDL_render_sw.c
+++ b/src/render/software/SDL_render_sw.c
@@ -239,7 +239,10 @@ SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
     SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
     SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
 
-    if (texture->access == SDL_TEXTUREACCESS_STATIC) {
+    /* Only RLE encode textures without an alpha channel since the RLE coder
+     * discards the color values of pixels with an alpha value of zero.
+     */
+    if (texture->access == SDL_TEXTUREACCESS_STATIC && !Amask) {
         SDL_SetSurfaceRLE(texture->driverdata, 1);
     }
 
@@ -599,9 +602,15 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
     SDL_Surface *surface = SW_ActivateRenderer(renderer);
     SDL_Surface *src = (SDL_Surface *) texture->driverdata;
     SDL_Rect final_rect, tmp_rect;
-    SDL_Surface *surface_rotated, *surface_scaled;
-    int retval, dstwidth, dstheight, abscenterx, abscentery;
+    SDL_Surface *src_clone, *src_rotated, *src_scaled;
+    SDL_Surface *mask = NULL, *mask_rotated = NULL;
+    int retval = 0, dstwidth, dstheight, abscenterx, abscentery;
     double cangle, sangle, px, py, p1x, p1y, p2x, p2y, p3x, p3y, p4x, p4y;
+    SDL_BlendMode blendmode;
+    Uint8 alphaMod, rMod, gMod, bMod;
+    int applyModulation = SDL_FALSE;
+    int blitRequired = SDL_FALSE;
+    int isOpaque = SDL_FALSE;
 
     if (!surface) {
         return -1;
@@ -617,69 +626,104 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
     final_rect.w = (int)dstrect->w;
     final_rect.h = (int)dstrect->h;
 
-    /* SDLgfx_rotateSurface doesn't accept a source rectangle, so crop and scale if we need to */
     tmp_rect = final_rect;
     tmp_rect.x = 0;
     tmp_rect.y = 0;
-    if (srcrect->w == final_rect.w && srcrect->h == final_rect.h && srcrect->x == 0 && srcrect->y == 0) {
-        surface_scaled = src; /* but if we don't need to, just use the original */
-        retval = 0;
-    } else {
-        SDL_Surface *blit_src = src;
-        Uint32 colorkey;
-        SDL_BlendMode blendMode;
-        Uint8 alphaMod, r, g, b;
-        SDL_bool cloneSource = SDL_FALSE;
-
-        surface_scaled = SDL_CreateRGBSurface(SDL_SWSURFACE, final_rect.w, final_rect.h, src->format->BitsPerPixel,
-                                              src->format->Rmask, src->format->Gmask,
-                                              src->format->Bmask, src->format->Amask );
-        if (!surface_scaled) {
-            return -1;
-        }
 
-        /* copy the color key, alpha mod, blend mode, and color mod so the scaled surface behaves like the source */
-        if (SDL_GetColorKey(src, &colorkey) == 0) {
-            SDL_SetColorKey(surface_scaled, SDL_TRUE, colorkey);
-            cloneSource = SDL_TRUE;
+    /* It is possible to encounter an RLE encoded surface here and locking it is
+     * necessary because this code is going to access the pixel buffer directly.
+     */
+    if (SDL_MUSTLOCK(src)) {
+        SDL_LockSurface(src);
+    }
+
+    /* Clone the source surface but use its pixel buffer directly.
+     * The original source surface must be treated as read-only.
+     */
+    src_clone = SDL_CreateRGBSurfaceFrom(src->pixels, src->w, src->h, src->format->BitsPerPixel, src->pitch,
+                                         src->format->Rmask, src->format->Gmask,
+                                         src->format->Bmask, src->format->Amask);
+    if (src_clone == NULL) {
+        if (SDL_MUSTLOCK(src)) {
+            SDL_UnlockSurface(src);
         }
-        SDL_GetSurfaceAlphaMod(src, &alphaMod); /* these will be copied to surface_scaled below if necessary */
-        SDL_GetSurfaceBlendMode(src, &blendMode);
-        SDL_GetSurfaceColorMod(src, &r, &g, &b);
-
-        /* now we need to blit the src into surface_scaled. since we want to copy the colors from the source to
-         * surface_scaled rather than blend them, etc. we'll need to disable the blend mode, alpha mod, etc.
-         * but we don't want to modify src (in case it's being used on other threads), so we'll need to clone it
-         * before changing the blend options
-         */
-        cloneSource |= blendMode != SDL_BLENDMODE_NONE || (alphaMod & r & g & b) != 255;
-        if (cloneSource) {
-            blit_src = SDL_ConvertSurface(src, src->format, src->flags); /* clone src */
-            if (!blit_src) {
-                SDL_FreeSurface(surface_scaled);
-                return -1;
-            }
-            SDL_SetSurfaceAlphaMod(blit_src, 255); /* disable all blending options in blit_src */
-            SDL_SetSurfaceBlendMode(blit_src, SDL_BLENDMODE_NONE);
-            SDL_SetColorKey(blit_src, 0, 0);
-            SDL_SetSurfaceColorMod(blit_src, 255, 255, 255);
-            SDL_SetSurfaceRLE(blit_src, 0); /* don't RLE encode a surface we'll only use once */
-
-            SDL_SetSurfaceAlphaMod(surface_scaled, alphaMod); /* copy blending options to surface_scaled */
-            SDL_SetSurfaceBlendMode(surface_scaled, blendMode);
-            SDL_SetSurfaceColorMod(surface_scaled, r, g, b);
+        return -1;
+    }
+
+    SDL_GetSurfaceBlendMode(src, &blendmode);
+    SDL_GetSurfaceAlphaMod(src, &alphaMod);
+    SDL_GetSurfaceColorMod(src, &rMod, &gMod, &bMod);
+
+    /* SDLgfx_rotateSurface only accepts 32-bit surfaces with a 8888 layout. Everything else has to be converted. */
+    if (src->format->BitsPerPixel != 32 || SDL_PIXELLAYOUT(src->format->format) != SDL_PACKEDLAYOUT_8888 || !src->format->Amask) {
+        blitRequired = SDL_TRUE;
+    }
+
+    /* If scaling and cropping is necessary, it has to be taken care of before the rotation. */
+    if (!(srcrect->w == final_rect.w && srcrect->h == final_rect.h && srcrect->x == 0 && srcrect->y == 0)) {
+        blitRequired = SDL_TRUE;
+    }
+
+    /* The color and alpha modulation has to be applied before the rotation when using the NONE and MOD blend modes. */
+    if ((blendmode == SDL_BLENDMODE_NONE || blendmode == SDL_BLENDMODE_MOD) && (alphaMod & rMod & gMod & bMod) != 255) {
+        applyModulation = SDL_TRUE;
+        SDL_SetSurfaceAlphaMod(src_clone, alphaMod);
+        SDL_SetSurfaceColorMod(src_clone, rMod, gMod, bMod);
+    }
+
+    /* Opaque surfaces are much easier to handle with the NONE blend mode. */
+    if (blendmode == SDL_BLENDMODE_NONE && !src->format->Amask && alphaMod == 255) {
+        isOpaque = SDL_TRUE;
+    }
+
+    /* The NONE blend mode requires a mask for non-opaque surfaces. This mask will be used
+     * to clear the pixels in the destination surface. The other steps are explained below.
+     */
+    if (blendmode == SDL_BLENDMODE_NONE && !isOpaque) {
+        mask = SDL_CreateRGBSurface(0, final_rect.w, final_rect.h, 32,
+                                    0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
+        if (mask == NULL) {
+            retval = -1;
+        } else {
+            SDL_SetSurfaceBlendMode(mask, SDL_BLENDMODE_MOD);
         }
+    }
 
-        retval = SDL_BlitScaled(blit_src, srcrect, surface_scaled, &tmp_rect);
-        if (blit_src != src) {
-            SDL_FreeSurface(blit_src);
+    /* Create a new surface should there be a format mismatch or if scaling, cropping,
+     * or modulation is required. It's possible to use the source surface directly otherwise.
+     */
+    if (!retval && (blitRequired || applyModulation)) {
+        SDL_Rect scale_rect = tmp_rect;
+        src_scaled = SDL_CreateRGBSurface(0, final_rect.w, final_rect.h, 32,
+                                          0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
+        if (src_scaled == NULL) {
+            retval = -1;
+        } else {
+            SDL_SetSurfaceBlendMode(src_clone, SDL_BLENDMODE_NONE);
+            retval = SDL_BlitScaled(src_clone, srcrect, src_scaled, &scale_rect);
+            SDL_FreeSurface(src_clone);
+            src_clone = src_scaled;
+            src_scaled = NULL;
         }
     }
 
+    /* SDLgfx_rotateSurface is going to make decisions depending on the blend mode. */
+    SDL_SetSurfaceBlendMode(src_clone, blendmode);
+
     if (!retval) {
         SDLgfx_rotozoomSurfaceSizeTrig(tmp_rect.w, tmp_rect.h, angle, &dstwidth, &dstheight, &cangle, &sangle);
-        surface_rotated = SDLgfx_rotateSurface(surface_scaled, angle, dstwidth/2, dstheight/2, GetScaleQuality(), flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL, dstwidth, dstheight, cangle, sangle);
-        if(surface_rotated) {
+        src_rotated = SDLgfx_rotateSurface(src_clone, angle, dstwidth/2, dstheight/2, GetScaleQuality(), flip & SDL_FLIP_HORIZONTAL, flip & SDL_FLIP_VERTICAL, dstwidth, dstheight, cangle, sangle);
+        if (src_rotated == NULL) {
+            retval = -1;
+        }
+        if (!retval && mask != NULL) {
+            /* The mask needed for the NONE blend mode gets rotated with the same parameters. */
+            mask_rotated = SDLgfx_rotateSurface(mask, angle, dstwidth/2, dstheight/2, SDL_FALSE, 0, 0, dstwidth, dstheight, cangle, sangle);
+            if (mask_rotated == NULL) {
+                retval = -1;
+            }
+        }
+        if (!retval) {
             /* Find out where the new origin is by rotating the four final_rect points around the center and then taking the extremes */
             abscenterx = final_rect.x + (int)center->x;
             abscentery = final_rect.y + (int)center->y;
@@ -715,13 +759,69 @@ SW_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
             tmp_rect.w = dstwidth;
             tmp_rect.h = dstheight;
 
-            retval = SDL_BlitSurface(surface_rotated, NULL, surface, &tmp_rect);
-            SDL_FreeSurface(surface_rotated);
+            /* The NONE blend mode needs some special care with non-opaque surfaces.
+             * Other blend modes or opaque surfaces can be blitted directly.
+             */
+            if (blendmode != SDL_BLENDMODE_NONE || isOpaque) {
+                if (applyModulation == SDL_FALSE) {
+                    /* If the modulation wasn't already applied, make it happen now. */
+                    SDL_SetSurfaceAlphaMod(src_rotated, alphaMod);
+                    SDL_SetSurfaceColorMod(src_rotated, rMod, gMod, bMod);
+                }
+                retval = SDL_BlitSurface(src_rotated, NULL, surface, &tmp_rect);
+            } else {
+                /* The NONE blend mode requires three steps to get the pixels onto the destination surface.
+                 * First, the area where the rotated pixels will be blitted to get set to zero.
+                 * This is accomplished by simply blitting a mask with the NONE blend mode.
+                 * The colorkey set by the rotate function will discard the correct pixels.
+                 */
+                SDL_Rect mask_rect = tmp_rect;
+                SDL_SetSurfaceBlendMode(mask_rotated, SDL_BLENDMODE_NONE);
+                retval = SDL_BlitSurface(mask_rotated, NULL, surface, &mask_rect);
+                if (!retval) {
+                    /* The next step copies the alpha value. This is done with the BLEND blend mode and
+                     * by modulating the source colors with 0. Since the destination is all zeros, this
+                     * will effectively set the destination alpha to the source alpha.
+                     */
+                    SDL_SetSurfaceColorMod(src_rotated, 0, 0, 0);
+                    mask_rect = tmp_rect;
+                    retval = SDL_BlitSurface(src_rotated, NULL, surface, &mask_rect);
+                    if (!retval) {
+                        /* The last step gets the color values in place. The ADD blend mode simply adds them to
+                         * the destination (where the color values are all zero). However, because the ADD blend
+                         * mode modulates the colors with the alpha channel, a surface without an alpha mask needs
+                         * to be created. This makes all source pixels opaque and the colors get copied correctly.
+                         */
+                        SDL_Surface *src_rotated_rgb;
+                        src_rotated_rgb = SDL_CreateRGBSurfaceFrom(src_rotated->pixels, src_rotated->w, src_rotated->h,
+                                                                   src_rotated->format->BitsPerPixel, src_rotated->pitch,
+                                                                   src_rotated->format->Rmask, src_rotated->format->Gmask,
+                                                                   src_rotated->format->Bmask, 0);
+                        if (src_rotated_rgb == NULL) {
+                            retval = -1;
+                        } else {
+                            SDL_SetSurfaceBlendMode(src_rotated_rgb, SDL_BLENDMODE_ADD);
+                            retval = SDL_BlitSurface(src_rotated_rgb, NULL, surface, &tmp_rect);
+                            SDL_FreeSurface(src_rotated_rgb);
+                        }
+                    }
+                }
+                SDL_FreeSurface(mask_rotated);
+            }
+            if (src_rotated != NULL) {
+                SDL_FreeSurface(src_rotated);
+            }
         }
     }
 
-    if (surface_scaled != src) {
-        SDL_FreeSurface(surface_scaled);
+    if (SDL_MUSTLOCK(src)) {
+        SDL_UnlockSurface(src);
+    }
+    if (mask != NULL) {
+        SDL_FreeSurface(mask);
+    }
+    if (src_clone != NULL) {
+        SDL_FreeSurface(src_clone);
     }
     return retval;
 }
diff --git a/src/render/software/SDL_rotate.c b/src/render/software/SDL_rotate.c
index 5c899c3..5813b94 100644
--- a/src/render/software/SDL_rotate.c
+++ b/src/render/software/SDL_rotate.c
@@ -142,7 +142,7 @@ SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle,
         cy = *cangle * y;
         sx = *sangle * x;
         sy = *sangle * y;
-        
+
         dstwidthhalf = MAX((int)
             SDL_ceil(MAX(MAX(MAX(SDL_fabs(cx + sy), SDL_fabs(cx - sy)), SDL_fabs(-cx + sy)), SDL_fabs(-cx - sy))), 1);
         dstheighthalf = MAX((int)
@@ -390,10 +390,14 @@ transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin
 /* !
 \brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
 
-Rotates a 32bit or 8bit 'src' surface to newly created 'dst' surface.
+Rotates a 32-bit or 8-bit 'src' surface to newly created 'dst' surface.
 'angle' is the rotation in degrees, 'centerx' and 'centery' the rotation center. If 'smooth' is set
-then the destination 32bit surface is anti-aliased. If the surface is not 8bit
-or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
+then the destination 32-bit surface is anti-aliased. 8-bit surfaces must have a colorkey. 32-bit
+surfaces must have a 8888 layout with red, green, blue and alpha masks (any ordering goes).
+The blend mode of the 'src' surface has some effects on generation of the 'dst' surface: The NONE
+mode will set the BLEND mode on the 'dst' surface. The MOD mode either generates a white 'dst'
+surface and sets the colorkey or fills the it with the colorkey before copying the pixels.
+When using the NONE and MOD modes, color and alpha modulation must be applied before using this function.
 
 \param src The surface to rotozoom.
 \param angle The angle to rotate in degrees.
@@ -413,69 +417,47 @@ or 32bit RGBA/ABGR it will be converted into a 32bit RGBA format on the fly.
 SDL_Surface *
 SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, int smooth, int flipx, int flipy, int dstwidth, int dstheight, double cangle, double sangle)
 {
-    SDL_Surface *rz_src;
     SDL_Surface *rz_dst;
-    int is32bit, angle90;
+    int is8bit, angle90;
     int i;
-    Uint8 r = 0, g = 0, b = 0, a = 0;
+    SDL_BlendMode blendmode;
     Uint32 colorkey = 0;
-    int colorKeyAvailable = 0;
+    int colorKeyAvailable = SDL_FALSE;
     double sangleinv, cangleinv;
 
-    /*
-    * Sanity check
-    */
+    /* Sanity check */
     if (src == NULL)
-        return (NULL);
+        return NULL;
 
-    if (src->flags & SDL_TRUE/* SDL_SRCCOLORKEY */) {
-        colorkey = _colorkey(src);
-        SDL_GetRGBA(colorkey, src->format, &r, &g, &b, &a);
-        colorKeyAvailable = 1;
+    if (SDL_GetColorKey(src, &colorkey) == 0) {
+        colorKeyAvailable = SDL_TRUE;
     }
 
-    /*
-    * Determine if source surface is 32bit or 8bit
-    */
-    is32bit = (src->format->BitsPerPixel == 32);
-    if ((is32bit) || (src->format->BitsPerPixel == 8)) {
-        /*
-        * Use source surface 'as is'
-        */
-        rz_src = src;
-    } else {
-        rz_src = SDL_ConvertSurfaceFormat(src, SDL_PIXELFORMAT_ARGB32, src->flags);
-        if (rz_src == NULL) {
-            return NULL;
-        }
-        is32bit = 1;
-    }
-
-    /* Determine target size */
-    /* _rotozoomSurfaceSizeTrig(rz_src->w, rz_src->h, angle, &dstwidth, &dstheight, &cangle, &sangle); */
+    /* This function requires a 32-bit surface or 8-bit surface with a colorkey */
+    is8bit = src->format->BitsPerPixel == 8 && colorKeyAvailable;
+    if (!(is8bit || (src->format->BitsPerPixel == 32 && src->format->Amask)))
+        return NULL;
 
-    /*
-    * Calculate target factors from sin/cos and zoom
-    */
+    /* Calculate target factors from sin/cos and zoom */
     sangleinv = sangle*65536.0;
     cangleinv = cangle*65536.0;
 
-    /*
-    * Alloc space to completely contain the rotated surface
-    */
-    if (is32bit) {
-        /*
-        * Target surface is 32bit with source RGBA/ABGR ordering
-        */
-        rz_dst =
-            SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 32,
-            rz_src->format->Rmask, rz_src->format->Gmask,
-            rz_src->format->Bmask, rz_src->format->Amask);
+    /* Alloc space to completely contain the rotated surface */
+    rz_dst = NULL;
+    if (is8bit) {
+        /* Target surface is 8 bit */
+        rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
+        if (rz_dst != NULL) {
+            for (i = 0; i < src->format->palette->ncolors; i++) {
+                rz_dst->format->palette->colors[i] = src->format->palette->colors[i];
+            }
+            rz_dst->format->palette->ncolors = src->format->palette->ncolors;
+        }
     } else {
-        /*
-        * Target surface is 8bit
-        */
-        rz_dst = SDL_CreateRGBSurface(SDL_SWSURFACE, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
+        /* Target surface is 32 bit with source RGBA ordering */
+        rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 32,
+                                      src->format->Rmask, src->format->Gmask,
+                                      src->format->Bmask, src->format->Amask);
     }
 
     /* Check target */
@@ -485,17 +467,32 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, 
     /* Adjust for guard rows */
     rz_dst->h = dstheight;
 
-    if (colorKeyAvailable == 1) {
-        colorkey = SDL_MapRGBA(rz_dst->format, r, g, b, a);
+    SDL_GetSurfaceBlendMode(src, &blendmode);
 
+    if (colorKeyAvailable == SDL_TRUE) {
+        /* If available, the colorkey will be used to discard the pixels that are outside of the rotated area. */
+        SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
+        SDL_FillRect(rz_dst, NULL, colorkey);
+    } else if (blendmode == SDL_BLENDMODE_NONE) {
+        blendmode = SDL_BLENDMODE_BLEND;
+    } else if (blendmode == SDL_BLENDMODE_MOD) {
+        /* Without a colorkey, the target texture has to be white for the MOD blend mode so
+         * that the pixels outside the rotated area don't affect the destination surface.
+         */
+        colorkey = SDL_MapRGBA(rz_dst->format, 255, 255, 255, 0);
         SDL_FillRect(rz_dst, NULL, colorkey);
+        /* Setting a white colorkey for the destination surface makes the final blit discard
+         * all pixels outside of the rotated area. This doesn't interfere with anything because
+         * white pixels are already a no-op and the MOD blend mode does not interact with alpha.
+         */
+        SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
     }
 
-    /*
-    * Lock source surface
-    */
-    if (SDL_MUSTLOCK(rz_src)) {
-        SDL_LockSurface(rz_src);
+    SDL_SetSurfaceBlendMode(rz_dst, blendmode);
+
+    /* Lock source surface */
+    if (SDL_MUSTLOCK(src)) {
+        SDL_LockSurface(src);
     }
 
     /* check if the rotation is a multiple of 90 degrees so we can take a fast path and also somewhat reduce
@@ -510,74 +507,29 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, 
         angle90 = -1;
     }
 
-    /*
-    * Check which kind of surface we have
-    */
-    if (is32bit) {
-        /*
-        * Call the 32bit transformation routine to do the rotation (using alpha)
-        */
-        if (angle90 >= 0) {
-            transformSurfaceRGBA90(rz_src, rz_dst, angle90, flipx, flipy);
+    if (is8bit) {
+        /* Call the 8-bit transformation routine to do the rotation */
+        if(angle90 >= 0) {
+            transformSurfaceY90(src, rz_dst, angle90, flipx, flipy);
         } else {
-            _transformSurfaceRGBA(rz_src, rz_dst, centerx, centery, (int) (sangleinv), (int) (cangleinv), flipx, flipy, smooth);
+            transformSurfaceY(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
+                              flipx, flipy);
         }
-        /*
-         * Turn on source-alpha support
-         */
-        /* SDL_SetAlpha(rz_dst, SDL_SRCALPHA, 255); */
     } else {
-        /*
-        * Copy palette and colorkey info
-        */
-        for (i = 0; i < rz_src->format->palette->ncolors; i++) {
-            rz_dst->format->palette->colors[i] = rz_src->format->palette->colors[i];
-        }
-        rz_dst->format->palette->ncolors = rz_src->format->palette->ncolors;
-        /*
-        * Call the 8bit transformation routine to do the rotation
-        */
-        if(angle90 >= 0) {
-            transformSurfaceY90(rz_src, rz_dst, angle90, flipx, flipy);
+        /* Call the 32-bit transformation routine to do the rotation */
+        if (angle90 >= 0) {
+            transformSurfaceRGBA90(src, rz_dst, angle90, flipx, flipy);
         } else {
-            transformSurfaceY(rz_src, rz_dst, centerx, centery, (int)(sangleinv), (int)(cangleinv), flipx, flipy);
+            _transformSurfaceRGBA(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
+                                  flipx, flipy, smooth);
         }
     }
 
-    if (colorKeyAvailable == 1) {
-       SDL_SetColorKey(rz_dst, /* SDL_SRCCOLORKEY */ SDL_TRUE | SDL_RLEACCEL, colorkey);
-    } else {
-       SDL_SetColorKey(rz_dst, SDL_FALSE, 0);
-    }
-
-    /* copy alpha mod, color mod, and blend mode */
-    {
-      SDL_BlendMode blendMode;
-      Uint8 alphaMod, cr, cg, cb;
-      SDL_GetSurfaceAlphaMod(src, &alphaMod);
-      SDL_GetSurfaceBlendMode(src, &blendMode);
-      SDL_GetSurfaceColorMod(src, &cr, &cg, &cb);
-      SDL_SetSurfaceAlphaMod(rz_dst, alphaMod);
-      SDL_SetSurfaceBlendMode(rz_dst, blendMode);
-      SDL_SetSurfaceColorMod(rz_dst, cr, cg, cb);
-    }
-
-    /*
-    * Unlock source surface
-    */
-    if (SDL_MUSTLOCK(rz_src)) {
-        SDL_UnlockSurface(rz_src);
-    }
-
-    /*
-    * Cleanup temp surface
-    */
-    if (rz_src != src) {
-        SDL_FreeSurface(rz_src);
+    /* Unlock source surface */
+    if (SDL_MUSTLOCK(src)) {
+        SDL_UnlockSurface(src);
     }
 
-    /*
-    * Return destination surface
-    */
-    return (rz_dst);
+    /* Return rotated surface */
+    return rz_dst;
 }