| 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 |
| 19 | #include "lottieitem.h" |
| 20 | #include <algorithm> |
| 21 | #include <cmath> |
| 22 | #include <iterator> |
| 23 | #include "lottiekeypath.h" |
| 24 | #include "vbitmap.h" |
| 25 | #include "vdasher.h" |
| 26 | #include "vpainter.h" |
| 27 | #include "vraster.h" |
| 28 |
| 29 | /* Lottie Layer Rules |
| 30 | * 1. time stretch is pre calculated and applied to all the properties of the |
| 31 | * lottilayer model and all its children |
| 32 | * 2. The frame property could be reversed using,time-reverse layer property in |
| 33 | * AE. which means (start frame > endFrame) 3. |
| 34 | */ |
| 35 |
| 36 | static bool transformProp(rlottie::Property prop) |
| 37 | { |
| 38 | switch (prop) { |
| 39 | case rlottie::Property::TrAnchor: |
| 40 | case rlottie::Property::TrScale: |
| 41 | case rlottie::Property::TrOpacity: |
| 42 | case rlottie::Property::TrPosition: |
| 43 | case rlottie::Property::TrRotation: |
| 44 | return true; |
| 45 | default: |
| 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | static bool fillProp(rlottie::Property prop) |
| 50 | { |
| 51 | switch (prop) { |
| 52 | case rlottie::Property::Color: |
| 53 | case rlottie::Property::FillOpacity: |
| 54 | return true; |
| 55 | default: |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 |
| 60 | static bool strokeProp(rlottie::Property prop) |
| 61 | { |
| 62 | switch (prop) { |
| 63 | case rlottie::Property::Color: |
| 64 | case rlottie::Property::StrokeOpacity: |
| 65 | case rlottie::Property::StrokeWidth: |
| 66 | return true; |
| 67 | default: |
| 68 | return false; |
| 69 | } |
| 70 | } |
| 71 |
| 72 | LOTCompItem::LOTCompItem(LOTModel *model) |
| 73 | : mUpdateViewBox(false), mCurFrameNo(-1) |
| 74 | { |
| 75 | mCompData = model->mRoot.get(); |
| 76 | mRootLayer = createLayerItem(mCompData->mRootLayer.get()); |
| 77 | mRootLayer->setComplexContent(false); |
| 78 | mViewSize = mCompData->size(); |
| 79 | } |
| 80 |
| 81 | static bool isGoodParentLayer(LOTLayerItem *parent, LOTLayerItem *child) { |
| 82 | do { |
| 83 | if (parent == child) { |
| 84 | return false; |
| 85 | } |
| 86 | parent = parent->resolvedParentLayer(); |
| 87 | } while (parent); |
| 88 | return true; |
| 89 | } |
| 90 |
| 91 | void LOTCompItem::setValue(const std::string &keypath, LOTVariant &value) |
| 92 | { |
| 93 | LOTKeyPath key(keypath); |
| 94 | mRootLayer->resolveKeyPath(key, 0, value); |
| 95 | mCurFrameNo = -1; |
| 96 | } |
| 97 |
| 98 | void LOTCompItem::resetCurrentFrame() |
| 99 | { |
| 100 | mCurFrameNo = -1; |
| 101 | } |
| 102 |
| 103 | std::unique_ptr<LOTLayerItem> LOTCompItem::createLayerItem( |
| 104 | LOTLayerData *layerData) |
| 105 | { |
| 106 | switch (layerData->mLayerType) { |
| 107 | case LayerType::Precomp: { |
| 108 | return std::make_unique<LOTCompLayerItem>(layerData); |
| 109 | } |
| 110 | case LayerType::Solid: { |
| 111 | return std::make_unique<LOTSolidLayerItem>(layerData); |
| 112 | } |
| 113 | case LayerType::Shape: { |
| 114 | return std::make_unique<LOTShapeLayerItem>(layerData); |
| 115 | } |
| 116 | case LayerType::Null: { |
| 117 | return std::make_unique<LOTNullLayerItem>(layerData); |
| 118 | } |
| 119 | case LayerType::Image: { |
| 120 | return std::make_unique<LOTImageLayerItem>(layerData); |
| 121 | } |
| 122 | default: |
| 123 | return nullptr; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 |
| 128 | void LOTCompItem::resize(const VSize &size) |
| 129 | { |
| 130 | if (mViewSize == size) return; |
| 131 | mViewSize = size; |
| 132 | mUpdateViewBox = true; |
| 133 | } |
| 134 |
| 135 | VSize LOTCompItem::size() const |
| 136 | { |
| 137 | return mViewSize; |
| 138 | } |
| 139 |
| 140 | bool LOTCompItem::update(int frameNo) |
| 141 | { |
| 142 | // check if cached frame is same as requested frame. |
| 143 | if (!mUpdateViewBox && (mCurFrameNo == frameNo)) return false; |
| 144 |
| 145 | /* |
| 146 | * if viewbox dosen't scale exactly to the viewport |
| 147 | * we scale the viewbox keeping AspectRatioPreserved and then align the |
| 148 | * viewbox to the viewport using AlignCenter rule. |
| 149 | */ |
| 150 | VSize viewPort = mViewSize; |
| 151 | VSize viewBox = mCompData->size(); |
| 152 |
| 153 | float sx = float(viewPort.width()) / viewBox.width(); |
| 154 | float sy = float(viewPort.height()) / viewBox.height(); |
| 155 | float scale = fmin(sx, sy); |
| 156 | float tx = (viewPort.width() - viewBox.width() * scale) * 0.5; |
| 157 | float ty = (viewPort.height() - viewBox.height() * scale) * 0.5; |
| 158 |
| 159 | VMatrix m; |
| 160 | m.translate(tx, ty).scale(scale, scale); |
| 161 | mRootLayer->update(frameNo, m, 1.0); |
| 162 |
| 163 | mCurFrameNo = frameNo; |
| 164 | mUpdateViewBox = false; |
| 165 | return true; |
| 166 | } |
| 167 |
| 168 | void LOTCompItem::buildRenderTree() |
| 169 | { |
| 170 | mRootLayer->buildLayerNode(); |
| 171 | } |
| 172 |
| 173 | const LOTLayerNode *LOTCompItem::renderTree() const |
| 174 | { |
| 175 | return mRootLayer->layerNode(); |
| 176 | } |
| 177 |
| 178 | bool LOTCompItem::render(const rlottie::Surface &surface, bool clear) |
| 179 | { |
| 180 | VBitmap bitmap(reinterpret_cast<uchar *>(surface.buffer()), surface.width(), |
| 181 | surface.height(), surface.bytesPerLine(), |
| 182 | VBitmap::Format::ARGB32); |
| 183 |
| 184 | /* schedule all preprocess task for this frame at once. |
| 185 | */ |
| 186 | mDrawableList.clear(); |
| 187 | mRootLayer->renderList(mDrawableList); |
| 188 | VRect clip(0, 0, surface.drawRegionWidth(), surface.drawRegionHeight()); |
| 189 | for (auto &e : mDrawableList) { |
| 190 | e->preprocess(clip); |
| 191 | } |
| 192 |
| 193 | VPainter painter(&bitmap, clear); |
| 194 | // set sub surface area for drawing. |
| 195 | painter.setDrawRegion( |
| 196 | VRect(surface.drawRegionPosX(), surface.drawRegionPosY(), |
| 197 | surface.drawRegionWidth(), surface.drawRegionHeight())); |
| 198 | mRootLayer->render(&painter, {}, {}); |
| 199 |
| 200 | return true; |
| 201 | } |
| 202 |
| 203 | void LOTMaskItem::update(int frameNo, const VMatrix & parentMatrix, |
| 204 | float /*parentAlpha*/, const DirtyFlag &flag) |
| 205 | { |
| 206 | if (flag.testFlag(DirtyFlagBit::None) && mData->isStatic()) return; |
| 207 |
| 208 | if (mData->mShape.isStatic()) { |
| 209 | if (mLocalPath.empty()) { |
| 210 | mData->mShape.value(frameNo).toPath(mLocalPath); |
| 211 | } |
| 212 | } else { |
| 213 | mData->mShape.value(frameNo).toPath(mLocalPath); |
| 214 | } |
| 215 | /* mask item dosen't inherit opacity */ |
| 216 | mCombinedAlpha = mData->opacity(frameNo); |
| 217 |
| 218 | mFinalPath.clone(mLocalPath); |
| 219 | mFinalPath.transform(parentMatrix); |
| 220 |
| 221 | mRasterizer.rasterize(mFinalPath); |
| 222 | mRasterRequest = true; |
| 223 | } |
| 224 |
| 225 | VRle LOTMaskItem::rle() |
| 226 | { |
| 227 | if (mRasterRequest) { |
| 228 | mRasterRequest = false; |
| 229 | if (!vCompare(mCombinedAlpha, 1.0f)) |
| 230 | mRasterizer.rle() *= (mCombinedAlpha * 255); |
| 231 | if (mData->mInv) mRasterizer.rle().invert(); |
| 232 | } |
| 233 | return mRasterizer.rle(); |
| 234 | } |
| 235 |
| 236 | void LOTLayerItem::buildLayerNode() |
| 237 | { |
| 238 | if (!mLayerCNode) { |
| 239 | mLayerCNode = std::make_unique<LOTLayerNode>(); |
| 240 | mLayerCNode->mMaskList.ptr = nullptr; |
| 241 | mLayerCNode->mMaskList.size = 0; |
| 242 | mLayerCNode->mLayerList.ptr = nullptr; |
| 243 | mLayerCNode->mLayerList.size = 0; |
| 244 | mLayerCNode->mNodeList.ptr = nullptr; |
| 245 | mLayerCNode->mNodeList.size = 0; |
| 246 | mLayerCNode->mMatte = MatteNone; |
| 247 | mLayerCNode->mVisible = 0; |
| 248 | mLayerCNode->mAlpha = 255; |
| 249 | mLayerCNode->mClipPath.ptPtr = nullptr; |
| 250 | mLayerCNode->mClipPath.elmPtr = nullptr; |
| 251 | mLayerCNode->mClipPath.ptCount = 0; |
| 252 | mLayerCNode->mClipPath.elmCount = 0; |
| 253 | mLayerCNode->name = name().c_str(); |
| 254 | } |
| 255 | if (complexContent()) mLayerCNode->mAlpha = combinedAlpha() * 255; |
| 256 | mLayerCNode->mVisible = visible(); |
| 257 | // update matte |
| 258 | if (hasMatte()) { |
| 259 | switch (mLayerData->mMatteType) { |
| 260 | case MatteType::Alpha: |
| 261 | mLayerCNode->mMatte = MatteAlpha; |
| 262 | break; |
| 263 | case MatteType::AlphaInv: |
| 264 | mLayerCNode->mMatte = MatteAlphaInv; |
| 265 | break; |
| 266 | case MatteType::Luma: |
| 267 | mLayerCNode->mMatte = MatteLuma; |
| 268 | break; |
| 269 | case MatteType::LumaInv: |
| 270 | mLayerCNode->mMatte = MatteLumaInv; |
| 271 | break; |
| 272 | default: |
| 273 | mLayerCNode->mMatte = MatteNone; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | if (mLayerMask) { |
| 278 | mMasksCNode.clear(); |
| 279 | mMasksCNode.resize(mLayerMask->mMasks.size()); |
| 280 | size_t i = 0; |
| 281 | for (const auto &mask : mLayerMask->mMasks) { |
| 282 | LOTMask * cNode = &mMasksCNode[i++]; |
| 283 | const std::vector<VPath::Element> &elm = mask.mFinalPath.elements(); |
| 284 | const std::vector<VPointF> & pts = mask.mFinalPath.points(); |
| 285 | const float *ptPtr = reinterpret_cast<const float *>(pts.data()); |
| 286 | const char * elmPtr = reinterpret_cast<const char *>(elm.data()); |
| 287 | cNode->mPath.ptPtr = ptPtr; |
| 288 | cNode->mPath.ptCount = pts.size(); |
| 289 | cNode->mPath.elmPtr = elmPtr; |
| 290 | cNode->mPath.elmCount = elm.size(); |
| 291 | cNode->mAlpha = mask.mCombinedAlpha * 255; |
| 292 | switch (mask.maskMode()) { |
| 293 | case LOTMaskData::Mode::Add: |
| 294 | cNode->mMode = MaskAdd; |
| 295 | break; |
| 296 | case LOTMaskData::Mode::Substarct: |
| 297 | cNode->mMode = MaskSubstract; |
| 298 | break; |
| 299 | case LOTMaskData::Mode::Intersect: |
| 300 | cNode->mMode = MaskIntersect; |
| 301 | break; |
| 302 | case LOTMaskData::Mode::Difference: |
| 303 | cNode->mMode = MaskDifference; |
| 304 | break; |
| 305 | default: |
| 306 | cNode->mMode = MaskAdd; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | mLayerCNode->mMaskList.ptr = mMasksCNode.data(); |
| 311 | mLayerCNode->mMaskList.size = mMasksCNode.size(); |
| 312 | } |
| 313 | } |
| 314 |
| 315 | void LOTLayerItem::render(VPainter *painter, const VRle &inheritMask, |
| 316 | const VRle &matteRle) |
| 317 | { |
| 318 | mDrawableList.clear(); |
| 319 | renderList(mDrawableList); |
| 320 |
| 321 | VRle mask; |
| 322 | if (mLayerMask) { |
| 323 | mask = mLayerMask->maskRle(painter->clipBoundingRect()); |
| 324 | if (!inheritMask.empty()) mask = mask & inheritMask; |
| 325 | // if resulting mask is empty then return. |
| 326 | if (mask.empty()) return; |
| 327 | } else { |
| 328 | mask = inheritMask; |
| 329 | } |
| 330 |
| 331 | for (auto &i : mDrawableList) { |
| 332 | painter->setBrush(i->mBrush); |
| 333 | VRle rle = i->rle(); |
| 334 | if (matteRle.empty()) { |
| 335 | if (mask.empty()) { |
| 336 | // no mask no matte |
| 337 | painter->drawRle(VPoint(), rle); |
| 338 | } else { |
| 339 | // only mask |
| 340 | painter->drawRle(rle, mask); |
| 341 | } |
| 342 |
| 343 | } else { |
| 344 | if (!mask.empty()) rle = rle & mask; |
| 345 |
| 346 | if (rle.empty()) continue; |
| 347 | if (matteType() == MatteType::AlphaInv) { |
| 348 | rle = rle - matteRle; |
| 349 | painter->drawRle(VPoint(), rle); |
| 350 | } else { |
| 351 | // render with matteRle as clip. |
| 352 | painter->drawRle(rle, matteRle); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 |
| 358 | LOTLayerMaskItem::LOTLayerMaskItem(LOTLayerData *layerData) |
| 359 | { |
| 360 | if (!layerData->mExtra) return; |
| 361 |
| 362 | mMasks.reserve(layerData->mExtra->mMasks.size()); |
| 363 |
| 364 | for (auto &i : layerData->mExtra->mMasks) { |
| 365 | mMasks.emplace_back(i.get()); |
| 366 | mStatic &= i->isStatic(); |
| 367 | } |
| 368 | } |
| 369 |
| 370 | void LOTLayerMaskItem::update(int frameNo, const VMatrix &parentMatrix, |
| 371 | float parentAlpha, const DirtyFlag &flag) |
| 372 | { |
| 373 | if (flag.testFlag(DirtyFlagBit::None) && isStatic()) return; |
| 374 |
| 375 | for (auto &i : mMasks) { |
| 376 | i.update(frameNo, parentMatrix, parentAlpha, flag); |
| 377 | } |
| 378 | mDirty = true; |
| 379 | } |
| 380 |
| 381 | VRle LOTLayerMaskItem::maskRle(const VRect &clipRect) |
| 382 | { |
| 383 | if (!mDirty) return mRle; |
| 384 |
| 385 | VRle rle; |
| 386 | for (auto &i : mMasks) { |
| 387 | switch (i.maskMode()) { |
| 388 | case LOTMaskData::Mode::Add: { |
| 389 | rle = rle + i.rle(); |
| 390 | break; |
| 391 | } |
| 392 | case LOTMaskData::Mode::Substarct: { |
| 393 | if (rle.empty() && !clipRect.empty()) rle = VRle::toRle(clipRect); |
| 394 | rle = rle - i.rle(); |
| 395 | break; |
| 396 | } |
| 397 | case LOTMaskData::Mode::Intersect: { |
| 398 | if (rle.empty() && !clipRect.empty()) rle = VRle::toRle(clipRect); |
| 399 | rle = rle & i.rle(); |
| 400 | break; |
| 401 | } |
| 402 | case LOTMaskData::Mode::Difference: { |
| 403 | rle = rle ^ i.rle(); |
| 404 | break; |
| 405 | } |
| 406 | default: |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 |
| 411 | if (!rle.empty() && !rle.unique()) { |
| 412 | mRle.clone(rle); |
| 413 | } else { |
| 414 | mRle = rle; |
| 415 | } |
| 416 | mDirty = false; |
| 417 | return mRle; |
| 418 | } |
| 419 |
| 420 | LOTLayerItem::LOTLayerItem(LOTLayerData *layerData) : mLayerData(layerData) |
| 421 | { |
| 422 | if (mLayerData->mHasMask) |
| 423 | mLayerMask = std::make_unique<LOTLayerMaskItem>(mLayerData); |
| 424 | } |
| 425 |
| 426 | bool LOTLayerItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 427 | LOTVariant &value) |
| 428 | { |
| 429 | if (!keyPath.matches(name(), depth)) { |
| 430 | return false; |
| 431 | } |
| 432 |
| 433 | if (!keyPath.skip(name())) { |
| 434 | if (keyPath.fullyResolvesTo(name(), depth) && transformProp(value.property())) { |
| 435 | mDirtyFlag = DirtyFlagBit::All; |
| 436 | } |
| 437 | } |
| 438 | return true; |
| 439 | } |
| 440 |
| 441 | bool LOTShapeLayerItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 442 | LOTVariant &value) |
| 443 | { |
| 444 | if (LOTLayerItem::resolveKeyPath(keyPath, depth, value)) { |
| 445 | if (keyPath.propagate(name(), depth)) { |
| 446 | uint newDepth = keyPath.nextDepth(name(), depth); |
| 447 | if (mRoot->resolveKeyPath(keyPath, newDepth, value)) { |
| 448 | mDirtyFlag = DirtyFlagBit::All; |
| 449 | } |
| 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | return false; |
| 454 | } |
| 455 |
| 456 | bool LOTCompLayerItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 457 | LOTVariant &value) |
| 458 | { |
| 459 | if (LOTLayerItem::resolveKeyPath(keyPath, depth, value)) { |
| 460 | if (keyPath.propagate(name(), depth)) { |
| 461 | uint newDepth = keyPath.nextDepth(name(), depth); |
| 462 | for (const auto &layer : mLayers) { |
| 463 | if (layer->resolveKeyPath(keyPath, newDepth, value)) { |
| 464 | mDirtyFlag = DirtyFlagBit::All; |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | return true; |
| 469 | } |
| 470 | return false; |
| 471 | } |
| 472 |
| 473 | void LOTLayerItem::update(int frameNumber, const VMatrix &parentMatrix, |
| 474 | float parentAlpha) |
| 475 | { |
| 476 | mFrameNo = frameNumber; |
| 477 | // 1. check if the layer is part of the current frame |
| 478 | if (!visible()) return; |
| 479 |
| 480 | float alpha = parentAlpha * opacity(frameNo()); |
| 481 | if (vIsZero(alpha)) { |
| 482 | mCombinedAlpha = 0; |
| 483 | return; |
| 484 | } |
| 485 |
| 486 | // 2. calculate the parent matrix and alpha |
| 487 | VMatrix m = matrix(frameNo()); |
| 488 | m *= parentMatrix; |
| 489 |
| 490 | // 3. update the dirty flag based on the change |
| 491 | if (!mCombinedMatrix.fuzzyCompare(m)) { |
| 492 | mDirtyFlag |= DirtyFlagBit::Matrix; |
| 493 | } |
| 494 | if (!vCompare(mCombinedAlpha, alpha)) { |
| 495 | mDirtyFlag |= DirtyFlagBit::Alpha; |
| 496 | } |
| 497 | mCombinedMatrix = m; |
| 498 | mCombinedAlpha = alpha; |
| 499 |
| 500 | // 4. update the mask |
| 501 | if (mLayerMask) { |
| 502 | mLayerMask->update(frameNo(), m, alpha, mDirtyFlag); |
| 503 | } |
| 504 |
| 505 | // 5. if no parent property change and layer is static then nothing to do. |
| 506 | if (!mLayerData->precompLayer() && flag().testFlag(DirtyFlagBit::None) && |
| 507 | isStatic()) |
| 508 | return; |
| 509 |
| 510 | // 6. update the content of the layer |
| 511 | updateContent(); |
| 512 |
| 513 | // 7. reset the dirty flag |
| 514 | mDirtyFlag = DirtyFlagBit::None; |
| 515 | } |
| 516 |
| 517 | VMatrix LOTLayerItem::matrix(int frameNo) const |
| 518 | { |
| 519 | return mParentLayer |
| 520 | ? (mLayerData->matrix(frameNo) * mParentLayer->matrix(frameNo)) |
| 521 | : mLayerData->matrix(frameNo); |
| 522 | } |
| 523 |
| 524 | bool LOTLayerItem::visible() const |
| 525 | { |
| 526 | if (frameNo() >= mLayerData->inFrame() && |
| 527 | frameNo() < mLayerData->outFrame()) |
| 528 | return true; |
| 529 | else |
| 530 | return false; |
| 531 | } |
| 532 |
| 533 | LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel) |
| 534 | : LOTLayerItem(layerModel) |
| 535 | { |
| 536 | // 1. create layer item |
| 537 | for (auto &i : mLayerData->mChildren) { |
| 538 | if (i->type() != LOTData::Type::Layer) { |
| 539 | continue; |
| 540 | } |
| 541 | LOTLayerData *layerModel = static_cast<LOTLayerData *>(i.get()); |
| 542 | auto layerItem = LOTCompItem::createLayerItem(layerModel); |
| 543 | if (layerItem) mLayers.push_back(std::move(layerItem)); |
| 544 | } |
| 545 |
| 546 | // 2. update parent layer |
| 547 | for (const auto &layer : mLayers) { |
| 548 | int id = layer->parentId(); |
| 549 | if (id >= 0) { |
| 550 | auto search = |
| 551 | std::find_if(mLayers.begin(), mLayers.end(), |
| 552 | [id](const auto &val) { return val->id() == id; }); |
| 553 | if (search != mLayers.end() && |
| 554 | isGoodParentLayer((*search).get(), layer.get())) { |
| 555 | layer->setParentLayer((*search).get()); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 |
| 560 | // 3. keep the layer in back-to-front order. |
| 561 | // as lottie model keeps the data in front-toback-order. |
| 562 | std::reverse(mLayers.begin(), mLayers.end()); |
| 563 |
| 564 | // 4. check if its a nested composition |
| 565 | if (!layerModel->layerSize().empty()) { |
| 566 | mClipper = std::make_unique<LOTClipperItem>(layerModel->layerSize()); |
| 567 | } |
| 568 |
| 569 | if (mLayers.size() > 1) setComplexContent(true); |
| 570 | } |
| 571 |
| 572 | void LOTCompLayerItem::buildLayerNode() |
| 573 | { |
| 574 | LOTLayerItem::buildLayerNode(); |
| 575 | if (mClipper) { |
| 576 | const std::vector<VPath::Element> &elm = mClipper->mPath.elements(); |
| 577 | const std::vector<VPointF> & pts = mClipper->mPath.points(); |
| 578 | const float *ptPtr = reinterpret_cast<const float *>(pts.data()); |
| 579 | const char * elmPtr = reinterpret_cast<const char *>(elm.data()); |
| 580 | layerNode()->mClipPath.ptPtr = ptPtr; |
| 581 | layerNode()->mClipPath.elmPtr = elmPtr; |
| 582 | layerNode()->mClipPath.ptCount = 2 * pts.size(); |
| 583 | layerNode()->mClipPath.elmCount = elm.size(); |
| 584 | } |
| 585 | if (mLayers.size() != mLayersCNode.size()) { |
| 586 | for (const auto &layer : mLayers) { |
| 587 | layer->buildLayerNode(); |
| 588 | mLayersCNode.push_back(layer->layerNode()); |
| 589 | } |
| 590 | layerNode()->mLayerList.ptr = mLayersCNode.data(); |
| 591 | layerNode()->mLayerList.size = mLayersCNode.size(); |
| 592 | } else { |
| 593 | for (const auto &layer : mLayers) { |
| 594 | layer->buildLayerNode(); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 |
| 599 | void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask, |
| 600 | const VRle &matteRle) |
| 601 | { |
| 602 | if (vIsZero(combinedAlpha())) return; |
| 603 |
| 604 | if (vCompare(combinedAlpha(), 1.0)) { |
| 605 | renderHelper(painter, inheritMask, matteRle); |
| 606 | } else { |
| 607 | if (complexContent()) { |
| 608 | VSize size = painter->clipBoundingRect().size(); |
| 609 | VPainter srcPainter; |
| 610 | VBitmap srcBitmap(size.width(), size.height(), |
| 611 | VBitmap::Format::ARGB32); |
| 612 | srcPainter.begin(&srcBitmap, true); |
| 613 | renderHelper(&srcPainter, inheritMask, matteRle); |
| 614 | srcPainter.end(); |
| 615 | painter->drawBitmap(VPoint(), srcBitmap, combinedAlpha() * 255); |
| 616 | } else { |
| 617 | renderHelper(painter, inheritMask, matteRle); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 |
| 622 | void LOTCompLayerItem::renderHelper(VPainter *painter, const VRle &inheritMask, |
| 623 | const VRle &matteRle) |
| 624 | { |
| 625 | VRle mask; |
| 626 | if (mLayerMask) { |
| 627 | mask = mLayerMask->maskRle(painter->clipBoundingRect()); |
| 628 | if (!inheritMask.empty()) mask = mask & inheritMask; |
| 629 | // if resulting mask is empty then return. |
| 630 | if (mask.empty()) return; |
| 631 | } else { |
| 632 | mask = inheritMask; |
| 633 | } |
| 634 |
| 635 | if (mClipper) { |
| 636 | if (mask.empty()) { |
| 637 | mask = mClipper->rle(); |
| 638 | } else { |
| 639 | mask = mClipper->rle() & mask; |
| 640 | } |
| 641 | } |
| 642 |
| 643 | LOTLayerItem *matte = nullptr; |
| 644 | for (const auto &layer : mLayers) { |
| 645 | if (layer->hasMatte()) { |
| 646 | matte = layer.get(); |
| 647 | } else { |
| 648 | if (layer->visible()) { |
| 649 | if (matte) { |
| 650 | if (matte->visible()) |
| 651 | renderMatteLayer(painter, mask, matteRle, matte, |
| 652 | layer.get()); |
| 653 | } else { |
| 654 | layer->render(painter, mask, matteRle); |
| 655 | } |
| 656 | } |
| 657 | matte = nullptr; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 |
| 662 | void LOTCompLayerItem::renderMatteLayer(VPainter *painter, const VRle &mask, |
| 663 | const VRle & matteRle, |
| 664 | LOTLayerItem *layer, LOTLayerItem *src) |
| 665 | { |
| 666 | VSize size = painter->clipBoundingRect().size(); |
| 667 | // Decide if we can use fast matte. |
| 668 | // 1. draw src layer to matte buffer |
| 669 | VPainter srcPainter; |
| 670 | src->bitmap().reset(size.width(), size.height(), |
| 671 | VBitmap::Format::ARGB32); |
| 672 | srcPainter.begin(&src->bitmap(), true); |
| 673 | src->render(&srcPainter, mask, matteRle); |
| 674 | srcPainter.end(); |
| 675 |
| 676 | // 2. draw layer to layer buffer |
| 677 | VPainter layerPainter; |
| 678 | layer->bitmap().reset(size.width(), size.height(), |
| 679 | VBitmap::Format::ARGB32); |
| 680 | layerPainter.begin(&layer->bitmap(), true); |
| 681 | layer->render(&layerPainter, mask, matteRle); |
| 682 |
| 683 | // 2.1update composition mode |
| 684 | switch (layer->matteType()) { |
| 685 | case MatteType::Alpha: |
| 686 | case MatteType::Luma: { |
| 687 | layerPainter.setCompositionMode( |
| 688 | VPainter::CompositionMode::CompModeDestIn); |
| 689 | break; |
| 690 | } |
| 691 | case MatteType::AlphaInv: |
| 692 | case MatteType::LumaInv: { |
| 693 | layerPainter.setCompositionMode( |
| 694 | VPainter::CompositionMode::CompModeDestOut); |
| 695 | break; |
| 696 | } |
| 697 | default: |
| 698 | break; |
| 699 | } |
| 700 |
| 701 | // 2.2 update srcBuffer if the matte is luma type |
| 702 | if (layer->matteType() == MatteType::Luma || |
| 703 | layer->matteType() == MatteType::LumaInv) { |
| 704 | src->bitmap().updateLuma(); |
| 705 | } |
| 706 |
| 707 | // 2.3 draw src buffer as mask |
| 708 | layerPainter.drawBitmap(VPoint(), src->bitmap()); |
| 709 | layerPainter.end(); |
| 710 | // 3. draw the result buffer into painter |
| 711 | painter->drawBitmap(VPoint(), layer->bitmap()); |
| 712 | } |
| 713 |
| 714 | void LOTClipperItem::update(const VMatrix &matrix) |
| 715 | { |
| 716 | mPath.reset(); |
| 717 | mPath.addRect(VRectF(0, 0, mSize.width(), mSize.height())); |
| 718 | mPath.transform(matrix); |
| 719 | mRasterizer.rasterize(mPath); |
| 720 | } |
| 721 |
| 722 | VRle LOTClipperItem::rle() |
| 723 | { |
| 724 | return mRasterizer.rle(); |
| 725 | } |
| 726 |
| 727 | void LOTCompLayerItem::updateContent() |
| 728 | { |
| 729 | if (mClipper && flag().testFlag(DirtyFlagBit::Matrix)) { |
| 730 | mClipper->update(combinedMatrix()); |
| 731 | } |
| 732 | int mappedFrame = mLayerData->timeRemap(frameNo()); |
| 733 | float alpha = combinedAlpha(); |
| 734 | if (complexContent()) alpha = 1; |
| 735 | for (const auto &layer : mLayers) { |
| 736 | layer->update(mappedFrame, combinedMatrix(), alpha); |
| 737 | } |
| 738 | } |
| 739 |
| 740 | void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list) |
| 741 | { |
| 742 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 743 |
| 744 | LOTLayerItem *matte = nullptr; |
| 745 | for (const auto &layer : mLayers) { |
| 746 | if (layer->hasMatte()) { |
| 747 | matte = layer.get(); |
| 748 | } else { |
| 749 | if (layer->visible()) { |
| 750 | if (matte) { |
| 751 | if (matte->visible()) { |
| 752 | layer->renderList(list); |
| 753 | matte->renderList(list); |
| 754 | } |
| 755 | } else { |
| 756 | layer->renderList(list); |
| 757 | } |
| 758 | } |
| 759 | matte = nullptr; |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 |
| 764 | LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData) |
| 765 | : LOTLayerItem(layerData) |
| 766 | { |
| 767 | } |
| 768 |
| 769 | void LOTSolidLayerItem::updateContent() |
| 770 | { |
| 771 | if (flag() & DirtyFlagBit::Matrix) { |
| 772 | VPath path; |
| 773 | path.addRect( |
| 774 | VRectF(0, 0, |
| 775 | mLayerData->layerSize().width(), |
| 776 | mLayerData->layerSize().height())); |
| 777 | path.transform(combinedMatrix()); |
| 778 | mRenderNode.mFlag |= VDrawable::DirtyState::Path; |
| 779 | mRenderNode.mPath = path; |
| 780 | } |
| 781 | if (flag() & DirtyFlagBit::Alpha) { |
| 782 | LottieColor color = mLayerData->solidColor(); |
| 783 | VBrush brush(color.toColor(combinedAlpha())); |
| 784 | mRenderNode.setBrush(brush); |
| 785 | mRenderNode.mFlag |= VDrawable::DirtyState::Brush; |
| 786 | } |
| 787 | } |
| 788 |
| 789 | void LOTSolidLayerItem::buildLayerNode() |
| 790 | { |
| 791 | LOTLayerItem::buildLayerNode(); |
| 792 |
| 793 | mDrawableList.clear(); |
| 794 | renderList(mDrawableList); |
| 795 |
| 796 | mCNodeList.clear(); |
| 797 | for (auto &i : mDrawableList) { |
| 798 | LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i); |
| 799 | lotDrawable->sync(); |
| 800 | mCNodeList.push_back(lotDrawable->mCNode.get()); |
| 801 | } |
| 802 | layerNode()->mNodeList.ptr = mCNodeList.data(); |
| 803 | layerNode()->mNodeList.size = mCNodeList.size(); |
| 804 | } |
| 805 |
| 806 | void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list) |
| 807 | { |
| 808 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 809 |
| 810 | list.push_back(&mRenderNode); |
| 811 | } |
| 812 |
| 813 | LOTImageLayerItem::LOTImageLayerItem(LOTLayerData *layerData) |
| 814 | : LOTLayerItem(layerData) |
| 815 | { |
| 816 | if (!mLayerData->asset()) return; |
| 817 |
| 818 | VBrush brush(mLayerData->asset()->bitmap()); |
| 819 | mRenderNode.setBrush(brush); |
| 820 | } |
| 821 |
| 822 | void LOTImageLayerItem::updateContent() |
| 823 | { |
| 824 | if (!mLayerData->asset()) return; |
| 825 |
| 826 | if (flag() & DirtyFlagBit::Matrix) { |
| 827 | VPath path; |
| 828 | path.addRect(VRectF(0, 0, mLayerData->asset()->mWidth, |
| 829 | mLayerData->asset()->mHeight)); |
| 830 | path.transform(combinedMatrix()); |
| 831 | mRenderNode.mFlag |= VDrawable::DirtyState::Path; |
| 832 | mRenderNode.mPath = path; |
| 833 | mRenderNode.mBrush.setMatrix(combinedMatrix()); |
| 834 | } |
| 835 |
| 836 | if (flag() & DirtyFlagBit::Alpha) { |
| 837 | //@TODO handle alpha with the image. |
| 838 | } |
| 839 | } |
| 840 |
| 841 | void LOTImageLayerItem::renderList(std::vector<VDrawable *> &list) |
| 842 | { |
| 843 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 844 |
| 845 | list.push_back(&mRenderNode); |
| 846 | } |
| 847 |
| 848 | void LOTImageLayerItem::buildLayerNode() |
| 849 | { |
| 850 | LOTLayerItem::buildLayerNode(); |
| 851 |
| 852 | mDrawableList.clear(); |
| 853 | renderList(mDrawableList); |
| 854 |
| 855 | mCNodeList.clear(); |
| 856 | for (auto &i : mDrawableList) { |
| 857 | LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i); |
| 858 | lotDrawable->sync(); |
| 859 |
| 860 | lotDrawable->mCNode->mImageInfo.data = |
| 861 | lotDrawable->mBrush.mTexture.data(); |
| 862 | lotDrawable->mCNode->mImageInfo.width = |
| 863 | lotDrawable->mBrush.mTexture.width(); |
| 864 | lotDrawable->mCNode->mImageInfo.height = |
| 865 | lotDrawable->mBrush.mTexture.height(); |
| 866 |
| 867 | lotDrawable->mCNode->mImageInfo.mMatrix.m11 = combinedMatrix().m_11(); |
| 868 | lotDrawable->mCNode->mImageInfo.mMatrix.m12 = combinedMatrix().m_12(); |
| 869 | lotDrawable->mCNode->mImageInfo.mMatrix.m13 = combinedMatrix().m_13(); |
| 870 |
| 871 | lotDrawable->mCNode->mImageInfo.mMatrix.m21 = combinedMatrix().m_21(); |
| 872 | lotDrawable->mCNode->mImageInfo.mMatrix.m22 = combinedMatrix().m_22(); |
| 873 | lotDrawable->mCNode->mImageInfo.mMatrix.m23 = combinedMatrix().m_23(); |
| 874 |
| 875 | lotDrawable->mCNode->mImageInfo.mMatrix.m31 = combinedMatrix().m_tx(); |
| 876 | lotDrawable->mCNode->mImageInfo.mMatrix.m32 = combinedMatrix().m_ty(); |
| 877 | lotDrawable->mCNode->mImageInfo.mMatrix.m33 = combinedMatrix().m_33(); |
| 878 |
| 879 | mCNodeList.push_back(lotDrawable->mCNode.get()); |
| 880 | } |
| 881 | layerNode()->mNodeList.ptr = mCNodeList.data(); |
| 882 | layerNode()->mNodeList.size = mCNodeList.size(); |
| 883 | } |
| 884 |
| 885 | LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData) |
| 886 | : LOTLayerItem(layerData) |
| 887 | { |
| 888 | } |
| 889 | void LOTNullLayerItem::updateContent() {} |
| 890 |
| 891 | LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData) |
| 892 | : LOTLayerItem(layerData), |
| 893 | mRoot(std::make_unique<LOTContentGroupItem>(nullptr)) |
| 894 | { |
| 895 | mRoot->addChildren(layerData); |
| 896 |
| 897 | std::vector<LOTPathDataItem *> list; |
| 898 | mRoot->processPaintItems(list); |
| 899 |
| 900 | if (layerData->hasPathOperator()) { |
| 901 | list.clear(); |
| 902 | mRoot->processTrimItems(list); |
| 903 | } |
| 904 | } |
| 905 |
| 906 | std::unique_ptr<LOTContentItem> LOTShapeLayerItem::createContentItem( |
| 907 | LOTData *contentData) |
| 908 | { |
| 909 | switch (contentData->type()) { |
| 910 | case LOTData::Type::ShapeGroup: { |
| 911 | return std::make_unique<LOTContentGroupItem>( |
| 912 | static_cast<LOTGroupData *>(contentData)); |
| 913 | } |
| 914 | case LOTData::Type::Rect: { |
| 915 | return std::make_unique<LOTRectItem>( |
| 916 | static_cast<LOTRectData *>(contentData)); |
| 917 | } |
| 918 | case LOTData::Type::Ellipse: { |
| 919 | return std::make_unique<LOTEllipseItem>( |
| 920 | static_cast<LOTEllipseData *>(contentData)); |
| 921 | } |
| 922 | case LOTData::Type::Shape: { |
| 923 | return std::make_unique<LOTShapeItem>( |
| 924 | static_cast<LOTShapeData *>(contentData)); |
| 925 | } |
| 926 | case LOTData::Type::Polystar: { |
| 927 | return std::make_unique<LOTPolystarItem>( |
| 928 | static_cast<LOTPolystarData *>(contentData)); |
| 929 | } |
| 930 | case LOTData::Type::Fill: { |
| 931 | return std::make_unique<LOTFillItem>( |
| 932 | static_cast<LOTFillData *>(contentData)); |
| 933 | } |
| 934 | case LOTData::Type::GFill: { |
| 935 | return std::make_unique<LOTGFillItem>( |
| 936 | static_cast<LOTGFillData *>(contentData)); |
| 937 | } |
| 938 | case LOTData::Type::Stroke: { |
| 939 | return std::make_unique<LOTStrokeItem>( |
| 940 | static_cast<LOTStrokeData *>(contentData)); |
| 941 | } |
| 942 | case LOTData::Type::GStroke: { |
| 943 | return std::make_unique<LOTGStrokeItem>( |
| 944 | static_cast<LOTGStrokeData *>(contentData)); |
| 945 | } |
| 946 | case LOTData::Type::Repeater: { |
| 947 | return std::make_unique<LOTRepeaterItem>( |
| 948 | static_cast<LOTRepeaterData *>(contentData)); |
| 949 | } |
| 950 | case LOTData::Type::Trim: { |
| 951 | return std::make_unique<LOTTrimItem>( |
| 952 | static_cast<LOTTrimData *>(contentData)); |
| 953 | } |
| 954 | default: |
| 955 | return nullptr; |
| 956 | break; |
| 957 | } |
| 958 | } |
| 959 |
| 960 | void LOTShapeLayerItem::updateContent() |
| 961 | { |
| 962 | mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag()); |
| 963 |
| 964 | if (mLayerData->hasPathOperator()) { |
| 965 | mRoot->applyTrim(); |
| 966 | } |
| 967 | } |
| 968 |
| 969 | void LOTShapeLayerItem::buildLayerNode() |
| 970 | { |
| 971 | LOTLayerItem::buildLayerNode(); |
| 972 |
| 973 | mDrawableList.clear(); |
| 974 | renderList(mDrawableList); |
| 975 |
| 976 | mCNodeList.clear(); |
| 977 | for (auto &i : mDrawableList) { |
| 978 | LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i); |
| 979 | lotDrawable->sync(); |
| 980 | mCNodeList.push_back(lotDrawable->mCNode.get()); |
| 981 | } |
| 982 | layerNode()->mNodeList.ptr = mCNodeList.data(); |
| 983 | layerNode()->mNodeList.size = mCNodeList.size(); |
| 984 | } |
| 985 |
| 986 | void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list) |
| 987 | { |
| 988 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 989 | mRoot->renderList(list); |
| 990 | } |
| 991 |
| 992 | bool LOTContentGroupItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 993 | LOTVariant &value) |
| 994 | { |
| 995 | if (!keyPath.matches(name(), depth)) { |
| 996 | return false; |
| 997 | } |
| 998 |
| 999 | if (!keyPath.skip(name())) { |
| 1000 | if (keyPath.fullyResolvesTo(name(), depth) && |
| 1001 | transformProp(value.property())) { |
| 1002 | //@TODO handle property update |
| 1003 | } |
| 1004 | } |
| 1005 |
| 1006 | if (keyPath.propagate(name(), depth)) { |
| 1007 | uint newDepth = keyPath.nextDepth(name(), depth); |
| 1008 | for (auto &child : mContents) { |
| 1009 | child->resolveKeyPath(keyPath, newDepth, value); |
| 1010 | } |
| 1011 | } |
| 1012 | return true; |
| 1013 | } |
| 1014 |
| 1015 | bool LOTFillItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 1016 | LOTVariant &value) |
| 1017 | { |
| 1018 | if (!keyPath.matches(mModel.name(), depth)) { |
| 1019 | return false; |
| 1020 | } |
| 1021 |
| 1022 | if (keyPath.fullyResolvesTo(mModel.name(), depth) && |
| 1023 | fillProp(value.property())) { |
| 1024 | mModel.filter().addValue(value); |
| 1025 | return true; |
| 1026 | } |
| 1027 | return false; |
| 1028 | } |
| 1029 |
| 1030 | bool LOTStrokeItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 1031 | LOTVariant &value) |
| 1032 | { |
| 1033 | if (!keyPath.matches(mModel.name(), depth)) { |
| 1034 | return false; |
| 1035 | } |
| 1036 |
| 1037 | if (keyPath.fullyResolvesTo(mModel.name(), depth) && |
| 1038 | strokeProp(value.property())) { |
| 1039 | mModel.filter().addValue(value); |
| 1040 | return true; |
| 1041 | } |
| 1042 | return false; |
| 1043 | } |
| 1044 |
| 1045 | LOTContentGroupItem::LOTContentGroupItem(LOTGroupData *data) |
| 1046 | : LOTContentItem(ContentType::Group), mData(data) |
| 1047 | { |
| 1048 | addChildren(mData); |
| 1049 | } |
| 1050 |
| 1051 | void LOTContentGroupItem::addChildren(LOTGroupData *data) |
| 1052 | { |
| 1053 | if (!data) return; |
| 1054 |
| 1055 | for (auto &i : data->mChildren) { |
| 1056 | auto content = LOTShapeLayerItem::createContentItem(i.get()); |
| 1057 | if (content) { |
| 1058 | content->setParent(this); |
| 1059 | mContents.push_back(std::move(content)); |
| 1060 | } |
| 1061 | } |
| 1062 |
| 1063 | // keep the content in back-to-front order. |
| 1064 | std::reverse(mContents.begin(), mContents.end()); |
| 1065 | } |
| 1066 |
| 1067 | void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix, |
| 1068 | float parentAlpha, const DirtyFlag &flag) |
| 1069 | { |
| 1070 | VMatrix m = parentMatrix; |
| 1071 | float alpha = parentAlpha; |
| 1072 | DirtyFlag newFlag = flag; |
| 1073 |
| 1074 | if (mData && mData->mTransform) { |
| 1075 | // update the matrix and the flag |
| 1076 | if ((flag & DirtyFlagBit::Matrix) || !mData->mTransform->isStatic()) { |
| 1077 | newFlag |= DirtyFlagBit::Matrix; |
| 1078 | } |
| 1079 | m = mData->mTransform->matrix(frameNo); |
| 1080 | m *= parentMatrix; |
| 1081 | alpha *= mData->mTransform->opacity(frameNo); |
| 1082 |
| 1083 | if (!vCompare(alpha, parentAlpha)) { |
| 1084 | newFlag |= DirtyFlagBit::Alpha; |
| 1085 | } |
| 1086 | } |
| 1087 |
| 1088 | mMatrix = m; |
| 1089 |
| 1090 | for (const auto &content : mContents) { |
| 1091 | content->update(frameNo, m, alpha, newFlag); |
| 1092 | } |
| 1093 | } |
| 1094 |
| 1095 | void LOTContentGroupItem::applyTrim() |
| 1096 | { |
| 1097 | for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) { |
| 1098 | auto content = (*i).get(); |
| 1099 | switch (content->type()) { |
| 1100 | case ContentType::Trim: { |
| 1101 | static_cast<LOTTrimItem *>(content)->update(); |
| 1102 | break; |
| 1103 | } |
| 1104 | case ContentType::Group: { |
| 1105 | static_cast<LOTContentGroupItem *>(content)->applyTrim(); |
| 1106 | break; |
| 1107 | } |
| 1108 | default: |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 |
| 1114 | void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list) |
| 1115 | { |
| 1116 | for (const auto &content : mContents) { |
| 1117 | content->renderList(list); |
| 1118 | } |
| 1119 | } |
| 1120 |
| 1121 | void LOTContentGroupItem::processPaintItems( |
| 1122 | std::vector<LOTPathDataItem *> &list) |
| 1123 | { |
| 1124 | int curOpCount = list.size(); |
| 1125 | for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) { |
| 1126 | auto content = (*i).get(); |
| 1127 | switch (content->type()) { |
| 1128 | case ContentType::Path: { |
| 1129 | list.push_back(static_cast<LOTPathDataItem *>(content)); |
| 1130 | break; |
| 1131 | } |
| 1132 | case ContentType::Paint: { |
| 1133 | static_cast<LOTPaintDataItem *>(content)->addPathItems(list, |
| 1134 | curOpCount); |
| 1135 | break; |
| 1136 | } |
| 1137 | case ContentType::Group: { |
| 1138 | static_cast<LOTContentGroupItem *>(content)->processPaintItems( |
| 1139 | list); |
| 1140 | break; |
| 1141 | } |
| 1142 | default: |
| 1143 | break; |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 |
| 1148 | void LOTContentGroupItem::processTrimItems(std::vector<LOTPathDataItem *> &list) |
| 1149 | { |
| 1150 | int curOpCount = list.size(); |
| 1151 | for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) { |
| 1152 | auto content = (*i).get(); |
| 1153 |
| 1154 | switch (content->type()) { |
| 1155 | case ContentType::Path: { |
| 1156 | list.push_back(static_cast<LOTPathDataItem *>(content)); |
| 1157 | break; |
| 1158 | } |
| 1159 | case ContentType::Trim: { |
| 1160 | static_cast<LOTTrimItem *>(content)->addPathItems(list, curOpCount); |
| 1161 | break; |
| 1162 | } |
| 1163 | case ContentType::Group: { |
| 1164 | static_cast<LOTContentGroupItem *>(content)->processTrimItems(list); |
| 1165 | break; |
| 1166 | } |
| 1167 | default: |
| 1168 | break; |
| 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 |
| 1173 | /* |
| 1174 | * LOTPathDataItem uses 3 path objects for path object reuse. |
| 1175 | * mLocalPath - keeps track of the local path of the item before |
| 1176 | * applying path operation and transformation. |
| 1177 | * mTemp - keeps a referece to the mLocalPath and can be updated by the |
| 1178 | * path operation objects(trim, merge path), |
| 1179 | * mFinalPath - it takes a deep copy of the intermediate path(mTemp) each time |
| 1180 | * when the path is dirty(so if path changes every frame we don't realloc just |
| 1181 | * copy to the final path). NOTE: As path objects are COW objects we have to be |
| 1182 | * carefull about the refcount so that we don't generate deep copy while |
| 1183 | * modifying the path objects. |
| 1184 | */ |
| 1185 | void LOTPathDataItem::update(int frameNo, const VMatrix &, float, |
| 1186 | const DirtyFlag &flag) |
| 1187 | { |
| 1188 | mPathChanged = false; |
| 1189 |
| 1190 | // 1. update the local path if needed |
| 1191 | if (hasChanged(frameNo)) { |
| 1192 | // loose the reference to mLocalPath if any |
| 1193 | // from the last frame update. |
| 1194 | mTemp = VPath(); |
| 1195 |
| 1196 | updatePath(mLocalPath, frameNo); |
| 1197 | mPathChanged = true; |
| 1198 | mNeedUpdate = true; |
| 1199 | } |
| 1200 | // 2. keep a reference path in temp in case there is some |
| 1201 | // path operation like trim which will update the path. |
| 1202 | // we don't want to update the local path. |
| 1203 | mTemp = mLocalPath; |
| 1204 |
| 1205 | // 3. compute the final path with parentMatrix |
| 1206 | if ((flag & DirtyFlagBit::Matrix) || mPathChanged) { |
| 1207 | mPathChanged = true; |
| 1208 | } |
| 1209 | } |
| 1210 |
| 1211 | const VPath &LOTPathDataItem::finalPath() |
| 1212 | { |
| 1213 | if (mPathChanged || mNeedUpdate) { |
| 1214 | mFinalPath.clone(mTemp); |
| 1215 | mFinalPath.transform( |
| 1216 | static_cast<LOTContentGroupItem *>(parent())->matrix()); |
| 1217 | mNeedUpdate = false; |
| 1218 | } |
| 1219 | return mFinalPath; |
| 1220 | } |
| 1221 | LOTRectItem::LOTRectItem(LOTRectData *data) |
| 1222 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1223 | { |
| 1224 | } |
| 1225 |
| 1226 | void LOTRectItem::updatePath(VPath &path, int frameNo) |
| 1227 | { |
| 1228 | VPointF pos = mData->mPos.value(frameNo); |
| 1229 | VPointF size = mData->mSize.value(frameNo); |
| 1230 | float roundness = mData->mRound.value(frameNo); |
| 1231 | VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(), |
| 1232 | size.y()); |
| 1233 |
| 1234 | path.reset(); |
| 1235 | path.addRoundRect(r, roundness, mData->direction()); |
| 1236 | } |
| 1237 |
| 1238 | LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data) |
| 1239 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1240 | { |
| 1241 | } |
| 1242 |
| 1243 | void LOTEllipseItem::updatePath(VPath &path, int frameNo) |
| 1244 | { |
| 1245 | VPointF pos = mData->mPos.value(frameNo); |
| 1246 | VPointF size = mData->mSize.value(frameNo); |
| 1247 | VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(), |
| 1248 | size.y()); |
| 1249 |
| 1250 | path.reset(); |
| 1251 | path.addOval(r, mData->direction()); |
| 1252 | } |
| 1253 |
| 1254 | LOTShapeItem::LOTShapeItem(LOTShapeData *data) |
| 1255 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1256 | { |
| 1257 | } |
| 1258 |
| 1259 | void LOTShapeItem::updatePath(VPath &path, int frameNo) |
| 1260 | { |
| 1261 | mData->mShape.value(frameNo).toPath(path); |
| 1262 | } |
| 1263 |
| 1264 | LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data) |
| 1265 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1266 | { |
| 1267 | } |
| 1268 |
| 1269 | void LOTPolystarItem::updatePath(VPath &path, int frameNo) |
| 1270 | { |
| 1271 | VPointF pos = mData->mPos.value(frameNo); |
| 1272 | float points = mData->mPointCount.value(frameNo); |
| 1273 | float innerRadius = mData->mInnerRadius.value(frameNo); |
| 1274 | float outerRadius = mData->mOuterRadius.value(frameNo); |
| 1275 | float innerRoundness = mData->mInnerRoundness.value(frameNo); |
| 1276 | float outerRoundness = mData->mOuterRoundness.value(frameNo); |
| 1277 | float rotation = mData->mRotation.value(frameNo); |
| 1278 |
| 1279 | path.reset(); |
| 1280 | VMatrix m; |
| 1281 |
| 1282 | if (mData->mType == LOTPolystarData::PolyType::Star) { |
| 1283 | path.addPolystar(points, innerRadius, outerRadius, innerRoundness, |
| 1284 | outerRoundness, 0.0, 0.0, 0.0, mData->direction()); |
| 1285 | } else { |
| 1286 | path.addPolygon(points, outerRadius, outerRoundness, 0.0, 0.0, 0.0, |
| 1287 | mData->direction()); |
| 1288 | } |
| 1289 |
| 1290 | m.translate(pos.x(), pos.y()).rotate(rotation); |
| 1291 | m.rotate(rotation); |
| 1292 | path.transform(m); |
| 1293 | } |
| 1294 |
| 1295 | /* |
| 1296 | * PaintData Node handling |
| 1297 | * |
| 1298 | */ |
| 1299 | LOTPaintDataItem::LOTPaintDataItem(bool staticContent) |
| 1300 | : LOTContentItem(ContentType::Paint), mStaticContent(staticContent) |
| 1301 | { |
| 1302 | } |
| 1303 |
| 1304 | void LOTPaintDataItem::update(int frameNo, const VMatrix & /*parentMatrix*/, |
| 1305 | float parentAlpha, const DirtyFlag &flag) |
| 1306 | { |
| 1307 | mRenderNodeUpdate = true; |
| 1308 | mParentAlpha = parentAlpha; |
| 1309 | mFlag = flag; |
| 1310 | mFrameNo = frameNo; |
| 1311 |
| 1312 | updateContent(frameNo); |
| 1313 | } |
| 1314 |
| 1315 | void LOTPaintDataItem::updateRenderNode() |
| 1316 | { |
| 1317 | bool dirty = false; |
| 1318 | for (auto &i : mPathItems) { |
| 1319 | if (i->dirty()) { |
| 1320 | dirty = true; |
| 1321 | break; |
| 1322 | } |
| 1323 | } |
| 1324 |
| 1325 | if (dirty) { |
| 1326 | mPath.reset(); |
| 1327 |
| 1328 | for (auto &i : mPathItems) { |
| 1329 | mPath.addPath(i->finalPath()); |
| 1330 | } |
| 1331 | mDrawable.setPath(mPath); |
| 1332 | } else { |
| 1333 | if (mDrawable.mFlag & VDrawable::DirtyState::Path) |
| 1334 | mDrawable.mPath = mPath; |
| 1335 | } |
| 1336 | } |
| 1337 |
| 1338 | void LOTPaintDataItem::renderList(std::vector<VDrawable *> &list) |
| 1339 | { |
| 1340 | if (mRenderNodeUpdate) { |
| 1341 | updateRenderNode(); |
| 1342 | LOTPaintDataItem::updateRenderNode(); |
| 1343 | mRenderNodeUpdate = false; |
| 1344 | } |
| 1345 | list.push_back(&mDrawable); |
| 1346 | } |
| 1347 |
| 1348 | void LOTPaintDataItem::addPathItems(std::vector<LOTPathDataItem *> &list, |
| 1349 | int startOffset) |
| 1350 | { |
| 1351 | std::copy(list.begin() + startOffset, list.end(), |
| 1352 | back_inserter(mPathItems)); |
| 1353 | } |
| 1354 |
| 1355 | LOTFillItem::LOTFillItem(LOTFillData *data) |
| 1356 | : LOTPaintDataItem(data->isStatic()), mModel(data) |
| 1357 | { |
| 1358 | } |
| 1359 |
| 1360 | void LOTFillItem::updateContent(int frameNo) |
| 1361 | { |
| 1362 | mColor = mModel.color(frameNo).toColor(mModel.opacity(frameNo)); |
| 1363 | } |
| 1364 |
| 1365 | void LOTFillItem::updateRenderNode() |
| 1366 | { |
| 1367 | VColor color = mColor; |
| 1368 |
| 1369 | color.setAlpha(color.a * parentAlpha()); |
| 1370 | VBrush brush(color); |
| 1371 | mDrawable.setBrush(brush); |
| 1372 | mDrawable.setFillRule(mModel.fillRule()); |
| 1373 | } |
| 1374 |
| 1375 | LOTGFillItem::LOTGFillItem(LOTGFillData *data) |
| 1376 | : LOTPaintDataItem(data->isStatic()), mData(data) |
| 1377 | { |
| 1378 | } |
| 1379 |
| 1380 | void LOTGFillItem::updateContent(int frameNo) |
| 1381 | { |
| 1382 | mAlpha = mData->opacity(frameNo); |
| 1383 | mData->update(mGradient, frameNo); |
| 1384 | mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix(); |
| 1385 | mFillRule = mData->fillRule(); |
| 1386 | } |
| 1387 |
| 1388 | void LOTGFillItem::updateRenderNode() |
| 1389 | { |
| 1390 | mGradient->setAlpha(mAlpha * parentAlpha()); |
| 1391 | mDrawable.setBrush(VBrush(mGradient.get())); |
| 1392 | mDrawable.setFillRule(mFillRule); |
| 1393 | } |
| 1394 |
| 1395 | LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data) |
| 1396 | : LOTPaintDataItem(data->isStatic()), mModel(data) |
| 1397 | { |
| 1398 | mDashArraySize = 0; |
| 1399 | } |
| 1400 |
| 1401 | void LOTStrokeItem::updateContent(int frameNo) |
| 1402 | { |
| 1403 | mColor = mModel.color(frameNo).toColor(mModel.opacity(frameNo)); |
| 1404 | mWidth = mModel.strokeWidth(frameNo); |
| 1405 | if (mModel.hasDashInfo()) { |
| 1406 | mDashArraySize = mModel.getDashInfo(frameNo, mDashArray); |
| 1407 | } |
| 1408 | } |
| 1409 |
| 1410 | static float getScale(const VMatrix &matrix) |
| 1411 | { |
| 1412 | constexpr float SQRT_2 = 1.41421; |
| 1413 | VPointF p1(0, 0); |
| 1414 | VPointF p2(SQRT_2, SQRT_2); |
| 1415 | p1 = matrix.map(p1); |
| 1416 | p2 = matrix.map(p2); |
| 1417 | VPointF final = p2 - p1; |
| 1418 |
| 1419 | return std::sqrt(final.x() * final.x() + final.y() * final.y()) / 2.0; |
| 1420 | } |
| 1421 |
| 1422 | void LOTStrokeItem::updateRenderNode() |
| 1423 | { |
| 1424 | VColor color = mColor; |
| 1425 |
| 1426 | color.setAlpha(color.a * parentAlpha()); |
| 1427 | VBrush brush(color); |
| 1428 | mDrawable.setBrush(brush); |
| 1429 | float scale = |
| 1430 | getScale(static_cast<LOTContentGroupItem *>(parent())->matrix()); |
| 1431 | mDrawable.setStrokeInfo(mModel.capStyle(), mModel.joinStyle(), |
| 1432 | mModel.meterLimit(), mWidth * scale); |
| 1433 | if (mDashArraySize) { |
| 1434 | for (int i = 0; i < mDashArraySize; i++) mDashArray[i] *= scale; |
| 1435 |
| 1436 | /* AE draw the dash even if dash value is 0 */ |
| 1437 | if (vCompare(mDashArray[0], 0.0f)) mDashArray[0] = 0.1; |
| 1438 |
| 1439 | mDrawable.setDashInfo(mDashArray, mDashArraySize); |
| 1440 | } |
| 1441 | } |
| 1442 |
| 1443 | LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data) |
| 1444 | : LOTPaintDataItem(data->isStatic()), mData(data) |
| 1445 | { |
| 1446 | mDashArraySize = 0; |
| 1447 | } |
| 1448 |
| 1449 | void LOTGStrokeItem::updateContent(int frameNo) |
| 1450 | { |
| 1451 | mAlpha = mData->opacity(frameNo); |
| 1452 | mData->update(mGradient, frameNo); |
| 1453 | mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix(); |
| 1454 | mCap = mData->capStyle(); |
| 1455 | mJoin = mData->joinStyle(); |
| 1456 | mMiterLimit = mData->meterLimit(); |
| 1457 | mWidth = mData->width(frameNo); |
| 1458 | if (mData->hasDashInfo()) { |
| 1459 | mDashArraySize = mData->getDashInfo(frameNo, mDashArray); |
| 1460 | } |
| 1461 | } |
| 1462 |
| 1463 | void LOTGStrokeItem::updateRenderNode() |
| 1464 | { |
| 1465 | float scale = getScale(mGradient->mMatrix); |
| 1466 | mGradient->setAlpha(mAlpha * parentAlpha()); |
| 1467 | mDrawable.setBrush(VBrush(mGradient.get())); |
| 1468 | mDrawable.setStrokeInfo(mCap, mJoin, mMiterLimit, mWidth * scale); |
| 1469 | if (mDashArraySize) { |
| 1470 | for (int i = 0; i < mDashArraySize; i++) mDashArray[i] *= scale; |
| 1471 | mDrawable.setDashInfo(mDashArray, mDashArraySize); |
| 1472 | } |
| 1473 | } |
| 1474 |
| 1475 | LOTTrimItem::LOTTrimItem(LOTTrimData *data) |
| 1476 | : LOTContentItem(ContentType::Trim), mData(data) |
| 1477 | { |
| 1478 | } |
| 1479 |
| 1480 | void LOTTrimItem::update(int frameNo, const VMatrix & /*parentMatrix*/, |
| 1481 | float /*parentAlpha*/, const DirtyFlag & /*flag*/) |
| 1482 | { |
| 1483 | mDirty = false; |
| 1484 |
| 1485 | if (mCache.mFrameNo == frameNo) return; |
| 1486 |
| 1487 | LOTTrimData::Segment segment = mData->segment(frameNo); |
| 1488 |
| 1489 | if (!(vCompare(mCache.mSegment.start, segment.start) && |
| 1490 | vCompare(mCache.mSegment.end, segment.end))) { |
| 1491 | mDirty = true; |
| 1492 | mCache.mSegment = segment; |
| 1493 | } |
| 1494 | mCache.mFrameNo = frameNo; |
| 1495 | } |
| 1496 |
| 1497 | void LOTTrimItem::update() |
| 1498 | { |
| 1499 | // when both path and trim are not dirty |
| 1500 | if (!(mDirty || pathDirty())) return; |
| 1501 |
| 1502 | if (vCompare(mCache.mSegment.start, mCache.mSegment.end)) { |
| 1503 | for (auto &i : mPathItems) { |
| 1504 | i->updatePath(VPath()); |
| 1505 | } |
| 1506 | return; |
| 1507 | } |
| 1508 |
| 1509 | if (vCompare(std::fabs(mCache.mSegment.start - mCache.mSegment.end), 1)) { |
| 1510 | for (auto &i : mPathItems) { |
| 1511 | i->updatePath(i->localPath()); |
| 1512 | } |
| 1513 | return; |
| 1514 | } |
| 1515 |
| 1516 | if (mData->type() == LOTTrimData::TrimType::Simultaneously) { |
| 1517 | for (auto &i : mPathItems) { |
| 1518 | VPathMesure pm; |
| 1519 | pm.setStart(mCache.mSegment.start); |
| 1520 | pm.setEnd(mCache.mSegment.end); |
| 1521 | i->updatePath(pm.trim(i->localPath())); |
| 1522 | } |
| 1523 | } else { // LOTTrimData::TrimType::Individually |
| 1524 | float totalLength = 0.0; |
| 1525 | for (auto &i : mPathItems) { |
| 1526 | totalLength += i->localPath().length(); |
| 1527 | } |
| 1528 | float start = totalLength * mCache.mSegment.start; |
| 1529 | float end = totalLength * mCache.mSegment.end; |
| 1530 |
| 1531 | if (start < end) { |
| 1532 | float curLen = 0.0; |
| 1533 | for (auto &i : mPathItems) { |
| 1534 | if (curLen > end) { |
| 1535 | // update with empty path. |
| 1536 | i->updatePath(VPath()); |
| 1537 | continue; |
| 1538 | } |
| 1539 | float len = i->localPath().length(); |
| 1540 |
| 1541 | if (curLen < start && curLen + len < start) { |
| 1542 | curLen += len; |
| 1543 | // update with empty path. |
| 1544 | i->updatePath(VPath()); |
| 1545 | continue; |
| 1546 | } else if (start <= curLen && end >= curLen + len) { |
| 1547 | // inside segment |
| 1548 | curLen += len; |
| 1549 | continue; |
| 1550 | } else { |
| 1551 | float local_start = start > curLen ? start - curLen : 0; |
| 1552 | local_start /= len; |
| 1553 | float local_end = curLen + len < end ? len : end - curLen; |
| 1554 | local_end /= len; |
| 1555 | VPathMesure pm; |
| 1556 | pm.setStart(local_start); |
| 1557 | pm.setEnd(local_end); |
| 1558 | VPath p = pm.trim(i->localPath()); |
| 1559 | i->updatePath(p); |
| 1560 | curLen += len; |
| 1561 | } |
| 1562 | } |
| 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 |
| 1567 | void LOTTrimItem::addPathItems(std::vector<LOTPathDataItem *> &list, |
| 1568 | int startOffset) |
| 1569 | { |
| 1570 | std::copy(list.begin() + startOffset, list.end(), |
| 1571 | back_inserter(mPathItems)); |
| 1572 | } |
| 1573 |
| 1574 | LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data) : mRepeaterData(data) |
| 1575 | { |
| 1576 | assert(mRepeaterData->content()); |
| 1577 |
| 1578 | mCopies = mRepeaterData->maxCopies(); |
| 1579 |
| 1580 | for (int i = 0; i < mCopies; i++) { |
| 1581 | auto content = |
| 1582 | std::make_unique<LOTContentGroupItem>(mRepeaterData->content()); |
| 1583 | content->setParent(this); |
| 1584 | mContents.push_back(std::move(content)); |
| 1585 | } |
| 1586 | } |
| 1587 |
| 1588 | void LOTRepeaterItem::update(int frameNo, const VMatrix &parentMatrix, |
| 1589 | float parentAlpha, const DirtyFlag &flag) |
| 1590 | { |
| 1591 | DirtyFlag newFlag = flag; |
| 1592 |
| 1593 | float copies = mRepeaterData->copies(frameNo); |
| 1594 | int visibleCopies = int(copies); |
| 1595 |
| 1596 | if (visibleCopies == 0) { |
| 1597 | mHidden = true; |
| 1598 | return; |
| 1599 | } else { |
| 1600 | mHidden = false; |
| 1601 | } |
| 1602 |
| 1603 | if (!mRepeaterData->isStatic()) newFlag |= DirtyFlagBit::Matrix; |
| 1604 |
| 1605 | float offset = mRepeaterData->offset(frameNo); |
| 1606 | float startOpacity = mRepeaterData->mTransform.startOpacity(frameNo); |
| 1607 | float endOpacity = mRepeaterData->mTransform.endOpacity(frameNo); |
| 1608 |
| 1609 | newFlag |= DirtyFlagBit::Alpha; |
| 1610 |
| 1611 | for (int i = 0; i < mCopies; ++i) { |
| 1612 | float newAlpha = |
| 1613 | parentAlpha * lerp(startOpacity, endOpacity, i / copies); |
| 1614 |
| 1615 | // hide rest of the copies , @TODO find a better solution. |
| 1616 | if (i >= visibleCopies) newAlpha = 0; |
| 1617 |
| 1618 | VMatrix result = mRepeaterData->mTransform.matrix(frameNo, i + offset) * |
| 1619 | parentMatrix; |
| 1620 | mContents[i]->update(frameNo, result, newAlpha, newFlag); |
| 1621 | } |
| 1622 | } |
| 1623 |
| 1624 | void LOTRepeaterItem::renderList(std::vector<VDrawable *> &list) |
| 1625 | { |
| 1626 | if (mHidden) return; |
| 1627 | return LOTContentGroupItem::renderList(list); |
| 1628 | } |
| 1629 |
| 1630 | static void updateGStops(LOTNode *n, const VGradient *grad) |
| 1631 | { |
| 1632 | if (grad->mStops.size() != n->mGradient.stopCount) { |
| 1633 | if (n->mGradient.stopCount) free(n->mGradient.stopPtr); |
| 1634 | n->mGradient.stopCount = grad->mStops.size(); |
| 1635 | n->mGradient.stopPtr = (LOTGradientStop *)malloc( |
| 1636 | n->mGradient.stopCount * sizeof(LOTGradientStop)); |
| 1637 | } |
| 1638 |
| 1639 | LOTGradientStop *ptr = n->mGradient.stopPtr; |
| 1640 | for (const auto &i : grad->mStops) { |
| 1641 | ptr->pos = i.first; |
| 1642 | ptr->a = i.second.alpha() * grad->alpha(); |
| 1643 | ptr->r = i.second.red(); |
| 1644 | ptr->g = i.second.green(); |
| 1645 | ptr->b = i.second.blue(); |
| 1646 | ptr++; |
| 1647 | } |
| 1648 | } |
| 1649 |
| 1650 | void LOTDrawable::sync() |
| 1651 | { |
| 1652 | if (!mCNode) { |
| 1653 | mCNode = std::make_unique<LOTNode>(); |
| 1654 | mCNode->mGradient.stopPtr = nullptr; |
| 1655 | mCNode->mGradient.stopCount = 0; |
| 1656 | } |
| 1657 |
| 1658 | mCNode->mFlag = ChangeFlagNone; |
| 1659 | if (mFlag & DirtyState::None) return; |
| 1660 |
| 1661 | if (mFlag & DirtyState::Path) { |
| 1662 | if (mStroke.mDash.size()) { |
| 1663 | VDasher dasher(mStroke.mDash.data(), mStroke.mDash.size()); |
| 1664 | mPath = dasher.dashed(mPath); |
| 1665 | } |
| 1666 | const std::vector<VPath::Element> &elm = mPath.elements(); |
| 1667 | const std::vector<VPointF> & pts = mPath.points(); |
| 1668 | const float *ptPtr = reinterpret_cast<const float *>(pts.data()); |
| 1669 | const char * elmPtr = reinterpret_cast<const char *>(elm.data()); |
| 1670 | mCNode->mPath.elmPtr = elmPtr; |
| 1671 | mCNode->mPath.elmCount = elm.size(); |
| 1672 | mCNode->mPath.ptPtr = ptPtr; |
| 1673 | mCNode->mPath.ptCount = 2 * pts.size(); |
| 1674 | mCNode->mFlag |= ChangeFlagPath; |
| 1675 | } |
| 1676 |
| 1677 | if (mStroke.enable) { |
| 1678 | mCNode->mStroke.width = mStroke.width; |
| 1679 | mCNode->mStroke.meterLimit = mStroke.meterLimit; |
| 1680 | mCNode->mStroke.enable = 1; |
| 1681 |
| 1682 | switch (mStroke.cap) { |
| 1683 | case CapStyle::Flat: |
| 1684 | mCNode->mStroke.cap = LOTCapStyle::CapFlat; |
| 1685 | break; |
| 1686 | case CapStyle::Square: |
| 1687 | mCNode->mStroke.cap = LOTCapStyle::CapSquare; |
| 1688 | break; |
| 1689 | case CapStyle::Round: |
| 1690 | mCNode->mStroke.cap = LOTCapStyle::CapRound; |
| 1691 | break; |
| 1692 | } |
| 1693 |
| 1694 | switch (mStroke.join) { |
| 1695 | case JoinStyle::Miter: |
| 1696 | mCNode->mStroke.join = LOTJoinStyle::JoinMiter; |
| 1697 | break; |
| 1698 | case JoinStyle::Bevel: |
| 1699 | mCNode->mStroke.join = LOTJoinStyle::JoinBevel; |
| 1700 | break; |
| 1701 | case JoinStyle::Round: |
| 1702 | mCNode->mStroke.join = LOTJoinStyle::JoinRound; |
| 1703 | break; |
| 1704 | default: |
| 1705 | mCNode->mStroke.join = LOTJoinStyle::JoinMiter; |
| 1706 | break; |
| 1707 | } |
| 1708 | } else { |
| 1709 | mCNode->mStroke.enable = 0; |
| 1710 | } |
| 1711 |
| 1712 | switch (mFillRule) { |
| 1713 | case FillRule::EvenOdd: |
| 1714 | mCNode->mFillRule = LOTFillRule::FillEvenOdd; |
| 1715 | break; |
| 1716 | default: |
| 1717 | mCNode->mFillRule = LOTFillRule::FillWinding; |
| 1718 | break; |
| 1719 | } |
| 1720 |
| 1721 | switch (mBrush.type()) { |
| 1722 | case VBrush::Type::Solid: |
| 1723 | mCNode->mBrushType = LOTBrushType::BrushSolid; |
| 1724 | mCNode->mColor.r = mBrush.mColor.r; |
| 1725 | mCNode->mColor.g = mBrush.mColor.g; |
| 1726 | mCNode->mColor.b = mBrush.mColor.b; |
| 1727 | mCNode->mColor.a = mBrush.mColor.a; |
| 1728 | break; |
| 1729 | case VBrush::Type::LinearGradient: { |
| 1730 | mCNode->mBrushType = LOTBrushType::BrushGradient; |
| 1731 | mCNode->mGradient.type = LOTGradientType::GradientLinear; |
| 1732 | VPointF s = mBrush.mGradient->mMatrix.map( |
| 1733 | {mBrush.mGradient->linear.x1, mBrush.mGradient->linear.y1}); |
| 1734 | VPointF e = mBrush.mGradient->mMatrix.map( |
| 1735 | {mBrush.mGradient->linear.x2, mBrush.mGradient->linear.y2}); |
| 1736 | mCNode->mGradient.start.x = s.x(); |
| 1737 | mCNode->mGradient.start.y = s.y(); |
| 1738 | mCNode->mGradient.end.x = e.x(); |
| 1739 | mCNode->mGradient.end.y = e.y(); |
| 1740 | updateGStops(mCNode.get(), mBrush.mGradient); |
| 1741 | break; |
| 1742 | } |
| 1743 | case VBrush::Type::RadialGradient: { |
| 1744 | mCNode->mBrushType = LOTBrushType::BrushGradient; |
| 1745 | mCNode->mGradient.type = LOTGradientType::GradientRadial; |
| 1746 | VPointF c = mBrush.mGradient->mMatrix.map( |
| 1747 | {mBrush.mGradient->radial.cx, mBrush.mGradient->radial.cy}); |
| 1748 | VPointF f = mBrush.mGradient->mMatrix.map( |
| 1749 | {mBrush.mGradient->radial.fx, mBrush.mGradient->radial.fy}); |
| 1750 | mCNode->mGradient.center.x = c.x(); |
| 1751 | mCNode->mGradient.center.y = c.y(); |
| 1752 | mCNode->mGradient.focal.x = f.x(); |
| 1753 | mCNode->mGradient.focal.y = f.y(); |
| 1754 |
| 1755 | float scale = getScale(mBrush.mGradient->mMatrix); |
| 1756 | mCNode->mGradient.cradius = mBrush.mGradient->radial.cradius * scale; |
| 1757 | mCNode->mGradient.fradius = mBrush.mGradient->radial.fradius * scale; |
| 1758 | updateGStops(mCNode.get(), mBrush.mGradient); |
| 1759 | break; |
| 1760 | } |
| 1761 | default: |
| 1762 | break; |
| 1763 | } |
| 1764 | } |
| 1765 |