summaryrefslogtreecommitdiff
path: root/src/modules/evas/vg_loaders/json/evas_vg_load_json.c
blob: 71ef07b9bbcdf05e267047814357f1b9e91a90da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <rlottie_capi.h>
#include "vg_common.h"

#ifdef ERR
# undef ERR
#endif
#define ERR(...) EINA_LOG_DOM_ERR(_evas_vg_loader_json_log_dom, __VA_ARGS__)

#ifdef INF
# undef INF
#endif
#define INF(...) EINA_LOG_DOM_INFO(_evas_vg_loader_json_log_dom, __VA_ARGS__)

static int _evas_vg_loader_json_log_dom = -1;

static Eina_Bool
evas_vg_load_file_close_json(Vg_File_Data *vfd)
{
   if (!vfd) return EINA_FALSE;

   Lottie_Animation *lot_anim = (Lottie_Animation *) vfd->loader_data;
   lottie_animation_destroy(lot_anim);
   if (vfd->anim_data) free(vfd->anim_data);
   if (vfd->root) efl_unref(vfd->root);
   free(vfd);

   return EINA_TRUE;
}

static Eina_Bool
evas_vg_load_file_data_json(Vg_File_Data *vfd)
{
   return vg_common_json_create_vg_node(vfd);
}

static Vg_File_Data*
evas_vg_load_file_open_json(Eina_File *file,
                            const char *key,
                            Eina_Bool mmap,
                            int *error EINA_UNUSED)
{
   Vg_File_Data *vfd = calloc(1, sizeof(Vg_File_Data));
   if (!vfd) return NULL;

   Lottie_Animation *lot_anim = NULL;

   if (mmap)
     {
        const char *data = (const char*) eina_file_map_all(file, EINA_FILE_SEQUENTIAL);
        if (!data) goto err;
        //@TODO pass corrct external_resource path.
        lot_anim = lottie_animation_from_data(data, key ? key:eina_file_filename_get(file), " ");
        eina_file_map_free(file, (void *) data);
     }
   else
     {
        lot_anim = lottie_animation_from_file(eina_file_filename_get(file));
     }

   if (!lot_anim)
     {
        ERR("Failed lottie_animation_from_file");
        goto err;
     }

   unsigned int frame_cnt = lottie_animation_get_totalframe(lot_anim);

   //Support animation
   if (frame_cnt > 1)
     {
        vfd->anim_data = calloc(1, sizeof(Vg_File_Anim_Data));
        if (!vfd->anim_data) goto err;
        vfd->anim_data->duration = lottie_animation_get_duration(lot_anim);
        vfd->anim_data->frame_cnt = frame_cnt;
     }

   //default size
   size_t w, h;
   lottie_animation_get_size(lot_anim, &w, &h);
   vfd->w = (int) w;
   vfd->h = (int) h;

   vfd->loader_data = (void *) lot_anim;
   vfd->no_share = EINA_TRUE;

   return vfd;

err:
   if (vfd)
     {
        if (vfd->anim_data) free(vfd->anim_data);
        free(vfd);
     }
   if (lot_anim) lottie_animation_destroy(lot_anim);

   return NULL;
}

static Evas_Vg_Load_Func evas_vg_load_json_func =
{
   evas_vg_load_file_open_json,
   evas_vg_load_file_close_json,
   evas_vg_load_file_data_json
};

static int
module_open(Evas_Module *em)
{
   if (!em) return 0;
   em->functions = (void *)(&evas_vg_load_json_func);
   _evas_vg_loader_json_log_dom = eina_log_domain_register
     ("vg-load-json", EVAS_DEFAULT_LOG_COLOR);
   if (_evas_vg_loader_json_log_dom < 0)
     {
        EINA_LOG_ERR("Can not create a module log domain.");
        return 0;
     }
   return 1;
}

static void
module_close(Evas_Module *em EINA_UNUSED)
{
   if (_evas_vg_loader_json_log_dom >= 0)
     {
        eina_log_domain_unregister(_evas_vg_loader_json_log_dom);
        _evas_vg_loader_json_log_dom = -1;
     }
}

static Evas_Module_Api evas_modapi =
{
   EVAS_MODULE_API_VERSION,
   "json",
   "none",
   {
     module_open,
     module_close
   }
};

EVAS_MODULE_DEFINE(EVAS_MODULE_TYPE_VG_LOADER, vg_loader, json);

#ifndef EVAS_STATIC_BUILD_VG_JSON
EVAS_EINA_MODULE_DEFINE(vg_loader, json);
#endif