| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | #include "config.h" |
| 19 | #include "lottieitem.h" |
| 20 | #include "lottieloader.h" |
| 21 | #include "lottiemodel.h" |
| 22 | #include "rlottie.h" |
| 23 |
| 24 | #include <fstream> |
| 25 |
| 26 | using namespace rlottie; |
| 27 |
| 28 | struct RenderTask { |
| 29 | RenderTask() { receiver = sender.get_future(); } |
| 30 | std::promise<Surface> sender; |
| 31 | std::future<Surface> receiver; |
| 32 | AnimationImpl * playerImpl{nullptr}; |
| 33 | size_t frameNo{0}; |
| 34 | Surface surface; |
| 35 | }; |
| 36 | using SharedRenderTask = std::shared_ptr<RenderTask>; |
| 37 |
| 38 | class AnimationImpl { |
| 39 | public: |
| 40 | void init(const std::shared_ptr<LOTModel> &model); |
| 41 | bool update(size_t frameNo, const VSize &size); |
| 42 | VSize size() const { return mCompItem->size(); } |
| 43 | double duration() const { return mModel->duration(); } |
| 44 | double frameRate() const { return mModel->frameRate(); } |
| 45 | size_t totalFrame() const { return mModel->totalFrame(); } |
| 46 | size_t frameAtPos(double pos) const { return mModel->frameAtPos(pos); } |
| 47 | Surface render(size_t frameNo, const Surface &surface, bool clear, bool* result); |
| 48 | const LOTLayerNode * renderTree(size_t frameNo, const VSize &size); |
| 49 |
| 50 | const LayerInfoList &layerInfoList() const |
| 51 | { |
| 52 | return mModel->layerInfoList(); |
| 53 | } |
| 54 | void setValue(const std::string &keypath, LOTVariant &&value); |
| 55 | void removeFilter(const std::string &keypath, Property prop); |
| 56 | void resetCurrentFrame(); |
| 57 |
| 58 | private: |
| 59 | std::string mFilePath; |
| 60 | std::shared_ptr<LOTModel> mModel; |
| 61 | std::unique_ptr<LOTCompItem> mCompItem; |
| 62 | SharedRenderTask mTask; |
| 63 | std::atomic<bool> mRenderInProgress{false}; |
| 64 | }; |
| 65 |
| 66 | void AnimationImpl::setValue(const std::string &keypath, LOTVariant &&value) |
| 67 | { |
| 68 | if (keypath.empty()) return; |
| 69 | mCompItem->setValue(keypath, value); |
| 70 | } |
| 71 |
| 72 | void AnimationImpl::resetCurrentFrame() { |
| 73 | mCompItem->resetCurrentFrame(); |
| 74 | } |
| 75 |
| 76 | const LOTLayerNode *AnimationImpl::renderTree(size_t frameNo, const VSize &size) |
| 77 | { |
| 78 | if (update(frameNo, size)) { |
| 79 | mCompItem->buildRenderTree(); |
| 80 | } |
| 81 | return mCompItem->renderTree(); |
| 82 | } |
| 83 |
| 84 | bool AnimationImpl::update(size_t frameNo, const VSize &size) |
| 85 | { |
| 86 | frameNo += mModel->startFrame(); |
| 87 |
| 88 | if (frameNo > mModel->endFrame()) frameNo = mModel->endFrame(); |
| 89 |
| 90 | if (frameNo < mModel->startFrame()) frameNo = mModel->startFrame(); |
| 91 |
| 92 | mCompItem->resize(size); |
| 93 | return mCompItem->update(frameNo); |
| 94 | } |
| 95 |
| 96 | Surface AnimationImpl::render(size_t frameNo, const Surface &surface, bool clear, bool* result) |
| 97 | { |
| 98 | bool renderInProgress = mRenderInProgress.load(); |
| 99 | if (renderInProgress) { |
| 100 | vCritical << "Already Rendering Scheduled for this Animation"; |
| 101 | *result = false; |
| 102 | return surface; |
| 103 | } |
| 104 |
| 105 | mRenderInProgress.store(true); |
| 106 | update(frameNo, |
| 107 | VSize(surface.drawRegionWidth(), surface.drawRegionHeight())); |
| 108 | mCompItem->render(surface, clear); |
| 109 | mRenderInProgress.store(false); |
| 110 | *result = true; |
| 111 |
| 112 | return surface; |
| 113 | } |
| 114 |
| 115 | void AnimationImpl::init(const std::shared_ptr<LOTModel> &model) |
| 116 | { |
| 117 | mModel = model; |
| 118 | mCompItem = std::make_unique<LOTCompItem>(mModel.get()); |
| 119 | mRenderInProgress = false; |
| 120 | } |
| 121 |
| 122 | /** |
| 123 | * \breif Brief abput the Api. |
| 124 | * Description about the setFilePath Api |
| 125 | * @param path add the details |
| 126 | */ |
| 127 | std::unique_ptr<Animation> Animation::loadFromData( |
| 128 | std::string jsonData, const std::string &key, |
| 129 | std::map<int32_t, int32_t> *colorReplacement, |
| 130 | FitzModifier fitzModifier, |
| 131 | const std::string &resourcePath) |
| 132 | { |
| 133 | if (jsonData.empty()) { |
| 134 | vWarning << "jason data is empty"; |
| 135 | return nullptr; |
| 136 | } |
| 137 |
| 138 | LottieLoader loader; |
| 139 | if (loader.loadFromData(std::move(jsonData), key, |
| 140 | colorReplacement, |
| 141 | (resourcePath.empty() ? " " : resourcePath), fitzModifier)) { |
| 142 | auto animation = std::unique_ptr<Animation>(new Animation); |
| 143 | animation->colorMap = colorReplacement; |
| 144 | animation->d->init(loader.model()); |
| 145 | return animation; |
| 146 | } |
| 147 | delete colorReplacement; |
| 148 | return nullptr; |
| 149 | } |
| 150 |
| 151 | std::unique_ptr<Animation> Animation::loadFromFile(const std::string &path, std::map<int32_t, int32_t> *colorReplacement, FitzModifier fitzModifier) |
| 152 | { |
| 153 | if (path.empty()) { |
| 154 | vWarning << "File path is empty"; |
| 155 | return nullptr; |
| 156 | } |
| 157 |
| 158 | LottieLoader loader; |
| 159 | if (loader.load(path, colorReplacement, fitzModifier)) { |
| 160 | auto animation = std::unique_ptr<Animation>(new Animation); |
| 161 | animation->colorMap = colorReplacement; |
| 162 | animation->d->init(loader.model()); |
| 163 | return animation; |
| 164 | } |
| 165 | delete colorReplacement; |
| 166 | return nullptr; |
| 167 | } |
| 168 |
| 169 | void Animation::size(size_t &width, size_t &height) const |
| 170 | { |
| 171 | VSize sz = d->size(); |
| 172 |
| 173 | width = sz.width(); |
| 174 | height = sz.height(); |
| 175 | } |
| 176 |
| 177 | double Animation::duration() const |
| 178 | { |
| 179 | return d->duration(); |
| 180 | } |
| 181 |
| 182 | double Animation::frameRate() const |
| 183 | { |
| 184 | return d->frameRate(); |
| 185 | } |
| 186 |
| 187 | size_t Animation::totalFrame() const |
| 188 | { |
| 189 | return d->totalFrame(); |
| 190 | } |
| 191 |
| 192 | size_t Animation::frameAtPos(double pos) |
| 193 | { |
| 194 | return d->frameAtPos(pos); |
| 195 | } |
| 196 |
| 197 | const LOTLayerNode *Animation::renderTree(size_t frameNo, size_t width, |
| 198 | size_t height) const |
| 199 | { |
| 200 | return d->renderTree(frameNo, VSize(width, height)); |
| 201 | } |
| 202 |
| 203 | void Animation::renderSync(size_t frameNo, Surface &surface, bool clear, bool* res) |
| 204 | { |
| 205 | d->render(frameNo, surface, clear, res); |
| 206 | } |
| 207 |
| 208 | const LayerInfoList &Animation::layers() const |
| 209 | { |
| 210 | return d->layerInfoList(); |
| 211 | } |
| 212 |
| 213 | void Animation::setValue(Color_Type, Property prop, const std::string &keypath, |
| 214 | Color value) |
| 215 | { |
| 216 | d->setValue(keypath, |
| 217 | LOTVariant(prop, [value](const FrameInfo &) { return value; })); |
| 218 | } |
| 219 |
| 220 | void Animation::setValue(Float_Type, Property prop, const std::string &keypath, |
| 221 | float value) |
| 222 | { |
| 223 | d->setValue(keypath, |
| 224 | LOTVariant(prop, [value](const FrameInfo &) { return value; })); |
| 225 | } |
| 226 |
| 227 | void Animation::setValue(Size_Type, Property prop, const std::string &keypath, |
| 228 | Size value) |
| 229 | { |
| 230 | d->setValue(keypath, |
| 231 | LOTVariant(prop, [value](const FrameInfo &) { return value; })); |
| 232 | } |
| 233 |
| 234 | void Animation::setValue(Point_Type, Property prop, const std::string &keypath, |
| 235 | Point value) |
| 236 | { |
| 237 | d->setValue(keypath, |
| 238 | LOTVariant(prop, [value](const FrameInfo &) { return value; })); |
| 239 | } |
| 240 |
| 241 | void Animation::setValue(Color_Type, Property prop, const std::string &keypath, |
| 242 | std::function<Color(const FrameInfo &)> &&value) |
| 243 | { |
| 244 | d->setValue(keypath, LOTVariant(prop, value)); |
| 245 | } |
| 246 |
| 247 | void Animation::setValue(Float_Type, Property prop, const std::string &keypath, |
| 248 | std::function<float(const FrameInfo &)> &&value) |
| 249 | { |
| 250 | d->setValue(keypath, LOTVariant(prop, value)); |
| 251 | } |
| 252 |
| 253 | void Animation::setValue(Size_Type, Property prop, const std::string &keypath, |
| 254 | std::function<Size(const FrameInfo &)> &&value) |
| 255 | { |
| 256 | d->setValue(keypath, LOTVariant(prop, value)); |
| 257 | } |
| 258 |
| 259 | void Animation::setValue(Point_Type, Property prop, const std::string &keypath, |
| 260 | std::function<Point(const FrameInfo &)> &&value) |
| 261 | { |
| 262 | d->setValue(keypath, LOTVariant(prop, value)); |
| 263 | } |
| 264 |
| 265 | Animation::Animation() : d(std::make_unique<AnimationImpl>()) {} |
| 266 |
| 267 | /* |
| 268 | * this is only to supress build fail |
| 269 | * because unique_ptr expects the destructor in the same translation unit. |
| 270 | */ |
| 271 | Animation::~Animation() { |
| 272 | if (colorMap != nullptr) { |
| 273 | delete colorMap; |
| 274 | colorMap = nullptr; |
| 275 | } |
| 276 | } |
| 277 |
| 278 | void Animation::resetCurrentFrame() { |
| 279 | d->resetCurrentFrame(); |
| 280 | } |
| 281 |
| 282 | Surface::Surface(uint32_t *buffer, size_t width, size_t height, |
| 283 | size_t bytesPerLine) |
| 284 | : mBuffer(buffer), |
| 285 | mWidth(width), |
| 286 | mHeight(height), |
| 287 | mBytesPerLine(bytesPerLine) |
| 288 | { |
| 289 | mDrawArea.w = mWidth; |
| 290 | mDrawArea.h = mHeight; |
| 291 | } |
| 292 |
| 293 | void Surface::setDrawRegion(size_t x, size_t y, size_t width, size_t height) |
| 294 | { |
| 295 | if ((x + width > mWidth) || (y + height > mHeight)) return; |
| 296 |
| 297 | mDrawArea.x = x; |
| 298 | mDrawArea.y = y; |
| 299 | mDrawArea.w = width; |
| 300 | mDrawArea.h = height; |
| 301 | } |
| 302 |
| 303 | #ifdef LOTTIE_LOGGING_SUPPORT |
| 304 | void initLogging() |
| 305 | { |
| 306 | #if defined(__ARM_NEON__) |
| 307 | set_log_level(LogLevel::OFF); |
| 308 | #else |
| 309 | initialize(GuaranteedLogger(), "/tmp/", "rlottie", 1); |
| 310 | set_log_level(LogLevel::INFO); |
| 311 | #endif |
| 312 | } |
| 313 |
| 314 | V_CONSTRUCTOR_FUNCTION(initLogging) |
| 315 | #endif |
| 316 |