Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport size related PRs to SDL2 #11185

Merged
merged 3 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions src/video/SDL_blit.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,15 @@ extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface *surface);
#else
#define USE_DUFFS_LOOP
#endif

#define DUFFS_LOOP1(pixel_copy_increment, width) \
{ \
int n; \
for (n = width; n > 0; --n) { \
pixel_copy_increment; \
} \
}

#ifdef USE_DUFFS_LOOP

/* 8-times unrolled loop */
Expand Down Expand Up @@ -527,8 +536,26 @@ extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface *surface);
} \
}

/* Use the 8-times version of the loop by default */
/* 2-times unrolled loop */
#define DUFFS_LOOP2(pixel_copy_increment, width) \
{ \
int n = (width + 1) / 2; \
switch (width & 1) { \
case 0: \
do { \
pixel_copy_increment; \
SDL_FALLTHROUGH; \
case 1: \
pixel_copy_increment; \
} while (--n > 0); \
} \
}

/* Use the 4-times version of the loop by default */
#define DUFFS_LOOP(pixel_copy_increment, width) \
DUFFS_LOOP4(pixel_copy_increment, width)
/* Use the 8-times version of the loop for simple routines */
#define DUFFS_LOOP_TRIVIAL(pixel_copy_increment, width) \
DUFFS_LOOP8(pixel_copy_increment, width)

/* Special version of Duff's device for even more optimization */
Expand Down Expand Up @@ -562,20 +589,19 @@ extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface *surface);

/* Don't use Duff's device to unroll loops */
#define DUFFS_LOOP(pixel_copy_increment, width) \
{ \
int n; \
for (n = width; n > 0; --n) { \
pixel_copy_increment; \
} \
}
DUFFS_LOOP1(pixel_copy_increment, width)
#define DUFFS_LOOP_TRIVIAL(pixel_copy_increment, width) \
DUFFS_LOOP1(pixel_copy_increment, width)
#define DUFFS_LOOP8(pixel_copy_increment, width) \
DUFFS_LOOP(pixel_copy_increment, width)
DUFFS_LOOP1(pixel_copy_increment, width)
#define DUFFS_LOOP4(pixel_copy_increment, width) \
DUFFS_LOOP(pixel_copy_increment, width)
DUFFS_LOOP1(pixel_copy_increment, width)
#define DUFFS_LOOP2(pixel_copy_increment, width) \
DUFFS_LOOP1(pixel_copy_increment, width)
#define DUFFS_LOOP_124(pixel_copy_increment1, \
pixel_copy_increment2, \
pixel_copy_increment4, width) \
DUFFS_LOOP(pixel_copy_increment1, width)
DUFFS_LOOP1(pixel_copy_increment1, width)

#endif /* USE_DUFFS_LOOP */

Expand Down
67 changes: 19 additions & 48 deletions src/video/SDL_blit_0.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,34 +605,35 @@ SDL_FORCE_INLINE void BlitBto4Key(SDL_BlitInfo *info, const Uint32 srcbpp)
}
}

