Skip to content

Commit

Permalink
A few #defines to reduce SDL2 footprint.
Browse files Browse the repository at this point in the history
Only applied when library is statically linked
  • Loading branch information
1bsyl committed Jan 21, 2020
1 parent 25c88ea commit 7df22cf
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/SDL_internal.h
Expand Up @@ -51,6 +51,17 @@

#include "SDL_config.h"

/* A few #defines to reduce SDL2 footprint.
Only applied when library is statically linked */
#define SDL_HAVE_BLIT_0 1
#define SDL_HAVE_BLIT_1 1
#define SDL_HAVE_BLIT_A 1
#define SDL_HAVE_BLIT_N 1
#define SDL_HAVE_BLIT_AUTO 1
#define SDL_HAVE_RLE 1
#define SDL_VIDEO_RENDER_SW 1
#define SDL_HAVE_YUV 1

#endif /* SDL_internal_h_ */

/* vi: set ts=4 sw=4 expandtab: */
26 changes: 23 additions & 3 deletions src/render/SDL_render.c
Expand Up @@ -105,7 +105,9 @@ static const SDL_RenderDriver *render_drivers[] = {
#if SDL_VIDEO_RENDER_PSP
&PSP_RenderDriver,
#endif
#if SDL_VIDEO_RENDER_SW
&SW_RenderDriver
#endif
};
#endif /* !SDL_RENDER_DISABLED */

Expand Down Expand Up @@ -907,7 +909,7 @@ SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
SDL_Renderer *
SDL_CreateSoftwareRenderer(SDL_Surface * surface)
{
#if !SDL_RENDER_DISABLED
#if !SDL_RENDER_DISABLED && SDL_VIDEO_RENDER_SW
SDL_Renderer *renderer;

renderer = SW_CreateRendererForSurface(surface);
Expand Down Expand Up @@ -1114,7 +1116,11 @@ SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int
renderer->textures = texture;

if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
#if SDL_HAVE_YUV
texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
#else
SDL_SetError("SDL not built with YUV support");
#endif
if (!texture->yuv) {
SDL_DestroyTexture(texture);
return NULL;
Expand Down Expand Up @@ -1532,8 +1538,10 @@ SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,

if ((rect->w == 0) || (rect->h == 0)) {
return 0; /* nothing to do. */
#if SDL_HAVE_YUV
} else if (texture->yuv) {
return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
#endif
} else if (texture->native) {
return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
} else {
Expand Down Expand Up @@ -1602,6 +1610,7 @@ int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch)
{
#if SDL_HAVE_YUV
SDL_Renderer *renderer;
SDL_Rect full_rect;

Expand Down Expand Up @@ -1658,6 +1667,9 @@ int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
return SDL_Unsupported();
}
}
#else
return -1;
#endif
}

static int
Expand Down Expand Up @@ -1699,12 +1711,15 @@ SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
rect = &full_rect;
}

