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:
Hermet Park 2023-12-20 14:22:19 +09:00 committed by Hermet Park
parent 58961f84d5
commit bd37e8ba37
21 changed files with 41 additions and 35 deletions

View file

@ -41,7 +41,7 @@ void JpgLoader::clear()
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
JpgLoader::JpgLoader() : LoadModule(FileType::Jpg) JpgLoader::JpgLoader() : ImageLoader(FileType::Jpg)
{ {
jpegDecompressor = tjInitDecompress(); jpegDecompressor = tjInitDecompress();
} }

View file

@ -28,7 +28,7 @@
using tjhandle = void*; using tjhandle = void*;
//TODO: Use Task? //TODO: Use Task?
class JpgLoader : public LoadModule class JpgLoader : public ImageLoader
{ {
public: public:
JpgLoader(); JpgLoader();

View file

@ -38,7 +38,7 @@ void PngLoader::clear()
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
PngLoader::PngLoader() : LoadModule(FileType::Png) PngLoader::PngLoader() : ImageLoader(FileType::Png)
{ {
image = static_cast<png_imagep>(calloc(1, sizeof(png_image))); image = static_cast<png_imagep>(calloc(1, sizeof(png_image)));
image->version = PNG_IMAGE_VERSION; image->version = PNG_IMAGE_VERSION;

View file

@ -26,7 +26,7 @@
#include <png.h> #include <png.h>
#include "tvgLoader.h" #include "tvgLoader.h"
class PngLoader : public LoadModule class PngLoader : public ImageLoader
{ {
public: public:
PngLoader(); PngLoader();

View file

@ -41,7 +41,7 @@ void WebpLoader::run(unsigned tid)
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
WebpLoader::WebpLoader() : LoadModule(FileType::Webp) WebpLoader::WebpLoader() : ImageLoader(FileType::Webp)
{ {
} }

View file

@ -26,7 +26,7 @@
#include "tvgLoader.h" #include "tvgLoader.h"
#include "tvgTaskScheduler.h" #include "tvgTaskScheduler.h"
class WebpLoader : public LoadModule, public Task class WebpLoader : public ImageLoader, public Task
{ {
public: public:
WebpLoader(); WebpLoader();

View file

@ -53,7 +53,7 @@ void JpgLoader::run(unsigned tid)
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
JpgLoader::JpgLoader() : LoadModule(FileType::Jpg) JpgLoader::JpgLoader() : ImageLoader(FileType::Jpg)
{ {
} }

View file

@ -27,7 +27,7 @@
#include "tvgTaskScheduler.h" #include "tvgTaskScheduler.h"
#include "tvgJpgd.h" #include "tvgJpgd.h"
class JpgLoader : public LoadModule, public Task class JpgLoader : public ImageLoader, public Task
{ {
private: private:
jpeg_decoder* decoder = nullptr; jpeg_decoder* decoder = nullptr;

View file

@ -49,7 +49,7 @@ void PngLoader::run(unsigned tid)
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
PngLoader::PngLoader() : LoadModule(FileType::Png) PngLoader::PngLoader() : ImageLoader(FileType::Png)
{ {
lodepng_state_init(&state); lodepng_state_init(&state);
} }

View file

@ -27,7 +27,7 @@
#include "tvgTaskScheduler.h" #include "tvgTaskScheduler.h"
class PngLoader : public LoadModule, public Task class PngLoader : public ImageLoader, public Task
{ {
private: private:
LodePNGState state; LodePNGState state;

View file

@ -34,7 +34,7 @@
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
RawLoader::RawLoader() : LoadModule(FileType::Raw) RawLoader::RawLoader() : ImageLoader(FileType::Raw)
{ {
} }

View file

@ -23,7 +23,7 @@
#ifndef _TVG_RAW_LOADER_H_ #ifndef _TVG_RAW_LOADER_H_
#define _TVG_RAW_LOADER_H_ #define _TVG_RAW_LOADER_H_
class RawLoader : public LoadModule class RawLoader : public ImageLoader
{ {
public: public:
uint32_t* content = nullptr; uint32_t* content = nullptr;

View file

@ -3538,7 +3538,7 @@ void SvgLoader::clear(bool all)
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
SvgLoader::SvgLoader() : LoadModule(FileType::Svg) SvgLoader::SvgLoader() : ImageLoader(FileType::Svg)
{ {
} }

View file

@ -26,7 +26,7 @@
#include "tvgTaskScheduler.h" #include "tvgTaskScheduler.h"
#include "tvgSvgLoaderCommon.h" #include "tvgSvgLoaderCommon.h"
class SvgLoader : public LoadModule, public Task class SvgLoader : public ImageLoader, public Task
{ {
public: public:
string filePath; string filePath;

View file

@ -121,7 +121,7 @@ void TvgLoader::run(unsigned tid)
/* External Class Implementation */ /* External Class Implementation */
/************************************************************************/ /************************************************************************/
TvgLoader::TvgLoader() : LoadModule(FileType::Tvg) TvgLoader::TvgLoader() : ImageLoader(FileType::Tvg)
{ {
} }

View file

@ -27,7 +27,7 @@
#include "tvgTvgCommon.h" #include "tvgTvgCommon.h"
class TvgLoader : public LoadModule, public Task class TvgLoader : public ImageLoader, public Task
{ {
public: public:
const char* data = nullptr; const char* data = nullptr;

View file

@ -28,10 +28,10 @@
namespace tvg namespace tvg
{ {
class FrameModule: public LoadModule class FrameModule: public ImageLoader
{ {
public: public:
FrameModule(FileType type) : LoadModule(type) {} FrameModule(FileType type) : ImageLoader(type) {}
virtual ~FrameModule() {} virtual ~FrameModule() {}
virtual bool frame(float no) = 0; //set the current frame number virtual bool frame(float no) = 0; //set the current frame number

View file

@ -26,8 +26,6 @@
#include "tvgRender.h" #include "tvgRender.h"
#include "tvgInlist.h" #include "tvgInlist.h"
namespace tvg
{
struct LoadModule struct LoadModule
{ {
@ -35,16 +33,17 @@ struct LoadModule
//Use either hashkey(data) or hashpath(path) //Use either hashkey(data) or hashpath(path)
uint64_t hashkey; 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 FileType type; //current loader file type
uint16_t sharing = 0; //reference count uint16_t sharing = 0; //reference count
bool readied = false; //read done already. bool readied = false; //read done already.
LoadModule(FileType type) : type(type) {} LoadModule(FileType type) : type(type) {}
virtual ~LoadModule() {} virtual ~LoadModule()
{
free(hashpath);
}
virtual bool open(const string& path) { return false; } virtual bool open(const string& path) { return false; }
virtual bool open(const char* data, uint32_t size, const string& rpath, bool copy) { 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. //Override this if the vector-format has own resizing policy.
virtual bool resize(Paint* paint, float w, float h) { return false; } 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 void sync() {}; //finish immediately if any async update jobs.
virtual bool read() virtual bool read()
@ -68,11 +66,19 @@ struct LoadModule
--sharing; --sharing;
return false; 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 unique_ptr<Surface> bitmap() { return nullptr; }
virtual Paint* paint() { return nullptr; } virtual Paint* paint() { return nullptr; }
}; };
}
#endif //_TVG_LOAD_MODULE_H_ #endif //_TVG_LOAD_MODULE_H_

View file

@ -194,7 +194,7 @@ static LoadModule* _findFromCache(const string& path)
auto loader = _activeLoaders.head; auto loader = _activeLoaders.head;
while (loader) { while (loader) {
if (loader->hashpath == path) { if (loader->hashpath && !strcmp(loader->hashpath, path.c_str())) {
++loader->sharing; ++loader->sharing;
return loader; return loader;
} }
@ -260,7 +260,7 @@ LoadModule* LoaderMgr::loader(const string& path, bool* invalid)
if (auto loader = _findByPath(path)) { if (auto loader = _findByPath(path)) {
if (loader->open(path)) { if (loader->open(path)) {
loader->hashpath = path; loader->hashpath = strdup(path.c_str());
_activeLoaders.back(loader); _activeLoaders.back(loader);
return loader; return loader;
} }

View file

@ -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. //Same resource has been loaded.
if (this->loader == loader) { if (this->loader == loader) {

View file

@ -57,7 +57,7 @@ struct PictureIterator : Iterator
struct Picture::Impl struct Picture::Impl
{ {
LoadModule* loader = nullptr; ImageLoader* loader = nullptr;
Paint* paint = nullptr; //vector picture uses Paint* paint = nullptr; //vector picture uses
Surface* surface = nullptr; //bitmap picture uses Surface* surface = nullptr; //bitmap picture uses
@ -73,7 +73,7 @@ struct Picture::Impl
bool render(RenderMethod &renderer); bool render(RenderMethod &renderer);
bool size(float w, float h); bool size(float w, float h);
RenderRegion bounds(RenderMethod& renderer); RenderRegion bounds(RenderMethod& renderer);
Result load(LoadModule* ploader); Result load(ImageLoader* ploader);
Impl(Picture* p) : picture(p) Impl(Picture* p) : picture(p)
{ {
@ -153,7 +153,7 @@ struct Picture::Impl
if (paint || surface) return Result::InsufficientCondition; if (paint || surface) return Result::InsufficientCondition;
bool invalid; //Invalid Path bool invalid; //Invalid Path
auto loader = LoaderMgr::loader(path, &invalid); auto loader = static_cast<ImageLoader*>(LoaderMgr::loader(path, &invalid));
if (!loader) { if (!loader) {
if (invalid) return Result::InvalidArguments; if (invalid) return Result::InvalidArguments;
return Result::NonSupport; 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) Result load(const char* data, uint32_t size, const string& mimeType, const string& rpath, bool copy)
{ {
if (paint || surface) return Result::InsufficientCondition; 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; if (!loader) return Result::NonSupport;
return load(loader); return load(loader);
} }
@ -173,7 +173,7 @@ struct Picture::Impl
{ {
if (paint || surface) return Result::InsufficientCondition; 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; if (!loader) return Result::FailedAllocation;
return load(loader); return load(loader);