Commit 5095592c authored by Sam Lantinga's avatar Sam Lantinga

A few fixes:

Fixed creating render texture framebuffer.
Removed the need for palette watch, added surface format caching.
Added an SDL_DONTFREE flag so you can't free the window and 1.2 shadow surfaces.
parent 13ba84bf
......@@ -253,24 +253,20 @@ typedef struct SDL_Color
} SDL_Color;
#define SDL_Colour SDL_Color
typedef struct SDL_Palette SDL_Palette;
typedef int (*SDL_PaletteChangedFunc) (void *userdata, SDL_Palette * palette);
typedef struct SDL_PaletteWatch SDL_PaletteWatch;
struct SDL_Palette
typedef struct SDL_Palette
{
int ncolors;
SDL_Color *colors;
Uint32 version;
int refcount;
SDL_PaletteWatch *watch;
};
} SDL_Palette;
/**
* \note Everything in the pixel format structure is read-only.
*/
typedef struct SDL_PixelFormat
{
Uint32 format;
SDL_Palette *palette;
Uint8 BitsPerPixel;
Uint8 BytesPerPixel;
......@@ -286,6 +282,8 @@ typedef struct SDL_PixelFormat
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
int refcount;
struct SDL_PixelFormat *next;
} SDL_PixelFormat;
/**
......@@ -321,6 +319,16 @@ extern DECLSPEC Uint32 SDLCALL SDL_MasksToPixelFormatEnum(int bpp,
Uint32 Bmask,
Uint32 Amask);
/**
* \brief Create an SDL_PixelFormat structure from a pixel format enum.
*/
extern DECLSPEC SDL_PixelFormat * SDLCALL SDL_AllocFormat(Uint32 pixel_format);
/**
* \brief Free an SDL_PixelFormat structure.
*/
extern DECLSPEC void SDLCALL SDL_FreeFormat(SDL_PixelFormat *format);
/**
* \brief Create a palette structure with the specified number of color
* entries.
......@@ -334,23 +342,10 @@ extern DECLSPEC Uint32 SDLCALL SDL_MasksToPixelFormatEnum(int bpp,
extern DECLSPEC SDL_Palette *SDLCALL SDL_AllocPalette(int ncolors);
/**
* \brief Add a callback function which is called when the palette changes.
*
* \sa SDL_DelPaletteWatch()
*/
extern DECLSPEC int SDLCALL SDL_AddPaletteWatch(SDL_Palette * palette,
SDL_PaletteChangedFunc
callback, void *userdata);
/**
* \brief Remove a callback function previously added with
* SDL_AddPaletteWatch().
*
* \sa SDL_AddPaletteWatch()
* \brief Set the palette for a pixel format structure.
*/
extern DECLSPEC void SDLCALL SDL_DelPaletteWatch(SDL_Palette * palette,
SDL_PaletteChangedFunc
callback, void *userdata);
extern DECLSPEC int SDLCALL SDL_SetPixelFormatPalette(SDL_PixelFormat * format,
SDL_Palette *palette);
/**
* \brief Set a range of colors in a palette.
......
......@@ -54,6 +54,7 @@ extern "C" {
/*@{*/
#define SDL_PREALLOC 0x00000001 /**< Surface uses preallocated memory */
#define SDL_RLEACCEL 0x00000002 /**< Surface is RLE encoded */
#define SDL_DONTFREE 0x00000004 /**< Surface is referenced internally */
/*@}*//*Surface flags*/
/**
......
......@@ -93,12 +93,7 @@ SDL_GetVideoInfo(void)
/* Memory leak, compatibility code, who cares? */
if (!info.vfmt && SDL_GetDesktopDisplayMode(GetVideoDisplay(), &mode) == 0) {
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask, &Bmask,
&Amask);
info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
info.vfmt = SDL_AllocFormat(mode.format);
info.current_w = mode.w;
info.current_h = mode.h;
}
......@@ -383,7 +378,7 @@ SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
int w, h;
/* We can't resize something we don't have... */
if (!SDL_VideoWindow) {
if (!SDL_VideoSurface) {
return -1;
}
......@@ -396,6 +391,9 @@ SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
if (flags != SDL_VideoFlags) {
return -1;
}
if (bpp != SDL_VideoSurface->format->BitsPerPixel) {
return -1;
}
/* Resize the window */
SDL_GetWindowSize(SDL_VideoWindow, &w, &h);
......@@ -469,6 +467,7 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
/* Destroy existing window */
SDL_PublicSurface = NULL;
if (SDL_ShadowSurface) {
SDL_ShadowSurface->flags &= ~SDL_DONTFREE;
SDL_FreeSurface(SDL_ShadowSurface);
SDL_ShadowSurface = NULL;
}
......@@ -564,6 +563,7 @@ SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
return NULL;
}
SDL_ShadowSurface->flags |= surface_flags;
SDL_ShadowSurface->flags |= SDL_DONTFREE;
/* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
if (SDL_ShadowSurface->format->palette) {
......@@ -670,7 +670,13 @@ SDL_DisplayFormatAlpha(SDL_Surface * surface)
optimised alpha format is written, add the converter here */
break;
}
format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
format = SDL_AllocFormat(SDL_MasksToPixelFormatEnum(32, rmask,
gmask,
bmask,
amask));
if (!format) {
return NULL;
}
converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
SDL_FreeFormat(format);
return converted;
......
......@@ -27,7 +27,6 @@
#include "SDL_log.h"
#include "SDL_render.h"
#include "SDL_sysrender.h"
#include "../video/SDL_pixels_c.h"
#include "software/SDL_render_sw_c.h"
......@@ -306,8 +305,6 @@ SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
SDL_bool needAlpha;
Uint32 i;
Uint32 format;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_Texture *texture;
CHECK_RENDERER_MAGIC(renderer, NULL);
......@@ -333,20 +330,13 @@ SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
}
}
if (!SDL_PixelFormatEnumToMasks(format, &bpp,
&Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown pixel format");
return NULL;
}
texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
surface->w, surface->h);
if (!texture) {
return NULL;
}
if (bpp == fmt->BitsPerPixel && Rmask == fmt->Rmask && Gmask == fmt->Gmask
&& Bmask == fmt->Bmask && Amask == fmt->Amask) {
if (format == surface->format->format) {
if (SDL_MUSTLOCK(surface)) {
SDL_LockSurface(surface);
SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
......@@ -359,7 +349,7 @@ SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
SDL_Surface *temp = NULL;
/* Set up a destination surface for the texture update */
SDL_InitFormat(&dst_fmt, bpp, Rmask, Gmask, Bmask, Amask);
SDL_InitFormat(&dst_fmt, format);
temp = SDL_ConvertSurface(surface, &dst_fmt, 0);
if (temp) {
SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
......
......@@ -24,7 +24,6 @@
#if !SDL_RENDER_DISABLED
#include "../SDL_sysrender.h"
#include "../../video/SDL_pixels_c.h"
#include "SDL_draw.h"
#include "SDL_blendfillrect.h"
......
......@@ -221,18 +221,8 @@ SDL_CalculateBlit(SDL_Surface * surface)
blit = SDL_CalculateBlitN(surface);
}
if (blit == NULL) {
Uint32 src_format =
SDL_MasksToPixelFormatEnum(surface->format->BitsPerPixel,
surface->format->Rmask,
surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask);
Uint32 dst_format =
SDL_MasksToPixelFormatEnum(dst->format->BitsPerPixel,
dst->format->Rmask,
dst->format->Gmask,
dst->format->Bmask,
dst->format->Amask);
Uint32 src_format = surface->format->format;
Uint32 dst_format = dst->format->format;
blit =
SDL_ChooseBlitFunc(src_format, dst_format, map->info.flags,
......
......@@ -105,7 +105,7 @@ typedef struct SDL_BlitMap
/* the version count matches the destination; mismatch indicates
an invalid mapping */
unsigned int format_version;
Uint32 palette_version;
} SDL_BlitMap;
/* Functions found in SDL_blit.c */
......@@ -129,10 +129,6 @@ extern SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface * surface);
#define DECLARE_ALIGNED(t,v,a) t v
#endif
#define FORMAT_EQUAL(A, B) \
((A)->BitsPerPixel == (B)->BitsPerPixel \
&& ((A)->Rmask == (B)->Rmask) && ((A)->Amask == (B)->Amask))
/* Load pixel of the specified format from a buffer and get its R-G-B values */
/* FIXME: rescale values to 0..255 here? */
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b) \
......
......@@ -437,17 +437,15 @@ SDL_SaveBMP_RW(SDL_Surface * saveme, SDL_RWops * dst, int freedst)
/* If the surface has a colorkey or alpha channel we'll save a
32-bit BMP with alpha channel, otherwise save a 24-bit BMP. */
if (save32bit) {
SDL_InitFormat(&format, 32,
0x00FF0000, 0x0000FF00, 0x000000FF,
0xFF000000);
} else {
SDL_InitFormat(&format, 24,
SDL_InitFormat(&format,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x00FF0000, 0x0000FF00, 0x000000FF,
SDL_PIXELFORMAT_ARGB8888
#else
0x000000FF, 0x0000FF00, 0x00FF0000,
SDL_PIXELFORMAT_BGRA8888
#endif
0);
);
} else {
SDL_InitFormat(&format, SDL_PIXELFORMAT_BGR24);
}
surface = SDL_ConvertSurface(saveme, &format, 0);
if (!surface) {
......
This diff is collapsed.
......@@ -26,14 +26,7 @@
#include "SDL_blit.h"
/* Pixel format functions */
extern SDL_PixelFormat *SDL_AllocFormat(int bpp,
Uint32 Rmask, Uint32 Gmask,
Uint32 Bmask, Uint32 Amask);
extern SDL_PixelFormat *SDL_InitFormat(SDL_PixelFormat * format, int bpp,
Uint32 Rmask, Uint32 Gmask,
Uint32 Bmask, Uint32 Amask);
extern void SDL_FormatChanged(SDL_Surface * surface);
extern void SDL_FreeFormat(SDL_PixelFormat * format);
extern int SDL_InitFormat(SDL_PixelFormat * format, Uint32 pixel_format);
/* Blit mapping functions */
extern SDL_BlitMap *SDL_AllocBlitMap(void);
......
......@@ -39,10 +39,18 @@ SDL_CreateRGBSurface(Uint32 flags,
Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
{
SDL_Surface *surface;
Uint32 format;
/* The flags are no longer used, make the compiler happy */
(void)flags;
/* Get the pixel format */
format = SDL_MasksToPixelFormatEnum(depth, Rmask, Gmask, Bmask, Amask);
if (format == SDL_PIXELFORMAT_UNKNOWN) {
SDL_SetError("Unknown pixel format");
return NULL;
}
/* Allocate the surface */
surface = (SDL_Surface *) SDL_calloc(1, sizeof(*surface));
if (surface == NULL) {
......@@ -50,7 +58,7 @@ SDL_CreateRGBSurface(Uint32 flags,
return NULL;
}
surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask);
surface->format = SDL_AllocFormat(format);
if (!surface->format) {
SDL_FreeSurface(surface);
return NULL;
......@@ -60,7 +68,7 @@ SDL_CreateRGBSurface(Uint32 flags,
surface->pitch = SDL_CalculatePitch(surface);
SDL_SetClipRect(surface, NULL);
if (surface->format->BitsPerPixel <= 8) {
if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
SDL_Palette *palette =
SDL_AllocPalette((1 << surface->format->BitsPerPixel));
if (!palette) {
......@@ -135,7 +143,6 @@ SDL_CreateRGBSurface(Uint32 flags,
SDL_FreeSurface(surface);
return NULL;
}
SDL_FormatChanged(surface);
/* By default surface with an alpha mask are set up for blending */
if (Amask) {
......@@ -171,46 +178,14 @@ SDL_CreateRGBSurfaceFrom(void *pixels,
return surface;
}
static int
SDL_SurfacePaletteChanged(void *userdata, SDL_Palette * palette)
{
SDL_Surface *surface = (SDL_Surface *) userdata;
SDL_FormatChanged(surface);
return 0;
}
int
SDL_SetSurfacePalette(SDL_Surface * surface, SDL_Palette * palette)
{
if (!surface || !surface->format) {
if (!surface) {
SDL_SetError("SDL_SetSurfacePalette() passed a NULL surface");
return -1;
}
if (palette && palette->ncolors != (1 << surface->format->BitsPerPixel)) {
SDL_SetError
("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
return -1;
}
if (surface->format->palette == palette) {
return 0;
}
if (surface->format->palette) {
SDL_DelPaletteWatch(surface->format->palette,
SDL_SurfacePaletteChanged, surface);
}
surface->format->palette = palette;
if (surface->format->palette) {
SDL_AddPaletteWatch(surface->format->palette,
SDL_SurfacePaletteChanged, surface);
}
return 0;
return SDL_SetPixelFormatPalette(surface->format, palette);
}
int
......@@ -556,7 +531,8 @@ SDL_LowerBlit(SDL_Surface * src, SDL_Rect * srcrect,
{
/* Check to make sure the blit mapping is valid */
if ((src->map->dst != dst) ||
(src->map->dst->format_version != src->map->format_version)) {
(dst->format->palette &&
src->map->palette_version != dst->format->palette->version)) {
if (SDL_MapSurface(src, dst) < 0) {
return (-1);
}
......@@ -801,21 +777,17 @@ SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
void * pixels, int pitch, SDL_Surface * surface,
SDL_PixelFormat * format, SDL_BlitMap * blitmap)
{
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
if (!SDL_PixelFormatEnumToMasks(pixel_format,
&bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
SDL_SetError("Indexed pixel formats not supported");
return SDL_FALSE;
}
if (bpp <= 8) {
SDL_SetError("Indexed pixel formats not supported");
if (SDL_InitFormat(format, pixel_format) < 0) {
return SDL_FALSE;
}
SDL_zerop(surface);
surface->flags = SDL_PREALLOC;
surface->format = SDL_InitFormat(format, bpp, Rmask, Gmask, Bmask, Amask);
surface->format = format;
surface->pixels = pixels;
surface->w = width;
surface->h = height;
......@@ -830,7 +802,6 @@ SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
blitmap->info.b = 0xFF;
blitmap->info.a = 0xFF;
surface->map = blitmap;
SDL_FormatChanged(surface);
/* The surface is ready to go */
surface->refcount = 1;
......@@ -905,6 +876,9 @@ SDL_FreeSurface(SDL_Surface * surface)
if (surface == NULL) {
return;
}
if (surface->flags & SDL_DONTFREE) {
return;
}
if (--surface->refcount > 0) {
return;
}
......
......@@ -208,7 +208,7 @@ static int
SDL_CreateWindowTexture(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
{
SDL_WindowTextureData *data;
SDL_Renderer *renderer;
SDL_Renderer *renderer = NULL;
SDL_RendererInfo info;
Uint32 i;
......@@ -1204,7 +1204,7 @@ SDL_RecreateWindow(SDL_Window * window, Uint32 flags)
/* Tear down the old native window */
if (window->surface) {
window->surface->refcount = 0;
window->surface->flags &= ~SDL_DONTFREE;
SDL_FreeSurface(window->surface);
}
if (_this->DestroyWindowFramebuffer) {
......@@ -1622,13 +1622,13 @@ SDL_GetWindowSurface(SDL_Window * window)
if (!window->surface_valid) {
if (window->surface) {
window->surface->refcount = 0;
window->surface->flags &= ~SDL_DONTFREE;
SDL_FreeSurface(window->surface);
}
window->surface = SDL_CreateWindowFramebuffer(window);
if (window->surface) {
window->surface_valid = SDL_TRUE;
window->surface->refcount = 0x7FFFFFF;
window->surface->flags |= SDL_DONTFREE;
}
}
return window->surface;
......@@ -1778,7 +1778,7 @@ SDL_DestroyWindow(SDL_Window * window)
SDL_UpdateFullscreenMode(window, SDL_FALSE);
if (window->surface) {
window->surface->refcount = 0;
window->surface->flags &= ~SDL_DONTFREE;
SDL_FreeSurface(window->surface);
}
if (_this->DestroyWindowFramebuffer) {
......
......@@ -220,8 +220,7 @@ DirectFB_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
int pitch, i;
/* Convert the icon to ARGB for modern window managers */
SDL_InitFormat(&format, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
0xFF000000);
SDL_InitFormat(&format, SDL_PIXELFORMAT_ARGB8888);
surface = SDL_ConvertSurface(icon, &format, 0);
if (!surface) {
return;
......
......@@ -332,8 +332,7 @@ WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
SDL_WriteLE32(dst, 0);
/* Convert the icon to a 32-bit surface with alpha channel */
SDL_InitFormat(&format, 32,
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
SDL_InitFormat(&format, SDL_PIXELFORMAT_ARGB8888);
surface = SDL_ConvertSurface(icon, &format, 0);
if (surface) {
/* Write the pixels upside down into the bitmap buffer */
......
......@@ -666,8 +666,7 @@ X11_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
long *propdata;
/* Convert the icon to ARGB for modern window managers */
SDL_InitFormat(&format, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
0xFF000000);
SDL_InitFormat(&format, SDL_PIXELFORMAT_ARGB8888);
surface = SDL_ConvertSurface(icon, &format, 0);
if (!surface) {
return;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment