CG gradient renderer cache moved out of Gradient
Component: WebCore Graphics | 458ceed
Source/WebCore/platform/graphics/cg/GradientCG.cpp
Gradient는 linear, radial, conic gradient를 플랫폼과 무관하게 기술하는 WebCore의 객체입니다. CoreGraphics에서 이를 그리려면 먼저 destination color space에 맞는 CGGradient로 변환해야 합니다. 기존에는 Gradient::paint()가 필요한 시점에 이 객체를 lazy하게 생성하고, Gradient 내부에 저장해 두었습니다. 한편 GPU process에서는 gradient가 RemoteResourceCache에 캐시되고, IPC 상에서는 identifier로 참조됩니다. 그래서 동일한 gradient가 backend work queue에서 그려지는 동안, snapshot replay 과정에서 main thread에서도 그려질 수 있습니다.
먼저 Gradient마다 갖고 있던 mutable m_platformRenderer가 제거되고, static lookup 모음인 GradientCacheCG로 대체되었습니다. paint()는 이제 그릴 CGGradientRef를 인자로 받으며, 실제로도 const로 동작하게 되었습니다. Non-transient gradient(예: SVG)와 sampled gradient(non-sRGB interpolation 또는 "none" component를 가진 경우)는 process 전역 LRU cache에 저장됩니다. 이 cache는 lock으로 보호되며 512개의 bucket을 갖습니다. Key는 interpolation method, color stop, color space로 구성되고, gradient identifier를 key로 사용하는 fast path도 함께 존재합니다. 그 밖의 transient gradient는 paint 시점에 CGGradient를 생성합니다. 이때 GraphicsContextCG는 마지막으로 사용한 gradient를 보관하며, 새로운 fillRect(const FloatRect&, const Gradient&) override도 추가되었습니다. 또한 WebKit::RemoteGradientIdentifier 대신 process 단위의 WebCore::GradientIdentifier가 도입되었고, cacheGradient, releaseGradient, setFillCachedGradient도 이에 맞춰 변경되었습니다.
Significance
work queue와 main thread가 같은 gradient를 그리는 상황에서, paint 과정이 저장된 renderer를 lazy하게 수정하며 발생하던 GPU process의 data race가 제거되었습니다. 다만 이를 위해 lock으로 보호되는 전역 공유 상태와 새로운 IPC identifier 체계가 함께 도입되었습니다.
Audit directions
가장 먼저 살펴봐야 할 곳은 새로 도입된 process 전역 cache입니다. Context가 여러 draw에 걸쳐 보유하는 CGGradient를 기준으로, cache entry의 locking과 lifetime이 올바르게 관리되는지 점검할 필요가 있습니다. Context state가 변경될 때 GraphicsContextCG의 last-gradient cache가 어떻게 유지되는지도 함께 확인해야 합니다. 한편 identifier fast path는 color stop 비교를 생략합니다. 따라서 identifier가 재사용되거나 해제되는 경우 어떤 동작이 발생하는지 살펴볼 필요가 있습니다. 비정상적인 stop이나 color space가 주어졌을 때의 CGGradientKey::matches 판정과 bucket collision도 점검 대상입니다. 마지막으로 RemoteResourceCache는 이제 다른 identifier 타입을 key로 사용합니다. 알 수 없거나 stale한 gradient identifier를 담은 message가 안전하게 처리되는지 확인해야 합니다.