mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 05:33:36 +00:00
common: Revise internal loader interfaces.
We are introducing the FontLoader, which slightly differs from the ImageLoader in terms of features. To adequately support both, we have separated the loader functionalities into FontLoader and ImageLoader. This allows us to optimally adapt the LoadModule for each case.
This commit is contained in:
parent
58961f84d5
commit
bd37e8ba37
21 changed files with 41 additions and 35 deletions
|
@ -41,7 +41,7 @@ void JpgLoader::clear()
|
|||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
JpgLoader::JpgLoader() : LoadModule(FileType::Jpg)
|
||||
JpgLoader::JpgLoader() : ImageLoader(FileType::Jpg)
|
||||
{
|
||||
jpegDecompressor = tjInitDecompress();
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@
|
|||
using tjhandle = void*;
|
||||
|
||||
//TODO: Use Task?
|
||||
class JpgLoader : public LoadModule
|
||||
class JpgLoader : public ImageLoader
|
||||
{
|
||||
public:
|
||||
JpgLoader();
|
||||
|
|
|
@ -38,7 +38,7 @@ void PngLoader::clear()
|
|||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
PngLoader::PngLoader() : LoadModule(FileType::Png)
|
||||
PngLoader::PngLoader() : ImageLoader(FileType::Png)
|
||||
{
|
||||
image = static_cast<png_imagep>(calloc(1, sizeof(png_image)));
|
||||
image->version = PNG_IMAGE_VERSION;
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
#include <png.h>
|
||||
#include "tvgLoader.h"
|
||||
|
||||
class PngLoader : public LoadModule
|
||||
class PngLoader : public ImageLoader
|
||||
{
|
||||
public:
|
||||
PngLoader();
|
||||
|
|
|
@ -41,7 +41,7 @@ void WebpLoader::run(unsigned tid)
|
|||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
WebpLoader::WebpLoader() : LoadModule(FileType::Webp)
|
||||
WebpLoader::WebpLoader() : ImageLoader(FileType::Webp)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
#include "tvgLoader.h"
|
||||
#include "tvgTaskScheduler.h"
|
||||
|
||||
class WebpLoader : public LoadModule, public Task
|
||||
class WebpLoader : public ImageLoader, public Task
|
||||
{
|
||||
public:
|
||||
WebpLoader();
|
||||
|
|
|
@ -53,7 +53,7 @@ void JpgLoader::run(unsigned tid)
|
|||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
JpgLoader::JpgLoader() : LoadModule(FileType::Jpg)
|
||||
JpgLoader::JpgLoader() : ImageLoader(FileType::Jpg)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
#include "tvgTaskScheduler.h"
|
||||
#include "tvgJpgd.h"
|
||||
|
||||
class JpgLoader : public LoadModule, public Task
|
||||
class JpgLoader : public ImageLoader, public Task
|
||||
{
|
||||
private:
|
||||
jpeg_decoder* decoder = nullptr;
|
||||
|
|
|
@ -49,7 +49,7 @@ void PngLoader::run(unsigned tid)
|
|||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
PngLoader::PngLoader() : LoadModule(FileType::Png)
|
||||
PngLoader::PngLoader() : ImageLoader(FileType::Png)
|
||||
{
|
||||
lodepng_state_init(&state);
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
#include "tvgTaskScheduler.h"
|
||||
|
||||
|
||||
class PngLoader : public LoadModule, public Task
|
||||
class PngLoader : public ImageLoader, public Task
|
||||
{
|
||||
private:
|
||||
LodePNGState state;
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
RawLoader::RawLoader() : LoadModule(FileType::Raw)
|
||||
RawLoader::RawLoader() : ImageLoader(FileType::Raw)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
#ifndef _TVG_RAW_LOADER_H_
|
||||
#define _TVG_RAW_LOADER_H_
|
||||
|
||||
class RawLoader : public LoadModule
|
||||
class RawLoader : public ImageLoader
|
||||
{
|
||||
public:
|
||||
uint32_t* content = nullptr;
|
||||
|
|
|
@ -3538,7 +3538,7 @@ void SvgLoader::clear(bool all)
|
|||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
SvgLoader::SvgLoader() : LoadModule(FileType::Svg)
|
||||
SvgLoader::SvgLoader() : ImageLoader(FileType::Svg)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
#include "tvgTaskScheduler.h"
|
||||
#include "tvgSvgLoaderCommon.h"
|
||||
|
||||
class SvgLoader : public LoadModule, public Task
|
||||
class SvgLoader : public ImageLoader, public Task
|
||||
{
|
||||
public:
|
||||
string filePath;
|
||||
|
|
|
@ -121,7 +121,7 @@ void TvgLoader::run(unsigned tid)
|
|||
/* External Class Implementation */
|
||||
/************************************************************************/
|
||||
|
||||
TvgLoader::TvgLoader() : LoadModule(FileType::Tvg)
|
||||
TvgLoader::TvgLoader() : ImageLoader(FileType::Tvg)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
#include "tvgTvgCommon.h"
|
||||
|
||||
|
||||
class TvgLoader : public LoadModule, public Task
|
||||
class TvgLoader : public ImageLoader, public Task
|
||||
{
|
||||
public:
|
||||
const char* data = nullptr;
|
||||
|
|
|
@ -28,10 +28,10 @@
|
|||
namespace tvg
|
||||
{
|
||||
|
||||
class FrameModule: public LoadModule
|
||||
class FrameModule: public ImageLoader
|
||||
{
|
||||
public:
|
||||
FrameModule(FileType type) : LoadModule(type) {}
|
||||
FrameModule(FileType type) : ImageLoader(type) {}
|
||||
virtual ~FrameModule() {}
|
||||
|
||||
virtual bool frame(float no) = 0; //set the current frame number
|
||||
|
|
|
@ -26,8 +26,6 @@
|
|||
#include "tvgRender.h"
|
||||
#include "tvgInlist.h"
|
||||
|
||||
namespace tvg
|
||||
{
|
||||
|
||||
struct LoadModule
|
||||
{
|
||||
|
@ -35,16 +33,17 @@ struct LoadModule
|
|||
|
||||
//Use either hashkey(data) or hashpath(path)
|
||||
uint64_t hashkey;
|
||||
string hashpath;
|
||||
char* hashpath = nullptr;
|
||||
|
||||
float w = 0, h = 0; //default image size
|
||||
ColorSpace cs = ColorSpace::Unsupported; //must be clarified at open()
|
||||
FileType type; //current loader file type
|
||||
uint16_t sharing = 0; //reference count
|
||||
bool readied = false; //read done already.
|
||||
|
||||
LoadModule(FileType type) : type(type) {}
|
||||
virtual ~LoadModule() {}
|
||||
virtual ~LoadModule()
|
||||
{
|
||||
free(hashpath);
|
||||
}
|
||||
|
||||
virtual bool open(const string& path) { return false; }
|
||||
virtual bool open(const char* data, uint32_t size, const string& rpath, bool copy) { return false; }
|
||||
|
@ -52,7 +51,6 @@ struct LoadModule
|
|||
//Override this if the vector-format has own resizing policy.
|
||||
virtual bool resize(Paint* paint, float w, float h) { return false; }
|
||||
|
||||
virtual bool animatable() { return false; } //true if this loader supports animation.
|
||||
virtual void sync() {}; //finish immediately if any async update jobs.
|
||||
|
||||
virtual bool read()
|
||||
|
@ -68,11 +66,19 @@ struct LoadModule
|
|||
--sharing;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct ImageLoader : LoadModule
|
||||
{
|
||||
float w = 0, h = 0; //default image size
|
||||
ColorSpace cs = ColorSpace::Unsupported; //must be clarified at open()
|
||||
|
||||
ImageLoader(FileType type) : LoadModule(type) {}
|
||||
|
||||
virtual bool animatable() { return false; } //true if this loader supports animation.
|
||||
virtual unique_ptr<Surface> bitmap() { return nullptr; }
|
||||
virtual Paint* paint() { return nullptr; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //_TVG_LOAD_MODULE_H_
|
||||
|
|
|
@ -194,7 +194,7 @@ static LoadModule* _findFromCache(const string& path)
|
|||
auto loader = _activeLoaders.head;
|
||||
|
||||
while (loader) {
|
||||
if (loader->hashpath == path) {
|
||||
if (loader->hashpath && !strcmp(loader->hashpath, path.c_str())) {
|
||||
++loader->sharing;
|
||||
return loader;
|
||||
}
|
||||
|
@ -260,7 +260,7 @@ LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
|
|||
|
||||
if (auto loader = _findByPath(path)) {
|
||||
if (loader->open(path)) {
|
||||
loader->hashpath = path;
|
||||
loader->hashpath = strdup(path.c_str());
|
||||
_activeLoaders.back(loader);
|
||||
return loader;
|
||||
}
|
||||
|
|
|
@ -118,7 +118,7 @@ RenderTransform Picture::Impl::resizeTransform(const RenderTransform* pTransform
|
|||
}
|
||||
|
||||
|
||||
Result Picture::Impl::load(LoadModule* loader)
|
||||
Result Picture::Impl::load(ImageLoader* loader)
|
||||
{
|
||||
//Same resource has been loaded.
|
||||
if (this->loader == loader) {
|
||||
|
|
|
@ -57,7 +57,7 @@ struct PictureIterator : Iterator
|
|||
|
||||
struct Picture::Impl
|
||||
{
|
||||
LoadModule* loader = nullptr;
|
||||
ImageLoader* loader = nullptr;
|
||||
|
||||
Paint* paint = nullptr; //vector picture uses
|
||||
Surface* surface = nullptr; //bitmap picture uses
|
||||
|
@ -73,7 +73,7 @@ struct Picture::Impl
|
|||
bool render(RenderMethod &renderer);
|
||||
bool size(float w, float h);
|
||||
RenderRegion bounds(RenderMethod& renderer);
|
||||
Result load(LoadModule* ploader);
|
||||
Result load(ImageLoader* ploader);
|
||||
|
||||
Impl(Picture* p) : picture(p)
|
||||
{
|
||||
|
@ -153,7 +153,7 @@ struct Picture::Impl
|
|||
if (paint || surface) return Result::InsufficientCondition;
|
||||
|
||||
bool invalid; //Invalid Path
|
||||
auto loader = LoaderMgr::loader(path, &invalid);
|
||||
auto loader = static_cast<ImageLoader*>(LoaderMgr::loader(path, &invalid));
|
||||
if (!loader) {
|
||||
if (invalid) return Result::InvalidArguments;
|
||||
return Result::NonSupport;
|
||||
|
@ -164,7 +164,7 @@ struct Picture::Impl
|
|||
Result load(const char* data, uint32_t size, const string& mimeType, const string& rpath, bool copy)
|
||||
{
|
||||
if (paint || surface) return Result::InsufficientCondition;
|
||||
auto loader = LoaderMgr::loader(data, size, mimeType, rpath, copy);
|
||||
auto loader = static_cast<ImageLoader*>(LoaderMgr::loader(data, size, mimeType, rpath, copy));
|
||||
if (!loader) return Result::NonSupport;
|
||||
return load(loader);
|
||||
}
|
||||
|
@ -173,7 +173,7 @@ struct Picture::Impl
|
|||
{
|
||||
if (paint || surface) return Result::InsufficientCondition;
|
||||
|
||||
auto loader = LoaderMgr::loader(data, w, h, premultiplied, copy);
|
||||
auto loader = static_cast<ImageLoader*>(LoaderMgr::loader(data, w, h, premultiplied, copy));
|
||||
if (!loader) return Result::FailedAllocation;
|
||||
|
||||
return load(loader);
|
||||
|
|
Loading…
Add table
Reference in a new issue