1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
|
/*
Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
Copyright (C) 2022 Sony Interactive Entertainment Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "TextureMapperLayer.h"
#include "BitmapTexture.h"
#include "ClipPath.h"
#include "FloatQuad.h"
#include "Region.h"
#include "TextureMapper.h"
#include "TextureMapperLayer3DRenderingContext.h"
#include <wtf/MathExtras.h>
#include <wtf/SetForScope.h>
#include <wtf/TZoneMallocInlines.h>
#if USE(COORDINATED_GRAPHICS)
#include "CoordinatedAnimatedBackingStoreClient.h"
#endif
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(TextureMapperLayer);
class TextureMapperPaintOptions {
public:
TextureMapperPaintOptions(TextureMapper& textureMapper)
: textureMapper(textureMapper)
{ }
TextureMapper& textureMapper;
TransformationMatrix transform;
RefPtr<BitmapTexture> surface;
float opacity { 1 };
IntSize offset;
TextureMapperLayer* backdropLayer { nullptr };
TextureMapperLayer* replicaLayer { nullptr };
};
struct TextureMapperLayer::ComputeTransformData {
double zNear { 0 };
double zFar { 0 };
void updateDepthRange(double z)
{
if (zNear < z)
zNear = z;
else if (zFar > z)
zFar = z;
}
};
class TextureMapperFlattenedLayer final {
WTF_MAKE_FAST_ALLOCATED;
public:
explicit TextureMapperFlattenedLayer(const IntRect& rect, double zNear, double zFar)
: m_rect(rect)
, m_zNear(zNear)
, m_zFar(zFar)
{ }
IntRect layerRect() const { return m_rect; }
bool needsUpdate() const { return m_needsUpdate; }
void update(TextureMapperPaintOptions& options, const std::function<void(TextureMapperPaintOptions&)>& paintFunction)
{
if (!m_needsUpdate)
return;
auto [prevZNear, prevZFar] = options.textureMapper.depthRange();
options.textureMapper.setDepthRange(m_zNear, m_zFar);
m_textures.clear();
auto maxTextureSize = options.textureMapper.maxTextureSize();
forEachTile(maxTextureSize, [&](const IntRect& tileRect) {
RefPtr<BitmapTexture> surface = options.textureMapper.acquireTextureFromPool(tileRect.size(), { BitmapTexture::Flags::SupportsAlpha });
{
SetForScope scopedSurface(options.surface, surface);
SetForScope scopedOffset(options.offset, -toIntSize(tileRect.location()));
SetForScope scopedOpacity(options.opacity, 1);
options.textureMapper.bindSurface(options.surface.get());
paintFunction(options);
// If paintFunction applies filters to flattened surface then surface object might have
// changed to new intermediate filter surface and it needs to be stored for painting.
ASSERT(options.surface);
surface = options.surface;
}
m_textures.append(WTFMove(surface));
});
options.textureMapper.bindSurface(options.surface.get());
options.textureMapper.setDepthRange(prevZNear, prevZFar);
m_needsUpdate = false;
}
void paintToTextureMapper(TextureMapper& textureMapper, const FloatRect& targetRect, TransformationMatrix& modelViewMatrix, float opacity)
{
auto maxTextureSize = textureMapper.maxTextureSize();
auto allEdgesExposed = m_rect.width() <= maxTextureSize.width() && m_rect.height() <= maxTextureSize.height()
? TextureMapper::AllEdgesExposed::Yes
: TextureMapper::AllEdgesExposed::No;
TransformationMatrix adjustedTransform = modelViewMatrix * TransformationMatrix::rectToRect(m_rect, targetRect);
size_t textureIndex = 0;
forEachTile(maxTextureSize, [&](const IntRect& tileRect) {
ASSERT(textureIndex < m_textures.size());
textureMapper.drawTexture(*m_textures[textureIndex++], tileRect, adjustedTransform, opacity, allEdgesExposed);
});
}
private:
void forEachTile(const IntSize& maxTextureSize, const std::function<void(const IntRect&)>& tileAction)
{
for (int x = m_rect.x(); x < m_rect.maxX(); x += maxTextureSize.width()) {
for (int y = m_rect.y(); y < m_rect.maxY(); y += maxTextureSize.height()) {
IntRect tileRect({ x, y }, maxTextureSize);
tileRect.intersect(m_rect);
tileAction(tileRect);
}
}
}
IntRect m_rect;
double m_zNear;
double m_zFar;
Vector<RefPtr<BitmapTexture>> m_textures;
bool m_needsUpdate { true };
};
TextureMapperLayer::TextureMapperLayer() = default;
TextureMapperLayer::~TextureMapperLayer()
{
for (auto* child : m_children)
child->m_parent = nullptr;
removeFromParent();
}
void TextureMapperLayer::processDescendantLayersFlatteningRequirements()
{
// Traverse all children in depth first, post-order
for (auto* child : m_children) {
child->processDescendantLayersFlatteningRequirements();
if (child->flattensAsLeafOf3DSceneOr3DPerspective())
child->processFlatteningRequirements();
}
}
void TextureMapperLayer::processFlatteningRequirements()
{
m_flattenedLayer = nullptr;
// Reset transformations as this layer is root 2D
SetForScope scopedLocalTransform(m_layerTransforms.localTransform, TransformationMatrix::identity);
m_layerTransforms.combined = { };
m_layerTransforms.combinedForChildren = { };
#if USE(COORDINATED_GRAPHICS)
SetForScope scopedFutureLocalTransform(m_layerTransforms.futureLocalTransform, TransformationMatrix::identity);
m_layerTransforms.futureLocalTransform = { };
m_layerTransforms.futureCombined = { };
m_layerTransforms.futureCombinedForChildren = { };
#endif
ComputeTransformData data;
if (m_state.maskLayer)
m_state.maskLayer->computeTransformsRecursive(data);
if (m_state.replicaLayer)
m_state.replicaLayer->computeTransformsRecursive(data);
if (m_state.backdropLayer)
m_state.backdropLayer->computeTransformsRecursive(data);
for (auto* child : m_children)
child->computeTransformsRecursive(data);
Region layerRegion;
computeFlattenedRegion(layerRegion, true);
if (layerRegion.isEmpty())
return;
m_flattenedLayer = makeUnique<TextureMapperFlattenedLayer>(layerRegion.bounds(), data.zNear, data.zFar);
}
void TextureMapperLayer::computeFlattenedRegion(Region& region, bool layerIsFlatteningRoot) const
{
auto rect = isFlattened() ? m_flattenedLayer->layerRect() : layerRect();
bool shouldExpand = m_currentFilters.hasOutsets() && !m_state.masksToBounds && !m_state.maskLayer;
if (shouldExpand && !layerIsFlatteningRoot) {
auto outsets = m_currentFilters.outsets();
rect.move(-outsets.left(), -outsets.top());
rect.expand(outsets.left() + outsets.right(), outsets.top() + outsets.bottom());
}
region.unite(enclosingIntRect(m_layerTransforms.combined.mapRect(rect)));
if (!m_state.masksToBounds && !m_state.maskLayer) {
if (m_state.replicaLayer)
region.unite(enclosingIntRect(m_state.replicaLayer->m_layerTransforms.combined.mapRect(m_state.replicaLayer->layerRect())));
for (auto* child : m_children)
child->computeFlattenedRegion(region, false);
}
if (shouldExpand && layerIsFlatteningRoot) {
auto bounds = region.bounds();
auto outsets = m_currentFilters.outsets();
bounds.move(-outsets.left(), -outsets.top());
bounds.expand(outsets.left() + outsets.right(), outsets.top() + outsets.bottom());
region = bounds;
}
}
void TextureMapperLayer::destroyFlattenedDescendantLayers()
{
if (m_flattenedLayer)
m_flattenedLayer = nullptr;
for (auto* child : m_children)
child->destroyFlattenedDescendantLayers();
}
void TextureMapperLayer::computeTransformsRecursive(ComputeTransformData& data)
{
if (m_state.size.isEmpty() && m_state.masksToBounds)
return;
// Compute transforms recursively on the way down to leafs.
{
TransformationMatrix parentTransform;
if (m_parent)
parentTransform = m_parent->m_layerTransforms.combinedForChildren;
else if (m_effectTarget)
parentTransform = m_effectTarget->m_layerTransforms.combined;
const float originX = m_state.anchorPoint.x() * m_state.size.width();
const float originY = m_state.anchorPoint.y() * m_state.size.height();
#if ENABLE(DAMAGE_TRACKING)
TransformationMatrix oldCombined = m_layerTransforms.combined;
#endif
m_layerTransforms.combined = parentTransform;
m_layerTransforms.combined
.translate3d(originX + (m_state.pos.x() - m_state.boundsOrigin.x()), originY + (m_state.pos.y() - m_state.boundsOrigin.y()), m_state.anchorPoint.z())
.multiply(m_layerTransforms.localTransform);
m_layerTransforms.combinedForChildren = m_layerTransforms.combined;
m_layerTransforms.combined.translate3d(-originX, -originY, -m_state.anchorPoint.z());
if (m_isReplica)
m_layerTransforms.combined.translate(-m_state.pos.x(), -m_state.pos.y());
if (!m_state.preserves3D)
m_layerTransforms.combinedForChildren.flatten();
m_layerTransforms.combinedForChildren.multiply(m_state.childrenTransform);
m_layerTransforms.combinedForChildren.translate3d(-originX, -originY, -m_state.anchorPoint.z());
#if USE(COORDINATED_GRAPHICS)
// Compute transforms for the future as well.
TransformationMatrix futureParentTransform;
if (m_parent)
futureParentTransform = m_parent->m_layerTransforms.futureCombinedForChildren;
else if (m_effectTarget)
futureParentTransform = m_effectTarget->m_layerTransforms.futureCombined;
m_layerTransforms.futureCombined = futureParentTransform;
m_layerTransforms.futureCombined
.translate3d(originX + (m_state.pos.x() - m_state.boundsOrigin.x()), originY + (m_state.pos.y() - m_state.boundsOrigin.y()), m_state.anchorPoint.z())
.multiply(m_layerTransforms.futureLocalTransform);
m_layerTransforms.futureCombinedForChildren = m_layerTransforms.futureCombined;
m_layerTransforms.futureCombined.translate3d(-originX, -originY, -m_state.anchorPoint.z());
if (!m_state.preserves3D)
m_layerTransforms.futureCombinedForChildren.flatten();
m_layerTransforms.futureCombinedForChildren.multiply(m_state.childrenTransform);
m_layerTransforms.futureCombinedForChildren.translate3d(-originX, -originY, -m_state.anchorPoint.z());
#endif
#if ENABLE(DAMAGE_TRACKING)
if (canInferDamage() && oldCombined != m_layerTransforms.combined)
damageWholeLayerDueToTransformChange(oldCombined, m_layerTransforms.combined);
#endif
}
m_state.visible = m_state.backfaceVisibility || !m_layerTransforms.combined.isBackFaceVisible();
auto calculateZ = [&](double x, double y) -> double {
double z = 0;
double w = 1;
m_layerTransforms.combined.map4ComponentPoint(x, y, z, w);
if (w <= 0) {
if (!z)
return 0;
if (z < 0)
return -std::numeric_limits<double>::infinity();
return std::numeric_limits<double>::infinity();
}
return z / w;
};
auto rect = isFlattened() ? m_flattenedLayer->layerRect() : layerRect();
data.updateDepthRange(calculateZ(rect.x(), rect.y()));
data.updateDepthRange(calculateZ(rect.x() + rect.width(), rect.y()));
data.updateDepthRange(calculateZ(rect.x(), rect.y() + rect.height()));
data.updateDepthRange(calculateZ(rect.x() + rect.width(), rect.y() + rect.height()));
if (m_state.backdropLayer)
m_state.backdropLayer->computeTransformsRecursive(data);
if (!isFlattened()) {
if (m_state.maskLayer)
m_state.maskLayer->computeTransformsRecursive(data);
if (m_state.replicaLayer)
m_state.replicaLayer->computeTransformsRecursive(data);
for (auto* child : m_children) {
ASSERT(child->m_parent == this);
child->computeTransformsRecursive(data);
}
}
#if USE(COORDINATED_GRAPHICS)
if (m_backingStore && m_animatedBackingStoreClient)
m_animatedBackingStoreClient->requestBackingStoreUpdateIfNeeded(m_layerTransforms.futureCombined);
#endif
}
void TextureMapperLayer::prepareForPainting(TextureMapper& textureMapper)
{
processDescendantLayersFlatteningRequirements();
ComputeTransformData data;
computeTransformsRecursive(data);
textureMapper.setDepthRange(data.zNear, data.zFar);
}
void TextureMapperLayer::paint(TextureMapper& textureMapper)
{
#if !ENABLE(DAMAGE_TRACKING)
prepareForPainting(textureMapper);
#endif
TextureMapperPaintOptions options(textureMapper);
options.surface = textureMapper.currentSurface();
paintRecursive(options);
destroyFlattenedDescendantLayers();
}
#if ENABLE(DAMAGE_TRACKING)
void TextureMapperLayer::setDamage(const Damage& damage)
{
m_damage = damage;
}
void TextureMapperLayer::collectDamage(TextureMapper& textureMapper, Damage& damage)
{
TextureMapperPaintOptions options(textureMapper);
options.surface = textureMapper.currentSurface();
collectDamageRecursive(options, damage);
}
void TextureMapperLayer::collectDamageRecursive(TextureMapperPaintOptions& options, Damage& damage)
{
if (!isVisible())
return;
SetForScope scopedOpacity(options.opacity, options.opacity * m_currentOpacity);
if (preserves3D() || isFlattened() || shouldBlend())
collectDamageSelfAndChildren(options, damage);
else
collectDamageSelfChildrenReplicaFilterAndMask(options, damage);
}
void TextureMapperLayer::collectDamageSelfAndChildren(TextureMapperPaintOptions& options, Damage& damage)
{
collectDamageSelf(options, damage);
for (auto* child : m_children)
child->collectDamageRecursive(options, damage);
}
void TextureMapperLayer::collectDamageSelf(TextureMapperPaintOptions& options, Damage& damage)
{
ASSERT(m_damagePropagation);
if (!m_state.visible || !m_state.contentsVisible)
return;
auto targetRect = layerRect();
if (targetRect.isEmpty())
return;
TransformationMatrix transform;
transform.translate(options.offset.width(), options.offset.height());
transform.multiply(options.transform);
transform.multiply(m_layerTransforms.combined);
ASSERT(!m_damage.isInvalid());
ASSERT(!m_inferredDamage.isInvalid());
if (!m_inferredDamage.isEmpty()) {
for (const auto& rect : m_inferredDamage.rects()) {
ASSERT(!rect.isEmpty());
damage.add(rect);
}
} else if (m_contentsLayer) {
// Layers with content layer are fully damaged if there's no explicit damage.
// FIXME: Remove that special case.
damage.add(transformRectForDamage(targetRect, transform, options));
} else {
// Use the damage information we received from the GraphicsLayer
// Here we ignore the targetRect parameter as it should already have
// been covered by the damage tracking in setNeedsDisplay/setNeedsDisplayInRect
// calls from GraphicsLayer.
for (const auto& rect : m_damage.rects()) {
ASSERT(!rect.isEmpty());
damage.add(transformRectForDamage(rect, transform, options));
}
}
m_damage = { };
m_inferredDamage = { };
}
void TextureMapperLayer::collectDamageSelfChildrenReplicaFilterAndMask(TextureMapperPaintOptions& options, Damage& damage)
{
const bool hasFilterOrMask = hasFilters() || m_state.maskLayer || (m_state.replicaLayer && m_state.replicaLayer->m_state.maskLayer);
auto collectDamageSelfAndOthers = [&]() {
if (hasFilterOrMask)
collectDamageSelfChildrenFilterAndMask(options, damage);
else
collectDamageSelfAndChildren(options, damage);
};
if (m_state.replicaLayer) {
SetForScope scopedReplicaLayer(options.replicaLayer, this);
SetForScope scopedTransform(options.transform, options.transform);
options.transform.multiply(replicaTransform());
collectDamageSelfAndOthers();
}
collectDamageSelfAndOthers();
}
void TextureMapperLayer::collectDamageSelfChildrenFilterAndMask(TextureMapperPaintOptions& options, Damage& damage)
{
const IntSize maxTextureSize = options.textureMapper.maxTextureSize();
for (auto& rect : computeConsolidatedOverlapRegionRects(options)) {
for (int x = rect.x(); x < rect.maxX(); x += maxTextureSize.width()) {
for (int y = rect.y(); y < rect.maxY(); y += maxTextureSize.height()) {
IntRect tileRect(IntPoint(x, y), maxTextureSize);
tileRect.intersect(rect);
m_accumulatedOverlapRegionDamage.unite(transformRectForDamage(tileRect, options.transform, options));
}
}
}
if (!m_accumulatedOverlapRegionDamage.isEmpty())
damage.add(m_accumulatedOverlapRegionDamage);
}
void TextureMapperLayer::damageWholeLayerDueToTransformChange(const TransformationMatrix& beforeChange, const TransformationMatrix& afterChange)
{
// When the layer's transform changes, we must not only damage whole layer using new transform,
// but also using old transform to cover the area not affected by layer anymore.
m_inferredDamage.add(afterChange.mapRect(layerRect()));
m_inferredDamage.add(beforeChange.mapRect(layerRect()));
}
FloatRect TextureMapperLayer::transformRectForDamage(const FloatRect& rect, const TransformationMatrix& transform, const TextureMapperPaintOptions& options)
{
auto transformedRect = transform.mapRect(rect);
// Some layers are drawn on an intermediate surface and have this offset applied to convert to the
// intermediate surface coordinates. In order to translate back to actual coordinates,
// we have to undo it.
transformedRect.move(-options.offset);
auto clipBounds = options.textureMapper.clipBounds();
clipBounds.move(-options.offset);
transformedRect.intersect(clipBounds);
return transformedRect;
}
#endif
void TextureMapperLayer::paintSelf(TextureMapperPaintOptions& options)
{
if (!m_state.visible || !m_state.contentsVisible)
return;
auto targetRect = layerRect();
if (targetRect.isEmpty())
return;
// We apply the following transform to compensate for painting into a surface, and then apply the offset so that the painting fits in the target rect.
TransformationMatrix transform;
transform.translate(options.offset.width(), options.offset.height());
transform.multiply(options.transform);
transform.multiply(m_layerTransforms.combined);
if (isFlattened() && !m_flattenedLayer->needsUpdate()) {
m_flattenedLayer->paintToTextureMapper(options.textureMapper, m_flattenedLayer->layerRect(), transform, options.opacity);
return;
}
TextureMapperSolidColorLayer solidColorLayer;
TextureMapperBackingStore* backingStore = m_backingStore;
if (m_state.backgroundColor.isValid()) {
solidColorLayer.setColor(m_state.backgroundColor);
backingStore = &solidColorLayer;
}
options.textureMapper.setWrapMode(TextureMapper::WrapMode::Stretch);
options.textureMapper.setPatternTransform(TransformationMatrix());
if (backingStore) {
backingStore->paintToTextureMapper(options.textureMapper, targetRect, transform, options.opacity);
if (m_state.showDebugBorders)
backingStore->drawBorder(options.textureMapper, m_state.debugBorderColor, m_state.debugBorderWidth, targetRect, transform);
// Only draw repaint count for the main backing store.
if (m_state.showRepaintCounter)
backingStore->drawRepaintCounter(options.textureMapper, m_state.repaintCount, m_state.debugBorderColor, targetRect, transform);
}
TextureMapperPlatformLayer* contentsLayer = m_contentsLayer;
if (m_state.solidColor.isValid() && m_state.solidColor.isVisible()) {
solidColorLayer.setColor(m_state.solidColor);
contentsLayer = &solidColorLayer;
}
if (!contentsLayer)
return;
if (!m_state.contentsTileSize.isEmpty()) {
options.textureMapper.setWrapMode(TextureMapper::WrapMode::Repeat);
auto patternTransform = TransformationMatrix::rectToRect({ { }, m_state.contentsTileSize }, { { }, m_state.contentsRect.size() })
.translate(m_state.contentsTilePhase.width() / m_state.contentsRect.width(), m_state.contentsTilePhase.height() / m_state.contentsRect.height());
options.textureMapper.setPatternTransform(patternTransform);
}
bool shouldClip = m_state.contentsClippingRect.isRounded() || !m_state.contentsClippingRect.rect().contains(m_state.contentsRect);
if (shouldClip) {
options.textureMapper.beginClip(transform, m_state.contentsClippingRect);
}
contentsLayer->paintToTextureMapper(options.textureMapper, m_state.contentsRect, transform, options.opacity);
if (shouldClip)
options.textureMapper.endClip();
if (m_state.showDebugBorders)
contentsLayer->drawBorder(options.textureMapper, m_state.debugBorderColor, m_state.debugBorderWidth, m_state.contentsRect, transform);
}
void TextureMapperLayer::paintBackdrop(TextureMapperPaintOptions& options)
{
TransformationMatrix clipTransform;
clipTransform.translate(options.offset.width(), options.offset.height());
clipTransform.multiply(options.transform);
clipTransform.multiply(m_layerTransforms.combined);
options.textureMapper.beginClip(clipTransform, m_state.backdropFiltersRect);
m_state.backdropLayer->paintRecursive(options);
options.textureMapper.endClip();
}
void TextureMapperLayer::paintSelfAndChildren(TextureMapperPaintOptions& options)
{
if (m_state.backdropLayer && m_state.backdropLayer == options.backdropLayer)
return;
if (m_state.backdropLayer && !options.backdropLayer)
paintBackdrop(options);
paintSelf(options);
if (m_children.isEmpty())
return;
bool shouldClip = (m_state.masksToBounds || m_state.contentsRectClipsDescendants) && !m_state.preserves3D;
if (shouldClip) {
TransformationMatrix clipTransform;
clipTransform.translate(options.offset.width(), options.offset.height());
clipTransform.multiply(options.transform);
clipTransform.multiply(m_layerTransforms.combined);
if (m_state.contentsRectClipsDescendants)
options.textureMapper.beginClip(clipTransform, m_state.contentsClippingRect);
else {
clipTransform.translate(m_state.boundsOrigin.x(), m_state.boundsOrigin.y());
options.textureMapper.beginClip(clipTransform, FloatRoundedRect(layerRect()));
}
// If as a result of beginClip(), the clipping area is empty, it means that the intersection of the previous
// clipping area and the current one don't have any pixels in common. In this case we can skip painting the
// children as they will be clipped out (see https://bugs.webkit.org/show_bug.cgi?id=181080).
if (options.textureMapper.clipBounds().isEmpty()) {
options.textureMapper.endClip();
return;
}
}
if (!isFlattened() || m_flattenedLayer->needsUpdate()) {
for (auto* child : m_children)
child->paintRecursive(options);
}
if (shouldClip)
options.textureMapper.endClip();
}
bool TextureMapperLayer::shouldBlend() const
{
if (m_state.preserves3D)
return false;
return m_currentOpacity < 1;
}
bool TextureMapperLayer::flattensAsLeafOf3DSceneOr3DPerspective() const
{
bool hasPerspective = m_layerTransforms.localTransform.hasPerspective() && m_layerTransforms.localTransform.m34() != -1.f;
if ((isLeafOf3DRenderingContext() || hasPerspective) && !m_children.isEmpty() && !m_layerTransforms.localTransform.isAffine())
return true;
return false;
}
bool TextureMapperLayer::isVisible() const
{
if (m_state.size.isEmpty() && (m_state.masksToBounds || m_state.maskLayer || m_children.isEmpty()))
return false;
if (!m_state.visible && m_children.isEmpty())
return false;
if (!m_state.contentsVisible && m_children.isEmpty())
return false;
if (m_currentOpacity < 0.01)
return false;
return true;
}
void TextureMapperLayer::paintSelfAndChildrenWithReplica(TextureMapperPaintOptions& options)
{
if (m_state.replicaLayer) {
SetForScope scopedReplicaLayer(options.replicaLayer, this);
SetForScope scopedTransform(options.transform, options.transform);
options.transform.multiply(replicaTransform());
paintSelfAndChildren(options);
}
paintSelfAndChildren(options);
}
TransformationMatrix TextureMapperLayer::replicaTransform()
{
return TransformationMatrix(m_state.replicaLayer->m_layerTransforms.combined)
.multiply(m_layerTransforms.combined.inverse().value_or(TransformationMatrix()));
}
static void resolveOverlaps(const IntRect& newRegion, Region& overlapRegion, Region& nonOverlapRegion)
{
Region newOverlapRegion(newRegion);
newOverlapRegion.intersect(nonOverlapRegion);
nonOverlapRegion.subtract(newOverlapRegion);
overlapRegion.unite(newOverlapRegion);
Region newNonOverlapRegion(newRegion);
newNonOverlapRegion.subtract(overlapRegion);
nonOverlapRegion.unite(newNonOverlapRegion);
}
template<typename T>
struct Point4D {
T x;
T y;
T z;
T w;
};
template<typename T>
Point4D<T> operator+(Point4D<T> s, Point4D<T> t)
{
return { s.x + t.x, s.y + t.y, s.z + t.z, s.w + t.w };
}
template<typename T>
Point4D<T> operator-(Point4D<T> s, Point4D<T> t)
{
return { s.x - t.x, s.y - t.y, s.z - t.z, s.w - t.w };
}
template<typename T>
Point4D<T> operator*(T s, Point4D<T> t)
{
return { s * t.x, s * t.y, s * t.z, s * t.w };
}
template<typename T>
struct MinMax {
T min;
T max;
};
IntRect transformedBoundingBox(const TransformationMatrix& transform, FloatRect rect, const IntRect& clip)
{
using Point = Point4D<double>;
auto mapPoint = [&](FloatPoint p) -> Point {
double x = p.x();
double y = p.y();
double z = 0;
double w = 1;
transform.map4ComponentPoint(x, y, z, w);
return { x, y, z, w };
};
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN // GLib/Win port
Point vertex[] = {
mapPoint(rect.minXMinYCorner()),
mapPoint(rect.maxXMinYCorner()),
mapPoint(rect.maxXMaxYCorner()),
mapPoint(rect.minXMaxYCorner())
};
bool isPositive[] = {
vertex[0].w >= 0,
vertex[1].w >= 0,
vertex[2].w >= 0,
vertex[3].w >= 0
};
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
auto findFirstPositiveVertex = [&]() {
int i = 0;
for (; i < 4; i++) {
if (isPositive[i])
return i;
}
return i;
};
auto findFirstNegativeVertex = [&]() {
int i = 0;
for (; i < 4; i++) {
if (!isPositive[i])
return i;
}
return i;
};
auto leftVertexIndex = [](int i) {
return (i + 1) % 4;
};
auto diagonalVertexIndex = [](int i) {
return (i + 2) % 4;
};
auto rightVertexIndex = [](int i) {
return (i + 3) % 4;
};
auto minMax2 = [](double d1, double d2) -> MinMax<double> {
if (d1 < d2)
return { d1, d2 };
return { d2, d1 };
};
auto minMax3 = [&](double d1, double d2, double d3) -> MinMax<double> {
auto minmax = minMax2(d1, d2);
return { std::min(minmax.min, d3), std::max(minmax.max, d3) };
};
auto minMax4 = [&](double d1, double d2, double d3, double d4) -> MinMax<double> {
auto minmax1 = minMax2(d1, d2);
auto minmax2 = minMax2(d3, d4);
return { std::min(minmax1.min, minmax2.min), std::max(minmax1.max, minmax2.max) };
};
auto clipped = [&](const MinMax<double>& xMinMax, const MinMax<double>& yMinMax) -> IntRect {
int minX = std::max<double>(xMinMax.min, clip.x());
int minY = std::max<double>(yMinMax.min, clip.y());
int maxX = std::min<double>(xMinMax.max, clip.maxX());
int maxY = std::min<double>(yMinMax.max, clip.maxY());
return { minX, minY, maxX - minX, maxY - minY };
};
auto toPositive = [&](Point positive, Point negative) -> Point {
ASSERT(positive.w > 0);
ASSERT(negative.w <= 0);
auto v = positive.w * negative - negative.w * positive;
v.w = 0;
return v;
};
auto boundingBoxPPP = [&](Point p1, Point p2, Point p3) -> IntRect {
ASSERT(p1.w >= 0);
ASSERT(p2.w >= 0);
ASSERT(p3.w >= 0);
auto xMinMax = minMax3(p1.x / p1.w, p2.x / p2.w, p3.x / p3.w);
auto yMinMax = minMax3(p1.y / p1.w, p2.y / p2.w, p3.y / p3.w);
return clipped(xMinMax, yMinMax);
};
auto boundingBoxPPN = [&](Point p1, Point p2, Point n3) -> IntRect {
return boundingBoxPPP(p1, p2, toPositive(p1, n3));
};
auto boundingBoxPNN = [&](Point p1, Point n2, Point n3) -> IntRect {
return boundingBoxPPP(p1, toPositive(p1, n2), toPositive(p1, n3));
};
auto boundingBoxPPPByIndex = [&](int i1, int i2, int i3) {
return boundingBoxPPP(vertex[i1], vertex[i2], vertex[i3]);
};
auto boundingBoxPPNByIndex = [&](int i1, int i2, int i3) {
return boundingBoxPPN(vertex[i1], vertex[i2], vertex[i3]);
};
auto boundingBoxPNNByIndex = [&](int i1, int i2, int i3) {
return boundingBoxPNN(vertex[i1], vertex[i2], vertex[i3]);
};
int count = isPositive[0] + isPositive[1] + isPositive[2] + isPositive[3];
switch (count) {
case 0:
return { };
case 1: {
int i = findFirstPositiveVertex();
ASSERT(i < 4);
return boundingBoxPNNByIndex(i, rightVertexIndex(i), leftVertexIndex(i));
}
case 2: {
int i = findFirstPositiveVertex();
ASSERT(i < 3);
if (!i && isPositive[3])
i = 3;
int positiveRightIndex = i;
int positiveLeftIndex = leftVertexIndex(i);
ASSERT(isPositive[positiveLeftIndex]);
return unionRect(boundingBoxPPNByIndex(positiveRightIndex, positiveLeftIndex, leftVertexIndex(positiveLeftIndex)), boundingBoxPPNByIndex(positiveRightIndex, positiveLeftIndex, rightVertexIndex(positiveRightIndex)));
}
case 3: {
int i = findFirstNegativeVertex();
ASSERT(i < 4);
return unionRect(boundingBoxPPNByIndex(leftVertexIndex(i), rightVertexIndex(i), i), boundingBoxPPPByIndex(leftVertexIndex(i), rightVertexIndex(i), diagonalVertexIndex(i)));
}
case 4: {
auto xMinMax = minMax4(vertex[0].x / vertex[0].w, vertex[1].x / vertex[1].w, vertex[2].x / vertex[2].w, vertex[3].x / vertex[3].w);
auto yMinMax = minMax4(vertex[0].y / vertex[0].w, vertex[1].y / vertex[1].w, vertex[2].y / vertex[2].w, vertex[3].y / vertex[3].w);
return clipped(xMinMax, yMinMax);
}
}
ASSERT_NOT_REACHED();
return { };
}
void TextureMapperLayer::computeOverlapRegions(ComputeOverlapRegionData& data, const TransformationMatrix& accumulatedReplicaTransform, bool includesReplica)
{
if (!m_state.visible || !m_state.contentsVisible)
return;
FloatRect localBoundingRect;
if (isFlattened() && !m_isBackdrop)
localBoundingRect = m_flattenedLayer->layerRect();
else if (m_backingStore || m_state.masksToBounds || m_state.maskLayer || hasFilters() || hasBackdrop())
localBoundingRect = layerRect();
else if (m_contentsLayer || m_state.solidColor.isVisible())
localBoundingRect = m_state.contentsRect;
if (m_currentFilters.hasOutsets() && !m_state.backdropLayer && !m_state.masksToBounds && !m_state.maskLayer) {
auto outsets = m_currentFilters.outsets();
localBoundingRect.move(-outsets.left(), -outsets.top());
localBoundingRect.expand(outsets.left() + outsets.right(), outsets.top() + outsets.bottom());
}
TransformationMatrix transform(accumulatedReplicaTransform);
transform.multiply(m_layerTransforms.combined);
IntRect viewportBoundingRect = transformedBoundingBox(transform, localBoundingRect, data.clipBounds);
switch (data.mode) {
case ComputeOverlapRegionMode::Intersection:
resolveOverlaps(viewportBoundingRect, data.overlapRegion, data.nonOverlapRegion);
break;
case ComputeOverlapRegionMode::Union:
case ComputeOverlapRegionMode::Mask:
data.overlapRegion.unite(viewportBoundingRect);
break;
}
if (m_state.replicaLayer && includesReplica) {
TransformationMatrix newReplicaTransform(accumulatedReplicaTransform);
newReplicaTransform.multiply(replicaTransform());
computeOverlapRegions(data, newReplicaTransform, false);
}
if (!m_state.masksToBounds && data.mode != ComputeOverlapRegionMode::Mask && !isFlattened()) {
for (auto* child : m_children)
child->computeOverlapRegions(data, accumulatedReplicaTransform);
}
}
void TextureMapperLayer::paintUsingOverlapRegions(TextureMapperPaintOptions& options)
{
Region overlapRegion;
Region nonOverlapRegion;
auto mode = ComputeOverlapRegionMode::Intersection;
ComputeOverlapRegionData data {
mode,
options.textureMapper.clipBounds(),
overlapRegion,
nonOverlapRegion
};
data.clipBounds.move(-options.offset);
computeOverlapRegions(data, options.transform);
if (overlapRegion.isEmpty()) {
paintSelfChildrenReplicaFilterAndMask(options);
return;
}
// Having both overlap and non-overlap regions carries some overhead. Avoid it if the overlap area
// is big anyway.
if (overlapRegion.totalArea() > nonOverlapRegion.totalArea()) {
overlapRegion.unite(nonOverlapRegion);
nonOverlapRegion = Region();
}
nonOverlapRegion.translate(options.offset);
auto rects = nonOverlapRegion.rects();
for (auto& rect : rects) {
options.textureMapper.beginClip(TransformationMatrix(), FloatRoundedRect(rect));
paintSelfChildrenReplicaFilterAndMask(options);
options.textureMapper.endClip();
}
rects = overlapRegion.rects();
static const size_t OverlapRegionConsolidationThreshold = 4;
if (nonOverlapRegion.isEmpty() && rects.size() > OverlapRegionConsolidationThreshold) {
rects.clear();
rects.append(overlapRegion.bounds());
}
IntSize maxTextureSize = options.textureMapper.maxTextureSize();
for (auto& rect : rects) {
for (int x = rect.x(); x < rect.maxX(); x += maxTextureSize.width()) {
for (int y = rect.y(); y < rect.maxY(); y += maxTextureSize.height()) {
IntRect tileRect(IntPoint(x, y), maxTextureSize);
tileRect.intersect(rect);
paintWithIntermediateSurface(options, tileRect);
}
}
}
}
Vector<IntRect, 1> TextureMapperLayer::computeConsolidatedOverlapRegionRects(TextureMapperPaintOptions& options)
{
Region overlapRegion;
Region nonOverlapRegion;
auto mode = ComputeOverlapRegionMode::Union;
if (m_state.maskLayer || (options.replicaLayer == this && m_state.replicaLayer->m_state.maskLayer))
mode = ComputeOverlapRegionMode::Mask;
ComputeOverlapRegionData data {
mode,
options.textureMapper.clipBounds(),
overlapRegion,
nonOverlapRegion
};
data.clipBounds.move(-options.offset);
computeOverlapRegions(data, options.transform, false);
ASSERT(nonOverlapRegion.isEmpty());
auto rects = overlapRegion.rects();
static const size_t OverlapRegionConsolidationThreshold = 4;
if (rects.size() > OverlapRegionConsolidationThreshold) {
rects.clear();
rects.append(overlapRegion.bounds());
}
return rects;
}
void TextureMapperLayer::paintSelfChildrenFilterAndMask(TextureMapperPaintOptions& options)
{
IntSize maxTextureSize = options.textureMapper.maxTextureSize();
for (auto& rect : computeConsolidatedOverlapRegionRects(options)) {
for (int x = rect.x(); x < rect.maxX(); x += maxTextureSize.width()) {
for (int y = rect.y(); y < rect.maxY(); y += maxTextureSize.height()) {
IntRect tileRect(IntPoint(x, y), maxTextureSize);
tileRect.intersect(rect);
paintSelfAndChildrenWithIntermediateSurface(options, tileRect);
}
}
}
}
void TextureMapperLayer::applyMask(TextureMapperPaintOptions& options)
{
options.textureMapper.setMaskMode(true);
paintSelf(options);
options.textureMapper.setMaskMode(false);
}
void TextureMapperLayer::paintIntoSurface(TextureMapperPaintOptions& options)
{
options.textureMapper.bindSurface(options.surface.get());
if (m_isBackdrop) {
SetForScope scopedTransform(options.transform, TransformationMatrix());
SetForScope scopedReplicaLayer(options.replicaLayer, nullptr);
SetForScope scopedBackdropLayer(options.backdropLayer, this);
backdropRootLayer().paintSelfAndChildren(options);
} else
paintSelfAndChildren(options);
bool hasMask = !!m_state.maskLayer;
bool hasReplicaMask = options.replicaLayer == this && m_state.replicaLayer->m_state.maskLayer;
bool defersLastFilterPass = !hasMask && !hasReplicaMask;
options.surface = options.textureMapper.applyFilters(options.surface, m_currentFilters, defersLastFilterPass);
options.textureMapper.bindSurface(options.surface.get());
if (hasMask)
m_state.maskLayer->applyMask(options);
if (hasReplicaMask)
m_state.replicaLayer->m_state.maskLayer->applyMask(options);
}
static void commitSurface(TextureMapperPaintOptions& options, BitmapTexture& surface, const IntRect& rect, float opacity)
{
IntRect targetRect(rect);
targetRect.move(options.offset);
options.textureMapper.bindSurface(options.surface.get());
options.textureMapper.drawTexture(surface, targetRect, { }, opacity);
}
void TextureMapperLayer::paintWithIntermediateSurface(TextureMapperPaintOptions& options, const IntRect& rect)
{
auto surface = options.textureMapper.acquireTextureFromPool(rect.size(), { BitmapTexture::Flags::SupportsAlpha });
{
SetForScope scopedSurface(options.surface, surface.ptr());
SetForScope scopedOffset(options.offset, -toIntSize(rect.location()));
SetForScope scopedOpacity(options.opacity, 1);
options.textureMapper.bindSurface(options.surface.get());
paintSelfChildrenReplicaFilterAndMask(options);
}
commitSurface(options, surface.get(), rect, options.opacity);
}
void TextureMapperLayer::paintSelfAndChildrenWithIntermediateSurface(TextureMapperPaintOptions& options, const IntRect& rect)
{
auto surface = options.textureMapper.acquireTextureFromPool(rect.size(), { BitmapTexture::Flags::SupportsAlpha });
{
SetForScope scopedSurface(options.surface, surface.ptr());
SetForScope scopedOffset(options.offset, -toIntSize(rect.location()));
SetForScope scopedOpacity(options.opacity, 1);
paintIntoSurface(options);
surface = Ref { *options.surface };
}
commitSurface(options, surface.get(), rect, options.opacity);
}
void TextureMapperLayer::paintSelfChildrenReplicaFilterAndMask(TextureMapperPaintOptions& options)
{
bool hasFilterOrMask = [&] {
if (hasFilters())
return true;
if (m_state.maskLayer)
return true;
if (m_state.replicaLayer && m_state.replicaLayer->m_state.maskLayer)
return true;
return false;
}();
if (hasFilterOrMask) {
if (m_state.replicaLayer) {
SetForScope scopedReplicaLayer(options.replicaLayer, this);
SetForScope scopedTransform(options.transform, options.transform);
options.transform.multiply(replicaTransform());
paintSelfChildrenFilterAndMask(options);
}
paintSelfChildrenFilterAndMask(options);
} else
paintSelfAndChildrenWithReplica(options);
}
void TextureMapperLayer::paintRecursive(TextureMapperPaintOptions& options)
{
if (!isVisible())
return;
SetForScope scopedOpacity(options.opacity, options.opacity * m_currentOpacity);
if (preserves3D())
paintWith3DRenderingContext(options);
else if (isFlattened())
paintFlattened(options);
else if (shouldBlend())
paintUsingOverlapRegions(options);
else
paintSelfChildrenReplicaFilterAndMask(options);
}
void TextureMapperLayer::paintFlattened(TextureMapperPaintOptions& options)
{
// If painting a backdrop for a flattened layer, processing should stop here.
// Only the elements up to the flattened layer need to be painted into the backdrop buffer.
if (m_state.backdropLayer && m_state.backdropLayer == options.backdropLayer)
return;
if (m_state.backdropLayer && !options.backdropLayer)
paintBackdrop(options);
if (m_flattenedLayer->needsUpdate()) {
SetForScope scopedOffset(options.offset, IntSize());
SetForScope scopedTransform(options.transform, TransformationMatrix());
SetForScope scopedCombinedTransform(m_layerTransforms.combined, TransformationMatrix());
SetForScope scopedBackdropLayer(m_state.backdropLayer, nullptr);
m_flattenedLayer->update(options, [&](TextureMapperPaintOptions& options) {
paintIntoSurface(options);
});
}
paintSelf(options);
}
void TextureMapperLayer::paintWith3DRenderingContext(TextureMapperPaintOptions& options)
{
Vector<TextureMapperLayer*> layers;
collect3DRenderingContextLayers(layers);
TextureMapperLayer3DRenderingContext context;
context.paint(options.textureMapper, layers, [&](TextureMapperLayer* layer, const ClipPath& clipPath) {
if (!clipPath.isEmpty())
options.textureMapper.beginClip(layer->toSurfaceTransform(), clipPath);
if (layer->preserves3D())
layer->paintSelf(options);
else
layer->paintRecursive(options);
if (!clipPath.isEmpty())
options.textureMapper.endClip();
});
}
void TextureMapperLayer::collect3DRenderingContextLayers(Vector<TextureMapperLayer*>& layers)
{
if (preserves3D() || isLeafOf3DRenderingContext()) {
bool hasVisualContent = m_backingStore || m_contentsLayer
|| (m_state.backgroundColor.isValid() && m_state.backgroundColor.isVisible())
|| (m_state.solidColor.isValid() && m_state.solidColor.isVisible())
|| hasFilters() || hasBackdrop();
// Add layers to 3d rendering context only if they get actually painted.
if (isVisible() && (hasVisualContent || (isLeafOf3DRenderingContext() && !m_children.isEmpty())))
layers.append(this);
// Stop recursion on 3d rendering context leaf
if (isLeafOf3DRenderingContext())
return;
}
for (auto* child : m_children)
child->collect3DRenderingContextLayers(layers);
}
void TextureMapperLayer::setChildren(const Vector<TextureMapperLayer*>& newChildren)
{
removeAllChildren();
for (auto* child : newChildren)
addChild(child);
}
void TextureMapperLayer::addChild(TextureMapperLayer* childLayer)
{
ASSERT(childLayer != this);
if (childLayer->m_parent)
childLayer->removeFromParent();
childLayer->m_parent = this;
m_children.append(childLayer);
}
void TextureMapperLayer::removeFromParent()
{
if (m_parent) {
size_t index = m_parent->m_children.find(this);
ASSERT(index != notFound);
m_parent->m_children.remove(index);
}
m_parent = nullptr;
}
void TextureMapperLayer::removeAllChildren()
{
auto oldChildren = WTFMove(m_children);
for (auto* child : oldChildren)
child->m_parent = nullptr;
}
void TextureMapperLayer::setMaskLayer(TextureMapperLayer* maskLayer)
{
if (maskLayer) {
maskLayer->m_effectTarget = m_isReplica ? m_effectTarget : *this;
m_state.maskLayer = *maskLayer;
} else
m_state.maskLayer = nullptr;
}
void TextureMapperLayer::setReplicaLayer(TextureMapperLayer* replicaLayer)
{
if (replicaLayer) {
replicaLayer->m_isReplica = true;
replicaLayer->m_effectTarget = *this;
m_state.replicaLayer = *replicaLayer;
} else
m_state.replicaLayer = nullptr;
}
void TextureMapperLayer::setBackdropLayer(TextureMapperLayer* backdropLayer)
{
if (backdropLayer) {
backdropLayer->m_isBackdrop = true;
backdropLayer->m_effectTarget = *this;
m_state.backdropLayer = *backdropLayer;
} else
m_state.backdropLayer = nullptr;
}
void TextureMapperLayer::setBackdropFiltersRect(const FloatRoundedRect& backdropFiltersRect)
{
m_state.backdropFiltersRect = backdropFiltersRect;
}
void TextureMapperLayer::setPosition(const FloatPoint& position)
{
m_state.pos = position;
}
void TextureMapperLayer::setBoundsOrigin(const FloatPoint& boundsOrigin)
{
m_state.boundsOrigin = boundsOrigin;
}
void TextureMapperLayer::setSize(const FloatSize& size)
{
#if ENABLE(DAMAGE_TRACKING)
if (canInferDamage() && m_state.size != size) {
// When layer size changes, we damage whole layer for now.
// FIXME: Damage only affected area.
m_inferredDamage.add(m_state.transform.mapRect(FloatRect(FloatPoint::zero(), size)));
}
#endif
m_state.size = size;
}
void TextureMapperLayer::setAnchorPoint(const FloatPoint3D& anchorPoint)
{
m_state.anchorPoint = anchorPoint;
}
void TextureMapperLayer::setPreserves3D(bool preserves3D)
{
m_state.preserves3D = preserves3D;
}
void TextureMapperLayer::setTransform(const TransformationMatrix& transform)
{
m_state.transform = transform;
}
void TextureMapperLayer::setChildrenTransform(const TransformationMatrix& childrenTransform)
{
m_state.childrenTransform = childrenTransform;
}
void TextureMapperLayer::setContentsRect(const FloatRect& contentsRect)
{
m_state.contentsRect = contentsRect;
}
void TextureMapperLayer::setContentsTileSize(const FloatSize& size)
{
m_state.contentsTileSize = size;
}
void TextureMapperLayer::setContentsTilePhase(const FloatSize& phase)
{
m_state.contentsTilePhase = phase;
}
void TextureMapperLayer::setContentsClippingRect(const FloatRoundedRect& contentsClippingRect)
{
m_state.contentsClippingRect = contentsClippingRect;
}
void TextureMapperLayer::setContentsRectClipsDescendants(bool clips)
{
m_state.contentsRectClipsDescendants = clips;
}
void TextureMapperLayer::setMasksToBounds(bool masksToBounds)
{
m_state.masksToBounds = masksToBounds;
}
void TextureMapperLayer::setDrawsContent(bool drawsContent)
{
m_state.drawsContent = drawsContent;
}
void TextureMapperLayer::setContentsVisible(bool contentsVisible)
{
m_state.contentsVisible = contentsVisible;
}
void TextureMapperLayer::setContentsOpaque(bool contentsOpaque)
{
m_state.contentsOpaque = contentsOpaque;
}
void TextureMapperLayer::setBackfaceVisibility(bool backfaceVisibility)
{
m_state.backfaceVisibility = backfaceVisibility;
}
void TextureMapperLayer::setOpacity(float opacity)
{
m_state.opacity = opacity;
}
void TextureMapperLayer::setSolidColor(const Color& color)
{
m_state.solidColor = color;
}
void TextureMapperLayer::setBackgroundColor(const Color& color)
{
m_state.backgroundColor = color;
}
void TextureMapperLayer::setFilters(const FilterOperations& filters)
{
m_state.filters = filters;
}
void TextureMapperLayer::setContentsLayer(TextureMapperPlatformLayer* platformLayer)
{
m_contentsLayer = platformLayer;
}
void TextureMapperLayer::setAnimations(const TextureMapperAnimations& animations)
{
m_animations = animations;
}
void TextureMapperLayer::setBackingStore(TextureMapperBackingStore* backingStore)
{
m_backingStore = backingStore;
}
#if USE(COORDINATED_GRAPHICS)
void TextureMapperLayer::setAnimatedBackingStoreClient(CoordinatedAnimatedBackingStoreClient* client)
{
m_animatedBackingStoreClient = client;
}
#endif
bool TextureMapperLayer::descendantsOrSelfHaveRunningAnimations() const
{
if (m_animations.hasRunningAnimations())
return true;
return std::any_of(m_children.begin(), m_children.end(),
[](TextureMapperLayer* child) {
return child->descendantsOrSelfHaveRunningAnimations();
});
}
bool TextureMapperLayer::applyAnimationsRecursively(MonotonicTime time)
{
bool hasRunningAnimations = syncAnimations(time);
if (m_state.replicaLayer)
hasRunningAnimations |= m_state.replicaLayer->applyAnimationsRecursively(time);
if (m_state.backdropLayer)
hasRunningAnimations |= m_state.backdropLayer->syncAnimations(time);
for (auto* child : m_children)
hasRunningAnimations |= child->applyAnimationsRecursively(time);
return hasRunningAnimations;
}
bool TextureMapperLayer::syncAnimations(MonotonicTime time)
{
TextureMapperAnimation::ApplicationResult applicationResults;
m_animations.apply(applicationResults, time);
m_layerTransforms.localTransform = applicationResults.transform.value_or(m_state.transform);
m_currentOpacity = applicationResults.opacity.value_or(m_state.opacity);
m_currentFilters = applicationResults.filters.value_or(m_state.filters);
#if USE(COORDINATED_GRAPHICS)
// Calculate localTransform 50ms in the future.
TextureMapperAnimation::ApplicationResult futureApplicationResults;
m_animations.apply(futureApplicationResults, time + 50_ms, TextureMapperAnimation::KeepInternalState::Yes);
m_layerTransforms.futureLocalTransform = futureApplicationResults.transform.value_or(m_layerTransforms.localTransform);
#endif
return applicationResults.hasRunningAnimations;
}
FloatRect TextureMapperLayer::effectiveLayerRect() const
{
if (isFlattened())
return m_flattenedLayer->layerRect();
if (isLeafOf3DRenderingContext() && !m_children.isEmpty()) {
// If leaf has children but no 3D transformation then it doesn't get flattened surface.
// However, from 3D rendering context/BSP calculation point of view the effective
// layer area is still the same as it would be flattened.
Region layerRegion;
computeFlattenedRegion(layerRegion, true);
return layerRegion.bounds();
}
return layerRect();
}
}
|