| File: | Volumes/Data/worker/Apple-iOS-26-Safer-CPP-Checks-EWS/build/Source/WebCore/rendering/svg/legacy/LegacyRenderSVGImage.cpp |
| Warning: | line 299, column 15 Local variable 'image' is uncounted and unsafe |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * Copyright (C) 2006 Alexander Kellett <lypanov@kde.org> |
| 3 | * Copyright (C) 2006 Apple Inc. All rights reserved. |
| 4 | * Copyright (C) 2007 Nikolas Zimmermann <zimmermann@kde.org> |
| 5 | * Copyright (C) 2007, 2008, 2009 Rob Buis <buis@kde.org> |
| 6 | * Copyright (C) 2009 Google, Inc. |
| 7 | * Copyright (C) 2009 Dirk Schulze <krit@webkit.org> |
| 8 | * Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com> |
| 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Library General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Library General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Library General Public License |
| 21 | * along with this library; see the file COPYING.LIB. If not, write to |
| 22 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 23 | * Boston, MA 02110-1301, USA. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "LegacyRenderSVGImage.h" |
| 28 | |
| 29 | #include "FloatQuad.h" |
| 30 | #include "GraphicsContext.h" |
| 31 | #include "HitTestResult.h" |
| 32 | #include "ImageQualityController.h" |
| 33 | #include "LayoutRepainter.h" |
| 34 | #include "LegacyRenderSVGResource.h" |
| 35 | #include "PointerEventsHitRules.h" |
| 36 | #include "RenderImageResource.h" |
| 37 | #include "RenderObjectInlines.h" |
| 38 | #include "RenderLayer.h" |
| 39 | #include "SVGElementTypeHelpers.h" |
| 40 | #include "SVGImageElement.h" |
| 41 | #include "SVGRenderingContext.h" |
| 42 | #include "SVGResources.h" |
| 43 | #include "SVGResourcesCache.h" |
| 44 | #include "SVGVisitedRendererTracking.h" |
| 45 | #include <wtf/StackStats.h> |
| 46 | #include <wtf/TZoneMallocInlines.h> |
| 47 | |
| 48 | namespace WebCore { |
| 49 | |
| 50 | WTF_MAKE_TZONE_ALLOCATED_IMPL(LegacyRenderSVGImage)::bmalloc::api::HeapRef LegacyRenderSVGImage::s_heapRef; const TZoneSpecification LegacyRenderSVGImage::s_heapSpec = { & LegacyRenderSVGImage::s_heapRef, TZoneSpecification::encodeSize <LegacyRenderSVGImage>(), TZoneSpecification::encodeAlignment <LegacyRenderSVGImage>(), TZoneSpecification::encodeCategory <LegacyRenderSVGImage>(), ::bmalloc::api::compactAllocationMode <LegacyRenderSVGImage>(), TZoneSpecification::encodeDescriptor <LegacyRenderSVGImage>(), }; void* LegacyRenderSVGImage ::operatorNewSlow(size_t size) { if constexpr (::bmalloc::api ::compactAllocationMode<LegacyRenderSVGImage>() == CompactAllocationMode ::Compact) return ::bmalloc::api::tzoneAllocateCompactSlow(size , s_heapSpec); return ::bmalloc::api::tzoneAllocateNonCompactSlow (size, s_heapSpec); } using __makeBtzoneMallocedInlineMacroSemicolonifier __attribute__((unused)) = int; |
| 51 | |
| 52 | LegacyRenderSVGImage::LegacyRenderSVGImage(SVGImageElement& element, RenderStyle&& style) |
| 53 | : LegacyRenderSVGModelObject(Type::LegacySVGImage, element, WTF::move(style), SVGModelObjectFlag::UsesBoundaryCaching) |
| 54 | , m_needsBoundariesUpdate(true) |
| 55 | , m_needsTransformUpdate(true) |
| 56 | , m_imageResource(makeUniqueRef<RenderImageResource>()) |
| 57 | { |
| 58 | imageResource().initialize(*this); |
| 59 | ASSERT(isLegacyRenderSVGImage())((void)0); |
| 60 | } |
| 61 | |
| 62 | LegacyRenderSVGImage::~LegacyRenderSVGImage() = default; |
| 63 | |
| 64 | void LegacyRenderSVGImage::notifyFinished(CachedResource& newImage, const NetworkLoadMetrics& metrics, LoadWillContinueInAnotherProcess loadWillContinueInAnotherProcess) |
| 65 | { |
| 66 | if (renderTreeBeingDestroyed()) |
| 67 | return; |
| 68 | |
| 69 | if (RefPtr image = dynamicDowncast<SVGImageElement>(LegacyRenderSVGModelObject::element())) |
| 70 | page().didFinishLoadingImageForSVGImage(*image); |
| 71 | |
| 72 | LegacyRenderSVGModelObject::notifyFinished(newImage, metrics, loadWillContinueInAnotherProcess); |
| 73 | } |
| 74 | |
| 75 | void LegacyRenderSVGImage::willBeDestroyed() |
| 76 | { |
| 77 | imageResource().willBeDestroyed(); |
| 78 | LegacyRenderSVGModelObject::willBeDestroyed(); |
| 79 | } |
| 80 | |
| 81 | SVGImageElement& LegacyRenderSVGImage::imageElement() const |
| 82 | { |
| 83 | return downcast<SVGImageElement>(LegacyRenderSVGModelObject::element()); |
| 84 | } |
| 85 | |
| 86 | FloatRect LegacyRenderSVGImage::calculateObjectBoundingBox() const |
| 87 | { |
| 88 | LayoutSize intrinsicSize; |
| 89 | if (CachedImage* cachedImage = imageResource().cachedImage()) |
| 90 | intrinsicSize = cachedImage->imageSizeForRenderer(nullptr, style().usedZoom()); |
| 91 | |
| 92 | Ref imageElement = this->imageElement(); |
| 93 | SVGLengthContext lengthContext(imageElement.ptr()); |
| 94 | |
| 95 | CheckedRef style = this->style(); |
| 96 | auto& width = style->width(); |
| 97 | auto& height = style->height(); |
| 98 | auto usedZoom = style->usedZoomForLength(); |
| 99 | |
| 100 | float concreteWidth; |
| 101 | if (!width.isAuto()) |
| 102 | concreteWidth = lengthContext.valueForLength(width, usedZoom, SVGLengthMode::Width); |
| 103 | else if (!height.isAuto() && !intrinsicSize.isEmpty()) |
| 104 | concreteWidth = lengthContext.valueForLength(height, usedZoom, SVGLengthMode::Height) * intrinsicSize.width() / intrinsicSize.height(); |
| 105 | else |
| 106 | concreteWidth = intrinsicSize.width(); |
| 107 | |
| 108 | float concreteHeight; |
| 109 | if (!height.isAuto()) |
| 110 | concreteHeight = lengthContext.valueForLength(height, usedZoom, SVGLengthMode::Height); |
| 111 | else if (!width.isAuto() && !intrinsicSize.isEmpty()) |
| 112 | concreteHeight = lengthContext.valueForLength(width, usedZoom, SVGLengthMode::Width) * intrinsicSize.height() / intrinsicSize.width(); |
| 113 | else |
| 114 | concreteHeight = intrinsicSize.height(); |
| 115 | |
| 116 | return { imageElement->x().value(lengthContext), imageElement->y().value(lengthContext), concreteWidth, concreteHeight }; |
| 117 | } |
| 118 | |
| 119 | bool LegacyRenderSVGImage::updateImageViewport() |
| 120 | { |
| 121 | FloatRect oldBoundaries = m_objectBoundingBox; |
| 122 | m_objectBoundingBox = calculateObjectBoundingBox(); |
| 123 | |
| 124 | bool updatedViewport = false; |
| 125 | URL imageSourceURL = document().completeURL(imageElement().imageSourceURL()); |
| 126 | |
| 127 | // Images with preserveAspectRatio=none should force non-uniform scaling. This can be achieved |
| 128 | // by setting the image's container size to its intrinsic size. |
| 129 | // See: http://www.w3.org/TR/SVG/single-page.html, 7.8 The ‘preserveAspectRatio’ attribute. |
| 130 | if (imageElement().preserveAspectRatio().align() == SVGPreserveAspectRatioValue::SVG_PRESERVEASPECTRATIO_NONE) { |
| 131 | if (CachedImage* cachedImage = imageResource().cachedImage()) { |
| 132 | LayoutSize intrinsicSize = cachedImage->imageSizeForRenderer(nullptr, style().usedZoom()); |
| 133 | if (intrinsicSize != imageResource().imageSize(style().usedZoom())) { |
| 134 | imageResource().setContainerContext(roundedIntSize(intrinsicSize), imageSourceURL); |
| 135 | updatedViewport = true; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (oldBoundaries != m_objectBoundingBox) { |
| 141 | if (!updatedViewport) |
| 142 | imageResource().setContainerContext(enclosingIntRect(m_objectBoundingBox).size(), imageSourceURL); |
| 143 | updatedViewport = true; |
| 144 | m_needsBoundariesUpdate = true; |
| 145 | } |
| 146 | |
| 147 | return updatedViewport; |
| 148 | } |
| 149 | |
| 150 | void LegacyRenderSVGImage::layout() |
| 151 | { |
| 152 | StackStats::LayoutCheckPoint layoutCheckPoint; |
| 153 | ASSERT(needsLayout())((void)0); |
| 154 | |
| 155 | auto checkForRepaintOverride = !selfNeedsLayout() ? LayoutRepainter::CheckForRepaint::No : SVGRenderSupport::checkForSVGRepaintDuringLayout(*this); |
| 156 | LayoutRepainter repainter(*this, checkForRepaintOverride, { }, RepaintOutlineBounds::No); |
| 157 | updateImageViewport(); |
| 158 | |
| 159 | bool transformOrBoundariesUpdate = m_needsTransformUpdate || m_needsBoundariesUpdate; |
| 160 | if (m_needsTransformUpdate) { |
| 161 | m_localTransform = imageElement().animatedLocalTransform(); |
| 162 | m_needsTransformUpdate = false; |
| 163 | } |
| 164 | |
| 165 | if (m_needsBoundariesUpdate) { |
| 166 | m_repaintBoundingBox = m_objectBoundingBox; |
| 167 | SVGRenderSupport::intersectRepaintRectWithResources(*this, m_repaintBoundingBox); |
| 168 | m_needsBoundariesUpdate = false; |
| 169 | } |
| 170 | |
| 171 | // Invalidate all resources of this client if our layout changed. |
| 172 | if (everHadLayout() && selfNeedsLayout()) |
| 173 | SVGResourcesCache::clientLayoutChanged(*this); |
| 174 | |
| 175 | // If our bounds changed, notify the parents. |
| 176 | if (transformOrBoundariesUpdate) { |
| 177 | if (CheckedPtr parent = this->parent()) |
| 178 | parent->invalidateCachedBoundaries(); |
| 179 | } |
| 180 | |
| 181 | repainter.repaintAfterLayout(); |
| 182 | clearNeedsLayout(); |
| 183 | } |
| 184 | |
| 185 | void LegacyRenderSVGImage::paint(PaintInfo& paintInfo, const LayoutPoint&) |
| 186 | { |
| 187 | if (paintInfo.context().paintingDisabled() || paintInfo.phase != PaintPhase::Foreground |
| 188 | || style().usedVisibility() == Visibility::Hidden || !imageResource().cachedImage()) |
| 189 | return; |
| 190 | |
| 191 | FloatRect boundingBox = repaintRectInLocalCoordinates(); |
| 192 | if (!SVGRenderSupport::paintInfoIntersectsRepaintRect(boundingBox, m_localTransform, paintInfo)) |
| 193 | return; |
| 194 | |
| 195 | PaintInfo childPaintInfo(paintInfo); |
| 196 | GraphicsContextStateSaver stateSaver(childPaintInfo.context()); |
| 197 | childPaintInfo.applyTransform(m_localTransform); |
| 198 | |
| 199 | if (childPaintInfo.phase == PaintPhase::Foreground) { |
| 200 | SVGRenderingContext renderingContext(*this, childPaintInfo); |
| 201 | |
| 202 | if (renderingContext.isRenderingPrepared()) { |
| 203 | if (style().bufferedRendering() == BufferedRendering::Static && renderingContext.bufferForeground(m_bufferedForeground)) |
| 204 | return; |
| 205 | |
| 206 | paintForeground(childPaintInfo); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if (style().usedOutlineWidth()) |
| 211 | paintOutline(childPaintInfo, IntRect(boundingBox)); |
| 212 | } |
| 213 | |
| 214 | void LegacyRenderSVGImage::paintForeground(PaintInfo& paintInfo) |
| 215 | { |
| 216 | RefPtr<Image> image = imageResource().image(); |
| 217 | if (!image) |
| 218 | return; |
| 219 | |
| 220 | FloatRect destRect = m_objectBoundingBox; |
| 221 | FloatRect srcRect(0, 0, image->width(), image->height()); |
| 222 | |
| 223 | imageElement().preserveAspectRatio().transformRect(destRect, srcRect); |
| 224 | |
| 225 | ImagePaintingOptions options = { |
| 226 | imageOrientation(), |
| 227 | ImageQualityController::chooseInterpolationQualityForSVG(paintInfo.context(), *this, *image), |
| 228 | paintInfo.paintBehavior.contains(PaintBehavior::DrawsHDRContent) ? DrawsHDRContent::Yes : DrawsHDRContent::No, |
| 229 | style().dynamicRangeLimit().toPlatformDynamicRangeLimit() |
| 230 | }; |
| 231 | |
| 232 | auto& context = paintInfo.context(); |
| 233 | context.drawImage(*image, destRect, srcRect, options); |
| 234 | |
| 235 | auto* cachedImage = imageResource().cachedImage(); |
| 236 | if (cachedImage && !context.paintingDisabled()) |
| 237 | protect(document())->didPaintImage(imageElement(), cachedImage, destRect); |
| 238 | } |
| 239 | |
| 240 | void LegacyRenderSVGImage::invalidateBufferedForeground() |
| 241 | { |
| 242 | m_bufferedForeground = nullptr; |
| 243 | } |
| 244 | |
| 245 | bool LegacyRenderSVGImage::nodeAtFloatPoint(const HitTestRequest& request, HitTestResult& result, const FloatPoint& pointInParent, HitTestAction hitTestAction) |
| 246 | { |
| 247 | // We only draw in the forground phase, so we only hit-test then. |
| 248 | if (hitTestAction != HitTestForeground) |
| 249 | return false; |
| 250 | |
| 251 | PointerEventsHitRules hitRules(PointerEventsHitRules::HitTestingTargetType::SVGImage, request, usedPointerEvents()); |
| 252 | if (request.isVisibleForStyle(style()) || !hitRules.requireVisible) { |
| 253 | static NeverDestroyed<SVGVisitedRendererTracking::VisitedSet> s_visitedSet; |
| 254 | |
| 255 | SVGVisitedRendererTracking recursionTracking(s_visitedSet); |
| 256 | if (recursionTracking.isVisiting(*this)) |
| 257 | return false; |
| 258 | |
| 259 | SVGVisitedRendererTracking::Scope recursionScope(recursionTracking, *this); |
| 260 | |
| 261 | FloatPoint localPoint = valueOrDefault(localToParentTransform().inverse()).mapPoint(pointInParent); |
| 262 | if (!SVGRenderSupport::pointInClippingArea(*this, localPoint)) |
| 263 | return false; |
| 264 | |
| 265 | if (hitRules.canHitFill) { |
| 266 | if (m_objectBoundingBox.contains(localPoint)) { |
| 267 | updateHitTestResult(result, LayoutPoint(localPoint)); |
| 268 | if (result.addNodeToListBasedTestResult(protect(nodeForHitTest()).get(), request, flooredLayoutPoint(localPoint)) == HitTestProgress::Stop) |
| 269 | return true; |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | void LegacyRenderSVGImage::imageChanged(WrappedImagePtr, const IntRect*) |
| 278 | { |
| 279 | if (!parent()) |
| 280 | return; |
| 281 | // The image resource defaults to nullImage until the resource arrives. |
| 282 | // This empty image may be cached by SVG resources which must be invalidated. |
| 283 | if (auto* resources = SVGResourcesCache::cachedResourcesForRenderer(*this)) |
| 284 | resources->removeClientFromCacheAndMarkForInvalidation(*this); |
| 285 | |
| 286 | // Eventually notify parent resources, that we've changed. |
| 287 | LegacyRenderSVGResource::markForLayoutAndParentResourceInvalidation(*this, false); |
| 288 | |
| 289 | // Update the SVGImageCache sizeAndScales entry in case image loading finished after layout. |
| 290 | // (https://bugs.webkit.org/show_bug.cgi?id=99489) |
| 291 | m_objectBoundingBox = FloatRect(); |
| 292 | if (updateImageViewport()) |
| 293 | setNeedsLayout(); |
| 294 | |
| 295 | invalidateBufferedForeground(); |
| 296 | |
| 297 | repaint(); |
| 298 | |
| 299 | if (auto* image = imageResource().cachedImage(); image && image->currentFrameIsComplete(this)) { |
Local variable 'image' is uncounted and unsafe | |
| 300 | if (auto styleable = Styleable::fromRenderer(*this)) |
| 301 | protect(document())->didLoadImage(protect(styleable->element).get(), image); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void LegacyRenderSVGImage::addFocusRingRects(Vector<LayoutRect>& rects, const LayoutPoint&, const RenderLayerModelObject*) const |
| 306 | { |
| 307 | // this is called from paint() after the localTransform has already been applied |
| 308 | LayoutRect contentRect = LayoutRect(repaintRectInLocalCoordinates()); |
| 309 | if (!contentRect.isEmpty()) |
| 310 | rects.append(contentRect); |
| 311 | } |
| 312 | |
| 313 | } // namespace WebCore |