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