From 929aeb1591e739588cd8cf629e7d7ac5a95261b1 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 26 Aug 2013 16:09:58 +0300 Subject: add surface allocation debug code. --- src/CMakeLists.txt | 2 + src/Makefile.am | 2 + src/client.cpp | 5 +- src/compoundsprite.cpp | 8 +- src/debug.h | 31 +++++- src/game.cpp | 3 +- src/gui/minimap.cpp | 5 +- src/gui/sdlfont.cpp | 18 ++-- src/localconsts.h | 3 + src/mobileopenglgraphics.cpp | 3 +- src/normalopenglgraphics.cpp | 3 +- src/resources/atlasmanager.cpp | 5 +- src/resources/image.cpp | 6 +- src/resources/imagehelper.cpp | 8 +- src/resources/openglimagehelper.cpp | 11 ++- src/resources/resourcemanager.cpp | 5 +- src/resources/sdl2imagehelper.cpp | 12 ++- src/resources/sdlimagehelper.cpp | 12 +-- src/resources/surfaceimagehelper.cpp | 12 ++- src/safeopenglgraphics.cpp | 4 +- src/sdl2graphics.cpp | 44 +++++++-- src/sdlgraphics.cpp | 4 +- src/utils/sdlcheckutils.cpp | 184 +++++++++++++++++++++++++++++++++++ src/utils/sdlcheckutils.h | 67 +++++++++++++ 24 files changed, 395 insertions(+), 62 deletions(-) create mode 100644 src/utils/sdlcheckutils.cpp create mode 100644 src/utils/sdlcheckutils.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 55b0eeca2..40c7bf942 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -538,6 +538,8 @@ SET(SRCS utils/process.h utils/sdl2helper.cpp utils/sdl2helper.h + utils/sdlcheckutils.cpp + utils/sdlcheckutils.h utils/sdlhelper.cpp utils/sdlhelper.h utils/stringutils.cpp diff --git a/src/Makefile.am b/src/Makefile.am index b211a8689..3d7240abb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -549,6 +549,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ utils/process.h \ utils/sdl2helper.cpp \ utils/sdl2helper.h \ + utils/sdlcheckutils.cpp \ + utils/sdlcheckutils.h \ utils/sdlhelper.cpp \ utils/sdlhelper.h \ utils/specialfolder.cpp \ diff --git a/src/client.cpp b/src/client.cpp index c98dfeb82..fef6f98ed 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -106,6 +106,7 @@ #include "utils/paths.h" #include "utils/physfstools.h" #include "utils/process.h" +#include "utils/sdlcheckutils.h" #include "utils/sdlhelper.h" #include "utils/translation/translationmanager.h" @@ -833,7 +834,7 @@ void Client::gameClear() if (logger) logger->log1("Quitting8"); - SDL_FreeSurface(mIcon); + MSDL_FreeSurface(mIcon); if (logger) logger->log1("Quitting9"); @@ -3078,7 +3079,7 @@ void Client::setIcon() if (icon) SetClassLong(pInfo.window, GCL_HICON, reinterpret_cast(icon)); #else - mIcon = IMG_Load(iconFile.c_str()); + mIcon = MIMG_Load(iconFile.c_str()); if (mIcon) { #ifdef USE_SDL2 diff --git a/src/compoundsprite.cpp b/src/compoundsprite.cpp index e89e1154b..df62ab9ff 100644 --- a/src/compoundsprite.cpp +++ b/src/compoundsprite.cpp @@ -327,7 +327,7 @@ void CompoundSprite::redraw() const const int amask = 0xff000000; #endif - SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_HWSURFACE, + SDL_Surface *const surface = MSDL_CreateRGBSurface(SDL_HWSURFACE, BUFFER_WIDTH, BUFFER_HEIGHT, 32, rmask, gmask, bmask, amask); if (!surface) @@ -363,7 +363,7 @@ void CompoundSprite::redraw() const delete graphics; graphics = nullptr; - SDL_Surface *const surfaceA = SDL_CreateRGBSurface(SDL_HWSURFACE, + SDL_Surface *const surfaceA = MSDL_CreateRGBSurface(SDL_HWSURFACE, BUFFER_WIDTH, BUFFER_HEIGHT, 32, rmask, gmask, bmask, amask); #ifdef USE_SDL2 @@ -377,12 +377,12 @@ void CompoundSprite::redraw() const delete mAlphaImage; mImage = imageHelper->load(surface); - SDL_FreeSurface(surface); + MSDL_FreeSurface(surface); if (ImageHelper::mEnableAlpha) { mAlphaImage = imageHelper->load(surfaceA); - SDL_FreeSurface(surfaceA); + MSDL_FreeSurface(surfaceA); } else { diff --git a/src/debug.h b/src/debug.h index 35edf12a0..a250aef68 100644 --- a/src/debug.h +++ b/src/debug.h @@ -33,9 +33,36 @@ #define reportTrue(val) reportTrue1(val, __FILE__, __LINE__) #define reportTrue1(val, file, line) reportTrueReal(val, file, line) -#else +#else // ENABLE_MEM_DEBUG #define reportFalse(val) (val) #define reportTrue(val) (val) -#endif +#endif // ENABLE_MEM_DEBUG + + +#ifdef DEBUG_SDL_SURFACES + +#define MIMG_LoadPNG_RW(src) FakeIMG_LoadPNG_RW(src, __FILE__, __LINE__) +#define MSDL_FreeSurface(s) FakeSDL_FreeSurface(s, __FILE__, __LINE__) +#define MSDL_CreateRGBSurface(flags, w, h, d, r, g, b, a) \ + FakeSDL_CreateRGBSurface(flags, w, h, d, r, g, b, a, __FILE__, __LINE__) +#define MSDL_ConvertSurface(src, fmt, flags) \ + FakeSDL_ConvertSurface(src, fmt, flags, __FILE__, __LINE__) +#define MTTF_RenderUTF8_Blended(font, text, fg) \ + FakeTTF_RenderUTF8_Blended(font, text, fg, __FILE__, __LINE__) +#define MIMG_Load(file) FakeIMG_Load(file, __FILE__, __LINE__) + +#else + +#define MIMG_LoadPNG_RW(src) IMG_LoadPNG_RW(src) +#define MSDL_FreeSurface(surface) SDL_FreeSurface(surface) +#define MSDL_CreateRGBSurface(flags, w, h, d, r, g, b, a) \ + SDL_CreateRGBSurface(flags, w, h, d, r, g, b, a) +#define MSDL_ConvertSurface(src, fmt, flags) \ + SDL_ConvertSurface(src, fmt, flags) +#define MTTF_RenderUTF8_Blended(font, text, fg) \ + TTF_RenderUTF8_Blended(font, text, fg) +#define MIMG_Load(file) IMG_Load(file) + +#endif // ENABLE_SDL_DEBUG diff --git a/src/game.cpp b/src/game.cpp index 9a18e9da7..962d8562e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -96,6 +96,7 @@ #include "utils/mkdir.h" #include "utils/physfstools.h" #include "utils/process.h" +#include "utils/sdlcheckutils.h" #include #include @@ -553,7 +554,7 @@ bool Game::saveScreenshot(SDL_Surface *const screenshot) logger->log1("Error: could not save screenshot."); } - SDL_FreeSurface(screenshot); + MSDL_FreeSurface(screenshot); return success; } diff --git a/src/gui/minimap.cpp b/src/gui/minimap.cpp index 8b2ddef5e..641f9f9f9 100644 --- a/src/gui/minimap.cpp +++ b/src/gui/minimap.cpp @@ -37,6 +37,7 @@ #include "resources/resourcemanager.h" #include "utils/gettext.h" +#include "utils/sdlcheckutils.h" #include @@ -124,7 +125,7 @@ void Minimap::setMap(const Map *const map) { if (config.getBoolValue("showExtMinimaps")) { - SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_SWSURFACE, + SDL_Surface *const surface = MSDL_CreateRGBSurface(SDL_SWSURFACE, map->getWidth(), map->getHeight(), 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000); if (!surface) @@ -155,7 +156,7 @@ void Minimap::setMap(const Map *const map) mMapImage = imageHelper->load(surface); mMapImage->setAlpha(client->getGuiAlpha()); mCustomMapImage = true; - SDL_FreeSurface(surface); + MSDL_FreeSurface(surface); } else { diff --git a/src/gui/sdlfont.cpp b/src/gui/sdlfont.cpp index b6e0b8986..c0ca34bbb 100644 --- a/src/gui/sdlfont.cpp +++ b/src/gui/sdlfont.cpp @@ -27,13 +27,15 @@ #include "logger.h" #include "main.h" #include "sdlgraphics.h" -#include "utils/paths.h" #include "resources/image.h" #include "resources/imagehelper.h" #include "resources/resourcemanager.h" #include "resources/surfaceimagehelper.h" +#include "utils/paths.h" +#include "utils/sdlcheckutils.h" + #include #include "debug.h" @@ -146,7 +148,7 @@ void SDLTextChunk::generate(TTF_Font *const font, const float alpha) getSafeUtf8String(text, strBuf); - SDL_Surface *surface = TTF_RenderUTF8_Blended( + SDL_Surface *surface = MTTF_RenderUTF8_Blended( font, strBuf, sdlCol); if (!surface) @@ -168,7 +170,7 @@ void SDLTextChunk::generate(TTF_Font *const font, const float alpha) if (!background) { img = nullptr; - SDL_FreeSurface(surface); + MSDL_FreeSurface(surface); BLOCK_END("SDLTextChunk::generate") return; } @@ -180,12 +182,12 @@ void SDLTextChunk::generate(TTF_Font *const font, const float alpha) #else sdlCol2.unused = 0; #endif - SDL_Surface *const surface2 = TTF_RenderUTF8_Blended( + SDL_Surface *const surface2 = MTTF_RenderUTF8_Blended( font, strBuf, sdlCol2); if (!surface2) { img = nullptr; - SDL_FreeSurface(surface); + MSDL_FreeSurface(surface); BLOCK_END("SDLTextChunk::generate") return; } @@ -212,13 +214,13 @@ void SDLTextChunk::generate(TTF_Font *const font, const float alpha) rect.y = 0; SurfaceImageHelper::combineSurface(surface, nullptr, background, &rect); - SDL_FreeSurface(surface); - SDL_FreeSurface(surface2); + MSDL_FreeSurface(surface); + MSDL_FreeSurface(surface2); surface = background; } img = imageHelper->createTextSurface( surface, width, height, alpha); - SDL_FreeSurface(surface); + MSDL_FreeSurface(surface); BLOCK_END("SDLTextChunk::generate") } diff --git a/src/localconsts.h b/src/localconsts.h index b367e98a2..d5ae0a904 100644 --- a/src/localconsts.h +++ b/src/localconsts.h @@ -84,4 +84,7 @@ // debug images usage // #define DEBUG_IMAGES 1 +// debug SDL surfaces +// #define DEBUG_SDL_SURFACES 1 + #include "utils/perfomance.h" diff --git a/src/mobileopenglgraphics.cpp b/src/mobileopenglgraphics.cpp index e2991eddc..c53de9e0e 100644 --- a/src/mobileopenglgraphics.cpp +++ b/src/mobileopenglgraphics.cpp @@ -34,6 +34,7 @@ #include "resources/image.h" #include "resources/openglimagehelper.h" +#include "utils/sdlcheckutils.h" #include "utils/stringutils.h" #include @@ -903,7 +904,7 @@ SDL_Surface* MobileOpenGLGraphics::getScreenshot() const int w = mRect.w - (mRect.w % 4); GLint pack = 1; - SDL_Surface *const screenshot = SDL_CreateRGBSurface( + SDL_Surface *const screenshot = MSDL_CreateRGBSurface( SDL_SWSURFACE, w, h, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000); diff --git a/src/normalopenglgraphics.cpp b/src/normalopenglgraphics.cpp index a4b9e8c07..9526d4ee3 100644 --- a/src/normalopenglgraphics.cpp +++ b/src/normalopenglgraphics.cpp @@ -33,6 +33,7 @@ #include "resources/image.h" #include "resources/openglimagehelper.h" +#include "utils/sdlcheckutils.h" #include "utils/stringutils.h" #include @@ -1113,7 +1114,7 @@ SDL_Surface* NormalOpenGLGraphics::getScreenshot() const int w = mRect.w - (mRect.w % 4); GLint pack = 1; - SDL_Surface *const screenshot = SDL_CreateRGBSurface( + SDL_Surface *const screenshot = MSDL_CreateRGBSurface( SDL_SWSURFACE, w, h, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000); diff --git a/src/resources/atlasmanager.cpp b/src/resources/atlasmanager.cpp index 01ea66233..80b0ad253 100644 --- a/src/resources/atlasmanager.cpp +++ b/src/resources/atlasmanager.cpp @@ -31,6 +31,7 @@ #include "utils/mathutils.h" #include "utils/physfsrwops.h" +#include "utils/sdlcheckutils.h" #include "resources/dye.h" #include "resources/fboinfo.h" @@ -79,7 +80,7 @@ AtlasResource *AtlasManager::loadTextureAtlas(const std::string &name, convertAtlas(atlas); // free SDL atlas surface - SDL_FreeSurface(surface); + MSDL_FreeSurface(surface); resource->atlases.push_back(atlas); } @@ -226,7 +227,7 @@ SDL_Surface *AtlasManager::createSDLAtlas(TextureAtlas *const atlas) const int width = atlas->width; const int height = atlas->height; // temp SDL surface for atlas - SDL_Surface *const surface = SDL_CreateRGBSurface(SDL_SWSURFACE, + SDL_Surface *const surface = MSDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, rmask, gmask, bmask, amask); if (!surface) return nullptr; diff --git a/src/resources/image.cpp b/src/resources/image.cpp index 759a3a055..b5a852996 100644 --- a/src/resources/image.cpp +++ b/src/resources/image.cpp @@ -39,6 +39,8 @@ #include "resources/sdlimagehelper.h" #include "resources/subimage.h" +#include "utils/sdlcheckutils.h" + #include #ifndef USE_SDL2 #include @@ -208,7 +210,7 @@ void Image::unload() { SDLCleanCache(); // Free the image surface. - SDL_FreeSurface(mSDLSurface); + MSDL_FreeSurface(mSDLSurface); mSDLSurface = nullptr; delete [] mAlphaChannel; @@ -398,7 +400,7 @@ Image* Image::SDLgetScaledImage(const int width, const int height) const if (scaledSurface) { scaledImage = imageHelper->load(scaledSurface); - SDL_FreeSurface(scaledSurface); + MSDL_FreeSurface(scaledSurface); } } #endif diff --git a/src/resources/imagehelper.cpp b/src/resources/imagehelper.cpp index 65af2755a..8a97b4a81 100644 --- a/src/resources/imagehelper.cpp +++ b/src/resources/imagehelper.cpp @@ -31,6 +31,8 @@ #include "resources/image.h" +#include "utils/sdlcheckutils.h" + #include #include "debug.h" @@ -51,7 +53,7 @@ Image *ImageHelper::load(SDL_RWops *const rw) const Image *const image = load(tmpImage); - SDL_FreeSurface(tmpImage); + MSDL_FreeSurface(tmpImage); return image; } @@ -94,7 +96,7 @@ SDL_Surface* ImageHelper::convertTo32Bit(SDL_Surface *const tmpImage) RGBAFormat.Ashift = 0; RGBAFormat.Aloss = 0; #endif - return SDL_ConvertSurface(tmpImage, &RGBAFormat, SDL_SWSURFACE); + return MSDL_ConvertSurface(tmpImage, &RGBAFormat, SDL_SWSURFACE); } void ImageHelper::dumpSurfaceFormat(const SDL_Surface *const image) const @@ -128,7 +130,7 @@ SDL_Surface *ImageHelper::loadPng(SDL_RWops *const rw) logger->log("Error, image is not png"); return nullptr; } - SDL_Surface *const tmpImage = IMG_LoadPNG_RW(rw); + SDL_Surface *const tmpImage = MIMG_LoadPNG_RW(rw); SDL_RWclose(rw); return tmpImage; } diff --git a/src/resources/openglimagehelper.cpp b/src/resources/openglimagehelper.cpp index 0debbe988..6e60cc647 100644 --- a/src/resources/openglimagehelper.cpp +++ b/src/resources/openglimagehelper.cpp @@ -37,6 +37,7 @@ #include "resources/image.h" #include "resources/resourcemanager.h" +#include "utils/sdlcheckutils.h" #include "utils/stringutils.h" #include @@ -60,7 +61,7 @@ Image *OpenGLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const } SDL_Surface *const surf = convertTo32Bit(tmpImage); - SDL_FreeSurface(tmpImage); + MSDL_FreeSurface(tmpImage); uint32_t *pixels = static_cast(surf->pixels); const int type = dye.getType(); @@ -90,7 +91,7 @@ Image *OpenGLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const } Image *const image = load(surf); - SDL_FreeSurface(surf); + MSDL_FreeSurface(surf); return image; } @@ -182,7 +183,7 @@ Image *OpenGLImageHelper::glLoad(SDL_Surface *tmpImage, #ifdef USE_SDL2 SDL_SetSurfaceBlendMode (oldImage, SDL_BLENDMODE_NONE); #endif - tmpImage = SDL_CreateRGBSurface(SDL_SWSURFACE, realWidth, realHeight, + tmpImage = MSDL_CreateRGBSurface(SDL_SWSURFACE, realWidth, realHeight, 32, rmask, gmask, bmask, amask); if (!tmpImage) @@ -260,7 +261,7 @@ Image *OpenGLImageHelper::glLoad(SDL_Surface *tmpImage, SDL_UnlockSurface(tmpImage); if (oldImage) - SDL_FreeSurface(tmpImage); + MSDL_FreeSurface(tmpImage); GLenum error = glGetError(); if (error) @@ -315,7 +316,7 @@ SDL_Surface *OpenGLImageHelper::create32BitSurface(int width, int height) const width = powerOfTwo(width); height = powerOfTwo(height); - return SDL_CreateRGBSurface(SDL_SWSURFACE, + return MSDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, rmask, gmask, bmask, amask); } diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 6015347a9..662024f36 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -42,6 +42,7 @@ #include "utils/mkdir.h" #include "utils/physfsrwops.h" +#include "utils/sdlcheckutils.h" #include #include @@ -1047,7 +1048,7 @@ SDL_Surface *ResourceManager::loadSDLSurface(const std::string &filename) const logger->log("Error, image is not png: " + filename); return nullptr; } - SDL_Surface *const surface = IMG_LoadPNG_RW(rw); + SDL_Surface *const surface = MIMG_LoadPNG_RW(rw); SDL_RWclose(rw); return surface; } @@ -1063,7 +1064,7 @@ void ResourceManager::clearScheduled() { BLOCK_START("ResourceManager::clearScheduled") FOR_EACH (std::set::iterator, i, deletedSurfaces) - SDL_FreeSurface(*i); + MSDL_FreeSurface(*i); deletedSurfaces.clear(); BLOCK_END("ResourceManager::clearScheduled") } diff --git a/src/resources/sdl2imagehelper.cpp b/src/resources/sdl2imagehelper.cpp index ee342ee26..66e280c59 100644 --- a/src/resources/sdl2imagehelper.cpp +++ b/src/resources/sdl2imagehelper.cpp @@ -33,6 +33,8 @@ #include "resources/image.h" +#include "utils/sdlcheckutils.h" + #include #include "debug.h" @@ -68,9 +70,9 @@ Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const rgba.Amask = 0x000000FF; #endif - SDL_Surface *const surf = SDL_ConvertSurface( + SDL_Surface *const surf = MSDL_ConvertSurface( tmpImage, &rgba, SDL_SWSURFACE); - SDL_FreeSurface(tmpImage); + MSDL_FreeSurface(tmpImage); uint32_t *pixels = static_cast(surf->pixels); const int type = dye.getType(); @@ -100,7 +102,7 @@ Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const } Image *const image = load(surf); - SDL_FreeSurface(surf); + MSDL_FreeSurface(surf); return image; } @@ -128,7 +130,7 @@ SDL_Surface* SDLImageHelper::SDLDuplicateSurface(SDL_Surface *const tmpImage) if (!tmpImage || !tmpImage->format) return nullptr; - return SDL_ConvertSurface(tmpImage, tmpImage->format, SDL_SWSURFACE); + return MSDL_ConvertSurface(tmpImage, tmpImage->format, SDL_SWSURFACE); } Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage) const @@ -163,7 +165,7 @@ SDL_Surface *SDLImageHelper::create32BitSurface(int width, int height) const const int amask = 0xff000000; #endif - return SDL_CreateRGBSurface(SDL_SWSURFACE, + return MSDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, rmask, gmask, bmask, amask); } diff --git a/src/resources/sdlimagehelper.cpp b/src/resources/sdlimagehelper.cpp index 7a7de1906..cc5460fde 100644 --- a/src/resources/sdlimagehelper.cpp +++ b/src/resources/sdlimagehelper.cpp @@ -68,9 +68,9 @@ Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const rgba.Amask = 0x000000FF; #endif - SDL_Surface *const surf = SDL_ConvertSurface( + SDL_Surface *const surf = MSDL_ConvertSurface( tmpImage, &rgba, SDL_SWSURFACE); - SDL_FreeSurface(tmpImage); + MSDL_FreeSurface(tmpImage); uint32_t *pixels = static_cast(surf->pixels); const int type = dye.getType(); @@ -100,7 +100,7 @@ Image *SDLImageHelper::load(SDL_RWops *const rw, Dye const &dye) const } Image *const image = load(surf); - SDL_FreeSurface(surf); + MSDL_FreeSurface(surf); return image; } @@ -183,7 +183,7 @@ SDL_Surface* SDLImageHelper::SDLDuplicateSurface(SDL_Surface *const tmpImage) if (!tmpImage || !tmpImage->format) return nullptr; - return SDL_ConvertSurface(tmpImage, tmpImage->format, SDL_SWSURFACE); + return MSDL_ConvertSurface(tmpImage, tmpImage->format, SDL_SWSURFACE); } Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage) const @@ -270,7 +270,7 @@ Image *SDLImageHelper::_SDLload(SDL_Surface *tmpImage) const } if (converted) - SDL_FreeSurface(tmpImage); + MSDL_FreeSurface(tmpImage); return new Image(image, hasAlpha, alphaChannel); } @@ -293,7 +293,7 @@ SDL_Surface *SDLImageHelper::create32BitSurface(int width, int height) const const int amask = 0xff000000; #endif - return SDL_CreateRGBSurface(SDL_SWSURFACE, + return MSDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, rmask, gmask, bmask, amask); } diff --git a/src/resources/surfaceimagehelper.cpp b/src/resources/surfaceimagehelper.cpp index 45a7d95fd..75e311a36 100644 --- a/src/resources/surfaceimagehelper.cpp +++ b/src/resources/surfaceimagehelper.cpp @@ -33,6 +33,8 @@ #include "resources/image.h" +#include "utils/sdlcheckutils.h" + #include #include "debug.h" @@ -65,9 +67,9 @@ Image *SurfaceImageHelper::load(SDL_RWops *const rw, Dye const &dye) const rgba.Amask = 0x000000FF; #endif - SDL_Surface *const surf = SDL_ConvertSurface( + SDL_Surface *const surf = MSDL_ConvertSurface( tmpImage, &rgba, SDL_SWSURFACE); - SDL_FreeSurface(tmpImage); + MSDL_FreeSurface(tmpImage); uint32_t *pixels = static_cast(surf->pixels); const int type = dye.getType(); @@ -97,7 +99,7 @@ Image *SurfaceImageHelper::load(SDL_RWops *const rw, Dye const &dye) const } Image *const image = load(surf); - SDL_FreeSurface(surf); + MSDL_FreeSurface(surf); return image; } @@ -130,7 +132,7 @@ SDL_Surface* SurfaceImageHelper::SDLDuplicateSurface(SDL_Surface *const if (!tmpImage || !tmpImage->format) return nullptr; - return SDL_ConvertSurface(tmpImage, tmpImage->format, SDL_SWSURFACE); + return MSDL_ConvertSurface(tmpImage, tmpImage->format, SDL_SWSURFACE); } Image *SurfaceImageHelper::_SDLload(SDL_Surface *tmpImage) const @@ -161,7 +163,7 @@ SDL_Surface *SurfaceImageHelper::create32BitSurface(int width, int height) const const int amask = 0xff000000; #endif - return SDL_CreateRGBSurface(SDL_SWSURFACE, + return MSDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, rmask, gmask, bmask, amask); } diff --git a/src/safeopenglgraphics.cpp b/src/safeopenglgraphics.cpp index fcb97a398..ef415c5c4 100644 --- a/src/safeopenglgraphics.cpp +++ b/src/safeopenglgraphics.cpp @@ -33,6 +33,8 @@ #include "resources/image.h" #include "resources/openglimagehelper.h" +#include "utils/sdlcheckutils.h" + #include #include "debug.h" @@ -440,7 +442,7 @@ SDL_Surface* SafeOpenGLGraphics::getScreenshot() const int w = mRect.w - (mRect.w % 4); GLint pack = 1; - SDL_Surface *const screenshot = SDL_CreateRGBSurface( + SDL_Surface *const screenshot = MSDL_CreateRGBSurface( SDL_SWSURFACE, w, h, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000); if (!screenshot) diff --git a/src/sdl2graphics.cpp b/src/sdl2graphics.cpp index 9801006d9..263404270 100644 --- a/src/sdl2graphics.cpp +++ b/src/sdl2graphics.cpp @@ -36,9 +36,9 @@ #include -#include +#include "utils/sdlcheckutils.h" -//#include +#include #include "debug.h" @@ -48,6 +48,32 @@ static unsigned int *cG = nullptr; static unsigned int *cB = nullptr; #endif +#ifdef DEBUG_SDL_SURFACES + +#define MSDL_RenderCopy(render, texture, src, dst) \ + FakeSDL_RenderCopy(render, texture, src, dst) + +static int FakeSDL_RenderCopy(SDL_Renderer *const renderer, + SDL_Texture *const texture, + const SDL_Rect *const srcrect, + const SDL_Rect *const dstrect) +{ + int ret = SDL_RenderCopy(renderer, texture, srcrect, dstrect); + if (ret) + { + logger->log("rendering error in texture %p: %s", + static_cast(texture), SDL_GetError()); + } + return ret; +} + +#else + +#define MSDL_RenderCopy(render, texture, src, dst) \ + SDL_RenderCopy(render, texture, src, dst) + +#endif + SDLGraphics::SDLGraphics() : Graphics(), mOldPixel(0), @@ -90,7 +116,7 @@ bool SDLGraphics::drawRescaledImage(const Image *const image, int srcX, int srcY dstRect.w = static_cast(desiredWidth); dstRect.h = static_cast(desiredHeight); - return (SDL_RenderCopy(mRenderer, image->mTexture, + return (MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect) < 0); } @@ -120,7 +146,7 @@ bool SDLGraphics::drawImage2(const Image *const image, int srcX, int srcY, dstRect.w = static_cast(width); dstRect.h = static_cast(height); - return !SDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); + return !MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); } void SDLGraphics::drawImagePattern(const Image *const image, @@ -163,7 +189,7 @@ void SDLGraphics::drawImagePattern(const Image *const image, dstRect.w = static_cast(dw); dstRect.h = static_cast(dh); - SDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); + MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); } } } @@ -217,7 +243,7 @@ void SDLGraphics::drawRescaledImagePattern(const Image *const image, dstRect.w = static_cast(dw); dstRect.h = static_cast(dh); - SDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); + MSDL_RenderCopy(mRenderer, image->mTexture, &srcRect, &dstRect); } } @@ -355,7 +381,7 @@ void SDLGraphics::drawTile(const ImageCollection *const vertCol) const DoubleRects::const_iterator it2_end = rects->end(); while (it2 != it2_end) { - SDL_RenderCopy(mRenderer, img->mTexture, + MSDL_RenderCopy(mRenderer, img->mTexture, &(*it2)->src, &(*it2)->dst); ++ it2; } @@ -371,7 +397,7 @@ void SDLGraphics::drawTile(const ImageVertexes *const vert) const DoubleRects::const_iterator it_end = rects->end(); while (it != it_end) { - SDL_RenderCopy(mRenderer, img->mTexture, &(*it)->src, &(*it)->dst); + MSDL_RenderCopy(mRenderer, img->mTexture, &(*it)->src, &(*it)->dst); ++ it; } } @@ -400,7 +426,7 @@ SDL_Surface *SDLGraphics::getScreenshot() #endif const int amask = 0x00000000; - SDL_Surface *const screenshot = SDL_CreateRGBSurface(SDL_SWSURFACE, + SDL_Surface *const screenshot = MSDL_CreateRGBSurface(SDL_SWSURFACE, mRect.w, mRect.h, 24, rmask, gmask, bmask, amask); // if (screenshot) diff --git a/src/sdlgraphics.cpp b/src/sdlgraphics.cpp index f62bca7c8..70713e1be 100644 --- a/src/sdlgraphics.cpp +++ b/src/sdlgraphics.cpp @@ -33,6 +33,8 @@ #include "resources/imagehelper.h" +#include "utils/sdlcheckutils.h" + #include #include @@ -418,7 +420,7 @@ SDL_Surface *SDLGraphics::getScreenshot() #endif const int amask = 0x00000000; - SDL_Surface *const screenshot = SDL_CreateRGBSurface(SDL_SWSURFACE, + SDL_Surface *const screenshot = MSDL_CreateRGBSurface(SDL_SWSURFACE, mRect.w, mRect.h, 24, rmask, gmask, bmask, amask); if (screenshot) diff --git a/src/utils/sdlcheckutils.cpp b/src/utils/sdlcheckutils.cpp new file mode 100644 index 000000000..0d471d005 --- /dev/null +++ b/src/utils/sdlcheckutils.cpp @@ -0,0 +1,184 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "utils/sdlcheckutils.h" + +#ifdef DEBUG_SDL_SURFACES + +#include "logger.h" + +#include "utils/stringutils.h" + +#include + +#include +#include + +#include "debug.h" + +// #define DEBUG_SURFACE_ALLOCATION 1 + +struct SurfaceObject +{ + SurfaceObject(const std::string name, const char *const file, + const unsigned int line) : + mName(name), + mAddFile(strprintf("%s:%u", file, line)), + mRemoveFile(), + mCnt(1) + { + } + + std::string mName; + std::string mAddFile; + std::string mRemoveFile; + int mCnt; +}; + +std::map mSurfaces; + +static SDL_Surface *addSurface(const char *const name, + SDL_Surface *const surface, + const char *const file, + const unsigned line) +{ +#ifdef DEBUG_SURFACE_ALLOCATION + logger->log("add surface: %s %s:%u %p", name, file, line, static_cast(surface)); +#endif + std::map::iterator + it = mSurfaces.find(surface); + if (it != mSurfaces.end()) + { + SurfaceObject *const obj = (*it).second; + if (obj) + { // found some time ago created surface +#ifdef DEBUG_SURFACE_ALLOCATION + logger->log("adding existing surface: %p, count:%d\n" + "was add %s\nwas deleted %s", surface, obj->mCnt, + obj->mAddFile.c_str(), obj->mRemoveFile.c_str()); +#endif + obj->mCnt ++; + } + } + else + { // creating surface object + mSurfaces[surface] = new SurfaceObject(name, file, line); + } + return surface; +} + +static void deleteSurface(const char *const name, + SDL_Surface *const surface, + const char *const file, + const unsigned line) +{ +#ifdef DEBUG_SURFACE_ALLOCATION + logger->log("delete surface: %s %s:%u %p", name, file, line, surface); +#endif + std::map::iterator + it = mSurfaces.find(surface); + if (it == mSurfaces.end()) + { + logger->log("bad surface delete: %p at %s:%d", surface, file, line); + } + else + { + SurfaceObject *const obj = (*it).second; + if (obj) + { + const int cnt = obj->mCnt; +#ifdef DEBUG_SURFACE_ALLOCATION + logger->log("debug deleting surface: %p, count:%d\n" + "was add %s\nwas deleted %s", surface, cnt, + obj->mAddFile.c_str(), obj->mRemoveFile.c_str()); +#endif + if (cnt < 1) + { // surface was here but was deleted + logger->log("deleting already deleted surface: %p at %s:%d\n" + "was add %s\nwas deleted %s", surface, file, line, + obj->mAddFile.c_str(), obj->mRemoveFile.c_str()); + } + else if (cnt == 1) + { + mSurfaces.erase(surface); + delete obj; + } + else + { + obj->mCnt --; + obj->mRemoveFile = strprintf("%s:%u", file, line); + } + } + } +} + +SDL_Surface *FakeIMG_LoadPNG_RW(SDL_RWops *const src, const char *const file, + const unsigned line) +{ + return addSurface("IMG_LoadPNG_RW", IMG_LoadPNG_RW(src), file, line); +} + +SDL_Surface *FakeIMG_Load(const char *name, const char *const file, + const unsigned line) +{ + return addSurface("IMG_Load", IMG_Load(name), file, line); +} + +void FakeSDL_FreeSurface(SDL_Surface *const surface, const char *const file, + const unsigned line) +{ + deleteSurface("SDL_FreeSurface", surface, file, line); + SDL_FreeSurface(surface); +} + +SDL_Surface *FakeSDL_CreateRGBSurface(const uint32_t flags, + const int width, const int height, + const int depth, const uint32_t rMask, + const uint32_t gMask, + const uint32_t bMask, + const uint32_t aMask, + const char *const file, + const unsigned line) +{ + return addSurface("SDL_CreateRGBSurface", SDL_CreateRGBSurface(flags, + width, height, depth, rMask, gMask, bMask, aMask), file, line); +} + +SDL_Surface *FakeSDL_ConvertSurface(SDL_Surface *const src, + SDL_PixelFormat *const fmt, + const uint32_t flags, + const char *const file, + const unsigned line) +{ + return addSurface("SDL_ConvertSurface", SDL_ConvertSurface( + src, fmt, flags), file, line); +} + +SDL_Surface *FakeTTF_RenderUTF8_Blended(_TTF_Font *const font, + const char *const text, + const SDL_Color fg, + const char *const file, + const unsigned line) +{ + return addSurface("TTF_RenderUTF8_Blended", TTF_RenderUTF8_Blended( + font, text, fg), file, line); +} + +#endif // DEBUG_SDL_SURFACES diff --git a/src/utils/sdlcheckutils.h b/src/utils/sdlcheckutils.h new file mode 100644 index 000000000..d65351c45 --- /dev/null +++ b/src/utils/sdlcheckutils.h @@ -0,0 +1,67 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef UTILS_SDLCHECKUTILS_H +#define UTILS_SDLCHECKUTILS_H + +#include "localconsts.h" +#ifdef DEBUG_SDL_SURFACES + +#include + +struct _TTF_Font; +struct SDL_Color; +struct SDL_PixelFormat; +struct SDL_RWops; +struct SDL_Surface; + +SDL_Surface *FakeIMG_LoadPNG_RW(SDL_RWops *const src, const char *const file, + const unsigned line); + +void FakeSDL_FreeSurface(SDL_Surface *const surface, const char *const file, + const unsigned line); + +SDL_Surface *FakeSDL_CreateRGBSurface(const uint32_t flags, + const int width, const int height, + const int depth, + const uint32_t rMask, + const uint32_t gMask, + const uint32_t bMask, + const uint32_t aMask, + const char *const file, + const unsigned line); + +SDL_Surface *FakeSDL_ConvertSurface(SDL_Surface *const src, + SDL_PixelFormat *const fmt, + const uint32_t flags, + const char *const file, + const unsigned line); + +SDL_Surface *FakeTTF_RenderUTF8_Blended(_TTF_Font *const font, + const char *const text, + const SDL_Color fg, + const char *const file, + const unsigned line); + +SDL_Surface *FakeIMG_Load(const char *name, const char *const file, + const unsigned line); + +#endif // DEBUG_SDL_SURFACES +#endif // UTILS_SDLCHECKUTILS_H -- cgit v1.2.3-70-g09d2