mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 13:43:43 +00:00
tvg_loader: introduce tvg interpreter base class for future extension.
tvg binary format might break the compatibility if any major features have been changed. It's allowed to do it when the major version is upgraded. In that case, still we need to support the backward compatibility, we can provide multiple binary interpreters and choose the proper one based on the current loading tvg binary format version. Thus, you can add further interpreters if it's necessary in the future. Our policy is to derive the TvgBinInterpreterBase class to make it running on the interface. for example, if the major version is upgraded 1.x, you can implement TvgBinInterpreter1.
This commit is contained in:
parent
d0efdb9854
commit
78f6f9d896
5 changed files with 44 additions and 14 deletions
|
@ -1,8 +1,8 @@
|
||||||
source_file = [
|
source_file = [
|
||||||
'tvgTvgLoader.h',
|
'tvgTvgLoader.h',
|
||||||
|
'tvgTvgCommon.h',
|
||||||
'tvgTvgLoader.cpp',
|
'tvgTvgLoader.cpp',
|
||||||
'tvgTvgLoadParser.h',
|
'tvgTvgBinInterpreter.cpp',
|
||||||
'tvgTvgLoadParser.cpp',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
subloader_dep += [declare_dependency(
|
subloader_dep += [declare_dependency(
|
||||||
|
|
|
@ -19,9 +19,8 @@
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#include "tvgTvgLoadParser.h"
|
#include "tvgTvgCommon.h"
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
@ -465,7 +464,7 @@ static Paint* _parsePaint(TvgBinBlock baseBlock)
|
||||||
/* External Class Implementation */
|
/* External Class Implementation */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
|
||||||
unique_ptr<Scene> tvgLoadData(const char *ptr, const char* end)
|
unique_ptr<Scene> TvgBinInterpreter::run(const char *ptr, const char* end)
|
||||||
{
|
{
|
||||||
auto scene = Scene::gen();
|
auto scene = Scene::gen();
|
||||||
if (!scene) return nullptr;
|
if (!scene) return nullptr;
|
|
@ -20,8 +20,8 @@
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef _TVG_TVG_LOAD_PARSER_H_
|
#ifndef _TVG_TVG_COMMON_H_
|
||||||
#define _TVG_TVG_LOAD_PARSER_H_
|
#define _TVG_TVG_COMMON_H_
|
||||||
|
|
||||||
#include "tvgCommon.h"
|
#include "tvgCommon.h"
|
||||||
#include "tvgBinaryDesc.h"
|
#include "tvgBinaryDesc.h"
|
||||||
|
@ -30,6 +30,25 @@
|
||||||
#define READ_UI32(dst, src) memcpy(dst, (src), sizeof(uint32_t))
|
#define READ_UI32(dst, src) memcpy(dst, (src), sizeof(uint32_t))
|
||||||
#define READ_FLOAT(dst, src) memcpy(dst, (src), sizeof(float))
|
#define READ_FLOAT(dst, src) memcpy(dst, (src), sizeof(float))
|
||||||
|
|
||||||
unique_ptr<Scene> tvgLoadData(const char* ptr, const char* end);
|
|
||||||
|
|
||||||
#endif //_TVG_TVG_LOAD_PARSER_H_
|
/* Interface for Tvg Binary Interpreter */
|
||||||
|
class TvgBinInterpreterBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~TvgBinInterpreterBase() {}
|
||||||
|
|
||||||
|
/* ptr: points the tvg binary body (after header)
|
||||||
|
end: end of the tvg binary data */
|
||||||
|
virtual unique_ptr<Scene> run(const char* ptr, const char* end) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* Version 0 */
|
||||||
|
class TvgBinInterpreter : public TvgBinInterpreterBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
unique_ptr<Scene> run(const char* ptr, const char* end) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif //_TVG_TVG_COMMON_H_
|
|
@ -19,24 +19,28 @@
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
|
#include <fstream>
|
||||||
#include "tvgLoader.h"
|
#include "tvgLoader.h"
|
||||||
#include "tvgTvgLoader.h"
|
#include "tvgTvgLoader.h"
|
||||||
#include "tvgTvgLoadParser.h"
|
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
/* Internal Class Implementation */
|
/* Internal Class Implementation */
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
void TvgLoader::clear()
|
void TvgLoader::clear()
|
||||||
{
|
{
|
||||||
if (copy) free((char*)data);
|
if (copy) free((char*)data);
|
||||||
ptr = data = nullptr;
|
ptr = data = nullptr;
|
||||||
size = 0;
|
size = 0;
|
||||||
copy = false;
|
copy = false;
|
||||||
|
|
||||||
|
if (interpreter) {
|
||||||
|
delete(interpreter);
|
||||||
|
interpreter = nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,6 +68,9 @@ bool TvgLoader::readHeader()
|
||||||
READ_FLOAT(&h, ptr);
|
READ_FLOAT(&h, ptr);
|
||||||
ptr += SIZE(float);
|
ptr += SIZE(float);
|
||||||
|
|
||||||
|
//Decide the proper Tvg Binary Interpreter based on the current file version
|
||||||
|
if (this->version >= 0) interpreter = new TvgBinInterpreter;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,7 +182,9 @@ bool TvgLoader::close()
|
||||||
void TvgLoader::run(unsigned tid)
|
void TvgLoader::run(unsigned tid)
|
||||||
{
|
{
|
||||||
if (root) root.reset();
|
if (root) root.reset();
|
||||||
root = tvgLoadData(ptr, data + size);
|
|
||||||
|
root = interpreter->run(ptr, data + size);
|
||||||
|
|
||||||
if (!root) clear();
|
if (!root) clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -185,4 +194,4 @@ unique_ptr<Paint> TvgLoader::paint()
|
||||||
this->done();
|
this->done();
|
||||||
if (root) return move(root);
|
if (root) return move(root);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
|
@ -24,6 +24,8 @@
|
||||||
#define _TVG_TVG_LOADER_H_
|
#define _TVG_TVG_LOADER_H_
|
||||||
|
|
||||||
#include "tvgTaskScheduler.h"
|
#include "tvgTaskScheduler.h"
|
||||||
|
#include "tvgTvgCommon.h"
|
||||||
|
|
||||||
|
|
||||||
class TvgLoader : public LoadModule, public Task
|
class TvgLoader : public LoadModule, public Task
|
||||||
{
|
{
|
||||||
|
@ -33,6 +35,7 @@ public:
|
||||||
uint32_t size = 0;
|
uint32_t size = 0;
|
||||||
uint16_t version = 0;
|
uint16_t version = 0;
|
||||||
unique_ptr<Scene> root = nullptr;
|
unique_ptr<Scene> root = nullptr;
|
||||||
|
TvgBinInterpreterBase* interpreter = nullptr;
|
||||||
bool copy = false;
|
bool copy = false;
|
||||||
|
|
||||||
~TvgLoader();
|
~TvgLoader();
|
||||||
|
|
Loading…
Add table
Reference in a new issue