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.
This commit is contained in:
Miro Kropacek 2023-03-12 00:20:41 +01:00 committed by Eugene Sandulenko
parent a63724afac
commit faeb24f037
4 changed files with 40 additions and 93 deletions

View file

@ -18,76 +18,54 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/scummsys.h"
#include "common/stream.h"
#include "common/textconsole.h"
#include "graphics/managed_surface.h"
#include "graphics/pixelformat.h"
#include "graphics/svg.h"
#include "common/endian.h"
#include "common/stream.h"
#include "common/textconsole.h"
#include "graphics/pixelformat.h"
#define NANOSVG_IMPLEMENTATION
#include "graphics/nanosvg/nanosvg.h"
#define NANOSVGRAST_IMPLEMENTATION
#include "graphics/nanosvg/nanosvgrast.h"
#ifdef SCUMM_BIG_ENDIAN
#define PIXELFORMAT Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0)
#else
#define PIXELFORMAT Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24)
#endif
namespace Graphics {
SVGBitmap::SVGBitmap(Common::SeekableReadStream *in) {
int32 size = in->size();
char *data = (char *)malloc(size + 1);
SVGBitmap::SVGBitmap(Common::SeekableReadStream *in, int dw, int dh)
: ManagedSurface(dw, dh, PIXELFORMAT) {
if (dw == 0 || dh == 0)
return;
int64 size = in->size();
char *data = new char[size + 1];
in->read(data, size);
data[size] = '\0';
_svg = nsvgParse(data, "px", 96);
free(data);
if (_svg == NULL)
NSVGimage *svg = nsvgParse(data, "px", 96);
if (svg == NULL)
error("Cannot parse SVG image");
_rasterizer = NULL;
_render = NULL;
delete[] data;
data = nullptr;
#ifdef SCUMM_BIG_ENDIAN
_pixelformat = new 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);
#endif
}
SVGBitmap::~SVGBitmap() {
if (_rasterizer)
nsvgDeleteRasterizer(_rasterizer);
nsvgDelete(_svg);
delete _render;
delete _pixelformat;
}
void SVGBitmap::render(Graphics::ManagedSurface &target, int dw, int dh) {
if (dw == 0 || dh == 0)
return;
if (_rasterizer == NULL)
_rasterizer = nsvgCreateRasterizer();
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 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

View file

@ -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

View file

@ -658,29 +658,19 @@ 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;
}
}
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;

View file

@ -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());
}