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:
parent
a63724afac
commit
faeb24f037
4 changed files with 40 additions and 93 deletions
|
@ -18,76 +18,54 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* 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 "graphics/svg.h"
|
||||||
|
|
||||||
|
#include "common/endian.h"
|
||||||
|
#include "common/stream.h"
|
||||||
|
#include "common/textconsole.h"
|
||||||
|
#include "graphics/pixelformat.h"
|
||||||
#define NANOSVG_IMPLEMENTATION
|
#define NANOSVG_IMPLEMENTATION
|
||||||
#include "graphics/nanosvg/nanosvg.h"
|
#include "graphics/nanosvg/nanosvg.h"
|
||||||
#define NANOSVGRAST_IMPLEMENTATION
|
#define NANOSVGRAST_IMPLEMENTATION
|
||||||
#include "graphics/nanosvg/nanosvgrast.h"
|
#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 {
|
namespace Graphics {
|
||||||
|
|
||||||
SVGBitmap::SVGBitmap(Common::SeekableReadStream *in) {
|
SVGBitmap::SVGBitmap(Common::SeekableReadStream *in, int dw, int dh)
|
||||||
int32 size = in->size();
|
: ManagedSurface(dw, dh, PIXELFORMAT) {
|
||||||
char *data = (char *)malloc(size + 1);
|
if (dw == 0 || dh == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int64 size = in->size();
|
||||||
|
char *data = new char[size + 1];
|
||||||
|
|
||||||
in->read(data, size);
|
in->read(data, size);
|
||||||
data[size] = '\0';
|
data[size] = '\0';
|
||||||
|
|
||||||
_svg = nsvgParse(data, "px", 96);
|
NSVGimage *svg = nsvgParse(data, "px", 96);
|
||||||
free(data);
|
if (svg == NULL)
|
||||||
|
|
||||||
if (_svg == NULL)
|
|
||||||
error("Cannot parse SVG image");
|
error("Cannot parse SVG image");
|
||||||
|
|
||||||
_rasterizer = NULL;
|
delete[] data;
|
||||||
_render = NULL;
|
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
|
// Maintain aspect ratio
|
||||||
float xRatio = 1.0f * dw / _svg->width;
|
float xRatio = 1.0f * dw / svg->width;
|
||||||
float yRatio = 1.0f * dh / _svg->height;
|
float yRatio = 1.0f * dh / svg->height;
|
||||||
float ratio = xRatio < yRatio ? xRatio : yRatio;
|
float ratio = xRatio < yRatio ? xRatio : yRatio;
|
||||||
|
|
||||||
delete _render;
|
NSVGrasterizer *rasterizer = nsvgCreateRasterizer();
|
||||||
_render = new ManagedSurface(dw, dh, *_pixelformat);
|
|
||||||
|
|
||||||
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
|
} // end of namespace Graphics
|
||||||
|
|
|
@ -21,35 +21,20 @@
|
||||||
#ifndef GRAPHICS_SVG_H
|
#ifndef GRAPHICS_SVG_H
|
||||||
#define GRAPHICS_SVG_H
|
#define GRAPHICS_SVG_H
|
||||||
|
|
||||||
|
#include "graphics/managed_surface.h"
|
||||||
|
|
||||||
namespace Common {
|
namespace Common {
|
||||||
class SeekableReadStream;
|
class SeekableReadStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct NSVGimage;
|
|
||||||
struct NSVGrasterizer;
|
|
||||||
|
|
||||||
namespace Graphics {
|
namespace Graphics {
|
||||||
|
|
||||||
class ManagedSurface;
|
/**
|
||||||
struct Surface;
|
* A derived graphics surface, which renders bitmap data from a SVG stream.
|
||||||
struct PixelFormat;
|
*/
|
||||||
|
class SVGBitmap : public ManagedSurface {
|
||||||
class SVGBitmap {
|
|
||||||
public:
|
public:
|
||||||
SVGBitmap(Common::SeekableReadStream *in);
|
SVGBitmap(Common::SeekableReadStream *in, int dw, int dh);
|
||||||
~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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end of namespace Graphics
|
} // end of namespace Graphics
|
||||||
|
|
|
@ -658,29 +658,19 @@ bool ThemeEngine::addBitmap(const Common::String &filename, const Common::String
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!scalablefile.empty()) {
|
if (!scalablefile.empty()) {
|
||||||
Graphics::SVGBitmap *image = nullptr;
|
|
||||||
Common::ArchiveMemberList members;
|
Common::ArchiveMemberList members;
|
||||||
_themeFiles.listMatchingMembers(members, scalablefile);
|
_themeFiles.listMatchingMembers(members, scalablefile);
|
||||||
for (Common::ArchiveMemberList::const_iterator i = members.begin(), end = members.end(); i != end; ++i) {
|
for (Common::ArchiveMemberList::const_iterator i = members.begin(), end = members.end(); i != end; ++i) {
|
||||||
Common::SeekableReadStream *stream = (*i)->createReadStream();
|
Common::SeekableReadStream *stream = (*i)->createReadStream();
|
||||||
if (stream) {
|
if (stream) {
|
||||||
image = new Graphics::SVGBitmap(stream);
|
_bitmaps[filename] = new Graphics::SVGBitmap(stream, width * _scaleFactor, height * _scaleFactor);
|
||||||
delete stream;
|
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 true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const Graphics::Surface *srcSurface = nullptr;
|
const Graphics::Surface *srcSurface = nullptr;
|
||||||
|
|
||||||
|
|
|
@ -327,14 +327,8 @@ Graphics::ManagedSurface *loadSurfaceFromFile(const Common::String &name, int re
|
||||||
g_gui.lockIconsSet();
|
g_gui.lockIconsSet();
|
||||||
if (g_gui.getIconsSet().hasFile(name)) {
|
if (g_gui.getIconsSet().hasFile(name)) {
|
||||||
Common::SeekableReadStream *stream = g_gui.getIconsSet().createReadStreamForMember(name);
|
Common::SeekableReadStream *stream = g_gui.getIconsSet().createReadStreamForMember(name);
|
||||||
Graphics::SVGBitmap *image = nullptr;
|
surf = new Graphics::SVGBitmap(stream, renderWidth, renderHeight);
|
||||||
image = new Graphics::SVGBitmap(stream);
|
|
||||||
|
|
||||||
delete stream;
|
delete stream;
|
||||||
|
|
||||||
surf = new Graphics::ManagedSurface(renderWidth, renderHeight, *image->getPixelFormat());
|
|
||||||
image->render(*surf, renderWidth, renderHeight);
|
|
||||||
delete image;
|
|
||||||
} else {
|
} else {
|
||||||
debug(5, "GridWidget: Cannot read file '%s'", name.c_str());
|
debug(5, "GridWidget: Cannot read file '%s'", name.c_str());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue