| 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) |
| 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); |
| 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 | LOTLayerData *layerModel = static_cast<LOTLayerData *>(i.get()); |
| 539 | auto layerItem = LOTCompItem::createLayerItem(layerModel); |
| 540 | if (layerItem) mLayers.push_back(std::move(layerItem)); |
| 541 | } |
| 542 |
| 543 | // 2. update parent layer |
| 544 | for (const auto &layer : mLayers) { |
| 545 | int id = layer->parentId(); |
| 546 | if (id >= 0) { |
| 547 | auto search = |
| 548 | std::find_if(mLayers.begin(), mLayers.end(), |
| 549 | [id](const auto &val) { return val->id() == id; }); |
| 550 | if (search != mLayers.end() && |
| 551 | isGoodParentLayer((*search).get(), layer.get())) { |
| 552 | layer->setParentLayer((*search).get()); |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 |
| 557 | // 3. keep the layer in back-to-front order. |
| 558 | // as lottie model keeps the data in front-toback-order. |
| 559 | std::reverse(mLayers.begin(), mLayers.end()); |
| 560 |
| 561 | // 4. check if its a nested composition |
| 562 | if (!layerModel->layerSize().empty()) { |
| 563 | mClipper = std::make_unique<LOTClipperItem>(layerModel->layerSize()); |
| 564 | } |
| 565 |
| 566 | if (mLayers.size() > 1) setComplexContent(true); |
| 567 | } |
| 568 |
| 569 | void LOTCompLayerItem::buildLayerNode() |
| 570 | { |
| 571 | LOTLayerItem::buildLayerNode(); |
| 572 | if (mClipper) { |
| 573 | const std::vector<VPath::Element> &elm = mClipper->mPath.elements(); |
| 574 | const std::vector<VPointF> & pts = mClipper->mPath.points(); |
| 575 | const float *ptPtr = reinterpret_cast<const float *>(pts.data()); |
| 576 | const char * elmPtr = reinterpret_cast<const char *>(elm.data()); |
| 577 | layerNode()->mClipPath.ptPtr = ptPtr; |
| 578 | layerNode()->mClipPath.elmPtr = elmPtr; |
| 579 | layerNode()->mClipPath.ptCount = 2 * pts.size(); |
| 580 | layerNode()->mClipPath.elmCount = elm.size(); |
| 581 | } |
| 582 | if (mLayers.size() != mLayersCNode.size()) { |
| 583 | for (const auto &layer : mLayers) { |
| 584 | layer->buildLayerNode(); |
| 585 | mLayersCNode.push_back(layer->layerNode()); |
| 586 | } |
| 587 | layerNode()->mLayerList.ptr = mLayersCNode.data(); |
| 588 | layerNode()->mLayerList.size = mLayersCNode.size(); |
| 589 | } else { |
| 590 | for (const auto &layer : mLayers) { |
| 591 | layer->buildLayerNode(); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 |
| 596 | void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask, |
| 597 | const VRle &matteRle) |
| 598 | { |
| 599 | if (vIsZero(combinedAlpha())) return; |
| 600 |
| 601 | if (vCompare(combinedAlpha(), 1.0)) { |
| 602 | renderHelper(painter, inheritMask, matteRle); |
| 603 | } else { |
| 604 | if (complexContent()) { |
| 605 | VSize size = painter->clipBoundingRect().size(); |
| 606 | VPainter srcPainter; |
| 607 | VBitmap srcBitmap(size.width(), size.height(), |
| 608 | VBitmap::Format::ARGB32); |
| 609 | srcPainter.begin(&srcBitmap); |
| 610 | renderHelper(&srcPainter, inheritMask, matteRle); |
| 611 | srcPainter.end(); |
| 612 | painter->drawBitmap(VPoint(), srcBitmap, combinedAlpha() * 255); |
| 613 | } else { |
| 614 | renderHelper(painter, inheritMask, matteRle); |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 |
| 619 | void LOTCompLayerItem::renderHelper(VPainter *painter, const VRle &inheritMask, |
| 620 | const VRle &matteRle) |
| 621 | { |
| 622 | VRle mask; |
| 623 | if (mLayerMask) { |
| 624 | mask = mLayerMask->maskRle(painter->clipBoundingRect()); |
| 625 | if (!inheritMask.empty()) mask = mask & inheritMask; |
| 626 | // if resulting mask is empty then return. |
| 627 | if (mask.empty()) return; |
| 628 | } else { |
| 629 | mask = inheritMask; |
| 630 | } |
| 631 |
| 632 | if (mClipper) { |
| 633 | if (mask.empty()) { |
| 634 | mask = mClipper->rle(); |
| 635 | } else { |
| 636 | mask = mClipper->rle() & mask; |
| 637 | } |
| 638 | } |
| 639 |
| 640 | LOTLayerItem *matte = nullptr; |
| 641 | for (const auto &layer : mLayers) { |
| 642 | if (layer->hasMatte()) { |
| 643 | matte = layer.get(); |
| 644 | } else { |
| 645 | if (layer->visible()) { |
| 646 | if (matte) { |
| 647 | if (matte->visible()) |
| 648 | renderMatteLayer(painter, mask, matteRle, matte, |
| 649 | layer.get()); |
| 650 | } else { |
| 651 | layer->render(painter, mask, matteRle); |
| 652 | } |
| 653 | } |
| 654 | matte = nullptr; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 |
| 659 | void LOTCompLayerItem::renderMatteLayer(VPainter *painter, const VRle &mask, |
| 660 | const VRle & matteRle, |
| 661 | LOTLayerItem *layer, LOTLayerItem *src) |
| 662 | { |
| 663 | VSize size = painter->clipBoundingRect().size(); |
| 664 | // Decide if we can use fast matte. |
| 665 | // 1. draw src layer to matte buffer |
| 666 | VPainter srcPainter; |
| 667 | src->bitmap().reset(size.width(), size.height(), |
| 668 | VBitmap::Format::ARGB32); |
| 669 | srcPainter.begin(&src->bitmap()); |
| 670 | src->render(&srcPainter, mask, matteRle); |
| 671 | srcPainter.end(); |
| 672 |
| 673 | // 2. draw layer to layer buffer |
| 674 | VPainter layerPainter; |
| 675 | layer->bitmap().reset(size.width(), size.height(), |
| 676 | VBitmap::Format::ARGB32); |
| 677 | layerPainter.begin(&layer->bitmap()); |
| 678 | layer->render(&layerPainter, mask, matteRle); |
| 679 |
| 680 | // 2.1update composition mode |
| 681 | switch (layer->matteType()) { |
| 682 | case MatteType::Alpha: |
| 683 | case MatteType::Luma: { |
| 684 | layerPainter.setCompositionMode( |
| 685 | VPainter::CompositionMode::CompModeDestIn); |
| 686 | break; |
| 687 | } |
| 688 | case MatteType::AlphaInv: |
| 689 | case MatteType::LumaInv: { |
| 690 | layerPainter.setCompositionMode( |
| 691 | VPainter::CompositionMode::CompModeDestOut); |
| 692 | break; |
| 693 | } |
| 694 | default: |
| 695 | break; |
| 696 | } |
| 697 |
| 698 | // 2.2 update srcBuffer if the matte is luma type |
| 699 | if (layer->matteType() == MatteType::Luma || |
| 700 | layer->matteType() == MatteType::LumaInv) { |
| 701 | src->bitmap().updateLuma(); |
| 702 | } |
| 703 |
| 704 | // 2.3 draw src buffer as mask |
| 705 | layerPainter.drawBitmap(VPoint(), src->bitmap()); |
| 706 | layerPainter.end(); |
| 707 | // 3. draw the result buffer into painter |
| 708 | painter->drawBitmap(VPoint(), layer->bitmap()); |
| 709 | } |
| 710 |
| 711 | void LOTClipperItem::update(const VMatrix &matrix) |
| 712 | { |
| 713 | mPath.reset(); |
| 714 | mPath.addRect(VRectF(0, 0, mSize.width(), mSize.height())); |
| 715 | mPath.transform(matrix); |
| 716 | mRasterizer.rasterize(mPath); |
| 717 | } |
| 718 |
| 719 | VRle LOTClipperItem::rle() |
| 720 | { |
| 721 | return mRasterizer.rle(); |
| 722 | } |
| 723 |
| 724 | void LOTCompLayerItem::updateContent() |
| 725 | { |
| 726 | if (mClipper && flag().testFlag(DirtyFlagBit::Matrix)) { |
| 727 | mClipper->update(combinedMatrix()); |
| 728 | } |
| 729 | int mappedFrame = mLayerData->timeRemap(frameNo()); |
| 730 | float alpha = combinedAlpha(); |
| 731 | if (complexContent()) alpha = 1; |
| 732 | for (const auto &layer : mLayers) { |
| 733 | layer->update(mappedFrame, combinedMatrix(), alpha); |
| 734 | } |
| 735 | } |
| 736 |
| 737 | void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list) |
| 738 | { |
| 739 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 740 |
| 741 | LOTLayerItem *matte = nullptr; |
| 742 | for (const auto &layer : mLayers) { |
| 743 | if (layer->hasMatte()) { |
| 744 | matte = layer.get(); |
| 745 | } else { |
| 746 | if (layer->visible()) { |
| 747 | if (matte) { |
| 748 | if (matte->visible()) { |
| 749 | layer->renderList(list); |
| 750 | matte->renderList(list); |
| 751 | } |
| 752 | } else { |
| 753 | layer->renderList(list); |
| 754 | } |
| 755 | } |
| 756 | matte = nullptr; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 |
| 761 | LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData) |
| 762 | : LOTLayerItem(layerData) |
| 763 | { |
| 764 | } |
| 765 |
| 766 | void LOTSolidLayerItem::updateContent() |
| 767 | { |
| 768 | if (flag() & DirtyFlagBit::Matrix) { |
| 769 | VPath path; |
| 770 | path.addRect( |
| 771 | VRectF(0, 0, |
| 772 | mLayerData->layerSize().width(), |
| 773 | mLayerData->layerSize().height())); |
| 774 | path.transform(combinedMatrix()); |
| 775 | mRenderNode.mFlag |= VDrawable::DirtyState::Path; |
| 776 | mRenderNode.mPath = path; |
| 777 | } |
| 778 | if (flag() & DirtyFlagBit::Alpha) { |
| 779 | LottieColor color = mLayerData->solidColor(); |
| 780 | VBrush brush(color.toColor(combinedAlpha())); |
| 781 | mRenderNode.setBrush(brush); |
| 782 | mRenderNode.mFlag |= VDrawable::DirtyState::Brush; |
| 783 | } |
| 784 | } |
| 785 |
| 786 | void LOTSolidLayerItem::buildLayerNode() |
| 787 | { |
| 788 | LOTLayerItem::buildLayerNode(); |
| 789 |
| 790 | mDrawableList.clear(); |
| 791 | renderList(mDrawableList); |
| 792 |
| 793 | mCNodeList.clear(); |
| 794 | for (auto &i : mDrawableList) { |
| 795 | LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i); |
| 796 | lotDrawable->sync(); |
| 797 | mCNodeList.push_back(lotDrawable->mCNode.get()); |
| 798 | } |
| 799 | layerNode()->mNodeList.ptr = mCNodeList.data(); |
| 800 | layerNode()->mNodeList.size = mCNodeList.size(); |
| 801 | } |
| 802 |
| 803 | void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list) |
| 804 | { |
| 805 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 806 |
| 807 | list.push_back(&mRenderNode); |
| 808 | } |
| 809 |
| 810 | LOTImageLayerItem::LOTImageLayerItem(LOTLayerData *layerData) |
| 811 | : LOTLayerItem(layerData) |
| 812 | { |
| 813 | if (!mLayerData->asset()) return; |
| 814 |
| 815 | VBrush brush(mLayerData->asset()->bitmap()); |
| 816 | mRenderNode.setBrush(brush); |
| 817 | } |
| 818 |
| 819 | void LOTImageLayerItem::updateContent() |
| 820 | { |
| 821 | if (!mLayerData->asset()) return; |
| 822 |
| 823 | if (flag() & DirtyFlagBit::Matrix) { |
| 824 | VPath path; |
| 825 | path.addRect(VRectF(0, 0, mLayerData->asset()->mWidth, |
| 826 | mLayerData->asset()->mHeight)); |
| 827 | path.transform(combinedMatrix()); |
| 828 | mRenderNode.mFlag |= VDrawable::DirtyState::Path; |
| 829 | mRenderNode.mPath = path; |
| 830 | mRenderNode.mBrush.setMatrix(combinedMatrix()); |
| 831 | } |
| 832 |
| 833 | if (flag() & DirtyFlagBit::Alpha) { |
| 834 | //@TODO handle alpha with the image. |
| 835 | } |
| 836 | } |
| 837 |
| 838 | void LOTImageLayerItem::renderList(std::vector<VDrawable *> &list) |
| 839 | { |
| 840 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 841 |
| 842 | list.push_back(&mRenderNode); |
| 843 | } |
| 844 |
| 845 | void LOTImageLayerItem::buildLayerNode() |
| 846 | { |
| 847 | LOTLayerItem::buildLayerNode(); |
| 848 |
| 849 | mDrawableList.clear(); |
| 850 | renderList(mDrawableList); |
| 851 |
| 852 | mCNodeList.clear(); |
| 853 | for (auto &i : mDrawableList) { |
| 854 | LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i); |
| 855 | lotDrawable->sync(); |
| 856 |
| 857 | lotDrawable->mCNode->mImageInfo.data = |
| 858 | lotDrawable->mBrush.mTexture.data(); |
| 859 | lotDrawable->mCNode->mImageInfo.width = |
| 860 | lotDrawable->mBrush.mTexture.width(); |
| 861 | lotDrawable->mCNode->mImageInfo.height = |
| 862 | lotDrawable->mBrush.mTexture.height(); |
| 863 |
| 864 | lotDrawable->mCNode->mImageInfo.mMatrix.m11 = combinedMatrix().m_11(); |
| 865 | lotDrawable->mCNode->mImageInfo.mMatrix.m12 = combinedMatrix().m_12(); |
| 866 | lotDrawable->mCNode->mImageInfo.mMatrix.m13 = combinedMatrix().m_13(); |
| 867 |
| 868 | lotDrawable->mCNode->mImageInfo.mMatrix.m21 = combinedMatrix().m_21(); |
| 869 | lotDrawable->mCNode->mImageInfo.mMatrix.m22 = combinedMatrix().m_22(); |
| 870 | lotDrawable->mCNode->mImageInfo.mMatrix.m23 = combinedMatrix().m_23(); |
| 871 |
| 872 | lotDrawable->mCNode->mImageInfo.mMatrix.m31 = combinedMatrix().m_tx(); |
| 873 | lotDrawable->mCNode->mImageInfo.mMatrix.m32 = combinedMatrix().m_ty(); |
| 874 | lotDrawable->mCNode->mImageInfo.mMatrix.m33 = combinedMatrix().m_33(); |
| 875 |
| 876 | mCNodeList.push_back(lotDrawable->mCNode.get()); |
| 877 | } |
| 878 | layerNode()->mNodeList.ptr = mCNodeList.data(); |
| 879 | layerNode()->mNodeList.size = mCNodeList.size(); |
| 880 | } |
| 881 |
| 882 | LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData) |
| 883 | : LOTLayerItem(layerData) |
| 884 | { |
| 885 | } |
| 886 | void LOTNullLayerItem::updateContent() {} |
| 887 |
| 888 | LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData) |
| 889 | : LOTLayerItem(layerData), |
| 890 | mRoot(std::make_unique<LOTContentGroupItem>(nullptr)) |
| 891 | { |
| 892 | mRoot->addChildren(layerData); |
| 893 |
| 894 | std::vector<LOTPathDataItem *> list; |
| 895 | mRoot->processPaintItems(list); |
| 896 |
| 897 | if (layerData->hasPathOperator()) { |
| 898 | list.clear(); |
| 899 | mRoot->processTrimItems(list); |
| 900 | } |
| 901 | } |
| 902 |
| 903 | std::unique_ptr<LOTContentItem> LOTShapeLayerItem::createContentItem( |
| 904 | LOTData *contentData) |
| 905 | { |
| 906 | switch (contentData->type()) { |
| 907 | case LOTData::Type::ShapeGroup: { |
| 908 | return std::make_unique<LOTContentGroupItem>( |
| 909 | static_cast<LOTGroupData *>(contentData)); |
| 910 | } |
| 911 | case LOTData::Type::Rect: { |
| 912 | return std::make_unique<LOTRectItem>( |
| 913 | static_cast<LOTRectData *>(contentData)); |
| 914 | } |
| 915 | case LOTData::Type::Ellipse: { |
| 916 | return std::make_unique<LOTEllipseItem>( |
| 917 | static_cast<LOTEllipseData *>(contentData)); |
| 918 | } |
| 919 | case LOTData::Type::Shape: { |
| 920 | return std::make_unique<LOTShapeItem>( |
| 921 | static_cast<LOTShapeData *>(contentData)); |
| 922 | } |
| 923 | case LOTData::Type::Polystar: { |
| 924 | return std::make_unique<LOTPolystarItem>( |
| 925 | static_cast<LOTPolystarData *>(contentData)); |
| 926 | } |
| 927 | case LOTData::Type::Fill: { |
| 928 | return std::make_unique<LOTFillItem>( |
| 929 | static_cast<LOTFillData *>(contentData)); |
| 930 | } |
| 931 | case LOTData::Type::GFill: { |
| 932 | return std::make_unique<LOTGFillItem>( |
| 933 | static_cast<LOTGFillData *>(contentData)); |
| 934 | } |
| 935 | case LOTData::Type::Stroke: { |
| 936 | return std::make_unique<LOTStrokeItem>( |
| 937 | static_cast<LOTStrokeData *>(contentData)); |
| 938 | } |
| 939 | case LOTData::Type::GStroke: { |
| 940 | return std::make_unique<LOTGStrokeItem>( |
| 941 | static_cast<LOTGStrokeData *>(contentData)); |
| 942 | } |
| 943 | case LOTData::Type::Repeater: { |
| 944 | return std::make_unique<LOTRepeaterItem>( |
| 945 | static_cast<LOTRepeaterData *>(contentData)); |
| 946 | } |
| 947 | case LOTData::Type::Trim: { |
| 948 | return std::make_unique<LOTTrimItem>( |
| 949 | static_cast<LOTTrimData *>(contentData)); |
| 950 | } |
| 951 | default: |
| 952 | return nullptr; |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 |
| 957 | void LOTShapeLayerItem::updateContent() |
| 958 | { |
| 959 | mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag()); |
| 960 |
| 961 | if (mLayerData->hasPathOperator()) { |
| 962 | mRoot->applyTrim(); |
| 963 | } |
| 964 | } |
| 965 |
| 966 | void LOTShapeLayerItem::buildLayerNode() |
| 967 | { |
| 968 | LOTLayerItem::buildLayerNode(); |
| 969 |
| 970 | mDrawableList.clear(); |
| 971 | renderList(mDrawableList); |
| 972 |
| 973 | mCNodeList.clear(); |
| 974 | for (auto &i : mDrawableList) { |
| 975 | LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i); |
| 976 | lotDrawable->sync(); |
| 977 | mCNodeList.push_back(lotDrawable->mCNode.get()); |
| 978 | } |
| 979 | layerNode()->mNodeList.ptr = mCNodeList.data(); |
| 980 | layerNode()->mNodeList.size = mCNodeList.size(); |
| 981 | } |
| 982 |
| 983 | void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list) |
| 984 | { |
| 985 | if (!visible() || vIsZero(combinedAlpha())) return; |
| 986 | mRoot->renderList(list); |
| 987 | } |
| 988 |
| 989 | bool LOTContentGroupItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 990 | LOTVariant &value) |
| 991 | { |
| 992 | if (!keyPath.matches(name(), depth)) { |
| 993 | return false; |
| 994 | } |
| 995 |
| 996 | if (!keyPath.skip(name())) { |
| 997 | if (keyPath.fullyResolvesTo(name(), depth) && |
| 998 | transformProp(value.property())) { |
| 999 | //@TODO handle property update |
| 1000 | } |
| 1001 | } |
| 1002 |
| 1003 | if (keyPath.propagate(name(), depth)) { |
| 1004 | uint newDepth = keyPath.nextDepth(name(), depth); |
| 1005 | for (auto &child : mContents) { |
| 1006 | child->resolveKeyPath(keyPath, newDepth, value); |
| 1007 | } |
| 1008 | } |
| 1009 | return true; |
| 1010 | } |
| 1011 |
| 1012 | bool LOTFillItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 1013 | LOTVariant &value) |
| 1014 | { |
| 1015 | if (!keyPath.matches(mModel.name(), depth)) { |
| 1016 | return false; |
| 1017 | } |
| 1018 |
| 1019 | if (keyPath.fullyResolvesTo(mModel.name(), depth) && |
| 1020 | fillProp(value.property())) { |
| 1021 | mModel.filter().addValue(value); |
| 1022 | return true; |
| 1023 | } |
| 1024 | return false; |
| 1025 | } |
| 1026 |
| 1027 | bool LOTStrokeItem::resolveKeyPath(LOTKeyPath &keyPath, uint depth, |
| 1028 | LOTVariant &value) |
| 1029 | { |
| 1030 | if (!keyPath.matches(mModel.name(), depth)) { |
| 1031 | return false; |
| 1032 | } |
| 1033 |
| 1034 | if (keyPath.fullyResolvesTo(mModel.name(), depth) && |
| 1035 | strokeProp(value.property())) { |
| 1036 | mModel.filter().addValue(value); |
| 1037 | return true; |
| 1038 | } |
| 1039 | return false; |
| 1040 | } |
| 1041 |
| 1042 | LOTContentGroupItem::LOTContentGroupItem(LOTGroupData *data) |
| 1043 | : LOTContentItem(ContentType::Group), mData(data) |
| 1044 | { |
| 1045 | addChildren(mData); |
| 1046 | } |
| 1047 |
| 1048 | void LOTContentGroupItem::addChildren(LOTGroupData *data) |
| 1049 | { |
| 1050 | if (!data) return; |
| 1051 |
| 1052 | for (auto &i : data->mChildren) { |
| 1053 | auto content = LOTShapeLayerItem::createContentItem(i.get()); |
| 1054 | if (content) { |
| 1055 | content->setParent(this); |
| 1056 | mContents.push_back(std::move(content)); |
| 1057 | } |
| 1058 | } |
| 1059 |
| 1060 | // keep the content in back-to-front order. |
| 1061 | std::reverse(mContents.begin(), mContents.end()); |
| 1062 | } |
| 1063 |
| 1064 | void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix, |
| 1065 | float parentAlpha, const DirtyFlag &flag) |
| 1066 | { |
| 1067 | VMatrix m = parentMatrix; |
| 1068 | float alpha = parentAlpha; |
| 1069 | DirtyFlag newFlag = flag; |
| 1070 |
| 1071 | if (mData && mData->mTransform) { |
| 1072 | // update the matrix and the flag |
| 1073 | if ((flag & DirtyFlagBit::Matrix) || !mData->mTransform->isStatic()) { |
| 1074 | newFlag |= DirtyFlagBit::Matrix; |
| 1075 | } |
| 1076 | m = mData->mTransform->matrix(frameNo); |
| 1077 | m *= parentMatrix; |
| 1078 | alpha *= mData->mTransform->opacity(frameNo); |
| 1079 |
| 1080 | if (!vCompare(alpha, parentAlpha)) { |
| 1081 | newFlag |= DirtyFlagBit::Alpha; |
| 1082 | } |
| 1083 | } |
| 1084 |
| 1085 | mMatrix = m; |
| 1086 |
| 1087 | for (const auto &content : mContents) { |
| 1088 | content->update(frameNo, m, alpha, newFlag); |
| 1089 | } |
| 1090 | } |
| 1091 |
| 1092 | void LOTContentGroupItem::applyTrim() |
| 1093 | { |
| 1094 | for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) { |
| 1095 | auto content = (*i).get(); |
| 1096 | switch (content->type()) { |
| 1097 | case ContentType::Trim: { |
| 1098 | static_cast<LOTTrimItem *>(content)->update(); |
| 1099 | break; |
| 1100 | } |
| 1101 | case ContentType::Group: { |
| 1102 | static_cast<LOTContentGroupItem *>(content)->applyTrim(); |
| 1103 | break; |
| 1104 | } |
| 1105 | default: |
| 1106 | break; |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 |
| 1111 | void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list) |
| 1112 | { |
| 1113 | for (const auto &content : mContents) { |
| 1114 | content->renderList(list); |
| 1115 | } |
| 1116 | } |
| 1117 |
| 1118 | void LOTContentGroupItem::processPaintItems( |
| 1119 | std::vector<LOTPathDataItem *> &list) |
| 1120 | { |
| 1121 | int curOpCount = list.size(); |
| 1122 | for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) { |
| 1123 | auto content = (*i).get(); |
| 1124 | switch (content->type()) { |
| 1125 | case ContentType::Path: { |
| 1126 | list.push_back(static_cast<LOTPathDataItem *>(content)); |
| 1127 | break; |
| 1128 | } |
| 1129 | case ContentType::Paint: { |
| 1130 | static_cast<LOTPaintDataItem *>(content)->addPathItems(list, |
| 1131 | curOpCount); |
| 1132 | break; |
| 1133 | } |
| 1134 | case ContentType::Group: { |
| 1135 | static_cast<LOTContentGroupItem *>(content)->processPaintItems( |
| 1136 | list); |
| 1137 | break; |
| 1138 | } |
| 1139 | default: |
| 1140 | break; |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 |
| 1145 | void LOTContentGroupItem::processTrimItems(std::vector<LOTPathDataItem *> &list) |
| 1146 | { |
| 1147 | int curOpCount = list.size(); |
| 1148 | for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) { |
| 1149 | auto content = (*i).get(); |
| 1150 |
| 1151 | switch (content->type()) { |
| 1152 | case ContentType::Path: { |
| 1153 | list.push_back(static_cast<LOTPathDataItem *>(content)); |
| 1154 | break; |
| 1155 | } |
| 1156 | case ContentType::Trim: { |
| 1157 | static_cast<LOTTrimItem *>(content)->addPathItems(list, curOpCount); |
| 1158 | break; |
| 1159 | } |
| 1160 | case ContentType::Group: { |
| 1161 | static_cast<LOTContentGroupItem *>(content)->processTrimItems(list); |
| 1162 | break; |
| 1163 | } |
| 1164 | default: |
| 1165 | break; |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 |
| 1170 | /* |
| 1171 | * LOTPathDataItem uses 3 path objects for path object reuse. |
| 1172 | * mLocalPath - keeps track of the local path of the item before |
| 1173 | * applying path operation and transformation. |
| 1174 | * mTemp - keeps a referece to the mLocalPath and can be updated by the |
| 1175 | * path operation objects(trim, merge path), |
| 1176 | * mFinalPath - it takes a deep copy of the intermediate path(mTemp) each time |
| 1177 | * when the path is dirty(so if path changes every frame we don't realloc just |
| 1178 | * copy to the final path). NOTE: As path objects are COW objects we have to be |
| 1179 | * carefull about the refcount so that we don't generate deep copy while |
| 1180 | * modifying the path objects. |
| 1181 | */ |
| 1182 | void LOTPathDataItem::update(int frameNo, const VMatrix &, float, |
| 1183 | const DirtyFlag &flag) |
| 1184 | { |
| 1185 | mPathChanged = false; |
| 1186 |
| 1187 | // 1. update the local path if needed |
| 1188 | if (hasChanged(frameNo)) { |
| 1189 | // loose the reference to mLocalPath if any |
| 1190 | // from the last frame update. |
| 1191 | mTemp = VPath(); |
| 1192 |
| 1193 | updatePath(mLocalPath, frameNo); |
| 1194 | mPathChanged = true; |
| 1195 | mNeedUpdate = true; |
| 1196 | } |
| 1197 | // 2. keep a reference path in temp in case there is some |
| 1198 | // path operation like trim which will update the path. |
| 1199 | // we don't want to update the local path. |
| 1200 | mTemp = mLocalPath; |
| 1201 |
| 1202 | // 3. compute the final path with parentMatrix |
| 1203 | if ((flag & DirtyFlagBit::Matrix) || mPathChanged) { |
| 1204 | mPathChanged = true; |
| 1205 | } |
| 1206 | } |
| 1207 |
| 1208 | const VPath &LOTPathDataItem::finalPath() |
| 1209 | { |
| 1210 | if (mPathChanged || mNeedUpdate) { |
| 1211 | mFinalPath.clone(mTemp); |
| 1212 | mFinalPath.transform( |
| 1213 | static_cast<LOTContentGroupItem *>(parent())->matrix()); |
| 1214 | mNeedUpdate = false; |
| 1215 | } |
| 1216 | return mFinalPath; |
| 1217 | } |
| 1218 | LOTRectItem::LOTRectItem(LOTRectData *data) |
| 1219 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1220 | { |
| 1221 | } |
| 1222 |
| 1223 | void LOTRectItem::updatePath(VPath &path, int frameNo) |
| 1224 | { |
| 1225 | VPointF pos = mData->mPos.value(frameNo); |
| 1226 | VPointF size = mData->mSize.value(frameNo); |
| 1227 | float roundness = mData->mRound.value(frameNo); |
| 1228 | VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(), |
| 1229 | size.y()); |
| 1230 |
| 1231 | path.reset(); |
| 1232 | path.addRoundRect(r, roundness, mData->direction()); |
| 1233 | } |
| 1234 |
| 1235 | LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data) |
| 1236 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1237 | { |
| 1238 | } |
| 1239 |
| 1240 | void LOTEllipseItem::updatePath(VPath &path, int frameNo) |
| 1241 | { |
| 1242 | VPointF pos = mData->mPos.value(frameNo); |
| 1243 | VPointF size = mData->mSize.value(frameNo); |
| 1244 | VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(), |
| 1245 | size.y()); |
| 1246 |
| 1247 | path.reset(); |
| 1248 | path.addOval(r, mData->direction()); |
| 1249 | } |
| 1250 |
| 1251 | LOTShapeItem::LOTShapeItem(LOTShapeData *data) |
| 1252 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1253 | { |
| 1254 | } |
| 1255 |
| 1256 | void LOTShapeItem::updatePath(VPath &path, int frameNo) |
| 1257 | { |
| 1258 | mData->mShape.value(frameNo).toPath(path); |
| 1259 | } |
| 1260 |
| 1261 | LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data) |
| 1262 | : LOTPathDataItem(data->isStatic()), mData(data) |
| 1263 | { |
| 1264 | } |
| 1265 |
| 1266 | void LOTPolystarItem::updatePath(VPath &path, int frameNo) |
| 1267 | { |
| 1268 | VPointF pos = mData->mPos.value(frameNo); |
| 1269 | float points = mData->mPointCount.value(frameNo); |
| 1270 | float innerRadius = mData->mInnerRadius.value(frameNo); |
| 1271 | float outerRadius = mData->mOuterRadius.value(frameNo); |
| 1272 | float innerRoundness = mData->mInnerRoundness.value(frameNo); |
| 1273 | float outerRoundness = mData->mOuterRoundness.value(frameNo); |
| 1274 | float rotation = mData->mRotation.value(frameNo); |
| 1275 |
| 1276 | path.reset(); |
| 1277 | VMatrix m; |
| 1278 |
| 1279 | if (mData->mType == LOTPolystarData::PolyType::Star) { |
| 1280 | path.addPolystar(points, innerRadius, outerRadius, innerRoundness, |
| 1281 | outerRoundness, 0.0, 0.0, 0.0, mData->direction()); |
| 1282 | } else { |
| 1283 | path.addPolygon(points, outerRadius, outerRoundness, 0.0, 0.0, 0.0, |
| 1284 | mData->direction()); |
| 1285 | } |
| 1286 |
| 1287 | m.translate(pos.x(), pos.y()).rotate(rotation); |
| 1288 | m.rotate(rotation); |
| 1289 | path.transform(m); |
| 1290 | } |
| 1291 |
| 1292 | /* |
| 1293 | * PaintData Node handling |
| 1294 | * |
| 1295 | */ |
| 1296 | LOTPaintDataItem::LOTPaintDataItem(bool staticContent) |
| 1297 | : LOTContentItem(ContentType::Paint), mStaticContent(staticContent) |
| 1298 | { |
| 1299 | } |
| 1300 |
| 1301 | void LOTPaintDataItem::update(int frameNo, const VMatrix & /*parentMatrix*/, |
| 1302 | float parentAlpha, const DirtyFlag &flag) |
| 1303 | { |
| 1304 | mRenderNodeUpdate = true; |
| 1305 | mParentAlpha = parentAlpha; |
| 1306 | mFlag = flag; |
| 1307 | mFrameNo = frameNo; |
| 1308 |
| 1309 | updateContent(frameNo); |
| 1310 | } |
| 1311 |
| 1312 | void LOTPaintDataItem::updateRenderNode() |
| 1313 | { |
| 1314 | bool dirty = false; |
| 1315 | for (auto &i : mPathItems) { |
| 1316 | if (i->dirty()) { |
| 1317 | dirty = true; |
| 1318 | break; |
| 1319 | } |
| 1320 | } |
| 1321 |
| 1322 | if (dirty) { |
| 1323 | mPath.reset(); |
| 1324 |
| 1325 | for (auto &i : mPathItems) { |
| 1326 | mPath.addPath(i->finalPath()); |
| 1327 | } |
| 1328 | mDrawable.setPath(mPath); |
| 1329 | } else { |
| 1330 | if (mDrawable.mFlag & VDrawable::DirtyState::Path) |
| 1331 | mDrawable.mPath = mPath; |
| 1332 | } |
| 1333 | } |
| 1334 |
| 1335 | void LOTPaintDataItem::renderList(std::vector<VDrawable *> &list) |
| 1336 | { |
| 1337 | if (mRenderNodeUpdate) { |
| 1338 | updateRenderNode(); |
| 1339 | LOTPaintDataItem::updateRenderNode(); |
| 1340 | mRenderNodeUpdate = false; |
| 1341 | } |
| 1342 | list.push_back(&mDrawable); |
| 1343 | } |
| 1344 |
| 1345 | void LOTPaintDataItem::addPathItems(std::vector<LOTPathDataItem *> &list, |
| 1346 | int startOffset) |
| 1347 | { |
| 1348 | std::copy(list.begin() + startOffset, list.end(), |
| 1349 | back_inserter(mPathItems)); |
| 1350 | } |
| 1351 |
| 1352 | LOTFillItem::LOTFillItem(LOTFillData *data) |
| 1353 | : LOTPaintDataItem(data->isStatic()), mModel(data) |
| 1354 | { |
| 1355 | } |
| 1356 |
| 1357 | void LOTFillItem::updateContent(int frameNo) |
| 1358 | { |
| 1359 | mColor = mModel.color(frameNo).toColor(mModel.opacity(frameNo)); |
| 1360 | } |
| 1361 |
| 1362 | void LOTFillItem::updateRenderNode() |
| 1363 | { |
| 1364 | VColor color = mColor; |
| 1365 |
| 1366 | color.setAlpha(color.a * parentAlpha()); |
| 1367 | VBrush brush(color); |
| 1368 | mDrawable.setBrush(brush); |
| 1369 | mDrawable.setFillRule(mModel.fillRule()); |
| 1370 | } |
| 1371 |
| 1372 | LOTGFillItem::LOTGFillItem(LOTGFillData *data) |
| 1373 | : LOTPaintDataItem(data->isStatic()), mData(data) |
| 1374 | { |
| 1375 | } |
| 1376 |
| 1377 | void LOTGFillItem::updateContent(int frameNo) |
| 1378 | { |
| 1379 | mAlpha = mData->opacity(frameNo); |
| 1380 | mData->update(mGradient, frameNo); |
| 1381 | mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix(); |
| 1382 | mFillRule = mData->fillRule(); |
| 1383 | } |
| 1384 |
| 1385 | void LOTGFillItem::updateRenderNode() |
| 1386 | { |
| 1387 | mGradient->setAlpha(mAlpha * parentAlpha()); |
| 1388 | mDrawable.setBrush(VBrush(mGradient.get())); |
| 1389 | mDrawable.setFillRule(mFillRule); |
| 1390 | } |
| 1391 |
| 1392 | LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data) |
| 1393 | : LOTPaintDataItem(data->isStatic()), mModel(data) |
| 1394 | { |
| 1395 | mDashArraySize = 0; |
| 1396 | } |
| 1397 |
| 1398 | void LOTStrokeItem::updateContent(int frameNo) |
| 1399 | { |
| 1400 | mColor = mModel.color(frameNo).toColor(mModel.opacity(frameNo)); |
| 1401 | mWidth = mModel.strokeWidth(frameNo); |
| 1402 | if (mModel.hasDashInfo()) { |
| 1403 | mDashArraySize = mModel.getDashInfo(frameNo, mDashArray); |
| 1404 | } |
| 1405 | } |
| 1406 |
| 1407 | static float getScale(const VMatrix &matrix) |
| 1408 | { |
| 1409 | constexpr float SQRT_2 = 1.41421; |
| 1410 | VPointF p1(0, 0); |
| 1411 | VPointF p2(SQRT_2, SQRT_2); |
| 1412 | p1 = matrix.map(p1); |
| 1413 | p2 = matrix.map(p2); |
| 1414 | VPointF final = p2 - p1; |
| 1415 |
| 1416 | return std::sqrt(final.x() * final.x() + final.y() * final.y()) / 2.0; |
| 1417 | } |
| 1418 |
| 1419 | void LOTStrokeItem::updateRenderNode() |
| 1420 | { |
| 1421 | VColor color = mColor; |
| 1422 |
| 1423 | color.setAlpha(color.a * parentAlpha()); |
| 1424 | VBrush brush(color); |
| 1425 | mDrawable.setBrush(brush); |
| 1426 | float scale = |
| 1427 | getScale(static_cast<LOTContentGroupItem *>(parent())->matrix()); |
| 1428 | mDrawable.setStrokeInfo(mModel.capStyle(), mModel.joinStyle(), |
| 1429 | mModel.meterLimit(), mWidth * scale); |
| 1430 | if (mDashArraySize) { |
| 1431 | for (int i = 0; i < mDashArraySize; i++) mDashArray[i] *= scale; |
| 1432 |
| 1433 | /* AE draw the dash even if dash value is 0 */ |
| 1434 | if (vCompare(mDashArray[0], 0.0f)) mDashArray[0] = 0.1; |
| 1435 |
| 1436 | mDrawable.setDashInfo(mDashArray, mDashArraySize); |
| 1437 | } |
| 1438 | } |
| 1439 |
| 1440 | LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data) |
| 1441 | : LOTPaintDataItem(data->isStatic()), mData(data) |
| 1442 | { |
| 1443 | mDashArraySize = 0; |
| 1444 | } |
| 1445 |
| 1446 | void LOTGStrokeItem::updateContent(int frameNo) |
| 1447 | { |
| 1448 | mAlpha = mData->opacity(frameNo); |
| 1449 | mData->update(mGradient, frameNo); |
| 1450 | mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix(); |
| 1451 | mCap = mData->capStyle(); |
| 1452 | mJoin = mData->joinStyle(); |
| 1453 | mMiterLimit = mData->meterLimit(); |
| 1454 | mWidth = mData->width(frameNo); |
| 1455 | if (mData->hasDashInfo()) { |
| 1456 | mDashArraySize = mData->getDashInfo(frameNo, mDashArray); |
| 1457 | } |
| 1458 | } |
| 1459 |
| 1460 | void LOTGStrokeItem::updateRenderNode() |
| 1461 | { |
| 1462 | float scale = getScale(mGradient->mMatrix); |
| 1463 | mGradient->setAlpha(mAlpha * parentAlpha()); |
| 1464 | mDrawable.setBrush(VBrush(mGradient.get())); |
| 1465 | mDrawable.setStrokeInfo(mCap, mJoin, mMiterLimit, mWidth * scale); |
| 1466 | if (mDashArraySize) { |
| 1467 | for (int i = 0; i < mDashArraySize; i++) mDashArray[i] *= scale; |
| 1468 | mDrawable.setDashInfo(mDashArray, mDashArraySize); |
| 1469 | } |
| 1470 | } |
| 1471 |
| 1472 | LOTTrimItem::LOTTrimItem(LOTTrimData *data) |
| 1473 | : LOTContentItem(ContentType::Trim), mData(data) |
| 1474 | { |
| 1475 | } |
| 1476 |
| 1477 | void LOTTrimItem::update(int frameNo, const VMatrix & /*parentMatrix*/, |
| 1478 | float /*parentAlpha*/, const DirtyFlag & /*flag*/) |
| 1479 | { |
| 1480 | mDirty = false; |
| 1481 |
| 1482 | if (mCache.mFrameNo == frameNo) return; |
| 1483 |
| 1484 | LOTTrimData::Segment segment = mData->segment(frameNo); |
| 1485 |
| 1486 | if (!(vCompare(mCache.mSegment.start, segment.start) && |
| 1487 | vCompare(mCache.mSegment.end, segment.end))) { |
| 1488 | mDirty = true; |
| 1489 | mCache.mSegment = segment; |
| 1490 | } |
| 1491 | mCache.mFrameNo = frameNo; |
| 1492 | } |
| 1493 |
| 1494 | void LOTTrimItem::update() |
| 1495 | { |
| 1496 | // when both path and trim are not dirty |
| 1497 | if (!(mDirty || pathDirty())) return; |
| 1498 |
| 1499 | if (vCompare(mCache.mSegment.start, mCache.mSegment.end)) { |
| 1500 | for (auto &i : mPathItems) { |
| 1501 | i->updatePath(VPath()); |
| 1502 | } |
| 1503 | return; |
| 1504 | } |
| 1505 |
| 1506 | if (vCompare(std::fabs(mCache.mSegment.start - mCache.mSegment.end), 1)) { |
| 1507 | for (auto &i : mPathItems) { |
| 1508 | i->updatePath(i->localPath()); |
| 1509 | } |
| 1510 | return; |
| 1511 | } |
| 1512 |
| 1513 | if (mData->type() == LOTTrimData::TrimType::Simultaneously) { |
| 1514 | for (auto &i : mPathItems) { |
| 1515 | VPathMesure pm; |
| 1516 | pm.setStart(mCache.mSegment.start); |
| 1517 | pm.setEnd(mCache.mSegment.end); |
| 1518 | i->updatePath(pm.trim(i->localPath())); |
| 1519 | } |
| 1520 | } else { // LOTTrimData::TrimType::Individually |
| 1521 | float totalLength = 0.0; |
| 1522 | for (auto &i : mPathItems) { |
| 1523 | totalLength += i->localPath().length(); |
| 1524 | } |
| 1525 | float start = totalLength * mCache.mSegment.start; |
| 1526 | float end = totalLength * mCache.mSegment.end; |
| 1527 |
| 1528 | if (start < end) { |
| 1529 | float curLen = 0.0; |
| 1530 | for (auto &i : mPathItems) { |
| 1531 | if (curLen > end) { |
| 1532 | // update with empty path. |
| 1533 | i->updatePath(VPath()); |
| 1534 | continue; |
| 1535 | } |
| 1536 | float len = i->localPath().length(); |
| 1537 |
| 1538 | if (curLen < start && curLen + len < start) { |
| 1539 | curLen += len; |
| 1540 | // update with empty path. |
| 1541 | i->updatePath(VPath()); |
| 1542 | continue; |
| 1543 | } else if (start <= curLen && end >= curLen + len) { |
| 1544 | // inside segment |
| 1545 | curLen += len; |
| 1546 | continue; |
| 1547 | } else { |
| 1548 | float local_start = start > curLen ? start - curLen : 0; |
| 1549 | local_start /= len; |
| 1550 | float local_end = curLen + len < end ? len : end - curLen; |
| 1551 | local_end /= len; |
| 1552 | VPathMesure pm; |
| 1553 | pm.setStart(local_start); |
| 1554 | pm.setEnd(local_end); |
| 1555 | VPath p = pm.trim(i->localPath()); |
| 1556 | i->updatePath(p); |
| 1557 | curLen += len; |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | } |
| 1563 |
| 1564 | void LOTTrimItem::addPathItems(std::vector<LOTPathDataItem *> &list, |
| 1565 | int startOffset) |
| 1566 | { |
| 1567 | std::copy(list.begin() + startOffset, list.end(), |
| 1568 | back_inserter(mPathItems)); |
| 1569 | } |
| 1570 |
| 1571 | LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data) : mRepeaterData(data) |
| 1572 | { |
| 1573 | assert(mRepeaterData->content()); |
| 1574 |
| 1575 | mCopies = mRepeaterData->maxCopies(); |
| 1576 |
| 1577 | for (int i = 0; i < mCopies; i++) { |
| 1578 | auto content = |
| 1579 | std::make_unique<LOTContentGroupItem>(mRepeaterData->content()); |
| 1580 | content->setParent(this); |
| 1581 | mContents.push_back(std::move(content)); |
| 1582 | } |
| 1583 | } |
| 1584 |
| 1585 | void LOTRepeaterItem::update(int frameNo, const VMatrix &parentMatrix, |
| 1586 | float parentAlpha, const DirtyFlag &flag) |
| 1587 | { |
| 1588 | DirtyFlag newFlag = flag; |
| 1589 |
| 1590 | float copies = mRepeaterData->copies(frameNo); |
| 1591 | int visibleCopies = int(copies); |
| 1592 |
| 1593 | if (visibleCopies == 0) { |
| 1594 | mHidden = true; |
| 1595 | return; |
| 1596 | } else { |
| 1597 | mHidden = false; |
| 1598 | } |
| 1599 |
| 1600 | if (!mRepeaterData->isStatic()) newFlag |= DirtyFlagBit::Matrix; |
| 1601 |
| 1602 | float offset = mRepeaterData->offset(frameNo); |
| 1603 | float startOpacity = mRepeaterData->mTransform.startOpacity(frameNo); |
| 1604 | float endOpacity = mRepeaterData->mTransform.endOpacity(frameNo); |
| 1605 |
| 1606 | newFlag |= DirtyFlagBit::Alpha; |
| 1607 |
| 1608 | for (int i = 0; i < mCopies; ++i) { |
| 1609 | float newAlpha = |
| 1610 | parentAlpha * lerp(startOpacity, endOpacity, i / copies); |
| 1611 |
| 1612 | // hide rest of the copies , @TODO find a better solution. |
| 1613 | if (i >= visibleCopies) newAlpha = 0; |
| 1614 |
| 1615 | VMatrix result = mRepeaterData->mTransform.matrix(frameNo, i + offset) * |
| 1616 | parentMatrix; |
| 1617 | mContents[i]->update(frameNo, result, newAlpha, newFlag); |
| 1618 | } |
| 1619 | } |
| 1620 |
| 1621 | void LOTRepeaterItem::renderList(std::vector<VDrawable *> &list) |
| 1622 | { |
| 1623 | if (mHidden) return; |
| 1624 | return LOTContentGroupItem::renderList(list); |
| 1625 | } |
| 1626 |
| 1627 | static void updateGStops(LOTNode *n, const VGradient *grad) |
| 1628 | { |
| 1629 | if (grad->mStops.size() != n->mGradient.stopCount) { |
| 1630 | if (n->mGradient.stopCount) free(n->mGradient.stopPtr); |
| 1631 | n->mGradient.stopCount = grad->mStops.size(); |
| 1632 | n->mGradient.stopPtr = (LOTGradientStop *)malloc( |
| 1633 | n->mGradient.stopCount * sizeof(LOTGradientStop)); |
| 1634 | } |
| 1635 |
| 1636 | LOTGradientStop *ptr = n->mGradient.stopPtr; |
| 1637 | for (const auto &i : grad->mStops) { |
| 1638 | ptr->pos = i.first; |
| 1639 | ptr->a = i.second.alpha() * grad->alpha(); |
| 1640 | ptr->r = i.second.red(); |
| 1641 | ptr->g = i.second.green(); |
| 1642 | ptr->b = i.second.blue(); |
| 1643 | ptr++; |
| 1644 | } |
| 1645 | } |
| 1646 |
| 1647 | void LOTDrawable::sync() |
| 1648 | { |
| 1649 | if (!mCNode) { |
| 1650 | mCNode = std::make_unique<LOTNode>(); |
| 1651 | mCNode->mGradient.stopPtr = nullptr; |
| 1652 | mCNode->mGradient.stopCount = 0; |
| 1653 | } |
| 1654 |
| 1655 | mCNode->mFlag = ChangeFlagNone; |
| 1656 | if (mFlag & DirtyState::None) return; |
| 1657 |
| 1658 | if (mFlag & DirtyState::Path) { |
| 1659 | if (mStroke.mDash.size()) { |
| 1660 | VDasher dasher(mStroke.mDash.data(), mStroke.mDash.size()); |
| 1661 | mPath = dasher.dashed(mPath); |
| 1662 | } |
| 1663 | const std::vector<VPath::Element> &elm = mPath.elements(); |
| 1664 | const std::vector<VPointF> & pts = mPath.points(); |
| 1665 | const float *ptPtr = reinterpret_cast<const float *>(pts.data()); |
| 1666 | const char * elmPtr = reinterpret_cast<const char *>(elm.data()); |
| 1667 | mCNode->mPath.elmPtr = elmPtr; |
| 1668 | mCNode->mPath.elmCount = elm.size(); |
| 1669 | mCNode->mPath.ptPtr = ptPtr; |
| 1670 | mCNode->mPath.ptCount = 2 * pts.size(); |
| 1671 | mCNode->mFlag |= ChangeFlagPath; |
| 1672 | } |
| 1673 |
| 1674 | if (mStroke.enable) { |
| 1675 | mCNode->mStroke.width = mStroke.width; |
| 1676 | mCNode->mStroke.meterLimit = mStroke.meterLimit; |
| 1677 | mCNode->mStroke.enable = 1; |
| 1678 |
| 1679 | switch (mStroke.cap) { |
| 1680 | case CapStyle::Flat: |
| 1681 | mCNode->mStroke.cap = LOTCapStyle::CapFlat; |
| 1682 | break; |
| 1683 | case CapStyle::Square: |
| 1684 | mCNode->mStroke.cap = LOTCapStyle::CapSquare; |
| 1685 | break; |
| 1686 | case CapStyle::Round: |
| 1687 | mCNode->mStroke.cap = LOTCapStyle::CapRound; |
| 1688 | break; |
| 1689 | } |
| 1690 |
| 1691 | switch (mStroke.join) { |
| 1692 | case JoinStyle::Miter: |
| 1693 | mCNode->mStroke.join = LOTJoinStyle::JoinMiter; |
| 1694 | break; |
| 1695 | case JoinStyle::Bevel: |
| 1696 | mCNode->mStroke.join = LOTJoinStyle::JoinBevel; |
| 1697 | break; |
| 1698 | case JoinStyle::Round: |
| 1699 | mCNode->mStroke.join = LOTJoinStyle::JoinRound; |
| 1700 | break; |
| 1701 | default: |
| 1702 | mCNode->mStroke.join = LOTJoinStyle::JoinMiter; |
| 1703 | break; |
| 1704 | } |
| 1705 | } else { |
| 1706 | mCNode->mStroke.enable = 0; |
| 1707 | } |
| 1708 |
| 1709 | switch (mFillRule) { |
| 1710 | case FillRule::EvenOdd: |
| 1711 | mCNode->mFillRule = LOTFillRule::FillEvenOdd; |
| 1712 | break; |
| 1713 | default: |
| 1714 | mCNode->mFillRule = LOTFillRule::FillWinding; |
| 1715 | break; |
| 1716 | } |
| 1717 |
| 1718 | switch (mBrush.type()) { |
| 1719 | case VBrush::Type::Solid: |
| 1720 | mCNode->mBrushType = LOTBrushType::BrushSolid; |
| 1721 | mCNode->mColor.r = mBrush.mColor.r; |
| 1722 | mCNode->mColor.g = mBrush.mColor.g; |
| 1723 | mCNode->mColor.b = mBrush.mColor.b; |
| 1724 | mCNode->mColor.a = mBrush.mColor.a; |
| 1725 | break; |
| 1726 | case VBrush::Type::LinearGradient: { |
| 1727 | mCNode->mBrushType = LOTBrushType::BrushGradient; |
| 1728 | mCNode->mGradient.type = LOTGradientType::GradientLinear; |
| 1729 | VPointF s = mBrush.mGradient->mMatrix.map( |
| 1730 | {mBrush.mGradient->linear.x1, mBrush.mGradient->linear.y1}); |
| 1731 | VPointF e = mBrush.mGradient->mMatrix.map( |
| 1732 | {mBrush.mGradient->linear.x2, mBrush.mGradient->linear.y2}); |
| 1733 | mCNode->mGradient.start.x = s.x(); |
| 1734 | mCNode->mGradient.start.y = s.y(); |
| 1735 | mCNode->mGradient.end.x = e.x(); |
| 1736 | mCNode->mGradient.end.y = e.y(); |
| 1737 | updateGStops(mCNode.get(), mBrush.mGradient); |
| 1738 | break; |
| 1739 | } |
| 1740 | case VBrush::Type::RadialGradient: { |
| 1741 | mCNode->mBrushType = LOTBrushType::BrushGradient; |
| 1742 | mCNode->mGradient.type = LOTGradientType::GradientRadial; |
| 1743 | VPointF c = mBrush.mGradient->mMatrix.map( |
| 1744 | {mBrush.mGradient->radial.cx, mBrush.mGradient->radial.cy}); |
| 1745 | VPointF f = mBrush.mGradient->mMatrix.map( |
| 1746 | {mBrush.mGradient->radial.fx, mBrush.mGradient->radial.fy}); |
| 1747 | mCNode->mGradient.center.x = c.x(); |
| 1748 | mCNode->mGradient.center.y = c.y(); |
| 1749 | mCNode->mGradient.focal.x = f.x(); |
| 1750 | mCNode->mGradient.focal.y = f.y(); |
| 1751 |
| 1752 | float scale = getScale(mBrush.mGradient->mMatrix); |
| 1753 | mCNode->mGradient.cradius = mBrush.mGradient->radial.cradius * scale; |
| 1754 | mCNode->mGradient.fradius = mBrush.mGradient->radial.fradius * scale; |
| 1755 | updateGStops(mCNode.get(), mBrush.mGradient); |
| 1756 | break; |
| 1757 | } |
| 1758 | default: |
| 1759 | break; |
| 1760 | } |
| 1761 | } |
| 1762 |