lottie/memoryleak: Destory the player handle properly in capi destroy() implementation.
[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         if (animation->mRenderTask.valid()) {
41             animation->mRenderTask.get();
42         }
43         animation->mAnimation = nullptr;
44         delete animation;
45     }
46 }
47
48 LOT_EXPORT void lottie_animation_get_size(const Lottie_Animation_S *animation, size_t *width, size_t *height)
49 {
50    if (!animation) return;
51
52    animation->mAnimation->size(*width, *height);
53 }
54
55 LOT_EXPORT double lottie_animation_get_duration(const Lottie_Animation_S *animation)
56 {
57    if (!animation) return 0;
58
59    return animation->mAnimation->duration();
60 }
61
62 LOT_EXPORT size_t lottie_animation_get_totalframe(const Lottie_Animation_S *animation)
63 {
64    if (!animation) return 0;
65
66    return animation->mAnimation->totalFrame();
67 }
68
69
70 LOT_EXPORT double lottie_animation_get_framerate(const Lottie_Animation_S *animation)
71 {
72    if (!animation) return 0;
73
74    return animation->mAnimation->frameRate();
75 }
76
77 LOT_EXPORT const LOTLayerNode * lottie_animation_render_tree(Lottie_Animation_S *animation, size_t frame_num, size_t width, size_t height)
78 {
79     if (!animation) return nullptr;
80
81     return animation->mAnimation->renderTree(frame_num, width, height);
82 }
83
84 LOT_EXPORT size_t
85 lottie_animation_get_frame_at_pos(const Lottie_Animation_S *animation, float pos)
86 {
87     if (!animation) return 0;
88
89     return animation->mAnimation->frameAtPos(pos);
90 }
91
92 LOT_EXPORT void
93 lottie_animation_render_async(Lottie_Animation_S *animation,
94                               size_t frame_number,
95                               uint32_t *buffer,
96                               size_t width,
97                               size_t height,
98                               size_t bytes_per_line)
99 {
100     if (!animation) return;
101
102     lottie::Surface surface(buffer, width, height, bytes_per_line);
103     animation->mRenderTask = animation->mAnimation->render(frame_number, surface);
104     animation->mBufferRef = buffer;
105 }
106
107 LOT_EXPORT uint32_t *
108 lottie_animation_render_flush(Lottie_Animation_S *animation)
109 {
110     if (!animation) return nullptr;
111
112     if (animation->mRenderTask.valid()) {
113         animation->mRenderTask.get();
114     }
115
116     return animation->mBufferRef;
117 }
118
119 }