#if SDL_HAVE_YUV
if (texture->yuv) {
if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
return -1;
}
return SDL_LockTextureYUV(texture, rect, pixels, pitch);
} else if (texture->native) {
} else
#endif
if (texture->native) {
/* Calls a real SDL_LockTexture/SDL_UnlockTexture on unlock, flushing then. */
return SDL_LockTextureNative(texture, rect, pixels, pitch);
} else {
Expand Down Expand Up @@ -1802,9 +1817,12 @@ SDL_UnlockTexture(SDL_Texture * texture)
if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
return;
}
#if SDL_HAVE_YUV
if (texture->yuv) {
SDL_UnlockTextureYUV(texture);
} else if (texture->native) {
} else
#endif
if (texture->native) {
SDL_UnlockTextureNative(texture);
} else {
SDL_Renderer *renderer = texture->renderer;
Expand Down Expand Up @@ -3159,9 +3177,11 @@ SDL_DestroyTexture(SDL_Texture * texture)
if (texture->native) {
SDL_DestroyTexture(texture->native);
}
#if SDL_HAVE_YUV
if (texture->yuv) {
SDL_SW_DestroyYUVTexture(texture->yuv);
}
#endif
SDL_free(texture->pixels);

renderer->DestroyTexture(renderer, texture);
Expand Down
4 changes: 2 additions & 2 deletions src/video/SDL_RLEaccel.c
Expand Up @@ -445,7 +445,7 @@ RLEClipBlit(int w, Uint8 * srcbuf, SDL_Surface * surf_dst,


/* blit a colorkeyed RLE surface */
int SDLCALL
static int SDLCALL
SDL_RLEBlit(SDL_Surface * surf_src, SDL_Rect * srcrect,
SDL_Surface * surf_dst, SDL_Rect * dstrect)
{
Expand Down Expand Up @@ -723,7 +723,7 @@ RLEAlphaClipBlit(int w, Uint8 * srcbuf, SDL_Surface * surf_dst,
}

/* blit a pixel-alpha RLE surface */
int SDLCALL
static int SDLCALL
SDL_RLEAlphaBlit(SDL_Surface * surf_src, SDL_Rect * srcrect,
SDL_Surface * surf_dst, SDL_Rect * dstrect)
{
Expand Down
4 changes: 0 additions & 4 deletions src/video/SDL_RLEaccel_c.h
Expand Up @@ -27,10 +27,6 @@
/* Useful functions and variables from SDL_RLEaccel.c */

extern int SDL_RLESurface(SDL_Surface * surface);
extern int SDLCALL SDL_RLEBlit (SDL_Surface * src, SDL_Rect * srcrect,
SDL_Surface * dst, SDL_Rect * dstrect);
extern int SDLCALL SDL_RLEAlphaBlit(SDL_Surface * src, SDL_Rect * srcrect,
SDL_Surface * dst, SDL_Rect * dstrect);
extern void SDL_UnRLESurface(SDL_Surface * surface, int recode);

#endif /* SDL_RLEaccel_c_h_ */
Expand Down
28 changes: 24 additions & 4 deletions src/video/SDL_blit.c
Expand Up @@ -202,22 +202,27 @@ SDL_CalculateBlit(SDL_Surface * surface)
return SDL_SetError("Blit combination not supported");
}

#if SDL_HAVE_RLE
/* Clean everything out to start */
if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
SDL_UnRLESurface(surface, 1);
}
#endif

map->blit = SDL_SoftBlit;
map->info.src_fmt = surface->format;
map->info.src_pitch = surface->pitch;
map->info.dst_fmt = dst->format;
map->info.dst_pitch = dst->pitch;

#if SDL_HAVE_RLE
/* See if we can do RLE acceleration */
if (map->info.flags & SDL_COPY_RLE_DESIRED) {
if (SDL_RLESurface(surface) == 0) {
return 0;
}
}
#endif

/* Choose a standard blit function */
if (map->identity && !(map->info.flags & ~SDL_COPY_RLE_DESIRED)) {
Expand All @@ -226,17 +231,30 @@ SDL_CalculateBlit(SDL_Surface * surface)
/* Greater than 8 bits per channel not supported yet */
SDL_InvalidateMap(map);
return SDL_SetError("Blit combination not supported");
} else if (surface->format->BitsPerPixel < 8 &&
}
#if SDL_HAVE_BLIT_0
else if (surface->format->BitsPerPixel < 8 &&
SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
blit = SDL_CalculateBlit0(surface);
} else if (surface->format->BytesPerPixel == 1 &&
}
#endif
#if SDL_HAVE_BLIT_1
else if (surface->format->BytesPerPixel == 1 &&
SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
blit = SDL_CalculateBlit1(surface);
} else if (map->info.flags & SDL_COPY_BLEND) {
}
#endif
#if SDL_HAVE_BLIT_A
else if (map->info.flags & SDL_COPY_BLEND) {
blit = SDL_CalculateBlitA(surface);
} else {
}
#endif
#if SDL_HAVE_BLIT_N
else {
blit = SDL_CalculateBlitN(surface);
}
#endif
#if SDL_HAVE_BLIT_AUTO
if (blit == NULL) {
Uint32 src_format = surface->format->format;
Uint32 dst_format = dst->format->format;
Expand All @@ -245,6 +263,8 @@ SDL_CalculateBlit(SDL_Surface * surface)
SDL_ChooseBlitFunc(src_format, dst_format, map->info.flags,
SDL_GeneratedBlitFuncTable);
}
#endif

#ifndef TEST_SLOW_BLIT
if (blit == NULL)
#endif
Expand Down
2 changes: 2 additions & 0 deletions src/video/SDL_pixels.c
Expand Up @@ -1003,9 +1003,11 @@ SDL_MapSurface(SDL_Surface * src, SDL_Surface * dst)

/* Clear out any previous mapping */
map = src->map;
#if SDL_HAVE_RLE
if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
SDL_UnRLESurface(src, 1);
}
#endif
SDL_InvalidateMap(map);

/* Figure out what kind of mapping we're doing */
Expand Down
13 changes: 13 additions & 0 deletions src/video/SDL_surface.c
Expand Up @@ -905,11 +905,13 @@ int
SDL_LockSurface(SDL_Surface * surface)
{
if (!surface->locked) {
#if SDL_HAVE_RLE
/* Perform the lock */
if (surface->flags & SDL_RLEACCEL) {
SDL_UnRLESurface(surface, 1);
surface->flags |= SDL_RLEACCEL; /* save accel'd state */
}
#endif
}

/* Increment the surface lock count, for recursive locks */
Expand All @@ -930,11 +932,13 @@ SDL_UnlockSurface(SDL_Surface * surface)
return;
}

#if SDL_HAVE_RLE
/* Update RLE encoded surface with new data */
if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
surface->flags &= ~SDL_RLEACCEL; /* stop lying */
SDL_RLESurface(surface);
}
#endif
}

/*
Expand Down Expand Up @@ -1210,13 +1214,20 @@ int SDL_ConvertPixels(int width, int height,
return SDL_InvalidParamError("dst_pitch");
}

#if SDL_HAVE_YUV
if (SDL_ISPIXELFORMAT_FOURCC(src_format) && SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
return SDL_ConvertPixels_YUV_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
} else if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
return SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
} else if (SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
return SDL_ConvertPixels_RGB_to_YUV(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
}
#else
if (SDL_ISPIXELFORMAT_FOURCC(src_format) || SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
SDL_SetError("SDL not built with YUV support");
return -1;
}
#endif

/* Fast path for same format copy */
if (src_format == dst_format) {
Expand Down Expand Up @@ -1269,9 +1280,11 @@ SDL_FreeSurface(SDL_Surface * surface)
while (surface->locked > 0) {
SDL_UnlockSurface(surface);
}
#if SDL_HAVE_RLE
if (surface->flags & SDL_RLEACCEL) {
SDL_UnRLESurface(surface, 0);
}
#endif
if (surface->format) {
SDL_SetSurfacePalette(surface, NULL);
SDL_FreeFormat(surface->format);
Expand Down

0 comments on commit 7df22cf

Please sign in to comment.