← All reports

CG gradient renderer cache moved out of Gradient

Component: WebCore Graphics | 458ceed

Source/WebCore/platform/graphics/cg/GradientCG.cpp

void Gradient::fill(GraphicsContext& context, const FloatRect& rect) const
{
context.clip(rect);
- paint(context);
+ paint(protect(context.platformContext()), GradientCacheCG::cgGradient(*this, GradientCacheCG::colorSpaceForDestination(context.colorSpace())).get());
}
 
-void Gradient::paint(CGContextRef platformContext, std::optional<ColorSpace> colorSpace) const
+void Gradient::paint(CGContextRef platformContext, CGGradientRef gradient) const
{
- if (!m_platformRenderer || m_platformRenderer->colorSpace() != colorSpace)
- m_platformRenderer = GradientRendererCG { m_colorInterpolationMethod, m_stops, colorSpace };
...
- m_platformRenderer->drawLinearGradient(platformContext, data.point0, data.point1, extendOptions);
+ CGContextDrawLinearGradient(platformContext, gradient, data.point0, data.point1, extendOptions);

A Gradient is WebCore's platform-independent description of a linear, radial or conic gradient. On CoreGraphics it must be turned into a CGGradient for the destination color space before drawing. Previously Gradient::paint() built that object lazily and stored it in the Gradient itself. In the GPU process, gradients are cached in the RemoteResourceCache and referenced by identifier over IPC, so the same gradient can be painted from a backend work queue and from the main thread during snapshot replay.

The per-Gradient mutable m_platformRenderer is replaced by GradientCacheCG, a set of static lookups, and paint() now takes the CGGradientRef to draw and is genuinely const. Non-transient gradients (e.g. SVG) and sampled ones (non-sRGB interpolation or "none" components) live in a process-wide, lock-protected LRU cache with 512 buckets. It is keyed by interpolation method, color stops and color space, with a fast path keyed by gradient identifier. Other transient gradients get a CGGradient at paint time, and GraphicsContextCG keeps the last one alongside a new fillRect(const FloatRect&, const Gradient&) override. A per-process WebCore::GradientIdentifier replaces WebKit::RemoteGradientIdentifier, and cacheGradient, releaseGradient and setFillCachedGradient change to match.

This removes a GPU-process data race in which painting lazily mutated a stored renderer while the work queue and main thread painted the same gradient. It does so by introducing shared, lock-protected global state and a new IPC identifier scheme.

The new process-wide cache is the place to look. Check locking and lifetime of cache entries against the CGGradient a context holds across draws, including the last-gradient cache in GraphicsContextCG across context state changes. The identifier fast path skips comparing color stops, so examine what happens when identifiers are reused or released. Check CGGradientKey::matches and bucket collisions with unusual stops or color spaces. In RemoteResourceCache, now keyed by a different identifier type, confirm that messages naming unknown or stale gradient identifiers are handled safely.