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
|
/*
* Copyright (C) 2025 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "LargestContentfulPaintData.h"
#include "CachedImage.h"
#include "ContainerNodeInlines.h"
#include "DocumentView.h"
#include "ElementInlines.h"
#include "FloatQuad.h"
#include "LargestContentfulPaint.h"
#include "LegacyRenderSVGImage.h"
#include "LocalDOMWindow.h"
#include "LocalFrameView.h"
#include "Logging.h"
#include "Page.h"
#include "Performance.h"
#include "RenderBlockFlow.h"
#include "RenderBox.h"
#include "RenderElementInlines.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderLayerInlines.h"
#include "RenderLineBreak.h"
#include "RenderObjectInlines.h"
#include "RenderReplaced.h"
#include "RenderSVGImage.h"
#include "RenderText.h"
#include "RenderView.h"
#include "VisibleRectContext.h"
#include <wtf/CheckedRef.h>
#include <wtf/Ref.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
WTF_MAKE_STRUCT_TZONE_ALLOCATED_IMPL(ElementLargestContentfulPaintData);
WTF_MAKE_TZONE_ALLOCATED_IMPL(LargestContentfulPaintData);
LargestContentfulPaintData::LargestContentfulPaintData() = default;
LargestContentfulPaintData::~LargestContentfulPaintData() = default;
// https://w3c.github.io/paint-timing/#exposed-for-paint-timing
bool LargestContentfulPaintData::isExposedForPaintTiming(const Element& element)
{
if (!element.protectedDocument()->isFullyActive())
return false;
if (!element.isInDocumentTree()) // Also checks isConnected().
return false;
return true;
}
// https://w3c.github.io/largest-contentful-paint/#largest-contentful-paint-candidate
bool LargestContentfulPaintData::isEligibleForLargestContentfulPaint(const Element& element, float effectiveVisualArea)
{
CheckedPtr renderer = element.renderer();
if (!renderer)
return false;
if (renderer->style().isEffectivelyTransparent())
return false;
// FIXME: Need to implement the response length vs. image size logic: webkit.org/b/299558.
UNUSED_PARAM(effectiveVisualArea);
return true;
}
bool LargestContentfulPaintData::canCompareWithLargestPaintArea(const Element& element)
{
CheckedPtr renderer = element.renderer();
if (!renderer)
return false;
CheckedPtr layer = renderer->enclosingLayer();
if (!layer)
return false;
// An ancestor transform may scale the rect.
if (layer->isTransformed() || layer->hasTransformedAncestor())
return false;
// Other properties like clipping on ancestors can only ever shrink the area, so it's safe to compare.
return true;
}
// https://w3c.github.io/largest-contentful-paint/#sec-effective-visual-size
std::optional<float> LargestContentfulPaintData::effectiveVisualArea(const Element& element, CachedImage* image, FloatRect imageLocalRect, FloatRect intersectionRect, FloatSize viewportSize)
{
RefPtr frameView = element.document().view();
if (!frameView)
return { };
auto area = intersectionRect.area();
if (area >= viewportSize.area())
return { };
if (image) {
CheckedPtr renderer = element.renderer();
if (!renderer)
return { };
auto absoluteContentRect = renderer->localToAbsoluteQuad(FloatRect(imageLocalRect)).boundingBox();
auto intersectingContentRect = intersection(absoluteContentRect, intersectionRect);
area = intersectingContentRect.area();
auto naturalSize = image->imageSizeForRenderer(renderer.get(), 1);
if (naturalSize.isEmpty())
return { };
auto scaleFactor = absoluteContentRect.area() / FloatSize { naturalSize }.area();
if (scaleFactor > 1)
area /= scaleFactor;
return area;
}
return area;
}
// https://w3c.github.io/largest-contentful-paint/#sec-add-lcp-entry
void LargestContentfulPaintData::potentiallyAddLargestContentfulPaintEntry(Element& element, CachedImage* image, FloatRect imageLocalRect, FloatRect intersectionRect, MonotonicTime loadTime, DOMHighResTimeStamp paintTimestamp, std::optional<FloatSize>& viewportSize)
{
if (!image) {
// For text we have to accumulate rectangles for a single element from possibly multiple text boxes, so we can only mark an element as being in the content set after all the painting is done.
ASSERT(!element.isInLargestContentfulPaintTextContentSet());
element.setInLargestContentfulPaintTextContentSet();
}
LOG_WITH_STREAM(LargestContentfulPaint, stream << "LargestContentfulPaintData " << this << " potentiallyAddLargestContentfulPaintEntry() " << element << " image " << (image ? image->url().string() : emptyString()) << " rect " << intersectionRect);
if (intersectionRect.isEmpty())
return;
if (canCompareWithLargestPaintArea(element) && intersectionRect.area() <= m_largestPaintArea)
return;
Ref document = element.document();
RefPtr window = document->window();
if (!window)
return;
RefPtr view = document->view();
if (!view)
return;
// The spec talks about trusted scroll events, but the intent is to detect user scrolls: https://github.com/w3c/largest-contentful-paint/issues/105
if ((view->wasEverScrolledExplicitlyByUser() || window->hasDispatchedInputEvent()))
return;
if (!viewportSize)
viewportSize = FloatSize { view->visualViewportRect().size() };
auto elementArea = effectiveVisualArea(element, image, imageLocalRect, intersectionRect, *viewportSize);
if (!elementArea)
return;
if (*elementArea <= m_largestPaintArea) {
LOG_WITH_STREAM(LargestContentfulPaint, stream << " element area " << elementArea << " less than LCP " << m_largestPaintArea);
return;
}
if (!isEligibleForLargestContentfulPaint(element, *elementArea))
return;
m_largestPaintArea = *elementArea;
Ref pendingEntry = LargestContentfulPaint::create(0);
pendingEntry->setElement(&element);
pendingEntry->setSize(std::round<unsigned>(m_largestPaintArea));
if (image) {
pendingEntry->setURLString(image->url().string());
auto loadTimestamp = window->protectedPerformance()->relativeTimeFromTimeOriginInReducedResolution(loadTime);
pendingEntry->setLoadTime(loadTimestamp);
}
if (element.hasID())
pendingEntry->setID(element.getIdAttribute().string());
pendingEntry->setRenderTime(paintTimestamp);
LOG_WITH_STREAM(LargestContentfulPaint, stream << " making new entry for " << element << " image " << (image ? image->url().string() : emptyString()) << " id " << pendingEntry->id() <<
": entry size " << pendingEntry->size() << ", loadTime " << pendingEntry->loadTime() << ", renderTime " << pendingEntry->renderTime());
m_pendingEntry = RefPtr { WTFMove(pendingEntry) };
}
// https://w3c.github.io/largest-contentful-paint/#sec-report-largest-contentful-paint
RefPtr<LargestContentfulPaint> LargestContentfulPaintData::generateLargestContentfulPaintEntry(DOMHighResTimeStamp paintTimestamp)
{
std::optional<FloatSize> viewportSize;
auto imageRecords = std::exchange(m_pendingImageRecords, { });
for (auto [weakElement, imageList] : imageRecords) {
RefPtr element = weakElement;
if (!element)
continue;
auto& lcpData = element->ensureLargestContentfulPaintData();
// FIXME: This is doing multiple localToAbsolute on the same element, but multiple images per element is rare.
for (auto image : imageList) {
if (!image)
continue;
auto findIndex = lcpData.imageData.findIf([&](auto& value) {
return image == value.image;
});
if (findIndex == notFound)
continue;
auto& imageData = lcpData.imageData[findIndex];
if (imageData.rect.isEmpty())
continue;
auto intersectionRect = computeViewportIntersectionRect(*element, imageData.rect);
auto loadTimeSeconds = imageData.loadTime ? *imageData.loadTime : MonotonicTime::now();
potentiallyAddLargestContentfulPaintEntry(*element, image.get(), imageData.rect, intersectionRect, loadTimeSeconds, paintTimestamp, viewportSize);
}
}
auto textRecords = std::exchange(m_paintedTextRecords, { });
for (RefPtr element : textRecords) {
if (!element)
continue;
auto rect = element->ensureLargestContentfulPaintData().accumulatedTextRect;
if (canCompareWithLargestPaintArea(*element) && rect.area() <= m_largestPaintArea)
continue;
auto intersectionRect = computeViewportIntersectionRect(*element, rect);
potentiallyAddLargestContentfulPaintEntry(*element, nullptr, { }, intersectionRect, { }, paintTimestamp, viewportSize);
}
m_haveNewCandidate = false;
return std::exchange(m_pendingEntry, nullptr);
}
// This is a simplified version of IntersectionObserver::computeIntersectionState(). Some code should be shared.
FloatRect LargestContentfulPaintData::computeViewportIntersectionRect(Element& element, FloatRect localRect)
{
RefPtr frameView = element.document().view();
if (!frameView)
return { };
CheckedPtr targetRenderer = element.renderer();
if (!targetRenderer)
return { };
if (targetRenderer->isSkippedContent())
return { };
CheckedPtr rootRenderer = frameView->renderView();
auto layoutViewport = frameView->layoutViewportRect();
auto localTargetBounds = LayoutRect { localRect };
auto absoluteRects = targetRenderer->computeVisibleRectsInContainer(
{ localTargetBounds },
&targetRenderer->checkedView().get(),
{
.hasPositionFixedDescendant = false,
.dirtyRectIsFlipped = false,
.options = {
VisibleRectContext::Option::UseEdgeInclusiveIntersection,
VisibleRectContext::Option::ApplyCompositedClips,
VisibleRectContext::Option::ApplyCompositedContainerScrolls
},
}
);
if (!absoluteRects)
return { };
auto intersectionRect = layoutViewport;
intersectionRect.edgeInclusiveIntersect(absoluteRects->clippedOverflowRect);
return intersectionRect;
}
FloatRect LargestContentfulPaintData::computeViewportIntersectionRectForTextContainer(Element& element, const WeakHashSet<Text, WeakPtrImplWithEventTargetData>& textNodes)
{
RefPtr frameView = element.document().view();
if (!frameView)
return { };
CheckedPtr rootRenderer = frameView->renderView();
auto layoutViewport = frameView->layoutViewportRect();
IntRect absoluteTextBounds;
for (RefPtr node : textNodes) {
if (!node)
continue;
CheckedPtr renderer = node->renderer();
if (!renderer)
continue;
if (renderer->isSkippedContent())
continue;
static constexpr bool useTransforms = true;
auto absoluteBounds = renderer->absoluteBoundingBoxRect(useTransforms);
absoluteTextBounds.unite(absoluteBounds);
}
auto intersectionRect = layoutViewport;
intersectionRect.edgeInclusiveIntersect(absoluteTextBounds);
return intersectionRect;
}
void LargestContentfulPaintData::didLoadImage(Element& element, CachedImage* image)
{
if (!image)
return;
// `loadTime` isn't interesting for a data URI, so let's avoid the overhead of tracking it.
if (image->url().protocolIsData())
return;
LOG_WITH_STREAM(LargestContentfulPaint, stream << "LargestContentfulPaintData " << this << " didLoadImage() " << element << " image " << (image ? image->url().string() : emptyString()));
auto& lcpData = element.ensureLargestContentfulPaintData();
auto findIndex = lcpData.imageData.findIf([&](auto& value) {
return image == value.image;
});
if (findIndex != notFound && lcpData.imageData[findIndex].inContentSet)
return;
if (!isExposedForPaintTiming(element))
return;
auto now = MonotonicTime::now();
if (findIndex == notFound) {
auto imageData = PerElementImageData { *image, { }, now };
lcpData.imageData.append(WTFMove(imageData));
} else
lcpData.imageData[findIndex].loadTime = now;
}
void LargestContentfulPaintData::didPaintImage(Element& element, CachedImage* image, FloatRect localRect)
{
LOG_WITH_STREAM(LargestContentfulPaint, stream << "LargestContentfulPaintData " << this << " didPaintImage() " << element << " image " << (image ? image->url().string() : emptyString()) << " localRect " << localRect);
if (!image)
return;
auto& lcpData = element.ensureLargestContentfulPaintData();
auto findIndex = lcpData.imageData.findIf([&](auto& value) {
return image == value.image;
});
if (findIndex == notFound) {
findIndex = lcpData.imageData.size();
auto imageData = PerElementImageData { *image, { }, MonotonicTime::now() };
lcpData.imageData.append(WTFMove(imageData));
}
auto& imageData = lcpData.imageData[findIndex];
if (imageData.inContentSet)
return;
imageData.inContentSet = true;
if (localRect.isEmpty())
return;
if (canCompareWithLargestPaintArea(element) && localRect.area() <= m_largestPaintArea)
return;
if (!isExposedForPaintTiming(element))
return;
if (!imageData.loadTime)
imageData.loadTime = MonotonicTime::now();
if (localRect.area() > imageData.rect.area())
imageData.rect = localRect;
m_pendingImageRecords.ensure(element, [] {
return Vector<WeakPtr<CachedImage>> { };
}).iterator->value.append(*image);
scheduleRenderingUpdateIfNecessary(element);
}
void LargestContentfulPaintData::didPaintText(const RenderBlockFlow& formattingContextRoot, FloatRect localRect, bool isOnlyTextBoxForElement)
{
if (localRect.isEmpty())
return;
auto& renderBlockFlow = const_cast<RenderBlockFlow&>(formattingContextRoot);
// https://w3c.github.io/paint-timing/#sec-modifications-dom says to get the containing block.
CheckedPtr<RenderBlock> containingBlock = &renderBlockFlow;
if (containingBlock->isAnonymous()) {
CheckedPtr ancestor = containingBlock->firstNonAnonymousAncestor();
if (CheckedPtr ancestorBlock = dynamicDowncast<RenderBlock>(ancestor.get()))
containingBlock = ancestorBlock;
else
containingBlock = containingBlock->containingBlock();
}
if (!containingBlock)
return;
RefPtr element = containingBlock->element();
if (!element)
return;
if (element->isInLargestContentfulPaintTextContentSet())
return;
if (isOnlyTextBoxForElement && canCompareWithLargestPaintArea(*element) && localRect.area() <= m_largestPaintArea)
return;
if (!isExposedForPaintTiming(*element))
return;
if (containingBlock != &formattingContextRoot)
localRect = formattingContextRoot.localToContainerQuad({ localRect }, containingBlock.get()).boundingBox();
element->ensureLargestContentfulPaintData().accumulatedTextRect.unite(localRect);
m_paintedTextRecords.add(*element);
scheduleRenderingUpdateIfNecessary(*element);
}
void LargestContentfulPaintData::scheduleRenderingUpdateIfNecessary(Element& element)
{
if (m_haveNewCandidate)
return;
m_haveNewCandidate = true;
if (RefPtr page = element.document().page())
page->scheduleRenderingUpdate(RenderingUpdateStep::PaintTiming);
}
} // namespace WebCore
|