tvg_loader: introduce tvg interpreter base class for future extension.
authorHermet Park <chuneon.park@samsung.com>
Wed, 4 Aug 2021 11:53:51 +0000 (20:53 +0900)
committerJunsuChoi <jsuya.choi@samsung.com>
Fri, 6 Aug 2021 01:04:08 +0000 (10:04 +0900)
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.

src/loaders/tvg/meson.build
src/loaders/tvg/tvgTvgBinInterpreter.cpp [moved from src/loaders/tvg/tvgTvgLoadParser.cpp with 99% similarity]
src/loaders/tvg/tvgTvgCommon.h [moved from src/loaders/tvg/tvgTvgLoadParser.h with 71% similarity]
src/loaders/tvg/tvgTvgLoader.cpp
src/loaders/tvg/tvgTvgLoader.h

index 57f4f2f..ad1f7a5 100644 (file)
@@ -1,8 +1,8 @@
 source_file = [
    'tvgTvgLoader.h',
+   'tvgTvgCommon.h',
    'tvgTvgLoader.cpp',
-   'tvgTvgLoadParser.h',
-   'tvgTvgLoadParser.cpp',
+   'tvgTvgBinInterpreter.cpp',
 ]
 
 subloader_dep += [declare_dependency(
similarity index 99%
rename from src/loaders/tvg/tvgTvgLoadParser.cpp
rename to src/loaders/tvg/tvgTvgBinInterpreter.cpp
index 62250b5..9c16ecd 100644 (file)
@@ -19,9 +19,8 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-
 #include <memory.h>
-#include "tvgTvgLoadParser.h"
+#include "tvgTvgCommon.h"
 
 
 /************************************************************************/
@@ -465,7 +464,7 @@ static Paint* _parsePaint(TvgBinBlock baseBlock)
 /* 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();
     if (!scene) return nullptr;
similarity index 71%
rename from src/loaders/tvg/tvgTvgLoadParser.h
rename to src/loaders/tvg/tvgTvgCommon.h
index 3e2daf7..e7c3eba 100644 (file)
@@ -20,8 +20,8 @@
  * SOFTWARE.
  */
 
-#ifndef _TVG_TVG_LOAD_PARSER_H_
-#define _TVG_TVG_LOAD_PARSER_H_
+#ifndef _TVG_TVG_COMMON_H_
+#define _TVG_TVG_COMMON_H_
 
 #include "tvgCommon.h"
 #include "tvgBinaryDesc.h"
 #define READ_UI32(dst, src) memcpy(dst, (src), sizeof(uint32_t))
 #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_
\ No newline at end of file
index 00c31a8..8a1a460 100644 (file)
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-
-#include <fstream>
 #include <memory.h>
+#include <fstream>
 #include "tvgLoader.h"
 #include "tvgTvgLoader.h"
-#include "tvgTvgLoadParser.h"
 
 
 /************************************************************************/
 /* Internal Class Implementation                                        */
 /************************************************************************/
 
+
 void TvgLoader::clear()
 {
     if (copy) free((char*)data);
     ptr = data = nullptr;
     size = 0;
     copy = false;
+
+    if (interpreter) {
+        delete(interpreter);
+        interpreter = nullptr;
+    }
 }
 
 
@@ -64,6 +68,9 @@ bool TvgLoader::readHeader()
     READ_FLOAT(&h, ptr);
     ptr += SIZE(float);
 
+    //Decide the proper Tvg Binary Interpreter based on the current file version
+    if (this->version >= 0) interpreter = new TvgBinInterpreter;
+
     return true;
 }
 
@@ -175,7 +182,9 @@ bool TvgLoader::close()
 void TvgLoader::run(unsigned tid)
 {
     if (root) root.reset();
-    root = tvgLoadData(ptr, data + size);
+
+    root = interpreter->run(ptr, data + size);
+
     if (!root) clear();
 }
 
@@ -185,4 +194,4 @@ unique_ptr<Paint> TvgLoader::paint()
     this->done();
     if (root) return move(root);
     return nullptr;
-}
+}
\ No newline at end of file
index d48e13b..ba756d0 100644 (file)
@@ -24,6 +24,8 @@
 #define _TVG_TVG_LOADER_H_
 
 #include "tvgTaskScheduler.h"
+#include "tvgTvgCommon.h"
+
 
 class TvgLoader : public LoadModule, public Task
 {
@@ -33,6 +35,7 @@ public:
     uint32_t size = 0;
     uint16_t version = 0;
     unique_ptr<Scene> root = nullptr;
+    TvgBinInterpreterBase* interpreter = nullptr;
     bool copy = false;
 
     ~TvgLoader();