File: LegacyRenderSVGResource.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (298 lines) | stat: -rw-r--r-- 12,891 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2007 Rob Buis <buis@kde.org>
 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
 * Copyright (C) 2023-2025 Apple Inc. All rights reserved.
 *
 * 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 "LegacyRenderSVGResource.h"

#include "LegacyRenderSVGResourceClipper.h"
#include "LegacyRenderSVGResourceFilter.h"
#include "LegacyRenderSVGResourceMasker.h"
#include "LegacyRenderSVGResourceSolidColor.h"
#include "LegacyRenderSVGRoot.h"
#include "LegacyRenderSVGShape.h"
#include "LocalFrame.h"
#include "LocalFrameView.h"
#include "RenderSVGRoot.h"
#include "RenderSVGShape.h"
#include "RenderView.h"
#include "SVGRenderStyle.h"
#include "SVGResourceElementClient.h"
#include "SVGResources.h"
#include "SVGResourcesCache.h"
#include "SVGURIReference.h"

namespace WebCore {

static inline bool inheritColorFromParentStyleIfNeeded(RenderElement& object, bool applyToFill, Color& color)
{
    if (color.isValid())
        return true;
    if (!object.parent())
        return false;
    Ref parentSVGStyle = object.parent()->style().svgStyle();
    color = object.style().colorResolvingCurrentColor(applyToFill ? parentSVGStyle->fillPaintColor() : parentSVGStyle->strokePaintColor());
    return true;
}

static inline LegacyRenderSVGResource* requestPaintingResource(RenderSVGResourceMode mode, RenderElement& renderer, const RenderStyle& style, Color& fallbackColor)
{
    bool applyToFill = mode == RenderSVGResourceMode::ApplyToFill;

    // When rendering the mask for a LegacyRenderSVGResourceClipper, always use the initial fill paint server.
    if (renderer.view().frameView().paintBehavior().contains(PaintBehavior::RenderingSVGClipOrMask)) {
        // Ignore stroke.
        if (!applyToFill)
            return nullptr;
        
        // But always use the initial fill paint server.
        LegacyRenderSVGResourceSolidColor* colorResource = LegacyRenderSVGResource::sharedSolidPaintingResource();
        colorResource->setColor(SVGRenderStyle::initialFillPaintColor().resolvedColor());
        return colorResource;
    }

    const auto& svgStyle = style.svgStyle();
    auto paintType = applyToFill ? svgStyle.fillPaintType() : svgStyle.strokePaintType();

    // If we have no fill/stroke, return nullptr.
    if (paintType == SVGPaintType::None)
        return nullptr;

    Color color;
    switch (paintType) {
    case SVGPaintType::CurrentColor:
    case SVGPaintType::RGBColor:
    case SVGPaintType::URICurrentColor:
    case SVGPaintType::URIRGBColor:
        color = style.colorResolvingCurrentColor(applyToFill ? svgStyle.fillPaintColor() : svgStyle.strokePaintColor());
        break;
    default:
        break;
    }

    if (style.insideLink() == InsideLink::InsideVisited) {
        // FIXME: This code doesn't support the uri component of the visited link paint, https://bugs.webkit.org/show_bug.cgi?id=70006
        SVGPaintType visitedPaintType = applyToFill ? svgStyle.visitedLinkFillPaintType() : svgStyle.visitedLinkStrokePaintType();

        // For SVGPaintType::CurrentColor, 'color' already contains the 'visitedColor'.
        if (visitedPaintType < SVGPaintType::URINone && visitedPaintType != SVGPaintType::CurrentColor) {
            const Color& visitedColor = style.colorResolvingCurrentColor(applyToFill ? svgStyle.visitedLinkFillPaintColor() : svgStyle.visitedLinkStrokePaintColor());
            if (visitedColor.isValid())
                color = visitedColor.colorWithAlpha(color.alphaAsFloat());
        }
    }

    // If the primary resource is just a color, return immediately.
    LegacyRenderSVGResourceSolidColor* colorResource = LegacyRenderSVGResource::sharedSolidPaintingResource();
    if (paintType < SVGPaintType::URINone) {
        if (!inheritColorFromParentStyleIfNeeded(renderer, applyToFill, color))
            return nullptr;

        colorResource->setColor(color);
        return colorResource;
    }

    // FIXME: [LBSE] Add support for non-solid color resources in LBSE (gradient/pattern).
    SVGResources* resources = nullptr;
    if (!renderer.document().settings().layerBasedSVGEngineEnabled())
        resources = SVGResourcesCache::cachedResourcesForRenderer(renderer);

    // If no resources are associated with the given renderer, return the color resource.
    if (!resources) {
        if (paintType == SVGPaintType::URINone || !inheritColorFromParentStyleIfNeeded(renderer, applyToFill, color))
            return nullptr;

        colorResource->setColor(color);
        return colorResource;
    }

    // If the requested resource is not available, return the color resource.
    LegacyRenderSVGResource* uriResource = mode == RenderSVGResourceMode::ApplyToFill ? resources->fill() : resources->stroke();
    if (!uriResource) {
        if (!inheritColorFromParentStyleIfNeeded(renderer, applyToFill, color))
            return nullptr;

        colorResource->setColor(color);
        return colorResource;
    }

    // The paint server resource exists, though it may be invalid (pattern with width/height=0). Pass the fallback color to our caller
    // so it can use the solid color painting resource, if applyResource() on the URI resource failed.
    fallbackColor = color;
    return uriResource;
}

void LegacyRenderSVGResource::removeAllClientsFromCache(bool markForInvalidation)
{
    SingleThreadWeakHashSet<RenderObject> visitedRenderers;
    removeAllClientsFromCacheIfNeeded(markForInvalidation, &visitedRenderers);
}

LegacyRenderSVGResource* LegacyRenderSVGResource::fillPaintingResource(RenderElement& renderer, const RenderStyle& style, Color& fallbackColor)
{
    return requestPaintingResource(RenderSVGResourceMode::ApplyToFill, renderer, style, fallbackColor);
}

LegacyRenderSVGResource* LegacyRenderSVGResource::strokePaintingResource(RenderElement& renderer, const RenderStyle& style, Color& fallbackColor)
{
    return requestPaintingResource(RenderSVGResourceMode::ApplyToStroke, renderer, style, fallbackColor);
}

LegacyRenderSVGResourceSolidColor* LegacyRenderSVGResource::sharedSolidPaintingResource()
{
    static LegacyRenderSVGResourceSolidColor* s_sharedSolidPaintingResource = 0;
    if (!s_sharedSolidPaintingResource)
        s_sharedSolidPaintingResource = new LegacyRenderSVGResourceSolidColor;
    return s_sharedSolidPaintingResource;
}

static void removeFromCacheAndInvalidateDependencies(RenderElement& renderer, bool needsLayout, SingleThreadWeakHashSet<RenderObject>* visitedRenderers)
{
    if (auto* resources = SVGResourcesCache::cachedResourcesForRenderer(renderer)) {
        if (LegacyRenderSVGResourceFilter* filter = resources->filter())
            filter->removeClientFromCache(renderer);

        if (LegacyRenderSVGResourceMasker* masker = resources->masker())
            masker->removeClientFromCache(renderer);

        if (LegacyRenderSVGResourceClipper* clipper = resources->clipper())
            clipper->removeClientFromCache(renderer);
    }

    auto svgElement = dynamicDowncast<SVGElement>(renderer.protectedElement());
    if (!svgElement)
        return;

    for (auto& element : svgElement->referencingElements()) {
        if (auto* renderer = element->renderer()) {
            // We allow cycles in SVGDocumentExtensions reference sets in order to avoid expensive
            // reference graph adjustments on changes, so we need to break possible cycles here.
            static NeverDestroyed<WeakHashSet<SVGElement, WeakPtrImplWithEventTargetData>> invalidatingDependencies;
            if (UNLIKELY(!invalidatingDependencies.get().add(element.get()).isNewEntry)) {
                // Reference cycle: we are in process of invalidating this dependant.
                continue;
            }
            LegacyRenderSVGResource::markForLayoutAndParentResourceInvalidationIfNeeded(*renderer, needsLayout, visitedRenderers);
            invalidatingDependencies.get().remove(element.get());
        }
    }

    for (auto& cssClient : svgElement->referencingCSSClients()) {
        if (!cssClient)
            continue;
        cssClient->resourceChanged(*svgElement);
    }
}

void LegacyRenderSVGResource::markForLayoutAndParentResourceInvalidation(RenderObject& object, bool needsLayout)
{
    SingleThreadWeakHashSet<RenderObject> visitedRenderers;
    markForLayoutAndParentResourceInvalidationIfNeeded(object, needsLayout, &visitedRenderers);
}

void LegacyRenderSVGResource::markForLayoutAndParentResourceInvalidationIfNeeded(RenderObject& object, bool needsLayout, SingleThreadWeakHashSet<RenderObject>* visitedRenderers)
{
    ASSERT(object.node());
    if (object.document().settings().layerBasedSVGEngineEnabled()) {
        RELEASE_ASSERT_NOT_REACHED();
        return;
    }

    if (visitedRenderers) {
        auto addResult = visitedRenderers->add(object);
        if (!addResult.isNewEntry)
            return;
    }

    if (needsLayout && !object.renderTreeBeingDestroyed()) {
        // If we are inside the layout of an LegacyRenderSVGRoot, do not cross the SVG boundary to
        // invalidate the ancestor renderer because it may have finished its layout already.
        if (CheckedPtr svgRoot = dynamicDowncast<LegacyRenderSVGRoot>(object); svgRoot && svgRoot->isInLayout())
            svgRoot->setNeedsLayout(MarkOnlyThis);
        else {
            if (CheckedPtr element = dynamicDowncast<RenderElement>(object)) {
                auto svgRoot = SVGRenderSupport::findTreeRootObject(*element);
                if (!svgRoot || !svgRoot->isInLayout())
                    element->setNeedsLayout(MarkContainingBlockChain);
                else {
                    // We just want to re-layout the ancestors up to the RenderSVGRoot.
                    element->setNeedsLayout(MarkOnlyThis);
                    for (auto current = element->parent(); current != svgRoot; current = current->parent())
                        current->setNeedsLayout(MarkOnlyThis);
                    svgRoot->setNeedsLayout(MarkOnlyThis);
                }
            } else
                object.setNeedsLayout(MarkOnlyThis);
        }
    }

    if (CheckedPtr element = dynamicDowncast<RenderElement>(object))
        removeFromCacheAndInvalidateDependencies(*element, needsLayout, visitedRenderers);

    // Invalidate resources in ancestor chain, if needed.
    auto current = object.parent();
    while (current) {
        removeFromCacheAndInvalidateDependencies(*current, needsLayout, visitedRenderers);

        if (CheckedPtr container = dynamicDowncast<LegacyRenderSVGResourceContainer>(*current)) {
            // This will process the rest of the ancestors.
            bool markForInvalidation = true;
            container->removeAllClientsFromCacheIfNeeded(markForInvalidation, visitedRenderers);
            break;
        }

        current = current->parent();
    }
}

void LegacyRenderSVGResource::fillAndStrokePathOrShape(GraphicsContext& context, OptionSet<RenderSVGResourceMode> resourceMode, const Path* path, const RenderElement* shape)
{
    if (shape) {
        ASSERT(shape->isRenderOrLegacyRenderSVGShape());

        if (resourceMode.contains(RenderSVGResourceMode::ApplyToFill)) {
            if (CheckedPtr svgShape = dynamicDowncast<LegacyRenderSVGShape>(shape))
                svgShape->fillShape(context);
            else if (CheckedPtr svgShape = dynamicDowncast<RenderSVGShape>(shape))
                svgShape->fillShape(context);
        }

        if (resourceMode.contains(RenderSVGResourceMode::ApplyToStroke)) {
            if (CheckedPtr svgShape = dynamicDowncast<LegacyRenderSVGShape>(shape))
                svgShape->strokeShape(context);
            else if (CheckedPtr svgShape = dynamicDowncast<RenderSVGShape>(shape))
                svgShape->strokeShape(context);
        }

        return;
    }

    if (!path)
        return;

    if (resourceMode.contains(RenderSVGResourceMode::ApplyToFill))
        context.fillPath(*path);
    if (resourceMode.contains(RenderSVGResourceMode::ApplyToStroke))
        context.strokePath(*path);
}

}