SDL_FORCE_INLINE void BlitBtoNAlpha(SDL_BlitInfo *info, const Uint32 srcbpp)
static void BlitBtoNAlpha(SDL_BlitInfo *info)
{
const Uint32 mask = (1 << srcbpp) - 1;
const Uint32 align = (8 / srcbpp) - 1;

int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
Uint8 *dst = info->dst;
int srcskip = info->src_skip;
int dstskip = info->dst_skip;
const SDL_Color *srcpal = info->src_fmt->palette->colors;
SDL_PixelFormat *srcfmt = info->src_fmt;
SDL_PixelFormat *dstfmt = info->dst_fmt;
int dstbpp;
int srcbpp, dstbpp;
int c;
Uint32 pixel;
Uint32 pixel, mask, align;
unsigned sR, sG, sB;
unsigned dR, dG, dB, dA;
const unsigned A = info->a;

/* Set up some basic variables */
srcbpp = srcfmt->BytesPerPixel;
dstbpp = dstfmt->BytesPerPixel;
if (srcbpp == 4)
srcskip += width - (width + 1) / 2;
else if (srcbpp == 2)
srcskip += width - (width + 3) / 4;
else if (srcbpp == 1)
srcskip += width - (width + 7) / 8;
mask = (1 << srcbpp) - 1;
align = (8 / srcbpp) - 1;

if (SDL_PIXELORDER(info->src_fmt->format) == SDL_BITMAPORDER_4321) {
while (height--) {
Expand Down Expand Up @@ -681,11 +682,8 @@ SDL_FORCE_INLINE void BlitBtoNAlpha(SDL_BlitInfo *info, const Uint32 srcbpp)
}
}

SDL_FORCE_INLINE void BlitBtoNAlphaKey(SDL_BlitInfo *info, const Uint32 srcbpp)
static void BlitBtoNAlphaKey(SDL_BlitInfo *info)
{
const Uint32 mask = (1 << srcbpp) - 1;
const Uint32 align = (8 / srcbpp) - 1;

int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
Expand All @@ -695,22 +693,25 @@ SDL_FORCE_INLINE void BlitBtoNAlphaKey(SDL_BlitInfo *info, const Uint32 srcbpp)
SDL_PixelFormat *srcfmt = info->src_fmt;
SDL_PixelFormat *dstfmt = info->dst_fmt;
const SDL_Color *srcpal = srcfmt->palette->colors;
int dstbpp;
int srcbpp, dstbpp;
int c;
Uint32 pixel;
Uint32 pixel, mask, align;
unsigned sR, sG, sB;
unsigned dR, dG, dB, dA;
const unsigned A = info->a;
Uint32 ckey = info->colorkey;

/* Set up some basic variables */
srcbpp = srcfmt->BytesPerPixel;
dstbpp = dstfmt->BytesPerPixel;
if (srcbpp == 4)
srcskip += width - (width + 1) / 2;
else if (srcbpp == 2)
srcskip += width - (width + 3) / 4;
else if (srcbpp == 1)
srcskip += width - (width + 7) / 8;
mask = (1 << srcbpp) - 1;
align = (8 / srcbpp) - 1;

if (SDL_PIXELORDER(info->src_fmt->format) == SDL_BITMAPORDER_4321) {
while (height--) {
Expand Down Expand Up @@ -801,16 +802,6 @@ static const SDL_BlitFunc colorkey_blit_1b[] = {
(SDL_BlitFunc)NULL, Blit1bto1Key, Blit1bto2Key, Blit1bto3Key, Blit1bto4Key
};

static void Blit1btoNAlpha(SDL_BlitInfo *info)
{
BlitBtoNAlpha(info, 1);
}

static void Blit1btoNAlphaKey(SDL_BlitInfo *info)
{
BlitBtoNAlphaKey(info, 1);
}



static void Blit2bto1(SDL_BlitInfo *info) {
Expand Down Expand Up @@ -853,16 +844,6 @@ static const SDL_BlitFunc colorkey_blit_2b[] = {
(SDL_BlitFunc)NULL, Blit2bto1Key, Blit2bto2Key, Blit2bto3Key, Blit2bto4Key
};

static void Blit2btoNAlpha(SDL_BlitInfo *info)
{
BlitBtoNAlpha(info, 2);
}

static void Blit2btoNAlphaKey(SDL_BlitInfo *info)
{
BlitBtoNAlphaKey(info, 2);
}



static void Blit4bto1(SDL_BlitInfo *info) {
Expand Down Expand Up @@ -905,16 +886,6 @@ static const SDL_BlitFunc colorkey_blit_4b[] = {
(SDL_BlitFunc)NULL, Blit4bto1Key, Blit4bto2Key, Blit4bto3Key, Blit4bto4Key
};

static void Blit4btoNAlpha(SDL_BlitInfo *info)
{
BlitBtoNAlpha(info, 4);
}

static void Blit4btoNAlphaKey(SDL_BlitInfo *info)
{
BlitBtoNAlphaKey(info, 4);
}



SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface *surface)
Expand All @@ -936,10 +907,10 @@ SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface *surface)
return colorkey_blit_1b[which];

case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
return which >= 2 ? Blit1btoNAlpha : (SDL_BlitFunc)NULL;
return which >= 2 ? BlitBtoNAlpha : (SDL_BlitFunc)NULL;

case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
return which >= 2 ? Blit1btoNAlphaKey : (SDL_BlitFunc)NULL;
return which >= 2 ? BlitBtoNAlphaKey : (SDL_BlitFunc)NULL;
}
return NULL;
}
Expand All @@ -953,10 +924,10 @@ SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface *surface)
return colorkey_blit_2b[which];

case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
return which >= 2 ? Blit2btoNAlpha : (SDL_BlitFunc)NULL;
return which >= 2 ? BlitBtoNAlpha : (SDL_BlitFunc)NULL;

case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
return which >= 2 ? Blit2btoNAlphaKey : (SDL_BlitFunc)NULL;
return which >= 2 ? BlitBtoNAlphaKey : (SDL_BlitFunc)NULL;
}
return NULL;
}
Expand All @@ -970,10 +941,10 @@ SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface *surface)
return colorkey_blit_4b[which];

