a619389dbe25a61418b5c27e46c83c8bb6140414
[platform/core/uifw/lottie-player.git] / src / binding / c / lottieanimation_capi.cpp
1 #include "lottieanimation.h"
2 #include "vdebug.h"
3
4 using namespace lottie;
5
6 extern "C" {
7
8 struct Lottie_Animation_S
9 {
10     std::unique_ptr<Animation> mAnimation;
11     size_t                     mCurFrame{0};
12     std::future<Surface>       mRenderTask;
13     size_t                     mFrameNo{0};
14     size_t                     mArraySize{0};
15     size_t                     mWidth{0};
16     size_t                     mHeight{0};
17 };
18
19 LOT_EXPORT Lottie_Animation_S *lottie_animation_from_file(const char *file)
20 {
21     if (auto animation = Animation::loadFromFile(file) ) {
22         Lottie_Animation_S *handle = new Lottie_Animation_S();
23         handle->mAnimation = std::move(animation);
24         return handle;
25     } else {
26         return nullptr;
27     }
28 }
29
30 LOT_EXPORT Lottie_Animation_S *lottie_animation_from_data(const char *data, const char *key)
31 {
32     if (auto animation = Animation::loadFromData(data, key) ) {
33         Lottie_Animation_S *handle = new Lottie_Animation_S();
34         handle->mAnimation = std::move(animation);
35         return handle;
36     } else {
37         return nullptr;
38     }
39 }
40
41 LOT_EXPORT void lottie_animation_destroy(Lottie_Animation_S *animation)
42 {
43     if (animation)
44         delete animation;
45 }
46
47 LOT_EXPORT void lottie_animation_get_size(const Lottie_Animation_S *animation, size_t *w, size_t *h)
48 {
49    if (!animation) return;
50
51    animation->mAnimation->size(*w, *h);
52 }
53
54 LOT_EXPORT double lottie_animation_get_duration(const Lottie_Animation_S *animation)
55 {
56    if (!animation) return 0;
57
58    return animation->mAnimation->duration();
59 }
60
61 LOT_EXPORT size_t lottie_animation_get_totalframe(const Lottie_Animation_S *animation)
62 {
63    if (!animation) return 0;
64
65    return animation->mAnimation->totalFrame();
66 }
67
68
69 LOT_EXPORT double lottie_animation_get_framerate(const Lottie_Animation_S *animation)
70 {
71    if (!animation) return 0;
72
73    return animation->mAnimation->frameRate();
74 }
75
76 LOT_EXPORT void lottie_animation_prepare_frame(Lottie_Animation_S *animation, size_t frameNo, size_t w, size_t h)
77 {
78     if (!animation) return;
79
80     animation->mFrameNo = frameNo;
81     animation->mWidth = w;
82     animation->mHeight = h;
83     animation->mArraySize = animation->mAnimation->renderList(frameNo, w, h).size();
84 }
85
86 LOT_EXPORT size_t lottie_animation_get_node_count(const Lottie_Animation_S *animation)
87 {
88    if (!animation) return 0;
89
90    return animation->mArraySize;
91 }
92
93 LOT_EXPORT const LOTNode* lottie_animation_get_node(const Lottie_Animation_S *animation, size_t idx)
94 {
95    if (!animation) return nullptr;
96
97    if (idx >= animation->mArraySize) return nullptr;
98
99    return animation->mAnimation->renderList(animation->mFrameNo, animation->mWidth, animation->mHeight)[idx];
100 }
101
102 LOT_EXPORT void
103 lottie_animation_render_async(Lottie_Animation_S *animation,
104                               size_t frame_number,
105                               uint32_t *buffer,
106                               size_t width,
107                               size_t height,
108                               size_t bytes_per_line)
109 {
110     if (!animation) return;
111
112     lottie::Surface surface(buffer, width, height, bytes_per_line);
113     animation->mRenderTask = animation->mAnimation->render(frame_number, surface);
114 }
115
116 LOT_EXPORT void
117 lottie_animation_render_flush(Lottie_Animation_S *animation)
118 {
119     if (!animation) return;
120
121     if (animation->mRenderTask.valid()) {
122         animation->mRenderTask.get();
123     }
124 }
125
126 }