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