rlottie.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. * The above copyright notice and this permission notice shall be included in all
  10. * copies or substantial portions of the Software.
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. #ifndef _RLOTTIE_H_
  20. #define _RLOTTIE_H_
  21. #include <future>
  22. #include <vector>
  23. #include <memory>
  24. #if defined _WIN32 || defined __CYGWIN__
  25. #ifdef RLOTTIE_BUILD_DLL
  26. #ifdef RLOTTIE_BUILD
  27. #define RLOTTIE_API __declspec(dllexport)
  28. #else
  29. #define RLOTTIE_API __declspec(dllimport)
  30. #endif
  31. #else
  32. #define RLOTTIE_API
  33. #endif
  34. #else
  35. #ifdef RLOTTIE_BUILD
  36. #define RLOTTIE_API __attribute__ ((visibility ("default")))
  37. #else
  38. #define RLOTTIE_API
  39. #endif
  40. #endif
  41. class AnimationImpl;
  42. struct LOTNode;
  43. struct LOTLayerNode;
  44. namespace rlottie {
  45. /**
  46. * @brief Configures rlottie model cache policy.
  47. *
  48. * Provides Library level control to configure model cache
  49. * policy. Setting it to 0 will disable
  50. * the cache as well as flush all the previously cached content.
  51. *
  52. * @param[in] cacheSize Maximum Model Cache size.
  53. *
  54. * @note to disable Caching configure with 0 size.
  55. * @note to flush the current Cache content configure it with 0 and
  56. * then reconfigure with the new size.
  57. *
  58. * @internal
  59. */
  60. RLOTTIE_API void configureModelCacheSize(size_t cacheSize);
  61. struct Color {
  62. Color() = default;
  63. Color(float r, float g , float b):_r(r), _g(g), _b(b){}
  64. float r() const {return _r;}
  65. float g() const {return _g;}
  66. float b() const {return _b;}
  67. private:
  68. float _r{0};
  69. float _g{0};
  70. float _b{0};
  71. };
  72. struct Size {
  73. Size() = default;
  74. Size(float w, float h):_w(w), _h(h){}
  75. float w() const {return _w;}
  76. float h() const {return _h;}
  77. private:
  78. float _w{0};
  79. float _h{0};
  80. };
  81. struct Point {
  82. Point() = default;
  83. Point(float x, float y):_x(x), _y(y){}
  84. float x() const {return _x;}
  85. float y() const {return _y;}
  86. private:
  87. float _x{0};
  88. float _y{0};
  89. };
  90. struct FrameInfo {
  91. explicit FrameInfo(uint32_t frame): _frameNo(frame){}
  92. uint32_t curFrame() const {return _frameNo;}
  93. private:
  94. uint32_t _frameNo;
  95. };
  96. enum class Property {
  97. FillColor, /*!< Color property of Fill object , value type is rlottie::Color */
  98. FillOpacity, /*!< Opacity property of Fill object , value type is float [ 0 .. 100] */
  99. StrokeColor, /*!< Color property of Stroke object , value type is rlottie::Color */
  100. StrokeOpacity, /*!< Opacity property of Stroke object , value type is float [ 0 .. 100] */
  101. StrokeWidth, /*!< stroke width property of Stroke object , value type is float */
  102. TrAnchor, /*!< Transform Anchor property of Layer and Group object , value type is rlottie::Point */
  103. TrPosition, /*!< Transform Position property of Layer and Group object , value type is rlottie::Point */
  104. TrScale, /*!< Transform Scale property of Layer and Group object , value type is rlottie::Size. range[0 ..100] */
  105. TrRotation, /*!< Transform Rotation property of Layer and Group object , value type is float. range[0 .. 360] in degrees*/
  106. TrOpacity /*!< Transform Opacity property of Layer and Group object , value type is float [ 0 .. 100] */
  107. };
  108. struct Color_Type{};
  109. struct Point_Type{};
  110. struct Size_Type{};
  111. struct Float_Type{};
  112. template <typename T> struct MapType;
  113. class RLOTTIE_API Surface {
  114. public:
  115. /**
  116. * @brief Surface object constructor.
  117. *
  118. * @param[in] buffer surface buffer.
  119. * @param[in] width surface width.
  120. * @param[in] height surface height.
  121. * @param[in] bytesPerLine number of bytes in a surface scanline.
  122. *
  123. * @note Default surface format is ARGB32_Premultiplied.
  124. *
  125. * @internal
  126. */
  127. Surface(uint32_t *buffer, size_t width, size_t height, size_t bytesPerLine);
  128. /**
  129. * @brief Sets the Draw Area available on the Surface.
  130. *
  131. * Lottie will use the draw region size to generate frame image
  132. * and will update only the draw rgion of the surface.
  133. *
  134. * @param[in] x region area x position.
  135. * @param[in] y region area y position.
  136. * @param[in] width region area width.
  137. * @param[in] height region area height.
  138. *
  139. * @note Default surface format is ARGB32_Premultiplied.
  140. * @note Default draw region area is [ 0 , 0, surface width , surface height]
  141. *
  142. * @internal
  143. */
  144. void setDrawRegion(size_t x, size_t y, size_t width, size_t height);
  145. /**
  146. * @brief Returns width of the surface.
  147. *
  148. * @return surface width
  149. *
  150. * @internal
  151. *
  152. */
  153. size_t width() const {return mWidth;}
  154. /**
  155. * @brief Returns height of the surface.
  156. *
  157. * @return surface height
  158. *
  159. * @internal
  160. */
  161. size_t height() const {return mHeight;}
  162. /**
  163. * @brief Returns number of bytes in the surface scanline.
  164. *
  165. * @return number of bytes in scanline.
  166. *
  167. * @internal
  168. */
  169. size_t bytesPerLine() const {return mBytesPerLine;}
  170. /**
  171. * @brief Returns buffer attached tp the surface.
  172. *
  173. * @return buffer attaced to the Surface.
  174. *
  175. * @internal
  176. */
  177. uint32_t *buffer() const {return mBuffer;}
  178. /**
  179. * @brief Returns drawable area width of the surface.
  180. *
  181. * @return drawable area width
  182. *
  183. * @note Default value is width() of the surface
  184. *
  185. * @internal
  186. *
  187. */
  188. size_t drawRegionWidth() const {return mDrawArea.w;}
  189. /**
  190. * @brief Returns drawable area height of the surface.
  191. *
  192. * @return drawable area height
  193. *
  194. * @note Default value is height() of the surface
  195. *
  196. * @internal
  197. */
  198. size_t drawRegionHeight() const {return mDrawArea.h;}
  199. /**
  200. * @brief Returns drawable area's x position of the surface.
  201. *
  202. * @return drawable area's x potition.
  203. *
  204. * @note Default value is 0
  205. *
  206. * @internal
  207. */
  208. size_t drawRegionPosX() const {return mDrawArea.x;}
  209. /**
  210. * @brief Returns drawable area's y position of the surface.
  211. *
  212. * @return drawable area's y potition.
  213. *
  214. * @note Default value is 0
  215. *
  216. * @internal
  217. */
  218. size_t drawRegionPosY() const {return mDrawArea.y;}
  219. /**
  220. * @brief Default constructor.
  221. */
  222. Surface() = default;
  223. private:
  224. uint32_t *mBuffer{nullptr};
  225. size_t mWidth{0};
  226. size_t mHeight{0};
  227. size_t mBytesPerLine{0};
  228. struct {
  229. size_t x{0};
  230. size_t y{0};
  231. size_t w{0};
  232. size_t h{0};
  233. }mDrawArea;
  234. };
  235. using MarkerList = std::vector<std::tuple<std::string, int , int>>;
  236. /**
  237. * @brief https://helpx.adobe.com/after-effects/using/layer-markers-composition-markers.html
  238. * Markers exported form AE are used to describe a segmnet of an animation {comment/tag , startFrame, endFrame}
  239. * Marker can be use to devide a resource in to separate animations by tagging the segment with comment string ,
  240. * start frame and duration of that segment.
  241. */
  242. using LayerInfoList = std::vector<std::tuple<std::string, int , int>>;
  243. using ColorFilter = std::function<void(float &r , float &g, float &b)>;
  244. class RLOTTIE_API Animation {
  245. public:
  246. /**
  247. * @brief Constructs an animation object from file path.
  248. *
  249. * @param[in] path Lottie resource file path
  250. * @param[in] cachePolicy whether to cache or not the model data.
  251. * use only when need to explicit disabl caching for a
  252. * particular resource. To disable caching at library level
  253. * use @see configureModelCacheSize() instead.
  254. *
  255. * @return Animation object that can render the contents of the
  256. * Lottie resource represented by file path.
  257. *
  258. * @internal
  259. */
  260. static std::unique_ptr<Animation>
  261. loadFromFile(const std::string &path, bool cachePolicy=true);
  262. /**
  263. * @brief Constructs an animation object from JSON string data.
  264. *
  265. * @param[in] jsonData The JSON string data.
  266. * @param[in] key the string that will be used to cache the JSON string data.
  267. * @param[in] resourcePath the path will be used to search for external resource.
  268. * @param[in] cachePolicy whether to cache or not the model data.
  269. * use only when need to explicit disabl caching for a
  270. * particular resource. To disable caching at library level
  271. * use @see configureModelCacheSize() instead.
  272. *
  273. * @return Animation object that can render the contents of the
  274. * Lottie resource represented by JSON string data.
  275. *
  276. * @internal
  277. */
  278. static std::unique_ptr<Animation>
  279. loadFromData(std::string jsonData, const std::string &key,
  280. const std::string &resourcePath="", bool cachePolicy=true);
  281. /**
  282. * @brief Constructs an animation object from JSON string data and update.
  283. * the color properties using ColorFilter.
  284. * @param[in] jsonData The JSON string data.
  285. * @param[in] resourcePath the path will be used to search for external resource.
  286. * @param[in] filter The color filter that will be applied for each color property
  287. * found during parsing.
  288. * @return Animation object that can render the contents of the
  289. * Lottie resource represented by JSON string data.
  290. *
  291. * @internal
  292. */
  293. static std::unique_ptr<Animation>
  294. loadFromData(std::string jsonData, std::string resourcePath, ColorFilter filter);
  295. /**
  296. * @brief Returns default framerate of the Lottie resource.
  297. *
  298. * @return framerate of the Lottie resource
  299. *
  300. * @internal
  301. *
  302. */
  303. double frameRate() const;
  304. /**
  305. * @brief Returns total number of frames present in the Lottie resource.
  306. *
  307. * @return frame count of the Lottie resource.
  308. *
  309. * @note frame number starts with 0.
  310. *
  311. * @internal
  312. */
  313. size_t totalFrame() const;
  314. /**
  315. * @brief Returns default viewport size of the Lottie resource.
  316. *
  317. * @param[out] width default width of the viewport.
  318. * @param[out] height default height of the viewport.
  319. *
  320. * @internal
  321. *
  322. */
  323. void size(size_t &width, size_t &height) const;
  324. /**
  325. * @brief Returns total animation duration of Lottie resource in second.
  326. * it uses totalFrame() and frameRate() to calculate the duration.
  327. * duration = totalFrame() / frameRate().
  328. *
  329. * @return total animation duration in second.
  330. * @retval 0 if the Lottie resource has no animation.
  331. *
  332. * @see totalFrame()
  333. * @see frameRate()
  334. *
  335. * @internal
  336. */
  337. double duration() const;
  338. /**
  339. * @brief Returns frame number for a given position.
  340. * this function helps to map the position value retuned
  341. * by the animator to a frame number in side the Lottie resource.
  342. * frame_number = lerp(start_frame, endframe, pos);
  343. *
  344. * @param[in] pos normalized position value [0 ... 1]
  345. *
  346. * @return frame numer maps to the position value [startFrame .... endFrame]
  347. *
  348. * @internal
  349. */
  350. size_t frameAtPos(double pos);
  351. /**
  352. * @brief Renders the content to surface Asynchronously.
  353. * it gives a future in return to get the result of the
  354. * rendering at a future point.
  355. * To get best performance user has to start rendering as soon as
  356. * it finds that content at {frameNo} has to be rendered and get the
  357. * result from the future at the last moment when the surface is needed
  358. * to draw into the screen.
  359. *
  360. *
  361. * @param[in] frameNo Content corresponds to the @p frameNo needs to be drawn
  362. * @param[in] surface Surface in which content will be drawn
  363. * @param[in] keepAspectRatio whether to keep the aspect ratio while scaling the content.
  364. *
  365. * @return future that will hold the result when rendering finished.
  366. *
  367. * for Synchronus rendering @see renderSync
  368. *
  369. * @see Surface
  370. * @internal
  371. */
  372. std::future<Surface> render(size_t frameNo, Surface surface, bool keepAspectRatio=true);
  373. /**
  374. * @brief Renders the content to surface synchronously.
  375. * for performance use the async rendering @see render
  376. *
  377. * @param[in] frameNo Content corresponds to the @p frameNo needs to be drawn
  378. * @param[in] surface Surface in which content will be drawn
  379. * @param[in] keepAspectRatio whether to keep the aspect ratio while scaling the content.
  380. *
  381. * @internal
  382. */
  383. void renderSync(size_t frameNo, Surface surface, bool keepAspectRatio=true);
  384. /**
  385. * @brief Returns root layer of the composition updated with
  386. * content of the Lottie resource at frame number @p frameNo.
  387. *
  388. * @param[in] frameNo Content corresponds to the @p frameNo needs to be extracted.
  389. * @param[in] width content viewbox width
  390. * @param[in] height content viewbox height
  391. *
  392. * @return Root layer node.
  393. *
  394. * @internal
  395. */
  396. const LOTLayerNode * renderTree(size_t frameNo, size_t width, size_t height) const;
  397. /**
  398. * @brief Returns Composition Markers.
  399. *
  400. *
  401. * @return returns MarkerList of the Composition.
  402. *
  403. * @see MarkerList
  404. * @internal
  405. */
  406. const MarkerList& markers() const;
  407. /**
  408. * @brief Returns Layer information{name, inFrame, outFrame} of all the child layers of the composition.
  409. *
  410. *
  411. * @return List of Layer Information of the Composition.
  412. *
  413. * @see LayerInfoList
  414. * @internal
  415. */
  416. const LayerInfoList& layers() const;
  417. /**
  418. * @brief Sets property value for the specified {@link KeyPath}. This {@link KeyPath} can resolve
  419. * to multiple contents. In that case, the callback's value will apply to all of them.
  420. *
  421. * Keypath should conatin object names separated by (.) and can handle globe(**) or wildchar(*).
  422. *
  423. * @usage
  424. * To change fillcolor property of fill1 object in the layer1->group1->fill1 hirarchy to RED color
  425. *
  426. * player->setValue<rlottie::Property::FillColor>("layer1.group1.fill1", rlottie::Color(1, 0, 0);
  427. *
  428. * if all the color property inside group1 needs to be changed to GREEN color
  429. *
  430. * player->setValue<rlottie::Property::FillColor>("**.group1.**", rlottie::Color(0, 1, 0);
  431. *
  432. * @internal
  433. */
  434. template<Property prop, typename AnyValue>
  435. void setValue(const std::string &keypath, AnyValue value)
  436. {
  437. setValue(MapType<std::integral_constant<Property, prop>>{}, prop, keypath, value);
  438. }
  439. /**
  440. * @brief default destructor
  441. *
  442. * @internal
  443. */
  444. ~Animation();
  445. private:
  446. void setValue(Color_Type, Property, const std::string &, Color);
  447. void setValue(Float_Type, Property, const std::string &, float);
  448. void setValue(Size_Type, Property, const std::string &, Size);
  449. void setValue(Point_Type, Property, const std::string &, Point);
  450. void setValue(Color_Type, Property, const std::string &, std::function<Color(const FrameInfo &)> &&);
  451. void setValue(Float_Type, Property, const std::string &, std::function<float(const FrameInfo &)> &&);
  452. void setValue(Size_Type, Property, const std::string &, std::function<Size(const FrameInfo &)> &&);
  453. void setValue(Point_Type, Property, const std::string &, std::function<Point(const FrameInfo &)> &&);
  454. /**
  455. * @brief default constructor
  456. *
  457. * @internal
  458. */
  459. Animation();
  460. std::unique_ptr<AnimationImpl> d;
  461. };
  462. //Map Property to Value type
  463. template<> struct MapType<std::integral_constant<Property, Property::FillColor>>: Color_Type{};
  464. template<> struct MapType<std::integral_constant<Property, Property::StrokeColor>>: Color_Type{};
  465. template<> struct MapType<std::integral_constant<Property, Property::FillOpacity>>: Float_Type{};
  466. template<> struct MapType<std::integral_constant<Property, Property::StrokeOpacity>>: Float_Type{};
  467. template<> struct MapType<std::integral_constant<Property, Property::StrokeWidth>>: Float_Type{};
  468. template<> struct MapType<std::integral_constant<Property, Property::TrRotation>>: Float_Type{};
  469. template<> struct MapType<std::integral_constant<Property, Property::TrOpacity>>: Float_Type{};
  470. template<> struct MapType<std::integral_constant<Property, Property::TrAnchor>>: Point_Type{};
  471. template<> struct MapType<std::integral_constant<Property, Property::TrPosition>>: Point_Type{};
  472. template<> struct MapType<std::integral_constant<Property, Property::TrScale>>: Size_Type{};
  473. } // namespace lotplayer
  474. #endif // _RLOTTIE_H_