[Scummvm-git-logs] scummvm master -> e00c5581ff25338794849b99424fb26f551abb0e

sev- noreply at scummvm.org
Thu Mar 16 18:20:55 UTC 2023


This automated email contains information about 3 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
a63724afac GRAPHICS: Don't allocate a temporary buffer for SVG rendering
faeb24f037 GRAPHICS: SVGBitmap now inherits from ManagedSurface
e00c5581ff GUI: Cache bitmaps again


Commit: a63724afacd17886339e3005c95be230099a04ba
    https://github.com/scummvm/scummvm/commit/a63724afacd17886339e3005c95be230099a04ba
Author: Le Philousophe (lephilousophe at users.noreply.github.com)
Date: 2023-03-16T19:20:50+01:00

Commit Message:
GRAPHICS: Don't allocate a temporary buffer for SVG rendering

This cache buffer has no reason to be here and it is leaked when the
class is destroyed.

Changed paths:
    graphics/svg.cpp
    graphics/svg.h


diff --git a/graphics/svg.cpp b/graphics/svg.cpp
index 4739d078442..763bd959130 100644
--- a/graphics/svg.cpp
+++ b/graphics/svg.cpp
@@ -49,8 +49,6 @@ SVGBitmap::SVGBitmap(Common::SeekableReadStream *in) {
 		error("Cannot parse SVG image");
 
 	_rasterizer = NULL;
-	_cachedW = _cachedH = -1;
-	_cache = NULL;
 	_render = NULL;
 
 #ifdef SCUMM_BIG_ENDIAN
@@ -77,33 +75,16 @@ void SVGBitmap::render(Graphics::ManagedSurface &target, int dw, int dh) {
 	if (_rasterizer == NULL)
 		_rasterizer = nsvgCreateRasterizer();
 
-	if (_cachedW != dw || _cachedH != dh) {
-		if (_cache)
-			free(_cache);
-
-		_cache = (byte *)malloc(dw * dh * 4);
-
+	if (!_render || _render->w != dw || _render->h != dh) {
 		// Maintain aspect ratio
 		float xRatio = 1.0f * dw / _svg->width;
 		float yRatio = 1.0f * dh / _svg->height;
 		float ratio = xRatio < yRatio ? xRatio : yRatio;
 
-		nsvgRasterize(_rasterizer, _svg, 0, 0, ratio, _cache, dw, dh, dw * 4);
-
-		_cachedW = dw;
-		_cachedH = dh;
-
-		if (_render)
-			delete _render;
-
-		Graphics::Surface tmp;;
-		tmp.init(dw, dh, dw * 4, _cache, *_pixelformat);
-
+		delete _render;
 		_render = new ManagedSurface(dw, dh, *_pixelformat);
 
-		_render->transBlitFrom(tmp);
-
-		tmp.free();
+		nsvgRasterize(_rasterizer, _svg, 0, 0, ratio, (byte *)_render->getPixels(), dw, dh, _render->pitch);
 	}
 
 	target.blitFrom(_render->rawSurface());
diff --git a/graphics/svg.h b/graphics/svg.h
index 3ff03859409..b501bc91310 100644
--- a/graphics/svg.h
+++ b/graphics/svg.h
@@ -48,8 +48,6 @@ private:
 	NSVGrasterizer *_rasterizer;
 
 	Graphics::ManagedSurface *_render;
-	int _cachedW, _cachedH;
-	byte *_cache;
 
 	Graphics::PixelFormat *_pixelformat;
 };


Commit: faeb24f037e3cc3d34c81d4da36996fde4ddf51d
    https://github.com/scummvm/scummvm/commit/faeb24f037e3cc3d34c81d4da36996fde4ddf51d
Author: Miro Kropacek (miro.kropacek at gmail.com)
Date: 2023-03-16T19:20:50+01:00

Commit Message:
GRAPHICS: SVGBitmap now inherits from ManagedSurface

Since the only use case for SVGBitmap is blitting it to a
ManagedSurface, combine both into one to avoid unnecessary allocation
and blitting.

Changed paths:
    graphics/svg.cpp
    graphics/svg.h
    gui/ThemeEngine.cpp
    gui/widgets/grid.cpp


diff --git a/graphics/svg.cpp b/graphics/svg.cpp
index 763bd959130..010d3bab5f1 100644
--- a/graphics/svg.cpp
+++ b/graphics/svg.cpp
@@ -18,76 +18,54 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "common/scummsys.h"
+#include "graphics/svg.h"
+
+#include "common/endian.h"
 #include "common/stream.h"
 #include "common/textconsole.h"
-
-#include "graphics/managed_surface.h"
 #include "graphics/pixelformat.h"
-
-#include "graphics/svg.h"
-
-
 #define NANOSVG_IMPLEMENTATION
 #include "graphics/nanosvg/nanosvg.h"
 #define NANOSVGRAST_IMPLEMENTATION
 #include "graphics/nanosvg/nanosvgrast.h"
 
-namespace Graphics {
-
-SVGBitmap::SVGBitmap(Common::SeekableReadStream *in) {
-	int32 size = in->size();
-	char *data = (char *)malloc(size + 1);
-
-	in->read(data, size);
-	data[size] = '\0';
-
-	_svg = nsvgParse(data, "px", 96);
-	free(data);
-
-	if (_svg == NULL)
-		error("Cannot parse SVG image");
-
-	_rasterizer = NULL;
-	_render = NULL;
-
 #ifdef SCUMM_BIG_ENDIAN
-	_pixelformat = new Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
+#define PIXELFORMAT Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0)
 #else
-	_pixelformat = new Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
+#define PIXELFORMAT Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24)
 #endif
-}
 
-SVGBitmap::~SVGBitmap() {
-	if (_rasterizer)
-		nsvgDeleteRasterizer(_rasterizer);
-
-	nsvgDelete(_svg);
-
-	delete _render;
-	delete _pixelformat;
-}
+namespace Graphics {
 
-void SVGBitmap::render(Graphics::ManagedSurface &target, int dw, int dh) {
+SVGBitmap::SVGBitmap(Common::SeekableReadStream *in, int dw, int dh)
+	: ManagedSurface(dw, dh, PIXELFORMAT) {
 	if (dw == 0 || dh == 0)
 		return;
 
-	if (_rasterizer == NULL)
-		_rasterizer = nsvgCreateRasterizer();
+	int64 size = in->size();
+	char *data = new char[size + 1];
+
+	in->read(data, size);
+	data[size] = '\0';
+
+	NSVGimage *svg = nsvgParse(data, "px", 96);
+	if (svg == NULL)
+		error("Cannot parse SVG image");
+
+	delete[] data;
+	data = nullptr;
 
-	if (!_render || _render->w != dw || _render->h != dh) {
-		// Maintain aspect ratio
-		float xRatio = 1.0f * dw / _svg->width;
-		float yRatio = 1.0f * dh / _svg->height;
-		float ratio = xRatio < yRatio ? xRatio : yRatio;
+	// Maintain aspect ratio
+	float xRatio = 1.0f * dw / svg->width;
+	float yRatio = 1.0f * dh / svg->height;
+	float ratio = xRatio < yRatio ? xRatio : yRatio;
 
-		delete _render;
-		_render = new ManagedSurface(dw, dh, *_pixelformat);
+	NSVGrasterizer *rasterizer = nsvgCreateRasterizer();
 
-		nsvgRasterize(_rasterizer, _svg, 0, 0, ratio, (byte *)_render->getPixels(), dw, dh, _render->pitch);
-	}
+	nsvgRasterize(rasterizer, svg, 0, 0, ratio, (byte *)getPixels(), dw, dh, pitch);
 
-	target.blitFrom(_render->rawSurface());
+	nsvgDeleteRasterizer(rasterizer);
+	nsvgDelete(svg);
 }
 
 } // end of namespace Graphics
diff --git a/graphics/svg.h b/graphics/svg.h
index b501bc91310..d3a52a39694 100644
--- a/graphics/svg.h
+++ b/graphics/svg.h
@@ -21,35 +21,20 @@
 #ifndef GRAPHICS_SVG_H
 #define GRAPHICS_SVG_H
 
+#include "graphics/managed_surface.h"
+
 namespace Common {
 class SeekableReadStream;
 }
 
-struct NSVGimage;
-struct NSVGrasterizer;
-
 namespace Graphics {
 
-class ManagedSurface;
-struct Surface;
-struct PixelFormat;
-
-class SVGBitmap {
+/**
+ * A derived graphics surface, which renders bitmap data from a SVG stream.
+ */
+class SVGBitmap : public ManagedSurface {
 public:
-	SVGBitmap(Common::SeekableReadStream *in);
-	~SVGBitmap();
-
-	Graphics::PixelFormat *getPixelFormat() { return _pixelformat; }
-
-	void render(Graphics::ManagedSurface &target, int dw, int dh);
-
-private:
-	NSVGimage *_svg;
-	NSVGrasterizer *_rasterizer;
-
-	Graphics::ManagedSurface *_render;
-
-	Graphics::PixelFormat *_pixelformat;
+	SVGBitmap(Common::SeekableReadStream *in, int dw, int dh);
 };
 
 } // end of namespace Graphics
diff --git a/gui/ThemeEngine.cpp b/gui/ThemeEngine.cpp
index f730cd9ea76..bcaab8c9863 100644
--- a/gui/ThemeEngine.cpp
+++ b/gui/ThemeEngine.cpp
@@ -658,28 +658,18 @@ bool ThemeEngine::addBitmap(const Common::String &filename, const Common::String
 	}
 
 	if (!scalablefile.empty()) {
-		Graphics::SVGBitmap *image = nullptr;
 		Common::ArchiveMemberList members;
 		_themeFiles.listMatchingMembers(members, scalablefile);
 		for (Common::ArchiveMemberList::const_iterator i = members.begin(), end = members.end(); i != end; ++i) {
 			Common::SeekableReadStream *stream = (*i)->createReadStream();
 			if (stream) {
-				image = new Graphics::SVGBitmap(stream);
+				_bitmaps[filename] = new Graphics::SVGBitmap(stream, width * _scaleFactor, height * _scaleFactor);
 				delete stream;
-				break;
+				return true;
 			}
 		}
 
-		if (image) {
-			_bitmaps[filename] = new Graphics::ManagedSurface(width * _scaleFactor, height * _scaleFactor, *image->getPixelFormat());
-			image->render(*_bitmaps[filename], width * _scaleFactor, height * _scaleFactor);
-
-			delete image;
-		} else {
-			return false;
-		}
-
-		return true;
+		return false;
 	}
 
 	const Graphics::Surface *srcSurface = nullptr;
diff --git a/gui/widgets/grid.cpp b/gui/widgets/grid.cpp
index 829cf80fdc1..3457212e5ed 100644
--- a/gui/widgets/grid.cpp
+++ b/gui/widgets/grid.cpp
@@ -327,14 +327,8 @@ Graphics::ManagedSurface *loadSurfaceFromFile(const Common::String &name, int re
 		g_gui.lockIconsSet();
 		if (g_gui.getIconsSet().hasFile(name)) {
 			Common::SeekableReadStream *stream = g_gui.getIconsSet().createReadStreamForMember(name);
-			Graphics::SVGBitmap *image = nullptr;
-			image = new Graphics::SVGBitmap(stream);
-
+			surf = new Graphics::SVGBitmap(stream, renderWidth, renderHeight);
 			delete stream;
-
-			surf = new Graphics::ManagedSurface(renderWidth, renderHeight, *image->getPixelFormat());
-			image->render(*surf, renderWidth, renderHeight);
-			delete image;
 		} else {
 			debug(5, "GridWidget: Cannot read file '%s'", name.c_str());
 		}


Commit: e00c5581ff25338794849b99424fb26f551abb0e
    https://github.com/scummvm/scummvm/commit/e00c5581ff25338794849b99424fb26f551abb0e
Author: Miro Kropacek (miro.kropacek at gmail.com)
Date: 2023-03-16T19:20:50+01:00

Commit Message:
GUI: Cache bitmaps again

This reverts commit 04f040af which forced a bitmap reloading to prevent
reusing already up/downscaled images in case that _scaleFactor has
changed.

However after commit ad31dfc this no longer applies as changing the
scale factor in GUI forces a ThemeEngine destroy and recreate. So
_bitmaps[filename] is safe to keep its cached image which is reused e.g.
during initial theme loading.

Changed paths:
    gui/ThemeEngine.cpp


diff --git a/gui/ThemeEngine.cpp b/gui/ThemeEngine.cpp
index bcaab8c9863..7a1b1c48237 100644
--- a/gui/ThemeEngine.cpp
+++ b/gui/ThemeEngine.cpp
@@ -650,11 +650,7 @@ bool ThemeEngine::addBitmap(const Common::String &filename, const Common::String
 	// Nothing has to be done if the bitmap already has been loaded.
 	Graphics::ManagedSurface *surf = _bitmaps[filename];
 	if (surf) {
-		surf->free();
-		delete surf;
-		surf = nullptr;
-
-		_bitmaps.erase(filename);
+		return true;
 	}
 
 	if (!scalablefile.empty()) {




More information about the Scummvm-git-logs mailing list