Skip to content

Commit e47c7c7

Browse files
committed
Added a fast path for non-indexed formats in SDL_MapRGB(A)
1 parent b306eff commit e47c7c7

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

src/sdl2_compat.c

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9535,7 +9535,6 @@ SDL_AllocFormat(Uint32 pixel_format)
95359535
format->Bshift = details->Bshift;
95369536
format->Ashift = details->Ashift;
95379537
format->refcount = 1;
9538-
format->next = (SDL2_PixelFormat *)details;
95399538

95409539
return format;
95419540
}
@@ -9993,34 +9992,59 @@ SDL_SetPixelFormatPalette(SDL2_PixelFormat *format, SDL_Palette *palette)
99939992

99949993
static const SDL_PixelFormatDetails *GetPixelFormatDetails(const SDL2_PixelFormat *format2)
99959994
{
9996-
return (SDL_PixelFormatDetails *)format2->next;
9995+
SDL_PixelFormat format = (SDL_PixelFormat)format2->format;
9996+
if (format == SDL_PIXELFORMAT_UNKNOWN) {
9997+
format = SDL3_GetPixelFormatForMasks(format2->BitsPerPixel, format2->Rmask, format2->Gmask, format2->Bmask, format2->Amask);
9998+
}
9999+
return SDL3_GetPixelFormatDetails(format);
999710000
}
999810001

999910002
SDL_DECLSPEC Uint32 SDLCALL
1000010003
SDL_MapRGB(const SDL2_PixelFormat *format2, Uint8 r, Uint8 g, Uint8 b)
1000110004
{
10002-
const SDL_PixelFormatDetails *format;
10005+
if (!format2) {
10006+
SDL3_InvalidParamError("format");
10007+
return 0;
10008+
}
1000310009

10004-
switch (format2->format) {
10005-
case SDL_PIXELFORMAT_XRGB8888:
10006-
return ((Uint32)r << 24) | ((Uint32)g << 16) | b;
10007-
default:
10008-
format = GetPixelFormatDetails(format2);
10010+
if (format2->palette) {
10011+
const SDL_PixelFormatDetails *format = GetPixelFormatDetails(format2);
1000910012
if (!format) {
1001010013
return 0;
1001110014
}
1001210015
return SDL3_MapRGB(format, format2->palette, r, g, b);
1001310016
}
10017+
10018+
switch (format2->format) {
10019+
case SDL_PIXELFORMAT_XRGB8888:
10020+
return ((Uint32)r << 16) | ((Uint32)g << 8) | b;
10021+
default:
10022+
return (r >> format2->Rloss) << format2->Rshift | (g >> format2->Gloss) << format2->Gshift | (b >> format2->Bloss) << format2->Bshift | format2->Amask;
10023+
}
1001410024
}
1001510025

1001610026
SDL_DECLSPEC Uint32 SDLCALL
1001710027
SDL_MapRGBA(const SDL2_PixelFormat *format2, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
1001810028
{
10019-
const SDL_PixelFormatDetails *format = GetPixelFormatDetails(format2);
10020-
if (!format) {
10029+
if (!format2) {
10030+
SDL3_InvalidParamError("format");
1002110031
return 0;
1002210032
}
10023-
return SDL3_MapRGBA(format, format2->palette, r, g, b, a);
10033+
10034+
if (format2->palette) {
10035+
const SDL_PixelFormatDetails *format = GetPixelFormatDetails(format2);
10036+
if (!format) {
10037+
return 0;
10038+
}
10039+
return SDL3_MapRGBA(format, format2->palette, r, g, b, a);
10040+
}
10041+
10042+
switch (format2->format) {
10043+
case SDL_PIXELFORMAT_ARGB8888:
10044+
return ((Uint32)a << 24) | ((Uint32)r << 16) | ((Uint32)g << 8) | b;
10045+
default:
10046+
return (r >> format2->Rloss) << format2->Rshift | (g >> format2->Gloss) << format2->Gshift | (b >> format2->Bloss) << format2->Bshift | (a >> format2->Aloss) << format2->Ashift;
10047+
}
1002410048
}
1002510049

1002610050
SDL_DECLSPEC void SDLCALL

0 commit comments

Comments
 (0)