case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
return which >= 2 ? Blit4btoNAlpha : (SDL_BlitFunc)NULL;
return which >= 2 ? BlitBtoNAlpha : (SDL_BlitFunc)NULL;

case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
return which >= 2 ? Blit4btoNAlphaKey : (SDL_BlitFunc)NULL;
return which >= 2 ? BlitBtoNAlphaKey : (SDL_BlitFunc)NULL;
}
return NULL;
}
Expand Down
16 changes: 8 additions & 8 deletions src/video/SDL_blit_1.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void Blit1to1(SDL_BlitInfo *info)
while (height--) {
#ifdef USE_DUFFS_LOOP
/* *INDENT-OFF* */ /* clang-format off */
DUFFS_LOOP(
DUFFS_LOOP_TRIVIAL(
{
*dst = map[*src];
}
Expand Down Expand Up @@ -102,7 +102,7 @@ static void Blit1to2(SDL_BlitInfo *info)
#ifdef USE_DUFFS_LOOP
while (height--) {
/* *INDENT-OFF* */ /* clang-format off */
DUFFS_LOOP(
DUFFS_LOOP_TRIVIAL(
{
*(Uint16 *)dst = map[*src++];
dst += 2;
Expand Down Expand Up @@ -258,7 +258,7 @@ static void Blit1to4(SDL_BlitInfo *info)
while (height--) {
#ifdef USE_DUFFS_LOOP
/* *INDENT-OFF* */ /* clang-format off */
DUFFS_LOOP(
DUFFS_LOOP_TRIVIAL(
*dst++ = map[*src++];
, width);
/* *INDENT-ON* */ /* clang-format on */
Expand Down Expand Up @@ -299,7 +299,7 @@ static void Blit1to1Key(SDL_BlitInfo *info)
if (palmap) {
while (height--) {
/* *INDENT-OFF* */ /* clang-format off */
DUFFS_LOOP(
DUFFS_LOOP_TRIVIAL(
{
if ( *src != ckey ) {
*dst = palmap[*src];
Expand All @@ -315,7 +315,7 @@ static void Blit1to1Key(SDL_BlitInfo *info)
} else {
while (height--) {
/* *INDENT-OFF* */ /* clang-format off */
DUFFS_LOOP(
DUFFS_LOOP_TRIVIAL(
{
if ( *src != ckey ) {
*dst = *src;
Expand Down Expand Up @@ -347,7 +347,7 @@ static void Blit1to2Key(SDL_BlitInfo *info)

while (height--) {
/* *INDENT-OFF* */ /* clang-format off */
DUFFS_LOOP(
DUFFS_LOOP_TRIVIAL(
{
if ( *src != ckey ) {
*dstp=palmap[*src];
Expand Down Expand Up @@ -410,7 +410,7 @@ static void Blit1to4Key(SDL_BlitInfo *info)

while (height--) {
/* *INDENT-OFF* */ /* clang-format off */
DUFFS_LOOP(
DUFFS_LOOP_TRIVIAL(
{
if ( *src != ckey ) {
*dstp = palmap[*src];
Expand Down Expand Up @@ -446,7 +446,7 @@ static void Blit1toNAlpha(SDL_BlitInfo *info)

while (height--) {
/* *INDENT-OFF* */ /* clang-format off */
DUFFS_LOOP4(
DUFFS_LOOP(
{
sR = srcpal[*src].r;
sG = srcpal[*src].g;
Expand Down
Loading
Loading